blob: b0b329539db533937d02372b3429db9361529202 [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
675int parse_events_load_bpf(struct parse_events_evlist *data,
676 struct list_head *list,
Wang Nand509db02015-10-14 12:41:20 +0000677 char *bpf_file_name,
678 bool source)
Wang Nan84c86ca2015-10-14 12:41:14 +0000679{
680 struct bpf_object *obj;
681
Wang Nand509db02015-10-14 12:41:20 +0000682 obj = bpf__prepare_load(bpf_file_name, source);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000683 if (IS_ERR(obj)) {
Wang Nan84c86ca2015-10-14 12:41:14 +0000684 char errbuf[BUFSIZ];
685 int err;
686
Wang Nan6371ca3b2015-11-06 13:49:37 +0000687 err = PTR_ERR(obj);
Wang Nan84c86ca2015-10-14 12:41:14 +0000688
689 if (err == -ENOTSUP)
690 snprintf(errbuf, sizeof(errbuf),
691 "BPF support is not compiled");
692 else
Wang Nand3e0ce32015-11-06 13:58:09 +0000693 bpf__strerror_prepare_load(bpf_file_name,
694 source,
695 -err, errbuf,
696 sizeof(errbuf));
Wang Nan84c86ca2015-10-14 12:41:14 +0000697
698 data->error->help = strdup("(add -v to see detail)");
699 data->error->str = strdup(errbuf);
700 return err;
701 }
702
703 return parse_events_load_bpf_obj(data, list, obj);
704}
705
Jiri Olsa89812fc2012-03-15 20:09:15 +0100706static int
707parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100708{
709 int i;
710
711 for (i = 0; i < 3; i++) {
Jiri Olsa89812fc2012-03-15 20:09:15 +0100712 if (!type || !type[i])
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100713 break;
714
Jiri Olsa75827322012-06-29 09:22:54 +0200715#define CHECK_SET_TYPE(bit) \
716do { \
717 if (attr->bp_type & bit) \
718 return -EINVAL; \
719 else \
720 attr->bp_type |= bit; \
721} while (0)
722
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100723 switch (type[i]) {
724 case 'r':
Jiri Olsa75827322012-06-29 09:22:54 +0200725 CHECK_SET_TYPE(HW_BREAKPOINT_R);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100726 break;
727 case 'w':
Jiri Olsa75827322012-06-29 09:22:54 +0200728 CHECK_SET_TYPE(HW_BREAKPOINT_W);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100729 break;
730 case 'x':
Jiri Olsa75827322012-06-29 09:22:54 +0200731 CHECK_SET_TYPE(HW_BREAKPOINT_X);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100732 break;
733 default:
Jiri Olsa89812fc2012-03-15 20:09:15 +0100734 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100735 }
736 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100737
Jiri Olsa75827322012-06-29 09:22:54 +0200738#undef CHECK_SET_TYPE
739
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100740 if (!attr->bp_type) /* Default */
741 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
742
Jiri Olsa89812fc2012-03-15 20:09:15 +0100743 return 0;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100744}
745
David Ahernc5cd8ac2013-07-02 13:27:25 -0600746int parse_events_add_breakpoint(struct list_head *list, int *idx,
Jacob Shin3741eb92014-05-29 17:26:51 +0200747 void *ptr, char *type, u64 len)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100748{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100749 struct perf_event_attr attr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100750
Jiri Olsa89812fc2012-03-15 20:09:15 +0100751 memset(&attr, 0, sizeof(attr));
Jiri Olsa9fafd982012-03-20 19:15:39 +0100752 attr.bp_addr = (unsigned long) ptr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100753
Jiri Olsa89812fc2012-03-15 20:09:15 +0100754 if (parse_breakpoint_type(type, &attr))
755 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100756
Jacob Shin3741eb92014-05-29 17:26:51 +0200757 /* Provide some defaults if len is not specified */
758 if (!len) {
759 if (attr.bp_type == HW_BREAKPOINT_X)
760 len = sizeof(long);
761 else
762 len = HW_BREAKPOINT_LEN_4;
763 }
764
765 attr.bp_len = len;
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200766
Jiri Olsa89812fc2012-03-15 20:09:15 +0100767 attr.type = PERF_TYPE_BREAKPOINT;
Jovi Zhang4a841d62012-07-15 03:03:10 +0800768 attr.sample_period = 1;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100769
Jiri Olsa930a2e22015-07-29 05:42:10 -0400770 return add_event(list, idx, &attr, NULL, NULL);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100771}
772
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200773static int check_type_val(struct parse_events_term *term,
774 struct parse_events_error *err,
775 int type)
Jiri Olsa8f707d82012-03-15 20:09:16 +0100776{
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200777 if (type == term->type_val)
778 return 0;
779
780 if (err) {
781 err->idx = term->err_val;
782 if (type == PARSE_EVENTS__TERM_TYPE_NUM)
783 err->str = strdup("expected numeric value");
784 else
785 err->str = strdup("expected string value");
786 }
787 return -EINVAL;
788}
789
Wang Nan17cb5f82016-02-19 11:43:57 +0000790/*
791 * Update according to parse-events.l
792 */
793static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
794 [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>",
795 [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config",
796 [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1",
797 [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2",
798 [PARSE_EVENTS__TERM_TYPE_NAME] = "name",
799 [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period",
800 [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq",
801 [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type",
802 [PARSE_EVENTS__TERM_TYPE_TIME] = "time",
803 [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph",
804 [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size",
805 [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit",
806 [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit",
807};
808
Wang Nan1669e502016-02-19 11:43:58 +0000809static bool config_term_shrinked;
810
811static bool
812config_term_avail(int term_type, struct parse_events_error *err)
813{
814 if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
815 err->str = strdup("Invalid term_type");
816 return false;
817 }
818 if (!config_term_shrinked)
819 return true;
820
821 switch (term_type) {
822 case PARSE_EVENTS__TERM_TYPE_CONFIG:
823 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
824 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
825 case PARSE_EVENTS__TERM_TYPE_NAME:
826 return true;
827 default:
828 if (!err)
829 return false;
830
831 /* term_type is validated so indexing is safe */
832 if (asprintf(&err->str, "'%s' is not usable in 'perf stat'",
833 config_term_names[term_type]) < 0)
834 err->str = NULL;
835 return false;
836 }
837}
838
839void parse_events__shrink_config_terms(void)
840{
841 config_term_shrinked = true;
842}
843
He Kuang0b8891a2015-09-28 03:52:13 +0000844static int config_term_common(struct perf_event_attr *attr,
845 struct parse_events_term *term,
846 struct parse_events_error *err)
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200847{
848#define CHECK_TYPE_VAL(type) \
849do { \
850 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
851 return -EINVAL; \
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200852} while (0)
853
854 switch (term->type_term) {
Jiri Olsa8f707d82012-03-15 20:09:16 +0100855 case PARSE_EVENTS__TERM_TYPE_CONFIG:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200856 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100857 attr->config = term->val.num;
858 break;
859 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200860 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100861 attr->config1 = term->val.num;
862 break;
863 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200864 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100865 attr->config2 = term->val.num;
866 break;
867 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200868 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100869 break;
Namhyung Kim09af2a52015-08-09 15:45:23 +0900870 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
871 CHECK_TYPE_VAL(NUM);
872 break;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100873 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
874 /*
875 * TODO uncomment when the field is available
876 * attr->branch_sample_type = term->val.num;
877 */
878 break;
Kan Liang32067712015-08-04 04:30:19 -0400879 case PARSE_EVENTS__TERM_TYPE_TIME:
880 CHECK_TYPE_VAL(NUM);
881 if (term->val.num > 1) {
882 err->str = strdup("expected 0 or 1");
883 err->idx = term->err_val;
884 return -EINVAL;
885 }
886 break;
Kan Liangd457c962015-08-11 06:30:47 -0400887 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
888 CHECK_TYPE_VAL(STR);
889 break;
890 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
891 CHECK_TYPE_VAL(NUM);
892 break;
Wang Nan374ce932015-10-28 10:55:02 +0000893 case PARSE_EVENTS__TERM_TYPE_INHERIT:
894 CHECK_TYPE_VAL(NUM);
895 break;
896 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
897 CHECK_TYPE_VAL(NUM);
898 break;
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200899 case PARSE_EVENTS__TERM_TYPE_NAME:
900 CHECK_TYPE_VAL(STR);
901 break;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100902 default:
He Kuangffeb8832015-09-28 03:52:14 +0000903 err->str = strdup("unknown term");
904 err->idx = term->err_term;
905 err->help = parse_events_formats_error_string(NULL);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100906 return -EINVAL;
907 }
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200908
Wang Nan1669e502016-02-19 11:43:58 +0000909 /*
910 * Check term availbility after basic checking so
911 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
912 *
913 * If check availbility at the entry of this function,
914 * user will see "'<sysfs term>' is not usable in 'perf stat'"
915 * if an invalid config term is provided for legacy events
916 * (for example, instructions/badterm/...), which is confusing.
917 */
918 if (!config_term_avail(term->type_term, err))
919 return -EINVAL;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100920 return 0;
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200921#undef CHECK_TYPE_VAL
Jiri Olsa8f707d82012-03-15 20:09:16 +0100922}
923
He Kuang0b8891a2015-09-28 03:52:13 +0000924static int config_term_pmu(struct perf_event_attr *attr,
925 struct parse_events_term *term,
926 struct parse_events_error *err)
927{
928 if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER)
929 /*
930 * Always succeed for sysfs terms, as we dont know
931 * at this point what type they need to have.
932 */
933 return 0;
934 else
935 return config_term_common(attr, term, err);
936}
937
He Kuange637d172015-09-28 03:52:16 +0000938static int config_term_tracepoint(struct perf_event_attr *attr,
939 struct parse_events_term *term,
940 struct parse_events_error *err)
941{
942 switch (term->type_term) {
943 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
944 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
Wang Nan374ce932015-10-28 10:55:02 +0000945 case PARSE_EVENTS__TERM_TYPE_INHERIT:
946 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
He Kuange637d172015-09-28 03:52:16 +0000947 return config_term_common(attr, term, err);
948 default:
949 if (err) {
950 err->idx = term->err_term;
951 err->str = strdup("unknown term");
952 err->help = strdup("valid terms: call-graph,stack-size\n");
953 }
954 return -EINVAL;
955 }
956
957 return 0;
958}
959
Jiri Olsa8f707d82012-03-15 20:09:16 +0100960static int config_attr(struct perf_event_attr *attr,
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200961 struct list_head *head,
He Kuang0b8891a2015-09-28 03:52:13 +0000962 struct parse_events_error *err,
963 config_term_func_t config_term)
Jiri Olsa8f707d82012-03-15 20:09:16 +0100964{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300965 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100966
967 list_for_each_entry(term, head, list)
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200968 if (config_term(attr, term, err))
Jiri Olsa8f707d82012-03-15 20:09:16 +0100969 return -EINVAL;
970
971 return 0;
972}
973
Jiri Olsa930a2e22015-07-29 05:42:10 -0400974static int get_config_terms(struct list_head *head_config,
975 struct list_head *head_terms __maybe_unused)
976{
977#define ADD_CONFIG_TERM(__type, __name, __val) \
978do { \
979 struct perf_evsel_config_term *__t; \
980 \
981 __t = zalloc(sizeof(*__t)); \
982 if (!__t) \
983 return -ENOMEM; \
984 \
985 INIT_LIST_HEAD(&__t->list); \
986 __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \
987 __t->val.__name = __val; \
988 list_add_tail(&__t->list, head_terms); \
989} while (0)
990
991 struct parse_events_term *term;
992
993 list_for_each_entry(term, head_config, list) {
994 switch (term->type_term) {
Jiri Olsaee4c7582015-07-29 05:42:11 -0400995 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
996 ADD_CONFIG_TERM(PERIOD, period, term->val.num);
Kan Liang32067712015-08-04 04:30:19 -0400997 break;
Namhyung Kim09af2a52015-08-09 15:45:23 +0900998 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
999 ADD_CONFIG_TERM(FREQ, freq, term->val.num);
1000 break;
Kan Liang32067712015-08-04 04:30:19 -04001001 case PARSE_EVENTS__TERM_TYPE_TIME:
1002 ADD_CONFIG_TERM(TIME, time, term->val.num);
1003 break;
Kan Liangd457c962015-08-11 06:30:47 -04001004 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1005 ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
1006 break;
1007 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1008 ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
1009 break;
Wang Nan374ce932015-10-28 10:55:02 +00001010 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1011 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0);
1012 break;
1013 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1014 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1);
1015 break;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001016 default:
1017 break;
1018 }
1019 }
1020#undef ADD_EVSEL_CONFIG
1021 return 0;
1022}
1023
He Kuange637d172015-09-28 03:52:16 +00001024int parse_events_add_tracepoint(struct list_head *list, int *idx,
1025 char *sys, char *event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001026 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +00001027 struct list_head *head_config)
1028{
1029 if (head_config) {
1030 struct perf_event_attr attr;
1031
Jiri Olsa272ed292015-09-29 17:05:31 +02001032 if (config_attr(&attr, head_config, err,
He Kuange637d172015-09-28 03:52:16 +00001033 config_term_tracepoint))
1034 return -EINVAL;
1035 }
1036
1037 if (strpbrk(sys, "*?"))
1038 return add_tracepoint_multi_sys(list, idx, sys, event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001039 err, head_config);
He Kuange637d172015-09-28 03:52:16 +00001040 else
1041 return add_tracepoint_event(list, idx, sys, event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001042 err, head_config);
He Kuange637d172015-09-28 03:52:16 +00001043}
1044
Jiri Olsa87d650b2015-04-22 21:10:24 +02001045int parse_events_add_numeric(struct parse_events_evlist *data,
1046 struct list_head *list,
Robert Richterb527bab2012-08-07 19:43:13 +02001047 u32 type, u64 config,
Jiri Olsa8f707d82012-03-15 20:09:16 +01001048 struct list_head *head_config)
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301049{
Jiri Olsa89812fc2012-03-15 20:09:15 +01001050 struct perf_event_attr attr;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001051 LIST_HEAD(config_terms);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301052
Jiri Olsa89812fc2012-03-15 20:09:15 +01001053 memset(&attr, 0, sizeof(attr));
1054 attr.type = type;
1055 attr.config = config;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001056
Jiri Olsa930a2e22015-07-29 05:42:10 -04001057 if (head_config) {
He Kuang0b8891a2015-09-28 03:52:13 +00001058 if (config_attr(&attr, head_config, data->error,
1059 config_term_common))
Jiri Olsa930a2e22015-07-29 05:42:10 -04001060 return -EINVAL;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001061
Jiri Olsa930a2e22015-07-29 05:42:10 -04001062 if (get_config_terms(head_config, &config_terms))
1063 return -ENOMEM;
1064 }
1065
Wang Nan10bf3582016-02-19 11:44:00 +00001066 return add_event(list, &data->idx, &attr,
1067 get_config_name(head_config), &config_terms);
Paul Mackerras61c45982009-07-01 13:04:34 +10001068}
1069
Jiri Olsa36adec82015-04-22 21:10:19 +02001070int parse_events_add_pmu(struct parse_events_evlist *data,
1071 struct list_head *list, char *name,
1072 struct list_head *head_config)
Jiri Olsa5f537a22012-03-15 20:09:18 +01001073{
1074 struct perf_event_attr attr;
Matt Fleming46441bd2014-09-24 15:04:06 +01001075 struct perf_pmu_info info;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001076 struct perf_pmu *pmu;
Stephane Eranian410136f2013-11-12 17:58:49 +01001077 struct perf_evsel *evsel;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001078 LIST_HEAD(config_terms);
Jiri Olsa5f537a22012-03-15 20:09:18 +01001079
1080 pmu = perf_pmu__find(name);
1081 if (!pmu)
1082 return -EINVAL;
1083
Adrian Hunterdc0a6202014-07-31 09:00:49 +03001084 if (pmu->default_config) {
1085 memcpy(&attr, pmu->default_config,
1086 sizeof(struct perf_event_attr));
1087 } else {
1088 memset(&attr, 0, sizeof(attr));
1089 }
Jiri Olsa5f537a22012-03-15 20:09:18 +01001090
Adrian Hunterad962272014-08-15 22:08:40 +03001091 if (!head_config) {
1092 attr.type = pmu->type;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001093 evsel = __add_event(list, &data->idx, &attr, NULL, pmu->cpus, NULL);
Adrian Hunterad962272014-08-15 22:08:40 +03001094 return evsel ? 0 : -ENOMEM;
1095 }
1096
Matt Fleming46441bd2014-09-24 15:04:06 +01001097 if (perf_pmu__check_alias(pmu, head_config, &info))
Zheng Yana6146d52012-06-15 14:31:41 +08001098 return -EINVAL;
1099
Jiri Olsa5f537a22012-03-15 20:09:18 +01001100 /*
1101 * Configure hardcoded terms first, no need to check
1102 * return value when called with fail == 0 ;)
1103 */
He Kuang0b8891a2015-09-28 03:52:13 +00001104 if (config_attr(&attr, head_config, data->error, config_term_pmu))
Jiri Olsac056ba6a2015-04-22 21:10:18 +02001105 return -EINVAL;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001106
Jiri Olsa930a2e22015-07-29 05:42:10 -04001107 if (get_config_terms(head_config, &config_terms))
1108 return -ENOMEM;
1109
Jiri Olsae64b0202015-04-22 21:10:21 +02001110 if (perf_pmu__config(pmu, &attr, head_config, data->error))
Jiri Olsa5f537a22012-03-15 20:09:18 +01001111 return -EINVAL;
1112
Jiri Olsa36adec82015-04-22 21:10:19 +02001113 evsel = __add_event(list, &data->idx, &attr,
Wang Nane814fdd2016-02-19 11:43:59 +00001114 get_config_name(head_config), pmu->cpus,
Jiri Olsa930a2e22015-07-29 05:42:10 -04001115 &config_terms);
Stephane Eranian410136f2013-11-12 17:58:49 +01001116 if (evsel) {
Matt Fleming46441bd2014-09-24 15:04:06 +01001117 evsel->unit = info.unit;
1118 evsel->scale = info.scale;
Matt Fleming044330c2014-11-21 10:31:12 +01001119 evsel->per_pkg = info.per_pkg;
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001120 evsel->snapshot = info.snapshot;
Stephane Eranian410136f2013-11-12 17:58:49 +01001121 }
1122
1123 return evsel ? 0 : -ENOMEM;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001124}
1125
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001126int parse_events__modifier_group(struct list_head *list,
1127 char *event_mod)
Jiri Olsa89efb022012-08-08 12:14:14 +02001128{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001129 return parse_events__modifier_event(list, event_mod, true);
1130}
1131
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -03001132void parse_events__set_leader(char *name, struct list_head *list)
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001133{
1134 struct perf_evsel *leader;
1135
Wang Nan854f7362015-09-06 07:13:17 +00001136 if (list_empty(list)) {
1137 WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1138 return;
1139 }
1140
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -03001141 __perf_evlist__set_leader(list);
1142 leader = list_entry(list->next, struct perf_evsel, node);
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001143 leader->group_name = name ? strdup(name) : NULL;
Jiri Olsa89efb022012-08-08 12:14:14 +02001144}
1145
David Ahernc5cd8ac2013-07-02 13:27:25 -06001146/* list_event is assumed to point to malloc'ed memory */
Jiri Olsa5d7be902012-03-20 19:15:40 +01001147void parse_events_update_lists(struct list_head *list_event,
1148 struct list_head *list_all)
1149{
1150 /*
1151 * Called for single event definition. Update the
Jiri Olsa89efb022012-08-08 12:14:14 +02001152 * 'all event' list, and reinit the 'single event'
Jiri Olsa5d7be902012-03-20 19:15:40 +01001153 * list, for next event definition.
1154 */
1155 list_splice_tail(list_event, list_all);
Jiri Olsab847cbd2012-05-21 09:12:51 +02001156 free(list_event);
Jiri Olsa5d7be902012-03-20 19:15:40 +01001157}
1158
Jiri Olsaf5b11352012-08-08 12:21:54 +02001159struct event_modifier {
1160 int eu;
1161 int ek;
1162 int eh;
1163 int eH;
1164 int eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001165 int eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001166 int precise;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001167 int precise_max;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001168 int exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02001169 int sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001170 int pinned;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001171};
Paul Mackerras61c45982009-07-01 13:04:34 +10001172
Jiri Olsaf5b11352012-08-08 12:21:54 +02001173static int get_event_modifier(struct event_modifier *mod, char *str,
1174 struct perf_evsel *evsel)
1175{
1176 int eu = evsel ? evsel->attr.exclude_user : 0;
1177 int ek = evsel ? evsel->attr.exclude_kernel : 0;
1178 int eh = evsel ? evsel->attr.exclude_hv : 0;
1179 int eH = evsel ? evsel->attr.exclude_host : 0;
1180 int eG = evsel ? evsel->attr.exclude_guest : 0;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001181 int eI = evsel ? evsel->attr.exclude_idle : 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001182 int precise = evsel ? evsel->attr.precise_ip : 0;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001183 int precise_max = 0;
Jiri Olsa3c176312012-10-10 17:39:03 +02001184 int sample_read = 0;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001185 int pinned = evsel ? evsel->attr.pinned : 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001186
1187 int exclude = eu | ek | eh;
1188 int exclude_GH = evsel ? evsel->exclude_GH : 0;
1189
Jiri Olsaf5b11352012-08-08 12:21:54 +02001190 memset(mod, 0, sizeof(*mod));
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001191
Paul Mackerras61c45982009-07-01 13:04:34 +10001192 while (*str) {
Peter Zijlstraab608342010-04-08 23:03:20 +02001193 if (*str == 'u') {
1194 if (!exclude)
1195 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001196 eu = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +02001197 } else if (*str == 'k') {
1198 if (!exclude)
1199 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001200 ek = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +02001201 } else if (*str == 'h') {
1202 if (!exclude)
1203 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001204 eh = 0;
Joerg Roedel99320cc2012-01-04 17:54:19 +01001205 } else if (*str == 'G') {
1206 if (!exclude_GH)
1207 exclude_GH = eG = eH = 1;
1208 eG = 0;
1209 } else if (*str == 'H') {
1210 if (!exclude_GH)
1211 exclude_GH = eG = eH = 1;
1212 eH = 0;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001213 } else if (*str == 'I') {
1214 eI = 1;
Peter Zijlstraab608342010-04-08 23:03:20 +02001215 } else if (*str == 'p') {
1216 precise++;
David Ahern13427982012-09-13 14:59:13 -06001217 /* use of precise requires exclude_guest */
1218 if (!exclude_GH)
1219 eG = 1;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001220 } else if (*str == 'P') {
1221 precise_max = 1;
Jiri Olsa3c176312012-10-10 17:39:03 +02001222 } else if (*str == 'S') {
1223 sample_read = 1;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001224 } else if (*str == 'D') {
1225 pinned = 1;
Peter Zijlstraab608342010-04-08 23:03:20 +02001226 } else
Paul Mackerras61c45982009-07-01 13:04:34 +10001227 break;
Peter Zijlstraab608342010-04-08 23:03:20 +02001228
Paul Mackerras61c45982009-07-01 13:04:34 +10001229 ++str;
1230 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001231
Jiri Olsa89812fc2012-03-15 20:09:15 +01001232 /*
1233 * precise ip:
1234 *
1235 * 0 - SAMPLE_IP can have arbitrary skid
1236 * 1 - SAMPLE_IP must have constant skid
1237 * 2 - SAMPLE_IP requested to have 0 skid
1238 * 3 - SAMPLE_IP must have 0 skid
1239 *
1240 * See also PERF_RECORD_MISC_EXACT_IP
1241 */
1242 if (precise > 3)
1243 return -EINVAL;
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001244
Jiri Olsaf5b11352012-08-08 12:21:54 +02001245 mod->eu = eu;
1246 mod->ek = ek;
1247 mod->eh = eh;
1248 mod->eH = eH;
1249 mod->eG = eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001250 mod->eI = eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001251 mod->precise = precise;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001252 mod->precise_max = precise_max;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001253 mod->exclude_GH = exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02001254 mod->sample_read = sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001255 mod->pinned = pinned;
1256
Jiri Olsaf5b11352012-08-08 12:21:54 +02001257 return 0;
1258}
1259
Jiri Olsa534123f2012-11-13 15:32:58 +01001260/*
1261 * Basic modifier sanity check to validate it contains only one
1262 * instance of any modifier (apart from 'p') present.
1263 */
1264static int check_modifier(char *str)
1265{
1266 char *p = str;
1267
1268 /* The sizeof includes 0 byte as well. */
Jiri Olsa7f94af72015-10-05 20:06:05 +02001269 if (strlen(str) > (sizeof("ukhGHpppPSDI") - 1))
Jiri Olsa534123f2012-11-13 15:32:58 +01001270 return -1;
1271
1272 while (*p) {
1273 if (*p != 'p' && strchr(p + 1, *p))
1274 return -1;
1275 p++;
1276 }
1277
1278 return 0;
1279}
1280
Jiri Olsaf5b11352012-08-08 12:21:54 +02001281int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1282{
1283 struct perf_evsel *evsel;
1284 struct event_modifier mod;
1285
1286 if (str == NULL)
1287 return 0;
1288
Jiri Olsa534123f2012-11-13 15:32:58 +01001289 if (check_modifier(str))
1290 return -EINVAL;
1291
Jiri Olsaf5b11352012-08-08 12:21:54 +02001292 if (!add && get_event_modifier(&mod, str, NULL))
1293 return -EINVAL;
1294
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001295 __evlist__for_each(list, evsel) {
Jiri Olsaf5b11352012-08-08 12:21:54 +02001296 if (add && get_event_modifier(&mod, str, evsel))
1297 return -EINVAL;
1298
1299 evsel->attr.exclude_user = mod.eu;
1300 evsel->attr.exclude_kernel = mod.ek;
1301 evsel->attr.exclude_hv = mod.eh;
1302 evsel->attr.precise_ip = mod.precise;
1303 evsel->attr.exclude_host = mod.eH;
1304 evsel->attr.exclude_guest = mod.eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001305 evsel->attr.exclude_idle = mod.eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001306 evsel->exclude_GH = mod.exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02001307 evsel->sample_read = mod.sample_read;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001308 evsel->precise_max = mod.precise_max;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001309
1310 if (perf_evsel__is_group_leader(evsel))
1311 evsel->attr.pinned = mod.pinned;
Jiri Olsa89812fc2012-03-15 20:09:15 +01001312 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001313
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301314 return 0;
1315}
1316
Robert Richterac2ba9f2012-08-16 21:10:21 +02001317int parse_events_name(struct list_head *list, char *name)
1318{
1319 struct perf_evsel *evsel;
1320
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001321 __evlist__for_each(list, evsel) {
Robert Richterac2ba9f2012-08-16 21:10:21 +02001322 if (!evsel->name)
1323 evsel->name = strdup(name);
1324 }
1325
1326 return 0;
1327}
1328
Kan Liangdcb4e102014-10-07 11:08:50 -04001329static int
1330comp_pmu(const void *p1, const void *p2)
1331{
1332 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
1333 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
1334
1335 return strcmp(pmu1->symbol, pmu2->symbol);
1336}
1337
1338static void perf_pmu__parse_cleanup(void)
1339{
1340 if (perf_pmu_events_list_num > 0) {
1341 struct perf_pmu_event_symbol *p;
1342 int i;
1343
1344 for (i = 0; i < perf_pmu_events_list_num; i++) {
1345 p = perf_pmu_events_list + i;
1346 free(p->symbol);
1347 }
1348 free(perf_pmu_events_list);
1349 perf_pmu_events_list = NULL;
1350 perf_pmu_events_list_num = 0;
1351 }
1352}
1353
1354#define SET_SYMBOL(str, stype) \
1355do { \
1356 p->symbol = str; \
1357 if (!p->symbol) \
1358 goto err; \
1359 p->type = stype; \
1360} while (0)
1361
1362/*
1363 * Read the pmu events list from sysfs
1364 * Save it into perf_pmu_events_list
1365 */
1366static void perf_pmu__parse_init(void)
1367{
1368
1369 struct perf_pmu *pmu = NULL;
1370 struct perf_pmu_alias *alias;
1371 int len = 0;
1372
1373 pmu = perf_pmu__find("cpu");
1374 if ((pmu == NULL) || list_empty(&pmu->aliases)) {
1375 perf_pmu_events_list_num = -1;
1376 return;
1377 }
1378 list_for_each_entry(alias, &pmu->aliases, list) {
1379 if (strchr(alias->name, '-'))
1380 len++;
1381 len++;
1382 }
1383 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1384 if (!perf_pmu_events_list)
1385 return;
1386 perf_pmu_events_list_num = len;
1387
1388 len = 0;
1389 list_for_each_entry(alias, &pmu->aliases, list) {
1390 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1391 char *tmp = strchr(alias->name, '-');
1392
1393 if (tmp != NULL) {
1394 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1395 PMU_EVENT_SYMBOL_PREFIX);
1396 p++;
1397 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1398 len += 2;
1399 } else {
1400 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1401 len++;
1402 }
1403 }
1404 qsort(perf_pmu_events_list, len,
1405 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1406
1407 return;
1408err:
1409 perf_pmu__parse_cleanup();
1410}
1411
1412enum perf_pmu_event_symbol_type
1413perf_pmu__parse_check(const char *name)
1414{
1415 struct perf_pmu_event_symbol p, *r;
1416
1417 /* scan kernel pmu events from sysfs if needed */
1418 if (perf_pmu_events_list_num == 0)
1419 perf_pmu__parse_init();
1420 /*
1421 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1422 * cpu-cycles has been handled by hardcode.
1423 * So it must be cpu// events, not kernel pmu event.
1424 */
1425 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1426 return PMU_EVENT_SYMBOL_ERR;
1427
1428 p.symbol = strdup(name);
1429 r = bsearch(&p, perf_pmu_events_list,
1430 (size_t) perf_pmu_events_list_num,
1431 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1432 free(p.symbol);
1433 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1434}
1435
Jiri Olsa90e2b222012-06-15 14:31:40 +08001436static int parse_events__scanner(const char *str, void *data, int start_token)
Zheng Yanac20de6f2012-06-15 14:31:39 +08001437{
1438 YY_BUFFER_STATE buffer;
1439 void *scanner;
1440 int ret;
1441
Jiri Olsa90e2b222012-06-15 14:31:40 +08001442 ret = parse_events_lex_init_extra(start_token, &scanner);
Zheng Yanac20de6f2012-06-15 14:31:39 +08001443 if (ret)
1444 return ret;
1445
1446 buffer = parse_events__scan_string(str, scanner);
1447
1448#ifdef PARSER_DEBUG
1449 parse_events_debug = 1;
1450#endif
1451 ret = parse_events_parse(data, scanner);
1452
1453 parse_events__flush_buffer(buffer, scanner);
1454 parse_events__delete_buffer(buffer, scanner);
1455 parse_events_lex_destroy(scanner);
1456 return ret;
1457}
1458
Jiri Olsa90e2b222012-06-15 14:31:40 +08001459/*
1460 * parse event config string, return a list of event terms.
1461 */
1462int parse_events_terms(struct list_head *terms, const char *str)
1463{
Arnaldo Carvalho de Melo23b63392013-01-18 16:56:57 -03001464 struct parse_events_terms data = {
Jiri Olsa90e2b222012-06-15 14:31:40 +08001465 .terms = NULL,
1466 };
1467 int ret;
1468
1469 ret = parse_events__scanner(str, &data, PE_START_TERMS);
1470 if (!ret) {
1471 list_splice(data.terms, terms);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001472 zfree(&data.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001473 return 0;
1474 }
1475
Arnaldo Carvalho de Melo2146afc2016-02-12 17:09:17 -03001476 parse_events_terms__delete(data.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001477 return ret;
1478}
1479
Jiri Olsab39b8392015-04-22 21:10:16 +02001480int parse_events(struct perf_evlist *evlist, const char *str,
1481 struct parse_events_error *err)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001482{
Arnaldo Carvalho de Melo23b63392013-01-18 16:56:57 -03001483 struct parse_events_evlist data = {
Jiri Olsab39b8392015-04-22 21:10:16 +02001484 .list = LIST_HEAD_INIT(data.list),
1485 .idx = evlist->nr_entries,
1486 .error = err,
Jiri Olsa46010ab2012-06-15 14:31:38 +08001487 };
Jiri Olsa46010ab2012-06-15 14:31:38 +08001488 int ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001489
Jiri Olsa90e2b222012-06-15 14:31:40 +08001490 ret = parse_events__scanner(str, &data, PE_START_EVENTS);
Kan Liangdcb4e102014-10-07 11:08:50 -04001491 perf_pmu__parse_cleanup();
Jiri Olsa89812fc2012-03-15 20:09:15 +01001492 if (!ret) {
Wang Nan15bfd2c2015-07-10 07:36:09 +00001493 struct perf_evsel *last;
1494
Wang Nan854f7362015-09-06 07:13:17 +00001495 if (list_empty(&data.list)) {
1496 WARN_ONCE(true, "WARNING: event parser found nothing");
1497 return -1;
1498 }
1499
Adrian Hunterf114d6e2015-09-08 10:58:53 +03001500 perf_evlist__splice_list_tail(evlist, &data.list);
Namhyung Kim97f63e42013-01-22 18:09:29 +09001501 evlist->nr_groups += data.nr_groups;
Wang Nan15bfd2c2015-07-10 07:36:09 +00001502 last = perf_evlist__last(evlist);
1503 last->cmdline_group_boundary = true;
1504
Jiri Olsa89812fc2012-03-15 20:09:15 +01001505 return 0;
1506 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02001507
Jiri Olsa5d7be902012-03-20 19:15:40 +01001508 /*
1509 * There are 2 users - builtin-record and builtin-test objects.
1510 * Both call perf_evlist__delete in case of error, so we dont
1511 * need to bother.
1512 */
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02001513 return ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001514}
1515
Jiri Olsab39b8392015-04-22 21:10:16 +02001516#define MAX_WIDTH 1000
1517static int get_term_width(void)
1518{
1519 struct winsize ws;
1520
1521 get_term_dimensions(&ws);
1522 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1523}
1524
1525static void parse_events_print_error(struct parse_events_error *err,
1526 const char *event)
1527{
1528 const char *str = "invalid or unsupported event: ";
1529 char _buf[MAX_WIDTH];
1530 char *buf = (char *) event;
1531 int idx = 0;
1532
1533 if (err->str) {
1534 /* -2 for extra '' in the final fprintf */
1535 int width = get_term_width() - 2;
1536 int len_event = strlen(event);
1537 int len_str, max_len, cut = 0;
1538
1539 /*
1540 * Maximum error index indent, we will cut
1541 * the event string if it's bigger.
1542 */
Adrian Hunter141b2d32015-07-17 19:33:51 +03001543 int max_err_idx = 13;
Jiri Olsab39b8392015-04-22 21:10:16 +02001544
1545 /*
1546 * Let's be specific with the message when
1547 * we have the precise error.
1548 */
1549 str = "event syntax error: ";
1550 len_str = strlen(str);
1551 max_len = width - len_str;
1552
1553 buf = _buf;
1554
1555 /* We're cutting from the beggining. */
1556 if (err->idx > max_err_idx)
1557 cut = err->idx - max_err_idx;
1558
1559 strncpy(buf, event + cut, max_len);
1560
1561 /* Mark cut parts with '..' on both sides. */
1562 if (cut)
1563 buf[0] = buf[1] = '.';
1564
1565 if ((len_event - cut) > max_len) {
1566 buf[max_len - 1] = buf[max_len - 2] = '.';
1567 buf[max_len] = 0;
1568 }
1569
1570 idx = len_str + err->idx - cut;
1571 }
1572
1573 fprintf(stderr, "%s'%s'\n", str, buf);
1574 if (idx) {
1575 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
1576 if (err->help)
1577 fprintf(stderr, "\n%s\n", err->help);
1578 free(err->str);
1579 free(err->help);
1580 }
1581
1582 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
1583}
1584
1585#undef MAX_WIDTH
1586
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001587int parse_events_option(const struct option *opt, const char *str,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001588 int unset __maybe_unused)
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001589{
1590 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
Jiri Olsab39b8392015-04-22 21:10:16 +02001591 struct parse_events_error err = { .idx = 0, };
1592 int ret = parse_events(evlist, str, &err);
Andi Kleen9175ce12012-10-26 13:30:06 -07001593
Jiri Olsab39b8392015-04-22 21:10:16 +02001594 if (ret)
1595 parse_events_print_error(&err, str);
1596
Andi Kleen9175ce12012-10-26 13:30:06 -07001597 return ret;
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001598}
1599
Wang Nan4ba1faa2015-07-10 07:36:10 +00001600static int
1601foreach_evsel_in_last_glob(struct perf_evlist *evlist,
1602 int (*func)(struct perf_evsel *evsel,
1603 const void *arg),
1604 const void *arg)
Li Zefanc171b552009-10-15 11:22:07 +08001605{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001606 struct perf_evsel *last = NULL;
Wang Nan4ba1faa2015-07-10 07:36:10 +00001607 int err;
Li Zefanc171b552009-10-15 11:22:07 +08001608
Wang Nan854f7362015-09-06 07:13:17 +00001609 /*
1610 * Don't return when list_empty, give func a chance to report
1611 * error when it found last == NULL.
1612 *
1613 * So no need to WARN here, let *func do this.
1614 */
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001615 if (evlist->nr_entries > 0)
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001616 last = perf_evlist__last(evlist);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001617
Wang Nan15bfd2c2015-07-10 07:36:09 +00001618 do {
Wang Nan4ba1faa2015-07-10 07:36:10 +00001619 err = (*func)(last, arg);
1620 if (err)
Wang Nan15bfd2c2015-07-10 07:36:09 +00001621 return -1;
Wang Nan4ba1faa2015-07-10 07:36:10 +00001622 if (!last)
1623 return 0;
Wang Nan15bfd2c2015-07-10 07:36:09 +00001624
1625 if (last->node.prev == &evlist->entries)
1626 return 0;
1627 last = list_entry(last->node.prev, struct perf_evsel, node);
1628 } while (!last->cmdline_group_boundary);
Li Zefanc171b552009-10-15 11:22:07 +08001629
1630 return 0;
1631}
1632
Wang Nan4ba1faa2015-07-10 07:36:10 +00001633static int set_filter(struct perf_evsel *evsel, const void *arg)
1634{
1635 const char *str = arg;
1636
1637 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
1638 fprintf(stderr,
1639 "--filter option should follow a -e tracepoint option\n");
1640 return -1;
1641 }
1642
1643 if (perf_evsel__append_filter(evsel, "&&", str) < 0) {
1644 fprintf(stderr,
1645 "not enough memory to hold filter string\n");
1646 return -1;
1647 }
1648
1649 return 0;
1650}
1651
1652int parse_filter(const struct option *opt, const char *str,
1653 int unset __maybe_unused)
1654{
1655 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1656
1657 return foreach_evsel_in_last_glob(evlist, set_filter,
1658 (const void *)str);
1659}
1660
1661static int add_exclude_perf_filter(struct perf_evsel *evsel,
1662 const void *arg __maybe_unused)
1663{
1664 char new_filter[64];
1665
1666 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
1667 fprintf(stderr,
1668 "--exclude-perf option should follow a -e tracepoint option\n");
1669 return -1;
1670 }
1671
1672 snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
1673
1674 if (perf_evsel__append_filter(evsel, "&&", new_filter) < 0) {
1675 fprintf(stderr,
1676 "not enough memory to hold filter string\n");
1677 return -1;
1678 }
1679
1680 return 0;
1681}
1682
1683int exclude_perf(const struct option *opt,
1684 const char *arg __maybe_unused,
1685 int unset __maybe_unused)
1686{
1687 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1688
1689 return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
1690 NULL);
1691}
1692
Thomas Gleixner86847b62009-06-06 12:24:17 +02001693static const char * const event_type_descriptors[] = {
Thomas Gleixner86847b62009-06-06 12:24:17 +02001694 "Hardware event",
1695 "Software event",
1696 "Tracepoint event",
1697 "Hardware cache event",
Liming Wang41bdcb22009-12-29 16:37:07 +08001698 "Raw hardware event descriptor",
1699 "Hardware breakpoint",
Thomas Gleixner86847b62009-06-06 12:24:17 +02001700};
1701
Yunlong Songab0e4802015-02-27 18:21:25 +08001702static int cmp_string(const void *a, const void *b)
1703{
1704 const char * const *as = a;
1705 const char * const *bs = b;
1706
1707 return strcmp(*as, *bs);
1708}
1709
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001710/*
Jason Baronf6bdafe2009-07-21 12:20:22 -04001711 * Print the events from <debugfs_mount_point>/tracing/events
1712 */
1713
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001714void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
1715 bool name_only)
Jason Baronf6bdafe2009-07-21 12:20:22 -04001716{
1717 DIR *sys_dir, *evt_dir;
1718 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001719 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +02001720 char dir_path[MAXPATHLEN];
Yunlong Songab0e4802015-02-27 18:21:25 +08001721 char **evt_list = NULL;
1722 unsigned int evt_i = 0, evt_num = 0;
1723 bool evt_num_known = false;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001724
Yunlong Songab0e4802015-02-27 18:21:25 +08001725restart:
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001726 sys_dir = opendir(tracing_events_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -04001727 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +02001728 return;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001729
Yunlong Songab0e4802015-02-27 18:21:25 +08001730 if (evt_num_known) {
1731 evt_list = zalloc(sizeof(char *) * evt_num);
1732 if (!evt_list)
1733 goto out_close_sys_dir;
1734 }
1735
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03001736 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001737 if (subsys_glob != NULL &&
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001738 !strglobmatch(sys_dirent.d_name, subsys_glob))
1739 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02001740
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001741 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Eric Dumazet725b1362009-09-24 15:39:09 +02001742 sys_dirent.d_name);
1743 evt_dir = opendir(dir_path);
1744 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03001745 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02001746
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03001747 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001748 if (event_glob != NULL &&
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001749 !strglobmatch(evt_dirent.d_name, event_glob))
1750 continue;
1751
Yunlong Songab0e4802015-02-27 18:21:25 +08001752 if (!evt_num_known) {
1753 evt_num++;
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001754 continue;
1755 }
1756
Jason Baronf6bdafe2009-07-21 12:20:22 -04001757 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1758 sys_dirent.d_name, evt_dirent.d_name);
Yunlong Songab0e4802015-02-27 18:21:25 +08001759
1760 evt_list[evt_i] = strdup(evt_path);
1761 if (evt_list[evt_i] == NULL)
1762 goto out_close_evt_dir;
1763 evt_i++;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001764 }
1765 closedir(evt_dir);
1766 }
Jason Baronf6bdafe2009-07-21 12:20:22 -04001767 closedir(sys_dir);
Yunlong Songab0e4802015-02-27 18:21:25 +08001768
1769 if (!evt_num_known) {
1770 evt_num_known = true;
1771 goto restart;
1772 }
1773 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1774 evt_i = 0;
1775 while (evt_i < evt_num) {
1776 if (name_only) {
1777 printf("%s ", evt_list[evt_i++]);
1778 continue;
1779 }
1780 printf(" %-50s [%s]\n", evt_list[evt_i++],
1781 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1782 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03001783 if (evt_num && pager_in_use())
Yunlong Songab0e4802015-02-27 18:21:25 +08001784 printf("\n");
1785
1786out_free:
1787 evt_num = evt_i;
1788 for (evt_i = 0; evt_i < evt_num; evt_i++)
1789 zfree(&evt_list[evt_i]);
1790 zfree(&evt_list);
1791 return;
1792
1793out_close_evt_dir:
1794 closedir(evt_dir);
1795out_close_sys_dir:
1796 closedir(sys_dir);
1797
1798 printf("FATAL: not enough memory to print %s\n",
1799 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1800 if (evt_list)
1801 goto out_free;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001802}
1803
1804/*
Thomas Renninger20c457b2011-01-03 17:50:45 +01001805 * Check whether event is in <debugfs_mount_point>/tracing/events
1806 */
1807
1808int is_valid_tracepoint(const char *event_string)
1809{
1810 DIR *sys_dir, *evt_dir;
1811 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
1812 char evt_path[MAXPATHLEN];
1813 char dir_path[MAXPATHLEN];
1814
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001815 sys_dir = opendir(tracing_events_path);
Thomas Renninger20c457b2011-01-03 17:50:45 +01001816 if (!sys_dir)
1817 return 0;
1818
1819 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1820
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001821 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Thomas Renninger20c457b2011-01-03 17:50:45 +01001822 sys_dirent.d_name);
1823 evt_dir = opendir(dir_path);
1824 if (!evt_dir)
1825 continue;
1826
1827 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1828 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1829 sys_dirent.d_name, evt_dirent.d_name);
1830 if (!strcmp(evt_path, event_string)) {
1831 closedir(evt_dir);
1832 closedir(sys_dir);
1833 return 1;
1834 }
1835 }
1836 closedir(evt_dir);
1837 }
1838 closedir(sys_dir);
1839 return 0;
1840}
1841
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001842static bool is_event_supported(u8 type, unsigned config)
1843{
1844 bool ret = true;
Vince Weaver88fee522013-12-30 15:39:45 -05001845 int open_return;
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001846 struct perf_evsel *evsel;
1847 struct perf_event_attr attr = {
1848 .type = type,
1849 .config = config,
1850 .disabled = 1,
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001851 };
1852 struct {
1853 struct thread_map map;
1854 int threads[1];
1855 } tmap = {
1856 .map.nr = 1,
1857 .threads = { 0 },
1858 };
1859
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03001860 evsel = perf_evsel__new(&attr);
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001861 if (evsel) {
Vince Weaver88fee522013-12-30 15:39:45 -05001862 open_return = perf_evsel__open(evsel, NULL, &tmap.map);
1863 ret = open_return >= 0;
1864
1865 if (open_return == -EACCES) {
1866 /*
1867 * This happens if the paranoid value
1868 * /proc/sys/kernel/perf_event_paranoid is set to 2
1869 * Re-run with exclude_kernel set; we don't do that
1870 * by default as some ARM machines do not support it.
1871 *
1872 */
1873 evsel->attr.exclude_kernel = 1;
1874 ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0;
1875 }
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001876 perf_evsel__delete(evsel);
1877 }
1878
1879 return ret;
1880}
1881
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001882int print_hwcache_events(const char *event_glob, bool name_only)
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001883{
Yunlong Songab0e4802015-02-27 18:21:25 +08001884 unsigned int type, op, i, evt_i = 0, evt_num = 0;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03001885 char name[64];
Yunlong Songab0e4802015-02-27 18:21:25 +08001886 char **evt_list = NULL;
1887 bool evt_num_known = false;
1888
1889restart:
1890 if (evt_num_known) {
1891 evt_list = zalloc(sizeof(char *) * evt_num);
1892 if (!evt_list)
1893 goto out_enomem;
1894 }
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001895
1896 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1897 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1898 /* skip invalid cache type */
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03001899 if (!perf_evsel__is_cache_op_valid(type, op))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001900 continue;
1901
1902 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03001903 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
1904 name, sizeof(name));
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001905 if (event_glob != NULL && !strglobmatch(name, event_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001906 continue;
1907
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001908 if (!is_event_supported(PERF_TYPE_HW_CACHE,
1909 type | (op << 8) | (i << 16)))
1910 continue;
1911
Yunlong Songab0e4802015-02-27 18:21:25 +08001912 if (!evt_num_known) {
1913 evt_num++;
1914 continue;
1915 }
1916
1917 evt_list[evt_i] = strdup(name);
1918 if (evt_list[evt_i] == NULL)
1919 goto out_enomem;
1920 evt_i++;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001921 }
1922 }
1923 }
1924
Yunlong Songab0e4802015-02-27 18:21:25 +08001925 if (!evt_num_known) {
1926 evt_num_known = true;
1927 goto restart;
1928 }
1929 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1930 evt_i = 0;
1931 while (evt_i < evt_num) {
1932 if (name_only) {
1933 printf("%s ", evt_list[evt_i++]);
1934 continue;
1935 }
1936 printf(" %-50s [%s]\n", evt_list[evt_i++],
1937 event_type_descriptors[PERF_TYPE_HW_CACHE]);
1938 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03001939 if (evt_num && pager_in_use())
Andi Kleendc098b32013-04-20 11:02:29 -07001940 printf("\n");
Yunlong Songab0e4802015-02-27 18:21:25 +08001941
1942out_free:
1943 evt_num = evt_i;
1944 for (evt_i = 0; evt_i < evt_num; evt_i++)
1945 zfree(&evt_list[evt_i]);
1946 zfree(&evt_list);
1947 return evt_num;
1948
1949out_enomem:
1950 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
1951 if (evt_list)
1952 goto out_free;
1953 return evt_num;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001954}
1955
Yunlong Song705750f2015-02-27 18:21:27 +08001956void print_symbol_events(const char *event_glob, unsigned type,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001957 struct event_symbol *syms, unsigned max,
1958 bool name_only)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001959{
Yunlong Songab0e4802015-02-27 18:21:25 +08001960 unsigned int i, evt_i = 0, evt_num = 0;
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001961 char name[MAX_NAME_LEN];
Yunlong Songab0e4802015-02-27 18:21:25 +08001962 char **evt_list = NULL;
1963 bool evt_num_known = false;
1964
1965restart:
1966 if (evt_num_known) {
1967 evt_list = zalloc(sizeof(char *) * evt_num);
1968 if (!evt_list)
1969 goto out_enomem;
1970 syms -= max;
1971 }
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001972
Jiri Olsa1dc127602012-07-04 00:00:44 +02001973 for (i = 0; i < max; i++, syms++) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001974
Arnaldo Carvalho de Meloe37df6c2015-11-27 16:04:58 -03001975 if (event_glob != NULL && syms->symbol != NULL &&
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001976 !(strglobmatch(syms->symbol, event_glob) ||
1977 (syms->alias && strglobmatch(syms->alias, event_glob))))
1978 continue;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001979
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001980 if (!is_event_supported(type, i))
1981 continue;
1982
Yunlong Songab0e4802015-02-27 18:21:25 +08001983 if (!evt_num_known) {
1984 evt_num++;
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001985 continue;
1986 }
1987
Yunlong Songab0e4802015-02-27 18:21:25 +08001988 if (!name_only && strlen(syms->alias))
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001989 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301990 else
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001991 strncpy(name, syms->symbol, MAX_NAME_LEN);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001992
Yunlong Songab0e4802015-02-27 18:21:25 +08001993 evt_list[evt_i] = strdup(name);
1994 if (evt_list[evt_i] == NULL)
1995 goto out_enomem;
1996 evt_i++;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001997 }
1998
Yunlong Songab0e4802015-02-27 18:21:25 +08001999 if (!evt_num_known) {
2000 evt_num_known = true;
2001 goto restart;
2002 }
2003 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2004 evt_i = 0;
2005 while (evt_i < evt_num) {
2006 if (name_only) {
2007 printf("%s ", evt_list[evt_i++]);
2008 continue;
2009 }
2010 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
2011 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002012 if (evt_num && pager_in_use())
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002013 printf("\n");
Yunlong Songab0e4802015-02-27 18:21:25 +08002014
2015out_free:
2016 evt_num = evt_i;
2017 for (evt_i = 0; evt_i < evt_num; evt_i++)
2018 zfree(&evt_list[evt_i]);
2019 zfree(&evt_list);
2020 return;
2021
2022out_enomem:
2023 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
2024 if (evt_list)
2025 goto out_free;
Jiri Olsa1dc127602012-07-04 00:00:44 +02002026}
2027
2028/*
2029 * Print the help text for the event symbols:
2030 */
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002031void print_events(const char *event_glob, bool name_only)
Jiri Olsa1dc127602012-07-04 00:00:44 +02002032{
Jiri Olsa1dc127602012-07-04 00:00:44 +02002033 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002034 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
Jiri Olsa1dc127602012-07-04 00:00:44 +02002035
2036 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002037 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
Jiri Olsa1dc127602012-07-04 00:00:44 +02002038
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002039 print_hwcache_events(event_glob, name_only);
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002040
Andi Kleendc098b32013-04-20 11:02:29 -07002041 print_pmu_events(event_glob, name_only);
2042
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002043 if (event_glob != NULL)
2044 return;
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +05302045
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002046 if (!name_only) {
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002047 printf(" %-50s [%s]\n",
2048 "rNNN",
2049 event_type_descriptors[PERF_TYPE_RAW]);
2050 printf(" %-50s [%s]\n",
2051 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
2052 event_type_descriptors[PERF_TYPE_RAW]);
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002053 if (pager_in_use())
2054 printf(" (see 'man perf-list' on how to encode it)\n\n");
Thomas Gleixner86847b62009-06-06 12:24:17 +02002055
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002056 printf(" %-50s [%s]\n",
Jacob Shin3741eb92014-05-29 17:26:51 +02002057 "mem:<addr>[/len][:access]",
Liming Wang41bdcb22009-12-29 16:37:07 +08002058 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002059 if (pager_in_use())
2060 printf("\n");
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002061 }
Frederic Weisbecker1b290d62009-11-23 15:42:35 +01002062
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002063 print_tracepoint_events(NULL, NULL, name_only);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002064}
Jiri Olsa8f707d82012-03-15 20:09:16 +01002065
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002066int parse_events__is_hardcoded_term(struct parse_events_term *term)
Jiri Olsa8f707d82012-03-15 20:09:16 +01002067{
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002068 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002069}
2070
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002071static int new_term(struct parse_events_term **_term, int type_val,
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002072 int type_term, char *config,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002073 char *str, u64 num, int err_term, int err_val)
Jiri Olsa8f707d82012-03-15 20:09:16 +01002074{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002075 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002076
2077 term = zalloc(sizeof(*term));
2078 if (!term)
2079 return -ENOMEM;
2080
2081 INIT_LIST_HEAD(&term->list);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002082 term->type_val = type_val;
2083 term->type_term = type_term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002084 term->config = config;
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002085 term->err_term = err_term;
2086 term->err_val = err_val;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002087
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002088 switch (type_val) {
Jiri Olsa8f707d82012-03-15 20:09:16 +01002089 case PARSE_EVENTS__TERM_TYPE_NUM:
2090 term->val.num = num;
2091 break;
2092 case PARSE_EVENTS__TERM_TYPE_STR:
2093 term->val.str = str;
2094 break;
2095 default:
Adrian Hunter4be8be62013-07-04 16:20:24 +03002096 free(term);
Jiri Olsa8f707d82012-03-15 20:09:16 +01002097 return -EINVAL;
2098 }
2099
2100 *_term = term;
2101 return 0;
2102}
2103
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002104int parse_events_term__num(struct parse_events_term **term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002105 int type_term, char *config, u64 num,
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002106 void *loc_term_, void *loc_val_)
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002107{
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002108 YYLTYPE *loc_term = loc_term_;
2109 YYLTYPE *loc_val = loc_val_;
2110
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002111 return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002112 config, NULL, num,
2113 loc_term ? loc_term->first_column : 0,
2114 loc_val ? loc_val->first_column : 0);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002115}
2116
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002117int parse_events_term__str(struct parse_events_term **term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002118 int type_term, char *config, char *str,
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002119 void *loc_term_, void *loc_val_)
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002120{
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002121 YYLTYPE *loc_term = loc_term_;
2122 YYLTYPE *loc_val = loc_val_;
2123
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002124 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002125 config, str, 0,
2126 loc_term ? loc_term->first_column : 0,
2127 loc_val ? loc_val->first_column : 0);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002128}
2129
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002130int parse_events_term__sym_hw(struct parse_events_term **term,
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02002131 char *config, unsigned idx)
2132{
2133 struct event_symbol *sym;
2134
2135 BUG_ON(idx >= PERF_COUNT_HW_MAX);
2136 sym = &event_symbols_hw[idx];
2137
2138 if (config)
2139 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
2140 PARSE_EVENTS__TERM_TYPE_USER, config,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002141 (char *) sym->symbol, 0, 0, 0);
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02002142 else
2143 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
2144 PARSE_EVENTS__TERM_TYPE_USER,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002145 (char *) "event", (char *) sym->symbol,
2146 0, 0, 0);
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02002147}
2148
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002149int parse_events_term__clone(struct parse_events_term **new,
2150 struct parse_events_term *term)
Zheng Yana6146d52012-06-15 14:31:41 +08002151{
2152 return new_term(new, term->type_val, term->type_term, term->config,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002153 term->val.str, term->val.num,
2154 term->err_term, term->err_val);
Zheng Yana6146d52012-06-15 14:31:41 +08002155}
2156
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002157void parse_events_terms__purge(struct list_head *terms)
Jiri Olsa8f707d82012-03-15 20:09:16 +01002158{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002159 struct parse_events_term *term, *h;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002160
Wang Nana8adfce2016-02-12 16:31:23 -03002161 list_for_each_entry_safe(term, h, terms, list) {
2162 list_del_init(&term->list);
Jiri Olsa8f707d82012-03-15 20:09:16 +01002163 free(term);
Wang Nana8adfce2016-02-12 16:31:23 -03002164 }
Jiri Olsa8f707d82012-03-15 20:09:16 +01002165}
Jiri Olsab39b8392015-04-22 21:10:16 +02002166
Arnaldo Carvalho de Melo2146afc2016-02-12 17:09:17 -03002167void parse_events_terms__delete(struct list_head *terms)
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002168{
Arnaldo Carvalho de Melo2146afc2016-02-12 17:09:17 -03002169 if (!terms)
2170 return;
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002171 parse_events_terms__purge(terms);
Wang Nand20a5f22016-02-12 17:01:17 -03002172 free(terms);
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002173}
2174
Jiri Olsab39b8392015-04-22 21:10:16 +02002175void parse_events_evlist_error(struct parse_events_evlist *data,
2176 int idx, const char *str)
2177{
2178 struct parse_events_error *err = data->error;
2179
Adrian Huntera6ced2b2015-05-19 16:05:44 +03002180 if (!err)
2181 return;
Jiri Olsab39b8392015-04-22 21:10:16 +02002182 err->idx = idx;
2183 err->str = strdup(str);
2184 WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
2185}
He Kuangffeb8832015-09-28 03:52:14 +00002186
Wang Nan17cb5f82016-02-19 11:43:57 +00002187static void config_terms_list(char *buf, size_t buf_sz)
2188{
2189 int i;
2190 bool first = true;
2191
2192 buf[0] = '\0';
2193 for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
2194 const char *name = config_term_names[i];
2195
Wang Nan1669e502016-02-19 11:43:58 +00002196 if (!config_term_avail(i, NULL))
2197 continue;
Wang Nan17cb5f82016-02-19 11:43:57 +00002198 if (!name)
2199 continue;
2200 if (name[0] == '<')
2201 continue;
2202
2203 if (strlen(buf) + strlen(name) + 2 >= buf_sz)
2204 return;
2205
2206 if (!first)
2207 strcat(buf, ",");
2208 else
2209 first = false;
2210 strcat(buf, name);
2211 }
2212}
2213
He Kuangffeb8832015-09-28 03:52:14 +00002214/*
2215 * Return string contains valid config terms of an event.
2216 * @additional_terms: For terms such as PMU sysfs terms.
2217 */
2218char *parse_events_formats_error_string(char *additional_terms)
2219{
2220 char *str;
Wang Nan17cb5f82016-02-19 11:43:57 +00002221 /* "branch_type" is the longest name */
2222 char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
2223 (sizeof("branch_type") - 1)];
He Kuangffeb8832015-09-28 03:52:14 +00002224
Wang Nan17cb5f82016-02-19 11:43:57 +00002225 config_terms_list(static_terms, sizeof(static_terms));
He Kuangffeb8832015-09-28 03:52:14 +00002226 /* valid terms */
2227 if (additional_terms) {
Wang Nan26dee022016-02-19 11:43:52 +00002228 if (asprintf(&str, "valid terms: %s,%s",
2229 additional_terms, static_terms) < 0)
He Kuangffeb8832015-09-28 03:52:14 +00002230 goto fail;
2231 } else {
Wang Nan26dee022016-02-19 11:43:52 +00002232 if (asprintf(&str, "valid terms: %s", static_terms) < 0)
He Kuangffeb8832015-09-28 03:52:14 +00002233 goto fail;
2234 }
2235 return str;
2236
2237fail:
2238 return NULL;
2239}