blob: e111c0e0a5acb97f944864f58e8e4a4feabd288c [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
David Howellsd2709c72012-11-19 22:21:03 +00002#include <linux/hw_breakpoint.h>
Jiri Olsa8dd2a132015-09-07 10:38:06 +02003#include <linux/err.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -03004#include <linux/zalloc.h>
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -03005#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03006#include <errno.h>
Arnaldo Carvalho de Melo86a5e0c2017-04-19 19:03:14 -03007#include <sys/ioctl.h>
Arnaldo Carvalho de Melobafae982018-01-22 16:42:16 -03008#include <sys/types.h>
9#include <sys/stat.h>
10#include <fcntl.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030011#include <sys/param.h>
Arnaldo Carvalho de Melob0742e92017-04-18 11:08:10 -030012#include "term.h"
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -030013#include "../perf.h"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020014#include "evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020015#include "evsel.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060016#include <subcmd/parse-options.h>
Ingo Molnar8ad8db32009-05-26 11:10:09 +020017#include "parse-events.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060018#include <subcmd/exec-cmd.h>
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030019#include "string2.h"
Arnaldo Carvalho de Melo8ec20b12017-04-18 10:57:25 -030020#include "strlist.h"
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -030021#include "symbol.h"
Jason Baron5beeded2009-07-21 14:16:29 -040022#include "cache.h"
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020023#include "header.h"
Wang Nan84c86ca2015-10-14 12:41:14 +000024#include "bpf-loader.h"
Masami Hiramatsu6e81c742014-08-14 02:22:36 +000025#include "debug.h"
Jiri Olsa592d5a62015-09-02 09:56:34 +020026#include <api/fs/tracing_path.h>
Zheng Yanac20de6f2012-06-15 14:31:39 +080027#include "parse-events-bison.h"
Jiri Olsa90e2b222012-06-15 14:31:40 +080028#define YY_EXTRA_TYPE int
Jiri Olsa89812fc2012-03-15 20:09:15 +010029#include "parse-events-flex.h"
Jiri Olsa5f537a22012-03-15 20:09:18 +010030#include "pmu.h"
Namhyung Kimb41f1ce2013-08-27 11:41:53 +090031#include "thread_map.h"
Jiri Olsaf30a79b2015-06-23 00:36:04 +020032#include "cpumap.h"
Masami Hiramatsu40218da2016-06-24 18:06:46 +090033#include "probe-file.h"
Jiri Olsab39b8392015-04-22 21:10:16 +020034#include "asm/bug.h"
Andi Kleenac12f672016-10-12 14:02:06 -070035#include "util/parse-branch-options.h"
Andi Kleen71b0acc2017-08-31 12:40:32 -070036#include "metricgroup.h"
Jiri Olsa89812fc2012-03-15 20:09:15 +010037
38#define MAX_NAME_LEN 100
Ingo Molnar8ad8db32009-05-26 11:10:09 +020039
Jiri Olsa82ba1f22012-05-21 09:12:50 +020040#ifdef PARSER_DEBUG
41extern int parse_events_debug;
42#endif
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -030043int parse_events_parse(void *parse_state, void *scanner);
He Kuange637d172015-09-28 03:52:16 +000044static int get_config_terms(struct list_head *head_config,
45 struct list_head *head_terms __maybe_unused);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +020046
Kan Liangdcb4e102014-10-07 11:08:50 -040047static struct perf_pmu_event_symbol *perf_pmu_events_list;
48/*
49 * The variable indicates the number of supported pmu event symbols.
50 * 0 means not initialized and ready to init
51 * -1 means failed to init, don't try anymore
52 * >0 is the number of supported pmu event symbols
53 */
54static int perf_pmu_events_list_num;
55
Yunlong Song705750f2015-02-27 18:21:27 +080056struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
Jiri Olsa1dc127602012-07-04 00:00:44 +020057 [PERF_COUNT_HW_CPU_CYCLES] = {
58 .symbol = "cpu-cycles",
59 .alias = "cycles",
60 },
61 [PERF_COUNT_HW_INSTRUCTIONS] = {
62 .symbol = "instructions",
63 .alias = "",
64 },
65 [PERF_COUNT_HW_CACHE_REFERENCES] = {
66 .symbol = "cache-references",
67 .alias = "",
68 },
69 [PERF_COUNT_HW_CACHE_MISSES] = {
70 .symbol = "cache-misses",
71 .alias = "",
72 },
73 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
74 .symbol = "branch-instructions",
75 .alias = "branches",
76 },
77 [PERF_COUNT_HW_BRANCH_MISSES] = {
78 .symbol = "branch-misses",
79 .alias = "",
80 },
81 [PERF_COUNT_HW_BUS_CYCLES] = {
82 .symbol = "bus-cycles",
83 .alias = "",
84 },
85 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
86 .symbol = "stalled-cycles-frontend",
87 .alias = "idle-cycles-frontend",
88 },
89 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
90 .symbol = "stalled-cycles-backend",
91 .alias = "idle-cycles-backend",
92 },
93 [PERF_COUNT_HW_REF_CPU_CYCLES] = {
94 .symbol = "ref-cycles",
95 .alias = "",
96 },
97};
Ingo Molnar8ad8db32009-05-26 11:10:09 +020098
Yunlong Song705750f2015-02-27 18:21:27 +080099struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
Jiri Olsa1dc127602012-07-04 00:00:44 +0200100 [PERF_COUNT_SW_CPU_CLOCK] = {
101 .symbol = "cpu-clock",
102 .alias = "",
103 },
104 [PERF_COUNT_SW_TASK_CLOCK] = {
105 .symbol = "task-clock",
106 .alias = "",
107 },
108 [PERF_COUNT_SW_PAGE_FAULTS] = {
109 .symbol = "page-faults",
110 .alias = "faults",
111 },
112 [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
113 .symbol = "context-switches",
114 .alias = "cs",
115 },
116 [PERF_COUNT_SW_CPU_MIGRATIONS] = {
117 .symbol = "cpu-migrations",
118 .alias = "migrations",
119 },
120 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
121 .symbol = "minor-faults",
122 .alias = "",
123 },
124 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
125 .symbol = "major-faults",
126 .alias = "",
127 },
128 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
129 .symbol = "alignment-faults",
130 .alias = "",
131 },
132 [PERF_COUNT_SW_EMULATION_FAULTS] = {
133 .symbol = "emulation-faults",
134 .alias = "",
135 },
Adrian Hunterd22d1a22013-08-31 21:50:52 +0300136 [PERF_COUNT_SW_DUMMY] = {
137 .symbol = "dummy",
138 .alias = "",
139 },
Arnaldo Carvalho de Melobae9cc42015-11-27 15:54:33 -0300140 [PERF_COUNT_SW_BPF_OUTPUT] = {
141 .symbol = "bpf-output",
142 .alias = "",
143 },
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200144};
145
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200146#define __PERF_EVENT_FIELD(config, name) \
147 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
Ingo Molnar52425192009-05-26 09:17:18 +0200148
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200149#define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200150#define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200151#define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200152#define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
Ingo Molnar52425192009-05-26 09:17:18 +0200153
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300154#define for_each_subsystem(sys_dir, sys_dirent) \
155 while ((sys_dirent = readdir(sys_dir)) != NULL) \
156 if (sys_dirent->d_type == DT_DIR && \
157 (strcmp(sys_dirent->d_name, ".")) && \
158 (strcmp(sys_dirent->d_name, "..")))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400159
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -0300160static int tp_event_has_id(const char *dir_path, struct dirent *evt_dir)
Peter Zijlstraae07b632009-08-06 16:48:54 +0200161{
162 char evt_path[MAXPATHLEN];
163 int fd;
164
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -0300165 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, evt_dir->d_name);
Peter Zijlstraae07b632009-08-06 16:48:54 +0200166 fd = open(evt_path, O_RDONLY);
167 if (fd < 0)
168 return -EINVAL;
169 close(fd);
170
171 return 0;
172}
173
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -0300174#define for_each_event(dir_path, evt_dir, evt_dirent) \
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300175 while ((evt_dirent = readdir(evt_dir)) != NULL) \
176 if (evt_dirent->d_type == DT_DIR && \
177 (strcmp(evt_dirent->d_name, ".")) && \
178 (strcmp(evt_dirent->d_name, "..")) && \
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -0300179 (!tp_event_has_id(dir_path, evt_dirent)))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400180
Li Zefan270bbbe2009-09-17 16:34:51 +0800181#define MAX_EVENT_LENGTH 512
Jason Baronf6bdafe2009-07-21 12:20:22 -0400182
Jason Baronf6bdafe2009-07-21 12:20:22 -0400183
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200184struct tracepoint_path *tracepoint_id_to_path(u64 config)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400185{
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200186 struct tracepoint_path *path = NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400187 DIR *sys_dir, *evt_dir;
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300188 struct dirent *sys_dirent, *evt_dirent;
Stephane Eranian8aa8a7c2012-03-13 16:51:02 +0100189 char id_buf[24];
Eric Dumazet725b1362009-09-24 15:39:09 +0200190 int fd;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400191 u64 id;
192 char evt_path[MAXPATHLEN];
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300193 char *dir_path;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400194
Arnaldo Carvalho de Melo7014e0e2018-05-17 14:42:39 -0300195 sys_dir = tracing_events__opendir();
Jason Baronf6bdafe2009-07-21 12:20:22 -0400196 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200197 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400198
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300199 for_each_subsystem(sys_dir, sys_dirent) {
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300200 dir_path = get_events_file(sys_dirent->d_name);
201 if (!dir_path)
202 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200203 evt_dir = opendir(dir_path);
204 if (!evt_dir)
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300205 goto next;
Eric Dumazet725b1362009-09-24 15:39:09 +0200206
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -0300207 for_each_event(dir_path, evt_dir, evt_dirent) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200208
Jiri Olsa77f18152018-03-19 09:29:01 +0100209 scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
210 evt_dirent->d_name);
Eric Dumazet725b1362009-09-24 15:39:09 +0200211 fd = open(evt_path, O_RDONLY);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400212 if (fd < 0)
213 continue;
214 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
215 close(fd);
216 continue;
217 }
218 close(fd);
219 id = atoll(id_buf);
220 if (id == config) {
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300221 put_events_file(dir_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400222 closedir(evt_dir);
223 closedir(sys_dir);
Julia Lawall59b4cae2009-12-06 10:16:30 +0100224 path = zalloc(sizeof(*path));
Taeung Song5aa365f2017-02-01 21:34:05 +0900225 if (!path)
226 return NULL;
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200227 path->system = malloc(MAX_EVENT_LENGTH);
228 if (!path->system) {
229 free(path);
230 return NULL;
231 }
232 path->name = malloc(MAX_EVENT_LENGTH);
233 if (!path->name) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300234 zfree(&path->system);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200235 free(path);
236 return NULL;
237 }
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300238 strncpy(path->system, sys_dirent->d_name,
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200239 MAX_EVENT_LENGTH);
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300240 strncpy(path->name, evt_dirent->d_name,
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200241 MAX_EVENT_LENGTH);
242 return path;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400243 }
244 }
245 closedir(evt_dir);
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300246next:
247 put_events_file(dir_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400248 }
249
Jason Baronf6bdafe2009-07-21 12:20:22 -0400250 closedir(sys_dir);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200251 return NULL;
252}
253
Namhyung Kime7c93f02013-06-26 16:14:05 +0900254struct tracepoint_path *tracepoint_name_to_path(const char *name)
255{
256 struct tracepoint_path *path = zalloc(sizeof(*path));
257 char *str = strchr(name, ':');
258
259 if (path == NULL || str == NULL) {
260 free(path);
261 return NULL;
262 }
263
264 path->system = strndup(name, str - name);
265 path->name = strdup(str+1);
266
267 if (path->system == NULL || path->name == NULL) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300268 zfree(&path->system);
269 zfree(&path->name);
Taeung Song506fde12017-02-01 21:34:06 +0900270 zfree(&path);
Namhyung Kime7c93f02013-06-26 16:14:05 +0900271 }
272
273 return path;
274}
275
David Ahern1424dc92011-03-09 22:23:28 -0700276const char *event_type(int type)
277{
278 switch (type) {
279 case PERF_TYPE_HARDWARE:
280 return "hardware";
281
282 case PERF_TYPE_SOFTWARE:
283 return "software";
284
285 case PERF_TYPE_TRACEPOINT:
286 return "tracepoint";
287
288 case PERF_TYPE_HW_CACHE:
289 return "hardware-cache";
290
291 default:
292 break;
293 }
294
295 return "unknown";
296}
297
Wang Nane814fdd2016-02-19 11:43:59 +0000298static int parse_events__is_name_term(struct parse_events_term *term)
299{
300 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
301}
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800302
Wang Nane814fdd2016-02-19 11:43:59 +0000303static char *get_config_name(struct list_head *head_terms)
304{
305 struct parse_events_term *term;
306
307 if (!head_terms)
308 return NULL;
309
310 list_for_each_entry(term, head_terms, list)
311 if (parse_events__is_name_term(term))
312 return term->val.str;
313
314 return NULL;
315}
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800316
Jiri Olsa32dcd022019-07-21 13:23:51 +0200317static struct evsel *
Stephane Eranian410136f2013-11-12 17:58:49 +0100318__add_event(struct list_head *list, int *idx,
319 struct perf_event_attr *attr,
Mark Rutland66ec11912017-10-06 19:38:22 +0100320 char *name, struct perf_pmu *pmu,
Andi Kleenf0fbb112019-03-26 15:18:21 -0700321 struct list_head *config_terms, bool auto_merge_stats,
322 const char *cpu_list)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100323{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200324 struct evsel *evsel;
Jiri Olsaf8548392019-07-21 13:23:49 +0200325 struct perf_cpu_map *cpus = pmu ? pmu->cpus :
Andi Kleenf0fbb112019-03-26 15:18:21 -0700326 cpu_list ? cpu_map__new(cpu_list) : NULL;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100327
328 event_attr_init(attr);
329
Taeung Song75fc5ae2017-02-01 21:34:04 +0900330 evsel = perf_evsel__new_idx(attr, *idx);
David Ahernc5cd8ac2013-07-02 13:27:25 -0600331 if (!evsel)
Stephane Eranian410136f2013-11-12 17:58:49 +0100332 return NULL;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100333
Taeung Song75fc5ae2017-02-01 21:34:04 +0900334 (*idx)++;
Jiri Olsa38f01d82019-07-21 13:24:17 +0200335 evsel->cpus = perf_cpu_map__get(cpus);
336 evsel->own_cpus = perf_cpu_map__get(cpus);
Mark Rutland66ec11912017-10-06 19:38:22 +0100337 evsel->system_wide = pmu ? pmu->is_uncore : false;
Arnaldo Carvalho de Melo63ce8442017-08-31 15:32:18 -0300338 evsel->auto_merge_stats = auto_merge_stats;
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200339
Arnaldo Carvalho de Melo9db17632012-06-12 13:45:00 -0300340 if (name)
341 evsel->name = strdup(name);
Jiri Olsa930a2e22015-07-29 05:42:10 -0400342
343 if (config_terms)
344 list_splice(config_terms, &evsel->config_terms);
345
Jiri Olsab27c4ec2019-07-21 13:24:22 +0200346 list_add_tail(&evsel->core.node, list);
Stephane Eranian410136f2013-11-12 17:58:49 +0100347 return evsel;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100348}
349
David Ahernc5cd8ac2013-07-02 13:27:25 -0600350static int add_event(struct list_head *list, int *idx,
Jiri Olsa930a2e22015-07-29 05:42:10 -0400351 struct perf_event_attr *attr, char *name,
352 struct list_head *config_terms)
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800353{
Andi Kleenf0fbb112019-03-26 15:18:21 -0700354 return __add_event(list, idx, attr, name, NULL, config_terms, false, NULL) ? 0 : -ENOMEM;
355}
356
357static int add_event_tool(struct list_head *list, int *idx,
358 enum perf_tool_event tool_event)
359{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200360 struct evsel *evsel;
Andi Kleenf0fbb112019-03-26 15:18:21 -0700361 struct perf_event_attr attr = {
362 .type = PERF_TYPE_SOFTWARE,
363 .config = PERF_COUNT_SW_DUMMY,
364 };
365
366 evsel = __add_event(list, idx, &attr, NULL, NULL, NULL, false, "0");
367 if (!evsel)
368 return -ENOMEM;
369 evsel->tool_event = tool_event;
370 if (tool_event == PERF_TOOL_DURATION_TIME)
371 evsel->unit = strdup("ns");
372 return 0;
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800373}
374
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300375static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
Ingo Molnar8326f442009-06-05 20:22:46 +0200376{
377 int i, j;
Paul Mackerras61c45982009-07-01 13:04:34 +1000378 int n, longest = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200379
380 for (i = 0; i < size; i++) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300381 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
Paul Mackerras61c45982009-07-01 13:04:34 +1000382 n = strlen(names[i][j]);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100383 if (n > longest && !strncasecmp(str, names[i][j], n))
Paul Mackerras61c45982009-07-01 13:04:34 +1000384 longest = n;
385 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100386 if (longest > 0)
Paul Mackerras61c45982009-07-01 13:04:34 +1000387 return i;
Ingo Molnar8326f442009-06-05 20:22:46 +0200388 }
389
Ingo Molnar89536452009-06-06 21:04:17 +0200390 return -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200391}
392
Wang Nan43d0b972016-02-19 11:44:01 +0000393typedef int config_term_func_t(struct perf_event_attr *attr,
394 struct parse_events_term *term,
395 struct parse_events_error *err);
396static int config_term_common(struct perf_event_attr *attr,
397 struct parse_events_term *term,
398 struct parse_events_error *err);
399static int config_attr(struct perf_event_attr *attr,
400 struct list_head *head,
401 struct parse_events_error *err,
402 config_term_func_t config_term);
403
David Ahernc5cd8ac2013-07-02 13:27:25 -0600404int parse_events_add_cache(struct list_head *list, int *idx,
Wang Nan43d0b972016-02-19 11:44:01 +0000405 char *type, char *op_result1, char *op_result2,
Arnaldo Carvalho de Melo2c97b0d2016-02-19 19:47:04 -0300406 struct parse_events_error *err,
Wang Nan43d0b972016-02-19 11:44:01 +0000407 struct list_head *head_config)
Ingo Molnar8326f442009-06-05 20:22:46 +0200408{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100409 struct perf_event_attr attr;
Wang Nan43d0b972016-02-19 11:44:01 +0000410 LIST_HEAD(config_terms);
411 char name[MAX_NAME_LEN], *config_name;
Paul Mackerras61c45982009-07-01 13:04:34 +1000412 int cache_type = -1, cache_op = -1, cache_result = -1;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100413 char *op_result[2] = { op_result1, op_result2 };
414 int i, n;
Ingo Molnar8326f442009-06-05 20:22:46 +0200415
Ingo Molnar8326f442009-06-05 20:22:46 +0200416 /*
417 * No fallback - if we cannot get a clear cache type
418 * then bail out:
419 */
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300420 cache_type = parse_aliases(type, perf_evsel__hw_cache,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100421 PERF_COUNT_HW_CACHE_MAX);
Ingo Molnar8326f442009-06-05 20:22:46 +0200422 if (cache_type == -1)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100423 return -EINVAL;
Ingo Molnar8326f442009-06-05 20:22:46 +0200424
Wang Nan43d0b972016-02-19 11:44:01 +0000425 config_name = get_config_name(head_config);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100426 n = snprintf(name, MAX_NAME_LEN, "%s", type);
427
428 for (i = 0; (i < 2) && (op_result[i]); i++) {
429 char *str = op_result[i];
430
Jiri Olsa275ef382012-09-05 19:51:33 +0200431 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
Paul Mackerras61c45982009-07-01 13:04:34 +1000432
433 if (cache_op == -1) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300434 cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100435 PERF_COUNT_HW_CACHE_OP_MAX);
Paul Mackerras61c45982009-07-01 13:04:34 +1000436 if (cache_op >= 0) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300437 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
Jiri Olsa89812fc2012-03-15 20:09:15 +0100438 return -EINVAL;
Paul Mackerras61c45982009-07-01 13:04:34 +1000439 continue;
440 }
441 }
442
443 if (cache_result == -1) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300444 cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
445 PERF_COUNT_HW_CACHE_RESULT_MAX);
Paul Mackerras61c45982009-07-01 13:04:34 +1000446 if (cache_result >= 0)
447 continue;
448 }
Paul Mackerras61c45982009-07-01 13:04:34 +1000449 }
450
Ingo Molnar8326f442009-06-05 20:22:46 +0200451 /*
452 * Fall back to reads:
453 */
Ingo Molnar89536452009-06-06 21:04:17 +0200454 if (cache_op == -1)
455 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
Ingo Molnar8326f442009-06-05 20:22:46 +0200456
Ingo Molnar8326f442009-06-05 20:22:46 +0200457 /*
458 * Fall back to accesses:
459 */
460 if (cache_result == -1)
461 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
462
Jiri Olsa89812fc2012-03-15 20:09:15 +0100463 memset(&attr, 0, sizeof(attr));
464 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
465 attr.type = PERF_TYPE_HW_CACHE;
Wang Nan43d0b972016-02-19 11:44:01 +0000466
467 if (head_config) {
Arnaldo Carvalho de Melo2c97b0d2016-02-19 19:47:04 -0300468 if (config_attr(&attr, head_config, err,
Wang Nan43d0b972016-02-19 11:44:01 +0000469 config_term_common))
470 return -EINVAL;
471
472 if (get_config_terms(head_config, &config_terms))
473 return -ENOMEM;
474 }
475 return add_event(list, idx, &attr, config_name ? : name, &config_terms);
Ingo Molnar8326f442009-06-05 20:22:46 +0200476}
477
Jiri Olsa272ed292015-09-29 17:05:31 +0200478static void tracepoint_error(struct parse_events_error *e, int err,
Wang Nan8c619d62016-07-13 10:44:03 +0000479 const char *sys, const char *name)
Jiri Olsa19658172015-09-07 10:38:07 +0200480{
481 char help[BUFSIZ];
482
Adrian Hunterec183d22016-01-26 14:05:20 +0200483 if (!e)
484 return;
485
Jiri Olsa19658172015-09-07 10:38:07 +0200486 /*
487 * We get error directly from syscall errno ( > 0),
488 * or from encoded pointer's error ( < 0).
489 */
490 err = abs(err);
491
492 switch (err) {
493 case EACCES:
Jiri Olsa272ed292015-09-29 17:05:31 +0200494 e->str = strdup("can't access trace events");
Jiri Olsa19658172015-09-07 10:38:07 +0200495 break;
496 case ENOENT:
Jiri Olsa272ed292015-09-29 17:05:31 +0200497 e->str = strdup("unknown tracepoint");
Jiri Olsa19658172015-09-07 10:38:07 +0200498 break;
499 default:
Jiri Olsa272ed292015-09-29 17:05:31 +0200500 e->str = strdup("failed to add tracepoint");
Jiri Olsa19658172015-09-07 10:38:07 +0200501 break;
502 }
503
504 tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
Jiri Olsa272ed292015-09-29 17:05:31 +0200505 e->help = strdup(help);
Jiri Olsa19658172015-09-07 10:38:07 +0200506}
507
David Ahernc5cd8ac2013-07-02 13:27:25 -0600508static int add_tracepoint(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +0000509 const char *sys_name, const char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200510 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000511 struct list_head *head_config)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200512{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200513 struct evsel *evsel;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200514
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300515 evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
Jiri Olsa19658172015-09-07 10:38:07 +0200516 if (IS_ERR(evsel)) {
Jiri Olsa272ed292015-09-29 17:05:31 +0200517 tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200518 return PTR_ERR(evsel);
Jiri Olsa19658172015-09-07 10:38:07 +0200519 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200520
He Kuange637d172015-09-28 03:52:16 +0000521 if (head_config) {
522 LIST_HEAD(config_terms);
523
524 if (get_config_terms(head_config, &config_terms))
525 return -ENOMEM;
526 list_splice(&config_terms, &evsel->config_terms);
527 }
528
Jiri Olsab27c4ec2019-07-21 13:24:22 +0200529 list_add_tail(&evsel->core.node, list);
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300530 return 0;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200531}
532
David Ahernc5cd8ac2013-07-02 13:27:25 -0600533static int add_tracepoint_multi_event(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +0000534 const char *sys_name, const char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200535 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000536 struct list_head *head_config)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200537{
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300538 char *evt_path;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200539 struct dirent *evt_ent;
540 DIR *evt_dir;
Jiri Olsa27bf90b2015-10-05 21:31:17 +0200541 int ret = 0, found = 0;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200542
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300543 evt_path = get_events_file(sys_name);
544 if (!evt_path) {
545 tracepoint_error(err, errno, sys_name, evt_name);
546 return -1;
547 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200548 evt_dir = opendir(evt_path);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200549 if (!evt_dir) {
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300550 put_events_file(evt_path);
Jiri Olsa272ed292015-09-29 17:05:31 +0200551 tracepoint_error(err, errno, sys_name, evt_name);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100552 return -1;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200553 }
554
Jiri Olsa89812fc2012-03-15 20:09:15 +0100555 while (!ret && (evt_ent = readdir(evt_dir))) {
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200556 if (!strcmp(evt_ent->d_name, ".")
557 || !strcmp(evt_ent->d_name, "..")
558 || !strcmp(evt_ent->d_name, "enable")
559 || !strcmp(evt_ent->d_name, "filter"))
560 continue;
561
Jiri Olsa89812fc2012-03-15 20:09:15 +0100562 if (!strglobmatch(evt_ent->d_name, evt_name))
Masami Hiramatsufb1d2ed2010-01-05 17:47:17 -0500563 continue;
564
Jiri Olsa27bf90b2015-10-05 21:31:17 +0200565 found++;
566
He Kuange637d172015-09-28 03:52:16 +0000567 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200568 err, head_config);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200569 }
570
Jiri Olsa27bf90b2015-10-05 21:31:17 +0200571 if (!found) {
572 tracepoint_error(err, ENOENT, sys_name, evt_name);
573 ret = -1;
574 }
575
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300576 put_events_file(evt_path);
Jiri Olsa0bd3f082012-12-17 14:08:36 +0100577 closedir(evt_dir);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100578 return ret;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200579}
580
David Ahernc5cd8ac2013-07-02 13:27:25 -0600581static int add_tracepoint_event(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +0000582 const char *sys_name, const char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200583 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000584 struct list_head *head_config)
Jiri Olsaf35488f2012-12-17 14:08:37 +0100585{
586 return strpbrk(evt_name, "*?") ?
He Kuange637d172015-09-28 03:52:16 +0000587 add_tracepoint_multi_event(list, idx, sys_name, evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200588 err, head_config) :
He Kuange637d172015-09-28 03:52:16 +0000589 add_tracepoint(list, idx, sys_name, evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200590 err, head_config);
Jiri Olsaf35488f2012-12-17 14:08:37 +0100591}
592
David Ahernc5cd8ac2013-07-02 13:27:25 -0600593static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +0000594 const char *sys_name, const char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200595 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000596 struct list_head *head_config)
Jiri Olsaf35488f2012-12-17 14:08:37 +0100597{
598 struct dirent *events_ent;
599 DIR *events_dir;
600 int ret = 0;
601
Arnaldo Carvalho de Melo7014e0e2018-05-17 14:42:39 -0300602 events_dir = tracing_events__opendir();
Jiri Olsaf35488f2012-12-17 14:08:37 +0100603 if (!events_dir) {
Jiri Olsa272ed292015-09-29 17:05:31 +0200604 tracepoint_error(err, errno, sys_name, evt_name);
Jiri Olsaf35488f2012-12-17 14:08:37 +0100605 return -1;
606 }
607
608 while (!ret && (events_ent = readdir(events_dir))) {
609 if (!strcmp(events_ent->d_name, ".")
610 || !strcmp(events_ent->d_name, "..")
611 || !strcmp(events_ent->d_name, "enable")
612 || !strcmp(events_ent->d_name, "header_event")
613 || !strcmp(events_ent->d_name, "header_page"))
614 continue;
615
616 if (!strglobmatch(events_ent->d_name, sys_name))
617 continue;
618
619 ret = add_tracepoint_event(list, idx, events_ent->d_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200620 evt_name, err, head_config);
Jiri Olsaf35488f2012-12-17 14:08:37 +0100621 }
622
623 closedir(events_dir);
624 return ret;
625}
626
Wang Nan4edf30e2015-10-14 12:41:17 +0000627struct __add_bpf_event_param {
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300628 struct parse_events_state *parse_state;
Wang Nan4edf30e2015-10-14 12:41:17 +0000629 struct list_head *list;
Wang Nan95088a52016-02-22 09:10:36 +0000630 struct list_head *head_config;
Wang Nan4edf30e2015-10-14 12:41:17 +0000631};
632
Arnaldo Carvalho de Meloaf4a0992019-07-15 16:22:57 -0300633static int add_bpf_event(const char *group, const char *event, int fd, struct bpf_object *obj,
Wang Nan4edf30e2015-10-14 12:41:17 +0000634 void *_param)
635{
636 LIST_HEAD(new_evsels);
637 struct __add_bpf_event_param *param = _param;
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300638 struct parse_events_state *parse_state = param->parse_state;
Wang Nan4edf30e2015-10-14 12:41:17 +0000639 struct list_head *list = param->list;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200640 struct evsel *pos;
Wang Nan4edf30e2015-10-14 12:41:17 +0000641 int err;
Arnaldo Carvalho de Melo2620b7e2019-07-15 15:29:34 -0300642 /*
643 * Check if we should add the event, i.e. if it is a TP but starts with a '!',
644 * then don't add the tracepoint, this will be used for something else, like
645 * adding to a BPF_MAP_TYPE_PROG_ARRAY.
646 *
647 * See tools/perf/examples/bpf/augmented_raw_syscalls.c
648 */
649 if (group[0] == '!')
650 return 0;
Wang Nan4edf30e2015-10-14 12:41:17 +0000651
652 pr_debug("add bpf event %s:%s and attach bpf program %d\n",
Wang Nancd102d72016-07-13 10:44:04 +0000653 group, event, fd);
Wang Nan4edf30e2015-10-14 12:41:17 +0000654
Arnaldo Carvalho de Melo5d369a72017-08-17 14:13:25 -0300655 err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group,
656 event, parse_state->error,
Wang Nan95088a52016-02-22 09:10:36 +0000657 param->head_config);
Wang Nan4edf30e2015-10-14 12:41:17 +0000658 if (err) {
Jiri Olsa32dcd022019-07-21 13:23:51 +0200659 struct evsel *evsel, *tmp;
Wang Nan4edf30e2015-10-14 12:41:17 +0000660
661 pr_debug("Failed to add BPF event %s:%s\n",
Wang Nancd102d72016-07-13 10:44:04 +0000662 group, event);
Jiri Olsab27c4ec2019-07-21 13:24:22 +0200663 list_for_each_entry_safe(evsel, tmp, &new_evsels, core.node) {
664 list_del_init(&evsel->core.node);
Jiri Olsa5eb2dd22019-07-21 13:23:57 +0200665 evsel__delete(evsel);
Wang Nan4edf30e2015-10-14 12:41:17 +0000666 }
667 return err;
668 }
Wang Nancd102d72016-07-13 10:44:04 +0000669 pr_debug("adding %s:%s\n", group, event);
Wang Nan4edf30e2015-10-14 12:41:17 +0000670
Jiri Olsab27c4ec2019-07-21 13:24:22 +0200671 list_for_each_entry(pos, &new_evsels, core.node) {
Wang Nan1f45b1d2015-10-14 12:41:18 +0000672 pr_debug("adding %s:%s to %p\n",
Wang Nancd102d72016-07-13 10:44:04 +0000673 group, event, pos);
Wang Nan1f45b1d2015-10-14 12:41:18 +0000674 pos->bpf_fd = fd;
Arnaldo Carvalho de Meloaf4a0992019-07-15 16:22:57 -0300675 pos->bpf_obj = obj;
Wang Nan1f45b1d2015-10-14 12:41:18 +0000676 }
Wang Nan4edf30e2015-10-14 12:41:17 +0000677 list_splice(&new_evsels, list);
678 return 0;
679}
680
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300681int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
Wang Nan84c86ca2015-10-14 12:41:14 +0000682 struct list_head *list,
Wang Nan95088a52016-02-22 09:10:36 +0000683 struct bpf_object *obj,
684 struct list_head *head_config)
Wang Nan84c86ca2015-10-14 12:41:14 +0000685{
686 int err;
687 char errbuf[BUFSIZ];
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300688 struct __add_bpf_event_param param = {parse_state, list, head_config};
Wang Nanaa3abf32015-10-14 12:41:15 +0000689 static bool registered_unprobe_atexit = false;
Wang Nan84c86ca2015-10-14 12:41:14 +0000690
691 if (IS_ERR(obj) || !obj) {
692 snprintf(errbuf, sizeof(errbuf),
693 "Internal error: load bpf obj with NULL");
694 err = -EINVAL;
695 goto errout;
696 }
697
698 /*
Wang Nanaa3abf32015-10-14 12:41:15 +0000699 * Register atexit handler before calling bpf__probe() so
700 * bpf__probe() don't need to unprobe probe points its already
701 * created when failure.
702 */
703 if (!registered_unprobe_atexit) {
704 atexit(bpf__clear);
705 registered_unprobe_atexit = true;
706 }
707
708 err = bpf__probe(obj);
709 if (err) {
710 bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
711 goto errout;
712 }
713
Wang Nan1e5e3ee2015-10-14 12:41:16 +0000714 err = bpf__load(obj);
715 if (err) {
716 bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
717 goto errout;
718 }
719
Wang Nancd102d72016-07-13 10:44:04 +0000720 err = bpf__foreach_event(obj, add_bpf_event, &param);
Wang Nan4edf30e2015-10-14 12:41:17 +0000721 if (err) {
722 snprintf(errbuf, sizeof(errbuf),
723 "Attach events in BPF object failed");
724 goto errout;
725 }
726
727 return 0;
Wang Nan84c86ca2015-10-14 12:41:14 +0000728errout:
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300729 parse_state->error->help = strdup("(add -v to see detail)");
730 parse_state->error->str = strdup(errbuf);
Wang Nan84c86ca2015-10-14 12:41:14 +0000731 return err;
732}
733
Wang Nana34f3be2016-02-22 09:10:31 +0000734static int
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300735parse_events_config_bpf(struct parse_events_state *parse_state,
Wang Nana34f3be2016-02-22 09:10:31 +0000736 struct bpf_object *obj,
737 struct list_head *head_config)
738{
739 struct parse_events_term *term;
740 int error_pos;
741
742 if (!head_config || list_empty(head_config))
743 return 0;
744
745 list_for_each_entry(term, head_config, list) {
746 char errbuf[BUFSIZ];
747 int err;
748
749 if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) {
750 snprintf(errbuf, sizeof(errbuf),
751 "Invalid config term for BPF object");
752 errbuf[BUFSIZ - 1] = '\0';
753
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300754 parse_state->error->idx = term->err_term;
755 parse_state->error->str = strdup(errbuf);
Wang Nana34f3be2016-02-22 09:10:31 +0000756 return -EINVAL;
757 }
758
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300759 err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos);
Wang Nana34f3be2016-02-22 09:10:31 +0000760 if (err) {
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300761 bpf__strerror_config_obj(obj, term, parse_state->evlist,
Wang Nana34f3be2016-02-22 09:10:31 +0000762 &error_pos, err, errbuf,
763 sizeof(errbuf));
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300764 parse_state->error->help = strdup(
Wang Nan7630b3e2016-02-22 09:10:33 +0000765"Hint:\tValid config terms:\n"
Wang Nane571e022016-02-22 09:10:35 +0000766" \tmap:[<arraymap>].value<indices>=[value]\n"
767" \tmap:[<eventmap>].event<indices>=[event]\n"
Wang Nan7630b3e2016-02-22 09:10:33 +0000768"\n"
Wang Nane571e022016-02-22 09:10:35 +0000769" \twhere <indices> is something like [0,3...5] or [all]\n"
Wang Nana34f3be2016-02-22 09:10:31 +0000770" \t(add -v to see detail)");
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300771 parse_state->error->str = strdup(errbuf);
Wang Nana34f3be2016-02-22 09:10:31 +0000772 if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE)
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300773 parse_state->error->idx = term->err_val;
Wang Nana34f3be2016-02-22 09:10:31 +0000774 else
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300775 parse_state->error->idx = term->err_term + error_pos;
Wang Nana34f3be2016-02-22 09:10:31 +0000776 return err;
777 }
778 }
779 return 0;
780}
781
Wang Nan95088a52016-02-22 09:10:36 +0000782/*
783 * Split config terms:
784 * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
785 * 'call-graph=fp' is 'evt config', should be applied to each
786 * events in bpf.c.
787 * 'map:array.value[0]=1' is 'obj config', should be processed
788 * with parse_events_config_bpf.
789 *
790 * Move object config terms from the first list to obj_head_config.
791 */
792static void
793split_bpf_config_terms(struct list_head *evt_head_config,
794 struct list_head *obj_head_config)
795{
796 struct parse_events_term *term, *temp;
797
798 /*
799 * Currectly, all possible user config term
800 * belong to bpf object. parse_events__is_hardcoded_term()
801 * happends to be a good flag.
802 *
803 * See parse_events_config_bpf() and
804 * config_term_tracepoint().
805 */
806 list_for_each_entry_safe(term, temp, evt_head_config, list)
807 if (!parse_events__is_hardcoded_term(term))
808 list_move_tail(&term->list, obj_head_config);
809}
810
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300811int parse_events_load_bpf(struct parse_events_state *parse_state,
Wang Nan84c86ca2015-10-14 12:41:14 +0000812 struct list_head *list,
Wang Nand509db02015-10-14 12:41:20 +0000813 char *bpf_file_name,
Wang Nana34f3be2016-02-22 09:10:31 +0000814 bool source,
815 struct list_head *head_config)
Wang Nan84c86ca2015-10-14 12:41:14 +0000816{
Wang Nana34f3be2016-02-22 09:10:31 +0000817 int err;
Wang Nan95088a52016-02-22 09:10:36 +0000818 struct bpf_object *obj;
819 LIST_HEAD(obj_head_config);
820
821 if (head_config)
822 split_bpf_config_terms(head_config, &obj_head_config);
Wang Nan84c86ca2015-10-14 12:41:14 +0000823
Wang Nand509db02015-10-14 12:41:20 +0000824 obj = bpf__prepare_load(bpf_file_name, source);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000825 if (IS_ERR(obj)) {
Wang Nan84c86ca2015-10-14 12:41:14 +0000826 char errbuf[BUFSIZ];
Wang Nan84c86ca2015-10-14 12:41:14 +0000827
Wang Nan6371ca3b2015-11-06 13:49:37 +0000828 err = PTR_ERR(obj);
Wang Nan84c86ca2015-10-14 12:41:14 +0000829
830 if (err == -ENOTSUP)
831 snprintf(errbuf, sizeof(errbuf),
832 "BPF support is not compiled");
833 else
Wang Nand3e0ce32015-11-06 13:58:09 +0000834 bpf__strerror_prepare_load(bpf_file_name,
835 source,
836 -err, errbuf,
837 sizeof(errbuf));
Wang Nan84c86ca2015-10-14 12:41:14 +0000838
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300839 parse_state->error->help = strdup("(add -v to see detail)");
840 parse_state->error->str = strdup(errbuf);
Wang Nan84c86ca2015-10-14 12:41:14 +0000841 return err;
842 }
843
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300844 err = parse_events_load_bpf_obj(parse_state, list, obj, head_config);
Wang Nana34f3be2016-02-22 09:10:31 +0000845 if (err)
846 return err;
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300847 err = parse_events_config_bpf(parse_state, obj, &obj_head_config);
Wang Nan95088a52016-02-22 09:10:36 +0000848
849 /*
850 * Caller doesn't know anything about obj_head_config,
851 * so combine them together again before returnning.
852 */
853 if (head_config)
854 list_splice_tail(&obj_head_config, head_config);
855 return err;
Wang Nan84c86ca2015-10-14 12:41:14 +0000856}
857
Jiri Olsa89812fc2012-03-15 20:09:15 +0100858static int
859parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100860{
861 int i;
862
863 for (i = 0; i < 3; i++) {
Jiri Olsa89812fc2012-03-15 20:09:15 +0100864 if (!type || !type[i])
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100865 break;
866
Jiri Olsa75827322012-06-29 09:22:54 +0200867#define CHECK_SET_TYPE(bit) \
868do { \
869 if (attr->bp_type & bit) \
870 return -EINVAL; \
871 else \
872 attr->bp_type |= bit; \
873} while (0)
874
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100875 switch (type[i]) {
876 case 'r':
Jiri Olsa75827322012-06-29 09:22:54 +0200877 CHECK_SET_TYPE(HW_BREAKPOINT_R);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100878 break;
879 case 'w':
Jiri Olsa75827322012-06-29 09:22:54 +0200880 CHECK_SET_TYPE(HW_BREAKPOINT_W);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100881 break;
882 case 'x':
Jiri Olsa75827322012-06-29 09:22:54 +0200883 CHECK_SET_TYPE(HW_BREAKPOINT_X);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100884 break;
885 default:
Jiri Olsa89812fc2012-03-15 20:09:15 +0100886 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100887 }
888 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100889
Jiri Olsa75827322012-06-29 09:22:54 +0200890#undef CHECK_SET_TYPE
891
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100892 if (!attr->bp_type) /* Default */
893 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
894
Jiri Olsa89812fc2012-03-15 20:09:15 +0100895 return 0;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100896}
897
David Ahernc5cd8ac2013-07-02 13:27:25 -0600898int parse_events_add_breakpoint(struct list_head *list, int *idx,
Jacob Shin3741eb92014-05-29 17:26:51 +0200899 void *ptr, char *type, u64 len)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100900{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100901 struct perf_event_attr attr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100902
Jiri Olsa89812fc2012-03-15 20:09:15 +0100903 memset(&attr, 0, sizeof(attr));
Jiri Olsa9fafd982012-03-20 19:15:39 +0100904 attr.bp_addr = (unsigned long) ptr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100905
Jiri Olsa89812fc2012-03-15 20:09:15 +0100906 if (parse_breakpoint_type(type, &attr))
907 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100908
Jacob Shin3741eb92014-05-29 17:26:51 +0200909 /* Provide some defaults if len is not specified */
910 if (!len) {
911 if (attr.bp_type == HW_BREAKPOINT_X)
912 len = sizeof(long);
913 else
914 len = HW_BREAKPOINT_LEN_4;
915 }
916
917 attr.bp_len = len;
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200918
Jiri Olsa89812fc2012-03-15 20:09:15 +0100919 attr.type = PERF_TYPE_BREAKPOINT;
Jovi Zhang4a841d62012-07-15 03:03:10 +0800920 attr.sample_period = 1;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100921
Jiri Olsa930a2e22015-07-29 05:42:10 -0400922 return add_event(list, idx, &attr, NULL, NULL);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100923}
924
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200925static int check_type_val(struct parse_events_term *term,
926 struct parse_events_error *err,
927 int type)
Jiri Olsa8f707d82012-03-15 20:09:16 +0100928{
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200929 if (type == term->type_val)
930 return 0;
931
932 if (err) {
933 err->idx = term->err_val;
934 if (type == PARSE_EVENTS__TERM_TYPE_NUM)
935 err->str = strdup("expected numeric value");
936 else
937 err->str = strdup("expected string value");
938 }
939 return -EINVAL;
940}
941
Wang Nan17cb5f82016-02-19 11:43:57 +0000942/*
943 * Update according to parse-events.l
944 */
945static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
946 [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>",
947 [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config",
948 [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1",
949 [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2",
950 [PARSE_EVENTS__TERM_TYPE_NAME] = "name",
951 [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period",
952 [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq",
953 [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type",
954 [PARSE_EVENTS__TERM_TYPE_TIME] = "time",
955 [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph",
956 [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size",
957 [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit",
958 [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit",
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300959 [PARSE_EVENTS__TERM_TYPE_MAX_STACK] = "max-stack",
Arnaldo Carvalho de Melo2fda5ad2018-10-19 15:47:34 -0300960 [PARSE_EVENTS__TERM_TYPE_MAX_EVENTS] = "nr",
Wang Nan626a6b72016-07-14 08:34:45 +0000961 [PARSE_EVENTS__TERM_TYPE_OVERWRITE] = "overwrite",
962 [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE] = "no-overwrite",
Mathieu Poirierdd60fba2016-09-06 10:37:15 -0600963 [PARSE_EVENTS__TERM_TYPE_DRV_CFG] = "driver-config",
Jin Yao064b4e82019-04-12 21:59:47 +0800964 [PARSE_EVENTS__TERM_TYPE_PERCORE] = "percore",
Wang Nan17cb5f82016-02-19 11:43:57 +0000965};
966
Wang Nan1669e502016-02-19 11:43:58 +0000967static bool config_term_shrinked;
968
969static bool
970config_term_avail(int term_type, struct parse_events_error *err)
971{
972 if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
973 err->str = strdup("Invalid term_type");
974 return false;
975 }
976 if (!config_term_shrinked)
977 return true;
978
979 switch (term_type) {
980 case PARSE_EVENTS__TERM_TYPE_CONFIG:
981 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
982 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
983 case PARSE_EVENTS__TERM_TYPE_NAME:
Sukadev Bhattiprolu06835542016-09-15 15:24:56 -0700984 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
Jin Yao064b4e82019-04-12 21:59:47 +0800985 case PARSE_EVENTS__TERM_TYPE_PERCORE:
Wang Nan1669e502016-02-19 11:43:58 +0000986 return true;
987 default:
988 if (!err)
989 return false;
990
991 /* term_type is validated so indexing is safe */
992 if (asprintf(&err->str, "'%s' is not usable in 'perf stat'",
993 config_term_names[term_type]) < 0)
994 err->str = NULL;
995 return false;
996 }
997}
998
999void parse_events__shrink_config_terms(void)
1000{
1001 config_term_shrinked = true;
1002}
1003
He Kuang0b8891a2015-09-28 03:52:13 +00001004static int config_term_common(struct perf_event_attr *attr,
1005 struct parse_events_term *term,
1006 struct parse_events_error *err)
Jiri Olsa3b0e3712015-04-22 21:10:22 +02001007{
1008#define CHECK_TYPE_VAL(type) \
1009do { \
1010 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
1011 return -EINVAL; \
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001012} while (0)
1013
1014 switch (term->type_term) {
Jiri Olsa8f707d82012-03-15 20:09:16 +01001015 case PARSE_EVENTS__TERM_TYPE_CONFIG:
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001016 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001017 attr->config = term->val.num;
1018 break;
1019 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001020 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001021 attr->config1 = term->val.num;
1022 break;
1023 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001024 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001025 attr->config2 = term->val.num;
1026 break;
1027 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001028 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001029 break;
Namhyung Kim09af2a52015-08-09 15:45:23 +09001030 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1031 CHECK_TYPE_VAL(NUM);
1032 break;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001033 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
Andi Kleenac12f672016-10-12 14:02:06 -07001034 CHECK_TYPE_VAL(STR);
1035 if (strcmp(term->val.str, "no") &&
1036 parse_branch_str(term->val.str, &attr->branch_sample_type)) {
1037 err->str = strdup("invalid branch sample type");
1038 err->idx = term->err_val;
1039 return -EINVAL;
1040 }
Jiri Olsa8f707d82012-03-15 20:09:16 +01001041 break;
Kan Liang32067712015-08-04 04:30:19 -04001042 case PARSE_EVENTS__TERM_TYPE_TIME:
1043 CHECK_TYPE_VAL(NUM);
1044 if (term->val.num > 1) {
1045 err->str = strdup("expected 0 or 1");
1046 err->idx = term->err_val;
1047 return -EINVAL;
1048 }
1049 break;
Kan Liangd457c962015-08-11 06:30:47 -04001050 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1051 CHECK_TYPE_VAL(STR);
1052 break;
1053 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1054 CHECK_TYPE_VAL(NUM);
1055 break;
Wang Nan374ce932015-10-28 10:55:02 +00001056 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1057 CHECK_TYPE_VAL(NUM);
1058 break;
1059 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1060 CHECK_TYPE_VAL(NUM);
1061 break;
Wang Nan626a6b72016-07-14 08:34:45 +00001062 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1063 CHECK_TYPE_VAL(NUM);
1064 break;
1065 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1066 CHECK_TYPE_VAL(NUM);
1067 break;
Jiri Olsa6b5fc392012-05-21 09:12:53 +02001068 case PARSE_EVENTS__TERM_TYPE_NAME:
1069 CHECK_TYPE_VAL(STR);
1070 break;
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -03001071 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1072 CHECK_TYPE_VAL(NUM);
1073 break;
Arnaldo Carvalho de Melo2fda5ad2018-10-19 15:47:34 -03001074 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1075 CHECK_TYPE_VAL(NUM);
1076 break;
Jin Yao064b4e82019-04-12 21:59:47 +08001077 case PARSE_EVENTS__TERM_TYPE_PERCORE:
1078 CHECK_TYPE_VAL(NUM);
1079 if ((unsigned int)term->val.num > 1) {
1080 err->str = strdup("expected 0 or 1");
1081 err->idx = term->err_val;
1082 return -EINVAL;
1083 }
1084 break;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001085 default:
He Kuangffeb8832015-09-28 03:52:14 +00001086 err->str = strdup("unknown term");
1087 err->idx = term->err_term;
1088 err->help = parse_events_formats_error_string(NULL);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001089 return -EINVAL;
1090 }
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001091
Wang Nan1669e502016-02-19 11:43:58 +00001092 /*
1093 * Check term availbility after basic checking so
1094 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
1095 *
1096 * If check availbility at the entry of this function,
1097 * user will see "'<sysfs term>' is not usable in 'perf stat'"
1098 * if an invalid config term is provided for legacy events
1099 * (for example, instructions/badterm/...), which is confusing.
1100 */
1101 if (!config_term_avail(term->type_term, err))
1102 return -EINVAL;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001103 return 0;
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001104#undef CHECK_TYPE_VAL
Jiri Olsa8f707d82012-03-15 20:09:16 +01001105}
1106
He Kuang0b8891a2015-09-28 03:52:13 +00001107static int config_term_pmu(struct perf_event_attr *attr,
1108 struct parse_events_term *term,
1109 struct parse_events_error *err)
1110{
Mathieu Poirierdd60fba2016-09-06 10:37:15 -06001111 if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER ||
1112 term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG)
He Kuang0b8891a2015-09-28 03:52:13 +00001113 /*
1114 * Always succeed for sysfs terms, as we dont know
1115 * at this point what type they need to have.
1116 */
1117 return 0;
1118 else
1119 return config_term_common(attr, term, err);
1120}
1121
He Kuange637d172015-09-28 03:52:16 +00001122static int config_term_tracepoint(struct perf_event_attr *attr,
1123 struct parse_events_term *term,
1124 struct parse_events_error *err)
1125{
1126 switch (term->type_term) {
1127 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1128 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
Wang Nan374ce932015-10-28 10:55:02 +00001129 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1130 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -03001131 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
Arnaldo Carvalho de Melo2fda5ad2018-10-19 15:47:34 -03001132 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
Wang Nan626a6b72016-07-14 08:34:45 +00001133 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1134 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
He Kuange637d172015-09-28 03:52:16 +00001135 return config_term_common(attr, term, err);
1136 default:
1137 if (err) {
1138 err->idx = term->err_term;
1139 err->str = strdup("unknown term");
1140 err->help = strdup("valid terms: call-graph,stack-size\n");
1141 }
1142 return -EINVAL;
1143 }
1144
1145 return 0;
1146}
1147
Jiri Olsa8f707d82012-03-15 20:09:16 +01001148static int config_attr(struct perf_event_attr *attr,
Jiri Olsa3b0e3712015-04-22 21:10:22 +02001149 struct list_head *head,
He Kuang0b8891a2015-09-28 03:52:13 +00001150 struct parse_events_error *err,
1151 config_term_func_t config_term)
Jiri Olsa8f707d82012-03-15 20:09:16 +01001152{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001153 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001154
1155 list_for_each_entry(term, head, list)
Jiri Olsa3b0e3712015-04-22 21:10:22 +02001156 if (config_term(attr, term, err))
Jiri Olsa8f707d82012-03-15 20:09:16 +01001157 return -EINVAL;
1158
1159 return 0;
1160}
1161
Jiri Olsa930a2e22015-07-29 05:42:10 -04001162static int get_config_terms(struct list_head *head_config,
1163 struct list_head *head_terms __maybe_unused)
1164{
1165#define ADD_CONFIG_TERM(__type, __name, __val) \
1166do { \
1167 struct perf_evsel_config_term *__t; \
1168 \
1169 __t = zalloc(sizeof(*__t)); \
1170 if (!__t) \
1171 return -ENOMEM; \
1172 \
1173 INIT_LIST_HEAD(&__t->list); \
1174 __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \
1175 __t->val.__name = __val; \
Andi Kleenc2f1cea2017-10-20 13:27:55 -07001176 __t->weak = term->weak; \
Jiri Olsa930a2e22015-07-29 05:42:10 -04001177 list_add_tail(&__t->list, head_terms); \
1178} while (0)
1179
1180 struct parse_events_term *term;
1181
1182 list_for_each_entry(term, head_config, list) {
1183 switch (term->type_term) {
Jiri Olsaee4c7582015-07-29 05:42:11 -04001184 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1185 ADD_CONFIG_TERM(PERIOD, period, term->val.num);
Kan Liang32067712015-08-04 04:30:19 -04001186 break;
Namhyung Kim09af2a52015-08-09 15:45:23 +09001187 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1188 ADD_CONFIG_TERM(FREQ, freq, term->val.num);
1189 break;
Kan Liang32067712015-08-04 04:30:19 -04001190 case PARSE_EVENTS__TERM_TYPE_TIME:
1191 ADD_CONFIG_TERM(TIME, time, term->val.num);
1192 break;
Kan Liangd457c962015-08-11 06:30:47 -04001193 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1194 ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
1195 break;
Andi Kleenac12f672016-10-12 14:02:06 -07001196 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1197 ADD_CONFIG_TERM(BRANCH, branch, term->val.str);
1198 break;
Kan Liangd457c962015-08-11 06:30:47 -04001199 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1200 ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
1201 break;
Wang Nan374ce932015-10-28 10:55:02 +00001202 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1203 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0);
1204 break;
1205 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1206 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1);
1207 break;
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -03001208 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1209 ADD_CONFIG_TERM(MAX_STACK, max_stack, term->val.num);
1210 break;
Arnaldo Carvalho de Melo2fda5ad2018-10-19 15:47:34 -03001211 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1212 ADD_CONFIG_TERM(MAX_EVENTS, max_events, term->val.num);
1213 break;
Wang Nan626a6b72016-07-14 08:34:45 +00001214 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1215 ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 1 : 0);
1216 break;
1217 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1218 ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 0 : 1);
1219 break;
Mathieu Poirierdd60fba2016-09-06 10:37:15 -06001220 case PARSE_EVENTS__TERM_TYPE_DRV_CFG:
1221 ADD_CONFIG_TERM(DRV_CFG, drv_cfg, term->val.str);
1222 break;
Jin Yao064b4e82019-04-12 21:59:47 +08001223 case PARSE_EVENTS__TERM_TYPE_PERCORE:
1224 ADD_CONFIG_TERM(PERCORE, percore,
1225 term->val.num ? true : false);
1226 break;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001227 default:
1228 break;
1229 }
1230 }
1231#undef ADD_EVSEL_CONFIG
1232 return 0;
1233}
1234
He Kuange637d172015-09-28 03:52:16 +00001235int parse_events_add_tracepoint(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +00001236 const char *sys, const char *event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001237 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +00001238 struct list_head *head_config)
1239{
1240 if (head_config) {
1241 struct perf_event_attr attr;
1242
Jiri Olsa272ed292015-09-29 17:05:31 +02001243 if (config_attr(&attr, head_config, err,
He Kuange637d172015-09-28 03:52:16 +00001244 config_term_tracepoint))
1245 return -EINVAL;
1246 }
1247
1248 if (strpbrk(sys, "*?"))
1249 return add_tracepoint_multi_sys(list, idx, sys, event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001250 err, head_config);
He Kuange637d172015-09-28 03:52:16 +00001251 else
1252 return add_tracepoint_event(list, idx, sys, event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001253 err, head_config);
He Kuange637d172015-09-28 03:52:16 +00001254}
1255
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001256int parse_events_add_numeric(struct parse_events_state *parse_state,
Jiri Olsa87d650b2015-04-22 21:10:24 +02001257 struct list_head *list,
Robert Richterb527bab2012-08-07 19:43:13 +02001258 u32 type, u64 config,
Jiri Olsa8f707d82012-03-15 20:09:16 +01001259 struct list_head *head_config)
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301260{
Jiri Olsa89812fc2012-03-15 20:09:15 +01001261 struct perf_event_attr attr;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001262 LIST_HEAD(config_terms);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301263
Jiri Olsa89812fc2012-03-15 20:09:15 +01001264 memset(&attr, 0, sizeof(attr));
1265 attr.type = type;
1266 attr.config = config;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001267
Jiri Olsa930a2e22015-07-29 05:42:10 -04001268 if (head_config) {
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001269 if (config_attr(&attr, head_config, parse_state->error,
He Kuang0b8891a2015-09-28 03:52:13 +00001270 config_term_common))
Jiri Olsa930a2e22015-07-29 05:42:10 -04001271 return -EINVAL;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001272
Jiri Olsa930a2e22015-07-29 05:42:10 -04001273 if (get_config_terms(head_config, &config_terms))
1274 return -ENOMEM;
1275 }
1276
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001277 return add_event(list, &parse_state->idx, &attr,
Wang Nan10bf3582016-02-19 11:44:00 +00001278 get_config_name(head_config), &config_terms);
Paul Mackerras61c45982009-07-01 13:04:34 +10001279}
1280
Andi Kleenf0fbb112019-03-26 15:18:21 -07001281int parse_events_add_tool(struct parse_events_state *parse_state,
1282 struct list_head *list,
1283 enum perf_tool_event tool_event)
1284{
1285 return add_event_tool(list, &parse_state->idx, tool_event);
1286}
1287
Jin Yao064b4e82019-04-12 21:59:47 +08001288static bool config_term_percore(struct list_head *config_terms)
1289{
1290 struct perf_evsel_config_term *term;
1291
1292 list_for_each_entry(term, config_terms, list) {
1293 if (term->type == PERF_EVSEL__CONFIG_TERM_PERCORE)
1294 return term->val.percore;
1295 }
1296
1297 return false;
1298}
1299
Agustin Vega-Friasc199c112018-03-06 09:04:44 -05001300int parse_events_add_pmu(struct parse_events_state *parse_state,
Jiri Olsa36adec82015-04-22 21:10:19 +02001301 struct list_head *list, char *name,
Kan Liang3cdc5c22018-05-07 14:13:43 -07001302 struct list_head *head_config,
1303 bool auto_merge_stats,
1304 bool use_alias)
Jiri Olsa5f537a22012-03-15 20:09:18 +01001305{
1306 struct perf_event_attr attr;
Matt Fleming46441bd2014-09-24 15:04:06 +01001307 struct perf_pmu_info info;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001308 struct perf_pmu *pmu;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001309 struct evsel *evsel;
Andi Kleen333b5662017-09-13 14:50:06 -07001310 struct parse_events_error *err = parse_state->error;
Kan Liang3cdc5c22018-05-07 14:13:43 -07001311 bool use_uncore_alias;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001312 LIST_HEAD(config_terms);
Jiri Olsa5f537a22012-03-15 20:09:18 +01001313
1314 pmu = perf_pmu__find(name);
Andi Kleen333b5662017-09-13 14:50:06 -07001315 if (!pmu) {
1316 if (asprintf(&err->str,
1317 "Cannot find PMU `%s'. Missing kernel support?",
1318 name) < 0)
1319 err->str = NULL;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001320 return -EINVAL;
Andi Kleen333b5662017-09-13 14:50:06 -07001321 }
Jiri Olsa5f537a22012-03-15 20:09:18 +01001322
Adrian Hunterdc0a6202014-07-31 09:00:49 +03001323 if (pmu->default_config) {
1324 memcpy(&attr, pmu->default_config,
1325 sizeof(struct perf_event_attr));
1326 } else {
1327 memset(&attr, 0, sizeof(attr));
1328 }
Jiri Olsa5f537a22012-03-15 20:09:18 +01001329
Kan Liang3cdc5c22018-05-07 14:13:43 -07001330 use_uncore_alias = (pmu->is_uncore && use_alias);
1331
Adrian Hunterad962272014-08-15 22:08:40 +03001332 if (!head_config) {
1333 attr.type = pmu->type;
Andi Kleenf0fbb112019-03-26 15:18:21 -07001334 evsel = __add_event(list, &parse_state->idx, &attr, NULL, pmu, NULL,
1335 auto_merge_stats, NULL);
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001336 if (evsel) {
1337 evsel->pmu_name = name;
Kan Liang3cdc5c22018-05-07 14:13:43 -07001338 evsel->use_uncore_alias = use_uncore_alias;
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001339 return 0;
1340 } else {
1341 return -ENOMEM;
1342 }
Adrian Hunterad962272014-08-15 22:08:40 +03001343 }
1344
Matt Fleming46441bd2014-09-24 15:04:06 +01001345 if (perf_pmu__check_alias(pmu, head_config, &info))
Zheng Yana6146d52012-06-15 14:31:41 +08001346 return -EINVAL;
1347
Jiri Olsa5f537a22012-03-15 20:09:18 +01001348 /*
1349 * Configure hardcoded terms first, no need to check
1350 * return value when called with fail == 0 ;)
1351 */
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001352 if (config_attr(&attr, head_config, parse_state->error, config_term_pmu))
Jiri Olsac056ba6a2015-04-22 21:10:18 +02001353 return -EINVAL;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001354
Jiri Olsa930a2e22015-07-29 05:42:10 -04001355 if (get_config_terms(head_config, &config_terms))
1356 return -ENOMEM;
1357
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001358 if (perf_pmu__config(pmu, &attr, head_config, parse_state->error))
Jiri Olsa5f537a22012-03-15 20:09:18 +01001359 return -EINVAL;
1360
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001361 evsel = __add_event(list, &parse_state->idx, &attr,
Mark Rutland66ec11912017-10-06 19:38:22 +01001362 get_config_name(head_config), pmu,
Andi Kleenf0fbb112019-03-26 15:18:21 -07001363 &config_terms, auto_merge_stats, NULL);
Stephane Eranian410136f2013-11-12 17:58:49 +01001364 if (evsel) {
Matt Fleming46441bd2014-09-24 15:04:06 +01001365 evsel->unit = info.unit;
1366 evsel->scale = info.scale;
Matt Fleming044330c2014-11-21 10:31:12 +01001367 evsel->per_pkg = info.per_pkg;
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001368 evsel->snapshot = info.snapshot;
Andi Kleen37932c12017-03-20 13:17:08 -07001369 evsel->metric_expr = info.metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001370 evsel->metric_name = info.metric_name;
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001371 evsel->pmu_name = name;
Kan Liang3cdc5c22018-05-07 14:13:43 -07001372 evsel->use_uncore_alias = use_uncore_alias;
Jin Yao064b4e82019-04-12 21:59:47 +08001373 evsel->percore = config_term_percore(&evsel->config_terms);
Stephane Eranian410136f2013-11-12 17:58:49 +01001374 }
1375
1376 return evsel ? 0 : -ENOMEM;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001377}
1378
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001379int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
Andi Kleen2073ad32017-03-20 13:17:02 -07001380 char *str, struct list_head **listp)
1381{
1382 struct list_head *head;
1383 struct parse_events_term *term;
1384 struct list_head *list;
1385 struct perf_pmu *pmu = NULL;
1386 int ok = 0;
1387
1388 *listp = NULL;
1389 /* Add it for all PMUs that support the alias */
1390 list = malloc(sizeof(struct list_head));
1391 if (!list)
1392 return -1;
1393 INIT_LIST_HEAD(list);
1394 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1395 struct perf_pmu_alias *alias;
1396
1397 list_for_each_entry(alias, &pmu->aliases, list) {
1398 if (!strcasecmp(alias->name, str)) {
1399 head = malloc(sizeof(struct list_head));
1400 if (!head)
1401 return -1;
1402 INIT_LIST_HEAD(head);
1403 if (parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
1404 str, 1, false, &str, NULL) < 0)
1405 return -1;
1406 list_add_tail(&term->list, head);
1407
Agustin Vega-Friasc199c112018-03-06 09:04:44 -05001408 if (!parse_events_add_pmu(parse_state, list,
Kan Liang3cdc5c22018-05-07 14:13:43 -07001409 pmu->name, head,
1410 true, true)) {
Andi Kleen2073ad32017-03-20 13:17:02 -07001411 pr_debug("%s -> %s/%s/\n", str,
1412 pmu->name, alias->str);
1413 ok++;
1414 }
1415
1416 parse_events_terms__delete(head);
1417 }
1418 }
1419 }
1420 if (!ok)
1421 return -1;
1422 *listp = list;
1423 return 0;
1424}
1425
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001426int parse_events__modifier_group(struct list_head *list,
1427 char *event_mod)
Jiri Olsa89efb022012-08-08 12:14:14 +02001428{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001429 return parse_events__modifier_event(list, event_mod, true);
1430}
1431
Kan Liang3cdc5c22018-05-07 14:13:43 -07001432/*
1433 * Check if the two uncore PMUs are from the same uncore block
1434 * The format of the uncore PMU name is uncore_#blockname_#pmuidx
1435 */
1436static bool is_same_uncore_block(const char *pmu_name_a, const char *pmu_name_b)
1437{
1438 char *end_a, *end_b;
1439
1440 end_a = strrchr(pmu_name_a, '_');
1441 end_b = strrchr(pmu_name_b, '_');
1442
1443 if (!end_a || !end_b)
1444 return false;
1445
1446 if ((end_a - pmu_name_a) != (end_b - pmu_name_b))
1447 return false;
1448
1449 return (strncmp(pmu_name_a, pmu_name_b, end_a - pmu_name_a) == 0);
1450}
1451
1452static int
1453parse_events__set_leader_for_uncore_aliase(char *name, struct list_head *list,
1454 struct parse_events_state *parse_state)
1455{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001456 struct evsel *evsel, *leader;
Kan Liang3cdc5c22018-05-07 14:13:43 -07001457 uintptr_t *leaders;
1458 bool is_leader = true;
1459 int i, nr_pmu = 0, total_members, ret = 0;
1460
Jiri Olsab27c4ec2019-07-21 13:24:22 +02001461 leader = list_first_entry(list, struct evsel, core.node);
1462 evsel = list_last_entry(list, struct evsel, core.node);
Kan Liang3cdc5c22018-05-07 14:13:43 -07001463 total_members = evsel->idx - leader->idx + 1;
1464
1465 leaders = calloc(total_members, sizeof(uintptr_t));
1466 if (WARN_ON(!leaders))
1467 return 0;
1468
1469 /*
1470 * Going through the whole group and doing sanity check.
1471 * All members must use alias, and be from the same uncore block.
1472 * Also, storing the leader events in an array.
1473 */
1474 __evlist__for_each_entry(list, evsel) {
1475
1476 /* Only split the uncore group which members use alias */
1477 if (!evsel->use_uncore_alias)
1478 goto out;
1479
1480 /* The events must be from the same uncore block */
1481 if (!is_same_uncore_block(leader->pmu_name, evsel->pmu_name))
1482 goto out;
1483
1484 if (!is_leader)
1485 continue;
1486 /*
1487 * If the event's PMU name starts to repeat, it must be a new
1488 * event. That can be used to distinguish the leader from
1489 * other members, even they have the same event name.
1490 */
1491 if ((leader != evsel) && (leader->pmu_name == evsel->pmu_name)) {
1492 is_leader = false;
1493 continue;
1494 }
1495 /* The name is always alias name */
1496 WARN_ON(strcmp(leader->name, evsel->name));
1497
1498 /* Store the leader event for each PMU */
1499 leaders[nr_pmu++] = (uintptr_t) evsel;
1500 }
1501
1502 /* only one event alias */
1503 if (nr_pmu == total_members) {
1504 parse_state->nr_groups--;
1505 goto handled;
1506 }
1507
1508 /*
1509 * An uncore event alias is a joint name which means the same event
1510 * runs on all PMUs of a block.
1511 * Perf doesn't support mixed events from different PMUs in the same
1512 * group. The big group has to be split into multiple small groups
1513 * which only include the events from the same PMU.
1514 *
1515 * Here the uncore event aliases must be from the same uncore block.
1516 * The number of PMUs must be same for each alias. The number of new
1517 * small groups equals to the number of PMUs.
1518 * Setting the leader event for corresponding members in each group.
1519 */
1520 i = 0;
1521 __evlist__for_each_entry(list, evsel) {
1522 if (i >= nr_pmu)
1523 i = 0;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001524 evsel->leader = (struct evsel *) leaders[i++];
Kan Liang3cdc5c22018-05-07 14:13:43 -07001525 }
1526
1527 /* The number of members and group name are same for each group */
1528 for (i = 0; i < nr_pmu; i++) {
Jiri Olsa32dcd022019-07-21 13:23:51 +02001529 evsel = (struct evsel *) leaders[i];
Kan Liang3cdc5c22018-05-07 14:13:43 -07001530 evsel->nr_members = total_members / nr_pmu;
1531 evsel->group_name = name ? strdup(name) : NULL;
1532 }
1533
1534 /* Take the new small groups into account */
1535 parse_state->nr_groups += nr_pmu - 1;
1536
1537handled:
1538 ret = 1;
1539out:
1540 free(leaders);
1541 return ret;
1542}
1543
1544void parse_events__set_leader(char *name, struct list_head *list,
1545 struct parse_events_state *parse_state)
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001546{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001547 struct evsel *leader;
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001548
Wang Nan854f7362015-09-06 07:13:17 +00001549 if (list_empty(list)) {
1550 WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1551 return;
1552 }
1553
Kan Liang3cdc5c22018-05-07 14:13:43 -07001554 if (parse_events__set_leader_for_uncore_aliase(name, list, parse_state))
1555 return;
1556
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -03001557 __perf_evlist__set_leader(list);
Jiri Olsab27c4ec2019-07-21 13:24:22 +02001558 leader = list_entry(list->next, struct evsel, core.node);
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001559 leader->group_name = name ? strdup(name) : NULL;
Jiri Olsa89efb022012-08-08 12:14:14 +02001560}
1561
David Ahernc5cd8ac2013-07-02 13:27:25 -06001562/* list_event is assumed to point to malloc'ed memory */
Jiri Olsa5d7be902012-03-20 19:15:40 +01001563void parse_events_update_lists(struct list_head *list_event,
1564 struct list_head *list_all)
1565{
1566 /*
1567 * Called for single event definition. Update the
Jiri Olsa89efb022012-08-08 12:14:14 +02001568 * 'all event' list, and reinit the 'single event'
Jiri Olsa5d7be902012-03-20 19:15:40 +01001569 * list, for next event definition.
1570 */
1571 list_splice_tail(list_event, list_all);
Jiri Olsab847cbd2012-05-21 09:12:51 +02001572 free(list_event);
Jiri Olsa5d7be902012-03-20 19:15:40 +01001573}
1574
Jiri Olsaf5b11352012-08-08 12:21:54 +02001575struct event_modifier {
1576 int eu;
1577 int ek;
1578 int eh;
1579 int eH;
1580 int eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001581 int eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001582 int precise;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001583 int precise_max;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001584 int exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02001585 int sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001586 int pinned;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001587 int weak;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001588};
Paul Mackerras61c45982009-07-01 13:04:34 +10001589
Jiri Olsaf5b11352012-08-08 12:21:54 +02001590static int get_event_modifier(struct event_modifier *mod, char *str,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001591 struct evsel *evsel)
Jiri Olsaf5b11352012-08-08 12:21:54 +02001592{
1593 int eu = evsel ? evsel->attr.exclude_user : 0;
1594 int ek = evsel ? evsel->attr.exclude_kernel : 0;
1595 int eh = evsel ? evsel->attr.exclude_hv : 0;
1596 int eH = evsel ? evsel->attr.exclude_host : 0;
1597 int eG = evsel ? evsel->attr.exclude_guest : 0;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001598 int eI = evsel ? evsel->attr.exclude_idle : 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001599 int precise = evsel ? evsel->attr.precise_ip : 0;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001600 int precise_max = 0;
Jiri Olsa3c176312012-10-10 17:39:03 +02001601 int sample_read = 0;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001602 int pinned = evsel ? evsel->attr.pinned : 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001603
1604 int exclude = eu | ek | eh;
1605 int exclude_GH = evsel ? evsel->exclude_GH : 0;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001606 int weak = 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001607
Jiri Olsaf5b11352012-08-08 12:21:54 +02001608 memset(mod, 0, sizeof(*mod));
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001609
Paul Mackerras61c45982009-07-01 13:04:34 +10001610 while (*str) {
Peter Zijlstraab608342010-04-08 23:03:20 +02001611 if (*str == 'u') {
1612 if (!exclude)
1613 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001614 eu = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +02001615 } else if (*str == 'k') {
1616 if (!exclude)
1617 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001618 ek = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +02001619 } else if (*str == 'h') {
1620 if (!exclude)
1621 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001622 eh = 0;
Joerg Roedel99320cc2012-01-04 17:54:19 +01001623 } else if (*str == 'G') {
1624 if (!exclude_GH)
1625 exclude_GH = eG = eH = 1;
1626 eG = 0;
1627 } else if (*str == 'H') {
1628 if (!exclude_GH)
1629 exclude_GH = eG = eH = 1;
1630 eH = 0;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001631 } else if (*str == 'I') {
1632 eI = 1;
Peter Zijlstraab608342010-04-08 23:03:20 +02001633 } else if (*str == 'p') {
1634 precise++;
David Ahern13427982012-09-13 14:59:13 -06001635 /* use of precise requires exclude_guest */
1636 if (!exclude_GH)
1637 eG = 1;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001638 } else if (*str == 'P') {
1639 precise_max = 1;
Jiri Olsa3c176312012-10-10 17:39:03 +02001640 } else if (*str == 'S') {
1641 sample_read = 1;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001642 } else if (*str == 'D') {
1643 pinned = 1;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001644 } else if (*str == 'W') {
1645 weak = 1;
Peter Zijlstraab608342010-04-08 23:03:20 +02001646 } else
Paul Mackerras61c45982009-07-01 13:04:34 +10001647 break;
Peter Zijlstraab608342010-04-08 23:03:20 +02001648
Paul Mackerras61c45982009-07-01 13:04:34 +10001649 ++str;
1650 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001651
Jiri Olsa89812fc2012-03-15 20:09:15 +01001652 /*
1653 * precise ip:
1654 *
1655 * 0 - SAMPLE_IP can have arbitrary skid
1656 * 1 - SAMPLE_IP must have constant skid
1657 * 2 - SAMPLE_IP requested to have 0 skid
1658 * 3 - SAMPLE_IP must have 0 skid
1659 *
1660 * See also PERF_RECORD_MISC_EXACT_IP
1661 */
1662 if (precise > 3)
1663 return -EINVAL;
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001664
Jiri Olsaf5b11352012-08-08 12:21:54 +02001665 mod->eu = eu;
1666 mod->ek = ek;
1667 mod->eh = eh;
1668 mod->eH = eH;
1669 mod->eG = eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001670 mod->eI = eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001671 mod->precise = precise;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001672 mod->precise_max = precise_max;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001673 mod->exclude_GH = exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02001674 mod->sample_read = sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001675 mod->pinned = pinned;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001676 mod->weak = weak;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001677
Jiri Olsaf5b11352012-08-08 12:21:54 +02001678 return 0;
1679}
1680
Jiri Olsa534123f2012-11-13 15:32:58 +01001681/*
1682 * Basic modifier sanity check to validate it contains only one
1683 * instance of any modifier (apart from 'p') present.
1684 */
1685static int check_modifier(char *str)
1686{
1687 char *p = str;
1688
1689 /* The sizeof includes 0 byte as well. */
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001690 if (strlen(str) > (sizeof("ukhGHpppPSDIW") - 1))
Jiri Olsa534123f2012-11-13 15:32:58 +01001691 return -1;
1692
1693 while (*p) {
1694 if (*p != 'p' && strchr(p + 1, *p))
1695 return -1;
1696 p++;
1697 }
1698
1699 return 0;
1700}
1701
Jiri Olsaf5b11352012-08-08 12:21:54 +02001702int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1703{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001704 struct evsel *evsel;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001705 struct event_modifier mod;
1706
1707 if (str == NULL)
1708 return 0;
1709
Jiri Olsa534123f2012-11-13 15:32:58 +01001710 if (check_modifier(str))
1711 return -EINVAL;
1712
Jiri Olsaf5b11352012-08-08 12:21:54 +02001713 if (!add && get_event_modifier(&mod, str, NULL))
1714 return -EINVAL;
1715
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001716 __evlist__for_each_entry(list, evsel) {
Jiri Olsaf5b11352012-08-08 12:21:54 +02001717 if (add && get_event_modifier(&mod, str, evsel))
1718 return -EINVAL;
1719
1720 evsel->attr.exclude_user = mod.eu;
1721 evsel->attr.exclude_kernel = mod.ek;
1722 evsel->attr.exclude_hv = mod.eh;
1723 evsel->attr.precise_ip = mod.precise;
1724 evsel->attr.exclude_host = mod.eH;
1725 evsel->attr.exclude_guest = mod.eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001726 evsel->attr.exclude_idle = mod.eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001727 evsel->exclude_GH = mod.exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02001728 evsel->sample_read = mod.sample_read;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001729 evsel->precise_max = mod.precise_max;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001730 evsel->weak_group = mod.weak;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001731
1732 if (perf_evsel__is_group_leader(evsel))
1733 evsel->attr.pinned = mod.pinned;
Jiri Olsa89812fc2012-03-15 20:09:15 +01001734 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001735
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301736 return 0;
1737}
1738
Robert Richterac2ba9f2012-08-16 21:10:21 +02001739int parse_events_name(struct list_head *list, char *name)
1740{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001741 struct evsel *evsel;
Robert Richterac2ba9f2012-08-16 21:10:21 +02001742
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001743 __evlist__for_each_entry(list, evsel) {
Robert Richterac2ba9f2012-08-16 21:10:21 +02001744 if (!evsel->name)
1745 evsel->name = strdup(name);
1746 }
1747
1748 return 0;
1749}
1750
Kan Liangdcb4e102014-10-07 11:08:50 -04001751static int
1752comp_pmu(const void *p1, const void *p2)
1753{
1754 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
1755 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
1756
Andi Kleene312bcf2016-09-15 15:24:53 -07001757 return strcasecmp(pmu1->symbol, pmu2->symbol);
Kan Liangdcb4e102014-10-07 11:08:50 -04001758}
1759
1760static void perf_pmu__parse_cleanup(void)
1761{
1762 if (perf_pmu_events_list_num > 0) {
1763 struct perf_pmu_event_symbol *p;
1764 int i;
1765
1766 for (i = 0; i < perf_pmu_events_list_num; i++) {
1767 p = perf_pmu_events_list + i;
Taeung Song360e0712017-02-01 21:34:07 +09001768 zfree(&p->symbol);
Kan Liangdcb4e102014-10-07 11:08:50 -04001769 }
Taeung Song506fde12017-02-01 21:34:06 +09001770 zfree(&perf_pmu_events_list);
Kan Liangdcb4e102014-10-07 11:08:50 -04001771 perf_pmu_events_list_num = 0;
1772 }
1773}
1774
1775#define SET_SYMBOL(str, stype) \
1776do { \
1777 p->symbol = str; \
1778 if (!p->symbol) \
1779 goto err; \
1780 p->type = stype; \
1781} while (0)
1782
1783/*
1784 * Read the pmu events list from sysfs
1785 * Save it into perf_pmu_events_list
1786 */
1787static void perf_pmu__parse_init(void)
1788{
1789
1790 struct perf_pmu *pmu = NULL;
1791 struct perf_pmu_alias *alias;
1792 int len = 0;
1793
Andi Kleen231bb2a2017-01-27 18:03:39 -08001794 pmu = NULL;
1795 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1796 list_for_each_entry(alias, &pmu->aliases, list) {
1797 if (strchr(alias->name, '-'))
1798 len++;
1799 len++;
1800 }
1801 }
1802
1803 if (len == 0) {
Kan Liangdcb4e102014-10-07 11:08:50 -04001804 perf_pmu_events_list_num = -1;
1805 return;
1806 }
Kan Liangdcb4e102014-10-07 11:08:50 -04001807 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1808 if (!perf_pmu_events_list)
1809 return;
1810 perf_pmu_events_list_num = len;
1811
1812 len = 0;
Andi Kleen231bb2a2017-01-27 18:03:39 -08001813 pmu = NULL;
1814 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1815 list_for_each_entry(alias, &pmu->aliases, list) {
1816 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1817 char *tmp = strchr(alias->name, '-');
Kan Liangdcb4e102014-10-07 11:08:50 -04001818
Andi Kleen231bb2a2017-01-27 18:03:39 -08001819 if (tmp != NULL) {
1820 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1821 PMU_EVENT_SYMBOL_PREFIX);
1822 p++;
1823 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1824 len += 2;
1825 } else {
1826 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1827 len++;
1828 }
Kan Liangdcb4e102014-10-07 11:08:50 -04001829 }
1830 }
1831 qsort(perf_pmu_events_list, len,
1832 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1833
1834 return;
1835err:
1836 perf_pmu__parse_cleanup();
1837}
1838
1839enum perf_pmu_event_symbol_type
1840perf_pmu__parse_check(const char *name)
1841{
1842 struct perf_pmu_event_symbol p, *r;
1843
1844 /* scan kernel pmu events from sysfs if needed */
1845 if (perf_pmu_events_list_num == 0)
1846 perf_pmu__parse_init();
1847 /*
1848 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1849 * cpu-cycles has been handled by hardcode.
1850 * So it must be cpu// events, not kernel pmu event.
1851 */
1852 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1853 return PMU_EVENT_SYMBOL_ERR;
1854
1855 p.symbol = strdup(name);
1856 r = bsearch(&p, perf_pmu_events_list,
1857 (size_t) perf_pmu_events_list_num,
1858 sizeof(struct perf_pmu_event_symbol), comp_pmu);
Taeung Song360e0712017-02-01 21:34:07 +09001859 zfree(&p.symbol);
Kan Liangdcb4e102014-10-07 11:08:50 -04001860 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1861}
1862
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001863static int parse_events__scanner(const char *str, void *parse_state, int start_token)
Zheng Yanac20de6f2012-06-15 14:31:39 +08001864{
1865 YY_BUFFER_STATE buffer;
1866 void *scanner;
1867 int ret;
1868
Jiri Olsa90e2b222012-06-15 14:31:40 +08001869 ret = parse_events_lex_init_extra(start_token, &scanner);
Zheng Yanac20de6f2012-06-15 14:31:39 +08001870 if (ret)
1871 return ret;
1872
1873 buffer = parse_events__scan_string(str, scanner);
1874
1875#ifdef PARSER_DEBUG
1876 parse_events_debug = 1;
1877#endif
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001878 ret = parse_events_parse(parse_state, scanner);
Zheng Yanac20de6f2012-06-15 14:31:39 +08001879
1880 parse_events__flush_buffer(buffer, scanner);
1881 parse_events__delete_buffer(buffer, scanner);
1882 parse_events_lex_destroy(scanner);
1883 return ret;
1884}
1885
Jiri Olsa90e2b222012-06-15 14:31:40 +08001886/*
1887 * parse event config string, return a list of event terms.
1888 */
1889int parse_events_terms(struct list_head *terms, const char *str)
1890{
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001891 struct parse_events_state parse_state = {
Jiri Olsa90e2b222012-06-15 14:31:40 +08001892 .terms = NULL,
1893 };
1894 int ret;
1895
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001896 ret = parse_events__scanner(str, &parse_state, PE_START_TERMS);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001897 if (!ret) {
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001898 list_splice(parse_state.terms, terms);
1899 zfree(&parse_state.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001900 return 0;
1901 }
1902
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001903 parse_events_terms__delete(parse_state.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001904 return ret;
1905}
1906
Jiri Olsa63503db2019-07-21 13:23:52 +02001907int parse_events(struct evlist *evlist, const char *str,
Jiri Olsab39b8392015-04-22 21:10:16 +02001908 struct parse_events_error *err)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001909{
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001910 struct parse_events_state parse_state = {
1911 .list = LIST_HEAD_INIT(parse_state.list),
Wang Nan7630b3e2016-02-22 09:10:33 +00001912 .idx = evlist->nr_entries,
1913 .error = err,
1914 .evlist = evlist,
Jiri Olsa46010ab2012-06-15 14:31:38 +08001915 };
Jiri Olsa46010ab2012-06-15 14:31:38 +08001916 int ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001917
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001918 ret = parse_events__scanner(str, &parse_state, PE_START_EVENTS);
Kan Liangdcb4e102014-10-07 11:08:50 -04001919 perf_pmu__parse_cleanup();
Jiri Olsa89812fc2012-03-15 20:09:15 +01001920 if (!ret) {
Jiri Olsa32dcd022019-07-21 13:23:51 +02001921 struct evsel *last;
Wang Nan15bfd2c2015-07-10 07:36:09 +00001922
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001923 if (list_empty(&parse_state.list)) {
Arnaldo Carvalho de Meloc23080a2018-05-03 15:59:58 -03001924 WARN_ONCE(true, "WARNING: event parser found nothing\n");
Wang Nan854f7362015-09-06 07:13:17 +00001925 return -1;
1926 }
1927
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001928 perf_evlist__splice_list_tail(evlist, &parse_state.list);
1929 evlist->nr_groups += parse_state.nr_groups;
Wang Nan15bfd2c2015-07-10 07:36:09 +00001930 last = perf_evlist__last(evlist);
1931 last->cmdline_group_boundary = true;
1932
Jiri Olsa89812fc2012-03-15 20:09:15 +01001933 return 0;
1934 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02001935
Jiri Olsa5d7be902012-03-20 19:15:40 +01001936 /*
1937 * There are 2 users - builtin-record and builtin-test objects.
Jiri Olsac12995a2019-07-21 13:23:56 +02001938 * Both call evlist__delete in case of error, so we dont
Jiri Olsa5d7be902012-03-20 19:15:40 +01001939 * need to bother.
1940 */
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02001941 return ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001942}
1943
Jiri Olsab39b8392015-04-22 21:10:16 +02001944#define MAX_WIDTH 1000
1945static int get_term_width(void)
1946{
1947 struct winsize ws;
1948
1949 get_term_dimensions(&ws);
1950 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1951}
1952
Andi Kleen333b5662017-09-13 14:50:06 -07001953void parse_events_print_error(struct parse_events_error *err,
1954 const char *event)
Jiri Olsab39b8392015-04-22 21:10:16 +02001955{
1956 const char *str = "invalid or unsupported event: ";
1957 char _buf[MAX_WIDTH];
1958 char *buf = (char *) event;
1959 int idx = 0;
1960
1961 if (err->str) {
1962 /* -2 for extra '' in the final fprintf */
1963 int width = get_term_width() - 2;
1964 int len_event = strlen(event);
1965 int len_str, max_len, cut = 0;
1966
1967 /*
1968 * Maximum error index indent, we will cut
1969 * the event string if it's bigger.
1970 */
Adrian Hunter141b2d32015-07-17 19:33:51 +03001971 int max_err_idx = 13;
Jiri Olsab39b8392015-04-22 21:10:16 +02001972
1973 /*
1974 * Let's be specific with the message when
1975 * we have the precise error.
1976 */
1977 str = "event syntax error: ";
1978 len_str = strlen(str);
1979 max_len = width - len_str;
1980
1981 buf = _buf;
1982
Adam Buchbinderbd1a0be52016-02-24 10:02:25 -08001983 /* We're cutting from the beginning. */
Jiri Olsab39b8392015-04-22 21:10:16 +02001984 if (err->idx > max_err_idx)
1985 cut = err->idx - max_err_idx;
1986
1987 strncpy(buf, event + cut, max_len);
1988
1989 /* Mark cut parts with '..' on both sides. */
1990 if (cut)
1991 buf[0] = buf[1] = '.';
1992
1993 if ((len_event - cut) > max_len) {
1994 buf[max_len - 1] = buf[max_len - 2] = '.';
1995 buf[max_len] = 0;
1996 }
1997
1998 idx = len_str + err->idx - cut;
1999 }
2000
2001 fprintf(stderr, "%s'%s'\n", str, buf);
2002 if (idx) {
2003 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
2004 if (err->help)
2005 fprintf(stderr, "\n%s\n", err->help);
Taeung Song360e0712017-02-01 21:34:07 +09002006 zfree(&err->str);
2007 zfree(&err->help);
Jiri Olsab39b8392015-04-22 21:10:16 +02002008 }
Jiri Olsab39b8392015-04-22 21:10:16 +02002009}
2010
2011#undef MAX_WIDTH
2012
Jiri Olsaf120f9d2011-07-14 11:25:32 +02002013int parse_events_option(const struct option *opt, const char *str,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002014 int unset __maybe_unused)
Jiri Olsaf120f9d2011-07-14 11:25:32 +02002015{
Jiri Olsa63503db2019-07-21 13:23:52 +02002016 struct evlist *evlist = *(struct evlist **)opt->value;
Jiri Olsab39b8392015-04-22 21:10:16 +02002017 struct parse_events_error err = { .idx = 0, };
2018 int ret = parse_events(evlist, str, &err);
Andi Kleen9175ce12012-10-26 13:30:06 -07002019
Andi Kleen333b5662017-09-13 14:50:06 -07002020 if (ret) {
Jiri Olsab39b8392015-04-22 21:10:16 +02002021 parse_events_print_error(&err, str);
Andi Kleen333b5662017-09-13 14:50:06 -07002022 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
2023 }
Jiri Olsab39b8392015-04-22 21:10:16 +02002024
Andi Kleen9175ce12012-10-26 13:30:06 -07002025 return ret;
Jiri Olsaf120f9d2011-07-14 11:25:32 +02002026}
2027
Wang Nan4ba1faa2015-07-10 07:36:10 +00002028static int
Jiri Olsa63503db2019-07-21 13:23:52 +02002029foreach_evsel_in_last_glob(struct evlist *evlist,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002030 int (*func)(struct evsel *evsel,
Wang Nan4ba1faa2015-07-10 07:36:10 +00002031 const void *arg),
2032 const void *arg)
Li Zefanc171b552009-10-15 11:22:07 +08002033{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002034 struct evsel *last = NULL;
Wang Nan4ba1faa2015-07-10 07:36:10 +00002035 int err;
Li Zefanc171b552009-10-15 11:22:07 +08002036
Wang Nan854f7362015-09-06 07:13:17 +00002037 /*
2038 * Don't return when list_empty, give func a chance to report
2039 * error when it found last == NULL.
2040 *
2041 * So no need to WARN here, let *func do this.
2042 */
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002043 if (evlist->nr_entries > 0)
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03002044 last = perf_evlist__last(evlist);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02002045
Wang Nan15bfd2c2015-07-10 07:36:09 +00002046 do {
Wang Nan4ba1faa2015-07-10 07:36:10 +00002047 err = (*func)(last, arg);
2048 if (err)
Wang Nan15bfd2c2015-07-10 07:36:09 +00002049 return -1;
Wang Nan4ba1faa2015-07-10 07:36:10 +00002050 if (!last)
2051 return 0;
Wang Nan15bfd2c2015-07-10 07:36:09 +00002052
Jiri Olsab27c4ec2019-07-21 13:24:22 +02002053 if (last->core.node.prev == &evlist->entries)
Wang Nan15bfd2c2015-07-10 07:36:09 +00002054 return 0;
Jiri Olsab27c4ec2019-07-21 13:24:22 +02002055 last = list_entry(last->core.node.prev, struct evsel, core.node);
Wang Nan15bfd2c2015-07-10 07:36:09 +00002056 } while (!last->cmdline_group_boundary);
Li Zefanc171b552009-10-15 11:22:07 +08002057
2058 return 0;
2059}
2060
Jiri Olsa32dcd022019-07-21 13:23:51 +02002061static int set_filter(struct evsel *evsel, const void *arg)
Wang Nan4ba1faa2015-07-10 07:36:10 +00002062{
2063 const char *str = arg;
Mathieu Poirier1e857482016-09-16 08:44:05 -06002064 bool found = false;
2065 int nr_addr_filters = 0;
2066 struct perf_pmu *pmu = NULL;
Wang Nan4ba1faa2015-07-10 07:36:10 +00002067
Jack Henschel49836f72018-07-04 14:13:45 +02002068 if (evsel == NULL) {
2069 fprintf(stderr,
2070 "--filter option should follow a -e tracepoint or HW tracer option\n");
2071 return -1;
2072 }
Mathieu Poirier1e857482016-09-16 08:44:05 -06002073
2074 if (evsel->attr.type == PERF_TYPE_TRACEPOINT) {
2075 if (perf_evsel__append_tp_filter(evsel, str) < 0) {
2076 fprintf(stderr,
2077 "not enough memory to hold filter string\n");
2078 return -1;
2079 }
2080
2081 return 0;
Wang Nan4ba1faa2015-07-10 07:36:10 +00002082 }
2083
Mathieu Poirier1e857482016-09-16 08:44:05 -06002084 while ((pmu = perf_pmu__scan(pmu)) != NULL)
2085 if (pmu->type == evsel->attr.type) {
2086 found = true;
2087 break;
2088 }
2089
2090 if (found)
2091 perf_pmu__scan_file(pmu, "nr_addr_filters",
2092 "%d", &nr_addr_filters);
2093
Jack Henschel49836f72018-07-04 14:13:45 +02002094 if (!nr_addr_filters) {
2095 fprintf(stderr,
2096 "This CPU does not support address filtering\n");
2097 return -1;
2098 }
Mathieu Poirier1e857482016-09-16 08:44:05 -06002099
2100 if (perf_evsel__append_addr_filter(evsel, str) < 0) {
Wang Nan4ba1faa2015-07-10 07:36:10 +00002101 fprintf(stderr,
2102 "not enough memory to hold filter string\n");
2103 return -1;
2104 }
2105
2106 return 0;
2107}
2108
2109int parse_filter(const struct option *opt, const char *str,
2110 int unset __maybe_unused)
2111{
Jiri Olsa63503db2019-07-21 13:23:52 +02002112 struct evlist *evlist = *(struct evlist **)opt->value;
Wang Nan4ba1faa2015-07-10 07:36:10 +00002113
2114 return foreach_evsel_in_last_glob(evlist, set_filter,
2115 (const void *)str);
2116}
2117
Jiri Olsa32dcd022019-07-21 13:23:51 +02002118static int add_exclude_perf_filter(struct evsel *evsel,
Wang Nan4ba1faa2015-07-10 07:36:10 +00002119 const void *arg __maybe_unused)
2120{
2121 char new_filter[64];
2122
2123 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
2124 fprintf(stderr,
2125 "--exclude-perf option should follow a -e tracepoint option\n");
2126 return -1;
2127 }
2128
2129 snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
2130
Mathieu Poirier3541c032016-09-16 08:44:04 -06002131 if (perf_evsel__append_tp_filter(evsel, new_filter) < 0) {
Wang Nan4ba1faa2015-07-10 07:36:10 +00002132 fprintf(stderr,
2133 "not enough memory to hold filter string\n");
2134 return -1;
2135 }
2136
2137 return 0;
2138}
2139
2140int exclude_perf(const struct option *opt,
2141 const char *arg __maybe_unused,
2142 int unset __maybe_unused)
2143{
Jiri Olsa63503db2019-07-21 13:23:52 +02002144 struct evlist *evlist = *(struct evlist **)opt->value;
Wang Nan4ba1faa2015-07-10 07:36:10 +00002145
2146 return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
2147 NULL);
2148}
2149
Thomas Gleixner86847b62009-06-06 12:24:17 +02002150static const char * const event_type_descriptors[] = {
Thomas Gleixner86847b62009-06-06 12:24:17 +02002151 "Hardware event",
2152 "Software event",
2153 "Tracepoint event",
2154 "Hardware cache event",
Liming Wang41bdcb22009-12-29 16:37:07 +08002155 "Raw hardware event descriptor",
2156 "Hardware breakpoint",
Thomas Gleixner86847b62009-06-06 12:24:17 +02002157};
2158
Yunlong Songab0e4802015-02-27 18:21:25 +08002159static int cmp_string(const void *a, const void *b)
2160{
2161 const char * const *as = a;
2162 const char * const *bs = b;
2163
2164 return strcmp(*as, *bs);
2165}
2166
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002167/*
Jason Baronf6bdafe2009-07-21 12:20:22 -04002168 * Print the events from <debugfs_mount_point>/tracing/events
2169 */
2170
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002171void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
2172 bool name_only)
Jason Baronf6bdafe2009-07-21 12:20:22 -04002173{
2174 DIR *sys_dir, *evt_dir;
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002175 struct dirent *sys_dirent, *evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -04002176 char evt_path[MAXPATHLEN];
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002177 char *dir_path;
Yunlong Songab0e4802015-02-27 18:21:25 +08002178 char **evt_list = NULL;
2179 unsigned int evt_i = 0, evt_num = 0;
2180 bool evt_num_known = false;
Jason Baronf6bdafe2009-07-21 12:20:22 -04002181
Yunlong Songab0e4802015-02-27 18:21:25 +08002182restart:
Arnaldo Carvalho de Melo7014e0e2018-05-17 14:42:39 -03002183 sys_dir = tracing_events__opendir();
Jason Baronf6bdafe2009-07-21 12:20:22 -04002184 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +02002185 return;
Jason Baronf6bdafe2009-07-21 12:20:22 -04002186
Yunlong Songab0e4802015-02-27 18:21:25 +08002187 if (evt_num_known) {
2188 evt_list = zalloc(sizeof(char *) * evt_num);
2189 if (!evt_list)
2190 goto out_close_sys_dir;
2191 }
2192
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002193 for_each_subsystem(sys_dir, sys_dirent) {
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03002194 if (subsys_glob != NULL &&
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002195 !strglobmatch(sys_dirent->d_name, subsys_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002196 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02002197
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002198 dir_path = get_events_file(sys_dirent->d_name);
2199 if (!dir_path)
2200 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02002201 evt_dir = opendir(dir_path);
2202 if (!evt_dir)
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002203 goto next;
Eric Dumazet725b1362009-09-24 15:39:09 +02002204
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -03002205 for_each_event(dir_path, evt_dir, evt_dirent) {
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03002206 if (event_glob != NULL &&
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002207 !strglobmatch(evt_dirent->d_name, event_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002208 continue;
2209
Yunlong Songab0e4802015-02-27 18:21:25 +08002210 if (!evt_num_known) {
2211 evt_num++;
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002212 continue;
2213 }
2214
Jason Baronf6bdafe2009-07-21 12:20:22 -04002215 snprintf(evt_path, MAXPATHLEN, "%s:%s",
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002216 sys_dirent->d_name, evt_dirent->d_name);
Yunlong Songab0e4802015-02-27 18:21:25 +08002217
2218 evt_list[evt_i] = strdup(evt_path);
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002219 if (evt_list[evt_i] == NULL) {
2220 put_events_file(dir_path);
Yunlong Songab0e4802015-02-27 18:21:25 +08002221 goto out_close_evt_dir;
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002222 }
Yunlong Songab0e4802015-02-27 18:21:25 +08002223 evt_i++;
Jason Baronf6bdafe2009-07-21 12:20:22 -04002224 }
2225 closedir(evt_dir);
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002226next:
2227 put_events_file(dir_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -04002228 }
Jason Baronf6bdafe2009-07-21 12:20:22 -04002229 closedir(sys_dir);
Yunlong Songab0e4802015-02-27 18:21:25 +08002230
2231 if (!evt_num_known) {
2232 evt_num_known = true;
2233 goto restart;
2234 }
2235 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2236 evt_i = 0;
2237 while (evt_i < evt_num) {
2238 if (name_only) {
2239 printf("%s ", evt_list[evt_i++]);
2240 continue;
2241 }
2242 printf(" %-50s [%s]\n", evt_list[evt_i++],
2243 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2244 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002245 if (evt_num && pager_in_use())
Yunlong Songab0e4802015-02-27 18:21:25 +08002246 printf("\n");
2247
2248out_free:
2249 evt_num = evt_i;
2250 for (evt_i = 0; evt_i < evt_num; evt_i++)
2251 zfree(&evt_list[evt_i]);
2252 zfree(&evt_list);
2253 return;
2254
2255out_close_evt_dir:
2256 closedir(evt_dir);
2257out_close_sys_dir:
2258 closedir(sys_dir);
2259
2260 printf("FATAL: not enough memory to print %s\n",
2261 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2262 if (evt_list)
2263 goto out_free;
Jason Baronf6bdafe2009-07-21 12:20:22 -04002264}
2265
2266/*
Thomas Renninger20c457b2011-01-03 17:50:45 +01002267 * Check whether event is in <debugfs_mount_point>/tracing/events
2268 */
2269
2270int is_valid_tracepoint(const char *event_string)
2271{
2272 DIR *sys_dir, *evt_dir;
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002273 struct dirent *sys_dirent, *evt_dirent;
Thomas Renninger20c457b2011-01-03 17:50:45 +01002274 char evt_path[MAXPATHLEN];
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002275 char *dir_path;
Thomas Renninger20c457b2011-01-03 17:50:45 +01002276
Arnaldo Carvalho de Melo7014e0e2018-05-17 14:42:39 -03002277 sys_dir = tracing_events__opendir();
Thomas Renninger20c457b2011-01-03 17:50:45 +01002278 if (!sys_dir)
2279 return 0;
2280
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002281 for_each_subsystem(sys_dir, sys_dirent) {
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002282 dir_path = get_events_file(sys_dirent->d_name);
2283 if (!dir_path)
2284 continue;
Thomas Renninger20c457b2011-01-03 17:50:45 +01002285 evt_dir = opendir(dir_path);
2286 if (!evt_dir)
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002287 goto next;
Thomas Renninger20c457b2011-01-03 17:50:45 +01002288
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -03002289 for_each_event(dir_path, evt_dir, evt_dirent) {
Thomas Renninger20c457b2011-01-03 17:50:45 +01002290 snprintf(evt_path, MAXPATHLEN, "%s:%s",
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002291 sys_dirent->d_name, evt_dirent->d_name);
Thomas Renninger20c457b2011-01-03 17:50:45 +01002292 if (!strcmp(evt_path, event_string)) {
2293 closedir(evt_dir);
2294 closedir(sys_dir);
2295 return 1;
2296 }
2297 }
2298 closedir(evt_dir);
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002299next:
2300 put_events_file(dir_path);
Thomas Renninger20c457b2011-01-03 17:50:45 +01002301 }
2302 closedir(sys_dir);
2303 return 0;
2304}
2305
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002306static bool is_event_supported(u8 type, unsigned config)
2307{
2308 bool ret = true;
Vince Weaver88fee522013-12-30 15:39:45 -05002309 int open_return;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002310 struct evsel *evsel;
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002311 struct perf_event_attr attr = {
2312 .type = type,
2313 .config = config,
2314 .disabled = 1,
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002315 };
Jiri Olsa9749b902019-07-21 13:23:50 +02002316 struct perf_thread_map *tmap = thread_map__new_by_tid(0);
Arnaldo Carvalho de Melo89896052017-02-14 10:59:04 -03002317
2318 if (tmap == NULL)
2319 return false;
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002320
Jiri Olsa365c3ae2019-07-21 13:23:58 +02002321 evsel = evsel__new(&attr);
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002322 if (evsel) {
Jiri Olsa5972d1e2019-07-21 13:24:01 +02002323 open_return = evsel__open(evsel, NULL, tmap);
Vince Weaver88fee522013-12-30 15:39:45 -05002324 ret = open_return >= 0;
2325
2326 if (open_return == -EACCES) {
2327 /*
2328 * This happens if the paranoid value
2329 * /proc/sys/kernel/perf_event_paranoid is set to 2
2330 * Re-run with exclude_kernel set; we don't do that
2331 * by default as some ARM machines do not support it.
2332 *
2333 */
2334 evsel->attr.exclude_kernel = 1;
Jiri Olsa5972d1e2019-07-21 13:24:01 +02002335 ret = evsel__open(evsel, NULL, tmap) >= 0;
Vince Weaver88fee522013-12-30 15:39:45 -05002336 }
Jiri Olsa5eb2dd22019-07-21 13:23:57 +02002337 evsel__delete(evsel);
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002338 }
2339
Jiri Olsa7836e522019-07-21 13:24:20 +02002340 perf_thread_map__put(tmap);
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002341 return ret;
2342}
2343
Masami Hiramatsu40218da2016-06-24 18:06:46 +09002344void print_sdt_events(const char *subsys_glob, const char *event_glob,
2345 bool name_only)
2346{
2347 struct probe_cache *pcache;
2348 struct probe_cache_entry *ent;
2349 struct strlist *bidlist, *sdtlist;
2350 struct strlist_config cfg = {.dont_dupstr = true};
2351 struct str_node *nd, *nd2;
2352 char *buf, *path, *ptr = NULL;
2353 bool show_detail = false;
2354 int ret;
2355
2356 sdtlist = strlist__new(NULL, &cfg);
2357 if (!sdtlist) {
2358 pr_debug("Failed to allocate new strlist for SDT\n");
2359 return;
2360 }
2361 bidlist = build_id_cache__list_all(true);
2362 if (!bidlist) {
2363 pr_debug("Failed to get buildids: %d\n", errno);
2364 return;
2365 }
2366 strlist__for_each_entry(nd, bidlist) {
Krister Johansenf045b8c2017-07-05 18:48:11 -07002367 pcache = probe_cache__new(nd->s, NULL);
Masami Hiramatsu40218da2016-06-24 18:06:46 +09002368 if (!pcache)
2369 continue;
2370 list_for_each_entry(ent, &pcache->entries, node) {
2371 if (!ent->sdt)
2372 continue;
2373 if (subsys_glob &&
2374 !strglobmatch(ent->pev.group, subsys_glob))
2375 continue;
2376 if (event_glob &&
2377 !strglobmatch(ent->pev.event, event_glob))
2378 continue;
2379 ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
2380 ent->pev.event, nd->s);
2381 if (ret > 0)
2382 strlist__add(sdtlist, buf);
2383 }
2384 probe_cache__delete(pcache);
2385 }
2386 strlist__delete(bidlist);
2387
2388 strlist__for_each_entry(nd, sdtlist) {
2389 buf = strchr(nd->s, '@');
2390 if (buf)
2391 *(buf++) = '\0';
2392 if (name_only) {
2393 printf("%s ", nd->s);
2394 continue;
2395 }
2396 nd2 = strlist__next(nd);
2397 if (nd2) {
2398 ptr = strchr(nd2->s, '@');
2399 if (ptr)
2400 *ptr = '\0';
2401 if (strcmp(nd->s, nd2->s) == 0)
2402 show_detail = true;
2403 }
2404 if (show_detail) {
2405 path = build_id_cache__origname(buf);
2406 ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf);
2407 if (ret > 0) {
2408 printf(" %-50s [%s]\n", buf, "SDT event");
2409 free(buf);
2410 }
Changbin Du8bde8512019-03-16 16:05:46 +08002411 free(path);
Masami Hiramatsu40218da2016-06-24 18:06:46 +09002412 } else
2413 printf(" %-50s [%s]\n", nd->s, "SDT event");
2414 if (nd2) {
2415 if (strcmp(nd->s, nd2->s) != 0)
2416 show_detail = false;
2417 if (ptr)
2418 *ptr = '@';
2419 }
2420 }
2421 strlist__delete(sdtlist);
2422}
2423
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002424int print_hwcache_events(const char *event_glob, bool name_only)
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002425{
Yunlong Songab0e4802015-02-27 18:21:25 +08002426 unsigned int type, op, i, evt_i = 0, evt_num = 0;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03002427 char name[64];
Yunlong Songab0e4802015-02-27 18:21:25 +08002428 char **evt_list = NULL;
2429 bool evt_num_known = false;
2430
2431restart:
2432 if (evt_num_known) {
2433 evt_list = zalloc(sizeof(char *) * evt_num);
2434 if (!evt_list)
2435 goto out_enomem;
2436 }
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002437
2438 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
2439 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
2440 /* skip invalid cache type */
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03002441 if (!perf_evsel__is_cache_op_valid(type, op))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002442 continue;
2443
2444 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03002445 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
2446 name, sizeof(name));
Ingo Molnar947b4ad2011-04-29 22:52:42 +02002447 if (event_glob != NULL && !strglobmatch(name, event_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002448 continue;
2449
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002450 if (!is_event_supported(PERF_TYPE_HW_CACHE,
2451 type | (op << 8) | (i << 16)))
2452 continue;
2453
Yunlong Songab0e4802015-02-27 18:21:25 +08002454 if (!evt_num_known) {
2455 evt_num++;
2456 continue;
2457 }
2458
2459 evt_list[evt_i] = strdup(name);
2460 if (evt_list[evt_i] == NULL)
2461 goto out_enomem;
2462 evt_i++;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002463 }
2464 }
2465 }
2466
Yunlong Songab0e4802015-02-27 18:21:25 +08002467 if (!evt_num_known) {
2468 evt_num_known = true;
2469 goto restart;
2470 }
2471 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2472 evt_i = 0;
2473 while (evt_i < evt_num) {
2474 if (name_only) {
2475 printf("%s ", evt_list[evt_i++]);
2476 continue;
2477 }
2478 printf(" %-50s [%s]\n", evt_list[evt_i++],
2479 event_type_descriptors[PERF_TYPE_HW_CACHE]);
2480 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002481 if (evt_num && pager_in_use())
Andi Kleendc098b32013-04-20 11:02:29 -07002482 printf("\n");
Yunlong Songab0e4802015-02-27 18:21:25 +08002483
2484out_free:
2485 evt_num = evt_i;
2486 for (evt_i = 0; evt_i < evt_num; evt_i++)
2487 zfree(&evt_list[evt_i]);
2488 zfree(&evt_list);
2489 return evt_num;
2490
2491out_enomem:
2492 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
2493 if (evt_list)
2494 goto out_free;
2495 return evt_num;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002496}
2497
Andi Kleen5e0861b2019-03-26 15:18:23 -07002498static void print_tool_event(const char *name, const char *event_glob,
2499 bool name_only)
2500{
2501 if (event_glob && !strglobmatch(name, event_glob))
2502 return;
2503 if (name_only)
2504 printf("%s ", name);
2505 else
2506 printf(" %-50s [%s]\n", name, "Tool event");
2507
2508}
2509
2510void print_tool_events(const char *event_glob, bool name_only)
2511{
2512 print_tool_event("duration_time", event_glob, name_only);
2513 if (pager_in_use())
2514 printf("\n");
2515}
2516
Yunlong Song705750f2015-02-27 18:21:27 +08002517void print_symbol_events(const char *event_glob, unsigned type,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002518 struct event_symbol *syms, unsigned max,
2519 bool name_only)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002520{
Yunlong Songab0e4802015-02-27 18:21:25 +08002521 unsigned int i, evt_i = 0, evt_num = 0;
Ingo Molnar947b4ad2011-04-29 22:52:42 +02002522 char name[MAX_NAME_LEN];
Yunlong Songab0e4802015-02-27 18:21:25 +08002523 char **evt_list = NULL;
2524 bool evt_num_known = false;
2525
2526restart:
2527 if (evt_num_known) {
2528 evt_list = zalloc(sizeof(char *) * evt_num);
2529 if (!evt_list)
2530 goto out_enomem;
2531 syms -= max;
2532 }
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002533
Jiri Olsa1dc127602012-07-04 00:00:44 +02002534 for (i = 0; i < max; i++, syms++) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002535
Arnaldo Carvalho de Meloe37df6c2015-11-27 16:04:58 -03002536 if (event_glob != NULL && syms->symbol != NULL &&
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002537 !(strglobmatch(syms->symbol, event_glob) ||
2538 (syms->alias && strglobmatch(syms->alias, event_glob))))
2539 continue;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002540
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002541 if (!is_event_supported(type, i))
2542 continue;
2543
Yunlong Songab0e4802015-02-27 18:21:25 +08002544 if (!evt_num_known) {
2545 evt_num++;
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002546 continue;
2547 }
2548
Yunlong Songab0e4802015-02-27 18:21:25 +08002549 if (!name_only && strlen(syms->alias))
Ingo Molnar947b4ad2011-04-29 22:52:42 +02002550 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05302551 else
Arnaldo Carvalho de Melobd8d57f2018-12-06 13:52:13 -03002552 strlcpy(name, syms->symbol, MAX_NAME_LEN);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002553
Yunlong Songab0e4802015-02-27 18:21:25 +08002554 evt_list[evt_i] = strdup(name);
2555 if (evt_list[evt_i] == NULL)
2556 goto out_enomem;
2557 evt_i++;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002558 }
2559
Yunlong Songab0e4802015-02-27 18:21:25 +08002560 if (!evt_num_known) {
2561 evt_num_known = true;
2562 goto restart;
2563 }
2564 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2565 evt_i = 0;
2566 while (evt_i < evt_num) {
2567 if (name_only) {
2568 printf("%s ", evt_list[evt_i++]);
2569 continue;
2570 }
2571 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
2572 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002573 if (evt_num && pager_in_use())
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002574 printf("\n");
Yunlong Songab0e4802015-02-27 18:21:25 +08002575
2576out_free:
2577 evt_num = evt_i;
2578 for (evt_i = 0; evt_i < evt_num; evt_i++)
2579 zfree(&evt_list[evt_i]);
2580 zfree(&evt_list);
2581 return;
2582
2583out_enomem:
2584 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
2585 if (evt_list)
2586 goto out_free;
Jiri Olsa1dc127602012-07-04 00:00:44 +02002587}
2588
2589/*
2590 * Print the help text for the event symbols:
2591 */
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -07002592void print_events(const char *event_glob, bool name_only, bool quiet_flag,
Andi Kleenbf874fc2017-03-20 13:17:11 -07002593 bool long_desc, bool details_flag)
Jiri Olsa1dc127602012-07-04 00:00:44 +02002594{
Jiri Olsa1dc127602012-07-04 00:00:44 +02002595 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002596 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
Jiri Olsa1dc127602012-07-04 00:00:44 +02002597
2598 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002599 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
Andi Kleen5e0861b2019-03-26 15:18:23 -07002600 print_tool_events(event_glob, name_only);
Jiri Olsa1dc127602012-07-04 00:00:44 +02002601
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002602 print_hwcache_events(event_glob, name_only);
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002603
Andi Kleenbf874fc2017-03-20 13:17:11 -07002604 print_pmu_events(event_glob, name_only, quiet_flag, long_desc,
2605 details_flag);
Andi Kleendc098b32013-04-20 11:02:29 -07002606
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002607 if (event_glob != NULL)
2608 return;
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +05302609
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002610 if (!name_only) {
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002611 printf(" %-50s [%s]\n",
2612 "rNNN",
2613 event_type_descriptors[PERF_TYPE_RAW]);
2614 printf(" %-50s [%s]\n",
2615 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
2616 event_type_descriptors[PERF_TYPE_RAW]);
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002617 if (pager_in_use())
2618 printf(" (see 'man perf-list' on how to encode it)\n\n");
Thomas Gleixner86847b62009-06-06 12:24:17 +02002619
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002620 printf(" %-50s [%s]\n",
Jacob Shin3741eb92014-05-29 17:26:51 +02002621 "mem:<addr>[/len][:access]",
Liming Wang41bdcb22009-12-29 16:37:07 +08002622 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002623 if (pager_in_use())
2624 printf("\n");
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002625 }
Frederic Weisbecker1b290d62009-11-23 15:42:35 +01002626
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002627 print_tracepoint_events(NULL, NULL, name_only);
Masami Hiramatsu40218da2016-06-24 18:06:46 +09002628
2629 print_sdt_events(NULL, NULL, name_only);
Andi Kleen71b0acc2017-08-31 12:40:32 -07002630
Jiri Olsa33bbc572019-02-13 13:32:41 +01002631 metricgroup__print(true, true, NULL, name_only, details_flag);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002632}
Jiri Olsa8f707d82012-03-15 20:09:16 +01002633
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002634int parse_events__is_hardcoded_term(struct parse_events_term *term)
Jiri Olsa8f707d82012-03-15 20:09:16 +01002635{
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002636 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002637}
2638
Jiri Olsa67b49b32017-02-17 15:00:55 +01002639static int new_term(struct parse_events_term **_term,
2640 struct parse_events_term *temp,
2641 char *str, u64 num)
Jiri Olsa8f707d82012-03-15 20:09:16 +01002642{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002643 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002644
Jiri Olsa67b49b32017-02-17 15:00:55 +01002645 term = malloc(sizeof(*term));
Jiri Olsa8f707d82012-03-15 20:09:16 +01002646 if (!term)
2647 return -ENOMEM;
2648
Jiri Olsa67b49b32017-02-17 15:00:55 +01002649 *term = *temp;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002650 INIT_LIST_HEAD(&term->list);
Andi Kleenc2f1cea2017-10-20 13:27:55 -07002651 term->weak = false;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002652
Jiri Olsa67b49b32017-02-17 15:00:55 +01002653 switch (term->type_val) {
Jiri Olsa8f707d82012-03-15 20:09:16 +01002654 case PARSE_EVENTS__TERM_TYPE_NUM:
2655 term->val.num = num;
2656 break;
2657 case PARSE_EVENTS__TERM_TYPE_STR:
2658 term->val.str = str;
2659 break;
2660 default:
Adrian Hunter4be8be62013-07-04 16:20:24 +03002661 free(term);
Jiri Olsa8f707d82012-03-15 20:09:16 +01002662 return -EINVAL;
2663 }
2664
2665 *_term = term;
2666 return 0;
2667}
2668
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002669int parse_events_term__num(struct parse_events_term **term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002670 int type_term, char *config, u64 num,
Jiri Olsa99e71382017-02-17 15:00:56 +01002671 bool no_value,
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002672 void *loc_term_, void *loc_val_)
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002673{
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002674 YYLTYPE *loc_term = loc_term_;
2675 YYLTYPE *loc_val = loc_val_;
2676
Jiri Olsa67b49b32017-02-17 15:00:55 +01002677 struct parse_events_term temp = {
2678 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
2679 .type_term = type_term,
2680 .config = config,
Jiri Olsa99e71382017-02-17 15:00:56 +01002681 .no_value = no_value,
Jiri Olsa67b49b32017-02-17 15:00:55 +01002682 .err_term = loc_term ? loc_term->first_column : 0,
2683 .err_val = loc_val ? loc_val->first_column : 0,
2684 };
2685
2686 return new_term(term, &temp, NULL, num);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002687}
2688
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002689int parse_events_term__str(struct parse_events_term **term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002690 int type_term, char *config, char *str,
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002691 void *loc_term_, void *loc_val_)
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002692{
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002693 YYLTYPE *loc_term = loc_term_;
2694 YYLTYPE *loc_val = loc_val_;
2695
Jiri Olsa67b49b32017-02-17 15:00:55 +01002696 struct parse_events_term temp = {
2697 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
2698 .type_term = type_term,
2699 .config = config,
2700 .err_term = loc_term ? loc_term->first_column : 0,
2701 .err_val = loc_val ? loc_val->first_column : 0,
2702 };
2703
2704 return new_term(term, &temp, str, 0);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002705}
2706
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002707int parse_events_term__sym_hw(struct parse_events_term **term,
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02002708 char *config, unsigned idx)
2709{
2710 struct event_symbol *sym;
Jiri Olsa67b49b32017-02-17 15:00:55 +01002711 struct parse_events_term temp = {
2712 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
2713 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
2714 .config = config ?: (char *) "event",
2715 };
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02002716
2717 BUG_ON(idx >= PERF_COUNT_HW_MAX);
2718 sym = &event_symbols_hw[idx];
2719
Jiri Olsa67b49b32017-02-17 15:00:55 +01002720 return new_term(term, &temp, (char *) sym->symbol, 0);
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02002721}
2722
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002723int parse_events_term__clone(struct parse_events_term **new,
2724 struct parse_events_term *term)
Zheng Yana6146d52012-06-15 14:31:41 +08002725{
Jiri Olsa67b49b32017-02-17 15:00:55 +01002726 struct parse_events_term temp = {
2727 .type_val = term->type_val,
2728 .type_term = term->type_term,
2729 .config = term->config,
2730 .err_term = term->err_term,
2731 .err_val = term->err_val,
2732 };
2733
2734 return new_term(new, &temp, term->val.str, term->val.num);
Zheng Yana6146d52012-06-15 14:31:41 +08002735}
2736
Andi Kleen82557182017-03-20 13:17:03 -07002737int parse_events_copy_term_list(struct list_head *old,
2738 struct list_head **new)
2739{
2740 struct parse_events_term *term, *n;
2741 int ret;
2742
2743 if (!old) {
2744 *new = NULL;
2745 return 0;
2746 }
2747
2748 *new = malloc(sizeof(struct list_head));
2749 if (!*new)
2750 return -ENOMEM;
2751 INIT_LIST_HEAD(*new);
2752
2753 list_for_each_entry (term, old, list) {
2754 ret = parse_events_term__clone(&n, term);
2755 if (ret)
2756 return ret;
2757 list_add_tail(&n->list, *new);
2758 }
2759 return 0;
2760}
2761
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002762void parse_events_terms__purge(struct list_head *terms)
Jiri Olsa8f707d82012-03-15 20:09:16 +01002763{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002764 struct parse_events_term *term, *h;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002765
Wang Nana8adfce2016-02-12 16:31:23 -03002766 list_for_each_entry_safe(term, h, terms, list) {
Wang Nan2d055bf2016-02-22 09:10:34 +00002767 if (term->array.nr_ranges)
Taeung Song360e0712017-02-01 21:34:07 +09002768 zfree(&term->array.ranges);
Wang Nana8adfce2016-02-12 16:31:23 -03002769 list_del_init(&term->list);
Jiri Olsa8f707d82012-03-15 20:09:16 +01002770 free(term);
Wang Nana8adfce2016-02-12 16:31:23 -03002771 }
Jiri Olsa8f707d82012-03-15 20:09:16 +01002772}
Jiri Olsab39b8392015-04-22 21:10:16 +02002773
Arnaldo Carvalho de Melo2146afc2016-02-12 17:09:17 -03002774void parse_events_terms__delete(struct list_head *terms)
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002775{
Arnaldo Carvalho de Melo2146afc2016-02-12 17:09:17 -03002776 if (!terms)
2777 return;
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002778 parse_events_terms__purge(terms);
Wang Nand20a5f22016-02-12 17:01:17 -03002779 free(terms);
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002780}
2781
Wang Nan2d055bf2016-02-22 09:10:34 +00002782void parse_events__clear_array(struct parse_events_array *a)
2783{
Taeung Song360e0712017-02-01 21:34:07 +09002784 zfree(&a->ranges);
Wang Nan2d055bf2016-02-22 09:10:34 +00002785}
2786
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03002787void parse_events_evlist_error(struct parse_events_state *parse_state,
Jiri Olsab39b8392015-04-22 21:10:16 +02002788 int idx, const char *str)
2789{
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03002790 struct parse_events_error *err = parse_state->error;
Jiri Olsab39b8392015-04-22 21:10:16 +02002791
Adrian Huntera6ced2b2015-05-19 16:05:44 +03002792 if (!err)
2793 return;
Jiri Olsab39b8392015-04-22 21:10:16 +02002794 err->idx = idx;
2795 err->str = strdup(str);
2796 WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
2797}
He Kuangffeb8832015-09-28 03:52:14 +00002798
Wang Nan17cb5f82016-02-19 11:43:57 +00002799static void config_terms_list(char *buf, size_t buf_sz)
2800{
2801 int i;
2802 bool first = true;
2803
2804 buf[0] = '\0';
2805 for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
2806 const char *name = config_term_names[i];
2807
Wang Nan1669e502016-02-19 11:43:58 +00002808 if (!config_term_avail(i, NULL))
2809 continue;
Wang Nan17cb5f82016-02-19 11:43:57 +00002810 if (!name)
2811 continue;
2812 if (name[0] == '<')
2813 continue;
2814
2815 if (strlen(buf) + strlen(name) + 2 >= buf_sz)
2816 return;
2817
2818 if (!first)
2819 strcat(buf, ",");
2820 else
2821 first = false;
2822 strcat(buf, name);
2823 }
2824}
2825
He Kuangffeb8832015-09-28 03:52:14 +00002826/*
2827 * Return string contains valid config terms of an event.
2828 * @additional_terms: For terms such as PMU sysfs terms.
2829 */
2830char *parse_events_formats_error_string(char *additional_terms)
2831{
2832 char *str;
Wang Nan626a6b72016-07-14 08:34:45 +00002833 /* "no-overwrite" is the longest name */
Wang Nan17cb5f82016-02-19 11:43:57 +00002834 char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
Wang Nan626a6b72016-07-14 08:34:45 +00002835 (sizeof("no-overwrite") - 1)];
He Kuangffeb8832015-09-28 03:52:14 +00002836
Wang Nan17cb5f82016-02-19 11:43:57 +00002837 config_terms_list(static_terms, sizeof(static_terms));
He Kuangffeb8832015-09-28 03:52:14 +00002838 /* valid terms */
2839 if (additional_terms) {
Wang Nan26dee022016-02-19 11:43:52 +00002840 if (asprintf(&str, "valid terms: %s,%s",
2841 additional_terms, static_terms) < 0)
He Kuangffeb8832015-09-28 03:52:14 +00002842 goto fail;
2843 } else {
Wang Nan26dee022016-02-19 11:43:52 +00002844 if (asprintf(&str, "valid terms: %s", static_terms) < 0)
He Kuangffeb8832015-09-28 03:52:14 +00002845 goto fail;
2846 }
2847 return str;
2848
2849fail:
2850 return NULL;
2851}