blob: a5dd6703a56b9d4002446bdf7b946c9b18ac40af [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>
Ingo Molnar8ad8db32009-05-26 11:10:09 +02003#include "util.h"
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03004#include "../perf.h"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02005#include "evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02006#include "evsel.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06007#include <subcmd/parse-options.h>
Ingo Molnar8ad8db32009-05-26 11:10:09 +02008#include "parse-events.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06009#include <subcmd/exec-cmd.h>
Kan Liang42f60c22014-10-07 11:08:49 -040010#include "string.h"
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -030011#include "symbol.h"
Jason Baron5beeded2009-07-21 14:16:29 -040012#include "cache.h"
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020013#include "header.h"
Wang Nan84c86ca2015-10-14 12:41:14 +000014#include "bpf-loader.h"
Masami Hiramatsu6e81c742014-08-14 02:22:36 +000015#include "debug.h"
Jiri Olsa592d5a62015-09-02 09:56:34 +020016#include <api/fs/tracing_path.h>
Zheng Yanac20de6f2012-06-15 14:31:39 +080017#include "parse-events-bison.h"
Jiri Olsa90e2b222012-06-15 14:31:40 +080018#define YY_EXTRA_TYPE int
Jiri Olsa89812fc2012-03-15 20:09:15 +010019#include "parse-events-flex.h"
Jiri Olsa5f537a22012-03-15 20:09:18 +010020#include "pmu.h"
Namhyung Kimb41f1ce2013-08-27 11:41:53 +090021#include "thread_map.h"
Jiri Olsaf30a79b2015-06-23 00:36:04 +020022#include "cpumap.h"
Jiri Olsab39b8392015-04-22 21:10:16 +020023#include "asm/bug.h"
Jiri Olsa89812fc2012-03-15 20:09:15 +010024
25#define MAX_NAME_LEN 100
Ingo Molnar8ad8db32009-05-26 11:10:09 +020026
Jiri Olsa82ba1f22012-05-21 09:12:50 +020027#ifdef PARSER_DEBUG
28extern int parse_events_debug;
29#endif
Zheng Yanac20de6f2012-06-15 14:31:39 +080030int parse_events_parse(void *data, void *scanner);
He Kuange637d172015-09-28 03:52:16 +000031static int get_config_terms(struct list_head *head_config,
32 struct list_head *head_terms __maybe_unused);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +020033
Kan Liangdcb4e102014-10-07 11:08:50 -040034static struct perf_pmu_event_symbol *perf_pmu_events_list;
35/*
36 * The variable indicates the number of supported pmu event symbols.
37 * 0 means not initialized and ready to init
38 * -1 means failed to init, don't try anymore
39 * >0 is the number of supported pmu event symbols
40 */
41static int perf_pmu_events_list_num;
42
Yunlong Song705750f2015-02-27 18:21:27 +080043struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
Jiri Olsa1dc127602012-07-04 00:00:44 +020044 [PERF_COUNT_HW_CPU_CYCLES] = {
45 .symbol = "cpu-cycles",
46 .alias = "cycles",
47 },
48 [PERF_COUNT_HW_INSTRUCTIONS] = {
49 .symbol = "instructions",
50 .alias = "",
51 },
52 [PERF_COUNT_HW_CACHE_REFERENCES] = {
53 .symbol = "cache-references",
54 .alias = "",
55 },
56 [PERF_COUNT_HW_CACHE_MISSES] = {
57 .symbol = "cache-misses",
58 .alias = "",
59 },
60 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
61 .symbol = "branch-instructions",
62 .alias = "branches",
63 },
64 [PERF_COUNT_HW_BRANCH_MISSES] = {
65 .symbol = "branch-misses",
66 .alias = "",
67 },
68 [PERF_COUNT_HW_BUS_CYCLES] = {
69 .symbol = "bus-cycles",
70 .alias = "",
71 },
72 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
73 .symbol = "stalled-cycles-frontend",
74 .alias = "idle-cycles-frontend",
75 },
76 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
77 .symbol = "stalled-cycles-backend",
78 .alias = "idle-cycles-backend",
79 },
80 [PERF_COUNT_HW_REF_CPU_CYCLES] = {
81 .symbol = "ref-cycles",
82 .alias = "",
83 },
84};
Ingo Molnar8ad8db32009-05-26 11:10:09 +020085
Yunlong Song705750f2015-02-27 18:21:27 +080086struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
Jiri Olsa1dc127602012-07-04 00:00:44 +020087 [PERF_COUNT_SW_CPU_CLOCK] = {
88 .symbol = "cpu-clock",
89 .alias = "",
90 },
91 [PERF_COUNT_SW_TASK_CLOCK] = {
92 .symbol = "task-clock",
93 .alias = "",
94 },
95 [PERF_COUNT_SW_PAGE_FAULTS] = {
96 .symbol = "page-faults",
97 .alias = "faults",
98 },
99 [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
100 .symbol = "context-switches",
101 .alias = "cs",
102 },
103 [PERF_COUNT_SW_CPU_MIGRATIONS] = {
104 .symbol = "cpu-migrations",
105 .alias = "migrations",
106 },
107 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
108 .symbol = "minor-faults",
109 .alias = "",
110 },
111 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
112 .symbol = "major-faults",
113 .alias = "",
114 },
115 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
116 .symbol = "alignment-faults",
117 .alias = "",
118 },
119 [PERF_COUNT_SW_EMULATION_FAULTS] = {
120 .symbol = "emulation-faults",
121 .alias = "",
122 },
Adrian Hunterd22d1a22013-08-31 21:50:52 +0300123 [PERF_COUNT_SW_DUMMY] = {
124 .symbol = "dummy",
125 .alias = "",
126 },
Arnaldo Carvalho de Melobae9cc42015-11-27 15:54:33 -0300127 [PERF_COUNT_SW_BPF_OUTPUT] = {
128 .symbol = "bpf-output",
129 .alias = "",
130 },
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200131};
132
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200133#define __PERF_EVENT_FIELD(config, name) \
134 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
Ingo Molnar52425192009-05-26 09:17:18 +0200135
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200136#define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200137#define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200138#define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200139#define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
Ingo Molnar52425192009-05-26 09:17:18 +0200140
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300141#define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400142 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300143 if (sys_dirent.d_type == DT_DIR && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400144 (strcmp(sys_dirent.d_name, ".")) && \
145 (strcmp(sys_dirent.d_name, "..")))
146
Peter Zijlstraae07b632009-08-06 16:48:54 +0200147static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
148{
149 char evt_path[MAXPATHLEN];
150 int fd;
151
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200152 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
Peter Zijlstraae07b632009-08-06 16:48:54 +0200153 sys_dir->d_name, evt_dir->d_name);
154 fd = open(evt_path, O_RDONLY);
155 if (fd < 0)
156 return -EINVAL;
157 close(fd);
158
159 return 0;
160}
161
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300162#define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400163 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300164 if (evt_dirent.d_type == DT_DIR && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400165 (strcmp(evt_dirent.d_name, ".")) && \
Peter Zijlstraae07b632009-08-06 16:48:54 +0200166 (strcmp(evt_dirent.d_name, "..")) && \
167 (!tp_event_has_id(&sys_dirent, &evt_dirent)))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400168
Li Zefan270bbbe2009-09-17 16:34:51 +0800169#define MAX_EVENT_LENGTH 512
Jason Baronf6bdafe2009-07-21 12:20:22 -0400170
Jason Baronf6bdafe2009-07-21 12:20:22 -0400171
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200172struct tracepoint_path *tracepoint_id_to_path(u64 config)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400173{
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200174 struct tracepoint_path *path = NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400175 DIR *sys_dir, *evt_dir;
176 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Stephane Eranian8aa8a7c2012-03-13 16:51:02 +0100177 char id_buf[24];
Eric Dumazet725b1362009-09-24 15:39:09 +0200178 int fd;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400179 u64 id;
180 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +0200181 char dir_path[MAXPATHLEN];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400182
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200183 sys_dir = opendir(tracing_events_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400184 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200185 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400186
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300187 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200188
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200189 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Eric Dumazet725b1362009-09-24 15:39:09 +0200190 sys_dirent.d_name);
191 evt_dir = opendir(dir_path);
192 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300193 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200194
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300195 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200196
197 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
Jason Baronf6bdafe2009-07-21 12:20:22 -0400198 evt_dirent.d_name);
Eric Dumazet725b1362009-09-24 15:39:09 +0200199 fd = open(evt_path, O_RDONLY);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400200 if (fd < 0)
201 continue;
202 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
203 close(fd);
204 continue;
205 }
206 close(fd);
207 id = atoll(id_buf);
208 if (id == config) {
209 closedir(evt_dir);
210 closedir(sys_dir);
Julia Lawall59b4cae2009-12-06 10:16:30 +0100211 path = zalloc(sizeof(*path));
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200212 path->system = malloc(MAX_EVENT_LENGTH);
213 if (!path->system) {
214 free(path);
215 return NULL;
216 }
217 path->name = malloc(MAX_EVENT_LENGTH);
218 if (!path->name) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300219 zfree(&path->system);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200220 free(path);
221 return NULL;
222 }
223 strncpy(path->system, sys_dirent.d_name,
224 MAX_EVENT_LENGTH);
225 strncpy(path->name, evt_dirent.d_name,
226 MAX_EVENT_LENGTH);
227 return path;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400228 }
229 }
230 closedir(evt_dir);
231 }
232
Jason Baronf6bdafe2009-07-21 12:20:22 -0400233 closedir(sys_dir);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200234 return NULL;
235}
236
Namhyung Kime7c93f02013-06-26 16:14:05 +0900237struct tracepoint_path *tracepoint_name_to_path(const char *name)
238{
239 struct tracepoint_path *path = zalloc(sizeof(*path));
240 char *str = strchr(name, ':');
241
242 if (path == NULL || str == NULL) {
243 free(path);
244 return NULL;
245 }
246
247 path->system = strndup(name, str - name);
248 path->name = strdup(str+1);
249
250 if (path->system == NULL || path->name == NULL) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300251 zfree(&path->system);
252 zfree(&path->name);
Namhyung Kime7c93f02013-06-26 16:14:05 +0900253 free(path);
254 path = NULL;
255 }
256
257 return path;
258}
259
David Ahern1424dc92011-03-09 22:23:28 -0700260const char *event_type(int type)
261{
262 switch (type) {
263 case PERF_TYPE_HARDWARE:
264 return "hardware";
265
266 case PERF_TYPE_SOFTWARE:
267 return "software";
268
269 case PERF_TYPE_TRACEPOINT:
270 return "tracepoint";
271
272 case PERF_TYPE_HW_CACHE:
273 return "hardware-cache";
274
275 default:
276 break;
277 }
278
279 return "unknown";
280}
281
Wang Nane814fdd2016-02-19 11:43:59 +0000282static int parse_events__is_name_term(struct parse_events_term *term)
283{
284 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
285}
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800286
Wang Nane814fdd2016-02-19 11:43:59 +0000287static char *get_config_name(struct list_head *head_terms)
288{
289 struct parse_events_term *term;
290
291 if (!head_terms)
292 return NULL;
293
294 list_for_each_entry(term, head_terms, list)
295 if (parse_events__is_name_term(term))
296 return term->val.str;
297
298 return NULL;
299}
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800300
Stephane Eranian410136f2013-11-12 17:58:49 +0100301static struct perf_evsel *
302__add_event(struct list_head *list, int *idx,
303 struct perf_event_attr *attr,
Jiri Olsa930a2e22015-07-29 05:42:10 -0400304 char *name, struct cpu_map *cpus,
305 struct list_head *config_terms)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100306{
307 struct perf_evsel *evsel;
308
309 event_attr_init(attr);
310
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300311 evsel = perf_evsel__new_idx(attr, (*idx)++);
David Ahernc5cd8ac2013-07-02 13:27:25 -0600312 if (!evsel)
Stephane Eranian410136f2013-11-12 17:58:49 +0100313 return NULL;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100314
Adrian Hunterfce4d292015-09-08 10:58:55 +0300315 evsel->cpus = cpu_map__get(cpus);
316 evsel->own_cpus = cpu_map__get(cpus);
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200317
Arnaldo Carvalho de Melo9db17632012-06-12 13:45:00 -0300318 if (name)
319 evsel->name = strdup(name);
Jiri Olsa930a2e22015-07-29 05:42:10 -0400320
321 if (config_terms)
322 list_splice(config_terms, &evsel->config_terms);
323
Jiri Olsab847cbd2012-05-21 09:12:51 +0200324 list_add_tail(&evsel->node, list);
Stephane Eranian410136f2013-11-12 17:58:49 +0100325 return evsel;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100326}
327
David Ahernc5cd8ac2013-07-02 13:27:25 -0600328static int add_event(struct list_head *list, int *idx,
Jiri Olsa930a2e22015-07-29 05:42:10 -0400329 struct perf_event_attr *attr, char *name,
330 struct list_head *config_terms)
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800331{
Jiri Olsa930a2e22015-07-29 05:42:10 -0400332 return __add_event(list, idx, attr, name, NULL, config_terms) ? 0 : -ENOMEM;
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800333}
334
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300335static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
Ingo Molnar8326f442009-06-05 20:22:46 +0200336{
337 int i, j;
Paul Mackerras61c45982009-07-01 13:04:34 +1000338 int n, longest = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200339
340 for (i = 0; i < size; i++) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300341 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
Paul Mackerras61c45982009-07-01 13:04:34 +1000342 n = strlen(names[i][j]);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100343 if (n > longest && !strncasecmp(str, names[i][j], n))
Paul Mackerras61c45982009-07-01 13:04:34 +1000344 longest = n;
345 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100346 if (longest > 0)
Paul Mackerras61c45982009-07-01 13:04:34 +1000347 return i;
Ingo Molnar8326f442009-06-05 20:22:46 +0200348 }
349
Ingo Molnar89536452009-06-06 21:04:17 +0200350 return -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200351}
352
Wang Nan43d0b972016-02-19 11:44:01 +0000353typedef int config_term_func_t(struct perf_event_attr *attr,
354 struct parse_events_term *term,
355 struct parse_events_error *err);
356static int config_term_common(struct perf_event_attr *attr,
357 struct parse_events_term *term,
358 struct parse_events_error *err);
359static int config_attr(struct perf_event_attr *attr,
360 struct list_head *head,
361 struct parse_events_error *err,
362 config_term_func_t config_term);
363
David Ahernc5cd8ac2013-07-02 13:27:25 -0600364int parse_events_add_cache(struct list_head *list, int *idx,
Wang Nan43d0b972016-02-19 11:44:01 +0000365 char *type, char *op_result1, char *op_result2,
Arnaldo Carvalho de Melo2c97b0d2016-02-19 19:47:04 -0300366 struct parse_events_error *err,
Wang Nan43d0b972016-02-19 11:44:01 +0000367 struct list_head *head_config)
Ingo Molnar8326f442009-06-05 20:22:46 +0200368{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100369 struct perf_event_attr attr;
Wang Nan43d0b972016-02-19 11:44:01 +0000370 LIST_HEAD(config_terms);
371 char name[MAX_NAME_LEN], *config_name;
Paul Mackerras61c45982009-07-01 13:04:34 +1000372 int cache_type = -1, cache_op = -1, cache_result = -1;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100373 char *op_result[2] = { op_result1, op_result2 };
374 int i, n;
Ingo Molnar8326f442009-06-05 20:22:46 +0200375
Ingo Molnar8326f442009-06-05 20:22:46 +0200376 /*
377 * No fallback - if we cannot get a clear cache type
378 * then bail out:
379 */
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300380 cache_type = parse_aliases(type, perf_evsel__hw_cache,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100381 PERF_COUNT_HW_CACHE_MAX);
Ingo Molnar8326f442009-06-05 20:22:46 +0200382 if (cache_type == -1)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100383 return -EINVAL;
Ingo Molnar8326f442009-06-05 20:22:46 +0200384
Wang Nan43d0b972016-02-19 11:44:01 +0000385 config_name = get_config_name(head_config);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100386 n = snprintf(name, MAX_NAME_LEN, "%s", type);
387
388 for (i = 0; (i < 2) && (op_result[i]); i++) {
389 char *str = op_result[i];
390
Jiri Olsa275ef382012-09-05 19:51:33 +0200391 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
Paul Mackerras61c45982009-07-01 13:04:34 +1000392
393 if (cache_op == -1) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300394 cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100395 PERF_COUNT_HW_CACHE_OP_MAX);
Paul Mackerras61c45982009-07-01 13:04:34 +1000396 if (cache_op >= 0) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300397 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
Jiri Olsa89812fc2012-03-15 20:09:15 +0100398 return -EINVAL;
Paul Mackerras61c45982009-07-01 13:04:34 +1000399 continue;
400 }
401 }
402
403 if (cache_result == -1) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300404 cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
405 PERF_COUNT_HW_CACHE_RESULT_MAX);
Paul Mackerras61c45982009-07-01 13:04:34 +1000406 if (cache_result >= 0)
407 continue;
408 }
Paul Mackerras61c45982009-07-01 13:04:34 +1000409 }
410
Ingo Molnar8326f442009-06-05 20:22:46 +0200411 /*
412 * Fall back to reads:
413 */
Ingo Molnar89536452009-06-06 21:04:17 +0200414 if (cache_op == -1)
415 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
Ingo Molnar8326f442009-06-05 20:22:46 +0200416
Ingo Molnar8326f442009-06-05 20:22:46 +0200417 /*
418 * Fall back to accesses:
419 */
420 if (cache_result == -1)
421 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
422
Jiri Olsa89812fc2012-03-15 20:09:15 +0100423 memset(&attr, 0, sizeof(attr));
424 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
425 attr.type = PERF_TYPE_HW_CACHE;
Wang Nan43d0b972016-02-19 11:44:01 +0000426
427 if (head_config) {
Arnaldo Carvalho de Melo2c97b0d2016-02-19 19:47:04 -0300428 if (config_attr(&attr, head_config, err,
Wang Nan43d0b972016-02-19 11:44:01 +0000429 config_term_common))
430 return -EINVAL;
431
432 if (get_config_terms(head_config, &config_terms))
433 return -ENOMEM;
434 }
435 return add_event(list, idx, &attr, config_name ? : name, &config_terms);
Ingo Molnar8326f442009-06-05 20:22:46 +0200436}
437
Jiri Olsa272ed292015-09-29 17:05:31 +0200438static void tracepoint_error(struct parse_events_error *e, int err,
Jiri Olsa19658172015-09-07 10:38:07 +0200439 char *sys, char *name)
440{
441 char help[BUFSIZ];
442
Adrian Hunterec183d22016-01-26 14:05:20 +0200443 if (!e)
444 return;
445
Jiri Olsa19658172015-09-07 10:38:07 +0200446 /*
447 * We get error directly from syscall errno ( > 0),
448 * or from encoded pointer's error ( < 0).
449 */
450 err = abs(err);
451
452 switch (err) {
453 case EACCES:
Jiri Olsa272ed292015-09-29 17:05:31 +0200454 e->str = strdup("can't access trace events");
Jiri Olsa19658172015-09-07 10:38:07 +0200455 break;
456 case ENOENT:
Jiri Olsa272ed292015-09-29 17:05:31 +0200457 e->str = strdup("unknown tracepoint");
Jiri Olsa19658172015-09-07 10:38:07 +0200458 break;
459 default:
Jiri Olsa272ed292015-09-29 17:05:31 +0200460 e->str = strdup("failed to add tracepoint");
Jiri Olsa19658172015-09-07 10:38:07 +0200461 break;
462 }
463
464 tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
Jiri Olsa272ed292015-09-29 17:05:31 +0200465 e->help = strdup(help);
Jiri Olsa19658172015-09-07 10:38:07 +0200466}
467
David Ahernc5cd8ac2013-07-02 13:27:25 -0600468static int add_tracepoint(struct list_head *list, int *idx,
Jiri Olsae2f9f8e2015-09-07 10:38:05 +0200469 char *sys_name, char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200470 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000471 struct list_head *head_config)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200472{
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300473 struct perf_evsel *evsel;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200474
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300475 evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
Jiri Olsa19658172015-09-07 10:38:07 +0200476 if (IS_ERR(evsel)) {
Jiri Olsa272ed292015-09-29 17:05:31 +0200477 tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200478 return PTR_ERR(evsel);
Jiri Olsa19658172015-09-07 10:38:07 +0200479 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200480
He Kuange637d172015-09-28 03:52:16 +0000481 if (head_config) {
482 LIST_HEAD(config_terms);
483
484 if (get_config_terms(head_config, &config_terms))
485 return -ENOMEM;
486 list_splice(&config_terms, &evsel->config_terms);
487 }
488
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300489 list_add_tail(&evsel->node, list);
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300490 return 0;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200491}
492
David Ahernc5cd8ac2013-07-02 13:27:25 -0600493static int add_tracepoint_multi_event(struct list_head *list, int *idx,
Jiri Olsae2f9f8e2015-09-07 10:38:05 +0200494 char *sys_name, char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200495 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000496 struct list_head *head_config)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200497{
498 char evt_path[MAXPATHLEN];
499 struct dirent *evt_ent;
500 DIR *evt_dir;
Jiri Olsa27bf90b2015-10-05 21:31:17 +0200501 int ret = 0, found = 0;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200502
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200503 snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200504 evt_dir = opendir(evt_path);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200505 if (!evt_dir) {
Jiri Olsa272ed292015-09-29 17:05:31 +0200506 tracepoint_error(err, errno, sys_name, evt_name);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100507 return -1;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200508 }
509
Jiri Olsa89812fc2012-03-15 20:09:15 +0100510 while (!ret && (evt_ent = readdir(evt_dir))) {
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200511 if (!strcmp(evt_ent->d_name, ".")
512 || !strcmp(evt_ent->d_name, "..")
513 || !strcmp(evt_ent->d_name, "enable")
514 || !strcmp(evt_ent->d_name, "filter"))
515 continue;
516
Jiri Olsa89812fc2012-03-15 20:09:15 +0100517 if (!strglobmatch(evt_ent->d_name, evt_name))
Masami Hiramatsufb1d2ed2010-01-05 17:47:17 -0500518 continue;
519
Jiri Olsa27bf90b2015-10-05 21:31:17 +0200520 found++;
521
He Kuange637d172015-09-28 03:52:16 +0000522 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200523 err, head_config);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200524 }
525
Jiri Olsa27bf90b2015-10-05 21:31:17 +0200526 if (!found) {
527 tracepoint_error(err, ENOENT, sys_name, evt_name);
528 ret = -1;
529 }
530
Jiri Olsa0bd3f082012-12-17 14:08:36 +0100531 closedir(evt_dir);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100532 return ret;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200533}
534
David Ahernc5cd8ac2013-07-02 13:27:25 -0600535static int add_tracepoint_event(struct list_head *list, int *idx,
Jiri Olsae2f9f8e2015-09-07 10:38:05 +0200536 char *sys_name, char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200537 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000538 struct list_head *head_config)
Jiri Olsaf35488f2012-12-17 14:08:37 +0100539{
540 return strpbrk(evt_name, "*?") ?
He Kuange637d172015-09-28 03:52:16 +0000541 add_tracepoint_multi_event(list, idx, sys_name, evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200542 err, head_config) :
He Kuange637d172015-09-28 03:52:16 +0000543 add_tracepoint(list, idx, sys_name, evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200544 err, head_config);
Jiri Olsaf35488f2012-12-17 14:08:37 +0100545}
546
David Ahernc5cd8ac2013-07-02 13:27:25 -0600547static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
Jiri Olsae2f9f8e2015-09-07 10:38:05 +0200548 char *sys_name, 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 struct dirent *events_ent;
553 DIR *events_dir;
554 int ret = 0;
555
556 events_dir = opendir(tracing_events_path);
557 if (!events_dir) {
Jiri Olsa272ed292015-09-29 17:05:31 +0200558 tracepoint_error(err, errno, sys_name, evt_name);
Jiri Olsaf35488f2012-12-17 14:08:37 +0100559 return -1;
560 }
561
562 while (!ret && (events_ent = readdir(events_dir))) {
563 if (!strcmp(events_ent->d_name, ".")
564 || !strcmp(events_ent->d_name, "..")
565 || !strcmp(events_ent->d_name, "enable")
566 || !strcmp(events_ent->d_name, "header_event")
567 || !strcmp(events_ent->d_name, "header_page"))
568 continue;
569
570 if (!strglobmatch(events_ent->d_name, sys_name))
571 continue;
572
573 ret = add_tracepoint_event(list, idx, events_ent->d_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200574 evt_name, err, head_config);
Jiri Olsaf35488f2012-12-17 14:08:37 +0100575 }
576
577 closedir(events_dir);
578 return ret;
579}
580
Wang Nan4edf30e2015-10-14 12:41:17 +0000581struct __add_bpf_event_param {
582 struct parse_events_evlist *data;
583 struct list_head *list;
584};
585
586static int add_bpf_event(struct probe_trace_event *tev, int fd,
587 void *_param)
588{
589 LIST_HEAD(new_evsels);
590 struct __add_bpf_event_param *param = _param;
591 struct parse_events_evlist *evlist = param->data;
592 struct list_head *list = param->list;
Wang Nan1f45b1d2015-10-14 12:41:18 +0000593 struct perf_evsel *pos;
Wang Nan4edf30e2015-10-14 12:41:17 +0000594 int err;
595
596 pr_debug("add bpf event %s:%s and attach bpf program %d\n",
597 tev->group, tev->event, fd);
598
599 err = parse_events_add_tracepoint(&new_evsels, &evlist->idx, tev->group,
600 tev->event, evlist->error, NULL);
601 if (err) {
602 struct perf_evsel *evsel, *tmp;
603
604 pr_debug("Failed to add BPF event %s:%s\n",
605 tev->group, tev->event);
606 list_for_each_entry_safe(evsel, tmp, &new_evsels, node) {
607 list_del(&evsel->node);
608 perf_evsel__delete(evsel);
609 }
610 return err;
611 }
612 pr_debug("adding %s:%s\n", tev->group, tev->event);
613
Wang Nan1f45b1d2015-10-14 12:41:18 +0000614 list_for_each_entry(pos, &new_evsels, node) {
615 pr_debug("adding %s:%s to %p\n",
616 tev->group, tev->event, pos);
617 pos->bpf_fd = fd;
618 }
Wang Nan4edf30e2015-10-14 12:41:17 +0000619 list_splice(&new_evsels, list);
620 return 0;
621}
622
Wang Nan84c86ca2015-10-14 12:41:14 +0000623int parse_events_load_bpf_obj(struct parse_events_evlist *data,
624 struct list_head *list,
625 struct bpf_object *obj)
626{
627 int err;
628 char errbuf[BUFSIZ];
Wang Nan4edf30e2015-10-14 12:41:17 +0000629 struct __add_bpf_event_param param = {data, list};
Wang Nanaa3abf32015-10-14 12:41:15 +0000630 static bool registered_unprobe_atexit = false;
Wang Nan84c86ca2015-10-14 12:41:14 +0000631
632 if (IS_ERR(obj) || !obj) {
633 snprintf(errbuf, sizeof(errbuf),
634 "Internal error: load bpf obj with NULL");
635 err = -EINVAL;
636 goto errout;
637 }
638
639 /*
Wang Nanaa3abf32015-10-14 12:41:15 +0000640 * Register atexit handler before calling bpf__probe() so
641 * bpf__probe() don't need to unprobe probe points its already
642 * created when failure.
643 */
644 if (!registered_unprobe_atexit) {
645 atexit(bpf__clear);
646 registered_unprobe_atexit = true;
647 }
648
649 err = bpf__probe(obj);
650 if (err) {
651 bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
652 goto errout;
653 }
654
Wang Nan1e5e3ee2015-10-14 12:41:16 +0000655 err = bpf__load(obj);
656 if (err) {
657 bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
658 goto errout;
659 }
660
Wang Nan4edf30e2015-10-14 12:41:17 +0000661 err = bpf__foreach_tev(obj, add_bpf_event, &param);
662 if (err) {
663 snprintf(errbuf, sizeof(errbuf),
664 "Attach events in BPF object failed");
665 goto errout;
666 }
667
668 return 0;
Wang Nan84c86ca2015-10-14 12:41:14 +0000669errout:
670 data->error->help = strdup("(add -v to see detail)");
671 data->error->str = strdup(errbuf);
672 return err;
673}
674
Wang Nana34f3be2016-02-22 09:10:31 +0000675static int
676parse_events_config_bpf(struct parse_events_evlist *data,
677 struct bpf_object *obj,
678 struct list_head *head_config)
679{
680 struct parse_events_term *term;
681 int error_pos;
682
683 if (!head_config || list_empty(head_config))
684 return 0;
685
686 list_for_each_entry(term, head_config, list) {
687 char errbuf[BUFSIZ];
688 int err;
689
690 if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) {
691 snprintf(errbuf, sizeof(errbuf),
692 "Invalid config term for BPF object");
693 errbuf[BUFSIZ - 1] = '\0';
694
695 data->error->idx = term->err_term;
696 data->error->str = strdup(errbuf);
697 return -EINVAL;
698 }
699
700 err = bpf__config_obj(obj, term, NULL, &error_pos);
701 if (err) {
702 bpf__strerror_config_obj(obj, term, NULL,
703 &error_pos, err, errbuf,
704 sizeof(errbuf));
705 data->error->help = strdup(
706"Hint:\tValid config term:\n"
707" \tmap:[<arraymap>].value=[value]\n"
708" \t(add -v to see detail)");
709 data->error->str = strdup(errbuf);
710 if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE)
711 data->error->idx = term->err_val;
712 else
713 data->error->idx = term->err_term + error_pos;
714 return err;
715 }
716 }
717 return 0;
718}
719
Wang Nan84c86ca2015-10-14 12:41:14 +0000720int parse_events_load_bpf(struct parse_events_evlist *data,
721 struct list_head *list,
Wang Nand509db02015-10-14 12:41:20 +0000722 char *bpf_file_name,
Wang Nana34f3be2016-02-22 09:10:31 +0000723 bool source,
724 struct list_head *head_config)
Wang Nan84c86ca2015-10-14 12:41:14 +0000725{
726 struct bpf_object *obj;
Wang Nana34f3be2016-02-22 09:10:31 +0000727 int err;
Wang Nan84c86ca2015-10-14 12:41:14 +0000728
Wang Nand509db02015-10-14 12:41:20 +0000729 obj = bpf__prepare_load(bpf_file_name, source);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000730 if (IS_ERR(obj)) {
Wang Nan84c86ca2015-10-14 12:41:14 +0000731 char errbuf[BUFSIZ];
Wang Nan84c86ca2015-10-14 12:41:14 +0000732
Wang Nan6371ca3b2015-11-06 13:49:37 +0000733 err = PTR_ERR(obj);
Wang Nan84c86ca2015-10-14 12:41:14 +0000734
735 if (err == -ENOTSUP)
736 snprintf(errbuf, sizeof(errbuf),
737 "BPF support is not compiled");
738 else
Wang Nand3e0ce32015-11-06 13:58:09 +0000739 bpf__strerror_prepare_load(bpf_file_name,
740 source,
741 -err, errbuf,
742 sizeof(errbuf));
Wang Nan84c86ca2015-10-14 12:41:14 +0000743
744 data->error->help = strdup("(add -v to see detail)");
745 data->error->str = strdup(errbuf);
746 return err;
747 }
748
Wang Nana34f3be2016-02-22 09:10:31 +0000749 err = parse_events_load_bpf_obj(data, list, obj);
750 if (err)
751 return err;
752 return parse_events_config_bpf(data, obj, head_config);
Wang Nan84c86ca2015-10-14 12:41:14 +0000753}
754
Jiri Olsa89812fc2012-03-15 20:09:15 +0100755static int
756parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100757{
758 int i;
759
760 for (i = 0; i < 3; i++) {
Jiri Olsa89812fc2012-03-15 20:09:15 +0100761 if (!type || !type[i])
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100762 break;
763
Jiri Olsa75827322012-06-29 09:22:54 +0200764#define CHECK_SET_TYPE(bit) \
765do { \
766 if (attr->bp_type & bit) \
767 return -EINVAL; \
768 else \
769 attr->bp_type |= bit; \
770} while (0)
771
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100772 switch (type[i]) {
773 case 'r':
Jiri Olsa75827322012-06-29 09:22:54 +0200774 CHECK_SET_TYPE(HW_BREAKPOINT_R);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100775 break;
776 case 'w':
Jiri Olsa75827322012-06-29 09:22:54 +0200777 CHECK_SET_TYPE(HW_BREAKPOINT_W);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100778 break;
779 case 'x':
Jiri Olsa75827322012-06-29 09:22:54 +0200780 CHECK_SET_TYPE(HW_BREAKPOINT_X);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100781 break;
782 default:
Jiri Olsa89812fc2012-03-15 20:09:15 +0100783 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100784 }
785 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100786
Jiri Olsa75827322012-06-29 09:22:54 +0200787#undef CHECK_SET_TYPE
788
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100789 if (!attr->bp_type) /* Default */
790 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
791
Jiri Olsa89812fc2012-03-15 20:09:15 +0100792 return 0;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100793}
794
David Ahernc5cd8ac2013-07-02 13:27:25 -0600795int parse_events_add_breakpoint(struct list_head *list, int *idx,
Jacob Shin3741eb92014-05-29 17:26:51 +0200796 void *ptr, char *type, u64 len)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100797{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100798 struct perf_event_attr attr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100799
Jiri Olsa89812fc2012-03-15 20:09:15 +0100800 memset(&attr, 0, sizeof(attr));
Jiri Olsa9fafd982012-03-20 19:15:39 +0100801 attr.bp_addr = (unsigned long) ptr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100802
Jiri Olsa89812fc2012-03-15 20:09:15 +0100803 if (parse_breakpoint_type(type, &attr))
804 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100805
Jacob Shin3741eb92014-05-29 17:26:51 +0200806 /* Provide some defaults if len is not specified */
807 if (!len) {
808 if (attr.bp_type == HW_BREAKPOINT_X)
809 len = sizeof(long);
810 else
811 len = HW_BREAKPOINT_LEN_4;
812 }
813
814 attr.bp_len = len;
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200815
Jiri Olsa89812fc2012-03-15 20:09:15 +0100816 attr.type = PERF_TYPE_BREAKPOINT;
Jovi Zhang4a841d62012-07-15 03:03:10 +0800817 attr.sample_period = 1;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100818
Jiri Olsa930a2e22015-07-29 05:42:10 -0400819 return add_event(list, idx, &attr, NULL, NULL);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100820}
821
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200822static int check_type_val(struct parse_events_term *term,
823 struct parse_events_error *err,
824 int type)
Jiri Olsa8f707d82012-03-15 20:09:16 +0100825{
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200826 if (type == term->type_val)
827 return 0;
828
829 if (err) {
830 err->idx = term->err_val;
831 if (type == PARSE_EVENTS__TERM_TYPE_NUM)
832 err->str = strdup("expected numeric value");
833 else
834 err->str = strdup("expected string value");
835 }
836 return -EINVAL;
837}
838
Wang Nan17cb5f82016-02-19 11:43:57 +0000839/*
840 * Update according to parse-events.l
841 */
842static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
843 [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>",
844 [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config",
845 [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1",
846 [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2",
847 [PARSE_EVENTS__TERM_TYPE_NAME] = "name",
848 [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period",
849 [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq",
850 [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type",
851 [PARSE_EVENTS__TERM_TYPE_TIME] = "time",
852 [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph",
853 [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size",
854 [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit",
855 [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit",
856};
857
Wang Nan1669e502016-02-19 11:43:58 +0000858static bool config_term_shrinked;
859
860static bool
861config_term_avail(int term_type, struct parse_events_error *err)
862{
863 if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
864 err->str = strdup("Invalid term_type");
865 return false;
866 }
867 if (!config_term_shrinked)
868 return true;
869
870 switch (term_type) {
871 case PARSE_EVENTS__TERM_TYPE_CONFIG:
872 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
873 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
874 case PARSE_EVENTS__TERM_TYPE_NAME:
875 return true;
876 default:
877 if (!err)
878 return false;
879
880 /* term_type is validated so indexing is safe */
881 if (asprintf(&err->str, "'%s' is not usable in 'perf stat'",
882 config_term_names[term_type]) < 0)
883 err->str = NULL;
884 return false;
885 }
886}
887
888void parse_events__shrink_config_terms(void)
889{
890 config_term_shrinked = true;
891}
892
He Kuang0b8891a2015-09-28 03:52:13 +0000893static int config_term_common(struct perf_event_attr *attr,
894 struct parse_events_term *term,
895 struct parse_events_error *err)
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200896{
897#define CHECK_TYPE_VAL(type) \
898do { \
899 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
900 return -EINVAL; \
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200901} while (0)
902
903 switch (term->type_term) {
Jiri Olsa8f707d82012-03-15 20:09:16 +0100904 case PARSE_EVENTS__TERM_TYPE_CONFIG:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200905 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100906 attr->config = term->val.num;
907 break;
908 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200909 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100910 attr->config1 = term->val.num;
911 break;
912 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200913 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100914 attr->config2 = term->val.num;
915 break;
916 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200917 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100918 break;
Namhyung Kim09af2a52015-08-09 15:45:23 +0900919 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
920 CHECK_TYPE_VAL(NUM);
921 break;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100922 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
923 /*
924 * TODO uncomment when the field is available
925 * attr->branch_sample_type = term->val.num;
926 */
927 break;
Kan Liang32067712015-08-04 04:30:19 -0400928 case PARSE_EVENTS__TERM_TYPE_TIME:
929 CHECK_TYPE_VAL(NUM);
930 if (term->val.num > 1) {
931 err->str = strdup("expected 0 or 1");
932 err->idx = term->err_val;
933 return -EINVAL;
934 }
935 break;
Kan Liangd457c962015-08-11 06:30:47 -0400936 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
937 CHECK_TYPE_VAL(STR);
938 break;
939 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
940 CHECK_TYPE_VAL(NUM);
941 break;
Wang Nan374ce932015-10-28 10:55:02 +0000942 case PARSE_EVENTS__TERM_TYPE_INHERIT:
943 CHECK_TYPE_VAL(NUM);
944 break;
945 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
946 CHECK_TYPE_VAL(NUM);
947 break;
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200948 case PARSE_EVENTS__TERM_TYPE_NAME:
949 CHECK_TYPE_VAL(STR);
950 break;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100951 default:
He Kuangffeb8832015-09-28 03:52:14 +0000952 err->str = strdup("unknown term");
953 err->idx = term->err_term;
954 err->help = parse_events_formats_error_string(NULL);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100955 return -EINVAL;
956 }
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200957
Wang Nan1669e502016-02-19 11:43:58 +0000958 /*
959 * Check term availbility after basic checking so
960 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
961 *
962 * If check availbility at the entry of this function,
963 * user will see "'<sysfs term>' is not usable in 'perf stat'"
964 * if an invalid config term is provided for legacy events
965 * (for example, instructions/badterm/...), which is confusing.
966 */
967 if (!config_term_avail(term->type_term, err))
968 return -EINVAL;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100969 return 0;
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200970#undef CHECK_TYPE_VAL
Jiri Olsa8f707d82012-03-15 20:09:16 +0100971}
972
He Kuang0b8891a2015-09-28 03:52:13 +0000973static int config_term_pmu(struct perf_event_attr *attr,
974 struct parse_events_term *term,
975 struct parse_events_error *err)
976{
977 if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER)
978 /*
979 * Always succeed for sysfs terms, as we dont know
980 * at this point what type they need to have.
981 */
982 return 0;
983 else
984 return config_term_common(attr, term, err);
985}
986
He Kuange637d172015-09-28 03:52:16 +0000987static int config_term_tracepoint(struct perf_event_attr *attr,
988 struct parse_events_term *term,
989 struct parse_events_error *err)
990{
991 switch (term->type_term) {
992 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
993 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
Wang Nan374ce932015-10-28 10:55:02 +0000994 case PARSE_EVENTS__TERM_TYPE_INHERIT:
995 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
He Kuange637d172015-09-28 03:52:16 +0000996 return config_term_common(attr, term, err);
997 default:
998 if (err) {
999 err->idx = term->err_term;
1000 err->str = strdup("unknown term");
1001 err->help = strdup("valid terms: call-graph,stack-size\n");
1002 }
1003 return -EINVAL;
1004 }
1005
1006 return 0;
1007}
1008
Jiri Olsa8f707d82012-03-15 20:09:16 +01001009static int config_attr(struct perf_event_attr *attr,
Jiri Olsa3b0e3712015-04-22 21:10:22 +02001010 struct list_head *head,
He Kuang0b8891a2015-09-28 03:52:13 +00001011 struct parse_events_error *err,
1012 config_term_func_t config_term)
Jiri Olsa8f707d82012-03-15 20:09:16 +01001013{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001014 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001015
1016 list_for_each_entry(term, head, list)
Jiri Olsa3b0e3712015-04-22 21:10:22 +02001017 if (config_term(attr, term, err))
Jiri Olsa8f707d82012-03-15 20:09:16 +01001018 return -EINVAL;
1019
1020 return 0;
1021}
1022
Jiri Olsa930a2e22015-07-29 05:42:10 -04001023static int get_config_terms(struct list_head *head_config,
1024 struct list_head *head_terms __maybe_unused)
1025{
1026#define ADD_CONFIG_TERM(__type, __name, __val) \
1027do { \
1028 struct perf_evsel_config_term *__t; \
1029 \
1030 __t = zalloc(sizeof(*__t)); \
1031 if (!__t) \
1032 return -ENOMEM; \
1033 \
1034 INIT_LIST_HEAD(&__t->list); \
1035 __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \
1036 __t->val.__name = __val; \
1037 list_add_tail(&__t->list, head_terms); \
1038} while (0)
1039
1040 struct parse_events_term *term;
1041
1042 list_for_each_entry(term, head_config, list) {
1043 switch (term->type_term) {
Jiri Olsaee4c7582015-07-29 05:42:11 -04001044 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1045 ADD_CONFIG_TERM(PERIOD, period, term->val.num);
Kan Liang32067712015-08-04 04:30:19 -04001046 break;
Namhyung Kim09af2a52015-08-09 15:45:23 +09001047 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1048 ADD_CONFIG_TERM(FREQ, freq, term->val.num);
1049 break;
Kan Liang32067712015-08-04 04:30:19 -04001050 case PARSE_EVENTS__TERM_TYPE_TIME:
1051 ADD_CONFIG_TERM(TIME, time, term->val.num);
1052 break;
Kan Liangd457c962015-08-11 06:30:47 -04001053 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1054 ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
1055 break;
1056 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1057 ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
1058 break;
Wang Nan374ce932015-10-28 10:55:02 +00001059 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1060 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0);
1061 break;
1062 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1063 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1);
1064 break;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001065 default:
1066 break;
1067 }
1068 }
1069#undef ADD_EVSEL_CONFIG
1070 return 0;
1071}
1072
He Kuange637d172015-09-28 03:52:16 +00001073int parse_events_add_tracepoint(struct list_head *list, int *idx,
1074 char *sys, char *event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001075 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +00001076 struct list_head *head_config)
1077{
1078 if (head_config) {
1079 struct perf_event_attr attr;
1080
Jiri Olsa272ed292015-09-29 17:05:31 +02001081 if (config_attr(&attr, head_config, err,
He Kuange637d172015-09-28 03:52:16 +00001082 config_term_tracepoint))
1083 return -EINVAL;
1084 }
1085
1086 if (strpbrk(sys, "*?"))
1087 return add_tracepoint_multi_sys(list, idx, sys, event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001088 err, head_config);
He Kuange637d172015-09-28 03:52:16 +00001089 else
1090 return add_tracepoint_event(list, idx, sys, event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001091 err, head_config);
He Kuange637d172015-09-28 03:52:16 +00001092}
1093
Jiri Olsa87d650b2015-04-22 21:10:24 +02001094int parse_events_add_numeric(struct parse_events_evlist *data,
1095 struct list_head *list,
Robert Richterb527bab2012-08-07 19:43:13 +02001096 u32 type, u64 config,
Jiri Olsa8f707d82012-03-15 20:09:16 +01001097 struct list_head *head_config)
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301098{
Jiri Olsa89812fc2012-03-15 20:09:15 +01001099 struct perf_event_attr attr;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001100 LIST_HEAD(config_terms);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301101
Jiri Olsa89812fc2012-03-15 20:09:15 +01001102 memset(&attr, 0, sizeof(attr));
1103 attr.type = type;
1104 attr.config = config;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001105
Jiri Olsa930a2e22015-07-29 05:42:10 -04001106 if (head_config) {
He Kuang0b8891a2015-09-28 03:52:13 +00001107 if (config_attr(&attr, head_config, data->error,
1108 config_term_common))
Jiri Olsa930a2e22015-07-29 05:42:10 -04001109 return -EINVAL;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001110
Jiri Olsa930a2e22015-07-29 05:42:10 -04001111 if (get_config_terms(head_config, &config_terms))
1112 return -ENOMEM;
1113 }
1114
Wang Nan10bf3582016-02-19 11:44:00 +00001115 return add_event(list, &data->idx, &attr,
1116 get_config_name(head_config), &config_terms);
Paul Mackerras61c45982009-07-01 13:04:34 +10001117}
1118
Jiri Olsa36adec82015-04-22 21:10:19 +02001119int parse_events_add_pmu(struct parse_events_evlist *data,
1120 struct list_head *list, char *name,
1121 struct list_head *head_config)
Jiri Olsa5f537a22012-03-15 20:09:18 +01001122{
1123 struct perf_event_attr attr;
Matt Fleming46441bd2014-09-24 15:04:06 +01001124 struct perf_pmu_info info;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001125 struct perf_pmu *pmu;
Stephane Eranian410136f2013-11-12 17:58:49 +01001126 struct perf_evsel *evsel;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001127 LIST_HEAD(config_terms);
Jiri Olsa5f537a22012-03-15 20:09:18 +01001128
1129 pmu = perf_pmu__find(name);
1130 if (!pmu)
1131 return -EINVAL;
1132
Adrian Hunterdc0a6202014-07-31 09:00:49 +03001133 if (pmu->default_config) {
1134 memcpy(&attr, pmu->default_config,
1135 sizeof(struct perf_event_attr));
1136 } else {
1137 memset(&attr, 0, sizeof(attr));
1138 }
Jiri Olsa5f537a22012-03-15 20:09:18 +01001139
Adrian Hunterad962272014-08-15 22:08:40 +03001140 if (!head_config) {
1141 attr.type = pmu->type;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001142 evsel = __add_event(list, &data->idx, &attr, NULL, pmu->cpus, NULL);
Adrian Hunterad962272014-08-15 22:08:40 +03001143 return evsel ? 0 : -ENOMEM;
1144 }
1145
Matt Fleming46441bd2014-09-24 15:04:06 +01001146 if (perf_pmu__check_alias(pmu, head_config, &info))
Zheng Yana6146d52012-06-15 14:31:41 +08001147 return -EINVAL;
1148
Jiri Olsa5f537a22012-03-15 20:09:18 +01001149 /*
1150 * Configure hardcoded terms first, no need to check
1151 * return value when called with fail == 0 ;)
1152 */
He Kuang0b8891a2015-09-28 03:52:13 +00001153 if (config_attr(&attr, head_config, data->error, config_term_pmu))
Jiri Olsac056ba6a2015-04-22 21:10:18 +02001154 return -EINVAL;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001155
Jiri Olsa930a2e22015-07-29 05:42:10 -04001156 if (get_config_terms(head_config, &config_terms))
1157 return -ENOMEM;
1158
Jiri Olsae64b0202015-04-22 21:10:21 +02001159 if (perf_pmu__config(pmu, &attr, head_config, data->error))
Jiri Olsa5f537a22012-03-15 20:09:18 +01001160 return -EINVAL;
1161
Jiri Olsa36adec82015-04-22 21:10:19 +02001162 evsel = __add_event(list, &data->idx, &attr,
Wang Nane814fdd2016-02-19 11:43:59 +00001163 get_config_name(head_config), pmu->cpus,
Jiri Olsa930a2e22015-07-29 05:42:10 -04001164 &config_terms);
Stephane Eranian410136f2013-11-12 17:58:49 +01001165 if (evsel) {
Matt Fleming46441bd2014-09-24 15:04:06 +01001166 evsel->unit = info.unit;
1167 evsel->scale = info.scale;
Matt Fleming044330c2014-11-21 10:31:12 +01001168 evsel->per_pkg = info.per_pkg;
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001169 evsel->snapshot = info.snapshot;
Stephane Eranian410136f2013-11-12 17:58:49 +01001170 }
1171
1172 return evsel ? 0 : -ENOMEM;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001173}
1174
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001175int parse_events__modifier_group(struct list_head *list,
1176 char *event_mod)
Jiri Olsa89efb022012-08-08 12:14:14 +02001177{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001178 return parse_events__modifier_event(list, event_mod, true);
1179}
1180
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -03001181void parse_events__set_leader(char *name, struct list_head *list)
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001182{
1183 struct perf_evsel *leader;
1184
Wang Nan854f7362015-09-06 07:13:17 +00001185 if (list_empty(list)) {
1186 WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1187 return;
1188 }
1189
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -03001190 __perf_evlist__set_leader(list);
1191 leader = list_entry(list->next, struct perf_evsel, node);
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001192 leader->group_name = name ? strdup(name) : NULL;
Jiri Olsa89efb022012-08-08 12:14:14 +02001193}
1194
David Ahernc5cd8ac2013-07-02 13:27:25 -06001195/* list_event is assumed to point to malloc'ed memory */
Jiri Olsa5d7be902012-03-20 19:15:40 +01001196void parse_events_update_lists(struct list_head *list_event,
1197 struct list_head *list_all)
1198{
1199 /*
1200 * Called for single event definition. Update the
Jiri Olsa89efb022012-08-08 12:14:14 +02001201 * 'all event' list, and reinit the 'single event'
Jiri Olsa5d7be902012-03-20 19:15:40 +01001202 * list, for next event definition.
1203 */
1204 list_splice_tail(list_event, list_all);
Jiri Olsab847cbd2012-05-21 09:12:51 +02001205 free(list_event);
Jiri Olsa5d7be902012-03-20 19:15:40 +01001206}
1207
Jiri Olsaf5b11352012-08-08 12:21:54 +02001208struct event_modifier {
1209 int eu;
1210 int ek;
1211 int eh;
1212 int eH;
1213 int eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001214 int eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001215 int precise;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001216 int precise_max;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001217 int exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02001218 int sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001219 int pinned;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001220};
Paul Mackerras61c45982009-07-01 13:04:34 +10001221
Jiri Olsaf5b11352012-08-08 12:21:54 +02001222static int get_event_modifier(struct event_modifier *mod, char *str,
1223 struct perf_evsel *evsel)
1224{
1225 int eu = evsel ? evsel->attr.exclude_user : 0;
1226 int ek = evsel ? evsel->attr.exclude_kernel : 0;
1227 int eh = evsel ? evsel->attr.exclude_hv : 0;
1228 int eH = evsel ? evsel->attr.exclude_host : 0;
1229 int eG = evsel ? evsel->attr.exclude_guest : 0;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001230 int eI = evsel ? evsel->attr.exclude_idle : 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001231 int precise = evsel ? evsel->attr.precise_ip : 0;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001232 int precise_max = 0;
Jiri Olsa3c176312012-10-10 17:39:03 +02001233 int sample_read = 0;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001234 int pinned = evsel ? evsel->attr.pinned : 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001235
1236 int exclude = eu | ek | eh;
1237 int exclude_GH = evsel ? evsel->exclude_GH : 0;
1238
Jiri Olsaf5b11352012-08-08 12:21:54 +02001239 memset(mod, 0, sizeof(*mod));
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001240
Paul Mackerras61c45982009-07-01 13:04:34 +10001241 while (*str) {
Peter Zijlstraab608342010-04-08 23:03:20 +02001242 if (*str == 'u') {
1243 if (!exclude)
1244 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001245 eu = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +02001246 } else if (*str == 'k') {
1247 if (!exclude)
1248 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001249 ek = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +02001250 } else if (*str == 'h') {
1251 if (!exclude)
1252 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001253 eh = 0;
Joerg Roedel99320cc2012-01-04 17:54:19 +01001254 } else if (*str == 'G') {
1255 if (!exclude_GH)
1256 exclude_GH = eG = eH = 1;
1257 eG = 0;
1258 } else if (*str == 'H') {
1259 if (!exclude_GH)
1260 exclude_GH = eG = eH = 1;
1261 eH = 0;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001262 } else if (*str == 'I') {
1263 eI = 1;
Peter Zijlstraab608342010-04-08 23:03:20 +02001264 } else if (*str == 'p') {
1265 precise++;
David Ahern13427982012-09-13 14:59:13 -06001266 /* use of precise requires exclude_guest */
1267 if (!exclude_GH)
1268 eG = 1;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001269 } else if (*str == 'P') {
1270 precise_max = 1;
Jiri Olsa3c176312012-10-10 17:39:03 +02001271 } else if (*str == 'S') {
1272 sample_read = 1;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001273 } else if (*str == 'D') {
1274 pinned = 1;
Peter Zijlstraab608342010-04-08 23:03:20 +02001275 } else
Paul Mackerras61c45982009-07-01 13:04:34 +10001276 break;
Peter Zijlstraab608342010-04-08 23:03:20 +02001277
Paul Mackerras61c45982009-07-01 13:04:34 +10001278 ++str;
1279 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001280
Jiri Olsa89812fc2012-03-15 20:09:15 +01001281 /*
1282 * precise ip:
1283 *
1284 * 0 - SAMPLE_IP can have arbitrary skid
1285 * 1 - SAMPLE_IP must have constant skid
1286 * 2 - SAMPLE_IP requested to have 0 skid
1287 * 3 - SAMPLE_IP must have 0 skid
1288 *
1289 * See also PERF_RECORD_MISC_EXACT_IP
1290 */
1291 if (precise > 3)
1292 return -EINVAL;
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001293
Jiri Olsaf5b11352012-08-08 12:21:54 +02001294 mod->eu = eu;
1295 mod->ek = ek;
1296 mod->eh = eh;
1297 mod->eH = eH;
1298 mod->eG = eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001299 mod->eI = eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001300 mod->precise = precise;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001301 mod->precise_max = precise_max;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001302 mod->exclude_GH = exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02001303 mod->sample_read = sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001304 mod->pinned = pinned;
1305
Jiri Olsaf5b11352012-08-08 12:21:54 +02001306 return 0;
1307}
1308
Jiri Olsa534123f2012-11-13 15:32:58 +01001309/*
1310 * Basic modifier sanity check to validate it contains only one
1311 * instance of any modifier (apart from 'p') present.
1312 */
1313static int check_modifier(char *str)
1314{
1315 char *p = str;
1316
1317 /* The sizeof includes 0 byte as well. */
Jiri Olsa7f94af72015-10-05 20:06:05 +02001318 if (strlen(str) > (sizeof("ukhGHpppPSDI") - 1))
Jiri Olsa534123f2012-11-13 15:32:58 +01001319 return -1;
1320
1321 while (*p) {
1322 if (*p != 'p' && strchr(p + 1, *p))
1323 return -1;
1324 p++;
1325 }
1326
1327 return 0;
1328}
1329
Jiri Olsaf5b11352012-08-08 12:21:54 +02001330int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1331{
1332 struct perf_evsel *evsel;
1333 struct event_modifier mod;
1334
1335 if (str == NULL)
1336 return 0;
1337
Jiri Olsa534123f2012-11-13 15:32:58 +01001338 if (check_modifier(str))
1339 return -EINVAL;
1340
Jiri Olsaf5b11352012-08-08 12:21:54 +02001341 if (!add && get_event_modifier(&mod, str, NULL))
1342 return -EINVAL;
1343
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001344 __evlist__for_each(list, evsel) {
Jiri Olsaf5b11352012-08-08 12:21:54 +02001345 if (add && get_event_modifier(&mod, str, evsel))
1346 return -EINVAL;
1347
1348 evsel->attr.exclude_user = mod.eu;
1349 evsel->attr.exclude_kernel = mod.ek;
1350 evsel->attr.exclude_hv = mod.eh;
1351 evsel->attr.precise_ip = mod.precise;
1352 evsel->attr.exclude_host = mod.eH;
1353 evsel->attr.exclude_guest = mod.eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001354 evsel->attr.exclude_idle = mod.eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001355 evsel->exclude_GH = mod.exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02001356 evsel->sample_read = mod.sample_read;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001357 evsel->precise_max = mod.precise_max;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001358
1359 if (perf_evsel__is_group_leader(evsel))
1360 evsel->attr.pinned = mod.pinned;
Jiri Olsa89812fc2012-03-15 20:09:15 +01001361 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001362
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301363 return 0;
1364}
1365
Robert Richterac2ba9f2012-08-16 21:10:21 +02001366int parse_events_name(struct list_head *list, char *name)
1367{
1368 struct perf_evsel *evsel;
1369
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001370 __evlist__for_each(list, evsel) {
Robert Richterac2ba9f2012-08-16 21:10:21 +02001371 if (!evsel->name)
1372 evsel->name = strdup(name);
1373 }
1374
1375 return 0;
1376}
1377
Kan Liangdcb4e102014-10-07 11:08:50 -04001378static int
1379comp_pmu(const void *p1, const void *p2)
1380{
1381 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
1382 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
1383
1384 return strcmp(pmu1->symbol, pmu2->symbol);
1385}
1386
1387static void perf_pmu__parse_cleanup(void)
1388{
1389 if (perf_pmu_events_list_num > 0) {
1390 struct perf_pmu_event_symbol *p;
1391 int i;
1392
1393 for (i = 0; i < perf_pmu_events_list_num; i++) {
1394 p = perf_pmu_events_list + i;
1395 free(p->symbol);
1396 }
1397 free(perf_pmu_events_list);
1398 perf_pmu_events_list = NULL;
1399 perf_pmu_events_list_num = 0;
1400 }
1401}
1402
1403#define SET_SYMBOL(str, stype) \
1404do { \
1405 p->symbol = str; \
1406 if (!p->symbol) \
1407 goto err; \
1408 p->type = stype; \
1409} while (0)
1410
1411/*
1412 * Read the pmu events list from sysfs
1413 * Save it into perf_pmu_events_list
1414 */
1415static void perf_pmu__parse_init(void)
1416{
1417
1418 struct perf_pmu *pmu = NULL;
1419 struct perf_pmu_alias *alias;
1420 int len = 0;
1421
1422 pmu = perf_pmu__find("cpu");
1423 if ((pmu == NULL) || list_empty(&pmu->aliases)) {
1424 perf_pmu_events_list_num = -1;
1425 return;
1426 }
1427 list_for_each_entry(alias, &pmu->aliases, list) {
1428 if (strchr(alias->name, '-'))
1429 len++;
1430 len++;
1431 }
1432 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1433 if (!perf_pmu_events_list)
1434 return;
1435 perf_pmu_events_list_num = len;
1436
1437 len = 0;
1438 list_for_each_entry(alias, &pmu->aliases, list) {
1439 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1440 char *tmp = strchr(alias->name, '-');
1441
1442 if (tmp != NULL) {
1443 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1444 PMU_EVENT_SYMBOL_PREFIX);
1445 p++;
1446 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1447 len += 2;
1448 } else {
1449 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1450 len++;
1451 }
1452 }
1453 qsort(perf_pmu_events_list, len,
1454 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1455
1456 return;
1457err:
1458 perf_pmu__parse_cleanup();
1459}
1460
1461enum perf_pmu_event_symbol_type
1462perf_pmu__parse_check(const char *name)
1463{
1464 struct perf_pmu_event_symbol p, *r;
1465
1466 /* scan kernel pmu events from sysfs if needed */
1467 if (perf_pmu_events_list_num == 0)
1468 perf_pmu__parse_init();
1469 /*
1470 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1471 * cpu-cycles has been handled by hardcode.
1472 * So it must be cpu// events, not kernel pmu event.
1473 */
1474 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1475 return PMU_EVENT_SYMBOL_ERR;
1476
1477 p.symbol = strdup(name);
1478 r = bsearch(&p, perf_pmu_events_list,
1479 (size_t) perf_pmu_events_list_num,
1480 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1481 free(p.symbol);
1482 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1483}
1484
Jiri Olsa90e2b222012-06-15 14:31:40 +08001485static int parse_events__scanner(const char *str, void *data, int start_token)
Zheng Yanac20de6f2012-06-15 14:31:39 +08001486{
1487 YY_BUFFER_STATE buffer;
1488 void *scanner;
1489 int ret;
1490
Jiri Olsa90e2b222012-06-15 14:31:40 +08001491 ret = parse_events_lex_init_extra(start_token, &scanner);
Zheng Yanac20de6f2012-06-15 14:31:39 +08001492 if (ret)
1493 return ret;
1494
1495 buffer = parse_events__scan_string(str, scanner);
1496
1497#ifdef PARSER_DEBUG
1498 parse_events_debug = 1;
1499#endif
1500 ret = parse_events_parse(data, scanner);
1501
1502 parse_events__flush_buffer(buffer, scanner);
1503 parse_events__delete_buffer(buffer, scanner);
1504 parse_events_lex_destroy(scanner);
1505 return ret;
1506}
1507
Jiri Olsa90e2b222012-06-15 14:31:40 +08001508/*
1509 * parse event config string, return a list of event terms.
1510 */
1511int parse_events_terms(struct list_head *terms, const char *str)
1512{
Arnaldo Carvalho de Melo23b63392013-01-18 16:56:57 -03001513 struct parse_events_terms data = {
Jiri Olsa90e2b222012-06-15 14:31:40 +08001514 .terms = NULL,
1515 };
1516 int ret;
1517
1518 ret = parse_events__scanner(str, &data, PE_START_TERMS);
1519 if (!ret) {
1520 list_splice(data.terms, terms);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001521 zfree(&data.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001522 return 0;
1523 }
1524
Arnaldo Carvalho de Melo2146afc2016-02-12 17:09:17 -03001525 parse_events_terms__delete(data.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001526 return ret;
1527}
1528
Jiri Olsab39b8392015-04-22 21:10:16 +02001529int parse_events(struct perf_evlist *evlist, const char *str,
1530 struct parse_events_error *err)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001531{
Arnaldo Carvalho de Melo23b63392013-01-18 16:56:57 -03001532 struct parse_events_evlist data = {
Jiri Olsab39b8392015-04-22 21:10:16 +02001533 .list = LIST_HEAD_INIT(data.list),
1534 .idx = evlist->nr_entries,
1535 .error = err,
Jiri Olsa46010ab2012-06-15 14:31:38 +08001536 };
Jiri Olsa46010ab2012-06-15 14:31:38 +08001537 int ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001538
Jiri Olsa90e2b222012-06-15 14:31:40 +08001539 ret = parse_events__scanner(str, &data, PE_START_EVENTS);
Kan Liangdcb4e102014-10-07 11:08:50 -04001540 perf_pmu__parse_cleanup();
Jiri Olsa89812fc2012-03-15 20:09:15 +01001541 if (!ret) {
Wang Nan15bfd2c2015-07-10 07:36:09 +00001542 struct perf_evsel *last;
1543
Wang Nan854f7362015-09-06 07:13:17 +00001544 if (list_empty(&data.list)) {
1545 WARN_ONCE(true, "WARNING: event parser found nothing");
1546 return -1;
1547 }
1548
Adrian Hunterf114d6e2015-09-08 10:58:53 +03001549 perf_evlist__splice_list_tail(evlist, &data.list);
Namhyung Kim97f63e42013-01-22 18:09:29 +09001550 evlist->nr_groups += data.nr_groups;
Wang Nan15bfd2c2015-07-10 07:36:09 +00001551 last = perf_evlist__last(evlist);
1552 last->cmdline_group_boundary = true;
1553
Jiri Olsa89812fc2012-03-15 20:09:15 +01001554 return 0;
1555 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02001556
Jiri Olsa5d7be902012-03-20 19:15:40 +01001557 /*
1558 * There are 2 users - builtin-record and builtin-test objects.
1559 * Both call perf_evlist__delete in case of error, so we dont
1560 * need to bother.
1561 */
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02001562 return ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001563}
1564
Jiri Olsab39b8392015-04-22 21:10:16 +02001565#define MAX_WIDTH 1000
1566static int get_term_width(void)
1567{
1568 struct winsize ws;
1569
1570 get_term_dimensions(&ws);
1571 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1572}
1573
1574static void parse_events_print_error(struct parse_events_error *err,
1575 const char *event)
1576{
1577 const char *str = "invalid or unsupported event: ";
1578 char _buf[MAX_WIDTH];
1579 char *buf = (char *) event;
1580 int idx = 0;
1581
1582 if (err->str) {
1583 /* -2 for extra '' in the final fprintf */
1584 int width = get_term_width() - 2;
1585 int len_event = strlen(event);
1586 int len_str, max_len, cut = 0;
1587
1588 /*
1589 * Maximum error index indent, we will cut
1590 * the event string if it's bigger.
1591 */
Adrian Hunter141b2d32015-07-17 19:33:51 +03001592 int max_err_idx = 13;
Jiri Olsab39b8392015-04-22 21:10:16 +02001593
1594 /*
1595 * Let's be specific with the message when
1596 * we have the precise error.
1597 */
1598 str = "event syntax error: ";
1599 len_str = strlen(str);
1600 max_len = width - len_str;
1601
1602 buf = _buf;
1603
1604 /* We're cutting from the beggining. */
1605 if (err->idx > max_err_idx)
1606 cut = err->idx - max_err_idx;
1607
1608 strncpy(buf, event + cut, max_len);
1609
1610 /* Mark cut parts with '..' on both sides. */
1611 if (cut)
1612 buf[0] = buf[1] = '.';
1613
1614 if ((len_event - cut) > max_len) {
1615 buf[max_len - 1] = buf[max_len - 2] = '.';
1616 buf[max_len] = 0;
1617 }
1618
1619 idx = len_str + err->idx - cut;
1620 }
1621
1622 fprintf(stderr, "%s'%s'\n", str, buf);
1623 if (idx) {
1624 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
1625 if (err->help)
1626 fprintf(stderr, "\n%s\n", err->help);
1627 free(err->str);
1628 free(err->help);
1629 }
1630
1631 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
1632}
1633
1634#undef MAX_WIDTH
1635
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001636int parse_events_option(const struct option *opt, const char *str,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001637 int unset __maybe_unused)
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001638{
1639 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
Jiri Olsab39b8392015-04-22 21:10:16 +02001640 struct parse_events_error err = { .idx = 0, };
1641 int ret = parse_events(evlist, str, &err);
Andi Kleen9175ce12012-10-26 13:30:06 -07001642
Jiri Olsab39b8392015-04-22 21:10:16 +02001643 if (ret)
1644 parse_events_print_error(&err, str);
1645
Andi Kleen9175ce12012-10-26 13:30:06 -07001646 return ret;
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001647}
1648
Wang Nan4ba1faa2015-07-10 07:36:10 +00001649static int
1650foreach_evsel_in_last_glob(struct perf_evlist *evlist,
1651 int (*func)(struct perf_evsel *evsel,
1652 const void *arg),
1653 const void *arg)
Li Zefanc171b552009-10-15 11:22:07 +08001654{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001655 struct perf_evsel *last = NULL;
Wang Nan4ba1faa2015-07-10 07:36:10 +00001656 int err;
Li Zefanc171b552009-10-15 11:22:07 +08001657
Wang Nan854f7362015-09-06 07:13:17 +00001658 /*
1659 * Don't return when list_empty, give func a chance to report
1660 * error when it found last == NULL.
1661 *
1662 * So no need to WARN here, let *func do this.
1663 */
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001664 if (evlist->nr_entries > 0)
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001665 last = perf_evlist__last(evlist);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001666
Wang Nan15bfd2c2015-07-10 07:36:09 +00001667 do {
Wang Nan4ba1faa2015-07-10 07:36:10 +00001668 err = (*func)(last, arg);
1669 if (err)
Wang Nan15bfd2c2015-07-10 07:36:09 +00001670 return -1;
Wang Nan4ba1faa2015-07-10 07:36:10 +00001671 if (!last)
1672 return 0;
Wang Nan15bfd2c2015-07-10 07:36:09 +00001673
1674 if (last->node.prev == &evlist->entries)
1675 return 0;
1676 last = list_entry(last->node.prev, struct perf_evsel, node);
1677 } while (!last->cmdline_group_boundary);
Li Zefanc171b552009-10-15 11:22:07 +08001678
1679 return 0;
1680}
1681
Wang Nan4ba1faa2015-07-10 07:36:10 +00001682static int set_filter(struct perf_evsel *evsel, const void *arg)
1683{
1684 const char *str = arg;
1685
1686 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
1687 fprintf(stderr,
1688 "--filter option should follow a -e tracepoint option\n");
1689 return -1;
1690 }
1691
1692 if (perf_evsel__append_filter(evsel, "&&", str) < 0) {
1693 fprintf(stderr,
1694 "not enough memory to hold filter string\n");
1695 return -1;
1696 }
1697
1698 return 0;
1699}
1700
1701int parse_filter(const struct option *opt, const char *str,
1702 int unset __maybe_unused)
1703{
1704 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1705
1706 return foreach_evsel_in_last_glob(evlist, set_filter,
1707 (const void *)str);
1708}
1709
1710static int add_exclude_perf_filter(struct perf_evsel *evsel,
1711 const void *arg __maybe_unused)
1712{
1713 char new_filter[64];
1714
1715 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
1716 fprintf(stderr,
1717 "--exclude-perf option should follow a -e tracepoint option\n");
1718 return -1;
1719 }
1720
1721 snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
1722
1723 if (perf_evsel__append_filter(evsel, "&&", new_filter) < 0) {
1724 fprintf(stderr,
1725 "not enough memory to hold filter string\n");
1726 return -1;
1727 }
1728
1729 return 0;
1730}
1731
1732int exclude_perf(const struct option *opt,
1733 const char *arg __maybe_unused,
1734 int unset __maybe_unused)
1735{
1736 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1737
1738 return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
1739 NULL);
1740}
1741
Thomas Gleixner86847b62009-06-06 12:24:17 +02001742static const char * const event_type_descriptors[] = {
Thomas Gleixner86847b62009-06-06 12:24:17 +02001743 "Hardware event",
1744 "Software event",
1745 "Tracepoint event",
1746 "Hardware cache event",
Liming Wang41bdcb22009-12-29 16:37:07 +08001747 "Raw hardware event descriptor",
1748 "Hardware breakpoint",
Thomas Gleixner86847b62009-06-06 12:24:17 +02001749};
1750
Yunlong Songab0e4802015-02-27 18:21:25 +08001751static int cmp_string(const void *a, const void *b)
1752{
1753 const char * const *as = a;
1754 const char * const *bs = b;
1755
1756 return strcmp(*as, *bs);
1757}
1758
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001759/*
Jason Baronf6bdafe2009-07-21 12:20:22 -04001760 * Print the events from <debugfs_mount_point>/tracing/events
1761 */
1762
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001763void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
1764 bool name_only)
Jason Baronf6bdafe2009-07-21 12:20:22 -04001765{
1766 DIR *sys_dir, *evt_dir;
1767 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001768 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +02001769 char dir_path[MAXPATHLEN];
Yunlong Songab0e4802015-02-27 18:21:25 +08001770 char **evt_list = NULL;
1771 unsigned int evt_i = 0, evt_num = 0;
1772 bool evt_num_known = false;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001773
Yunlong Songab0e4802015-02-27 18:21:25 +08001774restart:
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001775 sys_dir = opendir(tracing_events_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -04001776 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +02001777 return;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001778
Yunlong Songab0e4802015-02-27 18:21:25 +08001779 if (evt_num_known) {
1780 evt_list = zalloc(sizeof(char *) * evt_num);
1781 if (!evt_list)
1782 goto out_close_sys_dir;
1783 }
1784
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03001785 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001786 if (subsys_glob != NULL &&
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001787 !strglobmatch(sys_dirent.d_name, subsys_glob))
1788 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02001789
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001790 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Eric Dumazet725b1362009-09-24 15:39:09 +02001791 sys_dirent.d_name);
1792 evt_dir = opendir(dir_path);
1793 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03001794 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02001795
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03001796 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001797 if (event_glob != NULL &&
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001798 !strglobmatch(evt_dirent.d_name, event_glob))
1799 continue;
1800
Yunlong Songab0e4802015-02-27 18:21:25 +08001801 if (!evt_num_known) {
1802 evt_num++;
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001803 continue;
1804 }
1805
Jason Baronf6bdafe2009-07-21 12:20:22 -04001806 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1807 sys_dirent.d_name, evt_dirent.d_name);
Yunlong Songab0e4802015-02-27 18:21:25 +08001808
1809 evt_list[evt_i] = strdup(evt_path);
1810 if (evt_list[evt_i] == NULL)
1811 goto out_close_evt_dir;
1812 evt_i++;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001813 }
1814 closedir(evt_dir);
1815 }
Jason Baronf6bdafe2009-07-21 12:20:22 -04001816 closedir(sys_dir);
Yunlong Songab0e4802015-02-27 18:21:25 +08001817
1818 if (!evt_num_known) {
1819 evt_num_known = true;
1820 goto restart;
1821 }
1822 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1823 evt_i = 0;
1824 while (evt_i < evt_num) {
1825 if (name_only) {
1826 printf("%s ", evt_list[evt_i++]);
1827 continue;
1828 }
1829 printf(" %-50s [%s]\n", evt_list[evt_i++],
1830 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1831 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03001832 if (evt_num && pager_in_use())
Yunlong Songab0e4802015-02-27 18:21:25 +08001833 printf("\n");
1834
1835out_free:
1836 evt_num = evt_i;
1837 for (evt_i = 0; evt_i < evt_num; evt_i++)
1838 zfree(&evt_list[evt_i]);
1839 zfree(&evt_list);
1840 return;
1841
1842out_close_evt_dir:
1843 closedir(evt_dir);
1844out_close_sys_dir:
1845 closedir(sys_dir);
1846
1847 printf("FATAL: not enough memory to print %s\n",
1848 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1849 if (evt_list)
1850 goto out_free;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001851}
1852
1853/*
Thomas Renninger20c457b2011-01-03 17:50:45 +01001854 * Check whether event is in <debugfs_mount_point>/tracing/events
1855 */
1856
1857int is_valid_tracepoint(const char *event_string)
1858{
1859 DIR *sys_dir, *evt_dir;
1860 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
1861 char evt_path[MAXPATHLEN];
1862 char dir_path[MAXPATHLEN];
1863
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001864 sys_dir = opendir(tracing_events_path);
Thomas Renninger20c457b2011-01-03 17:50:45 +01001865 if (!sys_dir)
1866 return 0;
1867
1868 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1869
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001870 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Thomas Renninger20c457b2011-01-03 17:50:45 +01001871 sys_dirent.d_name);
1872 evt_dir = opendir(dir_path);
1873 if (!evt_dir)
1874 continue;
1875
1876 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1877 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1878 sys_dirent.d_name, evt_dirent.d_name);
1879 if (!strcmp(evt_path, event_string)) {
1880 closedir(evt_dir);
1881 closedir(sys_dir);
1882 return 1;
1883 }
1884 }
1885 closedir(evt_dir);
1886 }
1887 closedir(sys_dir);
1888 return 0;
1889}
1890
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001891static bool is_event_supported(u8 type, unsigned config)
1892{
1893 bool ret = true;
Vince Weaver88fee522013-12-30 15:39:45 -05001894 int open_return;
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001895 struct perf_evsel *evsel;
1896 struct perf_event_attr attr = {
1897 .type = type,
1898 .config = config,
1899 .disabled = 1,
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001900 };
1901 struct {
1902 struct thread_map map;
1903 int threads[1];
1904 } tmap = {
1905 .map.nr = 1,
1906 .threads = { 0 },
1907 };
1908
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03001909 evsel = perf_evsel__new(&attr);
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001910 if (evsel) {
Vince Weaver88fee522013-12-30 15:39:45 -05001911 open_return = perf_evsel__open(evsel, NULL, &tmap.map);
1912 ret = open_return >= 0;
1913
1914 if (open_return == -EACCES) {
1915 /*
1916 * This happens if the paranoid value
1917 * /proc/sys/kernel/perf_event_paranoid is set to 2
1918 * Re-run with exclude_kernel set; we don't do that
1919 * by default as some ARM machines do not support it.
1920 *
1921 */
1922 evsel->attr.exclude_kernel = 1;
1923 ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0;
1924 }
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001925 perf_evsel__delete(evsel);
1926 }
1927
1928 return ret;
1929}
1930
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001931int print_hwcache_events(const char *event_glob, bool name_only)
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001932{
Yunlong Songab0e4802015-02-27 18:21:25 +08001933 unsigned int type, op, i, evt_i = 0, evt_num = 0;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03001934 char name[64];
Yunlong Songab0e4802015-02-27 18:21:25 +08001935 char **evt_list = NULL;
1936 bool evt_num_known = false;
1937
1938restart:
1939 if (evt_num_known) {
1940 evt_list = zalloc(sizeof(char *) * evt_num);
1941 if (!evt_list)
1942 goto out_enomem;
1943 }
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001944
1945 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1946 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1947 /* skip invalid cache type */
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03001948 if (!perf_evsel__is_cache_op_valid(type, op))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001949 continue;
1950
1951 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03001952 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
1953 name, sizeof(name));
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001954 if (event_glob != NULL && !strglobmatch(name, event_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001955 continue;
1956
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001957 if (!is_event_supported(PERF_TYPE_HW_CACHE,
1958 type | (op << 8) | (i << 16)))
1959 continue;
1960
Yunlong Songab0e4802015-02-27 18:21:25 +08001961 if (!evt_num_known) {
1962 evt_num++;
1963 continue;
1964 }
1965
1966 evt_list[evt_i] = strdup(name);
1967 if (evt_list[evt_i] == NULL)
1968 goto out_enomem;
1969 evt_i++;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001970 }
1971 }
1972 }
1973
Yunlong Songab0e4802015-02-27 18:21:25 +08001974 if (!evt_num_known) {
1975 evt_num_known = true;
1976 goto restart;
1977 }
1978 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1979 evt_i = 0;
1980 while (evt_i < evt_num) {
1981 if (name_only) {
1982 printf("%s ", evt_list[evt_i++]);
1983 continue;
1984 }
1985 printf(" %-50s [%s]\n", evt_list[evt_i++],
1986 event_type_descriptors[PERF_TYPE_HW_CACHE]);
1987 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03001988 if (evt_num && pager_in_use())
Andi Kleendc098b32013-04-20 11:02:29 -07001989 printf("\n");
Yunlong Songab0e4802015-02-27 18:21:25 +08001990
1991out_free:
1992 evt_num = evt_i;
1993 for (evt_i = 0; evt_i < evt_num; evt_i++)
1994 zfree(&evt_list[evt_i]);
1995 zfree(&evt_list);
1996 return evt_num;
1997
1998out_enomem:
1999 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
2000 if (evt_list)
2001 goto out_free;
2002 return evt_num;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002003}
2004
Yunlong Song705750f2015-02-27 18:21:27 +08002005void print_symbol_events(const char *event_glob, unsigned type,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002006 struct event_symbol *syms, unsigned max,
2007 bool name_only)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002008{
Yunlong Songab0e4802015-02-27 18:21:25 +08002009 unsigned int i, evt_i = 0, evt_num = 0;
Ingo Molnar947b4ad2011-04-29 22:52:42 +02002010 char name[MAX_NAME_LEN];
Yunlong Songab0e4802015-02-27 18:21:25 +08002011 char **evt_list = NULL;
2012 bool evt_num_known = false;
2013
2014restart:
2015 if (evt_num_known) {
2016 evt_list = zalloc(sizeof(char *) * evt_num);
2017 if (!evt_list)
2018 goto out_enomem;
2019 syms -= max;
2020 }
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002021
Jiri Olsa1dc127602012-07-04 00:00:44 +02002022 for (i = 0; i < max; i++, syms++) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002023
Arnaldo Carvalho de Meloe37df6c2015-11-27 16:04:58 -03002024 if (event_glob != NULL && syms->symbol != NULL &&
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002025 !(strglobmatch(syms->symbol, event_glob) ||
2026 (syms->alias && strglobmatch(syms->alias, event_glob))))
2027 continue;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002028
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002029 if (!is_event_supported(type, i))
2030 continue;
2031
Yunlong Songab0e4802015-02-27 18:21:25 +08002032 if (!evt_num_known) {
2033 evt_num++;
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002034 continue;
2035 }
2036
Yunlong Songab0e4802015-02-27 18:21:25 +08002037 if (!name_only && strlen(syms->alias))
Ingo Molnar947b4ad2011-04-29 22:52:42 +02002038 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05302039 else
Ingo Molnar947b4ad2011-04-29 22:52:42 +02002040 strncpy(name, syms->symbol, MAX_NAME_LEN);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002041
Yunlong Songab0e4802015-02-27 18:21:25 +08002042 evt_list[evt_i] = strdup(name);
2043 if (evt_list[evt_i] == NULL)
2044 goto out_enomem;
2045 evt_i++;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002046 }
2047
Yunlong Songab0e4802015-02-27 18:21:25 +08002048 if (!evt_num_known) {
2049 evt_num_known = true;
2050 goto restart;
2051 }
2052 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2053 evt_i = 0;
2054 while (evt_i < evt_num) {
2055 if (name_only) {
2056 printf("%s ", evt_list[evt_i++]);
2057 continue;
2058 }
2059 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
2060 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002061 if (evt_num && pager_in_use())
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002062 printf("\n");
Yunlong Songab0e4802015-02-27 18:21:25 +08002063
2064out_free:
2065 evt_num = evt_i;
2066 for (evt_i = 0; evt_i < evt_num; evt_i++)
2067 zfree(&evt_list[evt_i]);
2068 zfree(&evt_list);
2069 return;
2070
2071out_enomem:
2072 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
2073 if (evt_list)
2074 goto out_free;
Jiri Olsa1dc127602012-07-04 00:00:44 +02002075}
2076
2077/*
2078 * Print the help text for the event symbols:
2079 */
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002080void print_events(const char *event_glob, bool name_only)
Jiri Olsa1dc127602012-07-04 00:00:44 +02002081{
Jiri Olsa1dc127602012-07-04 00:00:44 +02002082 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002083 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
Jiri Olsa1dc127602012-07-04 00:00:44 +02002084
2085 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002086 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
Jiri Olsa1dc127602012-07-04 00:00:44 +02002087
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002088 print_hwcache_events(event_glob, name_only);
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002089
Andi Kleendc098b32013-04-20 11:02:29 -07002090 print_pmu_events(event_glob, name_only);
2091
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002092 if (event_glob != NULL)
2093 return;
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +05302094
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002095 if (!name_only) {
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002096 printf(" %-50s [%s]\n",
2097 "rNNN",
2098 event_type_descriptors[PERF_TYPE_RAW]);
2099 printf(" %-50s [%s]\n",
2100 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
2101 event_type_descriptors[PERF_TYPE_RAW]);
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002102 if (pager_in_use())
2103 printf(" (see 'man perf-list' on how to encode it)\n\n");
Thomas Gleixner86847b62009-06-06 12:24:17 +02002104
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002105 printf(" %-50s [%s]\n",
Jacob Shin3741eb92014-05-29 17:26:51 +02002106 "mem:<addr>[/len][:access]",
Liming Wang41bdcb22009-12-29 16:37:07 +08002107 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002108 if (pager_in_use())
2109 printf("\n");
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002110 }
Frederic Weisbecker1b290d62009-11-23 15:42:35 +01002111
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002112 print_tracepoint_events(NULL, NULL, name_only);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002113}
Jiri Olsa8f707d82012-03-15 20:09:16 +01002114
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002115int parse_events__is_hardcoded_term(struct parse_events_term *term)
Jiri Olsa8f707d82012-03-15 20:09:16 +01002116{
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002117 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002118}
2119
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002120static int new_term(struct parse_events_term **_term, int type_val,
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002121 int type_term, char *config,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002122 char *str, u64 num, int err_term, int err_val)
Jiri Olsa8f707d82012-03-15 20:09:16 +01002123{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002124 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002125
2126 term = zalloc(sizeof(*term));
2127 if (!term)
2128 return -ENOMEM;
2129
2130 INIT_LIST_HEAD(&term->list);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002131 term->type_val = type_val;
2132 term->type_term = type_term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002133 term->config = config;
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002134 term->err_term = err_term;
2135 term->err_val = err_val;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002136
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002137 switch (type_val) {
Jiri Olsa8f707d82012-03-15 20:09:16 +01002138 case PARSE_EVENTS__TERM_TYPE_NUM:
2139 term->val.num = num;
2140 break;
2141 case PARSE_EVENTS__TERM_TYPE_STR:
2142 term->val.str = str;
2143 break;
2144 default:
Adrian Hunter4be8be62013-07-04 16:20:24 +03002145 free(term);
Jiri Olsa8f707d82012-03-15 20:09:16 +01002146 return -EINVAL;
2147 }
2148
2149 *_term = term;
2150 return 0;
2151}
2152
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002153int parse_events_term__num(struct parse_events_term **term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002154 int type_term, char *config, u64 num,
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002155 void *loc_term_, void *loc_val_)
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002156{
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002157 YYLTYPE *loc_term = loc_term_;
2158 YYLTYPE *loc_val = loc_val_;
2159
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002160 return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002161 config, NULL, num,
2162 loc_term ? loc_term->first_column : 0,
2163 loc_val ? loc_val->first_column : 0);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002164}
2165
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002166int parse_events_term__str(struct parse_events_term **term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002167 int type_term, char *config, char *str,
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002168 void *loc_term_, void *loc_val_)
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002169{
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002170 YYLTYPE *loc_term = loc_term_;
2171 YYLTYPE *loc_val = loc_val_;
2172
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002173 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002174 config, str, 0,
2175 loc_term ? loc_term->first_column : 0,
2176 loc_val ? loc_val->first_column : 0);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002177}
2178
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002179int parse_events_term__sym_hw(struct parse_events_term **term,
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02002180 char *config, unsigned idx)
2181{
2182 struct event_symbol *sym;
2183
2184 BUG_ON(idx >= PERF_COUNT_HW_MAX);
2185 sym = &event_symbols_hw[idx];
2186
2187 if (config)
2188 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
2189 PARSE_EVENTS__TERM_TYPE_USER, config,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002190 (char *) sym->symbol, 0, 0, 0);
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02002191 else
2192 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
2193 PARSE_EVENTS__TERM_TYPE_USER,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002194 (char *) "event", (char *) sym->symbol,
2195 0, 0, 0);
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02002196}
2197
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002198int parse_events_term__clone(struct parse_events_term **new,
2199 struct parse_events_term *term)
Zheng Yana6146d52012-06-15 14:31:41 +08002200{
2201 return new_term(new, term->type_val, term->type_term, term->config,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002202 term->val.str, term->val.num,
2203 term->err_term, term->err_val);
Zheng Yana6146d52012-06-15 14:31:41 +08002204}
2205
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002206void parse_events_terms__purge(struct list_head *terms)
Jiri Olsa8f707d82012-03-15 20:09:16 +01002207{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002208 struct parse_events_term *term, *h;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002209
Wang Nana8adfce2016-02-12 16:31:23 -03002210 list_for_each_entry_safe(term, h, terms, list) {
2211 list_del_init(&term->list);
Jiri Olsa8f707d82012-03-15 20:09:16 +01002212 free(term);
Wang Nana8adfce2016-02-12 16:31:23 -03002213 }
Jiri Olsa8f707d82012-03-15 20:09:16 +01002214}
Jiri Olsab39b8392015-04-22 21:10:16 +02002215
Arnaldo Carvalho de Melo2146afc2016-02-12 17:09:17 -03002216void parse_events_terms__delete(struct list_head *terms)
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002217{
Arnaldo Carvalho de Melo2146afc2016-02-12 17:09:17 -03002218 if (!terms)
2219 return;
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002220 parse_events_terms__purge(terms);
Wang Nand20a5f22016-02-12 17:01:17 -03002221 free(terms);
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002222}
2223
Jiri Olsab39b8392015-04-22 21:10:16 +02002224void parse_events_evlist_error(struct parse_events_evlist *data,
2225 int idx, const char *str)
2226{
2227 struct parse_events_error *err = data->error;
2228
Adrian Huntera6ced2b2015-05-19 16:05:44 +03002229 if (!err)
2230 return;
Jiri Olsab39b8392015-04-22 21:10:16 +02002231 err->idx = idx;
2232 err->str = strdup(str);
2233 WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
2234}
He Kuangffeb8832015-09-28 03:52:14 +00002235
Wang Nan17cb5f82016-02-19 11:43:57 +00002236static void config_terms_list(char *buf, size_t buf_sz)
2237{
2238 int i;
2239 bool first = true;
2240
2241 buf[0] = '\0';
2242 for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
2243 const char *name = config_term_names[i];
2244
Wang Nan1669e502016-02-19 11:43:58 +00002245 if (!config_term_avail(i, NULL))
2246 continue;
Wang Nan17cb5f82016-02-19 11:43:57 +00002247 if (!name)
2248 continue;
2249 if (name[0] == '<')
2250 continue;
2251
2252 if (strlen(buf) + strlen(name) + 2 >= buf_sz)
2253 return;
2254
2255 if (!first)
2256 strcat(buf, ",");
2257 else
2258 first = false;
2259 strcat(buf, name);
2260 }
2261}
2262
He Kuangffeb8832015-09-28 03:52:14 +00002263/*
2264 * Return string contains valid config terms of an event.
2265 * @additional_terms: For terms such as PMU sysfs terms.
2266 */
2267char *parse_events_formats_error_string(char *additional_terms)
2268{
2269 char *str;
Wang Nan17cb5f82016-02-19 11:43:57 +00002270 /* "branch_type" is the longest name */
2271 char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
2272 (sizeof("branch_type") - 1)];
He Kuangffeb8832015-09-28 03:52:14 +00002273
Wang Nan17cb5f82016-02-19 11:43:57 +00002274 config_terms_list(static_terms, sizeof(static_terms));
He Kuangffeb8832015-09-28 03:52:14 +00002275 /* valid terms */
2276 if (additional_terms) {
Wang Nan26dee022016-02-19 11:43:52 +00002277 if (asprintf(&str, "valid terms: %s,%s",
2278 additional_terms, static_terms) < 0)
He Kuangffeb8832015-09-28 03:52:14 +00002279 goto fail;
2280 } else {
Wang Nan26dee022016-02-19 11:43:52 +00002281 if (asprintf(&str, "valid terms: %s", static_terms) < 0)
He Kuangffeb8832015-09-28 03:52:14 +00002282 goto fail;
2283 }
2284 return str;
2285
2286fail:
2287 return NULL;
2288}