blob: 4dcc01b2532c842dddc720d31b528f6be5532c02 [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 Melo76b31a22017-04-18 12:26:44 -03004#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03005#include <errno.h>
Arnaldo Carvalho de Melo86a5e0c2017-04-19 19:03:14 -03006#include <sys/ioctl.h>
Arnaldo Carvalho de Melobafae982018-01-22 16:42:16 -03007#include <sys/types.h>
8#include <sys/stat.h>
9#include <fcntl.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030010#include <sys/param.h>
Arnaldo Carvalho de Melob0742e92017-04-18 11:08:10 -030011#include "term.h"
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -030012#include "../perf.h"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020013#include "evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020014#include "evsel.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060015#include <subcmd/parse-options.h>
Ingo Molnar8ad8db32009-05-26 11:10:09 +020016#include "parse-events.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060017#include <subcmd/exec-cmd.h>
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030018#include "string2.h"
Arnaldo Carvalho de Melo8ec20b12017-04-18 10:57:25 -030019#include "strlist.h"
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -030020#include "symbol.h"
Jason Baron5beeded2009-07-21 14:16:29 -040021#include "cache.h"
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020022#include "header.h"
Wang Nan84c86ca2015-10-14 12:41:14 +000023#include "bpf-loader.h"
Masami Hiramatsu6e81c742014-08-14 02:22:36 +000024#include "debug.h"
Jiri Olsa592d5a62015-09-02 09:56:34 +020025#include <api/fs/tracing_path.h>
Zheng Yanac20de6f2012-06-15 14:31:39 +080026#include "parse-events-bison.h"
Jiri Olsa90e2b222012-06-15 14:31:40 +080027#define YY_EXTRA_TYPE int
Jiri Olsa89812fc2012-03-15 20:09:15 +010028#include "parse-events-flex.h"
Jiri Olsa5f537a22012-03-15 20:09:18 +010029#include "pmu.h"
Namhyung Kimb41f1ce2013-08-27 11:41:53 +090030#include "thread_map.h"
Jiri Olsaf30a79b2015-06-23 00:36:04 +020031#include "cpumap.h"
Masami Hiramatsu40218da2016-06-24 18:06:46 +090032#include "probe-file.h"
Jiri Olsab39b8392015-04-22 21:10:16 +020033#include "asm/bug.h"
Andi Kleenac12f672016-10-12 14:02:06 -070034#include "util/parse-branch-options.h"
Andi Kleen71b0acc2017-08-31 12:40:32 -070035#include "metricgroup.h"
Jiri Olsa89812fc2012-03-15 20:09:15 +010036
37#define MAX_NAME_LEN 100
Ingo Molnar8ad8db32009-05-26 11:10:09 +020038
Jiri Olsa82ba1f22012-05-21 09:12:50 +020039#ifdef PARSER_DEBUG
40extern int parse_events_debug;
41#endif
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -030042int parse_events_parse(void *parse_state, void *scanner);
He Kuange637d172015-09-28 03:52:16 +000043static int get_config_terms(struct list_head *head_config,
44 struct list_head *head_terms __maybe_unused);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +020045
Kan Liangdcb4e102014-10-07 11:08:50 -040046static struct perf_pmu_event_symbol *perf_pmu_events_list;
47/*
48 * The variable indicates the number of supported pmu event symbols.
49 * 0 means not initialized and ready to init
50 * -1 means failed to init, don't try anymore
51 * >0 is the number of supported pmu event symbols
52 */
53static int perf_pmu_events_list_num;
54
Yunlong Song705750f2015-02-27 18:21:27 +080055struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
Jiri Olsa1dc127602012-07-04 00:00:44 +020056 [PERF_COUNT_HW_CPU_CYCLES] = {
57 .symbol = "cpu-cycles",
58 .alias = "cycles",
59 },
60 [PERF_COUNT_HW_INSTRUCTIONS] = {
61 .symbol = "instructions",
62 .alias = "",
63 },
64 [PERF_COUNT_HW_CACHE_REFERENCES] = {
65 .symbol = "cache-references",
66 .alias = "",
67 },
68 [PERF_COUNT_HW_CACHE_MISSES] = {
69 .symbol = "cache-misses",
70 .alias = "",
71 },
72 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
73 .symbol = "branch-instructions",
74 .alias = "branches",
75 },
76 [PERF_COUNT_HW_BRANCH_MISSES] = {
77 .symbol = "branch-misses",
78 .alias = "",
79 },
80 [PERF_COUNT_HW_BUS_CYCLES] = {
81 .symbol = "bus-cycles",
82 .alias = "",
83 },
84 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
85 .symbol = "stalled-cycles-frontend",
86 .alias = "idle-cycles-frontend",
87 },
88 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
89 .symbol = "stalled-cycles-backend",
90 .alias = "idle-cycles-backend",
91 },
92 [PERF_COUNT_HW_REF_CPU_CYCLES] = {
93 .symbol = "ref-cycles",
94 .alias = "",
95 },
96};
Ingo Molnar8ad8db32009-05-26 11:10:09 +020097
Yunlong Song705750f2015-02-27 18:21:27 +080098struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
Jiri Olsa1dc127602012-07-04 00:00:44 +020099 [PERF_COUNT_SW_CPU_CLOCK] = {
100 .symbol = "cpu-clock",
101 .alias = "",
102 },
103 [PERF_COUNT_SW_TASK_CLOCK] = {
104 .symbol = "task-clock",
105 .alias = "",
106 },
107 [PERF_COUNT_SW_PAGE_FAULTS] = {
108 .symbol = "page-faults",
109 .alias = "faults",
110 },
111 [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
112 .symbol = "context-switches",
113 .alias = "cs",
114 },
115 [PERF_COUNT_SW_CPU_MIGRATIONS] = {
116 .symbol = "cpu-migrations",
117 .alias = "migrations",
118 },
119 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
120 .symbol = "minor-faults",
121 .alias = "",
122 },
123 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
124 .symbol = "major-faults",
125 .alias = "",
126 },
127 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
128 .symbol = "alignment-faults",
129 .alias = "",
130 },
131 [PERF_COUNT_SW_EMULATION_FAULTS] = {
132 .symbol = "emulation-faults",
133 .alias = "",
134 },
Adrian Hunterd22d1a22013-08-31 21:50:52 +0300135 [PERF_COUNT_SW_DUMMY] = {
136 .symbol = "dummy",
137 .alias = "",
138 },
Arnaldo Carvalho de Melobae9cc42015-11-27 15:54:33 -0300139 [PERF_COUNT_SW_BPF_OUTPUT] = {
140 .symbol = "bpf-output",
141 .alias = "",
142 },
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200143};
144
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200145#define __PERF_EVENT_FIELD(config, name) \
146 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
Ingo Molnar52425192009-05-26 09:17:18 +0200147
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200148#define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200149#define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200150#define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200151#define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
Ingo Molnar52425192009-05-26 09:17:18 +0200152
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300153#define for_each_subsystem(sys_dir, sys_dirent) \
154 while ((sys_dirent = readdir(sys_dir)) != NULL) \
155 if (sys_dirent->d_type == DT_DIR && \
156 (strcmp(sys_dirent->d_name, ".")) && \
157 (strcmp(sys_dirent->d_name, "..")))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400158
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -0300159static int tp_event_has_id(const char *dir_path, struct dirent *evt_dir)
Peter Zijlstraae07b632009-08-06 16:48:54 +0200160{
161 char evt_path[MAXPATHLEN];
162 int fd;
163
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -0300164 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, evt_dir->d_name);
Peter Zijlstraae07b632009-08-06 16:48:54 +0200165 fd = open(evt_path, O_RDONLY);
166 if (fd < 0)
167 return -EINVAL;
168 close(fd);
169
170 return 0;
171}
172
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -0300173#define for_each_event(dir_path, evt_dir, evt_dirent) \
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300174 while ((evt_dirent = readdir(evt_dir)) != NULL) \
175 if (evt_dirent->d_type == DT_DIR && \
176 (strcmp(evt_dirent->d_name, ".")) && \
177 (strcmp(evt_dirent->d_name, "..")) && \
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -0300178 (!tp_event_has_id(dir_path, evt_dirent)))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400179
Li Zefan270bbbe2009-09-17 16:34:51 +0800180#define MAX_EVENT_LENGTH 512
Jason Baronf6bdafe2009-07-21 12:20:22 -0400181
Jason Baronf6bdafe2009-07-21 12:20:22 -0400182
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200183struct tracepoint_path *tracepoint_id_to_path(u64 config)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400184{
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200185 struct tracepoint_path *path = NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400186 DIR *sys_dir, *evt_dir;
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300187 struct dirent *sys_dirent, *evt_dirent;
Stephane Eranian8aa8a7c2012-03-13 16:51:02 +0100188 char id_buf[24];
Eric Dumazet725b1362009-09-24 15:39:09 +0200189 int fd;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400190 u64 id;
191 char evt_path[MAXPATHLEN];
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300192 char *dir_path;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400193
Arnaldo Carvalho de Melo7014e0e2018-05-17 14:42:39 -0300194 sys_dir = tracing_events__opendir();
Jason Baronf6bdafe2009-07-21 12:20:22 -0400195 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200196 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400197
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300198 for_each_subsystem(sys_dir, sys_dirent) {
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300199 dir_path = get_events_file(sys_dirent->d_name);
200 if (!dir_path)
201 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200202 evt_dir = opendir(dir_path);
203 if (!evt_dir)
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300204 goto next;
Eric Dumazet725b1362009-09-24 15:39:09 +0200205
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -0300206 for_each_event(dir_path, evt_dir, evt_dirent) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200207
Jiri Olsa77f18152018-03-19 09:29:01 +0100208 scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
209 evt_dirent->d_name);
Eric Dumazet725b1362009-09-24 15:39:09 +0200210 fd = open(evt_path, O_RDONLY);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400211 if (fd < 0)
212 continue;
213 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
214 close(fd);
215 continue;
216 }
217 close(fd);
218 id = atoll(id_buf);
219 if (id == config) {
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300220 put_events_file(dir_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400221 closedir(evt_dir);
222 closedir(sys_dir);
Julia Lawall59b4cae2009-12-06 10:16:30 +0100223 path = zalloc(sizeof(*path));
Taeung Song5aa365f2017-02-01 21:34:05 +0900224 if (!path)
225 return NULL;
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200226 path->system = malloc(MAX_EVENT_LENGTH);
227 if (!path->system) {
228 free(path);
229 return NULL;
230 }
231 path->name = malloc(MAX_EVENT_LENGTH);
232 if (!path->name) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300233 zfree(&path->system);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200234 free(path);
235 return NULL;
236 }
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300237 strncpy(path->system, sys_dirent->d_name,
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200238 MAX_EVENT_LENGTH);
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300239 strncpy(path->name, evt_dirent->d_name,
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200240 MAX_EVENT_LENGTH);
241 return path;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400242 }
243 }
244 closedir(evt_dir);
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300245next:
246 put_events_file(dir_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400247 }
248
Jason Baronf6bdafe2009-07-21 12:20:22 -0400249 closedir(sys_dir);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200250 return NULL;
251}
252
Namhyung Kime7c93f02013-06-26 16:14:05 +0900253struct tracepoint_path *tracepoint_name_to_path(const char *name)
254{
255 struct tracepoint_path *path = zalloc(sizeof(*path));
256 char *str = strchr(name, ':');
257
258 if (path == NULL || str == NULL) {
259 free(path);
260 return NULL;
261 }
262
263 path->system = strndup(name, str - name);
264 path->name = strdup(str+1);
265
266 if (path->system == NULL || path->name == NULL) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300267 zfree(&path->system);
268 zfree(&path->name);
Taeung Song506fde12017-02-01 21:34:06 +0900269 zfree(&path);
Namhyung Kime7c93f02013-06-26 16:14:05 +0900270 }
271
272 return path;
273}
274
David Ahern1424dc92011-03-09 22:23:28 -0700275const char *event_type(int type)
276{
277 switch (type) {
278 case PERF_TYPE_HARDWARE:
279 return "hardware";
280
281 case PERF_TYPE_SOFTWARE:
282 return "software";
283
284 case PERF_TYPE_TRACEPOINT:
285 return "tracepoint";
286
287 case PERF_TYPE_HW_CACHE:
288 return "hardware-cache";
289
290 default:
291 break;
292 }
293
294 return "unknown";
295}
296
Wang Nane814fdd2016-02-19 11:43:59 +0000297static int parse_events__is_name_term(struct parse_events_term *term)
298{
299 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
300}
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800301
Wang Nane814fdd2016-02-19 11:43:59 +0000302static char *get_config_name(struct list_head *head_terms)
303{
304 struct parse_events_term *term;
305
306 if (!head_terms)
307 return NULL;
308
309 list_for_each_entry(term, head_terms, list)
310 if (parse_events__is_name_term(term))
311 return term->val.str;
312
313 return NULL;
314}
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800315
Stephane Eranian410136f2013-11-12 17:58:49 +0100316static struct perf_evsel *
317__add_event(struct list_head *list, int *idx,
318 struct perf_event_attr *attr,
Mark Rutland66ec11912017-10-06 19:38:22 +0100319 char *name, struct perf_pmu *pmu,
Arnaldo Carvalho de Melo63ce8442017-08-31 15:32:18 -0300320 struct list_head *config_terms, bool auto_merge_stats)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100321{
322 struct perf_evsel *evsel;
Mark Rutland66ec11912017-10-06 19:38:22 +0100323 struct cpu_map *cpus = pmu ? pmu->cpus : NULL;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100324
325 event_attr_init(attr);
326
Taeung Song75fc5ae2017-02-01 21:34:04 +0900327 evsel = perf_evsel__new_idx(attr, *idx);
David Ahernc5cd8ac2013-07-02 13:27:25 -0600328 if (!evsel)
Stephane Eranian410136f2013-11-12 17:58:49 +0100329 return NULL;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100330
Taeung Song75fc5ae2017-02-01 21:34:04 +0900331 (*idx)++;
Jiri Olsae3ba76d2017-02-27 10:48:18 +0100332 evsel->cpus = cpu_map__get(cpus);
333 evsel->own_cpus = cpu_map__get(cpus);
Mark Rutland66ec11912017-10-06 19:38:22 +0100334 evsel->system_wide = pmu ? pmu->is_uncore : false;
Arnaldo Carvalho de Melo63ce8442017-08-31 15:32:18 -0300335 evsel->auto_merge_stats = auto_merge_stats;
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200336
Arnaldo Carvalho de Melo9db17632012-06-12 13:45:00 -0300337 if (name)
338 evsel->name = strdup(name);
Jiri Olsa930a2e22015-07-29 05:42:10 -0400339
340 if (config_terms)
341 list_splice(config_terms, &evsel->config_terms);
342
Jiri Olsab847cbd2012-05-21 09:12:51 +0200343 list_add_tail(&evsel->node, list);
Stephane Eranian410136f2013-11-12 17:58:49 +0100344 return evsel;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100345}
346
David Ahernc5cd8ac2013-07-02 13:27:25 -0600347static int add_event(struct list_head *list, int *idx,
Jiri Olsa930a2e22015-07-29 05:42:10 -0400348 struct perf_event_attr *attr, char *name,
349 struct list_head *config_terms)
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800350{
Arnaldo Carvalho de Melo63ce8442017-08-31 15:32:18 -0300351 return __add_event(list, idx, attr, name, NULL, config_terms, false) ? 0 : -ENOMEM;
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800352}
353
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300354static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
Ingo Molnar8326f442009-06-05 20:22:46 +0200355{
356 int i, j;
Paul Mackerras61c45982009-07-01 13:04:34 +1000357 int n, longest = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200358
359 for (i = 0; i < size; i++) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300360 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
Paul Mackerras61c45982009-07-01 13:04:34 +1000361 n = strlen(names[i][j]);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100362 if (n > longest && !strncasecmp(str, names[i][j], n))
Paul Mackerras61c45982009-07-01 13:04:34 +1000363 longest = n;
364 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100365 if (longest > 0)
Paul Mackerras61c45982009-07-01 13:04:34 +1000366 return i;
Ingo Molnar8326f442009-06-05 20:22:46 +0200367 }
368
Ingo Molnar89536452009-06-06 21:04:17 +0200369 return -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200370}
371
Wang Nan43d0b972016-02-19 11:44:01 +0000372typedef int config_term_func_t(struct perf_event_attr *attr,
373 struct parse_events_term *term,
374 struct parse_events_error *err);
375static int config_term_common(struct perf_event_attr *attr,
376 struct parse_events_term *term,
377 struct parse_events_error *err);
378static int config_attr(struct perf_event_attr *attr,
379 struct list_head *head,
380 struct parse_events_error *err,
381 config_term_func_t config_term);
382
David Ahernc5cd8ac2013-07-02 13:27:25 -0600383int parse_events_add_cache(struct list_head *list, int *idx,
Wang Nan43d0b972016-02-19 11:44:01 +0000384 char *type, char *op_result1, char *op_result2,
Arnaldo Carvalho de Melo2c97b0d2016-02-19 19:47:04 -0300385 struct parse_events_error *err,
Wang Nan43d0b972016-02-19 11:44:01 +0000386 struct list_head *head_config)
Ingo Molnar8326f442009-06-05 20:22:46 +0200387{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100388 struct perf_event_attr attr;
Wang Nan43d0b972016-02-19 11:44:01 +0000389 LIST_HEAD(config_terms);
390 char name[MAX_NAME_LEN], *config_name;
Paul Mackerras61c45982009-07-01 13:04:34 +1000391 int cache_type = -1, cache_op = -1, cache_result = -1;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100392 char *op_result[2] = { op_result1, op_result2 };
393 int i, n;
Ingo Molnar8326f442009-06-05 20:22:46 +0200394
Ingo Molnar8326f442009-06-05 20:22:46 +0200395 /*
396 * No fallback - if we cannot get a clear cache type
397 * then bail out:
398 */
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300399 cache_type = parse_aliases(type, perf_evsel__hw_cache,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100400 PERF_COUNT_HW_CACHE_MAX);
Ingo Molnar8326f442009-06-05 20:22:46 +0200401 if (cache_type == -1)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100402 return -EINVAL;
Ingo Molnar8326f442009-06-05 20:22:46 +0200403
Wang Nan43d0b972016-02-19 11:44:01 +0000404 config_name = get_config_name(head_config);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100405 n = snprintf(name, MAX_NAME_LEN, "%s", type);
406
407 for (i = 0; (i < 2) && (op_result[i]); i++) {
408 char *str = op_result[i];
409
Jiri Olsa275ef382012-09-05 19:51:33 +0200410 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
Paul Mackerras61c45982009-07-01 13:04:34 +1000411
412 if (cache_op == -1) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300413 cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100414 PERF_COUNT_HW_CACHE_OP_MAX);
Paul Mackerras61c45982009-07-01 13:04:34 +1000415 if (cache_op >= 0) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300416 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
Jiri Olsa89812fc2012-03-15 20:09:15 +0100417 return -EINVAL;
Paul Mackerras61c45982009-07-01 13:04:34 +1000418 continue;
419 }
420 }
421
422 if (cache_result == -1) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300423 cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
424 PERF_COUNT_HW_CACHE_RESULT_MAX);
Paul Mackerras61c45982009-07-01 13:04:34 +1000425 if (cache_result >= 0)
426 continue;
427 }
Paul Mackerras61c45982009-07-01 13:04:34 +1000428 }
429
Ingo Molnar8326f442009-06-05 20:22:46 +0200430 /*
431 * Fall back to reads:
432 */
Ingo Molnar89536452009-06-06 21:04:17 +0200433 if (cache_op == -1)
434 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
Ingo Molnar8326f442009-06-05 20:22:46 +0200435
Ingo Molnar8326f442009-06-05 20:22:46 +0200436 /*
437 * Fall back to accesses:
438 */
439 if (cache_result == -1)
440 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
441
Jiri Olsa89812fc2012-03-15 20:09:15 +0100442 memset(&attr, 0, sizeof(attr));
443 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
444 attr.type = PERF_TYPE_HW_CACHE;
Wang Nan43d0b972016-02-19 11:44:01 +0000445
446 if (head_config) {
Arnaldo Carvalho de Melo2c97b0d2016-02-19 19:47:04 -0300447 if (config_attr(&attr, head_config, err,
Wang Nan43d0b972016-02-19 11:44:01 +0000448 config_term_common))
449 return -EINVAL;
450
451 if (get_config_terms(head_config, &config_terms))
452 return -ENOMEM;
453 }
454 return add_event(list, idx, &attr, config_name ? : name, &config_terms);
Ingo Molnar8326f442009-06-05 20:22:46 +0200455}
456
Jiri Olsa272ed292015-09-29 17:05:31 +0200457static void tracepoint_error(struct parse_events_error *e, int err,
Wang Nan8c619d62016-07-13 10:44:03 +0000458 const char *sys, const char *name)
Jiri Olsa19658172015-09-07 10:38:07 +0200459{
460 char help[BUFSIZ];
461
Adrian Hunterec183d22016-01-26 14:05:20 +0200462 if (!e)
463 return;
464
Jiri Olsa19658172015-09-07 10:38:07 +0200465 /*
466 * We get error directly from syscall errno ( > 0),
467 * or from encoded pointer's error ( < 0).
468 */
469 err = abs(err);
470
471 switch (err) {
472 case EACCES:
Jiri Olsa272ed292015-09-29 17:05:31 +0200473 e->str = strdup("can't access trace events");
Jiri Olsa19658172015-09-07 10:38:07 +0200474 break;
475 case ENOENT:
Jiri Olsa272ed292015-09-29 17:05:31 +0200476 e->str = strdup("unknown tracepoint");
Jiri Olsa19658172015-09-07 10:38:07 +0200477 break;
478 default:
Jiri Olsa272ed292015-09-29 17:05:31 +0200479 e->str = strdup("failed to add tracepoint");
Jiri Olsa19658172015-09-07 10:38:07 +0200480 break;
481 }
482
483 tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
Jiri Olsa272ed292015-09-29 17:05:31 +0200484 e->help = strdup(help);
Jiri Olsa19658172015-09-07 10:38:07 +0200485}
486
David Ahernc5cd8ac2013-07-02 13:27:25 -0600487static int add_tracepoint(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +0000488 const char *sys_name, const char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200489 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000490 struct list_head *head_config)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200491{
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300492 struct perf_evsel *evsel;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200493
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300494 evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
Jiri Olsa19658172015-09-07 10:38:07 +0200495 if (IS_ERR(evsel)) {
Jiri Olsa272ed292015-09-29 17:05:31 +0200496 tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200497 return PTR_ERR(evsel);
Jiri Olsa19658172015-09-07 10:38:07 +0200498 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200499
He Kuange637d172015-09-28 03:52:16 +0000500 if (head_config) {
501 LIST_HEAD(config_terms);
502
503 if (get_config_terms(head_config, &config_terms))
504 return -ENOMEM;
505 list_splice(&config_terms, &evsel->config_terms);
506 }
507
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300508 list_add_tail(&evsel->node, list);
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300509 return 0;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200510}
511
David Ahernc5cd8ac2013-07-02 13:27:25 -0600512static int add_tracepoint_multi_event(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +0000513 const char *sys_name, const char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200514 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000515 struct list_head *head_config)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200516{
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300517 char *evt_path;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200518 struct dirent *evt_ent;
519 DIR *evt_dir;
Jiri Olsa27bf90b2015-10-05 21:31:17 +0200520 int ret = 0, found = 0;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200521
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300522 evt_path = get_events_file(sys_name);
523 if (!evt_path) {
524 tracepoint_error(err, errno, sys_name, evt_name);
525 return -1;
526 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200527 evt_dir = opendir(evt_path);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200528 if (!evt_dir) {
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300529 put_events_file(evt_path);
Jiri Olsa272ed292015-09-29 17:05:31 +0200530 tracepoint_error(err, errno, sys_name, evt_name);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100531 return -1;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200532 }
533
Jiri Olsa89812fc2012-03-15 20:09:15 +0100534 while (!ret && (evt_ent = readdir(evt_dir))) {
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200535 if (!strcmp(evt_ent->d_name, ".")
536 || !strcmp(evt_ent->d_name, "..")
537 || !strcmp(evt_ent->d_name, "enable")
538 || !strcmp(evt_ent->d_name, "filter"))
539 continue;
540
Jiri Olsa89812fc2012-03-15 20:09:15 +0100541 if (!strglobmatch(evt_ent->d_name, evt_name))
Masami Hiramatsufb1d2ed2010-01-05 17:47:17 -0500542 continue;
543
Jiri Olsa27bf90b2015-10-05 21:31:17 +0200544 found++;
545
He Kuange637d172015-09-28 03:52:16 +0000546 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200547 err, head_config);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200548 }
549
Jiri Olsa27bf90b2015-10-05 21:31:17 +0200550 if (!found) {
551 tracepoint_error(err, ENOENT, sys_name, evt_name);
552 ret = -1;
553 }
554
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300555 put_events_file(evt_path);
Jiri Olsa0bd3f082012-12-17 14:08:36 +0100556 closedir(evt_dir);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100557 return ret;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200558}
559
David Ahernc5cd8ac2013-07-02 13:27:25 -0600560static int add_tracepoint_event(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +0000561 const char *sys_name, const char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200562 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000563 struct list_head *head_config)
Jiri Olsaf35488f2012-12-17 14:08:37 +0100564{
565 return strpbrk(evt_name, "*?") ?
He Kuange637d172015-09-28 03:52:16 +0000566 add_tracepoint_multi_event(list, idx, sys_name, evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200567 err, head_config) :
He Kuange637d172015-09-28 03:52:16 +0000568 add_tracepoint(list, idx, sys_name, evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200569 err, head_config);
Jiri Olsaf35488f2012-12-17 14:08:37 +0100570}
571
David Ahernc5cd8ac2013-07-02 13:27:25 -0600572static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +0000573 const char *sys_name, const char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200574 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000575 struct list_head *head_config)
Jiri Olsaf35488f2012-12-17 14:08:37 +0100576{
577 struct dirent *events_ent;
578 DIR *events_dir;
579 int ret = 0;
580
Arnaldo Carvalho de Melo7014e0e2018-05-17 14:42:39 -0300581 events_dir = tracing_events__opendir();
Jiri Olsaf35488f2012-12-17 14:08:37 +0100582 if (!events_dir) {
Jiri Olsa272ed292015-09-29 17:05:31 +0200583 tracepoint_error(err, errno, sys_name, evt_name);
Jiri Olsaf35488f2012-12-17 14:08:37 +0100584 return -1;
585 }
586
587 while (!ret && (events_ent = readdir(events_dir))) {
588 if (!strcmp(events_ent->d_name, ".")
589 || !strcmp(events_ent->d_name, "..")
590 || !strcmp(events_ent->d_name, "enable")
591 || !strcmp(events_ent->d_name, "header_event")
592 || !strcmp(events_ent->d_name, "header_page"))
593 continue;
594
595 if (!strglobmatch(events_ent->d_name, sys_name))
596 continue;
597
598 ret = add_tracepoint_event(list, idx, events_ent->d_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200599 evt_name, err, head_config);
Jiri Olsaf35488f2012-12-17 14:08:37 +0100600 }
601
602 closedir(events_dir);
603 return ret;
604}
605
Wang Nan4edf30e2015-10-14 12:41:17 +0000606struct __add_bpf_event_param {
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300607 struct parse_events_state *parse_state;
Wang Nan4edf30e2015-10-14 12:41:17 +0000608 struct list_head *list;
Wang Nan95088a52016-02-22 09:10:36 +0000609 struct list_head *head_config;
Wang Nan4edf30e2015-10-14 12:41:17 +0000610};
611
Wang Nancd102d72016-07-13 10:44:04 +0000612static int add_bpf_event(const char *group, const char *event, int fd,
Wang Nan4edf30e2015-10-14 12:41:17 +0000613 void *_param)
614{
615 LIST_HEAD(new_evsels);
616 struct __add_bpf_event_param *param = _param;
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300617 struct parse_events_state *parse_state = param->parse_state;
Wang Nan4edf30e2015-10-14 12:41:17 +0000618 struct list_head *list = param->list;
Wang Nan1f45b1d2015-10-14 12:41:18 +0000619 struct perf_evsel *pos;
Wang Nan4edf30e2015-10-14 12:41:17 +0000620 int err;
621
622 pr_debug("add bpf event %s:%s and attach bpf program %d\n",
Wang Nancd102d72016-07-13 10:44:04 +0000623 group, event, fd);
Wang Nan4edf30e2015-10-14 12:41:17 +0000624
Arnaldo Carvalho de Melo5d369a72017-08-17 14:13:25 -0300625 err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group,
626 event, parse_state->error,
Wang Nan95088a52016-02-22 09:10:36 +0000627 param->head_config);
Wang Nan4edf30e2015-10-14 12:41:17 +0000628 if (err) {
629 struct perf_evsel *evsel, *tmp;
630
631 pr_debug("Failed to add BPF event %s:%s\n",
Wang Nancd102d72016-07-13 10:44:04 +0000632 group, event);
Wang Nan4edf30e2015-10-14 12:41:17 +0000633 list_for_each_entry_safe(evsel, tmp, &new_evsels, node) {
634 list_del(&evsel->node);
635 perf_evsel__delete(evsel);
636 }
637 return err;
638 }
Wang Nancd102d72016-07-13 10:44:04 +0000639 pr_debug("adding %s:%s\n", group, event);
Wang Nan4edf30e2015-10-14 12:41:17 +0000640
Wang Nan1f45b1d2015-10-14 12:41:18 +0000641 list_for_each_entry(pos, &new_evsels, node) {
642 pr_debug("adding %s:%s to %p\n",
Wang Nancd102d72016-07-13 10:44:04 +0000643 group, event, pos);
Wang Nan1f45b1d2015-10-14 12:41:18 +0000644 pos->bpf_fd = fd;
645 }
Wang Nan4edf30e2015-10-14 12:41:17 +0000646 list_splice(&new_evsels, list);
647 return 0;
648}
649
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300650int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
Wang Nan84c86ca2015-10-14 12:41:14 +0000651 struct list_head *list,
Wang Nan95088a52016-02-22 09:10:36 +0000652 struct bpf_object *obj,
653 struct list_head *head_config)
Wang Nan84c86ca2015-10-14 12:41:14 +0000654{
655 int err;
656 char errbuf[BUFSIZ];
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300657 struct __add_bpf_event_param param = {parse_state, list, head_config};
Wang Nanaa3abf32015-10-14 12:41:15 +0000658 static bool registered_unprobe_atexit = false;
Wang Nan84c86ca2015-10-14 12:41:14 +0000659
660 if (IS_ERR(obj) || !obj) {
661 snprintf(errbuf, sizeof(errbuf),
662 "Internal error: load bpf obj with NULL");
663 err = -EINVAL;
664 goto errout;
665 }
666
667 /*
Wang Nanaa3abf32015-10-14 12:41:15 +0000668 * Register atexit handler before calling bpf__probe() so
669 * bpf__probe() don't need to unprobe probe points its already
670 * created when failure.
671 */
672 if (!registered_unprobe_atexit) {
673 atexit(bpf__clear);
674 registered_unprobe_atexit = true;
675 }
676
677 err = bpf__probe(obj);
678 if (err) {
679 bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
680 goto errout;
681 }
682
Wang Nan1e5e3ee2015-10-14 12:41:16 +0000683 err = bpf__load(obj);
684 if (err) {
685 bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
686 goto errout;
687 }
688
Wang Nancd102d72016-07-13 10:44:04 +0000689 err = bpf__foreach_event(obj, add_bpf_event, &param);
Wang Nan4edf30e2015-10-14 12:41:17 +0000690 if (err) {
691 snprintf(errbuf, sizeof(errbuf),
692 "Attach events in BPF object failed");
693 goto errout;
694 }
695
696 return 0;
Wang Nan84c86ca2015-10-14 12:41:14 +0000697errout:
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300698 parse_state->error->help = strdup("(add -v to see detail)");
699 parse_state->error->str = strdup(errbuf);
Wang Nan84c86ca2015-10-14 12:41:14 +0000700 return err;
701}
702
Wang Nana34f3be2016-02-22 09:10:31 +0000703static int
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300704parse_events_config_bpf(struct parse_events_state *parse_state,
Wang Nana34f3be2016-02-22 09:10:31 +0000705 struct bpf_object *obj,
706 struct list_head *head_config)
707{
708 struct parse_events_term *term;
709 int error_pos;
710
711 if (!head_config || list_empty(head_config))
712 return 0;
713
714 list_for_each_entry(term, head_config, list) {
715 char errbuf[BUFSIZ];
716 int err;
717
718 if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) {
719 snprintf(errbuf, sizeof(errbuf),
720 "Invalid config term for BPF object");
721 errbuf[BUFSIZ - 1] = '\0';
722
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300723 parse_state->error->idx = term->err_term;
724 parse_state->error->str = strdup(errbuf);
Wang Nana34f3be2016-02-22 09:10:31 +0000725 return -EINVAL;
726 }
727
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300728 err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos);
Wang Nana34f3be2016-02-22 09:10:31 +0000729 if (err) {
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300730 bpf__strerror_config_obj(obj, term, parse_state->evlist,
Wang Nana34f3be2016-02-22 09:10:31 +0000731 &error_pos, err, errbuf,
732 sizeof(errbuf));
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300733 parse_state->error->help = strdup(
Wang Nan7630b3e2016-02-22 09:10:33 +0000734"Hint:\tValid config terms:\n"
Wang Nane571e022016-02-22 09:10:35 +0000735" \tmap:[<arraymap>].value<indices>=[value]\n"
736" \tmap:[<eventmap>].event<indices>=[event]\n"
Wang Nan7630b3e2016-02-22 09:10:33 +0000737"\n"
Wang Nane571e022016-02-22 09:10:35 +0000738" \twhere <indices> is something like [0,3...5] or [all]\n"
Wang Nana34f3be2016-02-22 09:10:31 +0000739" \t(add -v to see detail)");
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300740 parse_state->error->str = strdup(errbuf);
Wang Nana34f3be2016-02-22 09:10:31 +0000741 if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE)
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300742 parse_state->error->idx = term->err_val;
Wang Nana34f3be2016-02-22 09:10:31 +0000743 else
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300744 parse_state->error->idx = term->err_term + error_pos;
Wang Nana34f3be2016-02-22 09:10:31 +0000745 return err;
746 }
747 }
748 return 0;
749}
750
Wang Nan95088a52016-02-22 09:10:36 +0000751/*
752 * Split config terms:
753 * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
754 * 'call-graph=fp' is 'evt config', should be applied to each
755 * events in bpf.c.
756 * 'map:array.value[0]=1' is 'obj config', should be processed
757 * with parse_events_config_bpf.
758 *
759 * Move object config terms from the first list to obj_head_config.
760 */
761static void
762split_bpf_config_terms(struct list_head *evt_head_config,
763 struct list_head *obj_head_config)
764{
765 struct parse_events_term *term, *temp;
766
767 /*
768 * Currectly, all possible user config term
769 * belong to bpf object. parse_events__is_hardcoded_term()
770 * happends to be a good flag.
771 *
772 * See parse_events_config_bpf() and
773 * config_term_tracepoint().
774 */
775 list_for_each_entry_safe(term, temp, evt_head_config, list)
776 if (!parse_events__is_hardcoded_term(term))
777 list_move_tail(&term->list, obj_head_config);
778}
779
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300780int parse_events_load_bpf(struct parse_events_state *parse_state,
Wang Nan84c86ca2015-10-14 12:41:14 +0000781 struct list_head *list,
Wang Nand509db02015-10-14 12:41:20 +0000782 char *bpf_file_name,
Wang Nana34f3be2016-02-22 09:10:31 +0000783 bool source,
784 struct list_head *head_config)
Wang Nan84c86ca2015-10-14 12:41:14 +0000785{
Wang Nana34f3be2016-02-22 09:10:31 +0000786 int err;
Wang Nan95088a52016-02-22 09:10:36 +0000787 struct bpf_object *obj;
788 LIST_HEAD(obj_head_config);
789
790 if (head_config)
791 split_bpf_config_terms(head_config, &obj_head_config);
Wang Nan84c86ca2015-10-14 12:41:14 +0000792
Wang Nand509db02015-10-14 12:41:20 +0000793 obj = bpf__prepare_load(bpf_file_name, source);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000794 if (IS_ERR(obj)) {
Wang Nan84c86ca2015-10-14 12:41:14 +0000795 char errbuf[BUFSIZ];
Wang Nan84c86ca2015-10-14 12:41:14 +0000796
Wang Nan6371ca3b2015-11-06 13:49:37 +0000797 err = PTR_ERR(obj);
Wang Nan84c86ca2015-10-14 12:41:14 +0000798
799 if (err == -ENOTSUP)
800 snprintf(errbuf, sizeof(errbuf),
801 "BPF support is not compiled");
802 else
Wang Nand3e0ce32015-11-06 13:58:09 +0000803 bpf__strerror_prepare_load(bpf_file_name,
804 source,
805 -err, errbuf,
806 sizeof(errbuf));
Wang Nan84c86ca2015-10-14 12:41:14 +0000807
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300808 parse_state->error->help = strdup("(add -v to see detail)");
809 parse_state->error->str = strdup(errbuf);
Wang Nan84c86ca2015-10-14 12:41:14 +0000810 return err;
811 }
812
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300813 err = parse_events_load_bpf_obj(parse_state, list, obj, head_config);
Wang Nana34f3be2016-02-22 09:10:31 +0000814 if (err)
815 return err;
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300816 err = parse_events_config_bpf(parse_state, obj, &obj_head_config);
Wang Nan95088a52016-02-22 09:10:36 +0000817
818 /*
819 * Caller doesn't know anything about obj_head_config,
820 * so combine them together again before returnning.
821 */
822 if (head_config)
823 list_splice_tail(&obj_head_config, head_config);
824 return err;
Wang Nan84c86ca2015-10-14 12:41:14 +0000825}
826
Jiri Olsa89812fc2012-03-15 20:09:15 +0100827static int
828parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100829{
830 int i;
831
832 for (i = 0; i < 3; i++) {
Jiri Olsa89812fc2012-03-15 20:09:15 +0100833 if (!type || !type[i])
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100834 break;
835
Jiri Olsa75827322012-06-29 09:22:54 +0200836#define CHECK_SET_TYPE(bit) \
837do { \
838 if (attr->bp_type & bit) \
839 return -EINVAL; \
840 else \
841 attr->bp_type |= bit; \
842} while (0)
843
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100844 switch (type[i]) {
845 case 'r':
Jiri Olsa75827322012-06-29 09:22:54 +0200846 CHECK_SET_TYPE(HW_BREAKPOINT_R);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100847 break;
848 case 'w':
Jiri Olsa75827322012-06-29 09:22:54 +0200849 CHECK_SET_TYPE(HW_BREAKPOINT_W);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100850 break;
851 case 'x':
Jiri Olsa75827322012-06-29 09:22:54 +0200852 CHECK_SET_TYPE(HW_BREAKPOINT_X);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100853 break;
854 default:
Jiri Olsa89812fc2012-03-15 20:09:15 +0100855 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100856 }
857 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100858
Jiri Olsa75827322012-06-29 09:22:54 +0200859#undef CHECK_SET_TYPE
860
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100861 if (!attr->bp_type) /* Default */
862 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
863
Jiri Olsa89812fc2012-03-15 20:09:15 +0100864 return 0;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100865}
866
David Ahernc5cd8ac2013-07-02 13:27:25 -0600867int parse_events_add_breakpoint(struct list_head *list, int *idx,
Jacob Shin3741eb92014-05-29 17:26:51 +0200868 void *ptr, char *type, u64 len)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100869{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100870 struct perf_event_attr attr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100871
Jiri Olsa89812fc2012-03-15 20:09:15 +0100872 memset(&attr, 0, sizeof(attr));
Jiri Olsa9fafd982012-03-20 19:15:39 +0100873 attr.bp_addr = (unsigned long) ptr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100874
Jiri Olsa89812fc2012-03-15 20:09:15 +0100875 if (parse_breakpoint_type(type, &attr))
876 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100877
Jacob Shin3741eb92014-05-29 17:26:51 +0200878 /* Provide some defaults if len is not specified */
879 if (!len) {
880 if (attr.bp_type == HW_BREAKPOINT_X)
881 len = sizeof(long);
882 else
883 len = HW_BREAKPOINT_LEN_4;
884 }
885
886 attr.bp_len = len;
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200887
Jiri Olsa89812fc2012-03-15 20:09:15 +0100888 attr.type = PERF_TYPE_BREAKPOINT;
Jovi Zhang4a841d62012-07-15 03:03:10 +0800889 attr.sample_period = 1;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100890
Jiri Olsa930a2e22015-07-29 05:42:10 -0400891 return add_event(list, idx, &attr, NULL, NULL);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100892}
893
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200894static int check_type_val(struct parse_events_term *term,
895 struct parse_events_error *err,
896 int type)
Jiri Olsa8f707d82012-03-15 20:09:16 +0100897{
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200898 if (type == term->type_val)
899 return 0;
900
901 if (err) {
902 err->idx = term->err_val;
903 if (type == PARSE_EVENTS__TERM_TYPE_NUM)
904 err->str = strdup("expected numeric value");
905 else
906 err->str = strdup("expected string value");
907 }
908 return -EINVAL;
909}
910
Wang Nan17cb5f82016-02-19 11:43:57 +0000911/*
912 * Update according to parse-events.l
913 */
914static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
915 [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>",
916 [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config",
917 [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1",
918 [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2",
919 [PARSE_EVENTS__TERM_TYPE_NAME] = "name",
920 [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period",
921 [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq",
922 [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type",
923 [PARSE_EVENTS__TERM_TYPE_TIME] = "time",
924 [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph",
925 [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size",
926 [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit",
927 [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit",
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300928 [PARSE_EVENTS__TERM_TYPE_MAX_STACK] = "max-stack",
Arnaldo Carvalho de Melo2fda5ad2018-10-19 15:47:34 -0300929 [PARSE_EVENTS__TERM_TYPE_MAX_EVENTS] = "nr",
Wang Nan626a6b72016-07-14 08:34:45 +0000930 [PARSE_EVENTS__TERM_TYPE_OVERWRITE] = "overwrite",
931 [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE] = "no-overwrite",
Mathieu Poirierdd60fba2016-09-06 10:37:15 -0600932 [PARSE_EVENTS__TERM_TYPE_DRV_CFG] = "driver-config",
Wang Nan17cb5f82016-02-19 11:43:57 +0000933};
934
Wang Nan1669e502016-02-19 11:43:58 +0000935static bool config_term_shrinked;
936
937static bool
938config_term_avail(int term_type, struct parse_events_error *err)
939{
940 if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
941 err->str = strdup("Invalid term_type");
942 return false;
943 }
944 if (!config_term_shrinked)
945 return true;
946
947 switch (term_type) {
948 case PARSE_EVENTS__TERM_TYPE_CONFIG:
949 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
950 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
951 case PARSE_EVENTS__TERM_TYPE_NAME:
Sukadev Bhattiprolu06835542016-09-15 15:24:56 -0700952 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
Wang Nan1669e502016-02-19 11:43:58 +0000953 return true;
954 default:
955 if (!err)
956 return false;
957
958 /* term_type is validated so indexing is safe */
959 if (asprintf(&err->str, "'%s' is not usable in 'perf stat'",
960 config_term_names[term_type]) < 0)
961 err->str = NULL;
962 return false;
963 }
964}
965
966void parse_events__shrink_config_terms(void)
967{
968 config_term_shrinked = true;
969}
970
He Kuang0b8891a2015-09-28 03:52:13 +0000971static int config_term_common(struct perf_event_attr *attr,
972 struct parse_events_term *term,
973 struct parse_events_error *err)
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200974{
975#define CHECK_TYPE_VAL(type) \
976do { \
977 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
978 return -EINVAL; \
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200979} while (0)
980
981 switch (term->type_term) {
Jiri Olsa8f707d82012-03-15 20:09:16 +0100982 case PARSE_EVENTS__TERM_TYPE_CONFIG:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200983 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100984 attr->config = term->val.num;
985 break;
986 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200987 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100988 attr->config1 = term->val.num;
989 break;
990 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200991 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100992 attr->config2 = term->val.num;
993 break;
994 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200995 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100996 break;
Namhyung Kim09af2a52015-08-09 15:45:23 +0900997 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
998 CHECK_TYPE_VAL(NUM);
999 break;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001000 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
Andi Kleenac12f672016-10-12 14:02:06 -07001001 CHECK_TYPE_VAL(STR);
1002 if (strcmp(term->val.str, "no") &&
1003 parse_branch_str(term->val.str, &attr->branch_sample_type)) {
1004 err->str = strdup("invalid branch sample type");
1005 err->idx = term->err_val;
1006 return -EINVAL;
1007 }
Jiri Olsa8f707d82012-03-15 20:09:16 +01001008 break;
Kan Liang32067712015-08-04 04:30:19 -04001009 case PARSE_EVENTS__TERM_TYPE_TIME:
1010 CHECK_TYPE_VAL(NUM);
1011 if (term->val.num > 1) {
1012 err->str = strdup("expected 0 or 1");
1013 err->idx = term->err_val;
1014 return -EINVAL;
1015 }
1016 break;
Kan Liangd457c962015-08-11 06:30:47 -04001017 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1018 CHECK_TYPE_VAL(STR);
1019 break;
1020 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1021 CHECK_TYPE_VAL(NUM);
1022 break;
Wang Nan374ce932015-10-28 10:55:02 +00001023 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1024 CHECK_TYPE_VAL(NUM);
1025 break;
1026 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1027 CHECK_TYPE_VAL(NUM);
1028 break;
Wang Nan626a6b72016-07-14 08:34:45 +00001029 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1030 CHECK_TYPE_VAL(NUM);
1031 break;
1032 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1033 CHECK_TYPE_VAL(NUM);
1034 break;
Jiri Olsa6b5fc392012-05-21 09:12:53 +02001035 case PARSE_EVENTS__TERM_TYPE_NAME:
1036 CHECK_TYPE_VAL(STR);
1037 break;
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -03001038 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1039 CHECK_TYPE_VAL(NUM);
1040 break;
Arnaldo Carvalho de Melo2fda5ad2018-10-19 15:47:34 -03001041 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1042 CHECK_TYPE_VAL(NUM);
1043 break;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001044 default:
He Kuangffeb8832015-09-28 03:52:14 +00001045 err->str = strdup("unknown term");
1046 err->idx = term->err_term;
1047 err->help = parse_events_formats_error_string(NULL);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001048 return -EINVAL;
1049 }
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001050
Wang Nan1669e502016-02-19 11:43:58 +00001051 /*
1052 * Check term availbility after basic checking so
1053 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
1054 *
1055 * If check availbility at the entry of this function,
1056 * user will see "'<sysfs term>' is not usable in 'perf stat'"
1057 * if an invalid config term is provided for legacy events
1058 * (for example, instructions/badterm/...), which is confusing.
1059 */
1060 if (!config_term_avail(term->type_term, err))
1061 return -EINVAL;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001062 return 0;
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001063#undef CHECK_TYPE_VAL
Jiri Olsa8f707d82012-03-15 20:09:16 +01001064}
1065
He Kuang0b8891a2015-09-28 03:52:13 +00001066static int config_term_pmu(struct perf_event_attr *attr,
1067 struct parse_events_term *term,
1068 struct parse_events_error *err)
1069{
Mathieu Poirierdd60fba2016-09-06 10:37:15 -06001070 if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER ||
1071 term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG)
He Kuang0b8891a2015-09-28 03:52:13 +00001072 /*
1073 * Always succeed for sysfs terms, as we dont know
1074 * at this point what type they need to have.
1075 */
1076 return 0;
1077 else
1078 return config_term_common(attr, term, err);
1079}
1080
He Kuange637d172015-09-28 03:52:16 +00001081static int config_term_tracepoint(struct perf_event_attr *attr,
1082 struct parse_events_term *term,
1083 struct parse_events_error *err)
1084{
1085 switch (term->type_term) {
1086 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1087 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
Wang Nan374ce932015-10-28 10:55:02 +00001088 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1089 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -03001090 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
Arnaldo Carvalho de Melo2fda5ad2018-10-19 15:47:34 -03001091 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
Wang Nan626a6b72016-07-14 08:34:45 +00001092 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1093 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
He Kuange637d172015-09-28 03:52:16 +00001094 return config_term_common(attr, term, err);
1095 default:
1096 if (err) {
1097 err->idx = term->err_term;
1098 err->str = strdup("unknown term");
1099 err->help = strdup("valid terms: call-graph,stack-size\n");
1100 }
1101 return -EINVAL;
1102 }
1103
1104 return 0;
1105}
1106
Jiri Olsa8f707d82012-03-15 20:09:16 +01001107static int config_attr(struct perf_event_attr *attr,
Jiri Olsa3b0e3712015-04-22 21:10:22 +02001108 struct list_head *head,
He Kuang0b8891a2015-09-28 03:52:13 +00001109 struct parse_events_error *err,
1110 config_term_func_t config_term)
Jiri Olsa8f707d82012-03-15 20:09:16 +01001111{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001112 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001113
1114 list_for_each_entry(term, head, list)
Jiri Olsa3b0e3712015-04-22 21:10:22 +02001115 if (config_term(attr, term, err))
Jiri Olsa8f707d82012-03-15 20:09:16 +01001116 return -EINVAL;
1117
1118 return 0;
1119}
1120
Jiri Olsa930a2e22015-07-29 05:42:10 -04001121static int get_config_terms(struct list_head *head_config,
1122 struct list_head *head_terms __maybe_unused)
1123{
1124#define ADD_CONFIG_TERM(__type, __name, __val) \
1125do { \
1126 struct perf_evsel_config_term *__t; \
1127 \
1128 __t = zalloc(sizeof(*__t)); \
1129 if (!__t) \
1130 return -ENOMEM; \
1131 \
1132 INIT_LIST_HEAD(&__t->list); \
1133 __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \
1134 __t->val.__name = __val; \
Andi Kleenc2f1cea2017-10-20 13:27:55 -07001135 __t->weak = term->weak; \
Jiri Olsa930a2e22015-07-29 05:42:10 -04001136 list_add_tail(&__t->list, head_terms); \
1137} while (0)
1138
1139 struct parse_events_term *term;
1140
1141 list_for_each_entry(term, head_config, list) {
1142 switch (term->type_term) {
Jiri Olsaee4c7582015-07-29 05:42:11 -04001143 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1144 ADD_CONFIG_TERM(PERIOD, period, term->val.num);
Kan Liang32067712015-08-04 04:30:19 -04001145 break;
Namhyung Kim09af2a52015-08-09 15:45:23 +09001146 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1147 ADD_CONFIG_TERM(FREQ, freq, term->val.num);
1148 break;
Kan Liang32067712015-08-04 04:30:19 -04001149 case PARSE_EVENTS__TERM_TYPE_TIME:
1150 ADD_CONFIG_TERM(TIME, time, term->val.num);
1151 break;
Kan Liangd457c962015-08-11 06:30:47 -04001152 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1153 ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
1154 break;
Andi Kleenac12f672016-10-12 14:02:06 -07001155 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1156 ADD_CONFIG_TERM(BRANCH, branch, term->val.str);
1157 break;
Kan Liangd457c962015-08-11 06:30:47 -04001158 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1159 ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
1160 break;
Wang Nan374ce932015-10-28 10:55:02 +00001161 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1162 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0);
1163 break;
1164 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1165 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1);
1166 break;
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -03001167 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1168 ADD_CONFIG_TERM(MAX_STACK, max_stack, term->val.num);
1169 break;
Arnaldo Carvalho de Melo2fda5ad2018-10-19 15:47:34 -03001170 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1171 ADD_CONFIG_TERM(MAX_EVENTS, max_events, term->val.num);
1172 break;
Wang Nan626a6b72016-07-14 08:34:45 +00001173 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1174 ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 1 : 0);
1175 break;
1176 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1177 ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 0 : 1);
1178 break;
Mathieu Poirierdd60fba2016-09-06 10:37:15 -06001179 case PARSE_EVENTS__TERM_TYPE_DRV_CFG:
1180 ADD_CONFIG_TERM(DRV_CFG, drv_cfg, term->val.str);
1181 break;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001182 default:
1183 break;
1184 }
1185 }
1186#undef ADD_EVSEL_CONFIG
1187 return 0;
1188}
1189
He Kuange637d172015-09-28 03:52:16 +00001190int parse_events_add_tracepoint(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +00001191 const char *sys, const char *event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001192 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +00001193 struct list_head *head_config)
1194{
1195 if (head_config) {
1196 struct perf_event_attr attr;
1197
Jiri Olsa272ed292015-09-29 17:05:31 +02001198 if (config_attr(&attr, head_config, err,
He Kuange637d172015-09-28 03:52:16 +00001199 config_term_tracepoint))
1200 return -EINVAL;
1201 }
1202
1203 if (strpbrk(sys, "*?"))
1204 return add_tracepoint_multi_sys(list, idx, sys, event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001205 err, head_config);
He Kuange637d172015-09-28 03:52:16 +00001206 else
1207 return add_tracepoint_event(list, idx, sys, event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001208 err, head_config);
He Kuange637d172015-09-28 03:52:16 +00001209}
1210
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001211int parse_events_add_numeric(struct parse_events_state *parse_state,
Jiri Olsa87d650b2015-04-22 21:10:24 +02001212 struct list_head *list,
Robert Richterb527bab2012-08-07 19:43:13 +02001213 u32 type, u64 config,
Jiri Olsa8f707d82012-03-15 20:09:16 +01001214 struct list_head *head_config)
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301215{
Jiri Olsa89812fc2012-03-15 20:09:15 +01001216 struct perf_event_attr attr;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001217 LIST_HEAD(config_terms);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301218
Jiri Olsa89812fc2012-03-15 20:09:15 +01001219 memset(&attr, 0, sizeof(attr));
1220 attr.type = type;
1221 attr.config = config;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001222
Jiri Olsa930a2e22015-07-29 05:42:10 -04001223 if (head_config) {
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001224 if (config_attr(&attr, head_config, parse_state->error,
He Kuang0b8891a2015-09-28 03:52:13 +00001225 config_term_common))
Jiri Olsa930a2e22015-07-29 05:42:10 -04001226 return -EINVAL;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001227
Jiri Olsa930a2e22015-07-29 05:42:10 -04001228 if (get_config_terms(head_config, &config_terms))
1229 return -ENOMEM;
1230 }
1231
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001232 return add_event(list, &parse_state->idx, &attr,
Wang Nan10bf3582016-02-19 11:44:00 +00001233 get_config_name(head_config), &config_terms);
Paul Mackerras61c45982009-07-01 13:04:34 +10001234}
1235
Agustin Vega-Friasc199c112018-03-06 09:04:44 -05001236int parse_events_add_pmu(struct parse_events_state *parse_state,
Jiri Olsa36adec82015-04-22 21:10:19 +02001237 struct list_head *list, char *name,
Kan Liang3cdc5c22018-05-07 14:13:43 -07001238 struct list_head *head_config,
1239 bool auto_merge_stats,
1240 bool use_alias)
Jiri Olsa5f537a22012-03-15 20:09:18 +01001241{
1242 struct perf_event_attr attr;
Matt Fleming46441bd2014-09-24 15:04:06 +01001243 struct perf_pmu_info info;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001244 struct perf_pmu *pmu;
Stephane Eranian410136f2013-11-12 17:58:49 +01001245 struct perf_evsel *evsel;
Andi Kleen333b5662017-09-13 14:50:06 -07001246 struct parse_events_error *err = parse_state->error;
Kan Liang3cdc5c22018-05-07 14:13:43 -07001247 bool use_uncore_alias;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001248 LIST_HEAD(config_terms);
Jiri Olsa5f537a22012-03-15 20:09:18 +01001249
1250 pmu = perf_pmu__find(name);
Andi Kleen333b5662017-09-13 14:50:06 -07001251 if (!pmu) {
1252 if (asprintf(&err->str,
1253 "Cannot find PMU `%s'. Missing kernel support?",
1254 name) < 0)
1255 err->str = NULL;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001256 return -EINVAL;
Andi Kleen333b5662017-09-13 14:50:06 -07001257 }
Jiri Olsa5f537a22012-03-15 20:09:18 +01001258
Adrian Hunterdc0a6202014-07-31 09:00:49 +03001259 if (pmu->default_config) {
1260 memcpy(&attr, pmu->default_config,
1261 sizeof(struct perf_event_attr));
1262 } else {
1263 memset(&attr, 0, sizeof(attr));
1264 }
Jiri Olsa5f537a22012-03-15 20:09:18 +01001265
Kan Liang3cdc5c22018-05-07 14:13:43 -07001266 use_uncore_alias = (pmu->is_uncore && use_alias);
1267
Adrian Hunterad962272014-08-15 22:08:40 +03001268 if (!head_config) {
1269 attr.type = pmu->type;
Mark Rutland66ec11912017-10-06 19:38:22 +01001270 evsel = __add_event(list, &parse_state->idx, &attr, NULL, pmu, NULL, auto_merge_stats);
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001271 if (evsel) {
1272 evsel->pmu_name = name;
Kan Liang3cdc5c22018-05-07 14:13:43 -07001273 evsel->use_uncore_alias = use_uncore_alias;
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001274 return 0;
1275 } else {
1276 return -ENOMEM;
1277 }
Adrian Hunterad962272014-08-15 22:08:40 +03001278 }
1279
Matt Fleming46441bd2014-09-24 15:04:06 +01001280 if (perf_pmu__check_alias(pmu, head_config, &info))
Zheng Yana6146d52012-06-15 14:31:41 +08001281 return -EINVAL;
1282
Jiri Olsa5f537a22012-03-15 20:09:18 +01001283 /*
1284 * Configure hardcoded terms first, no need to check
1285 * return value when called with fail == 0 ;)
1286 */
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001287 if (config_attr(&attr, head_config, parse_state->error, config_term_pmu))
Jiri Olsac056ba6a2015-04-22 21:10:18 +02001288 return -EINVAL;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001289
Jiri Olsa930a2e22015-07-29 05:42:10 -04001290 if (get_config_terms(head_config, &config_terms))
1291 return -ENOMEM;
1292
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001293 if (perf_pmu__config(pmu, &attr, head_config, parse_state->error))
Jiri Olsa5f537a22012-03-15 20:09:18 +01001294 return -EINVAL;
1295
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001296 evsel = __add_event(list, &parse_state->idx, &attr,
Mark Rutland66ec11912017-10-06 19:38:22 +01001297 get_config_name(head_config), pmu,
Arnaldo Carvalho de Melo63ce8442017-08-31 15:32:18 -03001298 &config_terms, auto_merge_stats);
Stephane Eranian410136f2013-11-12 17:58:49 +01001299 if (evsel) {
Matt Fleming46441bd2014-09-24 15:04:06 +01001300 evsel->unit = info.unit;
1301 evsel->scale = info.scale;
Matt Fleming044330c2014-11-21 10:31:12 +01001302 evsel->per_pkg = info.per_pkg;
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001303 evsel->snapshot = info.snapshot;
Andi Kleen37932c12017-03-20 13:17:08 -07001304 evsel->metric_expr = info.metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001305 evsel->metric_name = info.metric_name;
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001306 evsel->pmu_name = name;
Kan Liang3cdc5c22018-05-07 14:13:43 -07001307 evsel->use_uncore_alias = use_uncore_alias;
Stephane Eranian410136f2013-11-12 17:58:49 +01001308 }
1309
1310 return evsel ? 0 : -ENOMEM;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001311}
1312
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001313int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
Andi Kleen2073ad32017-03-20 13:17:02 -07001314 char *str, struct list_head **listp)
1315{
1316 struct list_head *head;
1317 struct parse_events_term *term;
1318 struct list_head *list;
1319 struct perf_pmu *pmu = NULL;
1320 int ok = 0;
1321
1322 *listp = NULL;
1323 /* Add it for all PMUs that support the alias */
1324 list = malloc(sizeof(struct list_head));
1325 if (!list)
1326 return -1;
1327 INIT_LIST_HEAD(list);
1328 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1329 struct perf_pmu_alias *alias;
1330
1331 list_for_each_entry(alias, &pmu->aliases, list) {
1332 if (!strcasecmp(alias->name, str)) {
1333 head = malloc(sizeof(struct list_head));
1334 if (!head)
1335 return -1;
1336 INIT_LIST_HEAD(head);
1337 if (parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
1338 str, 1, false, &str, NULL) < 0)
1339 return -1;
1340 list_add_tail(&term->list, head);
1341
Agustin Vega-Friasc199c112018-03-06 09:04:44 -05001342 if (!parse_events_add_pmu(parse_state, list,
Kan Liang3cdc5c22018-05-07 14:13:43 -07001343 pmu->name, head,
1344 true, true)) {
Andi Kleen2073ad32017-03-20 13:17:02 -07001345 pr_debug("%s -> %s/%s/\n", str,
1346 pmu->name, alias->str);
1347 ok++;
1348 }
1349
1350 parse_events_terms__delete(head);
1351 }
1352 }
1353 }
1354 if (!ok)
1355 return -1;
1356 *listp = list;
1357 return 0;
1358}
1359
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001360int parse_events__modifier_group(struct list_head *list,
1361 char *event_mod)
Jiri Olsa89efb022012-08-08 12:14:14 +02001362{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001363 return parse_events__modifier_event(list, event_mod, true);
1364}
1365
Kan Liang3cdc5c22018-05-07 14:13:43 -07001366/*
1367 * Check if the two uncore PMUs are from the same uncore block
1368 * The format of the uncore PMU name is uncore_#blockname_#pmuidx
1369 */
1370static bool is_same_uncore_block(const char *pmu_name_a, const char *pmu_name_b)
1371{
1372 char *end_a, *end_b;
1373
1374 end_a = strrchr(pmu_name_a, '_');
1375 end_b = strrchr(pmu_name_b, '_');
1376
1377 if (!end_a || !end_b)
1378 return false;
1379
1380 if ((end_a - pmu_name_a) != (end_b - pmu_name_b))
1381 return false;
1382
1383 return (strncmp(pmu_name_a, pmu_name_b, end_a - pmu_name_a) == 0);
1384}
1385
1386static int
1387parse_events__set_leader_for_uncore_aliase(char *name, struct list_head *list,
1388 struct parse_events_state *parse_state)
1389{
1390 struct perf_evsel *evsel, *leader;
1391 uintptr_t *leaders;
1392 bool is_leader = true;
1393 int i, nr_pmu = 0, total_members, ret = 0;
1394
1395 leader = list_first_entry(list, struct perf_evsel, node);
1396 evsel = list_last_entry(list, struct perf_evsel, node);
1397 total_members = evsel->idx - leader->idx + 1;
1398
1399 leaders = calloc(total_members, sizeof(uintptr_t));
1400 if (WARN_ON(!leaders))
1401 return 0;
1402
1403 /*
1404 * Going through the whole group and doing sanity check.
1405 * All members must use alias, and be from the same uncore block.
1406 * Also, storing the leader events in an array.
1407 */
1408 __evlist__for_each_entry(list, evsel) {
1409
1410 /* Only split the uncore group which members use alias */
1411 if (!evsel->use_uncore_alias)
1412 goto out;
1413
1414 /* The events must be from the same uncore block */
1415 if (!is_same_uncore_block(leader->pmu_name, evsel->pmu_name))
1416 goto out;
1417
1418 if (!is_leader)
1419 continue;
1420 /*
1421 * If the event's PMU name starts to repeat, it must be a new
1422 * event. That can be used to distinguish the leader from
1423 * other members, even they have the same event name.
1424 */
1425 if ((leader != evsel) && (leader->pmu_name == evsel->pmu_name)) {
1426 is_leader = false;
1427 continue;
1428 }
1429 /* The name is always alias name */
1430 WARN_ON(strcmp(leader->name, evsel->name));
1431
1432 /* Store the leader event for each PMU */
1433 leaders[nr_pmu++] = (uintptr_t) evsel;
1434 }
1435
1436 /* only one event alias */
1437 if (nr_pmu == total_members) {
1438 parse_state->nr_groups--;
1439 goto handled;
1440 }
1441
1442 /*
1443 * An uncore event alias is a joint name which means the same event
1444 * runs on all PMUs of a block.
1445 * Perf doesn't support mixed events from different PMUs in the same
1446 * group. The big group has to be split into multiple small groups
1447 * which only include the events from the same PMU.
1448 *
1449 * Here the uncore event aliases must be from the same uncore block.
1450 * The number of PMUs must be same for each alias. The number of new
1451 * small groups equals to the number of PMUs.
1452 * Setting the leader event for corresponding members in each group.
1453 */
1454 i = 0;
1455 __evlist__for_each_entry(list, evsel) {
1456 if (i >= nr_pmu)
1457 i = 0;
1458 evsel->leader = (struct perf_evsel *) leaders[i++];
1459 }
1460
1461 /* The number of members and group name are same for each group */
1462 for (i = 0; i < nr_pmu; i++) {
1463 evsel = (struct perf_evsel *) leaders[i];
1464 evsel->nr_members = total_members / nr_pmu;
1465 evsel->group_name = name ? strdup(name) : NULL;
1466 }
1467
1468 /* Take the new small groups into account */
1469 parse_state->nr_groups += nr_pmu - 1;
1470
1471handled:
1472 ret = 1;
1473out:
1474 free(leaders);
1475 return ret;
1476}
1477
1478void parse_events__set_leader(char *name, struct list_head *list,
1479 struct parse_events_state *parse_state)
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001480{
1481 struct perf_evsel *leader;
1482
Wang Nan854f7362015-09-06 07:13:17 +00001483 if (list_empty(list)) {
1484 WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1485 return;
1486 }
1487
Kan Liang3cdc5c22018-05-07 14:13:43 -07001488 if (parse_events__set_leader_for_uncore_aliase(name, list, parse_state))
1489 return;
1490
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -03001491 __perf_evlist__set_leader(list);
1492 leader = list_entry(list->next, struct perf_evsel, node);
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001493 leader->group_name = name ? strdup(name) : NULL;
Jiri Olsa89efb022012-08-08 12:14:14 +02001494}
1495
David Ahernc5cd8ac2013-07-02 13:27:25 -06001496/* list_event is assumed to point to malloc'ed memory */
Jiri Olsa5d7be902012-03-20 19:15:40 +01001497void parse_events_update_lists(struct list_head *list_event,
1498 struct list_head *list_all)
1499{
1500 /*
1501 * Called for single event definition. Update the
Jiri Olsa89efb022012-08-08 12:14:14 +02001502 * 'all event' list, and reinit the 'single event'
Jiri Olsa5d7be902012-03-20 19:15:40 +01001503 * list, for next event definition.
1504 */
1505 list_splice_tail(list_event, list_all);
Jiri Olsab847cbd2012-05-21 09:12:51 +02001506 free(list_event);
Jiri Olsa5d7be902012-03-20 19:15:40 +01001507}
1508
Jiri Olsaf5b11352012-08-08 12:21:54 +02001509struct event_modifier {
1510 int eu;
1511 int ek;
1512 int eh;
1513 int eH;
1514 int eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001515 int eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001516 int precise;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001517 int precise_max;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001518 int exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02001519 int sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001520 int pinned;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001521 int weak;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001522};
Paul Mackerras61c45982009-07-01 13:04:34 +10001523
Jiri Olsaf5b11352012-08-08 12:21:54 +02001524static int get_event_modifier(struct event_modifier *mod, char *str,
1525 struct perf_evsel *evsel)
1526{
1527 int eu = evsel ? evsel->attr.exclude_user : 0;
1528 int ek = evsel ? evsel->attr.exclude_kernel : 0;
1529 int eh = evsel ? evsel->attr.exclude_hv : 0;
1530 int eH = evsel ? evsel->attr.exclude_host : 0;
1531 int eG = evsel ? evsel->attr.exclude_guest : 0;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001532 int eI = evsel ? evsel->attr.exclude_idle : 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001533 int precise = evsel ? evsel->attr.precise_ip : 0;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001534 int precise_max = 0;
Jiri Olsa3c176312012-10-10 17:39:03 +02001535 int sample_read = 0;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001536 int pinned = evsel ? evsel->attr.pinned : 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001537
1538 int exclude = eu | ek | eh;
1539 int exclude_GH = evsel ? evsel->exclude_GH : 0;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001540 int weak = 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001541
Jiri Olsaf5b11352012-08-08 12:21:54 +02001542 memset(mod, 0, sizeof(*mod));
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001543
Paul Mackerras61c45982009-07-01 13:04:34 +10001544 while (*str) {
Peter Zijlstraab608342010-04-08 23:03:20 +02001545 if (*str == 'u') {
1546 if (!exclude)
1547 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001548 eu = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +02001549 } else if (*str == 'k') {
1550 if (!exclude)
1551 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001552 ek = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +02001553 } else if (*str == 'h') {
1554 if (!exclude)
1555 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001556 eh = 0;
Joerg Roedel99320cc2012-01-04 17:54:19 +01001557 } else if (*str == 'G') {
1558 if (!exclude_GH)
1559 exclude_GH = eG = eH = 1;
1560 eG = 0;
1561 } else if (*str == 'H') {
1562 if (!exclude_GH)
1563 exclude_GH = eG = eH = 1;
1564 eH = 0;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001565 } else if (*str == 'I') {
1566 eI = 1;
Peter Zijlstraab608342010-04-08 23:03:20 +02001567 } else if (*str == 'p') {
1568 precise++;
David Ahern13427982012-09-13 14:59:13 -06001569 /* use of precise requires exclude_guest */
1570 if (!exclude_GH)
1571 eG = 1;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001572 } else if (*str == 'P') {
1573 precise_max = 1;
Jiri Olsa3c176312012-10-10 17:39:03 +02001574 } else if (*str == 'S') {
1575 sample_read = 1;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001576 } else if (*str == 'D') {
1577 pinned = 1;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001578 } else if (*str == 'W') {
1579 weak = 1;
Peter Zijlstraab608342010-04-08 23:03:20 +02001580 } else
Paul Mackerras61c45982009-07-01 13:04:34 +10001581 break;
Peter Zijlstraab608342010-04-08 23:03:20 +02001582
Paul Mackerras61c45982009-07-01 13:04:34 +10001583 ++str;
1584 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001585
Jiri Olsa89812fc2012-03-15 20:09:15 +01001586 /*
1587 * precise ip:
1588 *
1589 * 0 - SAMPLE_IP can have arbitrary skid
1590 * 1 - SAMPLE_IP must have constant skid
1591 * 2 - SAMPLE_IP requested to have 0 skid
1592 * 3 - SAMPLE_IP must have 0 skid
1593 *
1594 * See also PERF_RECORD_MISC_EXACT_IP
1595 */
1596 if (precise > 3)
1597 return -EINVAL;
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001598
Jiri Olsaf5b11352012-08-08 12:21:54 +02001599 mod->eu = eu;
1600 mod->ek = ek;
1601 mod->eh = eh;
1602 mod->eH = eH;
1603 mod->eG = eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001604 mod->eI = eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001605 mod->precise = precise;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001606 mod->precise_max = precise_max;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001607 mod->exclude_GH = exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02001608 mod->sample_read = sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001609 mod->pinned = pinned;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001610 mod->weak = weak;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001611
Jiri Olsaf5b11352012-08-08 12:21:54 +02001612 return 0;
1613}
1614
Jiri Olsa534123f2012-11-13 15:32:58 +01001615/*
1616 * Basic modifier sanity check to validate it contains only one
1617 * instance of any modifier (apart from 'p') present.
1618 */
1619static int check_modifier(char *str)
1620{
1621 char *p = str;
1622
1623 /* The sizeof includes 0 byte as well. */
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001624 if (strlen(str) > (sizeof("ukhGHpppPSDIW") - 1))
Jiri Olsa534123f2012-11-13 15:32:58 +01001625 return -1;
1626
1627 while (*p) {
1628 if (*p != 'p' && strchr(p + 1, *p))
1629 return -1;
1630 p++;
1631 }
1632
1633 return 0;
1634}
1635
Jiri Olsaf5b11352012-08-08 12:21:54 +02001636int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1637{
1638 struct perf_evsel *evsel;
1639 struct event_modifier mod;
1640
1641 if (str == NULL)
1642 return 0;
1643
Jiri Olsa534123f2012-11-13 15:32:58 +01001644 if (check_modifier(str))
1645 return -EINVAL;
1646
Jiri Olsaf5b11352012-08-08 12:21:54 +02001647 if (!add && get_event_modifier(&mod, str, NULL))
1648 return -EINVAL;
1649
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001650 __evlist__for_each_entry(list, evsel) {
Jiri Olsaf5b11352012-08-08 12:21:54 +02001651 if (add && get_event_modifier(&mod, str, evsel))
1652 return -EINVAL;
1653
1654 evsel->attr.exclude_user = mod.eu;
1655 evsel->attr.exclude_kernel = mod.ek;
1656 evsel->attr.exclude_hv = mod.eh;
1657 evsel->attr.precise_ip = mod.precise;
1658 evsel->attr.exclude_host = mod.eH;
1659 evsel->attr.exclude_guest = mod.eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001660 evsel->attr.exclude_idle = mod.eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001661 evsel->exclude_GH = mod.exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02001662 evsel->sample_read = mod.sample_read;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001663 evsel->precise_max = mod.precise_max;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001664 evsel->weak_group = mod.weak;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001665
1666 if (perf_evsel__is_group_leader(evsel))
1667 evsel->attr.pinned = mod.pinned;
Jiri Olsa89812fc2012-03-15 20:09:15 +01001668 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001669
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301670 return 0;
1671}
1672
Robert Richterac2ba9f2012-08-16 21:10:21 +02001673int parse_events_name(struct list_head *list, char *name)
1674{
1675 struct perf_evsel *evsel;
1676
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001677 __evlist__for_each_entry(list, evsel) {
Robert Richterac2ba9f2012-08-16 21:10:21 +02001678 if (!evsel->name)
1679 evsel->name = strdup(name);
1680 }
1681
1682 return 0;
1683}
1684
Kan Liangdcb4e102014-10-07 11:08:50 -04001685static int
1686comp_pmu(const void *p1, const void *p2)
1687{
1688 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
1689 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
1690
Andi Kleene312bcf2016-09-15 15:24:53 -07001691 return strcasecmp(pmu1->symbol, pmu2->symbol);
Kan Liangdcb4e102014-10-07 11:08:50 -04001692}
1693
1694static void perf_pmu__parse_cleanup(void)
1695{
1696 if (perf_pmu_events_list_num > 0) {
1697 struct perf_pmu_event_symbol *p;
1698 int i;
1699
1700 for (i = 0; i < perf_pmu_events_list_num; i++) {
1701 p = perf_pmu_events_list + i;
Taeung Song360e0712017-02-01 21:34:07 +09001702 zfree(&p->symbol);
Kan Liangdcb4e102014-10-07 11:08:50 -04001703 }
Taeung Song506fde12017-02-01 21:34:06 +09001704 zfree(&perf_pmu_events_list);
Kan Liangdcb4e102014-10-07 11:08:50 -04001705 perf_pmu_events_list_num = 0;
1706 }
1707}
1708
1709#define SET_SYMBOL(str, stype) \
1710do { \
1711 p->symbol = str; \
1712 if (!p->symbol) \
1713 goto err; \
1714 p->type = stype; \
1715} while (0)
1716
1717/*
1718 * Read the pmu events list from sysfs
1719 * Save it into perf_pmu_events_list
1720 */
1721static void perf_pmu__parse_init(void)
1722{
1723
1724 struct perf_pmu *pmu = NULL;
1725 struct perf_pmu_alias *alias;
1726 int len = 0;
1727
Andi Kleen231bb2a2017-01-27 18:03:39 -08001728 pmu = NULL;
1729 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1730 list_for_each_entry(alias, &pmu->aliases, list) {
1731 if (strchr(alias->name, '-'))
1732 len++;
1733 len++;
1734 }
1735 }
1736
1737 if (len == 0) {
Kan Liangdcb4e102014-10-07 11:08:50 -04001738 perf_pmu_events_list_num = -1;
1739 return;
1740 }
Kan Liangdcb4e102014-10-07 11:08:50 -04001741 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1742 if (!perf_pmu_events_list)
1743 return;
1744 perf_pmu_events_list_num = len;
1745
1746 len = 0;
Andi Kleen231bb2a2017-01-27 18:03:39 -08001747 pmu = NULL;
1748 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1749 list_for_each_entry(alias, &pmu->aliases, list) {
1750 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1751 char *tmp = strchr(alias->name, '-');
Kan Liangdcb4e102014-10-07 11:08:50 -04001752
Andi Kleen231bb2a2017-01-27 18:03:39 -08001753 if (tmp != NULL) {
1754 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1755 PMU_EVENT_SYMBOL_PREFIX);
1756 p++;
1757 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1758 len += 2;
1759 } else {
1760 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1761 len++;
1762 }
Kan Liangdcb4e102014-10-07 11:08:50 -04001763 }
1764 }
1765 qsort(perf_pmu_events_list, len,
1766 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1767
1768 return;
1769err:
1770 perf_pmu__parse_cleanup();
1771}
1772
1773enum perf_pmu_event_symbol_type
1774perf_pmu__parse_check(const char *name)
1775{
1776 struct perf_pmu_event_symbol p, *r;
1777
1778 /* scan kernel pmu events from sysfs if needed */
1779 if (perf_pmu_events_list_num == 0)
1780 perf_pmu__parse_init();
1781 /*
1782 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1783 * cpu-cycles has been handled by hardcode.
1784 * So it must be cpu// events, not kernel pmu event.
1785 */
1786 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1787 return PMU_EVENT_SYMBOL_ERR;
1788
1789 p.symbol = strdup(name);
1790 r = bsearch(&p, perf_pmu_events_list,
1791 (size_t) perf_pmu_events_list_num,
1792 sizeof(struct perf_pmu_event_symbol), comp_pmu);
Taeung Song360e0712017-02-01 21:34:07 +09001793 zfree(&p.symbol);
Kan Liangdcb4e102014-10-07 11:08:50 -04001794 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1795}
1796
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001797static int parse_events__scanner(const char *str, void *parse_state, int start_token)
Zheng Yanac20de6f2012-06-15 14:31:39 +08001798{
1799 YY_BUFFER_STATE buffer;
1800 void *scanner;
1801 int ret;
1802
Jiri Olsa90e2b222012-06-15 14:31:40 +08001803 ret = parse_events_lex_init_extra(start_token, &scanner);
Zheng Yanac20de6f2012-06-15 14:31:39 +08001804 if (ret)
1805 return ret;
1806
1807 buffer = parse_events__scan_string(str, scanner);
1808
1809#ifdef PARSER_DEBUG
1810 parse_events_debug = 1;
1811#endif
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001812 ret = parse_events_parse(parse_state, scanner);
Zheng Yanac20de6f2012-06-15 14:31:39 +08001813
1814 parse_events__flush_buffer(buffer, scanner);
1815 parse_events__delete_buffer(buffer, scanner);
1816 parse_events_lex_destroy(scanner);
1817 return ret;
1818}
1819
Jiri Olsa90e2b222012-06-15 14:31:40 +08001820/*
1821 * parse event config string, return a list of event terms.
1822 */
1823int parse_events_terms(struct list_head *terms, const char *str)
1824{
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001825 struct parse_events_state parse_state = {
Jiri Olsa90e2b222012-06-15 14:31:40 +08001826 .terms = NULL,
1827 };
1828 int ret;
1829
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001830 ret = parse_events__scanner(str, &parse_state, PE_START_TERMS);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001831 if (!ret) {
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001832 list_splice(parse_state.terms, terms);
1833 zfree(&parse_state.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001834 return 0;
1835 }
1836
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001837 parse_events_terms__delete(parse_state.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001838 return ret;
1839}
1840
Jiri Olsab39b8392015-04-22 21:10:16 +02001841int parse_events(struct perf_evlist *evlist, const char *str,
1842 struct parse_events_error *err)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001843{
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001844 struct parse_events_state parse_state = {
1845 .list = LIST_HEAD_INIT(parse_state.list),
Wang Nan7630b3e2016-02-22 09:10:33 +00001846 .idx = evlist->nr_entries,
1847 .error = err,
1848 .evlist = evlist,
Jiri Olsa46010ab2012-06-15 14:31:38 +08001849 };
Jiri Olsa46010ab2012-06-15 14:31:38 +08001850 int ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001851
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001852 ret = parse_events__scanner(str, &parse_state, PE_START_EVENTS);
Kan Liangdcb4e102014-10-07 11:08:50 -04001853 perf_pmu__parse_cleanup();
Jiri Olsa89812fc2012-03-15 20:09:15 +01001854 if (!ret) {
Wang Nan15bfd2c2015-07-10 07:36:09 +00001855 struct perf_evsel *last;
1856
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001857 if (list_empty(&parse_state.list)) {
Arnaldo Carvalho de Meloc23080a2018-05-03 15:59:58 -03001858 WARN_ONCE(true, "WARNING: event parser found nothing\n");
Wang Nan854f7362015-09-06 07:13:17 +00001859 return -1;
1860 }
1861
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001862 perf_evlist__splice_list_tail(evlist, &parse_state.list);
1863 evlist->nr_groups += parse_state.nr_groups;
Wang Nan15bfd2c2015-07-10 07:36:09 +00001864 last = perf_evlist__last(evlist);
1865 last->cmdline_group_boundary = true;
1866
Jiri Olsa89812fc2012-03-15 20:09:15 +01001867 return 0;
1868 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02001869
Jiri Olsa5d7be902012-03-20 19:15:40 +01001870 /*
1871 * There are 2 users - builtin-record and builtin-test objects.
1872 * Both call perf_evlist__delete in case of error, so we dont
1873 * need to bother.
1874 */
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02001875 return ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001876}
1877
Jiri Olsab39b8392015-04-22 21:10:16 +02001878#define MAX_WIDTH 1000
1879static int get_term_width(void)
1880{
1881 struct winsize ws;
1882
1883 get_term_dimensions(&ws);
1884 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1885}
1886
Andi Kleen333b5662017-09-13 14:50:06 -07001887void parse_events_print_error(struct parse_events_error *err,
1888 const char *event)
Jiri Olsab39b8392015-04-22 21:10:16 +02001889{
1890 const char *str = "invalid or unsupported event: ";
1891 char _buf[MAX_WIDTH];
1892 char *buf = (char *) event;
1893 int idx = 0;
1894
1895 if (err->str) {
1896 /* -2 for extra '' in the final fprintf */
1897 int width = get_term_width() - 2;
1898 int len_event = strlen(event);
1899 int len_str, max_len, cut = 0;
1900
1901 /*
1902 * Maximum error index indent, we will cut
1903 * the event string if it's bigger.
1904 */
Adrian Hunter141b2d32015-07-17 19:33:51 +03001905 int max_err_idx = 13;
Jiri Olsab39b8392015-04-22 21:10:16 +02001906
1907 /*
1908 * Let's be specific with the message when
1909 * we have the precise error.
1910 */
1911 str = "event syntax error: ";
1912 len_str = strlen(str);
1913 max_len = width - len_str;
1914
1915 buf = _buf;
1916
Adam Buchbinderbd1a0be52016-02-24 10:02:25 -08001917 /* We're cutting from the beginning. */
Jiri Olsab39b8392015-04-22 21:10:16 +02001918 if (err->idx > max_err_idx)
1919 cut = err->idx - max_err_idx;
1920
1921 strncpy(buf, event + cut, max_len);
1922
1923 /* Mark cut parts with '..' on both sides. */
1924 if (cut)
1925 buf[0] = buf[1] = '.';
1926
1927 if ((len_event - cut) > max_len) {
1928 buf[max_len - 1] = buf[max_len - 2] = '.';
1929 buf[max_len] = 0;
1930 }
1931
1932 idx = len_str + err->idx - cut;
1933 }
1934
1935 fprintf(stderr, "%s'%s'\n", str, buf);
1936 if (idx) {
1937 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
1938 if (err->help)
1939 fprintf(stderr, "\n%s\n", err->help);
Taeung Song360e0712017-02-01 21:34:07 +09001940 zfree(&err->str);
1941 zfree(&err->help);
Jiri Olsab39b8392015-04-22 21:10:16 +02001942 }
Jiri Olsab39b8392015-04-22 21:10:16 +02001943}
1944
1945#undef MAX_WIDTH
1946
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001947int parse_events_option(const struct option *opt, const char *str,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001948 int unset __maybe_unused)
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001949{
1950 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
Jiri Olsab39b8392015-04-22 21:10:16 +02001951 struct parse_events_error err = { .idx = 0, };
1952 int ret = parse_events(evlist, str, &err);
Andi Kleen9175ce12012-10-26 13:30:06 -07001953
Andi Kleen333b5662017-09-13 14:50:06 -07001954 if (ret) {
Jiri Olsab39b8392015-04-22 21:10:16 +02001955 parse_events_print_error(&err, str);
Andi Kleen333b5662017-09-13 14:50:06 -07001956 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
1957 }
Jiri Olsab39b8392015-04-22 21:10:16 +02001958
Andi Kleen9175ce12012-10-26 13:30:06 -07001959 return ret;
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001960}
1961
Wang Nan4ba1faa2015-07-10 07:36:10 +00001962static int
1963foreach_evsel_in_last_glob(struct perf_evlist *evlist,
1964 int (*func)(struct perf_evsel *evsel,
1965 const void *arg),
1966 const void *arg)
Li Zefanc171b552009-10-15 11:22:07 +08001967{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001968 struct perf_evsel *last = NULL;
Wang Nan4ba1faa2015-07-10 07:36:10 +00001969 int err;
Li Zefanc171b552009-10-15 11:22:07 +08001970
Wang Nan854f7362015-09-06 07:13:17 +00001971 /*
1972 * Don't return when list_empty, give func a chance to report
1973 * error when it found last == NULL.
1974 *
1975 * So no need to WARN here, let *func do this.
1976 */
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001977 if (evlist->nr_entries > 0)
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001978 last = perf_evlist__last(evlist);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001979
Wang Nan15bfd2c2015-07-10 07:36:09 +00001980 do {
Wang Nan4ba1faa2015-07-10 07:36:10 +00001981 err = (*func)(last, arg);
1982 if (err)
Wang Nan15bfd2c2015-07-10 07:36:09 +00001983 return -1;
Wang Nan4ba1faa2015-07-10 07:36:10 +00001984 if (!last)
1985 return 0;
Wang Nan15bfd2c2015-07-10 07:36:09 +00001986
1987 if (last->node.prev == &evlist->entries)
1988 return 0;
1989 last = list_entry(last->node.prev, struct perf_evsel, node);
1990 } while (!last->cmdline_group_boundary);
Li Zefanc171b552009-10-15 11:22:07 +08001991
1992 return 0;
1993}
1994
Wang Nan4ba1faa2015-07-10 07:36:10 +00001995static int set_filter(struct perf_evsel *evsel, const void *arg)
1996{
1997 const char *str = arg;
Mathieu Poirier1e857482016-09-16 08:44:05 -06001998 bool found = false;
1999 int nr_addr_filters = 0;
2000 struct perf_pmu *pmu = NULL;
Wang Nan4ba1faa2015-07-10 07:36:10 +00002001
Jack Henschel49836f72018-07-04 14:13:45 +02002002 if (evsel == NULL) {
2003 fprintf(stderr,
2004 "--filter option should follow a -e tracepoint or HW tracer option\n");
2005 return -1;
2006 }
Mathieu Poirier1e857482016-09-16 08:44:05 -06002007
2008 if (evsel->attr.type == PERF_TYPE_TRACEPOINT) {
2009 if (perf_evsel__append_tp_filter(evsel, str) < 0) {
2010 fprintf(stderr,
2011 "not enough memory to hold filter string\n");
2012 return -1;
2013 }
2014
2015 return 0;
Wang Nan4ba1faa2015-07-10 07:36:10 +00002016 }
2017
Mathieu Poirier1e857482016-09-16 08:44:05 -06002018 while ((pmu = perf_pmu__scan(pmu)) != NULL)
2019 if (pmu->type == evsel->attr.type) {
2020 found = true;
2021 break;
2022 }
2023
2024 if (found)
2025 perf_pmu__scan_file(pmu, "nr_addr_filters",
2026 "%d", &nr_addr_filters);
2027
Jack Henschel49836f72018-07-04 14:13:45 +02002028 if (!nr_addr_filters) {
2029 fprintf(stderr,
2030 "This CPU does not support address filtering\n");
2031 return -1;
2032 }
Mathieu Poirier1e857482016-09-16 08:44:05 -06002033
2034 if (perf_evsel__append_addr_filter(evsel, str) < 0) {
Wang Nan4ba1faa2015-07-10 07:36:10 +00002035 fprintf(stderr,
2036 "not enough memory to hold filter string\n");
2037 return -1;
2038 }
2039
2040 return 0;
2041}
2042
2043int parse_filter(const struct option *opt, const char *str,
2044 int unset __maybe_unused)
2045{
2046 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
2047
2048 return foreach_evsel_in_last_glob(evlist, set_filter,
2049 (const void *)str);
2050}
2051
2052static int add_exclude_perf_filter(struct perf_evsel *evsel,
2053 const void *arg __maybe_unused)
2054{
2055 char new_filter[64];
2056
2057 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
2058 fprintf(stderr,
2059 "--exclude-perf option should follow a -e tracepoint option\n");
2060 return -1;
2061 }
2062
2063 snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
2064
Mathieu Poirier3541c032016-09-16 08:44:04 -06002065 if (perf_evsel__append_tp_filter(evsel, new_filter) < 0) {
Wang Nan4ba1faa2015-07-10 07:36:10 +00002066 fprintf(stderr,
2067 "not enough memory to hold filter string\n");
2068 return -1;
2069 }
2070
2071 return 0;
2072}
2073
2074int exclude_perf(const struct option *opt,
2075 const char *arg __maybe_unused,
2076 int unset __maybe_unused)
2077{
2078 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
2079
2080 return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
2081 NULL);
2082}
2083
Thomas Gleixner86847b62009-06-06 12:24:17 +02002084static const char * const event_type_descriptors[] = {
Thomas Gleixner86847b62009-06-06 12:24:17 +02002085 "Hardware event",
2086 "Software event",
2087 "Tracepoint event",
2088 "Hardware cache event",
Liming Wang41bdcb22009-12-29 16:37:07 +08002089 "Raw hardware event descriptor",
2090 "Hardware breakpoint",
Thomas Gleixner86847b62009-06-06 12:24:17 +02002091};
2092
Yunlong Songab0e4802015-02-27 18:21:25 +08002093static int cmp_string(const void *a, const void *b)
2094{
2095 const char * const *as = a;
2096 const char * const *bs = b;
2097
2098 return strcmp(*as, *bs);
2099}
2100
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002101/*
Jason Baronf6bdafe2009-07-21 12:20:22 -04002102 * Print the events from <debugfs_mount_point>/tracing/events
2103 */
2104
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002105void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
2106 bool name_only)
Jason Baronf6bdafe2009-07-21 12:20:22 -04002107{
2108 DIR *sys_dir, *evt_dir;
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002109 struct dirent *sys_dirent, *evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -04002110 char evt_path[MAXPATHLEN];
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002111 char *dir_path;
Yunlong Songab0e4802015-02-27 18:21:25 +08002112 char **evt_list = NULL;
2113 unsigned int evt_i = 0, evt_num = 0;
2114 bool evt_num_known = false;
Jason Baronf6bdafe2009-07-21 12:20:22 -04002115
Yunlong Songab0e4802015-02-27 18:21:25 +08002116restart:
Arnaldo Carvalho de Melo7014e0e2018-05-17 14:42:39 -03002117 sys_dir = tracing_events__opendir();
Jason Baronf6bdafe2009-07-21 12:20:22 -04002118 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +02002119 return;
Jason Baronf6bdafe2009-07-21 12:20:22 -04002120
Yunlong Songab0e4802015-02-27 18:21:25 +08002121 if (evt_num_known) {
2122 evt_list = zalloc(sizeof(char *) * evt_num);
2123 if (!evt_list)
2124 goto out_close_sys_dir;
2125 }
2126
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002127 for_each_subsystem(sys_dir, sys_dirent) {
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03002128 if (subsys_glob != NULL &&
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002129 !strglobmatch(sys_dirent->d_name, subsys_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002130 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02002131
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002132 dir_path = get_events_file(sys_dirent->d_name);
2133 if (!dir_path)
2134 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02002135 evt_dir = opendir(dir_path);
2136 if (!evt_dir)
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002137 goto next;
Eric Dumazet725b1362009-09-24 15:39:09 +02002138
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -03002139 for_each_event(dir_path, evt_dir, evt_dirent) {
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03002140 if (event_glob != NULL &&
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002141 !strglobmatch(evt_dirent->d_name, event_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002142 continue;
2143
Yunlong Songab0e4802015-02-27 18:21:25 +08002144 if (!evt_num_known) {
2145 evt_num++;
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002146 continue;
2147 }
2148
Jason Baronf6bdafe2009-07-21 12:20:22 -04002149 snprintf(evt_path, MAXPATHLEN, "%s:%s",
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002150 sys_dirent->d_name, evt_dirent->d_name);
Yunlong Songab0e4802015-02-27 18:21:25 +08002151
2152 evt_list[evt_i] = strdup(evt_path);
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002153 if (evt_list[evt_i] == NULL) {
2154 put_events_file(dir_path);
Yunlong Songab0e4802015-02-27 18:21:25 +08002155 goto out_close_evt_dir;
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002156 }
Yunlong Songab0e4802015-02-27 18:21:25 +08002157 evt_i++;
Jason Baronf6bdafe2009-07-21 12:20:22 -04002158 }
2159 closedir(evt_dir);
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002160next:
2161 put_events_file(dir_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -04002162 }
Jason Baronf6bdafe2009-07-21 12:20:22 -04002163 closedir(sys_dir);
Yunlong Songab0e4802015-02-27 18:21:25 +08002164
2165 if (!evt_num_known) {
2166 evt_num_known = true;
2167 goto restart;
2168 }
2169 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2170 evt_i = 0;
2171 while (evt_i < evt_num) {
2172 if (name_only) {
2173 printf("%s ", evt_list[evt_i++]);
2174 continue;
2175 }
2176 printf(" %-50s [%s]\n", evt_list[evt_i++],
2177 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2178 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002179 if (evt_num && pager_in_use())
Yunlong Songab0e4802015-02-27 18:21:25 +08002180 printf("\n");
2181
2182out_free:
2183 evt_num = evt_i;
2184 for (evt_i = 0; evt_i < evt_num; evt_i++)
2185 zfree(&evt_list[evt_i]);
2186 zfree(&evt_list);
2187 return;
2188
2189out_close_evt_dir:
2190 closedir(evt_dir);
2191out_close_sys_dir:
2192 closedir(sys_dir);
2193
2194 printf("FATAL: not enough memory to print %s\n",
2195 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2196 if (evt_list)
2197 goto out_free;
Jason Baronf6bdafe2009-07-21 12:20:22 -04002198}
2199
2200/*
Thomas Renninger20c457b2011-01-03 17:50:45 +01002201 * Check whether event is in <debugfs_mount_point>/tracing/events
2202 */
2203
2204int is_valid_tracepoint(const char *event_string)
2205{
2206 DIR *sys_dir, *evt_dir;
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002207 struct dirent *sys_dirent, *evt_dirent;
Thomas Renninger20c457b2011-01-03 17:50:45 +01002208 char evt_path[MAXPATHLEN];
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002209 char *dir_path;
Thomas Renninger20c457b2011-01-03 17:50:45 +01002210
Arnaldo Carvalho de Melo7014e0e2018-05-17 14:42:39 -03002211 sys_dir = tracing_events__opendir();
Thomas Renninger20c457b2011-01-03 17:50:45 +01002212 if (!sys_dir)
2213 return 0;
2214
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002215 for_each_subsystem(sys_dir, sys_dirent) {
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002216 dir_path = get_events_file(sys_dirent->d_name);
2217 if (!dir_path)
2218 continue;
Thomas Renninger20c457b2011-01-03 17:50:45 +01002219 evt_dir = opendir(dir_path);
2220 if (!evt_dir)
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002221 goto next;
Thomas Renninger20c457b2011-01-03 17:50:45 +01002222
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -03002223 for_each_event(dir_path, evt_dir, evt_dirent) {
Thomas Renninger20c457b2011-01-03 17:50:45 +01002224 snprintf(evt_path, MAXPATHLEN, "%s:%s",
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002225 sys_dirent->d_name, evt_dirent->d_name);
Thomas Renninger20c457b2011-01-03 17:50:45 +01002226 if (!strcmp(evt_path, event_string)) {
2227 closedir(evt_dir);
2228 closedir(sys_dir);
2229 return 1;
2230 }
2231 }
2232 closedir(evt_dir);
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002233next:
2234 put_events_file(dir_path);
Thomas Renninger20c457b2011-01-03 17:50:45 +01002235 }
2236 closedir(sys_dir);
2237 return 0;
2238}
2239
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002240static bool is_event_supported(u8 type, unsigned config)
2241{
2242 bool ret = true;
Vince Weaver88fee522013-12-30 15:39:45 -05002243 int open_return;
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002244 struct perf_evsel *evsel;
2245 struct perf_event_attr attr = {
2246 .type = type,
2247 .config = config,
2248 .disabled = 1,
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002249 };
Arnaldo Carvalho de Melo89896052017-02-14 10:59:04 -03002250 struct thread_map *tmap = thread_map__new_by_tid(0);
2251
2252 if (tmap == NULL)
2253 return false;
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002254
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002255 evsel = perf_evsel__new(&attr);
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002256 if (evsel) {
Arnaldo Carvalho de Melo89896052017-02-14 10:59:04 -03002257 open_return = perf_evsel__open(evsel, NULL, tmap);
Vince Weaver88fee522013-12-30 15:39:45 -05002258 ret = open_return >= 0;
2259
2260 if (open_return == -EACCES) {
2261 /*
2262 * This happens if the paranoid value
2263 * /proc/sys/kernel/perf_event_paranoid is set to 2
2264 * Re-run with exclude_kernel set; we don't do that
2265 * by default as some ARM machines do not support it.
2266 *
2267 */
2268 evsel->attr.exclude_kernel = 1;
Arnaldo Carvalho de Melo89896052017-02-14 10:59:04 -03002269 ret = perf_evsel__open(evsel, NULL, tmap) >= 0;
Vince Weaver88fee522013-12-30 15:39:45 -05002270 }
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002271 perf_evsel__delete(evsel);
2272 }
2273
2274 return ret;
2275}
2276
Masami Hiramatsu40218da2016-06-24 18:06:46 +09002277void print_sdt_events(const char *subsys_glob, const char *event_glob,
2278 bool name_only)
2279{
2280 struct probe_cache *pcache;
2281 struct probe_cache_entry *ent;
2282 struct strlist *bidlist, *sdtlist;
2283 struct strlist_config cfg = {.dont_dupstr = true};
2284 struct str_node *nd, *nd2;
2285 char *buf, *path, *ptr = NULL;
2286 bool show_detail = false;
2287 int ret;
2288
2289 sdtlist = strlist__new(NULL, &cfg);
2290 if (!sdtlist) {
2291 pr_debug("Failed to allocate new strlist for SDT\n");
2292 return;
2293 }
2294 bidlist = build_id_cache__list_all(true);
2295 if (!bidlist) {
2296 pr_debug("Failed to get buildids: %d\n", errno);
2297 return;
2298 }
2299 strlist__for_each_entry(nd, bidlist) {
Krister Johansenf045b8c2017-07-05 18:48:11 -07002300 pcache = probe_cache__new(nd->s, NULL);
Masami Hiramatsu40218da2016-06-24 18:06:46 +09002301 if (!pcache)
2302 continue;
2303 list_for_each_entry(ent, &pcache->entries, node) {
2304 if (!ent->sdt)
2305 continue;
2306 if (subsys_glob &&
2307 !strglobmatch(ent->pev.group, subsys_glob))
2308 continue;
2309 if (event_glob &&
2310 !strglobmatch(ent->pev.event, event_glob))
2311 continue;
2312 ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
2313 ent->pev.event, nd->s);
2314 if (ret > 0)
2315 strlist__add(sdtlist, buf);
2316 }
2317 probe_cache__delete(pcache);
2318 }
2319 strlist__delete(bidlist);
2320
2321 strlist__for_each_entry(nd, sdtlist) {
2322 buf = strchr(nd->s, '@');
2323 if (buf)
2324 *(buf++) = '\0';
2325 if (name_only) {
2326 printf("%s ", nd->s);
2327 continue;
2328 }
2329 nd2 = strlist__next(nd);
2330 if (nd2) {
2331 ptr = strchr(nd2->s, '@');
2332 if (ptr)
2333 *ptr = '\0';
2334 if (strcmp(nd->s, nd2->s) == 0)
2335 show_detail = true;
2336 }
2337 if (show_detail) {
2338 path = build_id_cache__origname(buf);
2339 ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf);
2340 if (ret > 0) {
2341 printf(" %-50s [%s]\n", buf, "SDT event");
2342 free(buf);
2343 }
2344 } else
2345 printf(" %-50s [%s]\n", nd->s, "SDT event");
2346 if (nd2) {
2347 if (strcmp(nd->s, nd2->s) != 0)
2348 show_detail = false;
2349 if (ptr)
2350 *ptr = '@';
2351 }
2352 }
2353 strlist__delete(sdtlist);
2354}
2355
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002356int print_hwcache_events(const char *event_glob, bool name_only)
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002357{
Yunlong Songab0e4802015-02-27 18:21:25 +08002358 unsigned int type, op, i, evt_i = 0, evt_num = 0;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03002359 char name[64];
Yunlong Songab0e4802015-02-27 18:21:25 +08002360 char **evt_list = NULL;
2361 bool evt_num_known = false;
2362
2363restart:
2364 if (evt_num_known) {
2365 evt_list = zalloc(sizeof(char *) * evt_num);
2366 if (!evt_list)
2367 goto out_enomem;
2368 }
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002369
2370 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
2371 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
2372 /* skip invalid cache type */
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03002373 if (!perf_evsel__is_cache_op_valid(type, op))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002374 continue;
2375
2376 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03002377 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
2378 name, sizeof(name));
Ingo Molnar947b4ad2011-04-29 22:52:42 +02002379 if (event_glob != NULL && !strglobmatch(name, event_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002380 continue;
2381
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002382 if (!is_event_supported(PERF_TYPE_HW_CACHE,
2383 type | (op << 8) | (i << 16)))
2384 continue;
2385
Yunlong Songab0e4802015-02-27 18:21:25 +08002386 if (!evt_num_known) {
2387 evt_num++;
2388 continue;
2389 }
2390
2391 evt_list[evt_i] = strdup(name);
2392 if (evt_list[evt_i] == NULL)
2393 goto out_enomem;
2394 evt_i++;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002395 }
2396 }
2397 }
2398
Yunlong Songab0e4802015-02-27 18:21:25 +08002399 if (!evt_num_known) {
2400 evt_num_known = true;
2401 goto restart;
2402 }
2403 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2404 evt_i = 0;
2405 while (evt_i < evt_num) {
2406 if (name_only) {
2407 printf("%s ", evt_list[evt_i++]);
2408 continue;
2409 }
2410 printf(" %-50s [%s]\n", evt_list[evt_i++],
2411 event_type_descriptors[PERF_TYPE_HW_CACHE]);
2412 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002413 if (evt_num && pager_in_use())
Andi Kleendc098b32013-04-20 11:02:29 -07002414 printf("\n");
Yunlong Songab0e4802015-02-27 18:21:25 +08002415
2416out_free:
2417 evt_num = evt_i;
2418 for (evt_i = 0; evt_i < evt_num; evt_i++)
2419 zfree(&evt_list[evt_i]);
2420 zfree(&evt_list);
2421 return evt_num;
2422
2423out_enomem:
2424 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
2425 if (evt_list)
2426 goto out_free;
2427 return evt_num;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002428}
2429
Yunlong Song705750f2015-02-27 18:21:27 +08002430void print_symbol_events(const char *event_glob, unsigned type,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002431 struct event_symbol *syms, unsigned max,
2432 bool name_only)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002433{
Yunlong Songab0e4802015-02-27 18:21:25 +08002434 unsigned int i, evt_i = 0, evt_num = 0;
Ingo Molnar947b4ad2011-04-29 22:52:42 +02002435 char name[MAX_NAME_LEN];
Yunlong Songab0e4802015-02-27 18:21:25 +08002436 char **evt_list = NULL;
2437 bool evt_num_known = false;
2438
2439restart:
2440 if (evt_num_known) {
2441 evt_list = zalloc(sizeof(char *) * evt_num);
2442 if (!evt_list)
2443 goto out_enomem;
2444 syms -= max;
2445 }
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002446
Jiri Olsa1dc127602012-07-04 00:00:44 +02002447 for (i = 0; i < max; i++, syms++) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002448
Arnaldo Carvalho de Meloe37df6c2015-11-27 16:04:58 -03002449 if (event_glob != NULL && syms->symbol != NULL &&
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002450 !(strglobmatch(syms->symbol, event_glob) ||
2451 (syms->alias && strglobmatch(syms->alias, event_glob))))
2452 continue;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002453
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002454 if (!is_event_supported(type, i))
2455 continue;
2456
Yunlong Songab0e4802015-02-27 18:21:25 +08002457 if (!evt_num_known) {
2458 evt_num++;
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002459 continue;
2460 }
2461
Yunlong Songab0e4802015-02-27 18:21:25 +08002462 if (!name_only && strlen(syms->alias))
Ingo Molnar947b4ad2011-04-29 22:52:42 +02002463 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05302464 else
Arnaldo Carvalho de Melobd8d57f2018-12-06 13:52:13 -03002465 strlcpy(name, syms->symbol, MAX_NAME_LEN);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002466
Yunlong Songab0e4802015-02-27 18:21:25 +08002467 evt_list[evt_i] = strdup(name);
2468 if (evt_list[evt_i] == NULL)
2469 goto out_enomem;
2470 evt_i++;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002471 }
2472
Yunlong Songab0e4802015-02-27 18:21:25 +08002473 if (!evt_num_known) {
2474 evt_num_known = true;
2475 goto restart;
2476 }
2477 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2478 evt_i = 0;
2479 while (evt_i < evt_num) {
2480 if (name_only) {
2481 printf("%s ", evt_list[evt_i++]);
2482 continue;
2483 }
2484 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
2485 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002486 if (evt_num && pager_in_use())
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002487 printf("\n");
Yunlong Songab0e4802015-02-27 18:21:25 +08002488
2489out_free:
2490 evt_num = evt_i;
2491 for (evt_i = 0; evt_i < evt_num; evt_i++)
2492 zfree(&evt_list[evt_i]);
2493 zfree(&evt_list);
2494 return;
2495
2496out_enomem:
2497 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
2498 if (evt_list)
2499 goto out_free;
Jiri Olsa1dc127602012-07-04 00:00:44 +02002500}
2501
2502/*
2503 * Print the help text for the event symbols:
2504 */
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -07002505void print_events(const char *event_glob, bool name_only, bool quiet_flag,
Andi Kleenbf874fc2017-03-20 13:17:11 -07002506 bool long_desc, bool details_flag)
Jiri Olsa1dc127602012-07-04 00:00:44 +02002507{
Jiri Olsa1dc127602012-07-04 00:00:44 +02002508 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002509 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
Jiri Olsa1dc127602012-07-04 00:00:44 +02002510
2511 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002512 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
Jiri Olsa1dc127602012-07-04 00:00:44 +02002513
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002514 print_hwcache_events(event_glob, name_only);
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002515
Andi Kleenbf874fc2017-03-20 13:17:11 -07002516 print_pmu_events(event_glob, name_only, quiet_flag, long_desc,
2517 details_flag);
Andi Kleendc098b32013-04-20 11:02:29 -07002518
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002519 if (event_glob != NULL)
2520 return;
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +05302521
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002522 if (!name_only) {
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002523 printf(" %-50s [%s]\n",
2524 "rNNN",
2525 event_type_descriptors[PERF_TYPE_RAW]);
2526 printf(" %-50s [%s]\n",
2527 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
2528 event_type_descriptors[PERF_TYPE_RAW]);
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002529 if (pager_in_use())
2530 printf(" (see 'man perf-list' on how to encode it)\n\n");
Thomas Gleixner86847b62009-06-06 12:24:17 +02002531
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002532 printf(" %-50s [%s]\n",
Jacob Shin3741eb92014-05-29 17:26:51 +02002533 "mem:<addr>[/len][:access]",
Liming Wang41bdcb22009-12-29 16:37:07 +08002534 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002535 if (pager_in_use())
2536 printf("\n");
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002537 }
Frederic Weisbecker1b290d62009-11-23 15:42:35 +01002538
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002539 print_tracepoint_events(NULL, NULL, name_only);
Masami Hiramatsu40218da2016-06-24 18:06:46 +09002540
2541 print_sdt_events(NULL, NULL, name_only);
Andi Kleen71b0acc2017-08-31 12:40:32 -07002542
Jiri Olsa33bbc572019-02-13 13:32:41 +01002543 metricgroup__print(true, true, NULL, name_only, details_flag);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002544}
Jiri Olsa8f707d82012-03-15 20:09:16 +01002545
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002546int parse_events__is_hardcoded_term(struct parse_events_term *term)
Jiri Olsa8f707d82012-03-15 20:09:16 +01002547{
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002548 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002549}
2550
Jiri Olsa67b49b32017-02-17 15:00:55 +01002551static int new_term(struct parse_events_term **_term,
2552 struct parse_events_term *temp,
2553 char *str, u64 num)
Jiri Olsa8f707d82012-03-15 20:09:16 +01002554{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002555 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002556
Jiri Olsa67b49b32017-02-17 15:00:55 +01002557 term = malloc(sizeof(*term));
Jiri Olsa8f707d82012-03-15 20:09:16 +01002558 if (!term)
2559 return -ENOMEM;
2560
Jiri Olsa67b49b32017-02-17 15:00:55 +01002561 *term = *temp;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002562 INIT_LIST_HEAD(&term->list);
Andi Kleenc2f1cea2017-10-20 13:27:55 -07002563 term->weak = false;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002564
Jiri Olsa67b49b32017-02-17 15:00:55 +01002565 switch (term->type_val) {
Jiri Olsa8f707d82012-03-15 20:09:16 +01002566 case PARSE_EVENTS__TERM_TYPE_NUM:
2567 term->val.num = num;
2568 break;
2569 case PARSE_EVENTS__TERM_TYPE_STR:
2570 term->val.str = str;
2571 break;
2572 default:
Adrian Hunter4be8be62013-07-04 16:20:24 +03002573 free(term);
Jiri Olsa8f707d82012-03-15 20:09:16 +01002574 return -EINVAL;
2575 }
2576
2577 *_term = term;
2578 return 0;
2579}
2580
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002581int parse_events_term__num(struct parse_events_term **term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002582 int type_term, char *config, u64 num,
Jiri Olsa99e71382017-02-17 15:00:56 +01002583 bool no_value,
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002584 void *loc_term_, void *loc_val_)
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002585{
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002586 YYLTYPE *loc_term = loc_term_;
2587 YYLTYPE *loc_val = loc_val_;
2588
Jiri Olsa67b49b32017-02-17 15:00:55 +01002589 struct parse_events_term temp = {
2590 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
2591 .type_term = type_term,
2592 .config = config,
Jiri Olsa99e71382017-02-17 15:00:56 +01002593 .no_value = no_value,
Jiri Olsa67b49b32017-02-17 15:00:55 +01002594 .err_term = loc_term ? loc_term->first_column : 0,
2595 .err_val = loc_val ? loc_val->first_column : 0,
2596 };
2597
2598 return new_term(term, &temp, NULL, num);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002599}
2600
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002601int parse_events_term__str(struct parse_events_term **term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002602 int type_term, char *config, char *str,
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002603 void *loc_term_, void *loc_val_)
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002604{
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002605 YYLTYPE *loc_term = loc_term_;
2606 YYLTYPE *loc_val = loc_val_;
2607
Jiri Olsa67b49b32017-02-17 15:00:55 +01002608 struct parse_events_term temp = {
2609 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
2610 .type_term = type_term,
2611 .config = config,
2612 .err_term = loc_term ? loc_term->first_column : 0,
2613 .err_val = loc_val ? loc_val->first_column : 0,
2614 };
2615
2616 return new_term(term, &temp, str, 0);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002617}
2618
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002619int parse_events_term__sym_hw(struct parse_events_term **term,
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02002620 char *config, unsigned idx)
2621{
2622 struct event_symbol *sym;
Jiri Olsa67b49b32017-02-17 15:00:55 +01002623 struct parse_events_term temp = {
2624 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
2625 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
2626 .config = config ?: (char *) "event",
2627 };
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02002628
2629 BUG_ON(idx >= PERF_COUNT_HW_MAX);
2630 sym = &event_symbols_hw[idx];
2631
Jiri Olsa67b49b32017-02-17 15:00:55 +01002632 return new_term(term, &temp, (char *) sym->symbol, 0);
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02002633}
2634
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002635int parse_events_term__clone(struct parse_events_term **new,
2636 struct parse_events_term *term)
Zheng Yana6146d52012-06-15 14:31:41 +08002637{
Jiri Olsa67b49b32017-02-17 15:00:55 +01002638 struct parse_events_term temp = {
2639 .type_val = term->type_val,
2640 .type_term = term->type_term,
2641 .config = term->config,
2642 .err_term = term->err_term,
2643 .err_val = term->err_val,
2644 };
2645
2646 return new_term(new, &temp, term->val.str, term->val.num);
Zheng Yana6146d52012-06-15 14:31:41 +08002647}
2648
Andi Kleen82557182017-03-20 13:17:03 -07002649int parse_events_copy_term_list(struct list_head *old,
2650 struct list_head **new)
2651{
2652 struct parse_events_term *term, *n;
2653 int ret;
2654
2655 if (!old) {
2656 *new = NULL;
2657 return 0;
2658 }
2659
2660 *new = malloc(sizeof(struct list_head));
2661 if (!*new)
2662 return -ENOMEM;
2663 INIT_LIST_HEAD(*new);
2664
2665 list_for_each_entry (term, old, list) {
2666 ret = parse_events_term__clone(&n, term);
2667 if (ret)
2668 return ret;
2669 list_add_tail(&n->list, *new);
2670 }
2671 return 0;
2672}
2673
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002674void parse_events_terms__purge(struct list_head *terms)
Jiri Olsa8f707d82012-03-15 20:09:16 +01002675{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002676 struct parse_events_term *term, *h;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002677
Wang Nana8adfce2016-02-12 16:31:23 -03002678 list_for_each_entry_safe(term, h, terms, list) {
Wang Nan2d055bf2016-02-22 09:10:34 +00002679 if (term->array.nr_ranges)
Taeung Song360e0712017-02-01 21:34:07 +09002680 zfree(&term->array.ranges);
Wang Nana8adfce2016-02-12 16:31:23 -03002681 list_del_init(&term->list);
Jiri Olsa8f707d82012-03-15 20:09:16 +01002682 free(term);
Wang Nana8adfce2016-02-12 16:31:23 -03002683 }
Jiri Olsa8f707d82012-03-15 20:09:16 +01002684}
Jiri Olsab39b8392015-04-22 21:10:16 +02002685
Arnaldo Carvalho de Melo2146afc2016-02-12 17:09:17 -03002686void parse_events_terms__delete(struct list_head *terms)
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002687{
Arnaldo Carvalho de Melo2146afc2016-02-12 17:09:17 -03002688 if (!terms)
2689 return;
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002690 parse_events_terms__purge(terms);
Wang Nand20a5f22016-02-12 17:01:17 -03002691 free(terms);
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002692}
2693
Wang Nan2d055bf2016-02-22 09:10:34 +00002694void parse_events__clear_array(struct parse_events_array *a)
2695{
Taeung Song360e0712017-02-01 21:34:07 +09002696 zfree(&a->ranges);
Wang Nan2d055bf2016-02-22 09:10:34 +00002697}
2698
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03002699void parse_events_evlist_error(struct parse_events_state *parse_state,
Jiri Olsab39b8392015-04-22 21:10:16 +02002700 int idx, const char *str)
2701{
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03002702 struct parse_events_error *err = parse_state->error;
Jiri Olsab39b8392015-04-22 21:10:16 +02002703
Adrian Huntera6ced2b2015-05-19 16:05:44 +03002704 if (!err)
2705 return;
Jiri Olsab39b8392015-04-22 21:10:16 +02002706 err->idx = idx;
2707 err->str = strdup(str);
2708 WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
2709}
He Kuangffeb8832015-09-28 03:52:14 +00002710
Wang Nan17cb5f82016-02-19 11:43:57 +00002711static void config_terms_list(char *buf, size_t buf_sz)
2712{
2713 int i;
2714 bool first = true;
2715
2716 buf[0] = '\0';
2717 for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
2718 const char *name = config_term_names[i];
2719
Wang Nan1669e502016-02-19 11:43:58 +00002720 if (!config_term_avail(i, NULL))
2721 continue;
Wang Nan17cb5f82016-02-19 11:43:57 +00002722 if (!name)
2723 continue;
2724 if (name[0] == '<')
2725 continue;
2726
2727 if (strlen(buf) + strlen(name) + 2 >= buf_sz)
2728 return;
2729
2730 if (!first)
2731 strcat(buf, ",");
2732 else
2733 first = false;
2734 strcat(buf, name);
2735 }
2736}
2737
He Kuangffeb8832015-09-28 03:52:14 +00002738/*
2739 * Return string contains valid config terms of an event.
2740 * @additional_terms: For terms such as PMU sysfs terms.
2741 */
2742char *parse_events_formats_error_string(char *additional_terms)
2743{
2744 char *str;
Wang Nan626a6b72016-07-14 08:34:45 +00002745 /* "no-overwrite" is the longest name */
Wang Nan17cb5f82016-02-19 11:43:57 +00002746 char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
Wang Nan626a6b72016-07-14 08:34:45 +00002747 (sizeof("no-overwrite") - 1)];
He Kuangffeb8832015-09-28 03:52:14 +00002748
Wang Nan17cb5f82016-02-19 11:43:57 +00002749 config_terms_list(static_terms, sizeof(static_terms));
He Kuangffeb8832015-09-28 03:52:14 +00002750 /* valid terms */
2751 if (additional_terms) {
Wang Nan26dee022016-02-19 11:43:52 +00002752 if (asprintf(&str, "valid terms: %s,%s",
2753 additional_terms, static_terms) < 0)
He Kuangffeb8832015-09-28 03:52:14 +00002754 goto fail;
2755 } else {
Wang Nan26dee022016-02-19 11:43:52 +00002756 if (asprintf(&str, "valid terms: %s", static_terms) < 0)
He Kuangffeb8832015-09-28 03:52:14 +00002757 goto fail;
2758 }
2759 return str;
2760
2761fail:
2762 return NULL;
2763}