blob: 75588920fccc372a68e0409ddf2f3ca6390ddaea [file] [log] [blame]
David Howellsd2709c72012-11-19 22:21:03 +00001#include <linux/hw_breakpoint.h>
Jiri Olsa8dd2a132015-09-07 10:38:06 +02002#include <linux/err.h>
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -03003#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03004#include <errno.h>
Arnaldo Carvalho de Melo86a5e0c2017-04-19 19:03:14 -03005#include <sys/ioctl.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -03006#include <sys/param.h>
Arnaldo Carvalho de Melob0742e92017-04-18 11:08:10 -03007#include "term.h"
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03008#include "../perf.h"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02009#include "evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020010#include "evsel.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060011#include <subcmd/parse-options.h>
Ingo Molnar8ad8db32009-05-26 11:10:09 +020012#include "parse-events.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060013#include <subcmd/exec-cmd.h>
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030014#include "string2.h"
Arnaldo Carvalho de Melo8ec20b12017-04-18 10:57:25 -030015#include "strlist.h"
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -030016#include "symbol.h"
Jason Baron5beeded2009-07-21 14:16:29 -040017#include "cache.h"
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020018#include "header.h"
Wang Nan84c86ca2015-10-14 12:41:14 +000019#include "bpf-loader.h"
Masami Hiramatsu6e81c742014-08-14 02:22:36 +000020#include "debug.h"
Jiri Olsa592d5a62015-09-02 09:56:34 +020021#include <api/fs/tracing_path.h>
Zheng Yanac20de6f2012-06-15 14:31:39 +080022#include "parse-events-bison.h"
Jiri Olsa90e2b222012-06-15 14:31:40 +080023#define YY_EXTRA_TYPE int
Jiri Olsa89812fc2012-03-15 20:09:15 +010024#include "parse-events-flex.h"
Jiri Olsa5f537a22012-03-15 20:09:18 +010025#include "pmu.h"
Namhyung Kimb41f1ce2013-08-27 11:41:53 +090026#include "thread_map.h"
Jiri Olsaf30a79b2015-06-23 00:36:04 +020027#include "cpumap.h"
Masami Hiramatsu40218da2016-06-24 18:06:46 +090028#include "probe-file.h"
Jiri Olsab39b8392015-04-22 21:10:16 +020029#include "asm/bug.h"
Andi Kleenac12f672016-10-12 14:02:06 -070030#include "util/parse-branch-options.h"
Andi Kleen71b0acc2017-08-31 12:40:32 -070031#include "metricgroup.h"
Jiri Olsa89812fc2012-03-15 20:09:15 +010032
33#define MAX_NAME_LEN 100
Ingo Molnar8ad8db32009-05-26 11:10:09 +020034
Jiri Olsa82ba1f22012-05-21 09:12:50 +020035#ifdef PARSER_DEBUG
36extern int parse_events_debug;
37#endif
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -030038int parse_events_parse(void *parse_state, void *scanner);
He Kuange637d172015-09-28 03:52:16 +000039static int get_config_terms(struct list_head *head_config,
40 struct list_head *head_terms __maybe_unused);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +020041
Kan Liangdcb4e102014-10-07 11:08:50 -040042static struct perf_pmu_event_symbol *perf_pmu_events_list;
43/*
44 * The variable indicates the number of supported pmu event symbols.
45 * 0 means not initialized and ready to init
46 * -1 means failed to init, don't try anymore
47 * >0 is the number of supported pmu event symbols
48 */
49static int perf_pmu_events_list_num;
50
Yunlong Song705750f2015-02-27 18:21:27 +080051struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
Jiri Olsa1dc127602012-07-04 00:00:44 +020052 [PERF_COUNT_HW_CPU_CYCLES] = {
53 .symbol = "cpu-cycles",
54 .alias = "cycles",
55 },
56 [PERF_COUNT_HW_INSTRUCTIONS] = {
57 .symbol = "instructions",
58 .alias = "",
59 },
60 [PERF_COUNT_HW_CACHE_REFERENCES] = {
61 .symbol = "cache-references",
62 .alias = "",
63 },
64 [PERF_COUNT_HW_CACHE_MISSES] = {
65 .symbol = "cache-misses",
66 .alias = "",
67 },
68 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
69 .symbol = "branch-instructions",
70 .alias = "branches",
71 },
72 [PERF_COUNT_HW_BRANCH_MISSES] = {
73 .symbol = "branch-misses",
74 .alias = "",
75 },
76 [PERF_COUNT_HW_BUS_CYCLES] = {
77 .symbol = "bus-cycles",
78 .alias = "",
79 },
80 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
81 .symbol = "stalled-cycles-frontend",
82 .alias = "idle-cycles-frontend",
83 },
84 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
85 .symbol = "stalled-cycles-backend",
86 .alias = "idle-cycles-backend",
87 },
88 [PERF_COUNT_HW_REF_CPU_CYCLES] = {
89 .symbol = "ref-cycles",
90 .alias = "",
91 },
92};
Ingo Molnar8ad8db32009-05-26 11:10:09 +020093
Yunlong Song705750f2015-02-27 18:21:27 +080094struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
Jiri Olsa1dc127602012-07-04 00:00:44 +020095 [PERF_COUNT_SW_CPU_CLOCK] = {
96 .symbol = "cpu-clock",
97 .alias = "",
98 },
99 [PERF_COUNT_SW_TASK_CLOCK] = {
100 .symbol = "task-clock",
101 .alias = "",
102 },
103 [PERF_COUNT_SW_PAGE_FAULTS] = {
104 .symbol = "page-faults",
105 .alias = "faults",
106 },
107 [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
108 .symbol = "context-switches",
109 .alias = "cs",
110 },
111 [PERF_COUNT_SW_CPU_MIGRATIONS] = {
112 .symbol = "cpu-migrations",
113 .alias = "migrations",
114 },
115 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
116 .symbol = "minor-faults",
117 .alias = "",
118 },
119 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
120 .symbol = "major-faults",
121 .alias = "",
122 },
123 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
124 .symbol = "alignment-faults",
125 .alias = "",
126 },
127 [PERF_COUNT_SW_EMULATION_FAULTS] = {
128 .symbol = "emulation-faults",
129 .alias = "",
130 },
Adrian Hunterd22d1a22013-08-31 21:50:52 +0300131 [PERF_COUNT_SW_DUMMY] = {
132 .symbol = "dummy",
133 .alias = "",
134 },
Arnaldo Carvalho de Melobae9cc42015-11-27 15:54:33 -0300135 [PERF_COUNT_SW_BPF_OUTPUT] = {
136 .symbol = "bpf-output",
137 .alias = "",
138 },
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200139};
140
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200141#define __PERF_EVENT_FIELD(config, name) \
142 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
Ingo Molnar52425192009-05-26 09:17:18 +0200143
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200144#define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200145#define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200146#define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200147#define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
Ingo Molnar52425192009-05-26 09:17:18 +0200148
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300149#define for_each_subsystem(sys_dir, sys_dirent) \
150 while ((sys_dirent = readdir(sys_dir)) != NULL) \
151 if (sys_dirent->d_type == DT_DIR && \
152 (strcmp(sys_dirent->d_name, ".")) && \
153 (strcmp(sys_dirent->d_name, "..")))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400154
Peter Zijlstraae07b632009-08-06 16:48:54 +0200155static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
156{
157 char evt_path[MAXPATHLEN];
158 int fd;
159
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200160 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
Peter Zijlstraae07b632009-08-06 16:48:54 +0200161 sys_dir->d_name, evt_dir->d_name);
162 fd = open(evt_path, O_RDONLY);
163 if (fd < 0)
164 return -EINVAL;
165 close(fd);
166
167 return 0;
168}
169
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300170#define for_each_event(sys_dirent, evt_dir, evt_dirent) \
171 while ((evt_dirent = readdir(evt_dir)) != NULL) \
172 if (evt_dirent->d_type == DT_DIR && \
173 (strcmp(evt_dirent->d_name, ".")) && \
174 (strcmp(evt_dirent->d_name, "..")) && \
175 (!tp_event_has_id(sys_dirent, evt_dirent)))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400176
Li Zefan270bbbe2009-09-17 16:34:51 +0800177#define MAX_EVENT_LENGTH 512
Jason Baronf6bdafe2009-07-21 12:20:22 -0400178
Jason Baronf6bdafe2009-07-21 12:20:22 -0400179
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200180struct tracepoint_path *tracepoint_id_to_path(u64 config)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400181{
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200182 struct tracepoint_path *path = NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400183 DIR *sys_dir, *evt_dir;
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300184 struct dirent *sys_dirent, *evt_dirent;
Stephane Eranian8aa8a7c2012-03-13 16:51:02 +0100185 char id_buf[24];
Eric Dumazet725b1362009-09-24 15:39:09 +0200186 int fd;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400187 u64 id;
188 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +0200189 char dir_path[MAXPATHLEN];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400190
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200191 sys_dir = opendir(tracing_events_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400192 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200193 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400194
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300195 for_each_subsystem(sys_dir, sys_dirent) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200196
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200197 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300198 sys_dirent->d_name);
Eric Dumazet725b1362009-09-24 15:39:09 +0200199 evt_dir = opendir(dir_path);
200 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300201 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200202
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300203 for_each_event(sys_dirent, evt_dir, evt_dirent) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200204
205 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300206 evt_dirent->d_name);
Eric Dumazet725b1362009-09-24 15:39:09 +0200207 fd = open(evt_path, O_RDONLY);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400208 if (fd < 0)
209 continue;
210 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
211 close(fd);
212 continue;
213 }
214 close(fd);
215 id = atoll(id_buf);
216 if (id == config) {
217 closedir(evt_dir);
218 closedir(sys_dir);
Julia Lawall59b4cae2009-12-06 10:16:30 +0100219 path = zalloc(sizeof(*path));
Taeung Song5aa365f2017-02-01 21:34:05 +0900220 if (!path)
221 return NULL;
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200222 path->system = malloc(MAX_EVENT_LENGTH);
223 if (!path->system) {
224 free(path);
225 return NULL;
226 }
227 path->name = malloc(MAX_EVENT_LENGTH);
228 if (!path->name) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300229 zfree(&path->system);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200230 free(path);
231 return NULL;
232 }
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300233 strncpy(path->system, sys_dirent->d_name,
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200234 MAX_EVENT_LENGTH);
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300235 strncpy(path->name, evt_dirent->d_name,
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200236 MAX_EVENT_LENGTH);
237 return path;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400238 }
239 }
240 closedir(evt_dir);
241 }
242
Jason Baronf6bdafe2009-07-21 12:20:22 -0400243 closedir(sys_dir);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200244 return NULL;
245}
246
Namhyung Kime7c93f02013-06-26 16:14:05 +0900247struct tracepoint_path *tracepoint_name_to_path(const char *name)
248{
249 struct tracepoint_path *path = zalloc(sizeof(*path));
250 char *str = strchr(name, ':');
251
252 if (path == NULL || str == NULL) {
253 free(path);
254 return NULL;
255 }
256
257 path->system = strndup(name, str - name);
258 path->name = strdup(str+1);
259
260 if (path->system == NULL || path->name == NULL) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300261 zfree(&path->system);
262 zfree(&path->name);
Taeung Song506fde12017-02-01 21:34:06 +0900263 zfree(&path);
Namhyung Kime7c93f02013-06-26 16:14:05 +0900264 }
265
266 return path;
267}
268
David Ahern1424dc92011-03-09 22:23:28 -0700269const char *event_type(int type)
270{
271 switch (type) {
272 case PERF_TYPE_HARDWARE:
273 return "hardware";
274
275 case PERF_TYPE_SOFTWARE:
276 return "software";
277
278 case PERF_TYPE_TRACEPOINT:
279 return "tracepoint";
280
281 case PERF_TYPE_HW_CACHE:
282 return "hardware-cache";
283
284 default:
285 break;
286 }
287
288 return "unknown";
289}
290
Wang Nane814fdd2016-02-19 11:43:59 +0000291static int parse_events__is_name_term(struct parse_events_term *term)
292{
293 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
294}
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800295
Wang Nane814fdd2016-02-19 11:43:59 +0000296static char *get_config_name(struct list_head *head_terms)
297{
298 struct parse_events_term *term;
299
300 if (!head_terms)
301 return NULL;
302
303 list_for_each_entry(term, head_terms, list)
304 if (parse_events__is_name_term(term))
305 return term->val.str;
306
307 return NULL;
308}
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800309
Stephane Eranian410136f2013-11-12 17:58:49 +0100310static struct perf_evsel *
311__add_event(struct list_head *list, int *idx,
312 struct perf_event_attr *attr,
Jiri Olsa930a2e22015-07-29 05:42:10 -0400313 char *name, struct cpu_map *cpus,
Arnaldo Carvalho de Melo63ce8442017-08-31 15:32:18 -0300314 struct list_head *config_terms, bool auto_merge_stats)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100315{
316 struct perf_evsel *evsel;
317
318 event_attr_init(attr);
319
Taeung Song75fc5ae2017-02-01 21:34:04 +0900320 evsel = perf_evsel__new_idx(attr, *idx);
David Ahernc5cd8ac2013-07-02 13:27:25 -0600321 if (!evsel)
Stephane Eranian410136f2013-11-12 17:58:49 +0100322 return NULL;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100323
Taeung Song75fc5ae2017-02-01 21:34:04 +0900324 (*idx)++;
Jiri Olsae3ba76d2017-02-27 10:48:18 +0100325 evsel->cpus = cpu_map__get(cpus);
326 evsel->own_cpus = cpu_map__get(cpus);
327 evsel->system_wide = !!cpus;
Arnaldo Carvalho de Melo63ce8442017-08-31 15:32:18 -0300328 evsel->auto_merge_stats = auto_merge_stats;
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200329
Arnaldo Carvalho de Melo9db17632012-06-12 13:45:00 -0300330 if (name)
331 evsel->name = strdup(name);
Jiri Olsa930a2e22015-07-29 05:42:10 -0400332
333 if (config_terms)
334 list_splice(config_terms, &evsel->config_terms);
335
Jiri Olsab847cbd2012-05-21 09:12:51 +0200336 list_add_tail(&evsel->node, list);
Stephane Eranian410136f2013-11-12 17:58:49 +0100337 return evsel;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100338}
339
David Ahernc5cd8ac2013-07-02 13:27:25 -0600340static int add_event(struct list_head *list, int *idx,
Jiri Olsa930a2e22015-07-29 05:42:10 -0400341 struct perf_event_attr *attr, char *name,
342 struct list_head *config_terms)
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800343{
Arnaldo Carvalho de Melo63ce8442017-08-31 15:32:18 -0300344 return __add_event(list, idx, attr, name, NULL, config_terms, false) ? 0 : -ENOMEM;
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800345}
346
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300347static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
Ingo Molnar8326f442009-06-05 20:22:46 +0200348{
349 int i, j;
Paul Mackerras61c45982009-07-01 13:04:34 +1000350 int n, longest = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200351
352 for (i = 0; i < size; i++) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300353 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
Paul Mackerras61c45982009-07-01 13:04:34 +1000354 n = strlen(names[i][j]);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100355 if (n > longest && !strncasecmp(str, names[i][j], n))
Paul Mackerras61c45982009-07-01 13:04:34 +1000356 longest = n;
357 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100358 if (longest > 0)
Paul Mackerras61c45982009-07-01 13:04:34 +1000359 return i;
Ingo Molnar8326f442009-06-05 20:22:46 +0200360 }
361
Ingo Molnar89536452009-06-06 21:04:17 +0200362 return -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200363}
364
Wang Nan43d0b972016-02-19 11:44:01 +0000365typedef int config_term_func_t(struct perf_event_attr *attr,
366 struct parse_events_term *term,
367 struct parse_events_error *err);
368static int config_term_common(struct perf_event_attr *attr,
369 struct parse_events_term *term,
370 struct parse_events_error *err);
371static int config_attr(struct perf_event_attr *attr,
372 struct list_head *head,
373 struct parse_events_error *err,
374 config_term_func_t config_term);
375
David Ahernc5cd8ac2013-07-02 13:27:25 -0600376int parse_events_add_cache(struct list_head *list, int *idx,
Wang Nan43d0b972016-02-19 11:44:01 +0000377 char *type, char *op_result1, char *op_result2,
Arnaldo Carvalho de Melo2c97b0d2016-02-19 19:47:04 -0300378 struct parse_events_error *err,
Wang Nan43d0b972016-02-19 11:44:01 +0000379 struct list_head *head_config)
Ingo Molnar8326f442009-06-05 20:22:46 +0200380{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100381 struct perf_event_attr attr;
Wang Nan43d0b972016-02-19 11:44:01 +0000382 LIST_HEAD(config_terms);
383 char name[MAX_NAME_LEN], *config_name;
Paul Mackerras61c45982009-07-01 13:04:34 +1000384 int cache_type = -1, cache_op = -1, cache_result = -1;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100385 char *op_result[2] = { op_result1, op_result2 };
386 int i, n;
Ingo Molnar8326f442009-06-05 20:22:46 +0200387
Ingo Molnar8326f442009-06-05 20:22:46 +0200388 /*
389 * No fallback - if we cannot get a clear cache type
390 * then bail out:
391 */
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300392 cache_type = parse_aliases(type, perf_evsel__hw_cache,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100393 PERF_COUNT_HW_CACHE_MAX);
Ingo Molnar8326f442009-06-05 20:22:46 +0200394 if (cache_type == -1)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100395 return -EINVAL;
Ingo Molnar8326f442009-06-05 20:22:46 +0200396
Wang Nan43d0b972016-02-19 11:44:01 +0000397 config_name = get_config_name(head_config);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100398 n = snprintf(name, MAX_NAME_LEN, "%s", type);
399
400 for (i = 0; (i < 2) && (op_result[i]); i++) {
401 char *str = op_result[i];
402
Jiri Olsa275ef382012-09-05 19:51:33 +0200403 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
Paul Mackerras61c45982009-07-01 13:04:34 +1000404
405 if (cache_op == -1) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300406 cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100407 PERF_COUNT_HW_CACHE_OP_MAX);
Paul Mackerras61c45982009-07-01 13:04:34 +1000408 if (cache_op >= 0) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300409 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
Jiri Olsa89812fc2012-03-15 20:09:15 +0100410 return -EINVAL;
Paul Mackerras61c45982009-07-01 13:04:34 +1000411 continue;
412 }
413 }
414
415 if (cache_result == -1) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300416 cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
417 PERF_COUNT_HW_CACHE_RESULT_MAX);
Paul Mackerras61c45982009-07-01 13:04:34 +1000418 if (cache_result >= 0)
419 continue;
420 }
Paul Mackerras61c45982009-07-01 13:04:34 +1000421 }
422
Ingo Molnar8326f442009-06-05 20:22:46 +0200423 /*
424 * Fall back to reads:
425 */
Ingo Molnar89536452009-06-06 21:04:17 +0200426 if (cache_op == -1)
427 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
Ingo Molnar8326f442009-06-05 20:22:46 +0200428
Ingo Molnar8326f442009-06-05 20:22:46 +0200429 /*
430 * Fall back to accesses:
431 */
432 if (cache_result == -1)
433 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
434
Jiri Olsa89812fc2012-03-15 20:09:15 +0100435 memset(&attr, 0, sizeof(attr));
436 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
437 attr.type = PERF_TYPE_HW_CACHE;
Wang Nan43d0b972016-02-19 11:44:01 +0000438
439 if (head_config) {
Arnaldo Carvalho de Melo2c97b0d2016-02-19 19:47:04 -0300440 if (config_attr(&attr, head_config, err,
Wang Nan43d0b972016-02-19 11:44:01 +0000441 config_term_common))
442 return -EINVAL;
443
444 if (get_config_terms(head_config, &config_terms))
445 return -ENOMEM;
446 }
447 return add_event(list, idx, &attr, config_name ? : name, &config_terms);
Ingo Molnar8326f442009-06-05 20:22:46 +0200448}
449
Jiri Olsa272ed292015-09-29 17:05:31 +0200450static void tracepoint_error(struct parse_events_error *e, int err,
Wang Nan8c619d62016-07-13 10:44:03 +0000451 const char *sys, const char *name)
Jiri Olsa19658172015-09-07 10:38:07 +0200452{
453 char help[BUFSIZ];
454
Adrian Hunterec183d22016-01-26 14:05:20 +0200455 if (!e)
456 return;
457
Jiri Olsa19658172015-09-07 10:38:07 +0200458 /*
459 * We get error directly from syscall errno ( > 0),
460 * or from encoded pointer's error ( < 0).
461 */
462 err = abs(err);
463
464 switch (err) {
465 case EACCES:
Jiri Olsa272ed292015-09-29 17:05:31 +0200466 e->str = strdup("can't access trace events");
Jiri Olsa19658172015-09-07 10:38:07 +0200467 break;
468 case ENOENT:
Jiri Olsa272ed292015-09-29 17:05:31 +0200469 e->str = strdup("unknown tracepoint");
Jiri Olsa19658172015-09-07 10:38:07 +0200470 break;
471 default:
Jiri Olsa272ed292015-09-29 17:05:31 +0200472 e->str = strdup("failed to add tracepoint");
Jiri Olsa19658172015-09-07 10:38:07 +0200473 break;
474 }
475
476 tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
Jiri Olsa272ed292015-09-29 17:05:31 +0200477 e->help = strdup(help);
Jiri Olsa19658172015-09-07 10:38:07 +0200478}
479
David Ahernc5cd8ac2013-07-02 13:27:25 -0600480static int add_tracepoint(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +0000481 const char *sys_name, const char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200482 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000483 struct list_head *head_config)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200484{
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300485 struct perf_evsel *evsel;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200486
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300487 evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
Jiri Olsa19658172015-09-07 10:38:07 +0200488 if (IS_ERR(evsel)) {
Jiri Olsa272ed292015-09-29 17:05:31 +0200489 tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200490 return PTR_ERR(evsel);
Jiri Olsa19658172015-09-07 10:38:07 +0200491 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200492
He Kuange637d172015-09-28 03:52:16 +0000493 if (head_config) {
494 LIST_HEAD(config_terms);
495
496 if (get_config_terms(head_config, &config_terms))
497 return -ENOMEM;
498 list_splice(&config_terms, &evsel->config_terms);
499 }
500
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300501 list_add_tail(&evsel->node, list);
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300502 return 0;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200503}
504
David Ahernc5cd8ac2013-07-02 13:27:25 -0600505static int add_tracepoint_multi_event(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +0000506 const char *sys_name, const char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200507 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000508 struct list_head *head_config)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200509{
510 char evt_path[MAXPATHLEN];
511 struct dirent *evt_ent;
512 DIR *evt_dir;
Jiri Olsa27bf90b2015-10-05 21:31:17 +0200513 int ret = 0, found = 0;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200514
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200515 snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200516 evt_dir = opendir(evt_path);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200517 if (!evt_dir) {
Jiri Olsa272ed292015-09-29 17:05:31 +0200518 tracepoint_error(err, errno, sys_name, evt_name);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100519 return -1;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200520 }
521
Jiri Olsa89812fc2012-03-15 20:09:15 +0100522 while (!ret && (evt_ent = readdir(evt_dir))) {
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200523 if (!strcmp(evt_ent->d_name, ".")
524 || !strcmp(evt_ent->d_name, "..")
525 || !strcmp(evt_ent->d_name, "enable")
526 || !strcmp(evt_ent->d_name, "filter"))
527 continue;
528
Jiri Olsa89812fc2012-03-15 20:09:15 +0100529 if (!strglobmatch(evt_ent->d_name, evt_name))
Masami Hiramatsufb1d2ed2010-01-05 17:47:17 -0500530 continue;
531
Jiri Olsa27bf90b2015-10-05 21:31:17 +0200532 found++;
533
He Kuange637d172015-09-28 03:52:16 +0000534 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200535 err, head_config);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200536 }
537
Jiri Olsa27bf90b2015-10-05 21:31:17 +0200538 if (!found) {
539 tracepoint_error(err, ENOENT, sys_name, evt_name);
540 ret = -1;
541 }
542
Jiri Olsa0bd3f082012-12-17 14:08:36 +0100543 closedir(evt_dir);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100544 return ret;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200545}
546
David Ahernc5cd8ac2013-07-02 13:27:25 -0600547static int add_tracepoint_event(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +0000548 const char *sys_name, const char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200549 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000550 struct list_head *head_config)
Jiri Olsaf35488f2012-12-17 14:08:37 +0100551{
552 return strpbrk(evt_name, "*?") ?
He Kuange637d172015-09-28 03:52:16 +0000553 add_tracepoint_multi_event(list, idx, sys_name, evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200554 err, head_config) :
He Kuange637d172015-09-28 03:52:16 +0000555 add_tracepoint(list, idx, sys_name, evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200556 err, head_config);
Jiri Olsaf35488f2012-12-17 14:08:37 +0100557}
558
David Ahernc5cd8ac2013-07-02 13:27:25 -0600559static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +0000560 const char *sys_name, const char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200561 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000562 struct list_head *head_config)
Jiri Olsaf35488f2012-12-17 14:08:37 +0100563{
564 struct dirent *events_ent;
565 DIR *events_dir;
566 int ret = 0;
567
568 events_dir = opendir(tracing_events_path);
569 if (!events_dir) {
Jiri Olsa272ed292015-09-29 17:05:31 +0200570 tracepoint_error(err, errno, sys_name, evt_name);
Jiri Olsaf35488f2012-12-17 14:08:37 +0100571 return -1;
572 }
573
574 while (!ret && (events_ent = readdir(events_dir))) {
575 if (!strcmp(events_ent->d_name, ".")
576 || !strcmp(events_ent->d_name, "..")
577 || !strcmp(events_ent->d_name, "enable")
578 || !strcmp(events_ent->d_name, "header_event")
579 || !strcmp(events_ent->d_name, "header_page"))
580 continue;
581
582 if (!strglobmatch(events_ent->d_name, sys_name))
583 continue;
584
585 ret = add_tracepoint_event(list, idx, events_ent->d_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200586 evt_name, err, head_config);
Jiri Olsaf35488f2012-12-17 14:08:37 +0100587 }
588
589 closedir(events_dir);
590 return ret;
591}
592
Wang Nan4edf30e2015-10-14 12:41:17 +0000593struct __add_bpf_event_param {
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300594 struct parse_events_state *parse_state;
Wang Nan4edf30e2015-10-14 12:41:17 +0000595 struct list_head *list;
Wang Nan95088a52016-02-22 09:10:36 +0000596 struct list_head *head_config;
Wang Nan4edf30e2015-10-14 12:41:17 +0000597};
598
Wang Nancd102d72016-07-13 10:44:04 +0000599static int add_bpf_event(const char *group, const char *event, int fd,
Wang Nan4edf30e2015-10-14 12:41:17 +0000600 void *_param)
601{
602 LIST_HEAD(new_evsels);
603 struct __add_bpf_event_param *param = _param;
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300604 struct parse_events_state *parse_state = param->parse_state;
Wang Nan4edf30e2015-10-14 12:41:17 +0000605 struct list_head *list = param->list;
Wang Nan1f45b1d2015-10-14 12:41:18 +0000606 struct perf_evsel *pos;
Wang Nan4edf30e2015-10-14 12:41:17 +0000607 int err;
608
609 pr_debug("add bpf event %s:%s and attach bpf program %d\n",
Wang Nancd102d72016-07-13 10:44:04 +0000610 group, event, fd);
Wang Nan4edf30e2015-10-14 12:41:17 +0000611
Arnaldo Carvalho de Melo5d369a72017-08-17 14:13:25 -0300612 err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group,
613 event, parse_state->error,
Wang Nan95088a52016-02-22 09:10:36 +0000614 param->head_config);
Wang Nan4edf30e2015-10-14 12:41:17 +0000615 if (err) {
616 struct perf_evsel *evsel, *tmp;
617
618 pr_debug("Failed to add BPF event %s:%s\n",
Wang Nancd102d72016-07-13 10:44:04 +0000619 group, event);
Wang Nan4edf30e2015-10-14 12:41:17 +0000620 list_for_each_entry_safe(evsel, tmp, &new_evsels, node) {
621 list_del(&evsel->node);
622 perf_evsel__delete(evsel);
623 }
624 return err;
625 }
Wang Nancd102d72016-07-13 10:44:04 +0000626 pr_debug("adding %s:%s\n", group, event);
Wang Nan4edf30e2015-10-14 12:41:17 +0000627
Wang Nan1f45b1d2015-10-14 12:41:18 +0000628 list_for_each_entry(pos, &new_evsels, node) {
629 pr_debug("adding %s:%s to %p\n",
Wang Nancd102d72016-07-13 10:44:04 +0000630 group, event, pos);
Wang Nan1f45b1d2015-10-14 12:41:18 +0000631 pos->bpf_fd = fd;
632 }
Wang Nan4edf30e2015-10-14 12:41:17 +0000633 list_splice(&new_evsels, list);
634 return 0;
635}
636
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300637int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
Wang Nan84c86ca2015-10-14 12:41:14 +0000638 struct list_head *list,
Wang Nan95088a52016-02-22 09:10:36 +0000639 struct bpf_object *obj,
640 struct list_head *head_config)
Wang Nan84c86ca2015-10-14 12:41:14 +0000641{
642 int err;
643 char errbuf[BUFSIZ];
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300644 struct __add_bpf_event_param param = {parse_state, list, head_config};
Wang Nanaa3abf32015-10-14 12:41:15 +0000645 static bool registered_unprobe_atexit = false;
Wang Nan84c86ca2015-10-14 12:41:14 +0000646
647 if (IS_ERR(obj) || !obj) {
648 snprintf(errbuf, sizeof(errbuf),
649 "Internal error: load bpf obj with NULL");
650 err = -EINVAL;
651 goto errout;
652 }
653
654 /*
Wang Nanaa3abf32015-10-14 12:41:15 +0000655 * Register atexit handler before calling bpf__probe() so
656 * bpf__probe() don't need to unprobe probe points its already
657 * created when failure.
658 */
659 if (!registered_unprobe_atexit) {
660 atexit(bpf__clear);
661 registered_unprobe_atexit = true;
662 }
663
664 err = bpf__probe(obj);
665 if (err) {
666 bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
667 goto errout;
668 }
669
Wang Nan1e5e3ee2015-10-14 12:41:16 +0000670 err = bpf__load(obj);
671 if (err) {
672 bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
673 goto errout;
674 }
675
Wang Nancd102d72016-07-13 10:44:04 +0000676 err = bpf__foreach_event(obj, add_bpf_event, &param);
Wang Nan4edf30e2015-10-14 12:41:17 +0000677 if (err) {
678 snprintf(errbuf, sizeof(errbuf),
679 "Attach events in BPF object failed");
680 goto errout;
681 }
682
683 return 0;
Wang Nan84c86ca2015-10-14 12:41:14 +0000684errout:
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300685 parse_state->error->help = strdup("(add -v to see detail)");
686 parse_state->error->str = strdup(errbuf);
Wang Nan84c86ca2015-10-14 12:41:14 +0000687 return err;
688}
689
Wang Nana34f3be2016-02-22 09:10:31 +0000690static int
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300691parse_events_config_bpf(struct parse_events_state *parse_state,
Wang Nana34f3be2016-02-22 09:10:31 +0000692 struct bpf_object *obj,
693 struct list_head *head_config)
694{
695 struct parse_events_term *term;
696 int error_pos;
697
698 if (!head_config || list_empty(head_config))
699 return 0;
700
701 list_for_each_entry(term, head_config, list) {
702 char errbuf[BUFSIZ];
703 int err;
704
705 if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) {
706 snprintf(errbuf, sizeof(errbuf),
707 "Invalid config term for BPF object");
708 errbuf[BUFSIZ - 1] = '\0';
709
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300710 parse_state->error->idx = term->err_term;
711 parse_state->error->str = strdup(errbuf);
Wang Nana34f3be2016-02-22 09:10:31 +0000712 return -EINVAL;
713 }
714
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300715 err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos);
Wang Nana34f3be2016-02-22 09:10:31 +0000716 if (err) {
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300717 bpf__strerror_config_obj(obj, term, parse_state->evlist,
Wang Nana34f3be2016-02-22 09:10:31 +0000718 &error_pos, err, errbuf,
719 sizeof(errbuf));
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300720 parse_state->error->help = strdup(
Wang Nan7630b3e2016-02-22 09:10:33 +0000721"Hint:\tValid config terms:\n"
Wang Nane571e022016-02-22 09:10:35 +0000722" \tmap:[<arraymap>].value<indices>=[value]\n"
723" \tmap:[<eventmap>].event<indices>=[event]\n"
Wang Nan7630b3e2016-02-22 09:10:33 +0000724"\n"
Wang Nane571e022016-02-22 09:10:35 +0000725" \twhere <indices> is something like [0,3...5] or [all]\n"
Wang Nana34f3be2016-02-22 09:10:31 +0000726" \t(add -v to see detail)");
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300727 parse_state->error->str = strdup(errbuf);
Wang Nana34f3be2016-02-22 09:10:31 +0000728 if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE)
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300729 parse_state->error->idx = term->err_val;
Wang Nana34f3be2016-02-22 09:10:31 +0000730 else
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300731 parse_state->error->idx = term->err_term + error_pos;
Wang Nana34f3be2016-02-22 09:10:31 +0000732 return err;
733 }
734 }
735 return 0;
736}
737
Wang Nan95088a52016-02-22 09:10:36 +0000738/*
739 * Split config terms:
740 * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
741 * 'call-graph=fp' is 'evt config', should be applied to each
742 * events in bpf.c.
743 * 'map:array.value[0]=1' is 'obj config', should be processed
744 * with parse_events_config_bpf.
745 *
746 * Move object config terms from the first list to obj_head_config.
747 */
748static void
749split_bpf_config_terms(struct list_head *evt_head_config,
750 struct list_head *obj_head_config)
751{
752 struct parse_events_term *term, *temp;
753
754 /*
755 * Currectly, all possible user config term
756 * belong to bpf object. parse_events__is_hardcoded_term()
757 * happends to be a good flag.
758 *
759 * See parse_events_config_bpf() and
760 * config_term_tracepoint().
761 */
762 list_for_each_entry_safe(term, temp, evt_head_config, list)
763 if (!parse_events__is_hardcoded_term(term))
764 list_move_tail(&term->list, obj_head_config);
765}
766
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300767int parse_events_load_bpf(struct parse_events_state *parse_state,
Wang Nan84c86ca2015-10-14 12:41:14 +0000768 struct list_head *list,
Wang Nand509db02015-10-14 12:41:20 +0000769 char *bpf_file_name,
Wang Nana34f3be2016-02-22 09:10:31 +0000770 bool source,
771 struct list_head *head_config)
Wang Nan84c86ca2015-10-14 12:41:14 +0000772{
Wang Nana34f3be2016-02-22 09:10:31 +0000773 int err;
Wang Nan95088a52016-02-22 09:10:36 +0000774 struct bpf_object *obj;
775 LIST_HEAD(obj_head_config);
776
777 if (head_config)
778 split_bpf_config_terms(head_config, &obj_head_config);
Wang Nan84c86ca2015-10-14 12:41:14 +0000779
Wang Nand509db02015-10-14 12:41:20 +0000780 obj = bpf__prepare_load(bpf_file_name, source);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000781 if (IS_ERR(obj)) {
Wang Nan84c86ca2015-10-14 12:41:14 +0000782 char errbuf[BUFSIZ];
Wang Nan84c86ca2015-10-14 12:41:14 +0000783
Wang Nan6371ca3b2015-11-06 13:49:37 +0000784 err = PTR_ERR(obj);
Wang Nan84c86ca2015-10-14 12:41:14 +0000785
786 if (err == -ENOTSUP)
787 snprintf(errbuf, sizeof(errbuf),
788 "BPF support is not compiled");
789 else
Wang Nand3e0ce32015-11-06 13:58:09 +0000790 bpf__strerror_prepare_load(bpf_file_name,
791 source,
792 -err, errbuf,
793 sizeof(errbuf));
Wang Nan84c86ca2015-10-14 12:41:14 +0000794
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300795 parse_state->error->help = strdup("(add -v to see detail)");
796 parse_state->error->str = strdup(errbuf);
Wang Nan84c86ca2015-10-14 12:41:14 +0000797 return err;
798 }
799
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300800 err = parse_events_load_bpf_obj(parse_state, list, obj, head_config);
Wang Nana34f3be2016-02-22 09:10:31 +0000801 if (err)
802 return err;
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300803 err = parse_events_config_bpf(parse_state, obj, &obj_head_config);
Wang Nan95088a52016-02-22 09:10:36 +0000804
805 /*
806 * Caller doesn't know anything about obj_head_config,
807 * so combine them together again before returnning.
808 */
809 if (head_config)
810 list_splice_tail(&obj_head_config, head_config);
811 return err;
Wang Nan84c86ca2015-10-14 12:41:14 +0000812}
813
Jiri Olsa89812fc2012-03-15 20:09:15 +0100814static int
815parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100816{
817 int i;
818
819 for (i = 0; i < 3; i++) {
Jiri Olsa89812fc2012-03-15 20:09:15 +0100820 if (!type || !type[i])
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100821 break;
822
Jiri Olsa75827322012-06-29 09:22:54 +0200823#define CHECK_SET_TYPE(bit) \
824do { \
825 if (attr->bp_type & bit) \
826 return -EINVAL; \
827 else \
828 attr->bp_type |= bit; \
829} while (0)
830
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100831 switch (type[i]) {
832 case 'r':
Jiri Olsa75827322012-06-29 09:22:54 +0200833 CHECK_SET_TYPE(HW_BREAKPOINT_R);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100834 break;
835 case 'w':
Jiri Olsa75827322012-06-29 09:22:54 +0200836 CHECK_SET_TYPE(HW_BREAKPOINT_W);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100837 break;
838 case 'x':
Jiri Olsa75827322012-06-29 09:22:54 +0200839 CHECK_SET_TYPE(HW_BREAKPOINT_X);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100840 break;
841 default:
Jiri Olsa89812fc2012-03-15 20:09:15 +0100842 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100843 }
844 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100845
Jiri Olsa75827322012-06-29 09:22:54 +0200846#undef CHECK_SET_TYPE
847
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100848 if (!attr->bp_type) /* Default */
849 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
850
Jiri Olsa89812fc2012-03-15 20:09:15 +0100851 return 0;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100852}
853
David Ahernc5cd8ac2013-07-02 13:27:25 -0600854int parse_events_add_breakpoint(struct list_head *list, int *idx,
Jacob Shin3741eb92014-05-29 17:26:51 +0200855 void *ptr, char *type, u64 len)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100856{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100857 struct perf_event_attr attr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100858
Jiri Olsa89812fc2012-03-15 20:09:15 +0100859 memset(&attr, 0, sizeof(attr));
Jiri Olsa9fafd982012-03-20 19:15:39 +0100860 attr.bp_addr = (unsigned long) ptr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100861
Jiri Olsa89812fc2012-03-15 20:09:15 +0100862 if (parse_breakpoint_type(type, &attr))
863 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100864
Jacob Shin3741eb92014-05-29 17:26:51 +0200865 /* Provide some defaults if len is not specified */
866 if (!len) {
867 if (attr.bp_type == HW_BREAKPOINT_X)
868 len = sizeof(long);
869 else
870 len = HW_BREAKPOINT_LEN_4;
871 }
872
873 attr.bp_len = len;
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200874
Jiri Olsa89812fc2012-03-15 20:09:15 +0100875 attr.type = PERF_TYPE_BREAKPOINT;
Jovi Zhang4a841d62012-07-15 03:03:10 +0800876 attr.sample_period = 1;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100877
Jiri Olsa930a2e22015-07-29 05:42:10 -0400878 return add_event(list, idx, &attr, NULL, NULL);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100879}
880
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200881static int check_type_val(struct parse_events_term *term,
882 struct parse_events_error *err,
883 int type)
Jiri Olsa8f707d82012-03-15 20:09:16 +0100884{
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200885 if (type == term->type_val)
886 return 0;
887
888 if (err) {
889 err->idx = term->err_val;
890 if (type == PARSE_EVENTS__TERM_TYPE_NUM)
891 err->str = strdup("expected numeric value");
892 else
893 err->str = strdup("expected string value");
894 }
895 return -EINVAL;
896}
897
Wang Nan17cb5f82016-02-19 11:43:57 +0000898/*
899 * Update according to parse-events.l
900 */
901static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
902 [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>",
903 [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config",
904 [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1",
905 [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2",
906 [PARSE_EVENTS__TERM_TYPE_NAME] = "name",
907 [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period",
908 [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq",
909 [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type",
910 [PARSE_EVENTS__TERM_TYPE_TIME] = "time",
911 [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph",
912 [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size",
913 [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit",
914 [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit",
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300915 [PARSE_EVENTS__TERM_TYPE_MAX_STACK] = "max-stack",
Wang Nan626a6b72016-07-14 08:34:45 +0000916 [PARSE_EVENTS__TERM_TYPE_OVERWRITE] = "overwrite",
917 [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE] = "no-overwrite",
Mathieu Poirierdd60fba2016-09-06 10:37:15 -0600918 [PARSE_EVENTS__TERM_TYPE_DRV_CFG] = "driver-config",
Wang Nan17cb5f82016-02-19 11:43:57 +0000919};
920
Wang Nan1669e502016-02-19 11:43:58 +0000921static bool config_term_shrinked;
922
923static bool
924config_term_avail(int term_type, struct parse_events_error *err)
925{
926 if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
927 err->str = strdup("Invalid term_type");
928 return false;
929 }
930 if (!config_term_shrinked)
931 return true;
932
933 switch (term_type) {
934 case PARSE_EVENTS__TERM_TYPE_CONFIG:
935 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
936 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
937 case PARSE_EVENTS__TERM_TYPE_NAME:
Sukadev Bhattiprolu06835542016-09-15 15:24:56 -0700938 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
Wang Nan1669e502016-02-19 11:43:58 +0000939 return true;
940 default:
941 if (!err)
942 return false;
943
944 /* term_type is validated so indexing is safe */
945 if (asprintf(&err->str, "'%s' is not usable in 'perf stat'",
946 config_term_names[term_type]) < 0)
947 err->str = NULL;
948 return false;
949 }
950}
951
952void parse_events__shrink_config_terms(void)
953{
954 config_term_shrinked = true;
955}
956
He Kuang0b8891a2015-09-28 03:52:13 +0000957static int config_term_common(struct perf_event_attr *attr,
958 struct parse_events_term *term,
959 struct parse_events_error *err)
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200960{
961#define CHECK_TYPE_VAL(type) \
962do { \
963 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
964 return -EINVAL; \
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200965} while (0)
966
967 switch (term->type_term) {
Jiri Olsa8f707d82012-03-15 20:09:16 +0100968 case PARSE_EVENTS__TERM_TYPE_CONFIG:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200969 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100970 attr->config = term->val.num;
971 break;
972 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200973 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100974 attr->config1 = term->val.num;
975 break;
976 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200977 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100978 attr->config2 = term->val.num;
979 break;
980 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200981 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100982 break;
Namhyung Kim09af2a52015-08-09 15:45:23 +0900983 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
984 CHECK_TYPE_VAL(NUM);
985 break;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100986 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
Andi Kleenac12f672016-10-12 14:02:06 -0700987 CHECK_TYPE_VAL(STR);
988 if (strcmp(term->val.str, "no") &&
989 parse_branch_str(term->val.str, &attr->branch_sample_type)) {
990 err->str = strdup("invalid branch sample type");
991 err->idx = term->err_val;
992 return -EINVAL;
993 }
Jiri Olsa8f707d82012-03-15 20:09:16 +0100994 break;
Kan Liang32067712015-08-04 04:30:19 -0400995 case PARSE_EVENTS__TERM_TYPE_TIME:
996 CHECK_TYPE_VAL(NUM);
997 if (term->val.num > 1) {
998 err->str = strdup("expected 0 or 1");
999 err->idx = term->err_val;
1000 return -EINVAL;
1001 }
1002 break;
Kan Liangd457c962015-08-11 06:30:47 -04001003 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1004 CHECK_TYPE_VAL(STR);
1005 break;
1006 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1007 CHECK_TYPE_VAL(NUM);
1008 break;
Wang Nan374ce932015-10-28 10:55:02 +00001009 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1010 CHECK_TYPE_VAL(NUM);
1011 break;
1012 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1013 CHECK_TYPE_VAL(NUM);
1014 break;
Wang Nan626a6b72016-07-14 08:34:45 +00001015 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1016 CHECK_TYPE_VAL(NUM);
1017 break;
1018 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1019 CHECK_TYPE_VAL(NUM);
1020 break;
Jiri Olsa6b5fc392012-05-21 09:12:53 +02001021 case PARSE_EVENTS__TERM_TYPE_NAME:
1022 CHECK_TYPE_VAL(STR);
1023 break;
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -03001024 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1025 CHECK_TYPE_VAL(NUM);
1026 break;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001027 default:
He Kuangffeb8832015-09-28 03:52:14 +00001028 err->str = strdup("unknown term");
1029 err->idx = term->err_term;
1030 err->help = parse_events_formats_error_string(NULL);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001031 return -EINVAL;
1032 }
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001033
Wang Nan1669e502016-02-19 11:43:58 +00001034 /*
1035 * Check term availbility after basic checking so
1036 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
1037 *
1038 * If check availbility at the entry of this function,
1039 * user will see "'<sysfs term>' is not usable in 'perf stat'"
1040 * if an invalid config term is provided for legacy events
1041 * (for example, instructions/badterm/...), which is confusing.
1042 */
1043 if (!config_term_avail(term->type_term, err))
1044 return -EINVAL;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001045 return 0;
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001046#undef CHECK_TYPE_VAL
Jiri Olsa8f707d82012-03-15 20:09:16 +01001047}
1048
He Kuang0b8891a2015-09-28 03:52:13 +00001049static int config_term_pmu(struct perf_event_attr *attr,
1050 struct parse_events_term *term,
1051 struct parse_events_error *err)
1052{
Mathieu Poirierdd60fba2016-09-06 10:37:15 -06001053 if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER ||
1054 term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG)
He Kuang0b8891a2015-09-28 03:52:13 +00001055 /*
1056 * Always succeed for sysfs terms, as we dont know
1057 * at this point what type they need to have.
1058 */
1059 return 0;
1060 else
1061 return config_term_common(attr, term, err);
1062}
1063
He Kuange637d172015-09-28 03:52:16 +00001064static int config_term_tracepoint(struct perf_event_attr *attr,
1065 struct parse_events_term *term,
1066 struct parse_events_error *err)
1067{
1068 switch (term->type_term) {
1069 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1070 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
Wang Nan374ce932015-10-28 10:55:02 +00001071 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1072 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -03001073 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
Wang Nan626a6b72016-07-14 08:34:45 +00001074 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1075 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
He Kuange637d172015-09-28 03:52:16 +00001076 return config_term_common(attr, term, err);
1077 default:
1078 if (err) {
1079 err->idx = term->err_term;
1080 err->str = strdup("unknown term");
1081 err->help = strdup("valid terms: call-graph,stack-size\n");
1082 }
1083 return -EINVAL;
1084 }
1085
1086 return 0;
1087}
1088
Jiri Olsa8f707d82012-03-15 20:09:16 +01001089static int config_attr(struct perf_event_attr *attr,
Jiri Olsa3b0e3712015-04-22 21:10:22 +02001090 struct list_head *head,
He Kuang0b8891a2015-09-28 03:52:13 +00001091 struct parse_events_error *err,
1092 config_term_func_t config_term)
Jiri Olsa8f707d82012-03-15 20:09:16 +01001093{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001094 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001095
1096 list_for_each_entry(term, head, list)
Jiri Olsa3b0e3712015-04-22 21:10:22 +02001097 if (config_term(attr, term, err))
Jiri Olsa8f707d82012-03-15 20:09:16 +01001098 return -EINVAL;
1099
1100 return 0;
1101}
1102
Jiri Olsa930a2e22015-07-29 05:42:10 -04001103static int get_config_terms(struct list_head *head_config,
1104 struct list_head *head_terms __maybe_unused)
1105{
1106#define ADD_CONFIG_TERM(__type, __name, __val) \
1107do { \
1108 struct perf_evsel_config_term *__t; \
1109 \
1110 __t = zalloc(sizeof(*__t)); \
1111 if (!__t) \
1112 return -ENOMEM; \
1113 \
1114 INIT_LIST_HEAD(&__t->list); \
1115 __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \
1116 __t->val.__name = __val; \
1117 list_add_tail(&__t->list, head_terms); \
1118} while (0)
1119
1120 struct parse_events_term *term;
1121
1122 list_for_each_entry(term, head_config, list) {
1123 switch (term->type_term) {
Jiri Olsaee4c7582015-07-29 05:42:11 -04001124 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1125 ADD_CONFIG_TERM(PERIOD, period, term->val.num);
Kan Liang32067712015-08-04 04:30:19 -04001126 break;
Namhyung Kim09af2a52015-08-09 15:45:23 +09001127 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1128 ADD_CONFIG_TERM(FREQ, freq, term->val.num);
1129 break;
Kan Liang32067712015-08-04 04:30:19 -04001130 case PARSE_EVENTS__TERM_TYPE_TIME:
1131 ADD_CONFIG_TERM(TIME, time, term->val.num);
1132 break;
Kan Liangd457c962015-08-11 06:30:47 -04001133 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1134 ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
1135 break;
Andi Kleenac12f672016-10-12 14:02:06 -07001136 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1137 ADD_CONFIG_TERM(BRANCH, branch, term->val.str);
1138 break;
Kan Liangd457c962015-08-11 06:30:47 -04001139 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1140 ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
1141 break;
Wang Nan374ce932015-10-28 10:55:02 +00001142 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1143 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0);
1144 break;
1145 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1146 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1);
1147 break;
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -03001148 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1149 ADD_CONFIG_TERM(MAX_STACK, max_stack, term->val.num);
1150 break;
Wang Nan626a6b72016-07-14 08:34:45 +00001151 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1152 ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 1 : 0);
1153 break;
1154 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1155 ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 0 : 1);
1156 break;
Mathieu Poirierdd60fba2016-09-06 10:37:15 -06001157 case PARSE_EVENTS__TERM_TYPE_DRV_CFG:
1158 ADD_CONFIG_TERM(DRV_CFG, drv_cfg, term->val.str);
1159 break;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001160 default:
1161 break;
1162 }
1163 }
1164#undef ADD_EVSEL_CONFIG
1165 return 0;
1166}
1167
He Kuange637d172015-09-28 03:52:16 +00001168int parse_events_add_tracepoint(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +00001169 const char *sys, const char *event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001170 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +00001171 struct list_head *head_config)
1172{
1173 if (head_config) {
1174 struct perf_event_attr attr;
1175
Jiri Olsa272ed292015-09-29 17:05:31 +02001176 if (config_attr(&attr, head_config, err,
He Kuange637d172015-09-28 03:52:16 +00001177 config_term_tracepoint))
1178 return -EINVAL;
1179 }
1180
1181 if (strpbrk(sys, "*?"))
1182 return add_tracepoint_multi_sys(list, idx, sys, event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001183 err, head_config);
He Kuange637d172015-09-28 03:52:16 +00001184 else
1185 return add_tracepoint_event(list, idx, sys, event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001186 err, head_config);
He Kuange637d172015-09-28 03:52:16 +00001187}
1188
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001189int parse_events_add_numeric(struct parse_events_state *parse_state,
Jiri Olsa87d650b2015-04-22 21:10:24 +02001190 struct list_head *list,
Robert Richterb527bab2012-08-07 19:43:13 +02001191 u32 type, u64 config,
Jiri Olsa8f707d82012-03-15 20:09:16 +01001192 struct list_head *head_config)
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301193{
Jiri Olsa89812fc2012-03-15 20:09:15 +01001194 struct perf_event_attr attr;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001195 LIST_HEAD(config_terms);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301196
Jiri Olsa89812fc2012-03-15 20:09:15 +01001197 memset(&attr, 0, sizeof(attr));
1198 attr.type = type;
1199 attr.config = config;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001200
Jiri Olsa930a2e22015-07-29 05:42:10 -04001201 if (head_config) {
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001202 if (config_attr(&attr, head_config, parse_state->error,
He Kuang0b8891a2015-09-28 03:52:13 +00001203 config_term_common))
Jiri Olsa930a2e22015-07-29 05:42:10 -04001204 return -EINVAL;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001205
Jiri Olsa930a2e22015-07-29 05:42:10 -04001206 if (get_config_terms(head_config, &config_terms))
1207 return -ENOMEM;
1208 }
1209
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001210 return add_event(list, &parse_state->idx, &attr,
Wang Nan10bf3582016-02-19 11:44:00 +00001211 get_config_name(head_config), &config_terms);
Paul Mackerras61c45982009-07-01 13:04:34 +10001212}
1213
Arnaldo Carvalho de Melo63ce8442017-08-31 15:32:18 -03001214static int __parse_events_add_pmu(struct parse_events_state *parse_state,
Jiri Olsa36adec82015-04-22 21:10:19 +02001215 struct list_head *list, char *name,
Arnaldo Carvalho de Melo63ce8442017-08-31 15:32:18 -03001216 struct list_head *head_config, bool auto_merge_stats)
Jiri Olsa5f537a22012-03-15 20:09:18 +01001217{
1218 struct perf_event_attr attr;
Matt Fleming46441bd2014-09-24 15:04:06 +01001219 struct perf_pmu_info info;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001220 struct perf_pmu *pmu;
Stephane Eranian410136f2013-11-12 17:58:49 +01001221 struct perf_evsel *evsel;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001222 LIST_HEAD(config_terms);
Jiri Olsa5f537a22012-03-15 20:09:18 +01001223
1224 pmu = perf_pmu__find(name);
1225 if (!pmu)
1226 return -EINVAL;
1227
Adrian Hunterdc0a6202014-07-31 09:00:49 +03001228 if (pmu->default_config) {
1229 memcpy(&attr, pmu->default_config,
1230 sizeof(struct perf_event_attr));
1231 } else {
1232 memset(&attr, 0, sizeof(attr));
1233 }
Jiri Olsa5f537a22012-03-15 20:09:18 +01001234
Adrian Hunterad962272014-08-15 22:08:40 +03001235 if (!head_config) {
1236 attr.type = pmu->type;
Arnaldo Carvalho de Melo63ce8442017-08-31 15:32:18 -03001237 evsel = __add_event(list, &parse_state->idx, &attr, NULL, pmu->cpus, NULL, auto_merge_stats);
Adrian Hunterad962272014-08-15 22:08:40 +03001238 return evsel ? 0 : -ENOMEM;
1239 }
1240
Matt Fleming46441bd2014-09-24 15:04:06 +01001241 if (perf_pmu__check_alias(pmu, head_config, &info))
Zheng Yana6146d52012-06-15 14:31:41 +08001242 return -EINVAL;
1243
Jiri Olsa5f537a22012-03-15 20:09:18 +01001244 /*
1245 * Configure hardcoded terms first, no need to check
1246 * return value when called with fail == 0 ;)
1247 */
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001248 if (config_attr(&attr, head_config, parse_state->error, config_term_pmu))
Jiri Olsac056ba6a2015-04-22 21:10:18 +02001249 return -EINVAL;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001250
Jiri Olsa930a2e22015-07-29 05:42:10 -04001251 if (get_config_terms(head_config, &config_terms))
1252 return -ENOMEM;
1253
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001254 if (perf_pmu__config(pmu, &attr, head_config, parse_state->error))
Jiri Olsa5f537a22012-03-15 20:09:18 +01001255 return -EINVAL;
1256
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001257 evsel = __add_event(list, &parse_state->idx, &attr,
Wang Nane814fdd2016-02-19 11:43:59 +00001258 get_config_name(head_config), pmu->cpus,
Arnaldo Carvalho de Melo63ce8442017-08-31 15:32:18 -03001259 &config_terms, auto_merge_stats);
Stephane Eranian410136f2013-11-12 17:58:49 +01001260 if (evsel) {
Matt Fleming46441bd2014-09-24 15:04:06 +01001261 evsel->unit = info.unit;
1262 evsel->scale = info.scale;
Matt Fleming044330c2014-11-21 10:31:12 +01001263 evsel->per_pkg = info.per_pkg;
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001264 evsel->snapshot = info.snapshot;
Andi Kleen37932c12017-03-20 13:17:08 -07001265 evsel->metric_expr = info.metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001266 evsel->metric_name = info.metric_name;
Stephane Eranian410136f2013-11-12 17:58:49 +01001267 }
1268
1269 return evsel ? 0 : -ENOMEM;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001270}
1271
Arnaldo Carvalho de Melo63ce8442017-08-31 15:32:18 -03001272int parse_events_add_pmu(struct parse_events_state *parse_state,
1273 struct list_head *list, char *name,
1274 struct list_head *head_config)
1275{
1276 return __parse_events_add_pmu(parse_state, list, name, head_config, false);
1277}
1278
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001279int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
Andi Kleen2073ad32017-03-20 13:17:02 -07001280 char *str, struct list_head **listp)
1281{
1282 struct list_head *head;
1283 struct parse_events_term *term;
1284 struct list_head *list;
1285 struct perf_pmu *pmu = NULL;
1286 int ok = 0;
1287
1288 *listp = NULL;
1289 /* Add it for all PMUs that support the alias */
1290 list = malloc(sizeof(struct list_head));
1291 if (!list)
1292 return -1;
1293 INIT_LIST_HEAD(list);
1294 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1295 struct perf_pmu_alias *alias;
1296
1297 list_for_each_entry(alias, &pmu->aliases, list) {
1298 if (!strcasecmp(alias->name, str)) {
1299 head = malloc(sizeof(struct list_head));
1300 if (!head)
1301 return -1;
1302 INIT_LIST_HEAD(head);
1303 if (parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
1304 str, 1, false, &str, NULL) < 0)
1305 return -1;
1306 list_add_tail(&term->list, head);
1307
Arnaldo Carvalho de Melo63ce8442017-08-31 15:32:18 -03001308 if (!__parse_events_add_pmu(parse_state, list,
1309 pmu->name, head, true)) {
Andi Kleen2073ad32017-03-20 13:17:02 -07001310 pr_debug("%s -> %s/%s/\n", str,
1311 pmu->name, alias->str);
1312 ok++;
1313 }
1314
1315 parse_events_terms__delete(head);
1316 }
1317 }
1318 }
1319 if (!ok)
1320 return -1;
1321 *listp = list;
1322 return 0;
1323}
1324
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001325int parse_events__modifier_group(struct list_head *list,
1326 char *event_mod)
Jiri Olsa89efb022012-08-08 12:14:14 +02001327{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001328 return parse_events__modifier_event(list, event_mod, true);
1329}
1330
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -03001331void parse_events__set_leader(char *name, struct list_head *list)
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001332{
1333 struct perf_evsel *leader;
1334
Wang Nan854f7362015-09-06 07:13:17 +00001335 if (list_empty(list)) {
1336 WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1337 return;
1338 }
1339
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -03001340 __perf_evlist__set_leader(list);
1341 leader = list_entry(list->next, struct perf_evsel, node);
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001342 leader->group_name = name ? strdup(name) : NULL;
Jiri Olsa89efb022012-08-08 12:14:14 +02001343}
1344
David Ahernc5cd8ac2013-07-02 13:27:25 -06001345/* list_event is assumed to point to malloc'ed memory */
Jiri Olsa5d7be902012-03-20 19:15:40 +01001346void parse_events_update_lists(struct list_head *list_event,
1347 struct list_head *list_all)
1348{
1349 /*
1350 * Called for single event definition. Update the
Jiri Olsa89efb022012-08-08 12:14:14 +02001351 * 'all event' list, and reinit the 'single event'
Jiri Olsa5d7be902012-03-20 19:15:40 +01001352 * list, for next event definition.
1353 */
1354 list_splice_tail(list_event, list_all);
Jiri Olsab847cbd2012-05-21 09:12:51 +02001355 free(list_event);
Jiri Olsa5d7be902012-03-20 19:15:40 +01001356}
1357
Jiri Olsaf5b11352012-08-08 12:21:54 +02001358struct event_modifier {
1359 int eu;
1360 int ek;
1361 int eh;
1362 int eH;
1363 int eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001364 int eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001365 int precise;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001366 int precise_max;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001367 int exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02001368 int sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001369 int pinned;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001370 int weak;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001371};
Paul Mackerras61c45982009-07-01 13:04:34 +10001372
Jiri Olsaf5b11352012-08-08 12:21:54 +02001373static int get_event_modifier(struct event_modifier *mod, char *str,
1374 struct perf_evsel *evsel)
1375{
1376 int eu = evsel ? evsel->attr.exclude_user : 0;
1377 int ek = evsel ? evsel->attr.exclude_kernel : 0;
1378 int eh = evsel ? evsel->attr.exclude_hv : 0;
1379 int eH = evsel ? evsel->attr.exclude_host : 0;
1380 int eG = evsel ? evsel->attr.exclude_guest : 0;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001381 int eI = evsel ? evsel->attr.exclude_idle : 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001382 int precise = evsel ? evsel->attr.precise_ip : 0;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001383 int precise_max = 0;
Jiri Olsa3c176312012-10-10 17:39:03 +02001384 int sample_read = 0;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001385 int pinned = evsel ? evsel->attr.pinned : 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001386
1387 int exclude = eu | ek | eh;
1388 int exclude_GH = evsel ? evsel->exclude_GH : 0;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001389 int weak = 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001390
Jiri Olsaf5b11352012-08-08 12:21:54 +02001391 memset(mod, 0, sizeof(*mod));
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001392
Paul Mackerras61c45982009-07-01 13:04:34 +10001393 while (*str) {
Peter Zijlstraab608342010-04-08 23:03:20 +02001394 if (*str == 'u') {
1395 if (!exclude)
1396 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001397 eu = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +02001398 } else if (*str == 'k') {
1399 if (!exclude)
1400 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001401 ek = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +02001402 } else if (*str == 'h') {
1403 if (!exclude)
1404 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001405 eh = 0;
Joerg Roedel99320cc2012-01-04 17:54:19 +01001406 } else if (*str == 'G') {
1407 if (!exclude_GH)
1408 exclude_GH = eG = eH = 1;
1409 eG = 0;
1410 } else if (*str == 'H') {
1411 if (!exclude_GH)
1412 exclude_GH = eG = eH = 1;
1413 eH = 0;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001414 } else if (*str == 'I') {
1415 eI = 1;
Peter Zijlstraab608342010-04-08 23:03:20 +02001416 } else if (*str == 'p') {
1417 precise++;
David Ahern13427982012-09-13 14:59:13 -06001418 /* use of precise requires exclude_guest */
1419 if (!exclude_GH)
1420 eG = 1;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001421 } else if (*str == 'P') {
1422 precise_max = 1;
Jiri Olsa3c176312012-10-10 17:39:03 +02001423 } else if (*str == 'S') {
1424 sample_read = 1;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001425 } else if (*str == 'D') {
1426 pinned = 1;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001427 } else if (*str == 'W') {
1428 weak = 1;
Peter Zijlstraab608342010-04-08 23:03:20 +02001429 } else
Paul Mackerras61c45982009-07-01 13:04:34 +10001430 break;
Peter Zijlstraab608342010-04-08 23:03:20 +02001431
Paul Mackerras61c45982009-07-01 13:04:34 +10001432 ++str;
1433 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001434
Jiri Olsa89812fc2012-03-15 20:09:15 +01001435 /*
1436 * precise ip:
1437 *
1438 * 0 - SAMPLE_IP can have arbitrary skid
1439 * 1 - SAMPLE_IP must have constant skid
1440 * 2 - SAMPLE_IP requested to have 0 skid
1441 * 3 - SAMPLE_IP must have 0 skid
1442 *
1443 * See also PERF_RECORD_MISC_EXACT_IP
1444 */
1445 if (precise > 3)
1446 return -EINVAL;
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001447
Jiri Olsaf5b11352012-08-08 12:21:54 +02001448 mod->eu = eu;
1449 mod->ek = ek;
1450 mod->eh = eh;
1451 mod->eH = eH;
1452 mod->eG = eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001453 mod->eI = eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001454 mod->precise = precise;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001455 mod->precise_max = precise_max;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001456 mod->exclude_GH = exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02001457 mod->sample_read = sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001458 mod->pinned = pinned;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001459 mod->weak = weak;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001460
Jiri Olsaf5b11352012-08-08 12:21:54 +02001461 return 0;
1462}
1463
Jiri Olsa534123f2012-11-13 15:32:58 +01001464/*
1465 * Basic modifier sanity check to validate it contains only one
1466 * instance of any modifier (apart from 'p') present.
1467 */
1468static int check_modifier(char *str)
1469{
1470 char *p = str;
1471
1472 /* The sizeof includes 0 byte as well. */
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001473 if (strlen(str) > (sizeof("ukhGHpppPSDIW") - 1))
Jiri Olsa534123f2012-11-13 15:32:58 +01001474 return -1;
1475
1476 while (*p) {
1477 if (*p != 'p' && strchr(p + 1, *p))
1478 return -1;
1479 p++;
1480 }
1481
1482 return 0;
1483}
1484
Jiri Olsaf5b11352012-08-08 12:21:54 +02001485int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1486{
1487 struct perf_evsel *evsel;
1488 struct event_modifier mod;
1489
1490 if (str == NULL)
1491 return 0;
1492
Jiri Olsa534123f2012-11-13 15:32:58 +01001493 if (check_modifier(str))
1494 return -EINVAL;
1495
Jiri Olsaf5b11352012-08-08 12:21:54 +02001496 if (!add && get_event_modifier(&mod, str, NULL))
1497 return -EINVAL;
1498
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001499 __evlist__for_each_entry(list, evsel) {
Jiri Olsaf5b11352012-08-08 12:21:54 +02001500 if (add && get_event_modifier(&mod, str, evsel))
1501 return -EINVAL;
1502
1503 evsel->attr.exclude_user = mod.eu;
1504 evsel->attr.exclude_kernel = mod.ek;
1505 evsel->attr.exclude_hv = mod.eh;
1506 evsel->attr.precise_ip = mod.precise;
1507 evsel->attr.exclude_host = mod.eH;
1508 evsel->attr.exclude_guest = mod.eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001509 evsel->attr.exclude_idle = mod.eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001510 evsel->exclude_GH = mod.exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02001511 evsel->sample_read = mod.sample_read;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001512 evsel->precise_max = mod.precise_max;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001513 evsel->weak_group = mod.weak;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001514
1515 if (perf_evsel__is_group_leader(evsel))
1516 evsel->attr.pinned = mod.pinned;
Jiri Olsa89812fc2012-03-15 20:09:15 +01001517 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001518
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301519 return 0;
1520}
1521
Robert Richterac2ba9f2012-08-16 21:10:21 +02001522int parse_events_name(struct list_head *list, char *name)
1523{
1524 struct perf_evsel *evsel;
1525
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001526 __evlist__for_each_entry(list, evsel) {
Robert Richterac2ba9f2012-08-16 21:10:21 +02001527 if (!evsel->name)
1528 evsel->name = strdup(name);
1529 }
1530
1531 return 0;
1532}
1533
Kan Liangdcb4e102014-10-07 11:08:50 -04001534static int
1535comp_pmu(const void *p1, const void *p2)
1536{
1537 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
1538 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
1539
Andi Kleene312bcf2016-09-15 15:24:53 -07001540 return strcasecmp(pmu1->symbol, pmu2->symbol);
Kan Liangdcb4e102014-10-07 11:08:50 -04001541}
1542
1543static void perf_pmu__parse_cleanup(void)
1544{
1545 if (perf_pmu_events_list_num > 0) {
1546 struct perf_pmu_event_symbol *p;
1547 int i;
1548
1549 for (i = 0; i < perf_pmu_events_list_num; i++) {
1550 p = perf_pmu_events_list + i;
Taeung Song360e0712017-02-01 21:34:07 +09001551 zfree(&p->symbol);
Kan Liangdcb4e102014-10-07 11:08:50 -04001552 }
Taeung Song506fde12017-02-01 21:34:06 +09001553 zfree(&perf_pmu_events_list);
Kan Liangdcb4e102014-10-07 11:08:50 -04001554 perf_pmu_events_list_num = 0;
1555 }
1556}
1557
1558#define SET_SYMBOL(str, stype) \
1559do { \
1560 p->symbol = str; \
1561 if (!p->symbol) \
1562 goto err; \
1563 p->type = stype; \
1564} while (0)
1565
1566/*
1567 * Read the pmu events list from sysfs
1568 * Save it into perf_pmu_events_list
1569 */
1570static void perf_pmu__parse_init(void)
1571{
1572
1573 struct perf_pmu *pmu = NULL;
1574 struct perf_pmu_alias *alias;
1575 int len = 0;
1576
Andi Kleen231bb2a2017-01-27 18:03:39 -08001577 pmu = NULL;
1578 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1579 list_for_each_entry(alias, &pmu->aliases, list) {
1580 if (strchr(alias->name, '-'))
1581 len++;
1582 len++;
1583 }
1584 }
1585
1586 if (len == 0) {
Kan Liangdcb4e102014-10-07 11:08:50 -04001587 perf_pmu_events_list_num = -1;
1588 return;
1589 }
Kan Liangdcb4e102014-10-07 11:08:50 -04001590 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1591 if (!perf_pmu_events_list)
1592 return;
1593 perf_pmu_events_list_num = len;
1594
1595 len = 0;
Andi Kleen231bb2a2017-01-27 18:03:39 -08001596 pmu = NULL;
1597 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1598 list_for_each_entry(alias, &pmu->aliases, list) {
1599 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1600 char *tmp = strchr(alias->name, '-');
Kan Liangdcb4e102014-10-07 11:08:50 -04001601
Andi Kleen231bb2a2017-01-27 18:03:39 -08001602 if (tmp != NULL) {
1603 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1604 PMU_EVENT_SYMBOL_PREFIX);
1605 p++;
1606 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1607 len += 2;
1608 } else {
1609 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1610 len++;
1611 }
Kan Liangdcb4e102014-10-07 11:08:50 -04001612 }
1613 }
1614 qsort(perf_pmu_events_list, len,
1615 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1616
1617 return;
1618err:
1619 perf_pmu__parse_cleanup();
1620}
1621
1622enum perf_pmu_event_symbol_type
1623perf_pmu__parse_check(const char *name)
1624{
1625 struct perf_pmu_event_symbol p, *r;
1626
1627 /* scan kernel pmu events from sysfs if needed */
1628 if (perf_pmu_events_list_num == 0)
1629 perf_pmu__parse_init();
1630 /*
1631 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1632 * cpu-cycles has been handled by hardcode.
1633 * So it must be cpu// events, not kernel pmu event.
1634 */
1635 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1636 return PMU_EVENT_SYMBOL_ERR;
1637
1638 p.symbol = strdup(name);
1639 r = bsearch(&p, perf_pmu_events_list,
1640 (size_t) perf_pmu_events_list_num,
1641 sizeof(struct perf_pmu_event_symbol), comp_pmu);
Taeung Song360e0712017-02-01 21:34:07 +09001642 zfree(&p.symbol);
Kan Liangdcb4e102014-10-07 11:08:50 -04001643 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1644}
1645
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001646static int parse_events__scanner(const char *str, void *parse_state, int start_token)
Zheng Yanac20de6f2012-06-15 14:31:39 +08001647{
1648 YY_BUFFER_STATE buffer;
1649 void *scanner;
1650 int ret;
1651
Jiri Olsa90e2b222012-06-15 14:31:40 +08001652 ret = parse_events_lex_init_extra(start_token, &scanner);
Zheng Yanac20de6f2012-06-15 14:31:39 +08001653 if (ret)
1654 return ret;
1655
1656 buffer = parse_events__scan_string(str, scanner);
1657
1658#ifdef PARSER_DEBUG
1659 parse_events_debug = 1;
1660#endif
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001661 ret = parse_events_parse(parse_state, scanner);
Zheng Yanac20de6f2012-06-15 14:31:39 +08001662
1663 parse_events__flush_buffer(buffer, scanner);
1664 parse_events__delete_buffer(buffer, scanner);
1665 parse_events_lex_destroy(scanner);
1666 return ret;
1667}
1668
Jiri Olsa90e2b222012-06-15 14:31:40 +08001669/*
1670 * parse event config string, return a list of event terms.
1671 */
1672int parse_events_terms(struct list_head *terms, const char *str)
1673{
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001674 struct parse_events_state parse_state = {
Jiri Olsa90e2b222012-06-15 14:31:40 +08001675 .terms = NULL,
1676 };
1677 int ret;
1678
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001679 ret = parse_events__scanner(str, &parse_state, PE_START_TERMS);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001680 if (!ret) {
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001681 list_splice(parse_state.terms, terms);
1682 zfree(&parse_state.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001683 return 0;
1684 }
1685
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001686 parse_events_terms__delete(parse_state.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001687 return ret;
1688}
1689
Jiri Olsab39b8392015-04-22 21:10:16 +02001690int parse_events(struct perf_evlist *evlist, const char *str,
1691 struct parse_events_error *err)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001692{
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001693 struct parse_events_state parse_state = {
1694 .list = LIST_HEAD_INIT(parse_state.list),
Wang Nan7630b3e2016-02-22 09:10:33 +00001695 .idx = evlist->nr_entries,
1696 .error = err,
1697 .evlist = evlist,
Jiri Olsa46010ab2012-06-15 14:31:38 +08001698 };
Jiri Olsa46010ab2012-06-15 14:31:38 +08001699 int ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001700
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001701 ret = parse_events__scanner(str, &parse_state, PE_START_EVENTS);
Kan Liangdcb4e102014-10-07 11:08:50 -04001702 perf_pmu__parse_cleanup();
Jiri Olsa89812fc2012-03-15 20:09:15 +01001703 if (!ret) {
Wang Nan15bfd2c2015-07-10 07:36:09 +00001704 struct perf_evsel *last;
1705
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001706 if (list_empty(&parse_state.list)) {
Wang Nan854f7362015-09-06 07:13:17 +00001707 WARN_ONCE(true, "WARNING: event parser found nothing");
1708 return -1;
1709 }
1710
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001711 perf_evlist__splice_list_tail(evlist, &parse_state.list);
1712 evlist->nr_groups += parse_state.nr_groups;
Wang Nan15bfd2c2015-07-10 07:36:09 +00001713 last = perf_evlist__last(evlist);
1714 last->cmdline_group_boundary = true;
1715
Jiri Olsa89812fc2012-03-15 20:09:15 +01001716 return 0;
1717 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02001718
Jiri Olsa5d7be902012-03-20 19:15:40 +01001719 /*
1720 * There are 2 users - builtin-record and builtin-test objects.
1721 * Both call perf_evlist__delete in case of error, so we dont
1722 * need to bother.
1723 */
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02001724 return ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001725}
1726
Jiri Olsab39b8392015-04-22 21:10:16 +02001727#define MAX_WIDTH 1000
1728static int get_term_width(void)
1729{
1730 struct winsize ws;
1731
1732 get_term_dimensions(&ws);
1733 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1734}
1735
1736static void parse_events_print_error(struct parse_events_error *err,
1737 const char *event)
1738{
1739 const char *str = "invalid or unsupported event: ";
1740 char _buf[MAX_WIDTH];
1741 char *buf = (char *) event;
1742 int idx = 0;
1743
1744 if (err->str) {
1745 /* -2 for extra '' in the final fprintf */
1746 int width = get_term_width() - 2;
1747 int len_event = strlen(event);
1748 int len_str, max_len, cut = 0;
1749
1750 /*
1751 * Maximum error index indent, we will cut
1752 * the event string if it's bigger.
1753 */
Adrian Hunter141b2d32015-07-17 19:33:51 +03001754 int max_err_idx = 13;
Jiri Olsab39b8392015-04-22 21:10:16 +02001755
1756 /*
1757 * Let's be specific with the message when
1758 * we have the precise error.
1759 */
1760 str = "event syntax error: ";
1761 len_str = strlen(str);
1762 max_len = width - len_str;
1763
1764 buf = _buf;
1765
Adam Buchbinderbd1a0be52016-02-24 10:02:25 -08001766 /* We're cutting from the beginning. */
Jiri Olsab39b8392015-04-22 21:10:16 +02001767 if (err->idx > max_err_idx)
1768 cut = err->idx - max_err_idx;
1769
1770 strncpy(buf, event + cut, max_len);
1771
1772 /* Mark cut parts with '..' on both sides. */
1773 if (cut)
1774 buf[0] = buf[1] = '.';
1775
1776 if ((len_event - cut) > max_len) {
1777 buf[max_len - 1] = buf[max_len - 2] = '.';
1778 buf[max_len] = 0;
1779 }
1780
1781 idx = len_str + err->idx - cut;
1782 }
1783
1784 fprintf(stderr, "%s'%s'\n", str, buf);
1785 if (idx) {
1786 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
1787 if (err->help)
1788 fprintf(stderr, "\n%s\n", err->help);
Taeung Song360e0712017-02-01 21:34:07 +09001789 zfree(&err->str);
1790 zfree(&err->help);
Jiri Olsab39b8392015-04-22 21:10:16 +02001791 }
1792
1793 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
1794}
1795
1796#undef MAX_WIDTH
1797
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001798int parse_events_option(const struct option *opt, const char *str,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001799 int unset __maybe_unused)
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001800{
1801 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
Jiri Olsab39b8392015-04-22 21:10:16 +02001802 struct parse_events_error err = { .idx = 0, };
1803 int ret = parse_events(evlist, str, &err);
Andi Kleen9175ce12012-10-26 13:30:06 -07001804
Jiri Olsab39b8392015-04-22 21:10:16 +02001805 if (ret)
1806 parse_events_print_error(&err, str);
1807
Andi Kleen9175ce12012-10-26 13:30:06 -07001808 return ret;
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001809}
1810
Wang Nan4ba1faa2015-07-10 07:36:10 +00001811static int
1812foreach_evsel_in_last_glob(struct perf_evlist *evlist,
1813 int (*func)(struct perf_evsel *evsel,
1814 const void *arg),
1815 const void *arg)
Li Zefanc171b552009-10-15 11:22:07 +08001816{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001817 struct perf_evsel *last = NULL;
Wang Nan4ba1faa2015-07-10 07:36:10 +00001818 int err;
Li Zefanc171b552009-10-15 11:22:07 +08001819
Wang Nan854f7362015-09-06 07:13:17 +00001820 /*
1821 * Don't return when list_empty, give func a chance to report
1822 * error when it found last == NULL.
1823 *
1824 * So no need to WARN here, let *func do this.
1825 */
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001826 if (evlist->nr_entries > 0)
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001827 last = perf_evlist__last(evlist);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001828
Wang Nan15bfd2c2015-07-10 07:36:09 +00001829 do {
Wang Nan4ba1faa2015-07-10 07:36:10 +00001830 err = (*func)(last, arg);
1831 if (err)
Wang Nan15bfd2c2015-07-10 07:36:09 +00001832 return -1;
Wang Nan4ba1faa2015-07-10 07:36:10 +00001833 if (!last)
1834 return 0;
Wang Nan15bfd2c2015-07-10 07:36:09 +00001835
1836 if (last->node.prev == &evlist->entries)
1837 return 0;
1838 last = list_entry(last->node.prev, struct perf_evsel, node);
1839 } while (!last->cmdline_group_boundary);
Li Zefanc171b552009-10-15 11:22:07 +08001840
1841 return 0;
1842}
1843
Wang Nan4ba1faa2015-07-10 07:36:10 +00001844static int set_filter(struct perf_evsel *evsel, const void *arg)
1845{
1846 const char *str = arg;
Mathieu Poirier1e857482016-09-16 08:44:05 -06001847 bool found = false;
1848 int nr_addr_filters = 0;
1849 struct perf_pmu *pmu = NULL;
Wang Nan4ba1faa2015-07-10 07:36:10 +00001850
Mathieu Poirier1e857482016-09-16 08:44:05 -06001851 if (evsel == NULL)
1852 goto err;
1853
1854 if (evsel->attr.type == PERF_TYPE_TRACEPOINT) {
1855 if (perf_evsel__append_tp_filter(evsel, str) < 0) {
1856 fprintf(stderr,
1857 "not enough memory to hold filter string\n");
1858 return -1;
1859 }
1860
1861 return 0;
Wang Nan4ba1faa2015-07-10 07:36:10 +00001862 }
1863
Mathieu Poirier1e857482016-09-16 08:44:05 -06001864 while ((pmu = perf_pmu__scan(pmu)) != NULL)
1865 if (pmu->type == evsel->attr.type) {
1866 found = true;
1867 break;
1868 }
1869
1870 if (found)
1871 perf_pmu__scan_file(pmu, "nr_addr_filters",
1872 "%d", &nr_addr_filters);
1873
1874 if (!nr_addr_filters)
1875 goto err;
1876
1877 if (perf_evsel__append_addr_filter(evsel, str) < 0) {
Wang Nan4ba1faa2015-07-10 07:36:10 +00001878 fprintf(stderr,
1879 "not enough memory to hold filter string\n");
1880 return -1;
1881 }
1882
1883 return 0;
Mathieu Poirier1e857482016-09-16 08:44:05 -06001884
1885err:
1886 fprintf(stderr,
1887 "--filter option should follow a -e tracepoint or HW tracer option\n");
1888
1889 return -1;
Wang Nan4ba1faa2015-07-10 07:36:10 +00001890}
1891
1892int parse_filter(const struct option *opt, const char *str,
1893 int unset __maybe_unused)
1894{
1895 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1896
1897 return foreach_evsel_in_last_glob(evlist, set_filter,
1898 (const void *)str);
1899}
1900
1901static int add_exclude_perf_filter(struct perf_evsel *evsel,
1902 const void *arg __maybe_unused)
1903{
1904 char new_filter[64];
1905
1906 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
1907 fprintf(stderr,
1908 "--exclude-perf option should follow a -e tracepoint option\n");
1909 return -1;
1910 }
1911
1912 snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
1913
Mathieu Poirier3541c032016-09-16 08:44:04 -06001914 if (perf_evsel__append_tp_filter(evsel, new_filter) < 0) {
Wang Nan4ba1faa2015-07-10 07:36:10 +00001915 fprintf(stderr,
1916 "not enough memory to hold filter string\n");
1917 return -1;
1918 }
1919
1920 return 0;
1921}
1922
1923int exclude_perf(const struct option *opt,
1924 const char *arg __maybe_unused,
1925 int unset __maybe_unused)
1926{
1927 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1928
1929 return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
1930 NULL);
1931}
1932
Thomas Gleixner86847b62009-06-06 12:24:17 +02001933static const char * const event_type_descriptors[] = {
Thomas Gleixner86847b62009-06-06 12:24:17 +02001934 "Hardware event",
1935 "Software event",
1936 "Tracepoint event",
1937 "Hardware cache event",
Liming Wang41bdcb22009-12-29 16:37:07 +08001938 "Raw hardware event descriptor",
1939 "Hardware breakpoint",
Thomas Gleixner86847b62009-06-06 12:24:17 +02001940};
1941
Yunlong Songab0e4802015-02-27 18:21:25 +08001942static int cmp_string(const void *a, const void *b)
1943{
1944 const char * const *as = a;
1945 const char * const *bs = b;
1946
1947 return strcmp(*as, *bs);
1948}
1949
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001950/*
Jason Baronf6bdafe2009-07-21 12:20:22 -04001951 * Print the events from <debugfs_mount_point>/tracing/events
1952 */
1953
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001954void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
1955 bool name_only)
Jason Baronf6bdafe2009-07-21 12:20:22 -04001956{
1957 DIR *sys_dir, *evt_dir;
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03001958 struct dirent *sys_dirent, *evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001959 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +02001960 char dir_path[MAXPATHLEN];
Yunlong Songab0e4802015-02-27 18:21:25 +08001961 char **evt_list = NULL;
1962 unsigned int evt_i = 0, evt_num = 0;
1963 bool evt_num_known = false;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001964
Yunlong Songab0e4802015-02-27 18:21:25 +08001965restart:
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001966 sys_dir = opendir(tracing_events_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -04001967 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +02001968 return;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001969
Yunlong Songab0e4802015-02-27 18:21:25 +08001970 if (evt_num_known) {
1971 evt_list = zalloc(sizeof(char *) * evt_num);
1972 if (!evt_list)
1973 goto out_close_sys_dir;
1974 }
1975
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03001976 for_each_subsystem(sys_dir, sys_dirent) {
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001977 if (subsys_glob != NULL &&
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03001978 !strglobmatch(sys_dirent->d_name, subsys_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001979 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02001980
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001981 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03001982 sys_dirent->d_name);
Eric Dumazet725b1362009-09-24 15:39:09 +02001983 evt_dir = opendir(dir_path);
1984 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03001985 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02001986
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03001987 for_each_event(sys_dirent, evt_dir, evt_dirent) {
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001988 if (event_glob != NULL &&
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03001989 !strglobmatch(evt_dirent->d_name, event_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001990 continue;
1991
Yunlong Songab0e4802015-02-27 18:21:25 +08001992 if (!evt_num_known) {
1993 evt_num++;
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001994 continue;
1995 }
1996
Jason Baronf6bdafe2009-07-21 12:20:22 -04001997 snprintf(evt_path, MAXPATHLEN, "%s:%s",
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03001998 sys_dirent->d_name, evt_dirent->d_name);
Yunlong Songab0e4802015-02-27 18:21:25 +08001999
2000 evt_list[evt_i] = strdup(evt_path);
2001 if (evt_list[evt_i] == NULL)
2002 goto out_close_evt_dir;
2003 evt_i++;
Jason Baronf6bdafe2009-07-21 12:20:22 -04002004 }
2005 closedir(evt_dir);
2006 }
Jason Baronf6bdafe2009-07-21 12:20:22 -04002007 closedir(sys_dir);
Yunlong Songab0e4802015-02-27 18:21:25 +08002008
2009 if (!evt_num_known) {
2010 evt_num_known = true;
2011 goto restart;
2012 }
2013 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2014 evt_i = 0;
2015 while (evt_i < evt_num) {
2016 if (name_only) {
2017 printf("%s ", evt_list[evt_i++]);
2018 continue;
2019 }
2020 printf(" %-50s [%s]\n", evt_list[evt_i++],
2021 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2022 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002023 if (evt_num && pager_in_use())
Yunlong Songab0e4802015-02-27 18:21:25 +08002024 printf("\n");
2025
2026out_free:
2027 evt_num = evt_i;
2028 for (evt_i = 0; evt_i < evt_num; evt_i++)
2029 zfree(&evt_list[evt_i]);
2030 zfree(&evt_list);
2031 return;
2032
2033out_close_evt_dir:
2034 closedir(evt_dir);
2035out_close_sys_dir:
2036 closedir(sys_dir);
2037
2038 printf("FATAL: not enough memory to print %s\n",
2039 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2040 if (evt_list)
2041 goto out_free;
Jason Baronf6bdafe2009-07-21 12:20:22 -04002042}
2043
2044/*
Thomas Renninger20c457b2011-01-03 17:50:45 +01002045 * Check whether event is in <debugfs_mount_point>/tracing/events
2046 */
2047
2048int is_valid_tracepoint(const char *event_string)
2049{
2050 DIR *sys_dir, *evt_dir;
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002051 struct dirent *sys_dirent, *evt_dirent;
Thomas Renninger20c457b2011-01-03 17:50:45 +01002052 char evt_path[MAXPATHLEN];
2053 char dir_path[MAXPATHLEN];
2054
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02002055 sys_dir = opendir(tracing_events_path);
Thomas Renninger20c457b2011-01-03 17:50:45 +01002056 if (!sys_dir)
2057 return 0;
2058
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002059 for_each_subsystem(sys_dir, sys_dirent) {
Thomas Renninger20c457b2011-01-03 17:50:45 +01002060
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02002061 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002062 sys_dirent->d_name);
Thomas Renninger20c457b2011-01-03 17:50:45 +01002063 evt_dir = opendir(dir_path);
2064 if (!evt_dir)
2065 continue;
2066
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002067 for_each_event(sys_dirent, evt_dir, evt_dirent) {
Thomas Renninger20c457b2011-01-03 17:50:45 +01002068 snprintf(evt_path, MAXPATHLEN, "%s:%s",
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002069 sys_dirent->d_name, evt_dirent->d_name);
Thomas Renninger20c457b2011-01-03 17:50:45 +01002070 if (!strcmp(evt_path, event_string)) {
2071 closedir(evt_dir);
2072 closedir(sys_dir);
2073 return 1;
2074 }
2075 }
2076 closedir(evt_dir);
2077 }
2078 closedir(sys_dir);
2079 return 0;
2080}
2081
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002082static bool is_event_supported(u8 type, unsigned config)
2083{
2084 bool ret = true;
Vince Weaver88fee522013-12-30 15:39:45 -05002085 int open_return;
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002086 struct perf_evsel *evsel;
2087 struct perf_event_attr attr = {
2088 .type = type,
2089 .config = config,
2090 .disabled = 1,
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002091 };
Arnaldo Carvalho de Melo89896052017-02-14 10:59:04 -03002092 struct thread_map *tmap = thread_map__new_by_tid(0);
2093
2094 if (tmap == NULL)
2095 return false;
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002096
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002097 evsel = perf_evsel__new(&attr);
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002098 if (evsel) {
Arnaldo Carvalho de Melo89896052017-02-14 10:59:04 -03002099 open_return = perf_evsel__open(evsel, NULL, tmap);
Vince Weaver88fee522013-12-30 15:39:45 -05002100 ret = open_return >= 0;
2101
2102 if (open_return == -EACCES) {
2103 /*
2104 * This happens if the paranoid value
2105 * /proc/sys/kernel/perf_event_paranoid is set to 2
2106 * Re-run with exclude_kernel set; we don't do that
2107 * by default as some ARM machines do not support it.
2108 *
2109 */
2110 evsel->attr.exclude_kernel = 1;
Arnaldo Carvalho de Melo89896052017-02-14 10:59:04 -03002111 ret = perf_evsel__open(evsel, NULL, tmap) >= 0;
Vince Weaver88fee522013-12-30 15:39:45 -05002112 }
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002113 perf_evsel__delete(evsel);
2114 }
2115
2116 return ret;
2117}
2118
Masami Hiramatsu40218da2016-06-24 18:06:46 +09002119void print_sdt_events(const char *subsys_glob, const char *event_glob,
2120 bool name_only)
2121{
2122 struct probe_cache *pcache;
2123 struct probe_cache_entry *ent;
2124 struct strlist *bidlist, *sdtlist;
2125 struct strlist_config cfg = {.dont_dupstr = true};
2126 struct str_node *nd, *nd2;
2127 char *buf, *path, *ptr = NULL;
2128 bool show_detail = false;
2129 int ret;
2130
2131 sdtlist = strlist__new(NULL, &cfg);
2132 if (!sdtlist) {
2133 pr_debug("Failed to allocate new strlist for SDT\n");
2134 return;
2135 }
2136 bidlist = build_id_cache__list_all(true);
2137 if (!bidlist) {
2138 pr_debug("Failed to get buildids: %d\n", errno);
2139 return;
2140 }
2141 strlist__for_each_entry(nd, bidlist) {
Krister Johansenf045b8c2017-07-05 18:48:11 -07002142 pcache = probe_cache__new(nd->s, NULL);
Masami Hiramatsu40218da2016-06-24 18:06:46 +09002143 if (!pcache)
2144 continue;
2145 list_for_each_entry(ent, &pcache->entries, node) {
2146 if (!ent->sdt)
2147 continue;
2148 if (subsys_glob &&
2149 !strglobmatch(ent->pev.group, subsys_glob))
2150 continue;
2151 if (event_glob &&
2152 !strglobmatch(ent->pev.event, event_glob))
2153 continue;
2154 ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
2155 ent->pev.event, nd->s);
2156 if (ret > 0)
2157 strlist__add(sdtlist, buf);
2158 }
2159 probe_cache__delete(pcache);
2160 }
2161 strlist__delete(bidlist);
2162
2163 strlist__for_each_entry(nd, sdtlist) {
2164 buf = strchr(nd->s, '@');
2165 if (buf)
2166 *(buf++) = '\0';
2167 if (name_only) {
2168 printf("%s ", nd->s);
2169 continue;
2170 }
2171 nd2 = strlist__next(nd);
2172 if (nd2) {
2173 ptr = strchr(nd2->s, '@');
2174 if (ptr)
2175 *ptr = '\0';
2176 if (strcmp(nd->s, nd2->s) == 0)
2177 show_detail = true;
2178 }
2179 if (show_detail) {
2180 path = build_id_cache__origname(buf);
2181 ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf);
2182 if (ret > 0) {
2183 printf(" %-50s [%s]\n", buf, "SDT event");
2184 free(buf);
2185 }
2186 } else
2187 printf(" %-50s [%s]\n", nd->s, "SDT event");
2188 if (nd2) {
2189 if (strcmp(nd->s, nd2->s) != 0)
2190 show_detail = false;
2191 if (ptr)
2192 *ptr = '@';
2193 }
2194 }
2195 strlist__delete(sdtlist);
2196}
2197
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002198int print_hwcache_events(const char *event_glob, bool name_only)
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002199{
Yunlong Songab0e4802015-02-27 18:21:25 +08002200 unsigned int type, op, i, evt_i = 0, evt_num = 0;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03002201 char name[64];
Yunlong Songab0e4802015-02-27 18:21:25 +08002202 char **evt_list = NULL;
2203 bool evt_num_known = false;
2204
2205restart:
2206 if (evt_num_known) {
2207 evt_list = zalloc(sizeof(char *) * evt_num);
2208 if (!evt_list)
2209 goto out_enomem;
2210 }
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002211
2212 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
2213 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
2214 /* skip invalid cache type */
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03002215 if (!perf_evsel__is_cache_op_valid(type, op))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002216 continue;
2217
2218 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03002219 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
2220 name, sizeof(name));
Ingo Molnar947b4ad2011-04-29 22:52:42 +02002221 if (event_glob != NULL && !strglobmatch(name, event_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002222 continue;
2223
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002224 if (!is_event_supported(PERF_TYPE_HW_CACHE,
2225 type | (op << 8) | (i << 16)))
2226 continue;
2227
Yunlong Songab0e4802015-02-27 18:21:25 +08002228 if (!evt_num_known) {
2229 evt_num++;
2230 continue;
2231 }
2232
2233 evt_list[evt_i] = strdup(name);
2234 if (evt_list[evt_i] == NULL)
2235 goto out_enomem;
2236 evt_i++;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002237 }
2238 }
2239 }
2240
Yunlong Songab0e4802015-02-27 18:21:25 +08002241 if (!evt_num_known) {
2242 evt_num_known = true;
2243 goto restart;
2244 }
2245 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2246 evt_i = 0;
2247 while (evt_i < evt_num) {
2248 if (name_only) {
2249 printf("%s ", evt_list[evt_i++]);
2250 continue;
2251 }
2252 printf(" %-50s [%s]\n", evt_list[evt_i++],
2253 event_type_descriptors[PERF_TYPE_HW_CACHE]);
2254 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002255 if (evt_num && pager_in_use())
Andi Kleendc098b32013-04-20 11:02:29 -07002256 printf("\n");
Yunlong Songab0e4802015-02-27 18:21:25 +08002257
2258out_free:
2259 evt_num = evt_i;
2260 for (evt_i = 0; evt_i < evt_num; evt_i++)
2261 zfree(&evt_list[evt_i]);
2262 zfree(&evt_list);
2263 return evt_num;
2264
2265out_enomem:
2266 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
2267 if (evt_list)
2268 goto out_free;
2269 return evt_num;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002270}
2271
Yunlong Song705750f2015-02-27 18:21:27 +08002272void print_symbol_events(const char *event_glob, unsigned type,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002273 struct event_symbol *syms, unsigned max,
2274 bool name_only)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002275{
Yunlong Songab0e4802015-02-27 18:21:25 +08002276 unsigned int i, evt_i = 0, evt_num = 0;
Ingo Molnar947b4ad2011-04-29 22:52:42 +02002277 char name[MAX_NAME_LEN];
Yunlong Songab0e4802015-02-27 18:21:25 +08002278 char **evt_list = NULL;
2279 bool evt_num_known = false;
2280
2281restart:
2282 if (evt_num_known) {
2283 evt_list = zalloc(sizeof(char *) * evt_num);
2284 if (!evt_list)
2285 goto out_enomem;
2286 syms -= max;
2287 }
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002288
Jiri Olsa1dc127602012-07-04 00:00:44 +02002289 for (i = 0; i < max; i++, syms++) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002290
Arnaldo Carvalho de Meloe37df6c2015-11-27 16:04:58 -03002291 if (event_glob != NULL && syms->symbol != NULL &&
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002292 !(strglobmatch(syms->symbol, event_glob) ||
2293 (syms->alias && strglobmatch(syms->alias, event_glob))))
2294 continue;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002295
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002296 if (!is_event_supported(type, i))
2297 continue;
2298
Yunlong Songab0e4802015-02-27 18:21:25 +08002299 if (!evt_num_known) {
2300 evt_num++;
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002301 continue;
2302 }
2303
Yunlong Songab0e4802015-02-27 18:21:25 +08002304 if (!name_only && strlen(syms->alias))
Ingo Molnar947b4ad2011-04-29 22:52:42 +02002305 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05302306 else
Ingo Molnar947b4ad2011-04-29 22:52:42 +02002307 strncpy(name, syms->symbol, MAX_NAME_LEN);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002308
Yunlong Songab0e4802015-02-27 18:21:25 +08002309 evt_list[evt_i] = strdup(name);
2310 if (evt_list[evt_i] == NULL)
2311 goto out_enomem;
2312 evt_i++;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002313 }
2314
Yunlong Songab0e4802015-02-27 18:21:25 +08002315 if (!evt_num_known) {
2316 evt_num_known = true;
2317 goto restart;
2318 }
2319 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2320 evt_i = 0;
2321 while (evt_i < evt_num) {
2322 if (name_only) {
2323 printf("%s ", evt_list[evt_i++]);
2324 continue;
2325 }
2326 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
2327 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002328 if (evt_num && pager_in_use())
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002329 printf("\n");
Yunlong Songab0e4802015-02-27 18:21:25 +08002330
2331out_free:
2332 evt_num = evt_i;
2333 for (evt_i = 0; evt_i < evt_num; evt_i++)
2334 zfree(&evt_list[evt_i]);
2335 zfree(&evt_list);
2336 return;
2337
2338out_enomem:
2339 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
2340 if (evt_list)
2341 goto out_free;
Jiri Olsa1dc127602012-07-04 00:00:44 +02002342}
2343
2344/*
2345 * Print the help text for the event symbols:
2346 */
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -07002347void print_events(const char *event_glob, bool name_only, bool quiet_flag,
Andi Kleenbf874fc2017-03-20 13:17:11 -07002348 bool long_desc, bool details_flag)
Jiri Olsa1dc127602012-07-04 00:00:44 +02002349{
Jiri Olsa1dc127602012-07-04 00:00:44 +02002350 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002351 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
Jiri Olsa1dc127602012-07-04 00:00:44 +02002352
2353 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002354 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
Jiri Olsa1dc127602012-07-04 00:00:44 +02002355
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002356 print_hwcache_events(event_glob, name_only);
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002357
Andi Kleenbf874fc2017-03-20 13:17:11 -07002358 print_pmu_events(event_glob, name_only, quiet_flag, long_desc,
2359 details_flag);
Andi Kleendc098b32013-04-20 11:02:29 -07002360
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002361 if (event_glob != NULL)
2362 return;
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +05302363
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002364 if (!name_only) {
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002365 printf(" %-50s [%s]\n",
2366 "rNNN",
2367 event_type_descriptors[PERF_TYPE_RAW]);
2368 printf(" %-50s [%s]\n",
2369 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
2370 event_type_descriptors[PERF_TYPE_RAW]);
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002371 if (pager_in_use())
2372 printf(" (see 'man perf-list' on how to encode it)\n\n");
Thomas Gleixner86847b62009-06-06 12:24:17 +02002373
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002374 printf(" %-50s [%s]\n",
Jacob Shin3741eb92014-05-29 17:26:51 +02002375 "mem:<addr>[/len][:access]",
Liming Wang41bdcb22009-12-29 16:37:07 +08002376 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002377 if (pager_in_use())
2378 printf("\n");
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002379 }
Frederic Weisbecker1b290d62009-11-23 15:42:35 +01002380
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002381 print_tracepoint_events(NULL, NULL, name_only);
Masami Hiramatsu40218da2016-06-24 18:06:46 +09002382
2383 print_sdt_events(NULL, NULL, name_only);
Andi Kleen71b0acc2017-08-31 12:40:32 -07002384
2385 metricgroup__print(true, true, NULL, name_only);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002386}
Jiri Olsa8f707d82012-03-15 20:09:16 +01002387
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002388int parse_events__is_hardcoded_term(struct parse_events_term *term)
Jiri Olsa8f707d82012-03-15 20:09:16 +01002389{
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002390 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002391}
2392
Jiri Olsa67b49b32017-02-17 15:00:55 +01002393static int new_term(struct parse_events_term **_term,
2394 struct parse_events_term *temp,
2395 char *str, u64 num)
Jiri Olsa8f707d82012-03-15 20:09:16 +01002396{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002397 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002398
Jiri Olsa67b49b32017-02-17 15:00:55 +01002399 term = malloc(sizeof(*term));
Jiri Olsa8f707d82012-03-15 20:09:16 +01002400 if (!term)
2401 return -ENOMEM;
2402
Jiri Olsa67b49b32017-02-17 15:00:55 +01002403 *term = *temp;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002404 INIT_LIST_HEAD(&term->list);
Jiri Olsa8f707d82012-03-15 20:09:16 +01002405
Jiri Olsa67b49b32017-02-17 15:00:55 +01002406 switch (term->type_val) {
Jiri Olsa8f707d82012-03-15 20:09:16 +01002407 case PARSE_EVENTS__TERM_TYPE_NUM:
2408 term->val.num = num;
2409 break;
2410 case PARSE_EVENTS__TERM_TYPE_STR:
2411 term->val.str = str;
2412 break;
2413 default:
Adrian Hunter4be8be62013-07-04 16:20:24 +03002414 free(term);
Jiri Olsa8f707d82012-03-15 20:09:16 +01002415 return -EINVAL;
2416 }
2417
2418 *_term = term;
2419 return 0;
2420}
2421
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002422int parse_events_term__num(struct parse_events_term **term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002423 int type_term, char *config, u64 num,
Jiri Olsa99e71382017-02-17 15:00:56 +01002424 bool no_value,
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002425 void *loc_term_, void *loc_val_)
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002426{
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002427 YYLTYPE *loc_term = loc_term_;
2428 YYLTYPE *loc_val = loc_val_;
2429
Jiri Olsa67b49b32017-02-17 15:00:55 +01002430 struct parse_events_term temp = {
2431 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
2432 .type_term = type_term,
2433 .config = config,
Jiri Olsa99e71382017-02-17 15:00:56 +01002434 .no_value = no_value,
Jiri Olsa67b49b32017-02-17 15:00:55 +01002435 .err_term = loc_term ? loc_term->first_column : 0,
2436 .err_val = loc_val ? loc_val->first_column : 0,
2437 };
2438
2439 return new_term(term, &temp, NULL, num);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002440}
2441
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002442int parse_events_term__str(struct parse_events_term **term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002443 int type_term, char *config, char *str,
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002444 void *loc_term_, void *loc_val_)
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002445{
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002446 YYLTYPE *loc_term = loc_term_;
2447 YYLTYPE *loc_val = loc_val_;
2448
Jiri Olsa67b49b32017-02-17 15:00:55 +01002449 struct parse_events_term temp = {
2450 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
2451 .type_term = type_term,
2452 .config = config,
2453 .err_term = loc_term ? loc_term->first_column : 0,
2454 .err_val = loc_val ? loc_val->first_column : 0,
2455 };
2456
2457 return new_term(term, &temp, str, 0);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002458}
2459
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002460int parse_events_term__sym_hw(struct parse_events_term **term,
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02002461 char *config, unsigned idx)
2462{
2463 struct event_symbol *sym;
Jiri Olsa67b49b32017-02-17 15:00:55 +01002464 struct parse_events_term temp = {
2465 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
2466 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
2467 .config = config ?: (char *) "event",
2468 };
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02002469
2470 BUG_ON(idx >= PERF_COUNT_HW_MAX);
2471 sym = &event_symbols_hw[idx];
2472
Jiri Olsa67b49b32017-02-17 15:00:55 +01002473 return new_term(term, &temp, (char *) sym->symbol, 0);
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02002474}
2475
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002476int parse_events_term__clone(struct parse_events_term **new,
2477 struct parse_events_term *term)
Zheng Yana6146d52012-06-15 14:31:41 +08002478{
Jiri Olsa67b49b32017-02-17 15:00:55 +01002479 struct parse_events_term temp = {
2480 .type_val = term->type_val,
2481 .type_term = term->type_term,
2482 .config = term->config,
2483 .err_term = term->err_term,
2484 .err_val = term->err_val,
2485 };
2486
2487 return new_term(new, &temp, term->val.str, term->val.num);
Zheng Yana6146d52012-06-15 14:31:41 +08002488}
2489
Andi Kleen82557182017-03-20 13:17:03 -07002490int parse_events_copy_term_list(struct list_head *old,
2491 struct list_head **new)
2492{
2493 struct parse_events_term *term, *n;
2494 int ret;
2495
2496 if (!old) {
2497 *new = NULL;
2498 return 0;
2499 }
2500
2501 *new = malloc(sizeof(struct list_head));
2502 if (!*new)
2503 return -ENOMEM;
2504 INIT_LIST_HEAD(*new);
2505
2506 list_for_each_entry (term, old, list) {
2507 ret = parse_events_term__clone(&n, term);
2508 if (ret)
2509 return ret;
2510 list_add_tail(&n->list, *new);
2511 }
2512 return 0;
2513}
2514
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002515void parse_events_terms__purge(struct list_head *terms)
Jiri Olsa8f707d82012-03-15 20:09:16 +01002516{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002517 struct parse_events_term *term, *h;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002518
Wang Nana8adfce2016-02-12 16:31:23 -03002519 list_for_each_entry_safe(term, h, terms, list) {
Wang Nan2d055bf2016-02-22 09:10:34 +00002520 if (term->array.nr_ranges)
Taeung Song360e0712017-02-01 21:34:07 +09002521 zfree(&term->array.ranges);
Wang Nana8adfce2016-02-12 16:31:23 -03002522 list_del_init(&term->list);
Jiri Olsa8f707d82012-03-15 20:09:16 +01002523 free(term);
Wang Nana8adfce2016-02-12 16:31:23 -03002524 }
Jiri Olsa8f707d82012-03-15 20:09:16 +01002525}
Jiri Olsab39b8392015-04-22 21:10:16 +02002526
Arnaldo Carvalho de Melo2146afc2016-02-12 17:09:17 -03002527void parse_events_terms__delete(struct list_head *terms)
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002528{
Arnaldo Carvalho de Melo2146afc2016-02-12 17:09:17 -03002529 if (!terms)
2530 return;
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002531 parse_events_terms__purge(terms);
Wang Nand20a5f22016-02-12 17:01:17 -03002532 free(terms);
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002533}
2534
Wang Nan2d055bf2016-02-22 09:10:34 +00002535void parse_events__clear_array(struct parse_events_array *a)
2536{
Taeung Song360e0712017-02-01 21:34:07 +09002537 zfree(&a->ranges);
Wang Nan2d055bf2016-02-22 09:10:34 +00002538}
2539
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03002540void parse_events_evlist_error(struct parse_events_state *parse_state,
Jiri Olsab39b8392015-04-22 21:10:16 +02002541 int idx, const char *str)
2542{
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03002543 struct parse_events_error *err = parse_state->error;
Jiri Olsab39b8392015-04-22 21:10:16 +02002544
Adrian Huntera6ced2b2015-05-19 16:05:44 +03002545 if (!err)
2546 return;
Jiri Olsab39b8392015-04-22 21:10:16 +02002547 err->idx = idx;
2548 err->str = strdup(str);
2549 WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
2550}
He Kuangffeb8832015-09-28 03:52:14 +00002551
Wang Nan17cb5f82016-02-19 11:43:57 +00002552static void config_terms_list(char *buf, size_t buf_sz)
2553{
2554 int i;
2555 bool first = true;
2556
2557 buf[0] = '\0';
2558 for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
2559 const char *name = config_term_names[i];
2560
Wang Nan1669e502016-02-19 11:43:58 +00002561 if (!config_term_avail(i, NULL))
2562 continue;
Wang Nan17cb5f82016-02-19 11:43:57 +00002563 if (!name)
2564 continue;
2565 if (name[0] == '<')
2566 continue;
2567
2568 if (strlen(buf) + strlen(name) + 2 >= buf_sz)
2569 return;
2570
2571 if (!first)
2572 strcat(buf, ",");
2573 else
2574 first = false;
2575 strcat(buf, name);
2576 }
2577}
2578
He Kuangffeb8832015-09-28 03:52:14 +00002579/*
2580 * Return string contains valid config terms of an event.
2581 * @additional_terms: For terms such as PMU sysfs terms.
2582 */
2583char *parse_events_formats_error_string(char *additional_terms)
2584{
2585 char *str;
Wang Nan626a6b72016-07-14 08:34:45 +00002586 /* "no-overwrite" is the longest name */
Wang Nan17cb5f82016-02-19 11:43:57 +00002587 char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
Wang Nan626a6b72016-07-14 08:34:45 +00002588 (sizeof("no-overwrite") - 1)];
He Kuangffeb8832015-09-28 03:52:14 +00002589
Wang Nan17cb5f82016-02-19 11:43:57 +00002590 config_terms_list(static_terms, sizeof(static_terms));
He Kuangffeb8832015-09-28 03:52:14 +00002591 /* valid terms */
2592 if (additional_terms) {
Wang Nan26dee022016-02-19 11:43:52 +00002593 if (asprintf(&str, "valid terms: %s,%s",
2594 additional_terms, static_terms) < 0)
He Kuangffeb8832015-09-28 03:52:14 +00002595 goto fail;
2596 } else {
Wang Nan26dee022016-02-19 11:43:52 +00002597 if (asprintf(&str, "valid terms: %s", static_terms) < 0)
He Kuangffeb8832015-09-28 03:52:14 +00002598 goto fail;
2599 }
2600 return str;
2601
2602fail:
2603 return NULL;
2604}