blob: 2996aa4207bde2f7b0ecbf66ba94032d3b3a3b88 [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 Olsa1dc12762012-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 Olsa1dc12762012-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,
366 struct parse_events_error *error,
367 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) {
428 if (config_attr(&attr, head_config, error,
429 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 Nan6371ca32015-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 Nan6371ca32015-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 +0000844typedef int config_term_func_t(struct perf_event_attr *attr,
845 struct parse_events_term *term,
846 struct parse_events_error *err);
847
848static int config_term_common(struct perf_event_attr *attr,
849 struct parse_events_term *term,
850 struct parse_events_error *err)
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200851{
852#define CHECK_TYPE_VAL(type) \
853do { \
854 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
855 return -EINVAL; \
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200856} while (0)
857
858 switch (term->type_term) {
Jiri Olsa8f707d82012-03-15 20:09:16 +0100859 case PARSE_EVENTS__TERM_TYPE_CONFIG:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200860 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100861 attr->config = term->val.num;
862 break;
863 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200864 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100865 attr->config1 = term->val.num;
866 break;
867 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200868 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100869 attr->config2 = term->val.num;
870 break;
871 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200872 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100873 break;
Namhyung Kim09af2a52015-08-09 15:45:23 +0900874 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
875 CHECK_TYPE_VAL(NUM);
876 break;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100877 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
878 /*
879 * TODO uncomment when the field is available
880 * attr->branch_sample_type = term->val.num;
881 */
882 break;
Kan Liang32067712015-08-04 04:30:19 -0400883 case PARSE_EVENTS__TERM_TYPE_TIME:
884 CHECK_TYPE_VAL(NUM);
885 if (term->val.num > 1) {
886 err->str = strdup("expected 0 or 1");
887 err->idx = term->err_val;
888 return -EINVAL;
889 }
890 break;
Kan Liangd457c962015-08-11 06:30:47 -0400891 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
892 CHECK_TYPE_VAL(STR);
893 break;
894 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
895 CHECK_TYPE_VAL(NUM);
896 break;
Wang Nan374ce932015-10-28 10:55:02 +0000897 case PARSE_EVENTS__TERM_TYPE_INHERIT:
898 CHECK_TYPE_VAL(NUM);
899 break;
900 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
901 CHECK_TYPE_VAL(NUM);
902 break;
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200903 case PARSE_EVENTS__TERM_TYPE_NAME:
904 CHECK_TYPE_VAL(STR);
905 break;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100906 default:
He Kuangffeb8832015-09-28 03:52:14 +0000907 err->str = strdup("unknown term");
908 err->idx = term->err_term;
909 err->help = parse_events_formats_error_string(NULL);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100910 return -EINVAL;
911 }
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200912
Wang Nan1669e502016-02-19 11:43:58 +0000913 /*
914 * Check term availbility after basic checking so
915 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
916 *
917 * If check availbility at the entry of this function,
918 * user will see "'<sysfs term>' is not usable in 'perf stat'"
919 * if an invalid config term is provided for legacy events
920 * (for example, instructions/badterm/...), which is confusing.
921 */
922 if (!config_term_avail(term->type_term, err))
923 return -EINVAL;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100924 return 0;
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200925#undef CHECK_TYPE_VAL
Jiri Olsa8f707d82012-03-15 20:09:16 +0100926}
927
He Kuang0b8891a2015-09-28 03:52:13 +0000928static int config_term_pmu(struct perf_event_attr *attr,
929 struct parse_events_term *term,
930 struct parse_events_error *err)
931{
932 if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER)
933 /*
934 * Always succeed for sysfs terms, as we dont know
935 * at this point what type they need to have.
936 */
937 return 0;
938 else
939 return config_term_common(attr, term, err);
940}
941
He Kuange637d172015-09-28 03:52:16 +0000942static int config_term_tracepoint(struct perf_event_attr *attr,
943 struct parse_events_term *term,
944 struct parse_events_error *err)
945{
946 switch (term->type_term) {
947 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
948 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
Wang Nan374ce932015-10-28 10:55:02 +0000949 case PARSE_EVENTS__TERM_TYPE_INHERIT:
950 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
He Kuange637d172015-09-28 03:52:16 +0000951 return config_term_common(attr, term, err);
952 default:
953 if (err) {
954 err->idx = term->err_term;
955 err->str = strdup("unknown term");
956 err->help = strdup("valid terms: call-graph,stack-size\n");
957 }
958 return -EINVAL;
959 }
960
961 return 0;
962}
963
Jiri Olsa8f707d82012-03-15 20:09:16 +0100964static int config_attr(struct perf_event_attr *attr,
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200965 struct list_head *head,
He Kuang0b8891a2015-09-28 03:52:13 +0000966 struct parse_events_error *err,
967 config_term_func_t config_term)
Jiri Olsa8f707d82012-03-15 20:09:16 +0100968{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300969 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100970
971 list_for_each_entry(term, head, list)
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200972 if (config_term(attr, term, err))
Jiri Olsa8f707d82012-03-15 20:09:16 +0100973 return -EINVAL;
974
975 return 0;
976}
977
Jiri Olsa930a2e22015-07-29 05:42:10 -0400978static int get_config_terms(struct list_head *head_config,
979 struct list_head *head_terms __maybe_unused)
980{
981#define ADD_CONFIG_TERM(__type, __name, __val) \
982do { \
983 struct perf_evsel_config_term *__t; \
984 \
985 __t = zalloc(sizeof(*__t)); \
986 if (!__t) \
987 return -ENOMEM; \
988 \
989 INIT_LIST_HEAD(&__t->list); \
990 __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \
991 __t->val.__name = __val; \
992 list_add_tail(&__t->list, head_terms); \
993} while (0)
994
995 struct parse_events_term *term;
996
997 list_for_each_entry(term, head_config, list) {
998 switch (term->type_term) {
Jiri Olsaee4c7582015-07-29 05:42:11 -0400999 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1000 ADD_CONFIG_TERM(PERIOD, period, term->val.num);
Kan Liang32067712015-08-04 04:30:19 -04001001 break;
Namhyung Kim09af2a52015-08-09 15:45:23 +09001002 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1003 ADD_CONFIG_TERM(FREQ, freq, term->val.num);
1004 break;
Kan Liang32067712015-08-04 04:30:19 -04001005 case PARSE_EVENTS__TERM_TYPE_TIME:
1006 ADD_CONFIG_TERM(TIME, time, term->val.num);
1007 break;
Kan Liangd457c962015-08-11 06:30:47 -04001008 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1009 ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
1010 break;
1011 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1012 ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
1013 break;
Wang Nan374ce932015-10-28 10:55:02 +00001014 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1015 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0);
1016 break;
1017 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1018 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1);
1019 break;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001020 default:
1021 break;
1022 }
1023 }
1024#undef ADD_EVSEL_CONFIG
1025 return 0;
1026}
1027
He Kuange637d172015-09-28 03:52:16 +00001028int parse_events_add_tracepoint(struct list_head *list, int *idx,
1029 char *sys, char *event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001030 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +00001031 struct list_head *head_config)
1032{
1033 if (head_config) {
1034 struct perf_event_attr attr;
1035
Jiri Olsa272ed292015-09-29 17:05:31 +02001036 if (config_attr(&attr, head_config, err,
He Kuange637d172015-09-28 03:52:16 +00001037 config_term_tracepoint))
1038 return -EINVAL;
1039 }
1040
1041 if (strpbrk(sys, "*?"))
1042 return add_tracepoint_multi_sys(list, idx, sys, event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001043 err, head_config);
He Kuange637d172015-09-28 03:52:16 +00001044 else
1045 return add_tracepoint_event(list, idx, sys, event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001046 err, head_config);
He Kuange637d172015-09-28 03:52:16 +00001047}
1048
Jiri Olsa87d650b2015-04-22 21:10:24 +02001049int parse_events_add_numeric(struct parse_events_evlist *data,
1050 struct list_head *list,
Robert Richterb527bab2012-08-07 19:43:13 +02001051 u32 type, u64 config,
Jiri Olsa8f707d82012-03-15 20:09:16 +01001052 struct list_head *head_config)
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301053{
Jiri Olsa89812fc2012-03-15 20:09:15 +01001054 struct perf_event_attr attr;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001055 LIST_HEAD(config_terms);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301056
Jiri Olsa89812fc2012-03-15 20:09:15 +01001057 memset(&attr, 0, sizeof(attr));
1058 attr.type = type;
1059 attr.config = config;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001060
Jiri Olsa930a2e22015-07-29 05:42:10 -04001061 if (head_config) {
He Kuang0b8891a2015-09-28 03:52:13 +00001062 if (config_attr(&attr, head_config, data->error,
1063 config_term_common))
Jiri Olsa930a2e22015-07-29 05:42:10 -04001064 return -EINVAL;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001065
Jiri Olsa930a2e22015-07-29 05:42:10 -04001066 if (get_config_terms(head_config, &config_terms))
1067 return -ENOMEM;
1068 }
1069
Wang Nan10bf3582016-02-19 11:44:00 +00001070 return add_event(list, &data->idx, &attr,
1071 get_config_name(head_config), &config_terms);
Paul Mackerras61c45982009-07-01 13:04:34 +10001072}
1073
Jiri Olsa36adec82015-04-22 21:10:19 +02001074int parse_events_add_pmu(struct parse_events_evlist *data,
1075 struct list_head *list, char *name,
1076 struct list_head *head_config)
Jiri Olsa5f537a22012-03-15 20:09:18 +01001077{
1078 struct perf_event_attr attr;
Matt Fleming46441bd2014-09-24 15:04:06 +01001079 struct perf_pmu_info info;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001080 struct perf_pmu *pmu;
Stephane Eranian410136f2013-11-12 17:58:49 +01001081 struct perf_evsel *evsel;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001082 LIST_HEAD(config_terms);
Jiri Olsa5f537a22012-03-15 20:09:18 +01001083
1084 pmu = perf_pmu__find(name);
1085 if (!pmu)
1086 return -EINVAL;
1087
Adrian Hunterdc0a6202014-07-31 09:00:49 +03001088 if (pmu->default_config) {
1089 memcpy(&attr, pmu->default_config,
1090 sizeof(struct perf_event_attr));
1091 } else {
1092 memset(&attr, 0, sizeof(attr));
1093 }
Jiri Olsa5f537a22012-03-15 20:09:18 +01001094
Adrian Hunterad962272014-08-15 22:08:40 +03001095 if (!head_config) {
1096 attr.type = pmu->type;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001097 evsel = __add_event(list, &data->idx, &attr, NULL, pmu->cpus, NULL);
Adrian Hunterad962272014-08-15 22:08:40 +03001098 return evsel ? 0 : -ENOMEM;
1099 }
1100
Matt Fleming46441bd2014-09-24 15:04:06 +01001101 if (perf_pmu__check_alias(pmu, head_config, &info))
Zheng Yana6146d52012-06-15 14:31:41 +08001102 return -EINVAL;
1103
Jiri Olsa5f537a22012-03-15 20:09:18 +01001104 /*
1105 * Configure hardcoded terms first, no need to check
1106 * return value when called with fail == 0 ;)
1107 */
He Kuang0b8891a2015-09-28 03:52:13 +00001108 if (config_attr(&attr, head_config, data->error, config_term_pmu))
Jiri Olsac056ba6a2015-04-22 21:10:18 +02001109 return -EINVAL;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001110
Jiri Olsa930a2e22015-07-29 05:42:10 -04001111 if (get_config_terms(head_config, &config_terms))
1112 return -ENOMEM;
1113
Jiri Olsae64b0202015-04-22 21:10:21 +02001114 if (perf_pmu__config(pmu, &attr, head_config, data->error))
Jiri Olsa5f537a22012-03-15 20:09:18 +01001115 return -EINVAL;
1116
Jiri Olsa36adec82015-04-22 21:10:19 +02001117 evsel = __add_event(list, &data->idx, &attr,
Wang Nane814fdd2016-02-19 11:43:59 +00001118 get_config_name(head_config), pmu->cpus,
Jiri Olsa930a2e22015-07-29 05:42:10 -04001119 &config_terms);
Stephane Eranian410136f2013-11-12 17:58:49 +01001120 if (evsel) {
Matt Fleming46441bd2014-09-24 15:04:06 +01001121 evsel->unit = info.unit;
1122 evsel->scale = info.scale;
Matt Fleming044330c2014-11-21 10:31:12 +01001123 evsel->per_pkg = info.per_pkg;
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001124 evsel->snapshot = info.snapshot;
Stephane Eranian410136f2013-11-12 17:58:49 +01001125 }
1126
1127 return evsel ? 0 : -ENOMEM;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001128}
1129
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001130int parse_events__modifier_group(struct list_head *list,
1131 char *event_mod)
Jiri Olsa89efb022012-08-08 12:14:14 +02001132{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001133 return parse_events__modifier_event(list, event_mod, true);
1134}
1135
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -03001136void parse_events__set_leader(char *name, struct list_head *list)
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001137{
1138 struct perf_evsel *leader;
1139
Wang Nan854f7362015-09-06 07:13:17 +00001140 if (list_empty(list)) {
1141 WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1142 return;
1143 }
1144
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -03001145 __perf_evlist__set_leader(list);
1146 leader = list_entry(list->next, struct perf_evsel, node);
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001147 leader->group_name = name ? strdup(name) : NULL;
Jiri Olsa89efb022012-08-08 12:14:14 +02001148}
1149
David Ahernc5cd8ac2013-07-02 13:27:25 -06001150/* list_event is assumed to point to malloc'ed memory */
Jiri Olsa5d7be902012-03-20 19:15:40 +01001151void parse_events_update_lists(struct list_head *list_event,
1152 struct list_head *list_all)
1153{
1154 /*
1155 * Called for single event definition. Update the
Jiri Olsa89efb022012-08-08 12:14:14 +02001156 * 'all event' list, and reinit the 'single event'
Jiri Olsa5d7be902012-03-20 19:15:40 +01001157 * list, for next event definition.
1158 */
1159 list_splice_tail(list_event, list_all);
Jiri Olsab847cbd2012-05-21 09:12:51 +02001160 free(list_event);
Jiri Olsa5d7be902012-03-20 19:15:40 +01001161}
1162
Jiri Olsaf5b11352012-08-08 12:21:54 +02001163struct event_modifier {
1164 int eu;
1165 int ek;
1166 int eh;
1167 int eH;
1168 int eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001169 int eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001170 int precise;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001171 int precise_max;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001172 int exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02001173 int sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001174 int pinned;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001175};
Paul Mackerras61c45982009-07-01 13:04:34 +10001176
Jiri Olsaf5b11352012-08-08 12:21:54 +02001177static int get_event_modifier(struct event_modifier *mod, char *str,
1178 struct perf_evsel *evsel)
1179{
1180 int eu = evsel ? evsel->attr.exclude_user : 0;
1181 int ek = evsel ? evsel->attr.exclude_kernel : 0;
1182 int eh = evsel ? evsel->attr.exclude_hv : 0;
1183 int eH = evsel ? evsel->attr.exclude_host : 0;
1184 int eG = evsel ? evsel->attr.exclude_guest : 0;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001185 int eI = evsel ? evsel->attr.exclude_idle : 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001186 int precise = evsel ? evsel->attr.precise_ip : 0;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001187 int precise_max = 0;
Jiri Olsa3c176312012-10-10 17:39:03 +02001188 int sample_read = 0;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001189 int pinned = evsel ? evsel->attr.pinned : 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001190
1191 int exclude = eu | ek | eh;
1192 int exclude_GH = evsel ? evsel->exclude_GH : 0;
1193
Jiri Olsaf5b11352012-08-08 12:21:54 +02001194 memset(mod, 0, sizeof(*mod));
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001195
Paul Mackerras61c45982009-07-01 13:04:34 +10001196 while (*str) {
Peter Zijlstraab608342010-04-08 23:03:20 +02001197 if (*str == 'u') {
1198 if (!exclude)
1199 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001200 eu = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +02001201 } else if (*str == 'k') {
1202 if (!exclude)
1203 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001204 ek = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +02001205 } else if (*str == 'h') {
1206 if (!exclude)
1207 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001208 eh = 0;
Joerg Roedel99320cc2012-01-04 17:54:19 +01001209 } else if (*str == 'G') {
1210 if (!exclude_GH)
1211 exclude_GH = eG = eH = 1;
1212 eG = 0;
1213 } else if (*str == 'H') {
1214 if (!exclude_GH)
1215 exclude_GH = eG = eH = 1;
1216 eH = 0;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001217 } else if (*str == 'I') {
1218 eI = 1;
Peter Zijlstraab608342010-04-08 23:03:20 +02001219 } else if (*str == 'p') {
1220 precise++;
David Ahern13427982012-09-13 14:59:13 -06001221 /* use of precise requires exclude_guest */
1222 if (!exclude_GH)
1223 eG = 1;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001224 } else if (*str == 'P') {
1225 precise_max = 1;
Jiri Olsa3c176312012-10-10 17:39:03 +02001226 } else if (*str == 'S') {
1227 sample_read = 1;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001228 } else if (*str == 'D') {
1229 pinned = 1;
Peter Zijlstraab608342010-04-08 23:03:20 +02001230 } else
Paul Mackerras61c45982009-07-01 13:04:34 +10001231 break;
Peter Zijlstraab608342010-04-08 23:03:20 +02001232
Paul Mackerras61c45982009-07-01 13:04:34 +10001233 ++str;
1234 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001235
Jiri Olsa89812fc2012-03-15 20:09:15 +01001236 /*
1237 * precise ip:
1238 *
1239 * 0 - SAMPLE_IP can have arbitrary skid
1240 * 1 - SAMPLE_IP must have constant skid
1241 * 2 - SAMPLE_IP requested to have 0 skid
1242 * 3 - SAMPLE_IP must have 0 skid
1243 *
1244 * See also PERF_RECORD_MISC_EXACT_IP
1245 */
1246 if (precise > 3)
1247 return -EINVAL;
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001248
Jiri Olsaf5b11352012-08-08 12:21:54 +02001249 mod->eu = eu;
1250 mod->ek = ek;
1251 mod->eh = eh;
1252 mod->eH = eH;
1253 mod->eG = eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001254 mod->eI = eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001255 mod->precise = precise;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001256 mod->precise_max = precise_max;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001257 mod->exclude_GH = exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02001258 mod->sample_read = sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001259 mod->pinned = pinned;
1260
Jiri Olsaf5b11352012-08-08 12:21:54 +02001261 return 0;
1262}
1263
Jiri Olsa534123f2012-11-13 15:32:58 +01001264/*
1265 * Basic modifier sanity check to validate it contains only one
1266 * instance of any modifier (apart from 'p') present.
1267 */
1268static int check_modifier(char *str)
1269{
1270 char *p = str;
1271
1272 /* The sizeof includes 0 byte as well. */
Jiri Olsa7f94af72015-10-05 20:06:05 +02001273 if (strlen(str) > (sizeof("ukhGHpppPSDI") - 1))
Jiri Olsa534123f2012-11-13 15:32:58 +01001274 return -1;
1275
1276 while (*p) {
1277 if (*p != 'p' && strchr(p + 1, *p))
1278 return -1;
1279 p++;
1280 }
1281
1282 return 0;
1283}
1284
Jiri Olsaf5b11352012-08-08 12:21:54 +02001285int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1286{
1287 struct perf_evsel *evsel;
1288 struct event_modifier mod;
1289
1290 if (str == NULL)
1291 return 0;
1292
Jiri Olsa534123f2012-11-13 15:32:58 +01001293 if (check_modifier(str))
1294 return -EINVAL;
1295
Jiri Olsaf5b11352012-08-08 12:21:54 +02001296 if (!add && get_event_modifier(&mod, str, NULL))
1297 return -EINVAL;
1298
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001299 __evlist__for_each(list, evsel) {
Jiri Olsaf5b11352012-08-08 12:21:54 +02001300 if (add && get_event_modifier(&mod, str, evsel))
1301 return -EINVAL;
1302
1303 evsel->attr.exclude_user = mod.eu;
1304 evsel->attr.exclude_kernel = mod.ek;
1305 evsel->attr.exclude_hv = mod.eh;
1306 evsel->attr.precise_ip = mod.precise;
1307 evsel->attr.exclude_host = mod.eH;
1308 evsel->attr.exclude_guest = mod.eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001309 evsel->attr.exclude_idle = mod.eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001310 evsel->exclude_GH = mod.exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02001311 evsel->sample_read = mod.sample_read;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001312 evsel->precise_max = mod.precise_max;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001313
1314 if (perf_evsel__is_group_leader(evsel))
1315 evsel->attr.pinned = mod.pinned;
Jiri Olsa89812fc2012-03-15 20:09:15 +01001316 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001317
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301318 return 0;
1319}
1320
Robert Richterac2ba9f2012-08-16 21:10:21 +02001321int parse_events_name(struct list_head *list, char *name)
1322{
1323 struct perf_evsel *evsel;
1324
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001325 __evlist__for_each(list, evsel) {
Robert Richterac2ba9f2012-08-16 21:10:21 +02001326 if (!evsel->name)
1327 evsel->name = strdup(name);
1328 }
1329
1330 return 0;
1331}
1332
Kan Liangdcb4e102014-10-07 11:08:50 -04001333static int
1334comp_pmu(const void *p1, const void *p2)
1335{
1336 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
1337 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
1338
1339 return strcmp(pmu1->symbol, pmu2->symbol);
1340}
1341
1342static void perf_pmu__parse_cleanup(void)
1343{
1344 if (perf_pmu_events_list_num > 0) {
1345 struct perf_pmu_event_symbol *p;
1346 int i;
1347
1348 for (i = 0; i < perf_pmu_events_list_num; i++) {
1349 p = perf_pmu_events_list + i;
1350 free(p->symbol);
1351 }
1352 free(perf_pmu_events_list);
1353 perf_pmu_events_list = NULL;
1354 perf_pmu_events_list_num = 0;
1355 }
1356}
1357
1358#define SET_SYMBOL(str, stype) \
1359do { \
1360 p->symbol = str; \
1361 if (!p->symbol) \
1362 goto err; \
1363 p->type = stype; \
1364} while (0)
1365
1366/*
1367 * Read the pmu events list from sysfs
1368 * Save it into perf_pmu_events_list
1369 */
1370static void perf_pmu__parse_init(void)
1371{
1372
1373 struct perf_pmu *pmu = NULL;
1374 struct perf_pmu_alias *alias;
1375 int len = 0;
1376
1377 pmu = perf_pmu__find("cpu");
1378 if ((pmu == NULL) || list_empty(&pmu->aliases)) {
1379 perf_pmu_events_list_num = -1;
1380 return;
1381 }
1382 list_for_each_entry(alias, &pmu->aliases, list) {
1383 if (strchr(alias->name, '-'))
1384 len++;
1385 len++;
1386 }
1387 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1388 if (!perf_pmu_events_list)
1389 return;
1390 perf_pmu_events_list_num = len;
1391
1392 len = 0;
1393 list_for_each_entry(alias, &pmu->aliases, list) {
1394 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1395 char *tmp = strchr(alias->name, '-');
1396
1397 if (tmp != NULL) {
1398 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1399 PMU_EVENT_SYMBOL_PREFIX);
1400 p++;
1401 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1402 len += 2;
1403 } else {
1404 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1405 len++;
1406 }
1407 }
1408 qsort(perf_pmu_events_list, len,
1409 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1410
1411 return;
1412err:
1413 perf_pmu__parse_cleanup();
1414}
1415
1416enum perf_pmu_event_symbol_type
1417perf_pmu__parse_check(const char *name)
1418{
1419 struct perf_pmu_event_symbol p, *r;
1420
1421 /* scan kernel pmu events from sysfs if needed */
1422 if (perf_pmu_events_list_num == 0)
1423 perf_pmu__parse_init();
1424 /*
1425 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1426 * cpu-cycles has been handled by hardcode.
1427 * So it must be cpu// events, not kernel pmu event.
1428 */
1429 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1430 return PMU_EVENT_SYMBOL_ERR;
1431
1432 p.symbol = strdup(name);
1433 r = bsearch(&p, perf_pmu_events_list,
1434 (size_t) perf_pmu_events_list_num,
1435 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1436 free(p.symbol);
1437 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1438}
1439
Jiri Olsa90e2b222012-06-15 14:31:40 +08001440static int parse_events__scanner(const char *str, void *data, int start_token)
Zheng Yanac20de6f2012-06-15 14:31:39 +08001441{
1442 YY_BUFFER_STATE buffer;
1443 void *scanner;
1444 int ret;
1445
Jiri Olsa90e2b222012-06-15 14:31:40 +08001446 ret = parse_events_lex_init_extra(start_token, &scanner);
Zheng Yanac20de6f2012-06-15 14:31:39 +08001447 if (ret)
1448 return ret;
1449
1450 buffer = parse_events__scan_string(str, scanner);
1451
1452#ifdef PARSER_DEBUG
1453 parse_events_debug = 1;
1454#endif
1455 ret = parse_events_parse(data, scanner);
1456
1457 parse_events__flush_buffer(buffer, scanner);
1458 parse_events__delete_buffer(buffer, scanner);
1459 parse_events_lex_destroy(scanner);
1460 return ret;
1461}
1462
Jiri Olsa90e2b222012-06-15 14:31:40 +08001463/*
1464 * parse event config string, return a list of event terms.
1465 */
1466int parse_events_terms(struct list_head *terms, const char *str)
1467{
Arnaldo Carvalho de Melo23b63392013-01-18 16:56:57 -03001468 struct parse_events_terms data = {
Jiri Olsa90e2b222012-06-15 14:31:40 +08001469 .terms = NULL,
1470 };
1471 int ret;
1472
1473 ret = parse_events__scanner(str, &data, PE_START_TERMS);
1474 if (!ret) {
1475 list_splice(data.terms, terms);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001476 zfree(&data.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001477 return 0;
1478 }
1479
Arnaldo Carvalho de Melo2146afc2016-02-12 17:09:17 -03001480 parse_events_terms__delete(data.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001481 return ret;
1482}
1483
Jiri Olsab39b8392015-04-22 21:10:16 +02001484int parse_events(struct perf_evlist *evlist, const char *str,
1485 struct parse_events_error *err)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001486{
Arnaldo Carvalho de Melo23b63392013-01-18 16:56:57 -03001487 struct parse_events_evlist data = {
Jiri Olsab39b8392015-04-22 21:10:16 +02001488 .list = LIST_HEAD_INIT(data.list),
1489 .idx = evlist->nr_entries,
1490 .error = err,
Jiri Olsa46010ab2012-06-15 14:31:38 +08001491 };
Jiri Olsa46010ab2012-06-15 14:31:38 +08001492 int ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001493
Jiri Olsa90e2b222012-06-15 14:31:40 +08001494 ret = parse_events__scanner(str, &data, PE_START_EVENTS);
Kan Liangdcb4e102014-10-07 11:08:50 -04001495 perf_pmu__parse_cleanup();
Jiri Olsa89812fc2012-03-15 20:09:15 +01001496 if (!ret) {
Wang Nan15bfd2c2015-07-10 07:36:09 +00001497 struct perf_evsel *last;
1498
Wang Nan854f7362015-09-06 07:13:17 +00001499 if (list_empty(&data.list)) {
1500 WARN_ONCE(true, "WARNING: event parser found nothing");
1501 return -1;
1502 }
1503
Adrian Hunterf114d6e2015-09-08 10:58:53 +03001504 perf_evlist__splice_list_tail(evlist, &data.list);
Namhyung Kim97f63e42013-01-22 18:09:29 +09001505 evlist->nr_groups += data.nr_groups;
Wang Nan15bfd2c2015-07-10 07:36:09 +00001506 last = perf_evlist__last(evlist);
1507 last->cmdline_group_boundary = true;
1508
Jiri Olsa89812fc2012-03-15 20:09:15 +01001509 return 0;
1510 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02001511
Jiri Olsa5d7be902012-03-20 19:15:40 +01001512 /*
1513 * There are 2 users - builtin-record and builtin-test objects.
1514 * Both call perf_evlist__delete in case of error, so we dont
1515 * need to bother.
1516 */
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02001517 return ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001518}
1519
Jiri Olsab39b8392015-04-22 21:10:16 +02001520#define MAX_WIDTH 1000
1521static int get_term_width(void)
1522{
1523 struct winsize ws;
1524
1525 get_term_dimensions(&ws);
1526 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1527}
1528
1529static void parse_events_print_error(struct parse_events_error *err,
1530 const char *event)
1531{
1532 const char *str = "invalid or unsupported event: ";
1533 char _buf[MAX_WIDTH];
1534 char *buf = (char *) event;
1535 int idx = 0;
1536
1537 if (err->str) {
1538 /* -2 for extra '' in the final fprintf */
1539 int width = get_term_width() - 2;
1540 int len_event = strlen(event);
1541 int len_str, max_len, cut = 0;
1542
1543 /*
1544 * Maximum error index indent, we will cut
1545 * the event string if it's bigger.
1546 */
Adrian Hunter141b2d32015-07-17 19:33:51 +03001547 int max_err_idx = 13;
Jiri Olsab39b8392015-04-22 21:10:16 +02001548
1549 /*
1550 * Let's be specific with the message when
1551 * we have the precise error.
1552 */
1553 str = "event syntax error: ";
1554 len_str = strlen(str);
1555 max_len = width - len_str;
1556
1557 buf = _buf;
1558
1559 /* We're cutting from the beggining. */
1560 if (err->idx > max_err_idx)
1561 cut = err->idx - max_err_idx;
1562
1563 strncpy(buf, event + cut, max_len);
1564
1565 /* Mark cut parts with '..' on both sides. */
1566 if (cut)
1567 buf[0] = buf[1] = '.';
1568
1569 if ((len_event - cut) > max_len) {
1570 buf[max_len - 1] = buf[max_len - 2] = '.';
1571 buf[max_len] = 0;
1572 }
1573
1574 idx = len_str + err->idx - cut;
1575 }
1576
1577 fprintf(stderr, "%s'%s'\n", str, buf);
1578 if (idx) {
1579 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
1580 if (err->help)
1581 fprintf(stderr, "\n%s\n", err->help);
1582 free(err->str);
1583 free(err->help);
1584 }
1585
1586 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
1587}
1588
1589#undef MAX_WIDTH
1590
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001591int parse_events_option(const struct option *opt, const char *str,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001592 int unset __maybe_unused)
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001593{
1594 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
Jiri Olsab39b8392015-04-22 21:10:16 +02001595 struct parse_events_error err = { .idx = 0, };
1596 int ret = parse_events(evlist, str, &err);
Andi Kleen9175ce12012-10-26 13:30:06 -07001597
Jiri Olsab39b8392015-04-22 21:10:16 +02001598 if (ret)
1599 parse_events_print_error(&err, str);
1600
Andi Kleen9175ce12012-10-26 13:30:06 -07001601 return ret;
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001602}
1603
Wang Nan4ba1faa2015-07-10 07:36:10 +00001604static int
1605foreach_evsel_in_last_glob(struct perf_evlist *evlist,
1606 int (*func)(struct perf_evsel *evsel,
1607 const void *arg),
1608 const void *arg)
Li Zefanc171b552009-10-15 11:22:07 +08001609{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001610 struct perf_evsel *last = NULL;
Wang Nan4ba1faa2015-07-10 07:36:10 +00001611 int err;
Li Zefanc171b552009-10-15 11:22:07 +08001612
Wang Nan854f7362015-09-06 07:13:17 +00001613 /*
1614 * Don't return when list_empty, give func a chance to report
1615 * error when it found last == NULL.
1616 *
1617 * So no need to WARN here, let *func do this.
1618 */
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001619 if (evlist->nr_entries > 0)
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001620 last = perf_evlist__last(evlist);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001621
Wang Nan15bfd2c2015-07-10 07:36:09 +00001622 do {
Wang Nan4ba1faa2015-07-10 07:36:10 +00001623 err = (*func)(last, arg);
1624 if (err)
Wang Nan15bfd2c2015-07-10 07:36:09 +00001625 return -1;
Wang Nan4ba1faa2015-07-10 07:36:10 +00001626 if (!last)
1627 return 0;
Wang Nan15bfd2c2015-07-10 07:36:09 +00001628
1629 if (last->node.prev == &evlist->entries)
1630 return 0;
1631 last = list_entry(last->node.prev, struct perf_evsel, node);
1632 } while (!last->cmdline_group_boundary);
Li Zefanc171b552009-10-15 11:22:07 +08001633
1634 return 0;
1635}
1636
Wang Nan4ba1faa2015-07-10 07:36:10 +00001637static int set_filter(struct perf_evsel *evsel, const void *arg)
1638{
1639 const char *str = arg;
1640
1641 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
1642 fprintf(stderr,
1643 "--filter option should follow a -e tracepoint option\n");
1644 return -1;
1645 }
1646
1647 if (perf_evsel__append_filter(evsel, "&&", str) < 0) {
1648 fprintf(stderr,
1649 "not enough memory to hold filter string\n");
1650 return -1;
1651 }
1652
1653 return 0;
1654}
1655
1656int parse_filter(const struct option *opt, const char *str,
1657 int unset __maybe_unused)
1658{
1659 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1660
1661 return foreach_evsel_in_last_glob(evlist, set_filter,
1662 (const void *)str);
1663}
1664
1665static int add_exclude_perf_filter(struct perf_evsel *evsel,
1666 const void *arg __maybe_unused)
1667{
1668 char new_filter[64];
1669
1670 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
1671 fprintf(stderr,
1672 "--exclude-perf option should follow a -e tracepoint option\n");
1673 return -1;
1674 }
1675
1676 snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
1677
1678 if (perf_evsel__append_filter(evsel, "&&", new_filter) < 0) {
1679 fprintf(stderr,
1680 "not enough memory to hold filter string\n");
1681 return -1;
1682 }
1683
1684 return 0;
1685}
1686
1687int exclude_perf(const struct option *opt,
1688 const char *arg __maybe_unused,
1689 int unset __maybe_unused)
1690{
1691 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1692
1693 return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
1694 NULL);
1695}
1696
Thomas Gleixner86847b62009-06-06 12:24:17 +02001697static const char * const event_type_descriptors[] = {
Thomas Gleixner86847b62009-06-06 12:24:17 +02001698 "Hardware event",
1699 "Software event",
1700 "Tracepoint event",
1701 "Hardware cache event",
Liming Wang41bdcb22009-12-29 16:37:07 +08001702 "Raw hardware event descriptor",
1703 "Hardware breakpoint",
Thomas Gleixner86847b62009-06-06 12:24:17 +02001704};
1705
Yunlong Songab0e4802015-02-27 18:21:25 +08001706static int cmp_string(const void *a, const void *b)
1707{
1708 const char * const *as = a;
1709 const char * const *bs = b;
1710
1711 return strcmp(*as, *bs);
1712}
1713
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001714/*
Jason Baronf6bdafe2009-07-21 12:20:22 -04001715 * Print the events from <debugfs_mount_point>/tracing/events
1716 */
1717
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001718void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
1719 bool name_only)
Jason Baronf6bdafe2009-07-21 12:20:22 -04001720{
1721 DIR *sys_dir, *evt_dir;
1722 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001723 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +02001724 char dir_path[MAXPATHLEN];
Yunlong Songab0e4802015-02-27 18:21:25 +08001725 char **evt_list = NULL;
1726 unsigned int evt_i = 0, evt_num = 0;
1727 bool evt_num_known = false;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001728
Yunlong Songab0e4802015-02-27 18:21:25 +08001729restart:
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001730 sys_dir = opendir(tracing_events_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -04001731 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +02001732 return;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001733
Yunlong Songab0e4802015-02-27 18:21:25 +08001734 if (evt_num_known) {
1735 evt_list = zalloc(sizeof(char *) * evt_num);
1736 if (!evt_list)
1737 goto out_close_sys_dir;
1738 }
1739
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03001740 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001741 if (subsys_glob != NULL &&
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001742 !strglobmatch(sys_dirent.d_name, subsys_glob))
1743 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02001744
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001745 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Eric Dumazet725b1362009-09-24 15:39:09 +02001746 sys_dirent.d_name);
1747 evt_dir = opendir(dir_path);
1748 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03001749 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02001750
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03001751 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001752 if (event_glob != NULL &&
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001753 !strglobmatch(evt_dirent.d_name, event_glob))
1754 continue;
1755
Yunlong Songab0e4802015-02-27 18:21:25 +08001756 if (!evt_num_known) {
1757 evt_num++;
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001758 continue;
1759 }
1760
Jason Baronf6bdafe2009-07-21 12:20:22 -04001761 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1762 sys_dirent.d_name, evt_dirent.d_name);
Yunlong Songab0e4802015-02-27 18:21:25 +08001763
1764 evt_list[evt_i] = strdup(evt_path);
1765 if (evt_list[evt_i] == NULL)
1766 goto out_close_evt_dir;
1767 evt_i++;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001768 }
1769 closedir(evt_dir);
1770 }
Jason Baronf6bdafe2009-07-21 12:20:22 -04001771 closedir(sys_dir);
Yunlong Songab0e4802015-02-27 18:21:25 +08001772
1773 if (!evt_num_known) {
1774 evt_num_known = true;
1775 goto restart;
1776 }
1777 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1778 evt_i = 0;
1779 while (evt_i < evt_num) {
1780 if (name_only) {
1781 printf("%s ", evt_list[evt_i++]);
1782 continue;
1783 }
1784 printf(" %-50s [%s]\n", evt_list[evt_i++],
1785 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1786 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03001787 if (evt_num && pager_in_use())
Yunlong Songab0e4802015-02-27 18:21:25 +08001788 printf("\n");
1789
1790out_free:
1791 evt_num = evt_i;
1792 for (evt_i = 0; evt_i < evt_num; evt_i++)
1793 zfree(&evt_list[evt_i]);
1794 zfree(&evt_list);
1795 return;
1796
1797out_close_evt_dir:
1798 closedir(evt_dir);
1799out_close_sys_dir:
1800 closedir(sys_dir);
1801
1802 printf("FATAL: not enough memory to print %s\n",
1803 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1804 if (evt_list)
1805 goto out_free;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001806}
1807
1808/*
Thomas Renninger20c457b2011-01-03 17:50:45 +01001809 * Check whether event is in <debugfs_mount_point>/tracing/events
1810 */
1811
1812int is_valid_tracepoint(const char *event_string)
1813{
1814 DIR *sys_dir, *evt_dir;
1815 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
1816 char evt_path[MAXPATHLEN];
1817 char dir_path[MAXPATHLEN];
1818
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001819 sys_dir = opendir(tracing_events_path);
Thomas Renninger20c457b2011-01-03 17:50:45 +01001820 if (!sys_dir)
1821 return 0;
1822
1823 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1824
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001825 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Thomas Renninger20c457b2011-01-03 17:50:45 +01001826 sys_dirent.d_name);
1827 evt_dir = opendir(dir_path);
1828 if (!evt_dir)
1829 continue;
1830
1831 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1832 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1833 sys_dirent.d_name, evt_dirent.d_name);
1834 if (!strcmp(evt_path, event_string)) {
1835 closedir(evt_dir);
1836 closedir(sys_dir);
1837 return 1;
1838 }
1839 }
1840 closedir(evt_dir);
1841 }
1842 closedir(sys_dir);
1843 return 0;
1844}
1845
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001846static bool is_event_supported(u8 type, unsigned config)
1847{
1848 bool ret = true;
Vince Weaver88fee522013-12-30 15:39:45 -05001849 int open_return;
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001850 struct perf_evsel *evsel;
1851 struct perf_event_attr attr = {
1852 .type = type,
1853 .config = config,
1854 .disabled = 1,
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001855 };
1856 struct {
1857 struct thread_map map;
1858 int threads[1];
1859 } tmap = {
1860 .map.nr = 1,
1861 .threads = { 0 },
1862 };
1863
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03001864 evsel = perf_evsel__new(&attr);
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001865 if (evsel) {
Vince Weaver88fee522013-12-30 15:39:45 -05001866 open_return = perf_evsel__open(evsel, NULL, &tmap.map);
1867 ret = open_return >= 0;
1868
1869 if (open_return == -EACCES) {
1870 /*
1871 * This happens if the paranoid value
1872 * /proc/sys/kernel/perf_event_paranoid is set to 2
1873 * Re-run with exclude_kernel set; we don't do that
1874 * by default as some ARM machines do not support it.
1875 *
1876 */
1877 evsel->attr.exclude_kernel = 1;
1878 ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0;
1879 }
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001880 perf_evsel__delete(evsel);
1881 }
1882
1883 return ret;
1884}
1885
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001886int print_hwcache_events(const char *event_glob, bool name_only)
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001887{
Yunlong Songab0e4802015-02-27 18:21:25 +08001888 unsigned int type, op, i, evt_i = 0, evt_num = 0;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03001889 char name[64];
Yunlong Songab0e4802015-02-27 18:21:25 +08001890 char **evt_list = NULL;
1891 bool evt_num_known = false;
1892
1893restart:
1894 if (evt_num_known) {
1895 evt_list = zalloc(sizeof(char *) * evt_num);
1896 if (!evt_list)
1897 goto out_enomem;
1898 }
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001899
1900 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1901 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1902 /* skip invalid cache type */
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03001903 if (!perf_evsel__is_cache_op_valid(type, op))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001904 continue;
1905
1906 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03001907 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
1908 name, sizeof(name));
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001909 if (event_glob != NULL && !strglobmatch(name, event_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001910 continue;
1911
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001912 if (!is_event_supported(PERF_TYPE_HW_CACHE,
1913 type | (op << 8) | (i << 16)))
1914 continue;
1915
Yunlong Songab0e4802015-02-27 18:21:25 +08001916 if (!evt_num_known) {
1917 evt_num++;
1918 continue;
1919 }
1920
1921 evt_list[evt_i] = strdup(name);
1922 if (evt_list[evt_i] == NULL)
1923 goto out_enomem;
1924 evt_i++;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001925 }
1926 }
1927 }
1928
Yunlong Songab0e4802015-02-27 18:21:25 +08001929 if (!evt_num_known) {
1930 evt_num_known = true;
1931 goto restart;
1932 }
1933 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1934 evt_i = 0;
1935 while (evt_i < evt_num) {
1936 if (name_only) {
1937 printf("%s ", evt_list[evt_i++]);
1938 continue;
1939 }
1940 printf(" %-50s [%s]\n", evt_list[evt_i++],
1941 event_type_descriptors[PERF_TYPE_HW_CACHE]);
1942 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03001943 if (evt_num && pager_in_use())
Andi Kleendc098b32013-04-20 11:02:29 -07001944 printf("\n");
Yunlong Songab0e4802015-02-27 18:21:25 +08001945
1946out_free:
1947 evt_num = evt_i;
1948 for (evt_i = 0; evt_i < evt_num; evt_i++)
1949 zfree(&evt_list[evt_i]);
1950 zfree(&evt_list);
1951 return evt_num;
1952
1953out_enomem:
1954 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
1955 if (evt_list)
1956 goto out_free;
1957 return evt_num;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001958}
1959
Yunlong Song705750f2015-02-27 18:21:27 +08001960void print_symbol_events(const char *event_glob, unsigned type,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001961 struct event_symbol *syms, unsigned max,
1962 bool name_only)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001963{
Yunlong Songab0e4802015-02-27 18:21:25 +08001964 unsigned int i, evt_i = 0, evt_num = 0;
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001965 char name[MAX_NAME_LEN];
Yunlong Songab0e4802015-02-27 18:21:25 +08001966 char **evt_list = NULL;
1967 bool evt_num_known = false;
1968
1969restart:
1970 if (evt_num_known) {
1971 evt_list = zalloc(sizeof(char *) * evt_num);
1972 if (!evt_list)
1973 goto out_enomem;
1974 syms -= max;
1975 }
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001976
Jiri Olsa1dc12762012-07-04 00:00:44 +02001977 for (i = 0; i < max; i++, syms++) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001978
Arnaldo Carvalho de Meloe37df6c2015-11-27 16:04:58 -03001979 if (event_glob != NULL && syms->symbol != NULL &&
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001980 !(strglobmatch(syms->symbol, event_glob) ||
1981 (syms->alias && strglobmatch(syms->alias, event_glob))))
1982 continue;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001983
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001984 if (!is_event_supported(type, i))
1985 continue;
1986
Yunlong Songab0e4802015-02-27 18:21:25 +08001987 if (!evt_num_known) {
1988 evt_num++;
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001989 continue;
1990 }
1991
Yunlong Songab0e4802015-02-27 18:21:25 +08001992 if (!name_only && strlen(syms->alias))
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001993 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301994 else
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001995 strncpy(name, syms->symbol, MAX_NAME_LEN);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001996
Yunlong Songab0e4802015-02-27 18:21:25 +08001997 evt_list[evt_i] = strdup(name);
1998 if (evt_list[evt_i] == NULL)
1999 goto out_enomem;
2000 evt_i++;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002001 }
2002
Yunlong Songab0e4802015-02-27 18:21:25 +08002003 if (!evt_num_known) {
2004 evt_num_known = true;
2005 goto restart;
2006 }
2007 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2008 evt_i = 0;
2009 while (evt_i < evt_num) {
2010 if (name_only) {
2011 printf("%s ", evt_list[evt_i++]);
2012 continue;
2013 }
2014 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
2015 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002016 if (evt_num && pager_in_use())
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002017 printf("\n");
Yunlong Songab0e4802015-02-27 18:21:25 +08002018
2019out_free:
2020 evt_num = evt_i;
2021 for (evt_i = 0; evt_i < evt_num; evt_i++)
2022 zfree(&evt_list[evt_i]);
2023 zfree(&evt_list);
2024 return;
2025
2026out_enomem:
2027 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
2028 if (evt_list)
2029 goto out_free;
Jiri Olsa1dc12762012-07-04 00:00:44 +02002030}
2031
2032/*
2033 * Print the help text for the event symbols:
2034 */
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002035void print_events(const char *event_glob, bool name_only)
Jiri Olsa1dc12762012-07-04 00:00:44 +02002036{
Jiri Olsa1dc12762012-07-04 00:00:44 +02002037 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002038 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
Jiri Olsa1dc12762012-07-04 00:00:44 +02002039
2040 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002041 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
Jiri Olsa1dc12762012-07-04 00:00:44 +02002042
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002043 print_hwcache_events(event_glob, name_only);
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002044
Andi Kleendc098b32013-04-20 11:02:29 -07002045 print_pmu_events(event_glob, name_only);
2046
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002047 if (event_glob != NULL)
2048 return;
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +05302049
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002050 if (!name_only) {
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002051 printf(" %-50s [%s]\n",
2052 "rNNN",
2053 event_type_descriptors[PERF_TYPE_RAW]);
2054 printf(" %-50s [%s]\n",
2055 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
2056 event_type_descriptors[PERF_TYPE_RAW]);
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002057 if (pager_in_use())
2058 printf(" (see 'man perf-list' on how to encode it)\n\n");
Thomas Gleixner86847b62009-06-06 12:24:17 +02002059
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002060 printf(" %-50s [%s]\n",
Jacob Shin3741eb92014-05-29 17:26:51 +02002061 "mem:<addr>[/len][:access]",
Liming Wang41bdcb22009-12-29 16:37:07 +08002062 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002063 if (pager_in_use())
2064 printf("\n");
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002065 }
Frederic Weisbecker1b290d62009-11-23 15:42:35 +01002066
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002067 print_tracepoint_events(NULL, NULL, name_only);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002068}
Jiri Olsa8f707d82012-03-15 20:09:16 +01002069
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002070int parse_events__is_hardcoded_term(struct parse_events_term *term)
Jiri Olsa8f707d82012-03-15 20:09:16 +01002071{
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002072 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002073}
2074
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002075static int new_term(struct parse_events_term **_term, int type_val,
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002076 int type_term, char *config,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002077 char *str, u64 num, int err_term, int err_val)
Jiri Olsa8f707d82012-03-15 20:09:16 +01002078{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002079 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002080
2081 term = zalloc(sizeof(*term));
2082 if (!term)
2083 return -ENOMEM;
2084
2085 INIT_LIST_HEAD(&term->list);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002086 term->type_val = type_val;
2087 term->type_term = type_term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002088 term->config = config;
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002089 term->err_term = err_term;
2090 term->err_val = err_val;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002091
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002092 switch (type_val) {
Jiri Olsa8f707d82012-03-15 20:09:16 +01002093 case PARSE_EVENTS__TERM_TYPE_NUM:
2094 term->val.num = num;
2095 break;
2096 case PARSE_EVENTS__TERM_TYPE_STR:
2097 term->val.str = str;
2098 break;
2099 default:
Adrian Hunter4be8be62013-07-04 16:20:24 +03002100 free(term);
Jiri Olsa8f707d82012-03-15 20:09:16 +01002101 return -EINVAL;
2102 }
2103
2104 *_term = term;
2105 return 0;
2106}
2107
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002108int parse_events_term__num(struct parse_events_term **term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002109 int type_term, char *config, u64 num,
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002110 void *loc_term_, void *loc_val_)
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002111{
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002112 YYLTYPE *loc_term = loc_term_;
2113 YYLTYPE *loc_val = loc_val_;
2114
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002115 return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002116 config, NULL, num,
2117 loc_term ? loc_term->first_column : 0,
2118 loc_val ? loc_val->first_column : 0);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002119}
2120
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002121int parse_events_term__str(struct parse_events_term **term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002122 int type_term, char *config, char *str,
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002123 void *loc_term_, void *loc_val_)
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002124{
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002125 YYLTYPE *loc_term = loc_term_;
2126 YYLTYPE *loc_val = loc_val_;
2127
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002128 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002129 config, str, 0,
2130 loc_term ? loc_term->first_column : 0,
2131 loc_val ? loc_val->first_column : 0);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002132}
2133
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002134int parse_events_term__sym_hw(struct parse_events_term **term,
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02002135 char *config, unsigned idx)
2136{
2137 struct event_symbol *sym;
2138
2139 BUG_ON(idx >= PERF_COUNT_HW_MAX);
2140 sym = &event_symbols_hw[idx];
2141
2142 if (config)
2143 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
2144 PARSE_EVENTS__TERM_TYPE_USER, config,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002145 (char *) sym->symbol, 0, 0, 0);
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02002146 else
2147 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
2148 PARSE_EVENTS__TERM_TYPE_USER,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002149 (char *) "event", (char *) sym->symbol,
2150 0, 0, 0);
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02002151}
2152
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002153int parse_events_term__clone(struct parse_events_term **new,
2154 struct parse_events_term *term)
Zheng Yana6146d52012-06-15 14:31:41 +08002155{
2156 return new_term(new, term->type_val, term->type_term, term->config,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002157 term->val.str, term->val.num,
2158 term->err_term, term->err_val);
Zheng Yana6146d52012-06-15 14:31:41 +08002159}
2160
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002161void parse_events_terms__purge(struct list_head *terms)
Jiri Olsa8f707d82012-03-15 20:09:16 +01002162{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002163 struct parse_events_term *term, *h;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002164
Wang Nana8adfce2016-02-12 16:31:23 -03002165 list_for_each_entry_safe(term, h, terms, list) {
2166 list_del_init(&term->list);
Jiri Olsa8f707d82012-03-15 20:09:16 +01002167 free(term);
Wang Nana8adfce2016-02-12 16:31:23 -03002168 }
Jiri Olsa8f707d82012-03-15 20:09:16 +01002169}
Jiri Olsab39b8392015-04-22 21:10:16 +02002170
Arnaldo Carvalho de Melo2146afc2016-02-12 17:09:17 -03002171void parse_events_terms__delete(struct list_head *terms)
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002172{
Arnaldo Carvalho de Melo2146afc2016-02-12 17:09:17 -03002173 if (!terms)
2174 return;
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002175 parse_events_terms__purge(terms);
Wang Nand20a5f22016-02-12 17:01:17 -03002176 free(terms);
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002177}
2178
Jiri Olsab39b8392015-04-22 21:10:16 +02002179void parse_events_evlist_error(struct parse_events_evlist *data,
2180 int idx, const char *str)
2181{
2182 struct parse_events_error *err = data->error;
2183
Adrian Huntera6ced2b2015-05-19 16:05:44 +03002184 if (!err)
2185 return;
Jiri Olsab39b8392015-04-22 21:10:16 +02002186 err->idx = idx;
2187 err->str = strdup(str);
2188 WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
2189}
He Kuangffeb8832015-09-28 03:52:14 +00002190
Wang Nan17cb5f82016-02-19 11:43:57 +00002191static void config_terms_list(char *buf, size_t buf_sz)
2192{
2193 int i;
2194 bool first = true;
2195
2196 buf[0] = '\0';
2197 for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
2198 const char *name = config_term_names[i];
2199
Wang Nan1669e502016-02-19 11:43:58 +00002200 if (!config_term_avail(i, NULL))
2201 continue;
Wang Nan17cb5f82016-02-19 11:43:57 +00002202 if (!name)
2203 continue;
2204 if (name[0] == '<')
2205 continue;
2206
2207 if (strlen(buf) + strlen(name) + 2 >= buf_sz)
2208 return;
2209
2210 if (!first)
2211 strcat(buf, ",");
2212 else
2213 first = false;
2214 strcat(buf, name);
2215 }
2216}
2217
He Kuangffeb8832015-09-28 03:52:14 +00002218/*
2219 * Return string contains valid config terms of an event.
2220 * @additional_terms: For terms such as PMU sysfs terms.
2221 */
2222char *parse_events_formats_error_string(char *additional_terms)
2223{
2224 char *str;
Wang Nan17cb5f82016-02-19 11:43:57 +00002225 /* "branch_type" is the longest name */
2226 char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
2227 (sizeof("branch_type") - 1)];
He Kuangffeb8832015-09-28 03:52:14 +00002228
Wang Nan17cb5f82016-02-19 11:43:57 +00002229 config_terms_list(static_terms, sizeof(static_terms));
He Kuangffeb8832015-09-28 03:52:14 +00002230 /* valid terms */
2231 if (additional_terms) {
Wang Nan26dee022016-02-19 11:43:52 +00002232 if (asprintf(&str, "valid terms: %s,%s",
2233 additional_terms, static_terms) < 0)
He Kuangffeb8832015-09-28 03:52:14 +00002234 goto fail;
2235 } else {
Wang Nan26dee022016-02-19 11:43:52 +00002236 if (asprintf(&str, "valid terms: %s", static_terms) < 0)
He Kuangffeb8832015-09-28 03:52:14 +00002237 goto fail;
2238 }
2239 return str;
2240
2241fail:
2242 return NULL;
2243}