blob: 8182b1e66ec6cf9c0ac778fc7629ab4779b9eee9 [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>
Jiri Olsa9c3516d2019-07-21 13:24:30 +020027#include <perf/cpumap.h>
Zheng Yanac20de6f2012-06-15 14:31:39 +080028#include "parse-events-bison.h"
Jiri Olsa90e2b222012-06-15 14:31:40 +080029#define YY_EXTRA_TYPE int
Jiri Olsa89812fc2012-03-15 20:09:15 +010030#include "parse-events-flex.h"
Jiri Olsa5f537a22012-03-15 20:09:18 +010031#include "pmu.h"
Namhyung Kimb41f1ce2013-08-27 11:41:53 +090032#include "thread_map.h"
Jiri Olsaf30a79b2015-06-23 00:36:04 +020033#include "cpumap.h"
Masami Hiramatsu40218da2016-06-24 18:06:46 +090034#include "probe-file.h"
Jiri Olsab39b8392015-04-22 21:10:16 +020035#include "asm/bug.h"
Andi Kleenac12f672016-10-12 14:02:06 -070036#include "util/parse-branch-options.h"
Andi Kleen71b0acc2017-08-31 12:40:32 -070037#include "metricgroup.h"
Jiri Olsa89812fc2012-03-15 20:09:15 +010038
39#define MAX_NAME_LEN 100
Ingo Molnar8ad8db32009-05-26 11:10:09 +020040
Jiri Olsa82ba1f22012-05-21 09:12:50 +020041#ifdef PARSER_DEBUG
42extern int parse_events_debug;
43#endif
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -030044int parse_events_parse(void *parse_state, void *scanner);
He Kuange637d172015-09-28 03:52:16 +000045static int get_config_terms(struct list_head *head_config,
46 struct list_head *head_terms __maybe_unused);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +020047
Kan Liangdcb4e102014-10-07 11:08:50 -040048static struct perf_pmu_event_symbol *perf_pmu_events_list;
49/*
50 * The variable indicates the number of supported pmu event symbols.
51 * 0 means not initialized and ready to init
52 * -1 means failed to init, don't try anymore
53 * >0 is the number of supported pmu event symbols
54 */
55static int perf_pmu_events_list_num;
56
Yunlong Song705750f2015-02-27 18:21:27 +080057struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
Jiri Olsa1dc127602012-07-04 00:00:44 +020058 [PERF_COUNT_HW_CPU_CYCLES] = {
59 .symbol = "cpu-cycles",
60 .alias = "cycles",
61 },
62 [PERF_COUNT_HW_INSTRUCTIONS] = {
63 .symbol = "instructions",
64 .alias = "",
65 },
66 [PERF_COUNT_HW_CACHE_REFERENCES] = {
67 .symbol = "cache-references",
68 .alias = "",
69 },
70 [PERF_COUNT_HW_CACHE_MISSES] = {
71 .symbol = "cache-misses",
72 .alias = "",
73 },
74 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
75 .symbol = "branch-instructions",
76 .alias = "branches",
77 },
78 [PERF_COUNT_HW_BRANCH_MISSES] = {
79 .symbol = "branch-misses",
80 .alias = "",
81 },
82 [PERF_COUNT_HW_BUS_CYCLES] = {
83 .symbol = "bus-cycles",
84 .alias = "",
85 },
86 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
87 .symbol = "stalled-cycles-frontend",
88 .alias = "idle-cycles-frontend",
89 },
90 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
91 .symbol = "stalled-cycles-backend",
92 .alias = "idle-cycles-backend",
93 },
94 [PERF_COUNT_HW_REF_CPU_CYCLES] = {
95 .symbol = "ref-cycles",
96 .alias = "",
97 },
98};
Ingo Molnar8ad8db32009-05-26 11:10:09 +020099
Yunlong Song705750f2015-02-27 18:21:27 +0800100struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
Jiri Olsa1dc127602012-07-04 00:00:44 +0200101 [PERF_COUNT_SW_CPU_CLOCK] = {
102 .symbol = "cpu-clock",
103 .alias = "",
104 },
105 [PERF_COUNT_SW_TASK_CLOCK] = {
106 .symbol = "task-clock",
107 .alias = "",
108 },
109 [PERF_COUNT_SW_PAGE_FAULTS] = {
110 .symbol = "page-faults",
111 .alias = "faults",
112 },
113 [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
114 .symbol = "context-switches",
115 .alias = "cs",
116 },
117 [PERF_COUNT_SW_CPU_MIGRATIONS] = {
118 .symbol = "cpu-migrations",
119 .alias = "migrations",
120 },
121 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
122 .symbol = "minor-faults",
123 .alias = "",
124 },
125 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
126 .symbol = "major-faults",
127 .alias = "",
128 },
129 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
130 .symbol = "alignment-faults",
131 .alias = "",
132 },
133 [PERF_COUNT_SW_EMULATION_FAULTS] = {
134 .symbol = "emulation-faults",
135 .alias = "",
136 },
Adrian Hunterd22d1a22013-08-31 21:50:52 +0300137 [PERF_COUNT_SW_DUMMY] = {
138 .symbol = "dummy",
139 .alias = "",
140 },
Arnaldo Carvalho de Melobae9cc42015-11-27 15:54:33 -0300141 [PERF_COUNT_SW_BPF_OUTPUT] = {
142 .symbol = "bpf-output",
143 .alias = "",
144 },
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200145};
146
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200147#define __PERF_EVENT_FIELD(config, name) \
148 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
Ingo Molnar52425192009-05-26 09:17:18 +0200149
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200150#define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200151#define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200152#define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200153#define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
Ingo Molnar52425192009-05-26 09:17:18 +0200154
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300155#define for_each_subsystem(sys_dir, sys_dirent) \
156 while ((sys_dirent = readdir(sys_dir)) != NULL) \
157 if (sys_dirent->d_type == DT_DIR && \
158 (strcmp(sys_dirent->d_name, ".")) && \
159 (strcmp(sys_dirent->d_name, "..")))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400160
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -0300161static int tp_event_has_id(const char *dir_path, struct dirent *evt_dir)
Peter Zijlstraae07b632009-08-06 16:48:54 +0200162{
163 char evt_path[MAXPATHLEN];
164 int fd;
165
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -0300166 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, evt_dir->d_name);
Peter Zijlstraae07b632009-08-06 16:48:54 +0200167 fd = open(evt_path, O_RDONLY);
168 if (fd < 0)
169 return -EINVAL;
170 close(fd);
171
172 return 0;
173}
174
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -0300175#define for_each_event(dir_path, evt_dir, evt_dirent) \
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300176 while ((evt_dirent = readdir(evt_dir)) != NULL) \
177 if (evt_dirent->d_type == DT_DIR && \
178 (strcmp(evt_dirent->d_name, ".")) && \
179 (strcmp(evt_dirent->d_name, "..")) && \
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -0300180 (!tp_event_has_id(dir_path, evt_dirent)))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400181
Li Zefan270bbbe2009-09-17 16:34:51 +0800182#define MAX_EVENT_LENGTH 512
Jason Baronf6bdafe2009-07-21 12:20:22 -0400183
Jason Baronf6bdafe2009-07-21 12:20:22 -0400184
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200185struct tracepoint_path *tracepoint_id_to_path(u64 config)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400186{
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200187 struct tracepoint_path *path = NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400188 DIR *sys_dir, *evt_dir;
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300189 struct dirent *sys_dirent, *evt_dirent;
Stephane Eranian8aa8a7c2012-03-13 16:51:02 +0100190 char id_buf[24];
Eric Dumazet725b1362009-09-24 15:39:09 +0200191 int fd;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400192 u64 id;
193 char evt_path[MAXPATHLEN];
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300194 char *dir_path;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400195
Arnaldo Carvalho de Melo7014e0e2018-05-17 14:42:39 -0300196 sys_dir = tracing_events__opendir();
Jason Baronf6bdafe2009-07-21 12:20:22 -0400197 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200198 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400199
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300200 for_each_subsystem(sys_dir, sys_dirent) {
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300201 dir_path = get_events_file(sys_dirent->d_name);
202 if (!dir_path)
203 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200204 evt_dir = opendir(dir_path);
205 if (!evt_dir)
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300206 goto next;
Eric Dumazet725b1362009-09-24 15:39:09 +0200207
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -0300208 for_each_event(dir_path, evt_dir, evt_dirent) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200209
Jiri Olsa77f18152018-03-19 09:29:01 +0100210 scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
211 evt_dirent->d_name);
Eric Dumazet725b1362009-09-24 15:39:09 +0200212 fd = open(evt_path, O_RDONLY);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400213 if (fd < 0)
214 continue;
215 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
216 close(fd);
217 continue;
218 }
219 close(fd);
220 id = atoll(id_buf);
221 if (id == config) {
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300222 put_events_file(dir_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400223 closedir(evt_dir);
224 closedir(sys_dir);
Julia Lawall59b4cae2009-12-06 10:16:30 +0100225 path = zalloc(sizeof(*path));
Taeung Song5aa365f2017-02-01 21:34:05 +0900226 if (!path)
227 return NULL;
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200228 path->system = malloc(MAX_EVENT_LENGTH);
229 if (!path->system) {
230 free(path);
231 return NULL;
232 }
233 path->name = malloc(MAX_EVENT_LENGTH);
234 if (!path->name) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300235 zfree(&path->system);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200236 free(path);
237 return NULL;
238 }
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300239 strncpy(path->system, sys_dirent->d_name,
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200240 MAX_EVENT_LENGTH);
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300241 strncpy(path->name, evt_dirent->d_name,
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200242 MAX_EVENT_LENGTH);
243 return path;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400244 }
245 }
246 closedir(evt_dir);
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300247next:
248 put_events_file(dir_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400249 }
250
Jason Baronf6bdafe2009-07-21 12:20:22 -0400251 closedir(sys_dir);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200252 return NULL;
253}
254
Namhyung Kime7c93f02013-06-26 16:14:05 +0900255struct tracepoint_path *tracepoint_name_to_path(const char *name)
256{
257 struct tracepoint_path *path = zalloc(sizeof(*path));
258 char *str = strchr(name, ':');
259
260 if (path == NULL || str == NULL) {
261 free(path);
262 return NULL;
263 }
264
265 path->system = strndup(name, str - name);
266 path->name = strdup(str+1);
267
268 if (path->system == NULL || path->name == NULL) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300269 zfree(&path->system);
270 zfree(&path->name);
Taeung Song506fde12017-02-01 21:34:06 +0900271 zfree(&path);
Namhyung Kime7c93f02013-06-26 16:14:05 +0900272 }
273
274 return path;
275}
276
David Ahern1424dc92011-03-09 22:23:28 -0700277const char *event_type(int type)
278{
279 switch (type) {
280 case PERF_TYPE_HARDWARE:
281 return "hardware";
282
283 case PERF_TYPE_SOFTWARE:
284 return "software";
285
286 case PERF_TYPE_TRACEPOINT:
287 return "tracepoint";
288
289 case PERF_TYPE_HW_CACHE:
290 return "hardware-cache";
291
292 default:
293 break;
294 }
295
296 return "unknown";
297}
298
Wang Nane814fdd2016-02-19 11:43:59 +0000299static int parse_events__is_name_term(struct parse_events_term *term)
300{
301 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
302}
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800303
Wang Nane814fdd2016-02-19 11:43:59 +0000304static char *get_config_name(struct list_head *head_terms)
305{
306 struct parse_events_term *term;
307
308 if (!head_terms)
309 return NULL;
310
311 list_for_each_entry(term, head_terms, list)
312 if (parse_events__is_name_term(term))
313 return term->val.str;
314
315 return NULL;
316}
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800317
Jiri Olsa32dcd022019-07-21 13:23:51 +0200318static struct evsel *
Stephane Eranian410136f2013-11-12 17:58:49 +0100319__add_event(struct list_head *list, int *idx,
320 struct perf_event_attr *attr,
Mark Rutland66ec11912017-10-06 19:38:22 +0100321 char *name, struct perf_pmu *pmu,
Andi Kleenf0fbb112019-03-26 15:18:21 -0700322 struct list_head *config_terms, bool auto_merge_stats,
323 const char *cpu_list)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100324{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200325 struct evsel *evsel;
Jiri Olsaf8548392019-07-21 13:23:49 +0200326 struct perf_cpu_map *cpus = pmu ? pmu->cpus :
Jiri Olsa9c3516d2019-07-21 13:24:30 +0200327 cpu_list ? perf_cpu_map__new(cpu_list) : NULL;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100328
329 event_attr_init(attr);
330
Taeung Song75fc5ae2017-02-01 21:34:04 +0900331 evsel = perf_evsel__new_idx(attr, *idx);
David Ahernc5cd8ac2013-07-02 13:27:25 -0600332 if (!evsel)
Stephane Eranian410136f2013-11-12 17:58:49 +0100333 return NULL;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100334
Taeung Song75fc5ae2017-02-01 21:34:04 +0900335 (*idx)++;
Jiri Olsad400bd32019-07-21 13:24:37 +0200336 evsel->core.cpus = perf_cpu_map__get(cpus);
Jiri Olsafe1f61b2019-07-21 13:24:38 +0200337 evsel->core.own_cpus = perf_cpu_map__get(cpus);
Mark Rutland66ec11912017-10-06 19:38:22 +0100338 evsel->system_wide = pmu ? pmu->is_uncore : false;
Arnaldo Carvalho de Melo63ce8442017-08-31 15:32:18 -0300339 evsel->auto_merge_stats = auto_merge_stats;
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200340
Arnaldo Carvalho de Melo9db17632012-06-12 13:45:00 -0300341 if (name)
342 evsel->name = strdup(name);
Jiri Olsa930a2e22015-07-29 05:42:10 -0400343
344 if (config_terms)
345 list_splice(config_terms, &evsel->config_terms);
346
Jiri Olsab27c4ec2019-07-21 13:24:22 +0200347 list_add_tail(&evsel->core.node, list);
Stephane Eranian410136f2013-11-12 17:58:49 +0100348 return evsel;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100349}
350
David Ahernc5cd8ac2013-07-02 13:27:25 -0600351static int add_event(struct list_head *list, int *idx,
Jiri Olsa930a2e22015-07-29 05:42:10 -0400352 struct perf_event_attr *attr, char *name,
353 struct list_head *config_terms)
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800354{
Andi Kleenf0fbb112019-03-26 15:18:21 -0700355 return __add_event(list, idx, attr, name, NULL, config_terms, false, NULL) ? 0 : -ENOMEM;
356}
357
358static int add_event_tool(struct list_head *list, int *idx,
359 enum perf_tool_event tool_event)
360{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200361 struct evsel *evsel;
Andi Kleenf0fbb112019-03-26 15:18:21 -0700362 struct perf_event_attr attr = {
363 .type = PERF_TYPE_SOFTWARE,
364 .config = PERF_COUNT_SW_DUMMY,
365 };
366
367 evsel = __add_event(list, idx, &attr, NULL, NULL, NULL, false, "0");
368 if (!evsel)
369 return -ENOMEM;
370 evsel->tool_event = tool_event;
371 if (tool_event == PERF_TOOL_DURATION_TIME)
372 evsel->unit = strdup("ns");
373 return 0;
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800374}
375
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300376static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
Ingo Molnar8326f442009-06-05 20:22:46 +0200377{
378 int i, j;
Paul Mackerras61c45982009-07-01 13:04:34 +1000379 int n, longest = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200380
381 for (i = 0; i < size; i++) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300382 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
Paul Mackerras61c45982009-07-01 13:04:34 +1000383 n = strlen(names[i][j]);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100384 if (n > longest && !strncasecmp(str, names[i][j], n))
Paul Mackerras61c45982009-07-01 13:04:34 +1000385 longest = n;
386 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100387 if (longest > 0)
Paul Mackerras61c45982009-07-01 13:04:34 +1000388 return i;
Ingo Molnar8326f442009-06-05 20:22:46 +0200389 }
390
Ingo Molnar89536452009-06-06 21:04:17 +0200391 return -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200392}
393
Wang Nan43d0b972016-02-19 11:44:01 +0000394typedef int config_term_func_t(struct perf_event_attr *attr,
395 struct parse_events_term *term,
396 struct parse_events_error *err);
397static int config_term_common(struct perf_event_attr *attr,
398 struct parse_events_term *term,
399 struct parse_events_error *err);
400static int config_attr(struct perf_event_attr *attr,
401 struct list_head *head,
402 struct parse_events_error *err,
403 config_term_func_t config_term);
404
David Ahernc5cd8ac2013-07-02 13:27:25 -0600405int parse_events_add_cache(struct list_head *list, int *idx,
Wang Nan43d0b972016-02-19 11:44:01 +0000406 char *type, char *op_result1, char *op_result2,
Arnaldo Carvalho de Melo2c97b0d2016-02-19 19:47:04 -0300407 struct parse_events_error *err,
Wang Nan43d0b972016-02-19 11:44:01 +0000408 struct list_head *head_config)
Ingo Molnar8326f442009-06-05 20:22:46 +0200409{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100410 struct perf_event_attr attr;
Wang Nan43d0b972016-02-19 11:44:01 +0000411 LIST_HEAD(config_terms);
412 char name[MAX_NAME_LEN], *config_name;
Paul Mackerras61c45982009-07-01 13:04:34 +1000413 int cache_type = -1, cache_op = -1, cache_result = -1;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100414 char *op_result[2] = { op_result1, op_result2 };
415 int i, n;
Ingo Molnar8326f442009-06-05 20:22:46 +0200416
Ingo Molnar8326f442009-06-05 20:22:46 +0200417 /*
418 * No fallback - if we cannot get a clear cache type
419 * then bail out:
420 */
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300421 cache_type = parse_aliases(type, perf_evsel__hw_cache,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100422 PERF_COUNT_HW_CACHE_MAX);
Ingo Molnar8326f442009-06-05 20:22:46 +0200423 if (cache_type == -1)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100424 return -EINVAL;
Ingo Molnar8326f442009-06-05 20:22:46 +0200425
Wang Nan43d0b972016-02-19 11:44:01 +0000426 config_name = get_config_name(head_config);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100427 n = snprintf(name, MAX_NAME_LEN, "%s", type);
428
429 for (i = 0; (i < 2) && (op_result[i]); i++) {
430 char *str = op_result[i];
431
Jiri Olsa275ef382012-09-05 19:51:33 +0200432 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
Paul Mackerras61c45982009-07-01 13:04:34 +1000433
434 if (cache_op == -1) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300435 cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100436 PERF_COUNT_HW_CACHE_OP_MAX);
Paul Mackerras61c45982009-07-01 13:04:34 +1000437 if (cache_op >= 0) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300438 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
Jiri Olsa89812fc2012-03-15 20:09:15 +0100439 return -EINVAL;
Paul Mackerras61c45982009-07-01 13:04:34 +1000440 continue;
441 }
442 }
443
444 if (cache_result == -1) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300445 cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
446 PERF_COUNT_HW_CACHE_RESULT_MAX);
Paul Mackerras61c45982009-07-01 13:04:34 +1000447 if (cache_result >= 0)
448 continue;
449 }
Paul Mackerras61c45982009-07-01 13:04:34 +1000450 }
451
Ingo Molnar8326f442009-06-05 20:22:46 +0200452 /*
453 * Fall back to reads:
454 */
Ingo Molnar89536452009-06-06 21:04:17 +0200455 if (cache_op == -1)
456 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
Ingo Molnar8326f442009-06-05 20:22:46 +0200457
Ingo Molnar8326f442009-06-05 20:22:46 +0200458 /*
459 * Fall back to accesses:
460 */
461 if (cache_result == -1)
462 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
463
Jiri Olsa89812fc2012-03-15 20:09:15 +0100464 memset(&attr, 0, sizeof(attr));
465 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
466 attr.type = PERF_TYPE_HW_CACHE;
Wang Nan43d0b972016-02-19 11:44:01 +0000467
468 if (head_config) {
Arnaldo Carvalho de Melo2c97b0d2016-02-19 19:47:04 -0300469 if (config_attr(&attr, head_config, err,
Wang Nan43d0b972016-02-19 11:44:01 +0000470 config_term_common))
471 return -EINVAL;
472
473 if (get_config_terms(head_config, &config_terms))
474 return -ENOMEM;
475 }
476 return add_event(list, idx, &attr, config_name ? : name, &config_terms);
Ingo Molnar8326f442009-06-05 20:22:46 +0200477}
478
Jiri Olsa272ed292015-09-29 17:05:31 +0200479static void tracepoint_error(struct parse_events_error *e, int err,
Wang Nan8c619d62016-07-13 10:44:03 +0000480 const char *sys, const char *name)
Jiri Olsa19658172015-09-07 10:38:07 +0200481{
482 char help[BUFSIZ];
483
Adrian Hunterec183d22016-01-26 14:05:20 +0200484 if (!e)
485 return;
486
Jiri Olsa19658172015-09-07 10:38:07 +0200487 /*
488 * We get error directly from syscall errno ( > 0),
489 * or from encoded pointer's error ( < 0).
490 */
491 err = abs(err);
492
493 switch (err) {
494 case EACCES:
Jiri Olsa272ed292015-09-29 17:05:31 +0200495 e->str = strdup("can't access trace events");
Jiri Olsa19658172015-09-07 10:38:07 +0200496 break;
497 case ENOENT:
Jiri Olsa272ed292015-09-29 17:05:31 +0200498 e->str = strdup("unknown tracepoint");
Jiri Olsa19658172015-09-07 10:38:07 +0200499 break;
500 default:
Jiri Olsa272ed292015-09-29 17:05:31 +0200501 e->str = strdup("failed to add tracepoint");
Jiri Olsa19658172015-09-07 10:38:07 +0200502 break;
503 }
504
505 tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
Jiri Olsa272ed292015-09-29 17:05:31 +0200506 e->help = strdup(help);
Jiri Olsa19658172015-09-07 10:38:07 +0200507}
508
David Ahernc5cd8ac2013-07-02 13:27:25 -0600509static int add_tracepoint(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +0000510 const char *sys_name, const char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200511 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000512 struct list_head *head_config)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200513{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200514 struct evsel *evsel;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200515
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300516 evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
Jiri Olsa19658172015-09-07 10:38:07 +0200517 if (IS_ERR(evsel)) {
Jiri Olsa272ed292015-09-29 17:05:31 +0200518 tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200519 return PTR_ERR(evsel);
Jiri Olsa19658172015-09-07 10:38:07 +0200520 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200521
He Kuange637d172015-09-28 03:52:16 +0000522 if (head_config) {
523 LIST_HEAD(config_terms);
524
525 if (get_config_terms(head_config, &config_terms))
526 return -ENOMEM;
527 list_splice(&config_terms, &evsel->config_terms);
528 }
529
Jiri Olsab27c4ec2019-07-21 13:24:22 +0200530 list_add_tail(&evsel->core.node, list);
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300531 return 0;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200532}
533
David Ahernc5cd8ac2013-07-02 13:27:25 -0600534static int add_tracepoint_multi_event(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +0000535 const char *sys_name, const char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200536 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000537 struct list_head *head_config)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200538{
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300539 char *evt_path;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200540 struct dirent *evt_ent;
541 DIR *evt_dir;
Jiri Olsa27bf90b2015-10-05 21:31:17 +0200542 int ret = 0, found = 0;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200543
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300544 evt_path = get_events_file(sys_name);
545 if (!evt_path) {
546 tracepoint_error(err, errno, sys_name, evt_name);
547 return -1;
548 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200549 evt_dir = opendir(evt_path);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200550 if (!evt_dir) {
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300551 put_events_file(evt_path);
Jiri Olsa272ed292015-09-29 17:05:31 +0200552 tracepoint_error(err, errno, sys_name, evt_name);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100553 return -1;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200554 }
555
Jiri Olsa89812fc2012-03-15 20:09:15 +0100556 while (!ret && (evt_ent = readdir(evt_dir))) {
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200557 if (!strcmp(evt_ent->d_name, ".")
558 || !strcmp(evt_ent->d_name, "..")
559 || !strcmp(evt_ent->d_name, "enable")
560 || !strcmp(evt_ent->d_name, "filter"))
561 continue;
562
Jiri Olsa89812fc2012-03-15 20:09:15 +0100563 if (!strglobmatch(evt_ent->d_name, evt_name))
Masami Hiramatsufb1d2ed2010-01-05 17:47:17 -0500564 continue;
565
Jiri Olsa27bf90b2015-10-05 21:31:17 +0200566 found++;
567
He Kuange637d172015-09-28 03:52:16 +0000568 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200569 err, head_config);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200570 }
571
Jiri Olsa27bf90b2015-10-05 21:31:17 +0200572 if (!found) {
573 tracepoint_error(err, ENOENT, sys_name, evt_name);
574 ret = -1;
575 }
576
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300577 put_events_file(evt_path);
Jiri Olsa0bd3f082012-12-17 14:08:36 +0100578 closedir(evt_dir);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100579 return ret;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200580}
581
David Ahernc5cd8ac2013-07-02 13:27:25 -0600582static int add_tracepoint_event(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +0000583 const char *sys_name, const char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200584 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000585 struct list_head *head_config)
Jiri Olsaf35488f2012-12-17 14:08:37 +0100586{
587 return strpbrk(evt_name, "*?") ?
He Kuange637d172015-09-28 03:52:16 +0000588 add_tracepoint_multi_event(list, idx, sys_name, evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200589 err, head_config) :
He Kuange637d172015-09-28 03:52:16 +0000590 add_tracepoint(list, idx, sys_name, evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200591 err, head_config);
Jiri Olsaf35488f2012-12-17 14:08:37 +0100592}
593
David Ahernc5cd8ac2013-07-02 13:27:25 -0600594static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +0000595 const char *sys_name, const char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200596 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000597 struct list_head *head_config)
Jiri Olsaf35488f2012-12-17 14:08:37 +0100598{
599 struct dirent *events_ent;
600 DIR *events_dir;
601 int ret = 0;
602
Arnaldo Carvalho de Melo7014e0e2018-05-17 14:42:39 -0300603 events_dir = tracing_events__opendir();
Jiri Olsaf35488f2012-12-17 14:08:37 +0100604 if (!events_dir) {
Jiri Olsa272ed292015-09-29 17:05:31 +0200605 tracepoint_error(err, errno, sys_name, evt_name);
Jiri Olsaf35488f2012-12-17 14:08:37 +0100606 return -1;
607 }
608
609 while (!ret && (events_ent = readdir(events_dir))) {
610 if (!strcmp(events_ent->d_name, ".")
611 || !strcmp(events_ent->d_name, "..")
612 || !strcmp(events_ent->d_name, "enable")
613 || !strcmp(events_ent->d_name, "header_event")
614 || !strcmp(events_ent->d_name, "header_page"))
615 continue;
616
617 if (!strglobmatch(events_ent->d_name, sys_name))
618 continue;
619
620 ret = add_tracepoint_event(list, idx, events_ent->d_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200621 evt_name, err, head_config);
Jiri Olsaf35488f2012-12-17 14:08:37 +0100622 }
623
624 closedir(events_dir);
625 return ret;
626}
627
Wang Nan4edf30e2015-10-14 12:41:17 +0000628struct __add_bpf_event_param {
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300629 struct parse_events_state *parse_state;
Wang Nan4edf30e2015-10-14 12:41:17 +0000630 struct list_head *list;
Wang Nan95088a52016-02-22 09:10:36 +0000631 struct list_head *head_config;
Wang Nan4edf30e2015-10-14 12:41:17 +0000632};
633
Arnaldo Carvalho de Meloaf4a0992019-07-15 16:22:57 -0300634static int add_bpf_event(const char *group, const char *event, int fd, struct bpf_object *obj,
Wang Nan4edf30e2015-10-14 12:41:17 +0000635 void *_param)
636{
637 LIST_HEAD(new_evsels);
638 struct __add_bpf_event_param *param = _param;
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300639 struct parse_events_state *parse_state = param->parse_state;
Wang Nan4edf30e2015-10-14 12:41:17 +0000640 struct list_head *list = param->list;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200641 struct evsel *pos;
Wang Nan4edf30e2015-10-14 12:41:17 +0000642 int err;
Arnaldo Carvalho de Melo2620b7e2019-07-15 15:29:34 -0300643 /*
644 * Check if we should add the event, i.e. if it is a TP but starts with a '!',
645 * then don't add the tracepoint, this will be used for something else, like
646 * adding to a BPF_MAP_TYPE_PROG_ARRAY.
647 *
648 * See tools/perf/examples/bpf/augmented_raw_syscalls.c
649 */
650 if (group[0] == '!')
651 return 0;
Wang Nan4edf30e2015-10-14 12:41:17 +0000652
653 pr_debug("add bpf event %s:%s and attach bpf program %d\n",
Wang Nancd102d72016-07-13 10:44:04 +0000654 group, event, fd);
Wang Nan4edf30e2015-10-14 12:41:17 +0000655
Arnaldo Carvalho de Melo5d369a72017-08-17 14:13:25 -0300656 err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group,
657 event, parse_state->error,
Wang Nan95088a52016-02-22 09:10:36 +0000658 param->head_config);
Wang Nan4edf30e2015-10-14 12:41:17 +0000659 if (err) {
Jiri Olsa32dcd022019-07-21 13:23:51 +0200660 struct evsel *evsel, *tmp;
Wang Nan4edf30e2015-10-14 12:41:17 +0000661
662 pr_debug("Failed to add BPF event %s:%s\n",
Wang Nancd102d72016-07-13 10:44:04 +0000663 group, event);
Jiri Olsab27c4ec2019-07-21 13:24:22 +0200664 list_for_each_entry_safe(evsel, tmp, &new_evsels, core.node) {
665 list_del_init(&evsel->core.node);
Jiri Olsa5eb2dd22019-07-21 13:23:57 +0200666 evsel__delete(evsel);
Wang Nan4edf30e2015-10-14 12:41:17 +0000667 }
668 return err;
669 }
Wang Nancd102d72016-07-13 10:44:04 +0000670 pr_debug("adding %s:%s\n", group, event);
Wang Nan4edf30e2015-10-14 12:41:17 +0000671
Jiri Olsab27c4ec2019-07-21 13:24:22 +0200672 list_for_each_entry(pos, &new_evsels, core.node) {
Wang Nan1f45b1d2015-10-14 12:41:18 +0000673 pr_debug("adding %s:%s to %p\n",
Wang Nancd102d72016-07-13 10:44:04 +0000674 group, event, pos);
Wang Nan1f45b1d2015-10-14 12:41:18 +0000675 pos->bpf_fd = fd;
Arnaldo Carvalho de Meloaf4a0992019-07-15 16:22:57 -0300676 pos->bpf_obj = obj;
Wang Nan1f45b1d2015-10-14 12:41:18 +0000677 }
Wang Nan4edf30e2015-10-14 12:41:17 +0000678 list_splice(&new_evsels, list);
679 return 0;
680}
681
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300682int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
Wang Nan84c86ca2015-10-14 12:41:14 +0000683 struct list_head *list,
Wang Nan95088a52016-02-22 09:10:36 +0000684 struct bpf_object *obj,
685 struct list_head *head_config)
Wang Nan84c86ca2015-10-14 12:41:14 +0000686{
687 int err;
688 char errbuf[BUFSIZ];
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300689 struct __add_bpf_event_param param = {parse_state, list, head_config};
Wang Nanaa3abf32015-10-14 12:41:15 +0000690 static bool registered_unprobe_atexit = false;
Wang Nan84c86ca2015-10-14 12:41:14 +0000691
692 if (IS_ERR(obj) || !obj) {
693 snprintf(errbuf, sizeof(errbuf),
694 "Internal error: load bpf obj with NULL");
695 err = -EINVAL;
696 goto errout;
697 }
698
699 /*
Wang Nanaa3abf32015-10-14 12:41:15 +0000700 * Register atexit handler before calling bpf__probe() so
701 * bpf__probe() don't need to unprobe probe points its already
702 * created when failure.
703 */
704 if (!registered_unprobe_atexit) {
705 atexit(bpf__clear);
706 registered_unprobe_atexit = true;
707 }
708
709 err = bpf__probe(obj);
710 if (err) {
711 bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
712 goto errout;
713 }
714
Wang Nan1e5e3ee2015-10-14 12:41:16 +0000715 err = bpf__load(obj);
716 if (err) {
717 bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
718 goto errout;
719 }
720
Wang Nancd102d72016-07-13 10:44:04 +0000721 err = bpf__foreach_event(obj, add_bpf_event, &param);
Wang Nan4edf30e2015-10-14 12:41:17 +0000722 if (err) {
723 snprintf(errbuf, sizeof(errbuf),
724 "Attach events in BPF object failed");
725 goto errout;
726 }
727
728 return 0;
Wang Nan84c86ca2015-10-14 12:41:14 +0000729errout:
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300730 parse_state->error->help = strdup("(add -v to see detail)");
731 parse_state->error->str = strdup(errbuf);
Wang Nan84c86ca2015-10-14 12:41:14 +0000732 return err;
733}
734
Wang Nana34f3be2016-02-22 09:10:31 +0000735static int
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300736parse_events_config_bpf(struct parse_events_state *parse_state,
Wang Nana34f3be2016-02-22 09:10:31 +0000737 struct bpf_object *obj,
738 struct list_head *head_config)
739{
740 struct parse_events_term *term;
741 int error_pos;
742
743 if (!head_config || list_empty(head_config))
744 return 0;
745
746 list_for_each_entry(term, head_config, list) {
747 char errbuf[BUFSIZ];
748 int err;
749
750 if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) {
751 snprintf(errbuf, sizeof(errbuf),
752 "Invalid config term for BPF object");
753 errbuf[BUFSIZ - 1] = '\0';
754
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300755 parse_state->error->idx = term->err_term;
756 parse_state->error->str = strdup(errbuf);
Wang Nana34f3be2016-02-22 09:10:31 +0000757 return -EINVAL;
758 }
759
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300760 err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos);
Wang Nana34f3be2016-02-22 09:10:31 +0000761 if (err) {
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300762 bpf__strerror_config_obj(obj, term, parse_state->evlist,
Wang Nana34f3be2016-02-22 09:10:31 +0000763 &error_pos, err, errbuf,
764 sizeof(errbuf));
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300765 parse_state->error->help = strdup(
Wang Nan7630b3e2016-02-22 09:10:33 +0000766"Hint:\tValid config terms:\n"
Wang Nane571e022016-02-22 09:10:35 +0000767" \tmap:[<arraymap>].value<indices>=[value]\n"
768" \tmap:[<eventmap>].event<indices>=[event]\n"
Wang Nan7630b3e2016-02-22 09:10:33 +0000769"\n"
Wang Nane571e022016-02-22 09:10:35 +0000770" \twhere <indices> is something like [0,3...5] or [all]\n"
Wang Nana34f3be2016-02-22 09:10:31 +0000771" \t(add -v to see detail)");
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300772 parse_state->error->str = strdup(errbuf);
Wang Nana34f3be2016-02-22 09:10:31 +0000773 if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE)
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300774 parse_state->error->idx = term->err_val;
Wang Nana34f3be2016-02-22 09:10:31 +0000775 else
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300776 parse_state->error->idx = term->err_term + error_pos;
Wang Nana34f3be2016-02-22 09:10:31 +0000777 return err;
778 }
779 }
780 return 0;
781}
782
Wang Nan95088a52016-02-22 09:10:36 +0000783/*
784 * Split config terms:
785 * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
786 * 'call-graph=fp' is 'evt config', should be applied to each
787 * events in bpf.c.
788 * 'map:array.value[0]=1' is 'obj config', should be processed
789 * with parse_events_config_bpf.
790 *
791 * Move object config terms from the first list to obj_head_config.
792 */
793static void
794split_bpf_config_terms(struct list_head *evt_head_config,
795 struct list_head *obj_head_config)
796{
797 struct parse_events_term *term, *temp;
798
799 /*
800 * Currectly, all possible user config term
801 * belong to bpf object. parse_events__is_hardcoded_term()
802 * happends to be a good flag.
803 *
804 * See parse_events_config_bpf() and
805 * config_term_tracepoint().
806 */
807 list_for_each_entry_safe(term, temp, evt_head_config, list)
808 if (!parse_events__is_hardcoded_term(term))
809 list_move_tail(&term->list, obj_head_config);
810}
811
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300812int parse_events_load_bpf(struct parse_events_state *parse_state,
Wang Nan84c86ca2015-10-14 12:41:14 +0000813 struct list_head *list,
Wang Nand509db02015-10-14 12:41:20 +0000814 char *bpf_file_name,
Wang Nana34f3be2016-02-22 09:10:31 +0000815 bool source,
816 struct list_head *head_config)
Wang Nan84c86ca2015-10-14 12:41:14 +0000817{
Wang Nana34f3be2016-02-22 09:10:31 +0000818 int err;
Wang Nan95088a52016-02-22 09:10:36 +0000819 struct bpf_object *obj;
820 LIST_HEAD(obj_head_config);
821
822 if (head_config)
823 split_bpf_config_terms(head_config, &obj_head_config);
Wang Nan84c86ca2015-10-14 12:41:14 +0000824
Wang Nand509db02015-10-14 12:41:20 +0000825 obj = bpf__prepare_load(bpf_file_name, source);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000826 if (IS_ERR(obj)) {
Wang Nan84c86ca2015-10-14 12:41:14 +0000827 char errbuf[BUFSIZ];
Wang Nan84c86ca2015-10-14 12:41:14 +0000828
Wang Nan6371ca3b2015-11-06 13:49:37 +0000829 err = PTR_ERR(obj);
Wang Nan84c86ca2015-10-14 12:41:14 +0000830
831 if (err == -ENOTSUP)
832 snprintf(errbuf, sizeof(errbuf),
833 "BPF support is not compiled");
834 else
Wang Nand3e0ce32015-11-06 13:58:09 +0000835 bpf__strerror_prepare_load(bpf_file_name,
836 source,
837 -err, errbuf,
838 sizeof(errbuf));
Wang Nan84c86ca2015-10-14 12:41:14 +0000839
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300840 parse_state->error->help = strdup("(add -v to see detail)");
841 parse_state->error->str = strdup(errbuf);
Wang Nan84c86ca2015-10-14 12:41:14 +0000842 return err;
843 }
844
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300845 err = parse_events_load_bpf_obj(parse_state, list, obj, head_config);
Wang Nana34f3be2016-02-22 09:10:31 +0000846 if (err)
847 return err;
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300848 err = parse_events_config_bpf(parse_state, obj, &obj_head_config);
Wang Nan95088a52016-02-22 09:10:36 +0000849
850 /*
851 * Caller doesn't know anything about obj_head_config,
852 * so combine them together again before returnning.
853 */
854 if (head_config)
855 list_splice_tail(&obj_head_config, head_config);
856 return err;
Wang Nan84c86ca2015-10-14 12:41:14 +0000857}
858
Jiri Olsa89812fc2012-03-15 20:09:15 +0100859static int
860parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100861{
862 int i;
863
864 for (i = 0; i < 3; i++) {
Jiri Olsa89812fc2012-03-15 20:09:15 +0100865 if (!type || !type[i])
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100866 break;
867
Jiri Olsa75827322012-06-29 09:22:54 +0200868#define CHECK_SET_TYPE(bit) \
869do { \
870 if (attr->bp_type & bit) \
871 return -EINVAL; \
872 else \
873 attr->bp_type |= bit; \
874} while (0)
875
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100876 switch (type[i]) {
877 case 'r':
Jiri Olsa75827322012-06-29 09:22:54 +0200878 CHECK_SET_TYPE(HW_BREAKPOINT_R);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100879 break;
880 case 'w':
Jiri Olsa75827322012-06-29 09:22:54 +0200881 CHECK_SET_TYPE(HW_BREAKPOINT_W);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100882 break;
883 case 'x':
Jiri Olsa75827322012-06-29 09:22:54 +0200884 CHECK_SET_TYPE(HW_BREAKPOINT_X);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100885 break;
886 default:
Jiri Olsa89812fc2012-03-15 20:09:15 +0100887 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100888 }
889 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100890
Jiri Olsa75827322012-06-29 09:22:54 +0200891#undef CHECK_SET_TYPE
892
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100893 if (!attr->bp_type) /* Default */
894 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
895
Jiri Olsa89812fc2012-03-15 20:09:15 +0100896 return 0;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100897}
898
David Ahernc5cd8ac2013-07-02 13:27:25 -0600899int parse_events_add_breakpoint(struct list_head *list, int *idx,
Jacob Shin3741eb92014-05-29 17:26:51 +0200900 void *ptr, char *type, u64 len)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100901{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100902 struct perf_event_attr attr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100903
Jiri Olsa89812fc2012-03-15 20:09:15 +0100904 memset(&attr, 0, sizeof(attr));
Jiri Olsa9fafd982012-03-20 19:15:39 +0100905 attr.bp_addr = (unsigned long) ptr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100906
Jiri Olsa89812fc2012-03-15 20:09:15 +0100907 if (parse_breakpoint_type(type, &attr))
908 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100909
Jacob Shin3741eb92014-05-29 17:26:51 +0200910 /* Provide some defaults if len is not specified */
911 if (!len) {
912 if (attr.bp_type == HW_BREAKPOINT_X)
913 len = sizeof(long);
914 else
915 len = HW_BREAKPOINT_LEN_4;
916 }
917
918 attr.bp_len = len;
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200919
Jiri Olsa89812fc2012-03-15 20:09:15 +0100920 attr.type = PERF_TYPE_BREAKPOINT;
Jovi Zhang4a841d62012-07-15 03:03:10 +0800921 attr.sample_period = 1;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100922
Jiri Olsa930a2e22015-07-29 05:42:10 -0400923 return add_event(list, idx, &attr, NULL, NULL);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100924}
925
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200926static int check_type_val(struct parse_events_term *term,
927 struct parse_events_error *err,
928 int type)
Jiri Olsa8f707d82012-03-15 20:09:16 +0100929{
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200930 if (type == term->type_val)
931 return 0;
932
933 if (err) {
934 err->idx = term->err_val;
935 if (type == PARSE_EVENTS__TERM_TYPE_NUM)
936 err->str = strdup("expected numeric value");
937 else
938 err->str = strdup("expected string value");
939 }
940 return -EINVAL;
941}
942
Wang Nan17cb5f82016-02-19 11:43:57 +0000943/*
944 * Update according to parse-events.l
945 */
946static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
947 [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>",
948 [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config",
949 [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1",
950 [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2",
951 [PARSE_EVENTS__TERM_TYPE_NAME] = "name",
952 [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period",
953 [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq",
954 [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type",
955 [PARSE_EVENTS__TERM_TYPE_TIME] = "time",
956 [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph",
957 [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size",
958 [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit",
959 [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit",
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300960 [PARSE_EVENTS__TERM_TYPE_MAX_STACK] = "max-stack",
Arnaldo Carvalho de Melo2fda5ad2018-10-19 15:47:34 -0300961 [PARSE_EVENTS__TERM_TYPE_MAX_EVENTS] = "nr",
Wang Nan626a6b72016-07-14 08:34:45 +0000962 [PARSE_EVENTS__TERM_TYPE_OVERWRITE] = "overwrite",
963 [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE] = "no-overwrite",
Mathieu Poirierdd60fba2016-09-06 10:37:15 -0600964 [PARSE_EVENTS__TERM_TYPE_DRV_CFG] = "driver-config",
Jin Yao064b4e82019-04-12 21:59:47 +0800965 [PARSE_EVENTS__TERM_TYPE_PERCORE] = "percore",
Wang Nan17cb5f82016-02-19 11:43:57 +0000966};
967
Wang Nan1669e502016-02-19 11:43:58 +0000968static bool config_term_shrinked;
969
970static bool
971config_term_avail(int term_type, struct parse_events_error *err)
972{
973 if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
974 err->str = strdup("Invalid term_type");
975 return false;
976 }
977 if (!config_term_shrinked)
978 return true;
979
980 switch (term_type) {
981 case PARSE_EVENTS__TERM_TYPE_CONFIG:
982 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
983 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
984 case PARSE_EVENTS__TERM_TYPE_NAME:
Sukadev Bhattiprolu06835542016-09-15 15:24:56 -0700985 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
Jin Yao064b4e82019-04-12 21:59:47 +0800986 case PARSE_EVENTS__TERM_TYPE_PERCORE:
Wang Nan1669e502016-02-19 11:43:58 +0000987 return true;
988 default:
989 if (!err)
990 return false;
991
992 /* term_type is validated so indexing is safe */
993 if (asprintf(&err->str, "'%s' is not usable in 'perf stat'",
994 config_term_names[term_type]) < 0)
995 err->str = NULL;
996 return false;
997 }
998}
999
1000void parse_events__shrink_config_terms(void)
1001{
1002 config_term_shrinked = true;
1003}
1004
He Kuang0b8891a2015-09-28 03:52:13 +00001005static int config_term_common(struct perf_event_attr *attr,
1006 struct parse_events_term *term,
1007 struct parse_events_error *err)
Jiri Olsa3b0e3712015-04-22 21:10:22 +02001008{
1009#define CHECK_TYPE_VAL(type) \
1010do { \
1011 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
1012 return -EINVAL; \
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001013} while (0)
1014
1015 switch (term->type_term) {
Jiri Olsa8f707d82012-03-15 20:09:16 +01001016 case PARSE_EVENTS__TERM_TYPE_CONFIG:
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001017 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001018 attr->config = term->val.num;
1019 break;
1020 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001021 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001022 attr->config1 = term->val.num;
1023 break;
1024 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001025 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001026 attr->config2 = term->val.num;
1027 break;
1028 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001029 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001030 break;
Namhyung Kim09af2a52015-08-09 15:45:23 +09001031 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1032 CHECK_TYPE_VAL(NUM);
1033 break;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001034 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
Andi Kleenac12f672016-10-12 14:02:06 -07001035 CHECK_TYPE_VAL(STR);
1036 if (strcmp(term->val.str, "no") &&
1037 parse_branch_str(term->val.str, &attr->branch_sample_type)) {
1038 err->str = strdup("invalid branch sample type");
1039 err->idx = term->err_val;
1040 return -EINVAL;
1041 }
Jiri Olsa8f707d82012-03-15 20:09:16 +01001042 break;
Kan Liang32067712015-08-04 04:30:19 -04001043 case PARSE_EVENTS__TERM_TYPE_TIME:
1044 CHECK_TYPE_VAL(NUM);
1045 if (term->val.num > 1) {
1046 err->str = strdup("expected 0 or 1");
1047 err->idx = term->err_val;
1048 return -EINVAL;
1049 }
1050 break;
Kan Liangd457c962015-08-11 06:30:47 -04001051 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1052 CHECK_TYPE_VAL(STR);
1053 break;
1054 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1055 CHECK_TYPE_VAL(NUM);
1056 break;
Wang Nan374ce932015-10-28 10:55:02 +00001057 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1058 CHECK_TYPE_VAL(NUM);
1059 break;
1060 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1061 CHECK_TYPE_VAL(NUM);
1062 break;
Wang Nan626a6b72016-07-14 08:34:45 +00001063 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1064 CHECK_TYPE_VAL(NUM);
1065 break;
1066 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1067 CHECK_TYPE_VAL(NUM);
1068 break;
Jiri Olsa6b5fc392012-05-21 09:12:53 +02001069 case PARSE_EVENTS__TERM_TYPE_NAME:
1070 CHECK_TYPE_VAL(STR);
1071 break;
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -03001072 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1073 CHECK_TYPE_VAL(NUM);
1074 break;
Arnaldo Carvalho de Melo2fda5ad2018-10-19 15:47:34 -03001075 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1076 CHECK_TYPE_VAL(NUM);
1077 break;
Jin Yao064b4e82019-04-12 21:59:47 +08001078 case PARSE_EVENTS__TERM_TYPE_PERCORE:
1079 CHECK_TYPE_VAL(NUM);
1080 if ((unsigned int)term->val.num > 1) {
1081 err->str = strdup("expected 0 or 1");
1082 err->idx = term->err_val;
1083 return -EINVAL;
1084 }
1085 break;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001086 default:
He Kuangffeb8832015-09-28 03:52:14 +00001087 err->str = strdup("unknown term");
1088 err->idx = term->err_term;
1089 err->help = parse_events_formats_error_string(NULL);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001090 return -EINVAL;
1091 }
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001092
Wang Nan1669e502016-02-19 11:43:58 +00001093 /*
1094 * Check term availbility after basic checking so
1095 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
1096 *
1097 * If check availbility at the entry of this function,
1098 * user will see "'<sysfs term>' is not usable in 'perf stat'"
1099 * if an invalid config term is provided for legacy events
1100 * (for example, instructions/badterm/...), which is confusing.
1101 */
1102 if (!config_term_avail(term->type_term, err))
1103 return -EINVAL;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001104 return 0;
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001105#undef CHECK_TYPE_VAL
Jiri Olsa8f707d82012-03-15 20:09:16 +01001106}
1107
He Kuang0b8891a2015-09-28 03:52:13 +00001108static int config_term_pmu(struct perf_event_attr *attr,
1109 struct parse_events_term *term,
1110 struct parse_events_error *err)
1111{
Mathieu Poirierdd60fba2016-09-06 10:37:15 -06001112 if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER ||
1113 term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG)
He Kuang0b8891a2015-09-28 03:52:13 +00001114 /*
1115 * Always succeed for sysfs terms, as we dont know
1116 * at this point what type they need to have.
1117 */
1118 return 0;
1119 else
1120 return config_term_common(attr, term, err);
1121}
1122
He Kuange637d172015-09-28 03:52:16 +00001123static int config_term_tracepoint(struct perf_event_attr *attr,
1124 struct parse_events_term *term,
1125 struct parse_events_error *err)
1126{
1127 switch (term->type_term) {
1128 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1129 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
Wang Nan374ce932015-10-28 10:55:02 +00001130 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1131 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -03001132 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
Arnaldo Carvalho de Melo2fda5ad2018-10-19 15:47:34 -03001133 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
Wang Nan626a6b72016-07-14 08:34:45 +00001134 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1135 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
He Kuange637d172015-09-28 03:52:16 +00001136 return config_term_common(attr, term, err);
1137 default:
1138 if (err) {
1139 err->idx = term->err_term;
1140 err->str = strdup("unknown term");
1141 err->help = strdup("valid terms: call-graph,stack-size\n");
1142 }
1143 return -EINVAL;
1144 }
1145
1146 return 0;
1147}
1148
Jiri Olsa8f707d82012-03-15 20:09:16 +01001149static int config_attr(struct perf_event_attr *attr,
Jiri Olsa3b0e3712015-04-22 21:10:22 +02001150 struct list_head *head,
He Kuang0b8891a2015-09-28 03:52:13 +00001151 struct parse_events_error *err,
1152 config_term_func_t config_term)
Jiri Olsa8f707d82012-03-15 20:09:16 +01001153{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001154 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001155
1156 list_for_each_entry(term, head, list)
Jiri Olsa3b0e3712015-04-22 21:10:22 +02001157 if (config_term(attr, term, err))
Jiri Olsa8f707d82012-03-15 20:09:16 +01001158 return -EINVAL;
1159
1160 return 0;
1161}
1162
Jiri Olsa930a2e22015-07-29 05:42:10 -04001163static int get_config_terms(struct list_head *head_config,
1164 struct list_head *head_terms __maybe_unused)
1165{
1166#define ADD_CONFIG_TERM(__type, __name, __val) \
1167do { \
1168 struct perf_evsel_config_term *__t; \
1169 \
1170 __t = zalloc(sizeof(*__t)); \
1171 if (!__t) \
1172 return -ENOMEM; \
1173 \
1174 INIT_LIST_HEAD(&__t->list); \
1175 __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \
1176 __t->val.__name = __val; \
Andi Kleenc2f1cea2017-10-20 13:27:55 -07001177 __t->weak = term->weak; \
Jiri Olsa930a2e22015-07-29 05:42:10 -04001178 list_add_tail(&__t->list, head_terms); \
1179} while (0)
1180
1181 struct parse_events_term *term;
1182
1183 list_for_each_entry(term, head_config, list) {
1184 switch (term->type_term) {
Jiri Olsaee4c7582015-07-29 05:42:11 -04001185 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1186 ADD_CONFIG_TERM(PERIOD, period, term->val.num);
Kan Liang32067712015-08-04 04:30:19 -04001187 break;
Namhyung Kim09af2a52015-08-09 15:45:23 +09001188 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1189 ADD_CONFIG_TERM(FREQ, freq, term->val.num);
1190 break;
Kan Liang32067712015-08-04 04:30:19 -04001191 case PARSE_EVENTS__TERM_TYPE_TIME:
1192 ADD_CONFIG_TERM(TIME, time, term->val.num);
1193 break;
Kan Liangd457c962015-08-11 06:30:47 -04001194 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1195 ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
1196 break;
Andi Kleenac12f672016-10-12 14:02:06 -07001197 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1198 ADD_CONFIG_TERM(BRANCH, branch, term->val.str);
1199 break;
Kan Liangd457c962015-08-11 06:30:47 -04001200 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1201 ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
1202 break;
Wang Nan374ce932015-10-28 10:55:02 +00001203 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1204 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0);
1205 break;
1206 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1207 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1);
1208 break;
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -03001209 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1210 ADD_CONFIG_TERM(MAX_STACK, max_stack, term->val.num);
1211 break;
Arnaldo Carvalho de Melo2fda5ad2018-10-19 15:47:34 -03001212 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1213 ADD_CONFIG_TERM(MAX_EVENTS, max_events, term->val.num);
1214 break;
Wang Nan626a6b72016-07-14 08:34:45 +00001215 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1216 ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 1 : 0);
1217 break;
1218 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1219 ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 0 : 1);
1220 break;
Mathieu Poirierdd60fba2016-09-06 10:37:15 -06001221 case PARSE_EVENTS__TERM_TYPE_DRV_CFG:
1222 ADD_CONFIG_TERM(DRV_CFG, drv_cfg, term->val.str);
1223 break;
Jin Yao064b4e82019-04-12 21:59:47 +08001224 case PARSE_EVENTS__TERM_TYPE_PERCORE:
1225 ADD_CONFIG_TERM(PERCORE, percore,
1226 term->val.num ? true : false);
1227 break;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001228 default:
1229 break;
1230 }
1231 }
1232#undef ADD_EVSEL_CONFIG
1233 return 0;
1234}
1235
He Kuange637d172015-09-28 03:52:16 +00001236int parse_events_add_tracepoint(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +00001237 const char *sys, const char *event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001238 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +00001239 struct list_head *head_config)
1240{
1241 if (head_config) {
1242 struct perf_event_attr attr;
1243
Jiri Olsa272ed292015-09-29 17:05:31 +02001244 if (config_attr(&attr, head_config, err,
He Kuange637d172015-09-28 03:52:16 +00001245 config_term_tracepoint))
1246 return -EINVAL;
1247 }
1248
1249 if (strpbrk(sys, "*?"))
1250 return add_tracepoint_multi_sys(list, idx, sys, event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001251 err, head_config);
He Kuange637d172015-09-28 03:52:16 +00001252 else
1253 return add_tracepoint_event(list, idx, sys, event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001254 err, head_config);
He Kuange637d172015-09-28 03:52:16 +00001255}
1256
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001257int parse_events_add_numeric(struct parse_events_state *parse_state,
Jiri Olsa87d650b2015-04-22 21:10:24 +02001258 struct list_head *list,
Robert Richterb527bab2012-08-07 19:43:13 +02001259 u32 type, u64 config,
Jiri Olsa8f707d82012-03-15 20:09:16 +01001260 struct list_head *head_config)
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301261{
Jiri Olsa89812fc2012-03-15 20:09:15 +01001262 struct perf_event_attr attr;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001263 LIST_HEAD(config_terms);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301264
Jiri Olsa89812fc2012-03-15 20:09:15 +01001265 memset(&attr, 0, sizeof(attr));
1266 attr.type = type;
1267 attr.config = config;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001268
Jiri Olsa930a2e22015-07-29 05:42:10 -04001269 if (head_config) {
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001270 if (config_attr(&attr, head_config, parse_state->error,
He Kuang0b8891a2015-09-28 03:52:13 +00001271 config_term_common))
Jiri Olsa930a2e22015-07-29 05:42:10 -04001272 return -EINVAL;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001273
Jiri Olsa930a2e22015-07-29 05:42:10 -04001274 if (get_config_terms(head_config, &config_terms))
1275 return -ENOMEM;
1276 }
1277
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001278 return add_event(list, &parse_state->idx, &attr,
Wang Nan10bf3582016-02-19 11:44:00 +00001279 get_config_name(head_config), &config_terms);
Paul Mackerras61c45982009-07-01 13:04:34 +10001280}
1281
Andi Kleenf0fbb112019-03-26 15:18:21 -07001282int parse_events_add_tool(struct parse_events_state *parse_state,
1283 struct list_head *list,
1284 enum perf_tool_event tool_event)
1285{
1286 return add_event_tool(list, &parse_state->idx, tool_event);
1287}
1288
Jin Yao064b4e82019-04-12 21:59:47 +08001289static bool config_term_percore(struct list_head *config_terms)
1290{
1291 struct perf_evsel_config_term *term;
1292
1293 list_for_each_entry(term, config_terms, list) {
1294 if (term->type == PERF_EVSEL__CONFIG_TERM_PERCORE)
1295 return term->val.percore;
1296 }
1297
1298 return false;
1299}
1300
Agustin Vega-Friasc199c112018-03-06 09:04:44 -05001301int parse_events_add_pmu(struct parse_events_state *parse_state,
Jiri Olsa36adec82015-04-22 21:10:19 +02001302 struct list_head *list, char *name,
Kan Liang3cdc5c22018-05-07 14:13:43 -07001303 struct list_head *head_config,
1304 bool auto_merge_stats,
1305 bool use_alias)
Jiri Olsa5f537a22012-03-15 20:09:18 +01001306{
1307 struct perf_event_attr attr;
Matt Fleming46441bd2014-09-24 15:04:06 +01001308 struct perf_pmu_info info;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001309 struct perf_pmu *pmu;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001310 struct evsel *evsel;
Andi Kleen333b5662017-09-13 14:50:06 -07001311 struct parse_events_error *err = parse_state->error;
Kan Liang3cdc5c22018-05-07 14:13:43 -07001312 bool use_uncore_alias;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001313 LIST_HEAD(config_terms);
Jiri Olsa5f537a22012-03-15 20:09:18 +01001314
1315 pmu = perf_pmu__find(name);
Andi Kleen333b5662017-09-13 14:50:06 -07001316 if (!pmu) {
1317 if (asprintf(&err->str,
1318 "Cannot find PMU `%s'. Missing kernel support?",
1319 name) < 0)
1320 err->str = NULL;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001321 return -EINVAL;
Andi Kleen333b5662017-09-13 14:50:06 -07001322 }
Jiri Olsa5f537a22012-03-15 20:09:18 +01001323
Adrian Hunterdc0a6202014-07-31 09:00:49 +03001324 if (pmu->default_config) {
1325 memcpy(&attr, pmu->default_config,
1326 sizeof(struct perf_event_attr));
1327 } else {
1328 memset(&attr, 0, sizeof(attr));
1329 }
Jiri Olsa5f537a22012-03-15 20:09:18 +01001330
Kan Liang3cdc5c22018-05-07 14:13:43 -07001331 use_uncore_alias = (pmu->is_uncore && use_alias);
1332
Adrian Hunterad962272014-08-15 22:08:40 +03001333 if (!head_config) {
1334 attr.type = pmu->type;
Andi Kleenf0fbb112019-03-26 15:18:21 -07001335 evsel = __add_event(list, &parse_state->idx, &attr, NULL, pmu, NULL,
1336 auto_merge_stats, NULL);
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001337 if (evsel) {
1338 evsel->pmu_name = name;
Kan Liang3cdc5c22018-05-07 14:13:43 -07001339 evsel->use_uncore_alias = use_uncore_alias;
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001340 return 0;
1341 } else {
1342 return -ENOMEM;
1343 }
Adrian Hunterad962272014-08-15 22:08:40 +03001344 }
1345
Matt Fleming46441bd2014-09-24 15:04:06 +01001346 if (perf_pmu__check_alias(pmu, head_config, &info))
Zheng Yana6146d52012-06-15 14:31:41 +08001347 return -EINVAL;
1348
Jiri Olsa5f537a22012-03-15 20:09:18 +01001349 /*
1350 * Configure hardcoded terms first, no need to check
1351 * return value when called with fail == 0 ;)
1352 */
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001353 if (config_attr(&attr, head_config, parse_state->error, config_term_pmu))
Jiri Olsac056ba6a2015-04-22 21:10:18 +02001354 return -EINVAL;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001355
Jiri Olsa930a2e22015-07-29 05:42:10 -04001356 if (get_config_terms(head_config, &config_terms))
1357 return -ENOMEM;
1358
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001359 if (perf_pmu__config(pmu, &attr, head_config, parse_state->error))
Jiri Olsa5f537a22012-03-15 20:09:18 +01001360 return -EINVAL;
1361
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001362 evsel = __add_event(list, &parse_state->idx, &attr,
Mark Rutland66ec11912017-10-06 19:38:22 +01001363 get_config_name(head_config), pmu,
Andi Kleenf0fbb112019-03-26 15:18:21 -07001364 &config_terms, auto_merge_stats, NULL);
Stephane Eranian410136f2013-11-12 17:58:49 +01001365 if (evsel) {
Matt Fleming46441bd2014-09-24 15:04:06 +01001366 evsel->unit = info.unit;
1367 evsel->scale = info.scale;
Matt Fleming044330c2014-11-21 10:31:12 +01001368 evsel->per_pkg = info.per_pkg;
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001369 evsel->snapshot = info.snapshot;
Andi Kleen37932c12017-03-20 13:17:08 -07001370 evsel->metric_expr = info.metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001371 evsel->metric_name = info.metric_name;
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001372 evsel->pmu_name = name;
Kan Liang3cdc5c22018-05-07 14:13:43 -07001373 evsel->use_uncore_alias = use_uncore_alias;
Jin Yao064b4e82019-04-12 21:59:47 +08001374 evsel->percore = config_term_percore(&evsel->config_terms);
Stephane Eranian410136f2013-11-12 17:58:49 +01001375 }
1376
1377 return evsel ? 0 : -ENOMEM;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001378}
1379
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001380int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
Andi Kleen2073ad32017-03-20 13:17:02 -07001381 char *str, struct list_head **listp)
1382{
1383 struct list_head *head;
1384 struct parse_events_term *term;
1385 struct list_head *list;
1386 struct perf_pmu *pmu = NULL;
1387 int ok = 0;
1388
1389 *listp = NULL;
1390 /* Add it for all PMUs that support the alias */
1391 list = malloc(sizeof(struct list_head));
1392 if (!list)
1393 return -1;
1394 INIT_LIST_HEAD(list);
1395 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1396 struct perf_pmu_alias *alias;
1397
1398 list_for_each_entry(alias, &pmu->aliases, list) {
1399 if (!strcasecmp(alias->name, str)) {
1400 head = malloc(sizeof(struct list_head));
1401 if (!head)
1402 return -1;
1403 INIT_LIST_HEAD(head);
1404 if (parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
1405 str, 1, false, &str, NULL) < 0)
1406 return -1;
1407 list_add_tail(&term->list, head);
1408
Agustin Vega-Friasc199c112018-03-06 09:04:44 -05001409 if (!parse_events_add_pmu(parse_state, list,
Kan Liang3cdc5c22018-05-07 14:13:43 -07001410 pmu->name, head,
1411 true, true)) {
Andi Kleen2073ad32017-03-20 13:17:02 -07001412 pr_debug("%s -> %s/%s/\n", str,
1413 pmu->name, alias->str);
1414 ok++;
1415 }
1416
1417 parse_events_terms__delete(head);
1418 }
1419 }
1420 }
1421 if (!ok)
1422 return -1;
1423 *listp = list;
1424 return 0;
1425}
1426
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001427int parse_events__modifier_group(struct list_head *list,
1428 char *event_mod)
Jiri Olsa89efb022012-08-08 12:14:14 +02001429{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001430 return parse_events__modifier_event(list, event_mod, true);
1431}
1432
Kan Liang3cdc5c22018-05-07 14:13:43 -07001433/*
1434 * Check if the two uncore PMUs are from the same uncore block
1435 * The format of the uncore PMU name is uncore_#blockname_#pmuidx
1436 */
1437static bool is_same_uncore_block(const char *pmu_name_a, const char *pmu_name_b)
1438{
1439 char *end_a, *end_b;
1440
1441 end_a = strrchr(pmu_name_a, '_');
1442 end_b = strrchr(pmu_name_b, '_');
1443
1444 if (!end_a || !end_b)
1445 return false;
1446
1447 if ((end_a - pmu_name_a) != (end_b - pmu_name_b))
1448 return false;
1449
1450 return (strncmp(pmu_name_a, pmu_name_b, end_a - pmu_name_a) == 0);
1451}
1452
1453static int
1454parse_events__set_leader_for_uncore_aliase(char *name, struct list_head *list,
1455 struct parse_events_state *parse_state)
1456{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001457 struct evsel *evsel, *leader;
Kan Liang3cdc5c22018-05-07 14:13:43 -07001458 uintptr_t *leaders;
1459 bool is_leader = true;
1460 int i, nr_pmu = 0, total_members, ret = 0;
1461
Jiri Olsab27c4ec2019-07-21 13:24:22 +02001462 leader = list_first_entry(list, struct evsel, core.node);
1463 evsel = list_last_entry(list, struct evsel, core.node);
Kan Liang3cdc5c22018-05-07 14:13:43 -07001464 total_members = evsel->idx - leader->idx + 1;
1465
1466 leaders = calloc(total_members, sizeof(uintptr_t));
1467 if (WARN_ON(!leaders))
1468 return 0;
1469
1470 /*
1471 * Going through the whole group and doing sanity check.
1472 * All members must use alias, and be from the same uncore block.
1473 * Also, storing the leader events in an array.
1474 */
1475 __evlist__for_each_entry(list, evsel) {
1476
1477 /* Only split the uncore group which members use alias */
1478 if (!evsel->use_uncore_alias)
1479 goto out;
1480
1481 /* The events must be from the same uncore block */
1482 if (!is_same_uncore_block(leader->pmu_name, evsel->pmu_name))
1483 goto out;
1484
1485 if (!is_leader)
1486 continue;
1487 /*
1488 * If the event's PMU name starts to repeat, it must be a new
1489 * event. That can be used to distinguish the leader from
1490 * other members, even they have the same event name.
1491 */
1492 if ((leader != evsel) && (leader->pmu_name == evsel->pmu_name)) {
1493 is_leader = false;
1494 continue;
1495 }
1496 /* The name is always alias name */
1497 WARN_ON(strcmp(leader->name, evsel->name));
1498
1499 /* Store the leader event for each PMU */
1500 leaders[nr_pmu++] = (uintptr_t) evsel;
1501 }
1502
1503 /* only one event alias */
1504 if (nr_pmu == total_members) {
1505 parse_state->nr_groups--;
1506 goto handled;
1507 }
1508
1509 /*
1510 * An uncore event alias is a joint name which means the same event
1511 * runs on all PMUs of a block.
1512 * Perf doesn't support mixed events from different PMUs in the same
1513 * group. The big group has to be split into multiple small groups
1514 * which only include the events from the same PMU.
1515 *
1516 * Here the uncore event aliases must be from the same uncore block.
1517 * The number of PMUs must be same for each alias. The number of new
1518 * small groups equals to the number of PMUs.
1519 * Setting the leader event for corresponding members in each group.
1520 */
1521 i = 0;
1522 __evlist__for_each_entry(list, evsel) {
1523 if (i >= nr_pmu)
1524 i = 0;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001525 evsel->leader = (struct evsel *) leaders[i++];
Kan Liang3cdc5c22018-05-07 14:13:43 -07001526 }
1527
1528 /* The number of members and group name are same for each group */
1529 for (i = 0; i < nr_pmu; i++) {
Jiri Olsa32dcd022019-07-21 13:23:51 +02001530 evsel = (struct evsel *) leaders[i];
Kan Liang3cdc5c22018-05-07 14:13:43 -07001531 evsel->nr_members = total_members / nr_pmu;
1532 evsel->group_name = name ? strdup(name) : NULL;
1533 }
1534
1535 /* Take the new small groups into account */
1536 parse_state->nr_groups += nr_pmu - 1;
1537
1538handled:
1539 ret = 1;
1540out:
1541 free(leaders);
1542 return ret;
1543}
1544
1545void parse_events__set_leader(char *name, struct list_head *list,
1546 struct parse_events_state *parse_state)
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001547{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001548 struct evsel *leader;
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001549
Wang Nan854f7362015-09-06 07:13:17 +00001550 if (list_empty(list)) {
1551 WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1552 return;
1553 }
1554
Kan Liang3cdc5c22018-05-07 14:13:43 -07001555 if (parse_events__set_leader_for_uncore_aliase(name, list, parse_state))
1556 return;
1557
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -03001558 __perf_evlist__set_leader(list);
Jiri Olsab27c4ec2019-07-21 13:24:22 +02001559 leader = list_entry(list->next, struct evsel, core.node);
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001560 leader->group_name = name ? strdup(name) : NULL;
Jiri Olsa89efb022012-08-08 12:14:14 +02001561}
1562
David Ahernc5cd8ac2013-07-02 13:27:25 -06001563/* list_event is assumed to point to malloc'ed memory */
Jiri Olsa5d7be902012-03-20 19:15:40 +01001564void parse_events_update_lists(struct list_head *list_event,
1565 struct list_head *list_all)
1566{
1567 /*
1568 * Called for single event definition. Update the
Jiri Olsa89efb022012-08-08 12:14:14 +02001569 * 'all event' list, and reinit the 'single event'
Jiri Olsa5d7be902012-03-20 19:15:40 +01001570 * list, for next event definition.
1571 */
1572 list_splice_tail(list_event, list_all);
Jiri Olsab847cbd2012-05-21 09:12:51 +02001573 free(list_event);
Jiri Olsa5d7be902012-03-20 19:15:40 +01001574}
1575
Jiri Olsaf5b11352012-08-08 12:21:54 +02001576struct event_modifier {
1577 int eu;
1578 int ek;
1579 int eh;
1580 int eH;
1581 int eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001582 int eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001583 int precise;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001584 int precise_max;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001585 int exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02001586 int sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001587 int pinned;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001588 int weak;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001589};
Paul Mackerras61c45982009-07-01 13:04:34 +10001590
Jiri Olsaf5b11352012-08-08 12:21:54 +02001591static int get_event_modifier(struct event_modifier *mod, char *str,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001592 struct evsel *evsel)
Jiri Olsaf5b11352012-08-08 12:21:54 +02001593{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001594 int eu = evsel ? evsel->core.attr.exclude_user : 0;
1595 int ek = evsel ? evsel->core.attr.exclude_kernel : 0;
1596 int eh = evsel ? evsel->core.attr.exclude_hv : 0;
1597 int eH = evsel ? evsel->core.attr.exclude_host : 0;
1598 int eG = evsel ? evsel->core.attr.exclude_guest : 0;
1599 int eI = evsel ? evsel->core.attr.exclude_idle : 0;
1600 int precise = evsel ? evsel->core.attr.precise_ip : 0;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001601 int precise_max = 0;
Jiri Olsa3c176312012-10-10 17:39:03 +02001602 int sample_read = 0;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001603 int pinned = evsel ? evsel->core.attr.pinned : 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001604
1605 int exclude = eu | ek | eh;
1606 int exclude_GH = evsel ? evsel->exclude_GH : 0;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001607 int weak = 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001608
Jiri Olsaf5b11352012-08-08 12:21:54 +02001609 memset(mod, 0, sizeof(*mod));
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001610
Paul Mackerras61c45982009-07-01 13:04:34 +10001611 while (*str) {
Peter Zijlstraab608342010-04-08 23:03:20 +02001612 if (*str == 'u') {
1613 if (!exclude)
1614 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001615 eu = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +02001616 } else if (*str == 'k') {
1617 if (!exclude)
1618 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001619 ek = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +02001620 } else if (*str == 'h') {
1621 if (!exclude)
1622 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001623 eh = 0;
Joerg Roedel99320cc2012-01-04 17:54:19 +01001624 } else if (*str == 'G') {
1625 if (!exclude_GH)
1626 exclude_GH = eG = eH = 1;
1627 eG = 0;
1628 } else if (*str == 'H') {
1629 if (!exclude_GH)
1630 exclude_GH = eG = eH = 1;
1631 eH = 0;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001632 } else if (*str == 'I') {
1633 eI = 1;
Peter Zijlstraab608342010-04-08 23:03:20 +02001634 } else if (*str == 'p') {
1635 precise++;
David Ahern13427982012-09-13 14:59:13 -06001636 /* use of precise requires exclude_guest */
1637 if (!exclude_GH)
1638 eG = 1;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001639 } else if (*str == 'P') {
1640 precise_max = 1;
Jiri Olsa3c176312012-10-10 17:39:03 +02001641 } else if (*str == 'S') {
1642 sample_read = 1;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001643 } else if (*str == 'D') {
1644 pinned = 1;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001645 } else if (*str == 'W') {
1646 weak = 1;
Peter Zijlstraab608342010-04-08 23:03:20 +02001647 } else
Paul Mackerras61c45982009-07-01 13:04:34 +10001648 break;
Peter Zijlstraab608342010-04-08 23:03:20 +02001649
Paul Mackerras61c45982009-07-01 13:04:34 +10001650 ++str;
1651 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001652
Jiri Olsa89812fc2012-03-15 20:09:15 +01001653 /*
1654 * precise ip:
1655 *
1656 * 0 - SAMPLE_IP can have arbitrary skid
1657 * 1 - SAMPLE_IP must have constant skid
1658 * 2 - SAMPLE_IP requested to have 0 skid
1659 * 3 - SAMPLE_IP must have 0 skid
1660 *
1661 * See also PERF_RECORD_MISC_EXACT_IP
1662 */
1663 if (precise > 3)
1664 return -EINVAL;
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001665
Jiri Olsaf5b11352012-08-08 12:21:54 +02001666 mod->eu = eu;
1667 mod->ek = ek;
1668 mod->eh = eh;
1669 mod->eH = eH;
1670 mod->eG = eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001671 mod->eI = eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001672 mod->precise = precise;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001673 mod->precise_max = precise_max;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001674 mod->exclude_GH = exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02001675 mod->sample_read = sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001676 mod->pinned = pinned;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001677 mod->weak = weak;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001678
Jiri Olsaf5b11352012-08-08 12:21:54 +02001679 return 0;
1680}
1681
Jiri Olsa534123f2012-11-13 15:32:58 +01001682/*
1683 * Basic modifier sanity check to validate it contains only one
1684 * instance of any modifier (apart from 'p') present.
1685 */
1686static int check_modifier(char *str)
1687{
1688 char *p = str;
1689
1690 /* The sizeof includes 0 byte as well. */
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001691 if (strlen(str) > (sizeof("ukhGHpppPSDIW") - 1))
Jiri Olsa534123f2012-11-13 15:32:58 +01001692 return -1;
1693
1694 while (*p) {
1695 if (*p != 'p' && strchr(p + 1, *p))
1696 return -1;
1697 p++;
1698 }
1699
1700 return 0;
1701}
1702
Jiri Olsaf5b11352012-08-08 12:21:54 +02001703int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1704{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001705 struct evsel *evsel;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001706 struct event_modifier mod;
1707
1708 if (str == NULL)
1709 return 0;
1710
Jiri Olsa534123f2012-11-13 15:32:58 +01001711 if (check_modifier(str))
1712 return -EINVAL;
1713
Jiri Olsaf5b11352012-08-08 12:21:54 +02001714 if (!add && get_event_modifier(&mod, str, NULL))
1715 return -EINVAL;
1716
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001717 __evlist__for_each_entry(list, evsel) {
Jiri Olsaf5b11352012-08-08 12:21:54 +02001718 if (add && get_event_modifier(&mod, str, evsel))
1719 return -EINVAL;
1720
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001721 evsel->core.attr.exclude_user = mod.eu;
1722 evsel->core.attr.exclude_kernel = mod.ek;
1723 evsel->core.attr.exclude_hv = mod.eh;
1724 evsel->core.attr.precise_ip = mod.precise;
1725 evsel->core.attr.exclude_host = mod.eH;
1726 evsel->core.attr.exclude_guest = mod.eG;
1727 evsel->core.attr.exclude_idle = mod.eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001728 evsel->exclude_GH = mod.exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02001729 evsel->sample_read = mod.sample_read;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001730 evsel->precise_max = mod.precise_max;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001731 evsel->weak_group = mod.weak;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001732
1733 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001734 evsel->core.attr.pinned = mod.pinned;
Jiri Olsa89812fc2012-03-15 20:09:15 +01001735 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001736
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301737 return 0;
1738}
1739
Robert Richterac2ba9f2012-08-16 21:10:21 +02001740int parse_events_name(struct list_head *list, char *name)
1741{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001742 struct evsel *evsel;
Robert Richterac2ba9f2012-08-16 21:10:21 +02001743
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001744 __evlist__for_each_entry(list, evsel) {
Robert Richterac2ba9f2012-08-16 21:10:21 +02001745 if (!evsel->name)
1746 evsel->name = strdup(name);
1747 }
1748
1749 return 0;
1750}
1751
Kan Liangdcb4e102014-10-07 11:08:50 -04001752static int
1753comp_pmu(const void *p1, const void *p2)
1754{
1755 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
1756 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
1757
Andi Kleene312bcf2016-09-15 15:24:53 -07001758 return strcasecmp(pmu1->symbol, pmu2->symbol);
Kan Liangdcb4e102014-10-07 11:08:50 -04001759}
1760
1761static void perf_pmu__parse_cleanup(void)
1762{
1763 if (perf_pmu_events_list_num > 0) {
1764 struct perf_pmu_event_symbol *p;
1765 int i;
1766
1767 for (i = 0; i < perf_pmu_events_list_num; i++) {
1768 p = perf_pmu_events_list + i;
Taeung Song360e0712017-02-01 21:34:07 +09001769 zfree(&p->symbol);
Kan Liangdcb4e102014-10-07 11:08:50 -04001770 }
Taeung Song506fde12017-02-01 21:34:06 +09001771 zfree(&perf_pmu_events_list);
Kan Liangdcb4e102014-10-07 11:08:50 -04001772 perf_pmu_events_list_num = 0;
1773 }
1774}
1775
1776#define SET_SYMBOL(str, stype) \
1777do { \
1778 p->symbol = str; \
1779 if (!p->symbol) \
1780 goto err; \
1781 p->type = stype; \
1782} while (0)
1783
1784/*
1785 * Read the pmu events list from sysfs
1786 * Save it into perf_pmu_events_list
1787 */
1788static void perf_pmu__parse_init(void)
1789{
1790
1791 struct perf_pmu *pmu = NULL;
1792 struct perf_pmu_alias *alias;
1793 int len = 0;
1794
Andi Kleen231bb2a2017-01-27 18:03:39 -08001795 pmu = NULL;
1796 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1797 list_for_each_entry(alias, &pmu->aliases, list) {
1798 if (strchr(alias->name, '-'))
1799 len++;
1800 len++;
1801 }
1802 }
1803
1804 if (len == 0) {
Kan Liangdcb4e102014-10-07 11:08:50 -04001805 perf_pmu_events_list_num = -1;
1806 return;
1807 }
Kan Liangdcb4e102014-10-07 11:08:50 -04001808 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1809 if (!perf_pmu_events_list)
1810 return;
1811 perf_pmu_events_list_num = len;
1812
1813 len = 0;
Andi Kleen231bb2a2017-01-27 18:03:39 -08001814 pmu = NULL;
1815 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1816 list_for_each_entry(alias, &pmu->aliases, list) {
1817 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1818 char *tmp = strchr(alias->name, '-');
Kan Liangdcb4e102014-10-07 11:08:50 -04001819
Andi Kleen231bb2a2017-01-27 18:03:39 -08001820 if (tmp != NULL) {
1821 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1822 PMU_EVENT_SYMBOL_PREFIX);
1823 p++;
1824 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1825 len += 2;
1826 } else {
1827 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1828 len++;
1829 }
Kan Liangdcb4e102014-10-07 11:08:50 -04001830 }
1831 }
1832 qsort(perf_pmu_events_list, len,
1833 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1834
1835 return;
1836err:
1837 perf_pmu__parse_cleanup();
1838}
1839
1840enum perf_pmu_event_symbol_type
1841perf_pmu__parse_check(const char *name)
1842{
1843 struct perf_pmu_event_symbol p, *r;
1844
1845 /* scan kernel pmu events from sysfs if needed */
1846 if (perf_pmu_events_list_num == 0)
1847 perf_pmu__parse_init();
1848 /*
1849 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1850 * cpu-cycles has been handled by hardcode.
1851 * So it must be cpu// events, not kernel pmu event.
1852 */
1853 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1854 return PMU_EVENT_SYMBOL_ERR;
1855
1856 p.symbol = strdup(name);
1857 r = bsearch(&p, perf_pmu_events_list,
1858 (size_t) perf_pmu_events_list_num,
1859 sizeof(struct perf_pmu_event_symbol), comp_pmu);
Taeung Song360e0712017-02-01 21:34:07 +09001860 zfree(&p.symbol);
Kan Liangdcb4e102014-10-07 11:08:50 -04001861 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1862}
1863
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001864static int parse_events__scanner(const char *str, void *parse_state, int start_token)
Zheng Yanac20de6f2012-06-15 14:31:39 +08001865{
1866 YY_BUFFER_STATE buffer;
1867 void *scanner;
1868 int ret;
1869
Jiri Olsa90e2b222012-06-15 14:31:40 +08001870 ret = parse_events_lex_init_extra(start_token, &scanner);
Zheng Yanac20de6f2012-06-15 14:31:39 +08001871 if (ret)
1872 return ret;
1873
1874 buffer = parse_events__scan_string(str, scanner);
1875
1876#ifdef PARSER_DEBUG
1877 parse_events_debug = 1;
1878#endif
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001879 ret = parse_events_parse(parse_state, scanner);
Zheng Yanac20de6f2012-06-15 14:31:39 +08001880
1881 parse_events__flush_buffer(buffer, scanner);
1882 parse_events__delete_buffer(buffer, scanner);
1883 parse_events_lex_destroy(scanner);
1884 return ret;
1885}
1886
Jiri Olsa90e2b222012-06-15 14:31:40 +08001887/*
1888 * parse event config string, return a list of event terms.
1889 */
1890int parse_events_terms(struct list_head *terms, const char *str)
1891{
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001892 struct parse_events_state parse_state = {
Jiri Olsa90e2b222012-06-15 14:31:40 +08001893 .terms = NULL,
1894 };
1895 int ret;
1896
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001897 ret = parse_events__scanner(str, &parse_state, PE_START_TERMS);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001898 if (!ret) {
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001899 list_splice(parse_state.terms, terms);
1900 zfree(&parse_state.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001901 return 0;
1902 }
1903
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001904 parse_events_terms__delete(parse_state.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001905 return ret;
1906}
1907
Jiri Olsa63503db2019-07-21 13:23:52 +02001908int parse_events(struct evlist *evlist, const char *str,
Jiri Olsab39b8392015-04-22 21:10:16 +02001909 struct parse_events_error *err)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001910{
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001911 struct parse_events_state parse_state = {
1912 .list = LIST_HEAD_INIT(parse_state.list),
Jiri Olsa6484d2f2019-07-21 13:24:28 +02001913 .idx = evlist->core.nr_entries,
Wang Nan7630b3e2016-02-22 09:10:33 +00001914 .error = err,
1915 .evlist = evlist,
Jiri Olsa46010ab2012-06-15 14:31:38 +08001916 };
Jiri Olsa46010ab2012-06-15 14:31:38 +08001917 int ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001918
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001919 ret = parse_events__scanner(str, &parse_state, PE_START_EVENTS);
Kan Liangdcb4e102014-10-07 11:08:50 -04001920 perf_pmu__parse_cleanup();
Jiri Olsa89812fc2012-03-15 20:09:15 +01001921 if (!ret) {
Jiri Olsa32dcd022019-07-21 13:23:51 +02001922 struct evsel *last;
Wang Nan15bfd2c2015-07-10 07:36:09 +00001923
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001924 if (list_empty(&parse_state.list)) {
Arnaldo Carvalho de Meloc23080a2018-05-03 15:59:58 -03001925 WARN_ONCE(true, "WARNING: event parser found nothing\n");
Wang Nan854f7362015-09-06 07:13:17 +00001926 return -1;
1927 }
1928
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001929 perf_evlist__splice_list_tail(evlist, &parse_state.list);
1930 evlist->nr_groups += parse_state.nr_groups;
Wang Nan15bfd2c2015-07-10 07:36:09 +00001931 last = perf_evlist__last(evlist);
1932 last->cmdline_group_boundary = true;
1933
Jiri Olsa89812fc2012-03-15 20:09:15 +01001934 return 0;
1935 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02001936
Jiri Olsa5d7be902012-03-20 19:15:40 +01001937 /*
1938 * There are 2 users - builtin-record and builtin-test objects.
Jiri Olsac12995a2019-07-21 13:23:56 +02001939 * Both call evlist__delete in case of error, so we dont
Jiri Olsa5d7be902012-03-20 19:15:40 +01001940 * need to bother.
1941 */
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02001942 return ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001943}
1944
Jiri Olsab39b8392015-04-22 21:10:16 +02001945#define MAX_WIDTH 1000
1946static int get_term_width(void)
1947{
1948 struct winsize ws;
1949
1950 get_term_dimensions(&ws);
1951 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1952}
1953
Andi Kleen333b5662017-09-13 14:50:06 -07001954void parse_events_print_error(struct parse_events_error *err,
1955 const char *event)
Jiri Olsab39b8392015-04-22 21:10:16 +02001956{
1957 const char *str = "invalid or unsupported event: ";
1958 char _buf[MAX_WIDTH];
1959 char *buf = (char *) event;
1960 int idx = 0;
1961
1962 if (err->str) {
1963 /* -2 for extra '' in the final fprintf */
1964 int width = get_term_width() - 2;
1965 int len_event = strlen(event);
1966 int len_str, max_len, cut = 0;
1967
1968 /*
1969 * Maximum error index indent, we will cut
1970 * the event string if it's bigger.
1971 */
Adrian Hunter141b2d32015-07-17 19:33:51 +03001972 int max_err_idx = 13;
Jiri Olsab39b8392015-04-22 21:10:16 +02001973
1974 /*
1975 * Let's be specific with the message when
1976 * we have the precise error.
1977 */
1978 str = "event syntax error: ";
1979 len_str = strlen(str);
1980 max_len = width - len_str;
1981
1982 buf = _buf;
1983
Adam Buchbinderbd1a0be52016-02-24 10:02:25 -08001984 /* We're cutting from the beginning. */
Jiri Olsab39b8392015-04-22 21:10:16 +02001985 if (err->idx > max_err_idx)
1986 cut = err->idx - max_err_idx;
1987
1988 strncpy(buf, event + cut, max_len);
1989
1990 /* Mark cut parts with '..' on both sides. */
1991 if (cut)
1992 buf[0] = buf[1] = '.';
1993
1994 if ((len_event - cut) > max_len) {
1995 buf[max_len - 1] = buf[max_len - 2] = '.';
1996 buf[max_len] = 0;
1997 }
1998
1999 idx = len_str + err->idx - cut;
2000 }
2001
2002 fprintf(stderr, "%s'%s'\n", str, buf);
2003 if (idx) {
2004 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
2005 if (err->help)
2006 fprintf(stderr, "\n%s\n", err->help);
Taeung Song360e0712017-02-01 21:34:07 +09002007 zfree(&err->str);
2008 zfree(&err->help);
Jiri Olsab39b8392015-04-22 21:10:16 +02002009 }
Jiri Olsab39b8392015-04-22 21:10:16 +02002010}
2011
2012#undef MAX_WIDTH
2013
Jiri Olsaf120f9d2011-07-14 11:25:32 +02002014int parse_events_option(const struct option *opt, const char *str,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002015 int unset __maybe_unused)
Jiri Olsaf120f9d2011-07-14 11:25:32 +02002016{
Jiri Olsa63503db2019-07-21 13:23:52 +02002017 struct evlist *evlist = *(struct evlist **)opt->value;
Jiri Olsab39b8392015-04-22 21:10:16 +02002018 struct parse_events_error err = { .idx = 0, };
2019 int ret = parse_events(evlist, str, &err);
Andi Kleen9175ce12012-10-26 13:30:06 -07002020
Andi Kleen333b5662017-09-13 14:50:06 -07002021 if (ret) {
Jiri Olsab39b8392015-04-22 21:10:16 +02002022 parse_events_print_error(&err, str);
Andi Kleen333b5662017-09-13 14:50:06 -07002023 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
2024 }
Jiri Olsab39b8392015-04-22 21:10:16 +02002025
Andi Kleen9175ce12012-10-26 13:30:06 -07002026 return ret;
Jiri Olsaf120f9d2011-07-14 11:25:32 +02002027}
2028
Wang Nan4ba1faa2015-07-10 07:36:10 +00002029static int
Jiri Olsa63503db2019-07-21 13:23:52 +02002030foreach_evsel_in_last_glob(struct evlist *evlist,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002031 int (*func)(struct evsel *evsel,
Wang Nan4ba1faa2015-07-10 07:36:10 +00002032 const void *arg),
2033 const void *arg)
Li Zefanc171b552009-10-15 11:22:07 +08002034{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002035 struct evsel *last = NULL;
Wang Nan4ba1faa2015-07-10 07:36:10 +00002036 int err;
Li Zefanc171b552009-10-15 11:22:07 +08002037
Wang Nan854f7362015-09-06 07:13:17 +00002038 /*
2039 * Don't return when list_empty, give func a chance to report
2040 * error when it found last == NULL.
2041 *
2042 * So no need to WARN here, let *func do this.
2043 */
Jiri Olsa6484d2f2019-07-21 13:24:28 +02002044 if (evlist->core.nr_entries > 0)
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03002045 last = perf_evlist__last(evlist);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02002046
Wang Nan15bfd2c2015-07-10 07:36:09 +00002047 do {
Wang Nan4ba1faa2015-07-10 07:36:10 +00002048 err = (*func)(last, arg);
2049 if (err)
Wang Nan15bfd2c2015-07-10 07:36:09 +00002050 return -1;
Wang Nan4ba1faa2015-07-10 07:36:10 +00002051 if (!last)
2052 return 0;
Wang Nan15bfd2c2015-07-10 07:36:09 +00002053
Jiri Olsace9036a2019-07-21 13:24:23 +02002054 if (last->core.node.prev == &evlist->core.entries)
Wang Nan15bfd2c2015-07-10 07:36:09 +00002055 return 0;
Jiri Olsab27c4ec2019-07-21 13:24:22 +02002056 last = list_entry(last->core.node.prev, struct evsel, core.node);
Wang Nan15bfd2c2015-07-10 07:36:09 +00002057 } while (!last->cmdline_group_boundary);
Li Zefanc171b552009-10-15 11:22:07 +08002058
2059 return 0;
2060}
2061
Jiri Olsa32dcd022019-07-21 13:23:51 +02002062static int set_filter(struct evsel *evsel, const void *arg)
Wang Nan4ba1faa2015-07-10 07:36:10 +00002063{
2064 const char *str = arg;
Mathieu Poirier1e857482016-09-16 08:44:05 -06002065 bool found = false;
2066 int nr_addr_filters = 0;
2067 struct perf_pmu *pmu = NULL;
Wang Nan4ba1faa2015-07-10 07:36:10 +00002068
Jack Henschel49836f72018-07-04 14:13:45 +02002069 if (evsel == NULL) {
2070 fprintf(stderr,
2071 "--filter option should follow a -e tracepoint or HW tracer option\n");
2072 return -1;
2073 }
Mathieu Poirier1e857482016-09-16 08:44:05 -06002074
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002075 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) {
Mathieu Poirier1e857482016-09-16 08:44:05 -06002076 if (perf_evsel__append_tp_filter(evsel, str) < 0) {
2077 fprintf(stderr,
2078 "not enough memory to hold filter string\n");
2079 return -1;
2080 }
2081
2082 return 0;
Wang Nan4ba1faa2015-07-10 07:36:10 +00002083 }
2084
Mathieu Poirier1e857482016-09-16 08:44:05 -06002085 while ((pmu = perf_pmu__scan(pmu)) != NULL)
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002086 if (pmu->type == evsel->core.attr.type) {
Mathieu Poirier1e857482016-09-16 08:44:05 -06002087 found = true;
2088 break;
2089 }
2090
2091 if (found)
2092 perf_pmu__scan_file(pmu, "nr_addr_filters",
2093 "%d", &nr_addr_filters);
2094
Jack Henschel49836f72018-07-04 14:13:45 +02002095 if (!nr_addr_filters) {
2096 fprintf(stderr,
2097 "This CPU does not support address filtering\n");
2098 return -1;
2099 }
Mathieu Poirier1e857482016-09-16 08:44:05 -06002100
2101 if (perf_evsel__append_addr_filter(evsel, str) < 0) {
Wang Nan4ba1faa2015-07-10 07:36:10 +00002102 fprintf(stderr,
2103 "not enough memory to hold filter string\n");
2104 return -1;
2105 }
2106
2107 return 0;
2108}
2109
2110int parse_filter(const struct option *opt, const char *str,
2111 int unset __maybe_unused)
2112{
Jiri Olsa63503db2019-07-21 13:23:52 +02002113 struct evlist *evlist = *(struct evlist **)opt->value;
Wang Nan4ba1faa2015-07-10 07:36:10 +00002114
2115 return foreach_evsel_in_last_glob(evlist, set_filter,
2116 (const void *)str);
2117}
2118
Jiri Olsa32dcd022019-07-21 13:23:51 +02002119static int add_exclude_perf_filter(struct evsel *evsel,
Wang Nan4ba1faa2015-07-10 07:36:10 +00002120 const void *arg __maybe_unused)
2121{
2122 char new_filter[64];
2123
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002124 if (evsel == NULL || evsel->core.attr.type != PERF_TYPE_TRACEPOINT) {
Wang Nan4ba1faa2015-07-10 07:36:10 +00002125 fprintf(stderr,
2126 "--exclude-perf option should follow a -e tracepoint option\n");
2127 return -1;
2128 }
2129
2130 snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
2131
Mathieu Poirier3541c032016-09-16 08:44:04 -06002132 if (perf_evsel__append_tp_filter(evsel, new_filter) < 0) {
Wang Nan4ba1faa2015-07-10 07:36:10 +00002133 fprintf(stderr,
2134 "not enough memory to hold filter string\n");
2135 return -1;
2136 }
2137
2138 return 0;
2139}
2140
2141int exclude_perf(const struct option *opt,
2142 const char *arg __maybe_unused,
2143 int unset __maybe_unused)
2144{
Jiri Olsa63503db2019-07-21 13:23:52 +02002145 struct evlist *evlist = *(struct evlist **)opt->value;
Wang Nan4ba1faa2015-07-10 07:36:10 +00002146
2147 return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
2148 NULL);
2149}
2150
Thomas Gleixner86847b62009-06-06 12:24:17 +02002151static const char * const event_type_descriptors[] = {
Thomas Gleixner86847b62009-06-06 12:24:17 +02002152 "Hardware event",
2153 "Software event",
2154 "Tracepoint event",
2155 "Hardware cache event",
Liming Wang41bdcb22009-12-29 16:37:07 +08002156 "Raw hardware event descriptor",
2157 "Hardware breakpoint",
Thomas Gleixner86847b62009-06-06 12:24:17 +02002158};
2159
Yunlong Songab0e4802015-02-27 18:21:25 +08002160static int cmp_string(const void *a, const void *b)
2161{
2162 const char * const *as = a;
2163 const char * const *bs = b;
2164
2165 return strcmp(*as, *bs);
2166}
2167
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002168/*
Jason Baronf6bdafe2009-07-21 12:20:22 -04002169 * Print the events from <debugfs_mount_point>/tracing/events
2170 */
2171
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002172void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
2173 bool name_only)
Jason Baronf6bdafe2009-07-21 12:20:22 -04002174{
2175 DIR *sys_dir, *evt_dir;
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002176 struct dirent *sys_dirent, *evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -04002177 char evt_path[MAXPATHLEN];
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002178 char *dir_path;
Yunlong Songab0e4802015-02-27 18:21:25 +08002179 char **evt_list = NULL;
2180 unsigned int evt_i = 0, evt_num = 0;
2181 bool evt_num_known = false;
Jason Baronf6bdafe2009-07-21 12:20:22 -04002182
Yunlong Songab0e4802015-02-27 18:21:25 +08002183restart:
Arnaldo Carvalho de Melo7014e0e2018-05-17 14:42:39 -03002184 sys_dir = tracing_events__opendir();
Jason Baronf6bdafe2009-07-21 12:20:22 -04002185 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +02002186 return;
Jason Baronf6bdafe2009-07-21 12:20:22 -04002187
Yunlong Songab0e4802015-02-27 18:21:25 +08002188 if (evt_num_known) {
2189 evt_list = zalloc(sizeof(char *) * evt_num);
2190 if (!evt_list)
2191 goto out_close_sys_dir;
2192 }
2193
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002194 for_each_subsystem(sys_dir, sys_dirent) {
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03002195 if (subsys_glob != NULL &&
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002196 !strglobmatch(sys_dirent->d_name, subsys_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002197 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02002198
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002199 dir_path = get_events_file(sys_dirent->d_name);
2200 if (!dir_path)
2201 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02002202 evt_dir = opendir(dir_path);
2203 if (!evt_dir)
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002204 goto next;
Eric Dumazet725b1362009-09-24 15:39:09 +02002205
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -03002206 for_each_event(dir_path, evt_dir, evt_dirent) {
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03002207 if (event_glob != NULL &&
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002208 !strglobmatch(evt_dirent->d_name, event_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002209 continue;
2210
Yunlong Songab0e4802015-02-27 18:21:25 +08002211 if (!evt_num_known) {
2212 evt_num++;
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002213 continue;
2214 }
2215
Jason Baronf6bdafe2009-07-21 12:20:22 -04002216 snprintf(evt_path, MAXPATHLEN, "%s:%s",
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002217 sys_dirent->d_name, evt_dirent->d_name);
Yunlong Songab0e4802015-02-27 18:21:25 +08002218
2219 evt_list[evt_i] = strdup(evt_path);
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002220 if (evt_list[evt_i] == NULL) {
2221 put_events_file(dir_path);
Yunlong Songab0e4802015-02-27 18:21:25 +08002222 goto out_close_evt_dir;
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002223 }
Yunlong Songab0e4802015-02-27 18:21:25 +08002224 evt_i++;
Jason Baronf6bdafe2009-07-21 12:20:22 -04002225 }
2226 closedir(evt_dir);
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002227next:
2228 put_events_file(dir_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -04002229 }
Jason Baronf6bdafe2009-07-21 12:20:22 -04002230 closedir(sys_dir);
Yunlong Songab0e4802015-02-27 18:21:25 +08002231
2232 if (!evt_num_known) {
2233 evt_num_known = true;
2234 goto restart;
2235 }
2236 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2237 evt_i = 0;
2238 while (evt_i < evt_num) {
2239 if (name_only) {
2240 printf("%s ", evt_list[evt_i++]);
2241 continue;
2242 }
2243 printf(" %-50s [%s]\n", evt_list[evt_i++],
2244 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2245 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002246 if (evt_num && pager_in_use())
Yunlong Songab0e4802015-02-27 18:21:25 +08002247 printf("\n");
2248
2249out_free:
2250 evt_num = evt_i;
2251 for (evt_i = 0; evt_i < evt_num; evt_i++)
2252 zfree(&evt_list[evt_i]);
2253 zfree(&evt_list);
2254 return;
2255
2256out_close_evt_dir:
2257 closedir(evt_dir);
2258out_close_sys_dir:
2259 closedir(sys_dir);
2260
2261 printf("FATAL: not enough memory to print %s\n",
2262 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2263 if (evt_list)
2264 goto out_free;
Jason Baronf6bdafe2009-07-21 12:20:22 -04002265}
2266
2267/*
Thomas Renninger20c457b2011-01-03 17:50:45 +01002268 * Check whether event is in <debugfs_mount_point>/tracing/events
2269 */
2270
2271int is_valid_tracepoint(const char *event_string)
2272{
2273 DIR *sys_dir, *evt_dir;
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002274 struct dirent *sys_dirent, *evt_dirent;
Thomas Renninger20c457b2011-01-03 17:50:45 +01002275 char evt_path[MAXPATHLEN];
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002276 char *dir_path;
Thomas Renninger20c457b2011-01-03 17:50:45 +01002277
Arnaldo Carvalho de Melo7014e0e2018-05-17 14:42:39 -03002278 sys_dir = tracing_events__opendir();
Thomas Renninger20c457b2011-01-03 17:50:45 +01002279 if (!sys_dir)
2280 return 0;
2281
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002282 for_each_subsystem(sys_dir, sys_dirent) {
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002283 dir_path = get_events_file(sys_dirent->d_name);
2284 if (!dir_path)
2285 continue;
Thomas Renninger20c457b2011-01-03 17:50:45 +01002286 evt_dir = opendir(dir_path);
2287 if (!evt_dir)
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002288 goto next;
Thomas Renninger20c457b2011-01-03 17:50:45 +01002289
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -03002290 for_each_event(dir_path, evt_dir, evt_dirent) {
Thomas Renninger20c457b2011-01-03 17:50:45 +01002291 snprintf(evt_path, MAXPATHLEN, "%s:%s",
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002292 sys_dirent->d_name, evt_dirent->d_name);
Thomas Renninger20c457b2011-01-03 17:50:45 +01002293 if (!strcmp(evt_path, event_string)) {
2294 closedir(evt_dir);
2295 closedir(sys_dir);
2296 return 1;
2297 }
2298 }
2299 closedir(evt_dir);
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002300next:
2301 put_events_file(dir_path);
Thomas Renninger20c457b2011-01-03 17:50:45 +01002302 }
2303 closedir(sys_dir);
2304 return 0;
2305}
2306
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002307static bool is_event_supported(u8 type, unsigned config)
2308{
2309 bool ret = true;
Vince Weaver88fee522013-12-30 15:39:45 -05002310 int open_return;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002311 struct evsel *evsel;
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002312 struct perf_event_attr attr = {
2313 .type = type,
2314 .config = config,
2315 .disabled = 1,
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002316 };
Jiri Olsa9749b902019-07-21 13:23:50 +02002317 struct perf_thread_map *tmap = thread_map__new_by_tid(0);
Arnaldo Carvalho de Melo89896052017-02-14 10:59:04 -03002318
2319 if (tmap == NULL)
2320 return false;
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002321
Jiri Olsa365c3ae2019-07-21 13:23:58 +02002322 evsel = evsel__new(&attr);
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002323 if (evsel) {
Jiri Olsa5972d1e2019-07-21 13:24:01 +02002324 open_return = evsel__open(evsel, NULL, tmap);
Vince Weaver88fee522013-12-30 15:39:45 -05002325 ret = open_return >= 0;
2326
2327 if (open_return == -EACCES) {
2328 /*
2329 * This happens if the paranoid value
2330 * /proc/sys/kernel/perf_event_paranoid is set to 2
2331 * Re-run with exclude_kernel set; we don't do that
2332 * by default as some ARM machines do not support it.
2333 *
2334 */
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002335 evsel->core.attr.exclude_kernel = 1;
Jiri Olsa5972d1e2019-07-21 13:24:01 +02002336 ret = evsel__open(evsel, NULL, tmap) >= 0;
Vince Weaver88fee522013-12-30 15:39:45 -05002337 }
Jiri Olsa5eb2dd22019-07-21 13:23:57 +02002338 evsel__delete(evsel);
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002339 }
2340
Jiri Olsa7836e522019-07-21 13:24:20 +02002341 perf_thread_map__put(tmap);
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002342 return ret;
2343}
2344
Masami Hiramatsu40218da2016-06-24 18:06:46 +09002345void print_sdt_events(const char *subsys_glob, const char *event_glob,
2346 bool name_only)
2347{
2348 struct probe_cache *pcache;
2349 struct probe_cache_entry *ent;
2350 struct strlist *bidlist, *sdtlist;
2351 struct strlist_config cfg = {.dont_dupstr = true};
2352 struct str_node *nd, *nd2;
2353 char *buf, *path, *ptr = NULL;
2354 bool show_detail = false;
2355 int ret;
2356
2357 sdtlist = strlist__new(NULL, &cfg);
2358 if (!sdtlist) {
2359 pr_debug("Failed to allocate new strlist for SDT\n");
2360 return;
2361 }
2362 bidlist = build_id_cache__list_all(true);
2363 if (!bidlist) {
2364 pr_debug("Failed to get buildids: %d\n", errno);
2365 return;
2366 }
2367 strlist__for_each_entry(nd, bidlist) {
Krister Johansenf045b8c2017-07-05 18:48:11 -07002368 pcache = probe_cache__new(nd->s, NULL);
Masami Hiramatsu40218da2016-06-24 18:06:46 +09002369 if (!pcache)
2370 continue;
2371 list_for_each_entry(ent, &pcache->entries, node) {
2372 if (!ent->sdt)
2373 continue;
2374 if (subsys_glob &&
2375 !strglobmatch(ent->pev.group, subsys_glob))
2376 continue;
2377 if (event_glob &&
2378 !strglobmatch(ent->pev.event, event_glob))
2379 continue;
2380 ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
2381 ent->pev.event, nd->s);
2382 if (ret > 0)
2383 strlist__add(sdtlist, buf);
2384 }
2385 probe_cache__delete(pcache);
2386 }
2387 strlist__delete(bidlist);
2388
2389 strlist__for_each_entry(nd, sdtlist) {
2390 buf = strchr(nd->s, '@');
2391 if (buf)
2392 *(buf++) = '\0';
2393 if (name_only) {
2394 printf("%s ", nd->s);
2395 continue;
2396 }
2397 nd2 = strlist__next(nd);
2398 if (nd2) {
2399 ptr = strchr(nd2->s, '@');
2400 if (ptr)
2401 *ptr = '\0';
2402 if (strcmp(nd->s, nd2->s) == 0)
2403 show_detail = true;
2404 }
2405 if (show_detail) {
2406 path = build_id_cache__origname(buf);
2407 ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf);
2408 if (ret > 0) {
2409 printf(" %-50s [%s]\n", buf, "SDT event");
2410 free(buf);
2411 }
Changbin Du8bde8512019-03-16 16:05:46 +08002412 free(path);
Masami Hiramatsu40218da2016-06-24 18:06:46 +09002413 } else
2414 printf(" %-50s [%s]\n", nd->s, "SDT event");
2415 if (nd2) {
2416 if (strcmp(nd->s, nd2->s) != 0)
2417 show_detail = false;
2418 if (ptr)
2419 *ptr = '@';
2420 }
2421 }
2422 strlist__delete(sdtlist);
2423}
2424
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002425int print_hwcache_events(const char *event_glob, bool name_only)
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002426{
Yunlong Songab0e4802015-02-27 18:21:25 +08002427 unsigned int type, op, i, evt_i = 0, evt_num = 0;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03002428 char name[64];
Yunlong Songab0e4802015-02-27 18:21:25 +08002429 char **evt_list = NULL;
2430 bool evt_num_known = false;
2431
2432restart:
2433 if (evt_num_known) {
2434 evt_list = zalloc(sizeof(char *) * evt_num);
2435 if (!evt_list)
2436 goto out_enomem;
2437 }
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002438
2439 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
2440 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
2441 /* skip invalid cache type */
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03002442 if (!perf_evsel__is_cache_op_valid(type, op))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002443 continue;
2444
2445 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03002446 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
2447 name, sizeof(name));
Ingo Molnar947b4ad2011-04-29 22:52:42 +02002448 if (event_glob != NULL && !strglobmatch(name, event_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002449 continue;
2450
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002451 if (!is_event_supported(PERF_TYPE_HW_CACHE,
2452 type | (op << 8) | (i << 16)))
2453 continue;
2454
Yunlong Songab0e4802015-02-27 18:21:25 +08002455 if (!evt_num_known) {
2456 evt_num++;
2457 continue;
2458 }
2459
2460 evt_list[evt_i] = strdup(name);
2461 if (evt_list[evt_i] == NULL)
2462 goto out_enomem;
2463 evt_i++;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002464 }
2465 }
2466 }
2467
Yunlong Songab0e4802015-02-27 18:21:25 +08002468 if (!evt_num_known) {
2469 evt_num_known = true;
2470 goto restart;
2471 }
2472 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2473 evt_i = 0;
2474 while (evt_i < evt_num) {
2475 if (name_only) {
2476 printf("%s ", evt_list[evt_i++]);
2477 continue;
2478 }
2479 printf(" %-50s [%s]\n", evt_list[evt_i++],
2480 event_type_descriptors[PERF_TYPE_HW_CACHE]);
2481 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002482 if (evt_num && pager_in_use())
Andi Kleendc098b32013-04-20 11:02:29 -07002483 printf("\n");
Yunlong Songab0e4802015-02-27 18:21:25 +08002484
2485out_free:
2486 evt_num = evt_i;
2487 for (evt_i = 0; evt_i < evt_num; evt_i++)
2488 zfree(&evt_list[evt_i]);
2489 zfree(&evt_list);
2490 return evt_num;
2491
2492out_enomem:
2493 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
2494 if (evt_list)
2495 goto out_free;
2496 return evt_num;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002497}
2498
Andi Kleen5e0861b2019-03-26 15:18:23 -07002499static void print_tool_event(const char *name, const char *event_glob,
2500 bool name_only)
2501{
2502 if (event_glob && !strglobmatch(name, event_glob))
2503 return;
2504 if (name_only)
2505 printf("%s ", name);
2506 else
2507 printf(" %-50s [%s]\n", name, "Tool event");
2508
2509}
2510
2511void print_tool_events(const char *event_glob, bool name_only)
2512{
2513 print_tool_event("duration_time", event_glob, name_only);
2514 if (pager_in_use())
2515 printf("\n");
2516}
2517
Yunlong Song705750f2015-02-27 18:21:27 +08002518void print_symbol_events(const char *event_glob, unsigned type,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002519 struct event_symbol *syms, unsigned max,
2520 bool name_only)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002521{
Yunlong Songab0e4802015-02-27 18:21:25 +08002522 unsigned int i, evt_i = 0, evt_num = 0;
Ingo Molnar947b4ad2011-04-29 22:52:42 +02002523 char name[MAX_NAME_LEN];
Yunlong Songab0e4802015-02-27 18:21:25 +08002524 char **evt_list = NULL;
2525 bool evt_num_known = false;
2526
2527restart:
2528 if (evt_num_known) {
2529 evt_list = zalloc(sizeof(char *) * evt_num);
2530 if (!evt_list)
2531 goto out_enomem;
2532 syms -= max;
2533 }
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002534
Jiri Olsa1dc127602012-07-04 00:00:44 +02002535 for (i = 0; i < max; i++, syms++) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002536
Arnaldo Carvalho de Meloe37df6c2015-11-27 16:04:58 -03002537 if (event_glob != NULL && syms->symbol != NULL &&
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002538 !(strglobmatch(syms->symbol, event_glob) ||
2539 (syms->alias && strglobmatch(syms->alias, event_glob))))
2540 continue;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002541
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002542 if (!is_event_supported(type, i))
2543 continue;
2544
Yunlong Songab0e4802015-02-27 18:21:25 +08002545 if (!evt_num_known) {
2546 evt_num++;
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002547 continue;
2548 }
2549
Yunlong Songab0e4802015-02-27 18:21:25 +08002550 if (!name_only && strlen(syms->alias))
Ingo Molnar947b4ad2011-04-29 22:52:42 +02002551 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05302552 else
Arnaldo Carvalho de Melobd8d57f2018-12-06 13:52:13 -03002553 strlcpy(name, syms->symbol, MAX_NAME_LEN);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002554
Yunlong Songab0e4802015-02-27 18:21:25 +08002555 evt_list[evt_i] = strdup(name);
2556 if (evt_list[evt_i] == NULL)
2557 goto out_enomem;
2558 evt_i++;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002559 }
2560
Yunlong Songab0e4802015-02-27 18:21:25 +08002561 if (!evt_num_known) {
2562 evt_num_known = true;
2563 goto restart;
2564 }
2565 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2566 evt_i = 0;
2567 while (evt_i < evt_num) {
2568 if (name_only) {
2569 printf("%s ", evt_list[evt_i++]);
2570 continue;
2571 }
2572 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
2573 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002574 if (evt_num && pager_in_use())
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002575 printf("\n");
Yunlong Songab0e4802015-02-27 18:21:25 +08002576
2577out_free:
2578 evt_num = evt_i;
2579 for (evt_i = 0; evt_i < evt_num; evt_i++)
2580 zfree(&evt_list[evt_i]);
2581 zfree(&evt_list);
2582 return;
2583
2584out_enomem:
2585 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
2586 if (evt_list)
2587 goto out_free;
Jiri Olsa1dc127602012-07-04 00:00:44 +02002588}
2589
2590/*
2591 * Print the help text for the event symbols:
2592 */
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -07002593void print_events(const char *event_glob, bool name_only, bool quiet_flag,
Andi Kleenbf874fc2017-03-20 13:17:11 -07002594 bool long_desc, bool details_flag)
Jiri Olsa1dc127602012-07-04 00:00:44 +02002595{
Jiri Olsa1dc127602012-07-04 00:00:44 +02002596 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002597 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
Jiri Olsa1dc127602012-07-04 00:00:44 +02002598
2599 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002600 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
Andi Kleen5e0861b2019-03-26 15:18:23 -07002601 print_tool_events(event_glob, name_only);
Jiri Olsa1dc127602012-07-04 00:00:44 +02002602
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002603 print_hwcache_events(event_glob, name_only);
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002604
Andi Kleenbf874fc2017-03-20 13:17:11 -07002605 print_pmu_events(event_glob, name_only, quiet_flag, long_desc,
2606 details_flag);
Andi Kleendc098b32013-04-20 11:02:29 -07002607
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002608 if (event_glob != NULL)
2609 return;
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +05302610
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002611 if (!name_only) {
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002612 printf(" %-50s [%s]\n",
2613 "rNNN",
2614 event_type_descriptors[PERF_TYPE_RAW]);
2615 printf(" %-50s [%s]\n",
2616 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
2617 event_type_descriptors[PERF_TYPE_RAW]);
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002618 if (pager_in_use())
2619 printf(" (see 'man perf-list' on how to encode it)\n\n");
Thomas Gleixner86847b62009-06-06 12:24:17 +02002620
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002621 printf(" %-50s [%s]\n",
Jacob Shin3741eb92014-05-29 17:26:51 +02002622 "mem:<addr>[/len][:access]",
Liming Wang41bdcb22009-12-29 16:37:07 +08002623 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002624 if (pager_in_use())
2625 printf("\n");
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002626 }
Frederic Weisbecker1b290d62009-11-23 15:42:35 +01002627
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002628 print_tracepoint_events(NULL, NULL, name_only);
Masami Hiramatsu40218da2016-06-24 18:06:46 +09002629
2630 print_sdt_events(NULL, NULL, name_only);
Andi Kleen71b0acc2017-08-31 12:40:32 -07002631
Jiri Olsa33bbc572019-02-13 13:32:41 +01002632 metricgroup__print(true, true, NULL, name_only, details_flag);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002633}
Jiri Olsa8f707d82012-03-15 20:09:16 +01002634
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002635int parse_events__is_hardcoded_term(struct parse_events_term *term)
Jiri Olsa8f707d82012-03-15 20:09:16 +01002636{
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002637 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002638}
2639
Jiri Olsa67b49b32017-02-17 15:00:55 +01002640static int new_term(struct parse_events_term **_term,
2641 struct parse_events_term *temp,
2642 char *str, u64 num)
Jiri Olsa8f707d82012-03-15 20:09:16 +01002643{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002644 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002645
Jiri Olsa67b49b32017-02-17 15:00:55 +01002646 term = malloc(sizeof(*term));
Jiri Olsa8f707d82012-03-15 20:09:16 +01002647 if (!term)
2648 return -ENOMEM;
2649
Jiri Olsa67b49b32017-02-17 15:00:55 +01002650 *term = *temp;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002651 INIT_LIST_HEAD(&term->list);
Andi Kleenc2f1cea2017-10-20 13:27:55 -07002652 term->weak = false;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002653
Jiri Olsa67b49b32017-02-17 15:00:55 +01002654 switch (term->type_val) {
Jiri Olsa8f707d82012-03-15 20:09:16 +01002655 case PARSE_EVENTS__TERM_TYPE_NUM:
2656 term->val.num = num;
2657 break;
2658 case PARSE_EVENTS__TERM_TYPE_STR:
2659 term->val.str = str;
2660 break;
2661 default:
Adrian Hunter4be8be62013-07-04 16:20:24 +03002662 free(term);
Jiri Olsa8f707d82012-03-15 20:09:16 +01002663 return -EINVAL;
2664 }
2665
2666 *_term = term;
2667 return 0;
2668}
2669
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002670int parse_events_term__num(struct parse_events_term **term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002671 int type_term, char *config, u64 num,
Jiri Olsa99e71382017-02-17 15:00:56 +01002672 bool no_value,
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002673 void *loc_term_, void *loc_val_)
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002674{
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002675 YYLTYPE *loc_term = loc_term_;
2676 YYLTYPE *loc_val = loc_val_;
2677
Jiri Olsa67b49b32017-02-17 15:00:55 +01002678 struct parse_events_term temp = {
2679 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
2680 .type_term = type_term,
2681 .config = config,
Jiri Olsa99e71382017-02-17 15:00:56 +01002682 .no_value = no_value,
Jiri Olsa67b49b32017-02-17 15:00:55 +01002683 .err_term = loc_term ? loc_term->first_column : 0,
2684 .err_val = loc_val ? loc_val->first_column : 0,
2685 };
2686
2687 return new_term(term, &temp, NULL, num);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002688}
2689
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002690int parse_events_term__str(struct parse_events_term **term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002691 int type_term, char *config, char *str,
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002692 void *loc_term_, void *loc_val_)
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002693{
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002694 YYLTYPE *loc_term = loc_term_;
2695 YYLTYPE *loc_val = loc_val_;
2696
Jiri Olsa67b49b32017-02-17 15:00:55 +01002697 struct parse_events_term temp = {
2698 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
2699 .type_term = type_term,
2700 .config = config,
2701 .err_term = loc_term ? loc_term->first_column : 0,
2702 .err_val = loc_val ? loc_val->first_column : 0,
2703 };
2704
2705 return new_term(term, &temp, str, 0);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002706}
2707
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002708int parse_events_term__sym_hw(struct parse_events_term **term,
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02002709 char *config, unsigned idx)
2710{
2711 struct event_symbol *sym;
Jiri Olsa67b49b32017-02-17 15:00:55 +01002712 struct parse_events_term temp = {
2713 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
2714 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
2715 .config = config ?: (char *) "event",
2716 };
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02002717
2718 BUG_ON(idx >= PERF_COUNT_HW_MAX);
2719 sym = &event_symbols_hw[idx];
2720
Jiri Olsa67b49b32017-02-17 15:00:55 +01002721 return new_term(term, &temp, (char *) sym->symbol, 0);
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02002722}
2723
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002724int parse_events_term__clone(struct parse_events_term **new,
2725 struct parse_events_term *term)
Zheng Yana6146d52012-06-15 14:31:41 +08002726{
Jiri Olsa67b49b32017-02-17 15:00:55 +01002727 struct parse_events_term temp = {
2728 .type_val = term->type_val,
2729 .type_term = term->type_term,
2730 .config = term->config,
2731 .err_term = term->err_term,
2732 .err_val = term->err_val,
2733 };
2734
2735 return new_term(new, &temp, term->val.str, term->val.num);
Zheng Yana6146d52012-06-15 14:31:41 +08002736}
2737
Andi Kleen82557182017-03-20 13:17:03 -07002738int parse_events_copy_term_list(struct list_head *old,
2739 struct list_head **new)
2740{
2741 struct parse_events_term *term, *n;
2742 int ret;
2743
2744 if (!old) {
2745 *new = NULL;
2746 return 0;
2747 }
2748
2749 *new = malloc(sizeof(struct list_head));
2750 if (!*new)
2751 return -ENOMEM;
2752 INIT_LIST_HEAD(*new);
2753
2754 list_for_each_entry (term, old, list) {
2755 ret = parse_events_term__clone(&n, term);
2756 if (ret)
2757 return ret;
2758 list_add_tail(&n->list, *new);
2759 }
2760 return 0;
2761}
2762
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002763void parse_events_terms__purge(struct list_head *terms)
Jiri Olsa8f707d82012-03-15 20:09:16 +01002764{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002765 struct parse_events_term *term, *h;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002766
Wang Nana8adfce2016-02-12 16:31:23 -03002767 list_for_each_entry_safe(term, h, terms, list) {
Wang Nan2d055bf2016-02-22 09:10:34 +00002768 if (term->array.nr_ranges)
Taeung Song360e0712017-02-01 21:34:07 +09002769 zfree(&term->array.ranges);
Wang Nana8adfce2016-02-12 16:31:23 -03002770 list_del_init(&term->list);
Jiri Olsa8f707d82012-03-15 20:09:16 +01002771 free(term);
Wang Nana8adfce2016-02-12 16:31:23 -03002772 }
Jiri Olsa8f707d82012-03-15 20:09:16 +01002773}
Jiri Olsab39b8392015-04-22 21:10:16 +02002774
Arnaldo Carvalho de Melo2146afc2016-02-12 17:09:17 -03002775void parse_events_terms__delete(struct list_head *terms)
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002776{
Arnaldo Carvalho de Melo2146afc2016-02-12 17:09:17 -03002777 if (!terms)
2778 return;
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002779 parse_events_terms__purge(terms);
Wang Nand20a5f22016-02-12 17:01:17 -03002780 free(terms);
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002781}
2782
Wang Nan2d055bf2016-02-22 09:10:34 +00002783void parse_events__clear_array(struct parse_events_array *a)
2784{
Taeung Song360e0712017-02-01 21:34:07 +09002785 zfree(&a->ranges);
Wang Nan2d055bf2016-02-22 09:10:34 +00002786}
2787
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03002788void parse_events_evlist_error(struct parse_events_state *parse_state,
Jiri Olsab39b8392015-04-22 21:10:16 +02002789 int idx, const char *str)
2790{
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03002791 struct parse_events_error *err = parse_state->error;
Jiri Olsab39b8392015-04-22 21:10:16 +02002792
Adrian Huntera6ced2b2015-05-19 16:05:44 +03002793 if (!err)
2794 return;
Jiri Olsab39b8392015-04-22 21:10:16 +02002795 err->idx = idx;
2796 err->str = strdup(str);
2797 WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
2798}
He Kuangffeb8832015-09-28 03:52:14 +00002799
Wang Nan17cb5f82016-02-19 11:43:57 +00002800static void config_terms_list(char *buf, size_t buf_sz)
2801{
2802 int i;
2803 bool first = true;
2804
2805 buf[0] = '\0';
2806 for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
2807 const char *name = config_term_names[i];
2808
Wang Nan1669e502016-02-19 11:43:58 +00002809 if (!config_term_avail(i, NULL))
2810 continue;
Wang Nan17cb5f82016-02-19 11:43:57 +00002811 if (!name)
2812 continue;
2813 if (name[0] == '<')
2814 continue;
2815
2816 if (strlen(buf) + strlen(name) + 2 >= buf_sz)
2817 return;
2818
2819 if (!first)
2820 strcat(buf, ",");
2821 else
2822 first = false;
2823 strcat(buf, name);
2824 }
2825}
2826
He Kuangffeb8832015-09-28 03:52:14 +00002827/*
2828 * Return string contains valid config terms of an event.
2829 * @additional_terms: For terms such as PMU sysfs terms.
2830 */
2831char *parse_events_formats_error_string(char *additional_terms)
2832{
2833 char *str;
Wang Nan626a6b72016-07-14 08:34:45 +00002834 /* "no-overwrite" is the longest name */
Wang Nan17cb5f82016-02-19 11:43:57 +00002835 char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
Wang Nan626a6b72016-07-14 08:34:45 +00002836 (sizeof("no-overwrite") - 1)];
He Kuangffeb8832015-09-28 03:52:14 +00002837
Wang Nan17cb5f82016-02-19 11:43:57 +00002838 config_terms_list(static_terms, sizeof(static_terms));
He Kuangffeb8832015-09-28 03:52:14 +00002839 /* valid terms */
2840 if (additional_terms) {
Wang Nan26dee022016-02-19 11:43:52 +00002841 if (asprintf(&str, "valid terms: %s,%s",
2842 additional_terms, static_terms) < 0)
He Kuangffeb8832015-09-28 03:52:14 +00002843 goto fail;
2844 } else {
Wang Nan26dee022016-02-19 11:43:52 +00002845 if (asprintf(&str, "valid terms: %s", static_terms) < 0)
He Kuangffeb8832015-09-28 03:52:14 +00002846 goto fail;
2847 }
2848 return str;
2849
2850fail:
2851 return NULL;
2852}