perf ebpf: Add the libbpf glue
[linux-2.6-block.git] / tools / perf / util / parse-events.c
CommitLineData
d2709c7c 1#include <linux/hw_breakpoint.h>
8dd2a131 2#include <linux/err.h>
8ad8db37 3#include "util.h"
6b58e7f1 4#include "../perf.h"
361c99a6 5#include "evlist.h"
69aad6f1 6#include "evsel.h"
8ad8db37
IM
7#include "parse-options.h"
8#include "parse-events.h"
9#include "exec_cmd.h"
42f60c2d 10#include "string.h"
5aab621b 11#include "symbol.h"
5beeded1 12#include "cache.h"
8755a8f2 13#include "header.h"
6e81c74c 14#include "debug.h"
592d5a6b 15#include <api/fs/tracing_path.h>
ac20de6f 16#include "parse-events-bison.h"
90e2b22d 17#define YY_EXTRA_TYPE int
89812fc8 18#include "parse-events-flex.h"
5f537a26 19#include "pmu.h"
b41f1cec 20#include "thread_map.h"
f30a79b0 21#include "cpumap.h"
b39b8393 22#include "asm/bug.h"
89812fc8
JO
23
24#define MAX_NAME_LEN 100
8ad8db37 25
82ba1f2f
JO
26#ifdef PARSER_DEBUG
27extern int parse_events_debug;
28#endif
ac20de6f 29int parse_events_parse(void *data, void *scanner);
e637d177
HK
30static int get_config_terms(struct list_head *head_config,
31 struct list_head *head_terms __maybe_unused);
bcd3279f 32
dcb4e102
KL
33static struct perf_pmu_event_symbol *perf_pmu_events_list;
34/*
35 * The variable indicates the number of supported pmu event symbols.
36 * 0 means not initialized and ready to init
37 * -1 means failed to init, don't try anymore
38 * >0 is the number of supported pmu event symbols
39 */
40static int perf_pmu_events_list_num;
41
705750f2 42struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
1dc12760
JO
43 [PERF_COUNT_HW_CPU_CYCLES] = {
44 .symbol = "cpu-cycles",
45 .alias = "cycles",
46 },
47 [PERF_COUNT_HW_INSTRUCTIONS] = {
48 .symbol = "instructions",
49 .alias = "",
50 },
51 [PERF_COUNT_HW_CACHE_REFERENCES] = {
52 .symbol = "cache-references",
53 .alias = "",
54 },
55 [PERF_COUNT_HW_CACHE_MISSES] = {
56 .symbol = "cache-misses",
57 .alias = "",
58 },
59 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
60 .symbol = "branch-instructions",
61 .alias = "branches",
62 },
63 [PERF_COUNT_HW_BRANCH_MISSES] = {
64 .symbol = "branch-misses",
65 .alias = "",
66 },
67 [PERF_COUNT_HW_BUS_CYCLES] = {
68 .symbol = "bus-cycles",
69 .alias = "",
70 },
71 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
72 .symbol = "stalled-cycles-frontend",
73 .alias = "idle-cycles-frontend",
74 },
75 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
76 .symbol = "stalled-cycles-backend",
77 .alias = "idle-cycles-backend",
78 },
79 [PERF_COUNT_HW_REF_CPU_CYCLES] = {
80 .symbol = "ref-cycles",
81 .alias = "",
82 },
83};
84
705750f2 85struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
1dc12760
JO
86 [PERF_COUNT_SW_CPU_CLOCK] = {
87 .symbol = "cpu-clock",
88 .alias = "",
89 },
90 [PERF_COUNT_SW_TASK_CLOCK] = {
91 .symbol = "task-clock",
92 .alias = "",
93 },
94 [PERF_COUNT_SW_PAGE_FAULTS] = {
95 .symbol = "page-faults",
96 .alias = "faults",
97 },
98 [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
99 .symbol = "context-switches",
100 .alias = "cs",
101 },
102 [PERF_COUNT_SW_CPU_MIGRATIONS] = {
103 .symbol = "cpu-migrations",
104 .alias = "migrations",
105 },
106 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
107 .symbol = "minor-faults",
108 .alias = "",
109 },
110 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
111 .symbol = "major-faults",
112 .alias = "",
113 },
114 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
115 .symbol = "alignment-faults",
116 .alias = "",
117 },
118 [PERF_COUNT_SW_EMULATION_FAULTS] = {
119 .symbol = "emulation-faults",
120 .alias = "",
121 },
d22d1a2a
AH
122 [PERF_COUNT_SW_DUMMY] = {
123 .symbol = "dummy",
124 .alias = "",
125 },
8ad8db37
IM
126};
127
cdd6c482
IM
128#define __PERF_EVENT_FIELD(config, name) \
129 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
5242519b 130
1fc570ad 131#define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
cdd6c482 132#define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
1fc570ad 133#define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
cdd6c482 134#define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
5242519b 135
6b58e7f1 136#define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
f6bdafef 137 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
6b58e7f1 138 if (sys_dirent.d_type == DT_DIR && \
f6bdafef
JB
139 (strcmp(sys_dirent.d_name, ".")) && \
140 (strcmp(sys_dirent.d_name, "..")))
141
ae07b63f
PZ
142static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
143{
144 char evt_path[MAXPATHLEN];
145 int fd;
146
ebf294bf 147 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
ae07b63f
PZ
148 sys_dir->d_name, evt_dir->d_name);
149 fd = open(evt_path, O_RDONLY);
150 if (fd < 0)
151 return -EINVAL;
152 close(fd);
153
154 return 0;
155}
156
6b58e7f1 157#define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
f6bdafef 158 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
6b58e7f1 159 if (evt_dirent.d_type == DT_DIR && \
f6bdafef 160 (strcmp(evt_dirent.d_name, ".")) && \
ae07b63f
PZ
161 (strcmp(evt_dirent.d_name, "..")) && \
162 (!tp_event_has_id(&sys_dirent, &evt_dirent)))
f6bdafef 163
270bbbe8 164#define MAX_EVENT_LENGTH 512
f6bdafef 165
f6bdafef 166
1ef2ed10 167struct tracepoint_path *tracepoint_id_to_path(u64 config)
f6bdafef 168{
1ef2ed10 169 struct tracepoint_path *path = NULL;
f6bdafef
JB
170 DIR *sys_dir, *evt_dir;
171 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
8aa8a7c8 172 char id_buf[24];
725b1368 173 int fd;
f6bdafef
JB
174 u64 id;
175 char evt_path[MAXPATHLEN];
725b1368 176 char dir_path[MAXPATHLEN];
f6bdafef 177
ebf294bf 178 sys_dir = opendir(tracing_events_path);
f6bdafef 179 if (!sys_dir)
725b1368 180 return NULL;
6b58e7f1
UD
181
182 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
725b1368 183
ebf294bf 184 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
725b1368
ED
185 sys_dirent.d_name);
186 evt_dir = opendir(dir_path);
187 if (!evt_dir)
6b58e7f1 188 continue;
725b1368 189
6b58e7f1 190 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
725b1368
ED
191
192 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
f6bdafef 193 evt_dirent.d_name);
725b1368 194 fd = open(evt_path, O_RDONLY);
f6bdafef
JB
195 if (fd < 0)
196 continue;
197 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
198 close(fd);
199 continue;
200 }
201 close(fd);
202 id = atoll(id_buf);
203 if (id == config) {
204 closedir(evt_dir);
205 closedir(sys_dir);
59b4caeb 206 path = zalloc(sizeof(*path));
1ef2ed10
FW
207 path->system = malloc(MAX_EVENT_LENGTH);
208 if (!path->system) {
209 free(path);
210 return NULL;
211 }
212 path->name = malloc(MAX_EVENT_LENGTH);
213 if (!path->name) {
74cf249d 214 zfree(&path->system);
1ef2ed10
FW
215 free(path);
216 return NULL;
217 }
218 strncpy(path->system, sys_dirent.d_name,
219 MAX_EVENT_LENGTH);
220 strncpy(path->name, evt_dirent.d_name,
221 MAX_EVENT_LENGTH);
222 return path;
f6bdafef
JB
223 }
224 }
225 closedir(evt_dir);
226 }
227
f6bdafef 228 closedir(sys_dir);
1ef2ed10
FW
229 return NULL;
230}
231
e7c93f09
NK
232struct tracepoint_path *tracepoint_name_to_path(const char *name)
233{
234 struct tracepoint_path *path = zalloc(sizeof(*path));
235 char *str = strchr(name, ':');
236
237 if (path == NULL || str == NULL) {
238 free(path);
239 return NULL;
240 }
241
242 path->system = strndup(name, str - name);
243 path->name = strdup(str+1);
244
245 if (path->system == NULL || path->name == NULL) {
74cf249d
ACM
246 zfree(&path->system);
247 zfree(&path->name);
e7c93f09
NK
248 free(path);
249 path = NULL;
250 }
251
252 return path;
253}
254
1424dc96
DA
255const char *event_type(int type)
256{
257 switch (type) {
258 case PERF_TYPE_HARDWARE:
259 return "hardware";
260
261 case PERF_TYPE_SOFTWARE:
262 return "software";
263
264 case PERF_TYPE_TRACEPOINT:
265 return "tracepoint";
266
267 case PERF_TYPE_HW_CACHE:
268 return "hardware-cache";
269
270 default:
271 break;
272 }
273
274 return "unknown";
275}
276
7ae92e74
YZ
277
278
410136f5
SE
279static struct perf_evsel *
280__add_event(struct list_head *list, int *idx,
281 struct perf_event_attr *attr,
930a2e29
JO
282 char *name, struct cpu_map *cpus,
283 struct list_head *config_terms)
89812fc8
JO
284{
285 struct perf_evsel *evsel;
286
287 event_attr_init(attr);
288
ef503831 289 evsel = perf_evsel__new_idx(attr, (*idx)++);
c5cd8ac0 290 if (!evsel)
410136f5 291 return NULL;
89812fc8 292
fce4d296
AH
293 evsel->cpus = cpu_map__get(cpus);
294 evsel->own_cpus = cpu_map__get(cpus);
f30a79b0 295
9db1763c
ACM
296 if (name)
297 evsel->name = strdup(name);
930a2e29
JO
298
299 if (config_terms)
300 list_splice(config_terms, &evsel->config_terms);
301
b847cbdc 302 list_add_tail(&evsel->node, list);
410136f5 303 return evsel;
89812fc8
JO
304}
305
c5cd8ac0 306static int add_event(struct list_head *list, int *idx,
930a2e29
JO
307 struct perf_event_attr *attr, char *name,
308 struct list_head *config_terms)
7ae92e74 309{
930a2e29 310 return __add_event(list, idx, attr, name, NULL, config_terms) ? 0 : -ENOMEM;
7ae92e74
YZ
311}
312
0b668bc9 313static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
8326f44d
IM
314{
315 int i, j;
61c45981 316 int n, longest = -1;
8326f44d
IM
317
318 for (i = 0; i < size; i++) {
0b668bc9 319 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
61c45981 320 n = strlen(names[i][j]);
89812fc8 321 if (n > longest && !strncasecmp(str, names[i][j], n))
61c45981
PM
322 longest = n;
323 }
89812fc8 324 if (longest > 0)
61c45981 325 return i;
8326f44d
IM
326 }
327
8953645f 328 return -1;
8326f44d
IM
329}
330
c5cd8ac0 331int parse_events_add_cache(struct list_head *list, int *idx,
89812fc8 332 char *type, char *op_result1, char *op_result2)
8326f44d 333{
89812fc8
JO
334 struct perf_event_attr attr;
335 char name[MAX_NAME_LEN];
61c45981 336 int cache_type = -1, cache_op = -1, cache_result = -1;
89812fc8
JO
337 char *op_result[2] = { op_result1, op_result2 };
338 int i, n;
8326f44d 339
8326f44d
IM
340 /*
341 * No fallback - if we cannot get a clear cache type
342 * then bail out:
343 */
0b668bc9 344 cache_type = parse_aliases(type, perf_evsel__hw_cache,
89812fc8 345 PERF_COUNT_HW_CACHE_MAX);
8326f44d 346 if (cache_type == -1)
89812fc8
JO
347 return -EINVAL;
348
349 n = snprintf(name, MAX_NAME_LEN, "%s", type);
61c45981 350
89812fc8
JO
351 for (i = 0; (i < 2) && (op_result[i]); i++) {
352 char *str = op_result[i];
353
275ef387 354 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
61c45981
PM
355
356 if (cache_op == -1) {
0b668bc9 357 cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
89812fc8 358 PERF_COUNT_HW_CACHE_OP_MAX);
61c45981 359 if (cache_op >= 0) {
0b668bc9 360 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
89812fc8 361 return -EINVAL;
61c45981
PM
362 continue;
363 }
364 }
365
366 if (cache_result == -1) {
0b668bc9
ACM
367 cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
368 PERF_COUNT_HW_CACHE_RESULT_MAX);
61c45981
PM
369 if (cache_result >= 0)
370 continue;
371 }
61c45981 372 }
8326f44d 373
8326f44d
IM
374 /*
375 * Fall back to reads:
376 */
8953645f
IM
377 if (cache_op == -1)
378 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
8326f44d 379
8326f44d
IM
380 /*
381 * Fall back to accesses:
382 */
383 if (cache_result == -1)
384 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
385
89812fc8
JO
386 memset(&attr, 0, sizeof(attr));
387 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
388 attr.type = PERF_TYPE_HW_CACHE;
930a2e29 389 return add_event(list, idx, &attr, name, NULL);
bcd3279f
FW
390}
391
272ed29a 392static void tracepoint_error(struct parse_events_error *e, int err,
19658171
JO
393 char *sys, char *name)
394{
395 char help[BUFSIZ];
396
397 /*
398 * We get error directly from syscall errno ( > 0),
399 * or from encoded pointer's error ( < 0).
400 */
401 err = abs(err);
402
403 switch (err) {
404 case EACCES:
272ed29a 405 e->str = strdup("can't access trace events");
19658171
JO
406 break;
407 case ENOENT:
272ed29a 408 e->str = strdup("unknown tracepoint");
19658171
JO
409 break;
410 default:
272ed29a 411 e->str = strdup("failed to add tracepoint");
19658171
JO
412 break;
413 }
414
415 tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
272ed29a 416 e->help = strdup(help);
19658171
JO
417}
418
c5cd8ac0 419static int add_tracepoint(struct list_head *list, int *idx,
e2f9f8ea 420 char *sys_name, char *evt_name,
272ed29a 421 struct parse_events_error *err,
e637d177 422 struct list_head *head_config)
bcd3279f 423{
82fe1c29 424 struct perf_evsel *evsel;
bcd3279f 425
ef503831 426 evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
19658171 427 if (IS_ERR(evsel)) {
272ed29a 428 tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
8dd2a131 429 return PTR_ERR(evsel);
19658171 430 }
bcd3279f 431
e637d177
HK
432 if (head_config) {
433 LIST_HEAD(config_terms);
434
435 if (get_config_terms(head_config, &config_terms))
436 return -ENOMEM;
437 list_splice(&config_terms, &evsel->config_terms);
438 }
439
82fe1c29 440 list_add_tail(&evsel->node, list);
82fe1c29 441 return 0;
8326f44d
IM
442}
443
c5cd8ac0 444static int add_tracepoint_multi_event(struct list_head *list, int *idx,
e2f9f8ea 445 char *sys_name, char *evt_name,
272ed29a 446 struct parse_events_error *err,
e637d177 447 struct list_head *head_config)
bcd3279f
FW
448{
449 char evt_path[MAXPATHLEN];
450 struct dirent *evt_ent;
451 DIR *evt_dir;
27bf90bf 452 int ret = 0, found = 0;
bcd3279f 453
ebf294bf 454 snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
bcd3279f 455 evt_dir = opendir(evt_path);
bcd3279f 456 if (!evt_dir) {
272ed29a 457 tracepoint_error(err, errno, sys_name, evt_name);
89812fc8 458 return -1;
bcd3279f
FW
459 }
460
89812fc8 461 while (!ret && (evt_ent = readdir(evt_dir))) {
bcd3279f
FW
462 if (!strcmp(evt_ent->d_name, ".")
463 || !strcmp(evt_ent->d_name, "..")
464 || !strcmp(evt_ent->d_name, "enable")
465 || !strcmp(evt_ent->d_name, "filter"))
466 continue;
467
89812fc8 468 if (!strglobmatch(evt_ent->d_name, evt_name))
fb1d2edf
MH
469 continue;
470
27bf90bf
JO
471 found++;
472
e637d177 473 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
272ed29a 474 err, head_config);
bcd3279f
FW
475 }
476
27bf90bf
JO
477 if (!found) {
478 tracepoint_error(err, ENOENT, sys_name, evt_name);
479 ret = -1;
480 }
481
0bd3f084 482 closedir(evt_dir);
89812fc8 483 return ret;
bcd3279f
FW
484}
485
c5cd8ac0 486static int add_tracepoint_event(struct list_head *list, int *idx,
e2f9f8ea 487 char *sys_name, char *evt_name,
272ed29a 488 struct parse_events_error *err,
e637d177 489 struct list_head *head_config)
f35488f9
JO
490{
491 return strpbrk(evt_name, "*?") ?
e637d177 492 add_tracepoint_multi_event(list, idx, sys_name, evt_name,
272ed29a 493 err, head_config) :
e637d177 494 add_tracepoint(list, idx, sys_name, evt_name,
272ed29a 495 err, head_config);
f35488f9
JO
496}
497
c5cd8ac0 498static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
e2f9f8ea 499 char *sys_name, char *evt_name,
272ed29a 500 struct parse_events_error *err,
e637d177 501 struct list_head *head_config)
f35488f9
JO
502{
503 struct dirent *events_ent;
504 DIR *events_dir;
505 int ret = 0;
506
507 events_dir = opendir(tracing_events_path);
508 if (!events_dir) {
272ed29a 509 tracepoint_error(err, errno, sys_name, evt_name);
f35488f9
JO
510 return -1;
511 }
512
513 while (!ret && (events_ent = readdir(events_dir))) {
514 if (!strcmp(events_ent->d_name, ".")
515 || !strcmp(events_ent->d_name, "..")
516 || !strcmp(events_ent->d_name, "enable")
517 || !strcmp(events_ent->d_name, "header_event")
518 || !strcmp(events_ent->d_name, "header_page"))
519 continue;
520
521 if (!strglobmatch(events_ent->d_name, sys_name))
522 continue;
523
524 ret = add_tracepoint_event(list, idx, events_ent->d_name,
272ed29a 525 evt_name, err, head_config);
f35488f9
JO
526 }
527
528 closedir(events_dir);
529 return ret;
530}
531
89812fc8
JO
532static int
533parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
1b290d67
FW
534{
535 int i;
536
537 for (i = 0; i < 3; i++) {
89812fc8 538 if (!type || !type[i])
1b290d67
FW
539 break;
540
7582732f
JO
541#define CHECK_SET_TYPE(bit) \
542do { \
543 if (attr->bp_type & bit) \
544 return -EINVAL; \
545 else \
546 attr->bp_type |= bit; \
547} while (0)
548
1b290d67
FW
549 switch (type[i]) {
550 case 'r':
7582732f 551 CHECK_SET_TYPE(HW_BREAKPOINT_R);
1b290d67
FW
552 break;
553 case 'w':
7582732f 554 CHECK_SET_TYPE(HW_BREAKPOINT_W);
1b290d67
FW
555 break;
556 case 'x':
7582732f 557 CHECK_SET_TYPE(HW_BREAKPOINT_X);
1b290d67
FW
558 break;
559 default:
89812fc8 560 return -EINVAL;
1b290d67
FW
561 }
562 }
89812fc8 563
7582732f
JO
564#undef CHECK_SET_TYPE
565
1b290d67
FW
566 if (!attr->bp_type) /* Default */
567 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
568
89812fc8 569 return 0;
1b290d67
FW
570}
571
c5cd8ac0 572int parse_events_add_breakpoint(struct list_head *list, int *idx,
3741eb9f 573 void *ptr, char *type, u64 len)
1b290d67 574{
89812fc8 575 struct perf_event_attr attr;
1b290d67 576
89812fc8 577 memset(&attr, 0, sizeof(attr));
9fafd98f 578 attr.bp_addr = (unsigned long) ptr;
1b290d67 579
89812fc8
JO
580 if (parse_breakpoint_type(type, &attr))
581 return -EINVAL;
1b290d67 582
3741eb9f
JS
583 /* Provide some defaults if len is not specified */
584 if (!len) {
585 if (attr.bp_type == HW_BREAKPOINT_X)
586 len = sizeof(long);
587 else
588 len = HW_BREAKPOINT_LEN_4;
589 }
590
591 attr.bp_len = len;
61c45981 592
89812fc8 593 attr.type = PERF_TYPE_BREAKPOINT;
4a841d65 594 attr.sample_period = 1;
b908debd 595
930a2e29 596 return add_event(list, idx, &attr, NULL, NULL);
74d5b588
JSR
597}
598
3b0e371c
JO
599static int check_type_val(struct parse_events_term *term,
600 struct parse_events_error *err,
601 int type)
602{
603 if (type == term->type_val)
604 return 0;
605
606 if (err) {
607 err->idx = term->err_val;
608 if (type == PARSE_EVENTS__TERM_TYPE_NUM)
609 err->str = strdup("expected numeric value");
610 else
611 err->str = strdup("expected string value");
612 }
613 return -EINVAL;
614}
615
0b8891a8
HK
616typedef int config_term_func_t(struct perf_event_attr *attr,
617 struct parse_events_term *term,
618 struct parse_events_error *err);
619
620static int config_term_common(struct perf_event_attr *attr,
621 struct parse_events_term *term,
622 struct parse_events_error *err)
8f707d84 623{
3b0e371c
JO
624#define CHECK_TYPE_VAL(type) \
625do { \
626 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
627 return -EINVAL; \
16fa7e82
JO
628} while (0)
629
630 switch (term->type_term) {
8f707d84 631 case PARSE_EVENTS__TERM_TYPE_CONFIG:
16fa7e82 632 CHECK_TYPE_VAL(NUM);
8f707d84
JO
633 attr->config = term->val.num;
634 break;
635 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
16fa7e82 636 CHECK_TYPE_VAL(NUM);
8f707d84
JO
637 attr->config1 = term->val.num;
638 break;
639 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
16fa7e82 640 CHECK_TYPE_VAL(NUM);
8f707d84
JO
641 attr->config2 = term->val.num;
642 break;
643 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
16fa7e82 644 CHECK_TYPE_VAL(NUM);
8f707d84 645 break;
09af2a55
NK
646 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
647 CHECK_TYPE_VAL(NUM);
648 break;
8f707d84
JO
649 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
650 /*
651 * TODO uncomment when the field is available
652 * attr->branch_sample_type = term->val.num;
653 */
654 break;
32067712
KL
655 case PARSE_EVENTS__TERM_TYPE_TIME:
656 CHECK_TYPE_VAL(NUM);
657 if (term->val.num > 1) {
658 err->str = strdup("expected 0 or 1");
659 err->idx = term->err_val;
660 return -EINVAL;
661 }
662 break;
d457c963
KL
663 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
664 CHECK_TYPE_VAL(STR);
665 break;
666 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
667 CHECK_TYPE_VAL(NUM);
668 break;
374ce938
WN
669 case PARSE_EVENTS__TERM_TYPE_INHERIT:
670 CHECK_TYPE_VAL(NUM);
671 break;
672 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
673 CHECK_TYPE_VAL(NUM);
674 break;
6b5fc39b
JO
675 case PARSE_EVENTS__TERM_TYPE_NAME:
676 CHECK_TYPE_VAL(STR);
677 break;
8f707d84 678 default:
ffeb883e
HK
679 err->str = strdup("unknown term");
680 err->idx = term->err_term;
681 err->help = parse_events_formats_error_string(NULL);
8f707d84
JO
682 return -EINVAL;
683 }
16fa7e82 684
8f707d84 685 return 0;
16fa7e82 686#undef CHECK_TYPE_VAL
8f707d84
JO
687}
688
0b8891a8
HK
689static int config_term_pmu(struct perf_event_attr *attr,
690 struct parse_events_term *term,
691 struct parse_events_error *err)
692{
693 if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER)
694 /*
695 * Always succeed for sysfs terms, as we dont know
696 * at this point what type they need to have.
697 */
698 return 0;
699 else
700 return config_term_common(attr, term, err);
701}
702
e637d177
HK
703static int config_term_tracepoint(struct perf_event_attr *attr,
704 struct parse_events_term *term,
705 struct parse_events_error *err)
706{
707 switch (term->type_term) {
708 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
709 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
374ce938
WN
710 case PARSE_EVENTS__TERM_TYPE_INHERIT:
711 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
e637d177
HK
712 return config_term_common(attr, term, err);
713 default:
714 if (err) {
715 err->idx = term->err_term;
716 err->str = strdup("unknown term");
717 err->help = strdup("valid terms: call-graph,stack-size\n");
718 }
719 return -EINVAL;
720 }
721
722 return 0;
723}
724
8f707d84 725static int config_attr(struct perf_event_attr *attr,
3b0e371c 726 struct list_head *head,
0b8891a8
HK
727 struct parse_events_error *err,
728 config_term_func_t config_term)
8f707d84 729{
6cee6cd3 730 struct parse_events_term *term;
8f707d84
JO
731
732 list_for_each_entry(term, head, list)
3b0e371c 733 if (config_term(attr, term, err))
8f707d84
JO
734 return -EINVAL;
735
736 return 0;
737}
738
930a2e29
JO
739static int get_config_terms(struct list_head *head_config,
740 struct list_head *head_terms __maybe_unused)
741{
742#define ADD_CONFIG_TERM(__type, __name, __val) \
743do { \
744 struct perf_evsel_config_term *__t; \
745 \
746 __t = zalloc(sizeof(*__t)); \
747 if (!__t) \
748 return -ENOMEM; \
749 \
750 INIT_LIST_HEAD(&__t->list); \
751 __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \
752 __t->val.__name = __val; \
753 list_add_tail(&__t->list, head_terms); \
754} while (0)
755
756 struct parse_events_term *term;
757
758 list_for_each_entry(term, head_config, list) {
759 switch (term->type_term) {
ee4c7588
JO
760 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
761 ADD_CONFIG_TERM(PERIOD, period, term->val.num);
32067712 762 break;
09af2a55
NK
763 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
764 ADD_CONFIG_TERM(FREQ, freq, term->val.num);
765 break;
32067712
KL
766 case PARSE_EVENTS__TERM_TYPE_TIME:
767 ADD_CONFIG_TERM(TIME, time, term->val.num);
768 break;
d457c963
KL
769 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
770 ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
771 break;
772 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
773 ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
774 break;
374ce938
WN
775 case PARSE_EVENTS__TERM_TYPE_INHERIT:
776 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0);
777 break;
778 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
779 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1);
780 break;
930a2e29
JO
781 default:
782 break;
783 }
784 }
785#undef ADD_EVSEL_CONFIG
786 return 0;
787}
788
e637d177
HK
789int parse_events_add_tracepoint(struct list_head *list, int *idx,
790 char *sys, char *event,
272ed29a 791 struct parse_events_error *err,
e637d177
HK
792 struct list_head *head_config)
793{
794 if (head_config) {
795 struct perf_event_attr attr;
796
272ed29a 797 if (config_attr(&attr, head_config, err,
e637d177
HK
798 config_term_tracepoint))
799 return -EINVAL;
800 }
801
802 if (strpbrk(sys, "*?"))
803 return add_tracepoint_multi_sys(list, idx, sys, event,
272ed29a 804 err, head_config);
e637d177
HK
805 else
806 return add_tracepoint_event(list, idx, sys, event,
272ed29a 807 err, head_config);
e637d177
HK
808}
809
87d650be
JO
810int parse_events_add_numeric(struct parse_events_evlist *data,
811 struct list_head *list,
b527bab5 812 u32 type, u64 config,
8f707d84 813 struct list_head *head_config)
8ad8db37 814{
89812fc8 815 struct perf_event_attr attr;
930a2e29 816 LIST_HEAD(config_terms);
61c45981 817
89812fc8
JO
818 memset(&attr, 0, sizeof(attr));
819 attr.type = type;
820 attr.config = config;
8f707d84 821
930a2e29 822 if (head_config) {
0b8891a8
HK
823 if (config_attr(&attr, head_config, data->error,
824 config_term_common))
930a2e29
JO
825 return -EINVAL;
826
827 if (get_config_terms(head_config, &config_terms))
828 return -ENOMEM;
829 }
8f707d84 830
930a2e29 831 return add_event(list, &data->idx, &attr, NULL, &config_terms);
61c45981 832}
8ad8db37 833
6cee6cd3 834static int parse_events__is_name_term(struct parse_events_term *term)
6b5fc39b
JO
835{
836 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
837}
838
9db1763c 839static char *pmu_event_name(struct list_head *head_terms)
6b5fc39b 840{
6cee6cd3 841 struct parse_events_term *term;
6b5fc39b
JO
842
843 list_for_each_entry(term, head_terms, list)
844 if (parse_events__is_name_term(term))
845 return term->val.str;
846
9db1763c 847 return NULL;
6b5fc39b
JO
848}
849
36adec85
JO
850int parse_events_add_pmu(struct parse_events_evlist *data,
851 struct list_head *list, char *name,
852 struct list_head *head_config)
5f537a26
JO
853{
854 struct perf_event_attr attr;
46441bdc 855 struct perf_pmu_info info;
5f537a26 856 struct perf_pmu *pmu;
410136f5 857 struct perf_evsel *evsel;
930a2e29 858 LIST_HEAD(config_terms);
5f537a26
JO
859
860 pmu = perf_pmu__find(name);
861 if (!pmu)
862 return -EINVAL;
863
dc0a6202
AH
864 if (pmu->default_config) {
865 memcpy(&attr, pmu->default_config,
866 sizeof(struct perf_event_attr));
867 } else {
868 memset(&attr, 0, sizeof(attr));
869 }
5f537a26 870
ad962273
AH
871 if (!head_config) {
872 attr.type = pmu->type;
930a2e29 873 evsel = __add_event(list, &data->idx, &attr, NULL, pmu->cpus, NULL);
ad962273
AH
874 return evsel ? 0 : -ENOMEM;
875 }
876
46441bdc 877 if (perf_pmu__check_alias(pmu, head_config, &info))
a6146d50
ZY
878 return -EINVAL;
879
5f537a26
JO
880 /*
881 * Configure hardcoded terms first, no need to check
882 * return value when called with fail == 0 ;)
883 */
0b8891a8 884 if (config_attr(&attr, head_config, data->error, config_term_pmu))
c056ba6a 885 return -EINVAL;
5f537a26 886
930a2e29
JO
887 if (get_config_terms(head_config, &config_terms))
888 return -ENOMEM;
889
e64b020b 890 if (perf_pmu__config(pmu, &attr, head_config, data->error))
5f537a26
JO
891 return -EINVAL;
892
36adec85 893 evsel = __add_event(list, &data->idx, &attr,
930a2e29
JO
894 pmu_event_name(head_config), pmu->cpus,
895 &config_terms);
410136f5 896 if (evsel) {
46441bdc
MF
897 evsel->unit = info.unit;
898 evsel->scale = info.scale;
044330c1 899 evsel->per_pkg = info.per_pkg;
1d9e446b 900 evsel->snapshot = info.snapshot;
410136f5
SE
901 }
902
903 return evsel ? 0 : -ENOMEM;
5f537a26
JO
904}
905
6a4bb04c
JO
906int parse_events__modifier_group(struct list_head *list,
907 char *event_mod)
89efb029 908{
6a4bb04c
JO
909 return parse_events__modifier_event(list, event_mod, true);
910}
911
63dab225 912void parse_events__set_leader(char *name, struct list_head *list)
6a4bb04c
JO
913{
914 struct perf_evsel *leader;
915
854f7363
WN
916 if (list_empty(list)) {
917 WARN_ONCE(true, "WARNING: failed to set leader: empty list");
918 return;
919 }
920
63dab225
ACM
921 __perf_evlist__set_leader(list);
922 leader = list_entry(list->next, struct perf_evsel, node);
6a4bb04c 923 leader->group_name = name ? strdup(name) : NULL;
89efb029
JO
924}
925
c5cd8ac0 926/* list_event is assumed to point to malloc'ed memory */
5d7be90e
JO
927void parse_events_update_lists(struct list_head *list_event,
928 struct list_head *list_all)
929{
930 /*
931 * Called for single event definition. Update the
89efb029 932 * 'all event' list, and reinit the 'single event'
5d7be90e
JO
933 * list, for next event definition.
934 */
935 list_splice_tail(list_event, list_all);
b847cbdc 936 free(list_event);
5d7be90e
JO
937}
938
f5b1135b
JO
939struct event_modifier {
940 int eu;
941 int ek;
942 int eh;
943 int eH;
944 int eG;
a1e12da4 945 int eI;
f5b1135b 946 int precise;
7f94af7a 947 int precise_max;
f5b1135b 948 int exclude_GH;
3c176311 949 int sample_read;
e9a7c414 950 int pinned;
f5b1135b
JO
951};
952
953static int get_event_modifier(struct event_modifier *mod, char *str,
954 struct perf_evsel *evsel)
61c45981 955{
f5b1135b
JO
956 int eu = evsel ? evsel->attr.exclude_user : 0;
957 int ek = evsel ? evsel->attr.exclude_kernel : 0;
958 int eh = evsel ? evsel->attr.exclude_hv : 0;
959 int eH = evsel ? evsel->attr.exclude_host : 0;
960 int eG = evsel ? evsel->attr.exclude_guest : 0;
a1e12da4 961 int eI = evsel ? evsel->attr.exclude_idle : 0;
f5b1135b 962 int precise = evsel ? evsel->attr.precise_ip : 0;
7f94af7a 963 int precise_max = 0;
3c176311 964 int sample_read = 0;
e9a7c414 965 int pinned = evsel ? evsel->attr.pinned : 0;
a21ca2ca 966
f5b1135b
JO
967 int exclude = eu | ek | eh;
968 int exclude_GH = evsel ? evsel->exclude_GH : 0;
969
f5b1135b 970 memset(mod, 0, sizeof(*mod));
ceb53fbf 971
61c45981 972 while (*str) {
ab608344
PZ
973 if (*str == 'u') {
974 if (!exclude)
975 exclude = eu = ek = eh = 1;
61c45981 976 eu = 0;
ab608344
PZ
977 } else if (*str == 'k') {
978 if (!exclude)
979 exclude = eu = ek = eh = 1;
61c45981 980 ek = 0;
ab608344
PZ
981 } else if (*str == 'h') {
982 if (!exclude)
983 exclude = eu = ek = eh = 1;
61c45981 984 eh = 0;
99320cc8
JR
985 } else if (*str == 'G') {
986 if (!exclude_GH)
987 exclude_GH = eG = eH = 1;
988 eG = 0;
989 } else if (*str == 'H') {
990 if (!exclude_GH)
991 exclude_GH = eG = eH = 1;
992 eH = 0;
a1e12da4
JO
993 } else if (*str == 'I') {
994 eI = 1;
ab608344
PZ
995 } else if (*str == 'p') {
996 precise++;
1342798c
DA
997 /* use of precise requires exclude_guest */
998 if (!exclude_GH)
999 eG = 1;
7f94af7a
JO
1000 } else if (*str == 'P') {
1001 precise_max = 1;
3c176311
JO
1002 } else if (*str == 'S') {
1003 sample_read = 1;
e9a7c414
ME
1004 } else if (*str == 'D') {
1005 pinned = 1;
ab608344 1006 } else
61c45981 1007 break;
ab608344 1008
61c45981 1009 ++str;
5242519b 1010 }
ceb53fbf 1011
89812fc8
JO
1012 /*
1013 * precise ip:
1014 *
1015 * 0 - SAMPLE_IP can have arbitrary skid
1016 * 1 - SAMPLE_IP must have constant skid
1017 * 2 - SAMPLE_IP requested to have 0 skid
1018 * 3 - SAMPLE_IP must have 0 skid
1019 *
1020 * See also PERF_RECORD_MISC_EXACT_IP
1021 */
1022 if (precise > 3)
1023 return -EINVAL;
ceb53fbf 1024
f5b1135b
JO
1025 mod->eu = eu;
1026 mod->ek = ek;
1027 mod->eh = eh;
1028 mod->eH = eH;
1029 mod->eG = eG;
a1e12da4 1030 mod->eI = eI;
f5b1135b 1031 mod->precise = precise;
7f94af7a 1032 mod->precise_max = precise_max;
f5b1135b 1033 mod->exclude_GH = exclude_GH;
3c176311 1034 mod->sample_read = sample_read;
e9a7c414
ME
1035 mod->pinned = pinned;
1036
f5b1135b
JO
1037 return 0;
1038}
1039
534123f4
JO
1040/*
1041 * Basic modifier sanity check to validate it contains only one
1042 * instance of any modifier (apart from 'p') present.
1043 */
1044static int check_modifier(char *str)
1045{
1046 char *p = str;
1047
1048 /* The sizeof includes 0 byte as well. */
7f94af7a 1049 if (strlen(str) > (sizeof("ukhGHpppPSDI") - 1))
534123f4
JO
1050 return -1;
1051
1052 while (*p) {
1053 if (*p != 'p' && strchr(p + 1, *p))
1054 return -1;
1055 p++;
1056 }
1057
1058 return 0;
1059}
1060
f5b1135b
JO
1061int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1062{
1063 struct perf_evsel *evsel;
1064 struct event_modifier mod;
1065
1066 if (str == NULL)
1067 return 0;
1068
534123f4
JO
1069 if (check_modifier(str))
1070 return -EINVAL;
1071
f5b1135b
JO
1072 if (!add && get_event_modifier(&mod, str, NULL))
1073 return -EINVAL;
1074
0050f7aa 1075 __evlist__for_each(list, evsel) {
f5b1135b
JO
1076 if (add && get_event_modifier(&mod, str, evsel))
1077 return -EINVAL;
1078
1079 evsel->attr.exclude_user = mod.eu;
1080 evsel->attr.exclude_kernel = mod.ek;
1081 evsel->attr.exclude_hv = mod.eh;
1082 evsel->attr.precise_ip = mod.precise;
1083 evsel->attr.exclude_host = mod.eH;
1084 evsel->attr.exclude_guest = mod.eG;
a1e12da4 1085 evsel->attr.exclude_idle = mod.eI;
f5b1135b 1086 evsel->exclude_GH = mod.exclude_GH;
3c176311 1087 evsel->sample_read = mod.sample_read;
7f94af7a 1088 evsel->precise_max = mod.precise_max;
e9a7c414
ME
1089
1090 if (perf_evsel__is_group_leader(evsel))
1091 evsel->attr.pinned = mod.pinned;
89812fc8 1092 }
ceb53fbf 1093
61c45981
PM
1094 return 0;
1095}
8ad8db37 1096
ac2ba9f3
RR
1097int parse_events_name(struct list_head *list, char *name)
1098{
1099 struct perf_evsel *evsel;
1100
0050f7aa 1101 __evlist__for_each(list, evsel) {
ac2ba9f3
RR
1102 if (!evsel->name)
1103 evsel->name = strdup(name);
1104 }
1105
1106 return 0;
1107}
1108
dcb4e102
KL
1109static int
1110comp_pmu(const void *p1, const void *p2)
1111{
1112 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
1113 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
1114
1115 return strcmp(pmu1->symbol, pmu2->symbol);
1116}
1117
1118static void perf_pmu__parse_cleanup(void)
1119{
1120 if (perf_pmu_events_list_num > 0) {
1121 struct perf_pmu_event_symbol *p;
1122 int i;
1123
1124 for (i = 0; i < perf_pmu_events_list_num; i++) {
1125 p = perf_pmu_events_list + i;
1126 free(p->symbol);
1127 }
1128 free(perf_pmu_events_list);
1129 perf_pmu_events_list = NULL;
1130 perf_pmu_events_list_num = 0;
1131 }
1132}
1133
1134#define SET_SYMBOL(str, stype) \
1135do { \
1136 p->symbol = str; \
1137 if (!p->symbol) \
1138 goto err; \
1139 p->type = stype; \
1140} while (0)
1141
1142/*
1143 * Read the pmu events list from sysfs
1144 * Save it into perf_pmu_events_list
1145 */
1146static void perf_pmu__parse_init(void)
1147{
1148
1149 struct perf_pmu *pmu = NULL;
1150 struct perf_pmu_alias *alias;
1151 int len = 0;
1152
1153 pmu = perf_pmu__find("cpu");
1154 if ((pmu == NULL) || list_empty(&pmu->aliases)) {
1155 perf_pmu_events_list_num = -1;
1156 return;
1157 }
1158 list_for_each_entry(alias, &pmu->aliases, list) {
1159 if (strchr(alias->name, '-'))
1160 len++;
1161 len++;
1162 }
1163 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1164 if (!perf_pmu_events_list)
1165 return;
1166 perf_pmu_events_list_num = len;
1167
1168 len = 0;
1169 list_for_each_entry(alias, &pmu->aliases, list) {
1170 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1171 char *tmp = strchr(alias->name, '-');
1172
1173 if (tmp != NULL) {
1174 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1175 PMU_EVENT_SYMBOL_PREFIX);
1176 p++;
1177 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1178 len += 2;
1179 } else {
1180 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1181 len++;
1182 }
1183 }
1184 qsort(perf_pmu_events_list, len,
1185 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1186
1187 return;
1188err:
1189 perf_pmu__parse_cleanup();
1190}
1191
1192enum perf_pmu_event_symbol_type
1193perf_pmu__parse_check(const char *name)
1194{
1195 struct perf_pmu_event_symbol p, *r;
1196
1197 /* scan kernel pmu events from sysfs if needed */
1198 if (perf_pmu_events_list_num == 0)
1199 perf_pmu__parse_init();
1200 /*
1201 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1202 * cpu-cycles has been handled by hardcode.
1203 * So it must be cpu// events, not kernel pmu event.
1204 */
1205 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1206 return PMU_EVENT_SYMBOL_ERR;
1207
1208 p.symbol = strdup(name);
1209 r = bsearch(&p, perf_pmu_events_list,
1210 (size_t) perf_pmu_events_list_num,
1211 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1212 free(p.symbol);
1213 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1214}
1215
90e2b22d 1216static int parse_events__scanner(const char *str, void *data, int start_token)
61c45981 1217{
89812fc8 1218 YY_BUFFER_STATE buffer;
ac20de6f 1219 void *scanner;
46010ab2 1220 int ret;
bcd3279f 1221
90e2b22d 1222 ret = parse_events_lex_init_extra(start_token, &scanner);
ac20de6f
ZY
1223 if (ret)
1224 return ret;
1225
1226 buffer = parse_events__scan_string(str, scanner);
a21ca2ca 1227
82ba1f2f
JO
1228#ifdef PARSER_DEBUG
1229 parse_events_debug = 1;
1230#endif
ac20de6f
ZY
1231 ret = parse_events_parse(data, scanner);
1232
1233 parse_events__flush_buffer(buffer, scanner);
1234 parse_events__delete_buffer(buffer, scanner);
1235 parse_events_lex_destroy(scanner);
1236 return ret;
1237}
bcd3279f 1238
90e2b22d
JO
1239/*
1240 * parse event config string, return a list of event terms.
1241 */
1242int parse_events_terms(struct list_head *terms, const char *str)
1243{
23b6339b 1244 struct parse_events_terms data = {
90e2b22d
JO
1245 .terms = NULL,
1246 };
1247 int ret;
1248
1249 ret = parse_events__scanner(str, &data, PE_START_TERMS);
1250 if (!ret) {
1251 list_splice(data.terms, terms);
74cf249d 1252 zfree(&data.terms);
90e2b22d
JO
1253 return 0;
1254 }
1255
b2c34fde
AH
1256 if (data.terms)
1257 parse_events__free_terms(data.terms);
90e2b22d
JO
1258 return ret;
1259}
1260
b39b8393
JO
1261int parse_events(struct perf_evlist *evlist, const char *str,
1262 struct parse_events_error *err)
ac20de6f 1263{
23b6339b 1264 struct parse_events_evlist data = {
b39b8393
JO
1265 .list = LIST_HEAD_INIT(data.list),
1266 .idx = evlist->nr_entries,
1267 .error = err,
ac20de6f
ZY
1268 };
1269 int ret;
bcd3279f 1270
90e2b22d 1271 ret = parse_events__scanner(str, &data, PE_START_EVENTS);
dcb4e102 1272 perf_pmu__parse_cleanup();
89812fc8 1273 if (!ret) {
15bfd2cc
WN
1274 struct perf_evsel *last;
1275
854f7363
WN
1276 if (list_empty(&data.list)) {
1277 WARN_ONCE(true, "WARNING: event parser found nothing");
1278 return -1;
1279 }
1280
f114d6ef 1281 perf_evlist__splice_list_tail(evlist, &data.list);
97f63e4a 1282 evlist->nr_groups += data.nr_groups;
15bfd2cc
WN
1283 last = perf_evlist__last(evlist);
1284 last->cmdline_group_boundary = true;
1285
89812fc8
JO
1286 return 0;
1287 }
bcd3279f 1288
5d7be90e
JO
1289 /*
1290 * There are 2 users - builtin-record and builtin-test objects.
1291 * Both call perf_evlist__delete in case of error, so we dont
1292 * need to bother.
1293 */
bcd3279f 1294 return ret;
8ad8db37
IM
1295}
1296
b39b8393
JO
1297#define MAX_WIDTH 1000
1298static int get_term_width(void)
1299{
1300 struct winsize ws;
1301
1302 get_term_dimensions(&ws);
1303 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1304}
1305
1306static void parse_events_print_error(struct parse_events_error *err,
1307 const char *event)
1308{
1309 const char *str = "invalid or unsupported event: ";
1310 char _buf[MAX_WIDTH];
1311 char *buf = (char *) event;
1312 int idx = 0;
1313
1314 if (err->str) {
1315 /* -2 for extra '' in the final fprintf */
1316 int width = get_term_width() - 2;
1317 int len_event = strlen(event);
1318 int len_str, max_len, cut = 0;
1319
1320 /*
1321 * Maximum error index indent, we will cut
1322 * the event string if it's bigger.
1323 */
141b2d31 1324 int max_err_idx = 13;
b39b8393
JO
1325
1326 /*
1327 * Let's be specific with the message when
1328 * we have the precise error.
1329 */
1330 str = "event syntax error: ";
1331 len_str = strlen(str);
1332 max_len = width - len_str;
1333
1334 buf = _buf;
1335
1336 /* We're cutting from the beggining. */
1337 if (err->idx > max_err_idx)
1338 cut = err->idx - max_err_idx;
1339
1340 strncpy(buf, event + cut, max_len);
1341
1342 /* Mark cut parts with '..' on both sides. */
1343 if (cut)
1344 buf[0] = buf[1] = '.';
1345
1346 if ((len_event - cut) > max_len) {
1347 buf[max_len - 1] = buf[max_len - 2] = '.';
1348 buf[max_len] = 0;
1349 }
1350
1351 idx = len_str + err->idx - cut;
1352 }
1353
1354 fprintf(stderr, "%s'%s'\n", str, buf);
1355 if (idx) {
1356 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
1357 if (err->help)
1358 fprintf(stderr, "\n%s\n", err->help);
1359 free(err->str);
1360 free(err->help);
1361 }
1362
1363 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
1364}
1365
1366#undef MAX_WIDTH
1367
f120f9d5 1368int parse_events_option(const struct option *opt, const char *str,
1d037ca1 1369 int unset __maybe_unused)
f120f9d5
JO
1370{
1371 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
b39b8393
JO
1372 struct parse_events_error err = { .idx = 0, };
1373 int ret = parse_events(evlist, str, &err);
1374
1375 if (ret)
1376 parse_events_print_error(&err, str);
9175ce1f 1377
9175ce1f 1378 return ret;
f120f9d5
JO
1379}
1380
4ba1faa1
WN
1381static int
1382foreach_evsel_in_last_glob(struct perf_evlist *evlist,
1383 int (*func)(struct perf_evsel *evsel,
1384 const void *arg),
1385 const void *arg)
c171b552 1386{
69aad6f1 1387 struct perf_evsel *last = NULL;
4ba1faa1 1388 int err;
c171b552 1389
854f7363
WN
1390 /*
1391 * Don't return when list_empty, give func a chance to report
1392 * error when it found last == NULL.
1393 *
1394 * So no need to WARN here, let *func do this.
1395 */
361c99a6 1396 if (evlist->nr_entries > 0)
0c21f736 1397 last = perf_evlist__last(evlist);
69aad6f1 1398
15bfd2cc 1399 do {
4ba1faa1
WN
1400 err = (*func)(last, arg);
1401 if (err)
15bfd2cc 1402 return -1;
4ba1faa1
WN
1403 if (!last)
1404 return 0;
15bfd2cc
WN
1405
1406 if (last->node.prev == &evlist->entries)
1407 return 0;
1408 last = list_entry(last->node.prev, struct perf_evsel, node);
1409 } while (!last->cmdline_group_boundary);
c171b552
LZ
1410
1411 return 0;
1412}
1413
4ba1faa1
WN
1414static int set_filter(struct perf_evsel *evsel, const void *arg)
1415{
1416 const char *str = arg;
1417
1418 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
1419 fprintf(stderr,
1420 "--filter option should follow a -e tracepoint option\n");
1421 return -1;
1422 }
1423
1424 if (perf_evsel__append_filter(evsel, "&&", str) < 0) {
1425 fprintf(stderr,
1426 "not enough memory to hold filter string\n");
1427 return -1;
1428 }
1429
1430 return 0;
1431}
1432
1433int parse_filter(const struct option *opt, const char *str,
1434 int unset __maybe_unused)
1435{
1436 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1437
1438 return foreach_evsel_in_last_glob(evlist, set_filter,
1439 (const void *)str);
1440}
1441
1442static int add_exclude_perf_filter(struct perf_evsel *evsel,
1443 const void *arg __maybe_unused)
1444{
1445 char new_filter[64];
1446
1447 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
1448 fprintf(stderr,
1449 "--exclude-perf option should follow a -e tracepoint option\n");
1450 return -1;
1451 }
1452
1453 snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
1454
1455 if (perf_evsel__append_filter(evsel, "&&", new_filter) < 0) {
1456 fprintf(stderr,
1457 "not enough memory to hold filter string\n");
1458 return -1;
1459 }
1460
1461 return 0;
1462}
1463
1464int exclude_perf(const struct option *opt,
1465 const char *arg __maybe_unused,
1466 int unset __maybe_unused)
1467{
1468 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1469
1470 return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
1471 NULL);
1472}
1473
86847b62 1474static const char * const event_type_descriptors[] = {
86847b62
TG
1475 "Hardware event",
1476 "Software event",
1477 "Tracepoint event",
1478 "Hardware cache event",
41bdcb23
LW
1479 "Raw hardware event descriptor",
1480 "Hardware breakpoint",
86847b62
TG
1481};
1482
ab0e4800
YS
1483static int cmp_string(const void *a, const void *b)
1484{
1485 const char * const *as = a;
1486 const char * const *bs = b;
1487
1488 return strcmp(*as, *bs);
1489}
1490
f6bdafef
JB
1491/*
1492 * Print the events from <debugfs_mount_point>/tracing/events
1493 */
1494
a3277d2d
FW
1495void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
1496 bool name_only)
f6bdafef
JB
1497{
1498 DIR *sys_dir, *evt_dir;
1499 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
f6bdafef 1500 char evt_path[MAXPATHLEN];
725b1368 1501 char dir_path[MAXPATHLEN];
ab0e4800
YS
1502 char **evt_list = NULL;
1503 unsigned int evt_i = 0, evt_num = 0;
1504 bool evt_num_known = false;
f6bdafef 1505
ab0e4800 1506restart:
ebf294bf 1507 sys_dir = opendir(tracing_events_path);
f6bdafef 1508 if (!sys_dir)
725b1368 1509 return;
6b58e7f1 1510
ab0e4800
YS
1511 if (evt_num_known) {
1512 evt_list = zalloc(sizeof(char *) * evt_num);
1513 if (!evt_list)
1514 goto out_close_sys_dir;
1515 }
1516
6b58e7f1 1517 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
48000a1a 1518 if (subsys_glob != NULL &&
668b8788
ACM
1519 !strglobmatch(sys_dirent.d_name, subsys_glob))
1520 continue;
725b1368 1521
ebf294bf 1522 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
725b1368
ED
1523 sys_dirent.d_name);
1524 evt_dir = opendir(dir_path);
1525 if (!evt_dir)
6b58e7f1 1526 continue;
725b1368 1527
6b58e7f1 1528 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
48000a1a 1529 if (event_glob != NULL &&
668b8788
ACM
1530 !strglobmatch(evt_dirent.d_name, event_glob))
1531 continue;
1532
ab0e4800
YS
1533 if (!evt_num_known) {
1534 evt_num++;
a3277d2d
FW
1535 continue;
1536 }
1537
f6bdafef
JB
1538 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1539 sys_dirent.d_name, evt_dirent.d_name);
ab0e4800
YS
1540
1541 evt_list[evt_i] = strdup(evt_path);
1542 if (evt_list[evt_i] == NULL)
1543 goto out_close_evt_dir;
1544 evt_i++;
f6bdafef
JB
1545 }
1546 closedir(evt_dir);
1547 }
f6bdafef 1548 closedir(sys_dir);
ab0e4800
YS
1549
1550 if (!evt_num_known) {
1551 evt_num_known = true;
1552 goto restart;
1553 }
1554 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1555 evt_i = 0;
1556 while (evt_i < evt_num) {
1557 if (name_only) {
1558 printf("%s ", evt_list[evt_i++]);
1559 continue;
1560 }
1561 printf(" %-50s [%s]\n", evt_list[evt_i++],
1562 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1563 }
dfc431cb 1564 if (evt_num && pager_in_use())
ab0e4800
YS
1565 printf("\n");
1566
1567out_free:
1568 evt_num = evt_i;
1569 for (evt_i = 0; evt_i < evt_num; evt_i++)
1570 zfree(&evt_list[evt_i]);
1571 zfree(&evt_list);
1572 return;
1573
1574out_close_evt_dir:
1575 closedir(evt_dir);
1576out_close_sys_dir:
1577 closedir(sys_dir);
1578
1579 printf("FATAL: not enough memory to print %s\n",
1580 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1581 if (evt_list)
1582 goto out_free;
f6bdafef
JB
1583}
1584
20c457b8
TR
1585/*
1586 * Check whether event is in <debugfs_mount_point>/tracing/events
1587 */
1588
1589int is_valid_tracepoint(const char *event_string)
1590{
1591 DIR *sys_dir, *evt_dir;
1592 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
1593 char evt_path[MAXPATHLEN];
1594 char dir_path[MAXPATHLEN];
1595
ebf294bf 1596 sys_dir = opendir(tracing_events_path);
20c457b8
TR
1597 if (!sys_dir)
1598 return 0;
1599
1600 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1601
ebf294bf 1602 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
20c457b8
TR
1603 sys_dirent.d_name);
1604 evt_dir = opendir(dir_path);
1605 if (!evt_dir)
1606 continue;
1607
1608 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1609 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1610 sys_dirent.d_name, evt_dirent.d_name);
1611 if (!strcmp(evt_path, event_string)) {
1612 closedir(evt_dir);
1613 closedir(sys_dir);
1614 return 1;
1615 }
1616 }
1617 closedir(evt_dir);
1618 }
1619 closedir(sys_dir);
1620 return 0;
1621}
1622
b41f1cec
NK
1623static bool is_event_supported(u8 type, unsigned config)
1624{
1625 bool ret = true;
88fee52e 1626 int open_return;
b41f1cec
NK
1627 struct perf_evsel *evsel;
1628 struct perf_event_attr attr = {
1629 .type = type,
1630 .config = config,
1631 .disabled = 1,
b41f1cec
NK
1632 };
1633 struct {
1634 struct thread_map map;
1635 int threads[1];
1636 } tmap = {
1637 .map.nr = 1,
1638 .threads = { 0 },
1639 };
1640
ef503831 1641 evsel = perf_evsel__new(&attr);
b41f1cec 1642 if (evsel) {
88fee52e
VW
1643 open_return = perf_evsel__open(evsel, NULL, &tmap.map);
1644 ret = open_return >= 0;
1645
1646 if (open_return == -EACCES) {
1647 /*
1648 * This happens if the paranoid value
1649 * /proc/sys/kernel/perf_event_paranoid is set to 2
1650 * Re-run with exclude_kernel set; we don't do that
1651 * by default as some ARM machines do not support it.
1652 *
1653 */
1654 evsel->attr.exclude_kernel = 1;
1655 ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0;
1656 }
b41f1cec
NK
1657 perf_evsel__delete(evsel);
1658 }
1659
1660 return ret;
1661}
1662
a3277d2d 1663int print_hwcache_events(const char *event_glob, bool name_only)
668b8788 1664{
ab0e4800 1665 unsigned int type, op, i, evt_i = 0, evt_num = 0;
0b668bc9 1666 char name[64];
ab0e4800
YS
1667 char **evt_list = NULL;
1668 bool evt_num_known = false;
1669
1670restart:
1671 if (evt_num_known) {
1672 evt_list = zalloc(sizeof(char *) * evt_num);
1673 if (!evt_list)
1674 goto out_enomem;
1675 }
668b8788
ACM
1676
1677 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1678 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1679 /* skip invalid cache type */
0b668bc9 1680 if (!perf_evsel__is_cache_op_valid(type, op))
668b8788
ACM
1681 continue;
1682
1683 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
0b668bc9
ACM
1684 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
1685 name, sizeof(name));
947b4ad1 1686 if (event_glob != NULL && !strglobmatch(name, event_glob))
668b8788
ACM
1687 continue;
1688
b41f1cec
NK
1689 if (!is_event_supported(PERF_TYPE_HW_CACHE,
1690 type | (op << 8) | (i << 16)))
1691 continue;
1692
ab0e4800
YS
1693 if (!evt_num_known) {
1694 evt_num++;
1695 continue;
1696 }
1697
1698 evt_list[evt_i] = strdup(name);
1699 if (evt_list[evt_i] == NULL)
1700 goto out_enomem;
1701 evt_i++;
668b8788
ACM
1702 }
1703 }
1704 }
1705
ab0e4800
YS
1706 if (!evt_num_known) {
1707 evt_num_known = true;
1708 goto restart;
1709 }
1710 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1711 evt_i = 0;
1712 while (evt_i < evt_num) {
1713 if (name_only) {
1714 printf("%s ", evt_list[evt_i++]);
1715 continue;
1716 }
1717 printf(" %-50s [%s]\n", evt_list[evt_i++],
1718 event_type_descriptors[PERF_TYPE_HW_CACHE]);
1719 }
dfc431cb 1720 if (evt_num && pager_in_use())
dc098b35 1721 printf("\n");
ab0e4800
YS
1722
1723out_free:
1724 evt_num = evt_i;
1725 for (evt_i = 0; evt_i < evt_num; evt_i++)
1726 zfree(&evt_list[evt_i]);
1727 zfree(&evt_list);
1728 return evt_num;
1729
1730out_enomem:
1731 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
1732 if (evt_list)
1733 goto out_free;
1734 return evt_num;
668b8788
ACM
1735}
1736
705750f2 1737void print_symbol_events(const char *event_glob, unsigned type,
a3277d2d
FW
1738 struct event_symbol *syms, unsigned max,
1739 bool name_only)
8ad8db37 1740{
ab0e4800 1741 unsigned int i, evt_i = 0, evt_num = 0;
947b4ad1 1742 char name[MAX_NAME_LEN];
ab0e4800
YS
1743 char **evt_list = NULL;
1744 bool evt_num_known = false;
1745
1746restart:
1747 if (evt_num_known) {
1748 evt_list = zalloc(sizeof(char *) * evt_num);
1749 if (!evt_list)
1750 goto out_enomem;
1751 syms -= max;
1752 }
8ad8db37 1753
1dc12760 1754 for (i = 0; i < max; i++, syms++) {
668b8788 1755
48000a1a 1756 if (event_glob != NULL &&
668b8788
ACM
1757 !(strglobmatch(syms->symbol, event_glob) ||
1758 (syms->alias && strglobmatch(syms->alias, event_glob))))
1759 continue;
8ad8db37 1760
b41f1cec
NK
1761 if (!is_event_supported(type, i))
1762 continue;
1763
ab0e4800
YS
1764 if (!evt_num_known) {
1765 evt_num++;
a3277d2d
FW
1766 continue;
1767 }
1768
ab0e4800 1769 if (!name_only && strlen(syms->alias))
947b4ad1 1770 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
74d5b588 1771 else
947b4ad1 1772 strncpy(name, syms->symbol, MAX_NAME_LEN);
8ad8db37 1773
ab0e4800
YS
1774 evt_list[evt_i] = strdup(name);
1775 if (evt_list[evt_i] == NULL)
1776 goto out_enomem;
1777 evt_i++;
8ad8db37
IM
1778 }
1779
ab0e4800
YS
1780 if (!evt_num_known) {
1781 evt_num_known = true;
1782 goto restart;
1783 }
1784 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1785 evt_i = 0;
1786 while (evt_i < evt_num) {
1787 if (name_only) {
1788 printf("%s ", evt_list[evt_i++]);
1789 continue;
1790 }
1791 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
1792 }
dfc431cb 1793 if (evt_num && pager_in_use())
668b8788 1794 printf("\n");
ab0e4800
YS
1795
1796out_free:
1797 evt_num = evt_i;
1798 for (evt_i = 0; evt_i < evt_num; evt_i++)
1799 zfree(&evt_list[evt_i]);
1800 zfree(&evt_list);
1801 return;
1802
1803out_enomem:
1804 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
1805 if (evt_list)
1806 goto out_free;
1dc12760
JO
1807}
1808
1809/*
1810 * Print the help text for the event symbols:
1811 */
a3277d2d 1812void print_events(const char *event_glob, bool name_only)
1dc12760 1813{
1dc12760 1814 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
a3277d2d 1815 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
1dc12760
JO
1816
1817 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
a3277d2d 1818 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
1dc12760 1819
a3277d2d 1820 print_hwcache_events(event_glob, name_only);
668b8788 1821
dc098b35
AK
1822 print_pmu_events(event_glob, name_only);
1823
668b8788
ACM
1824 if (event_glob != NULL)
1825 return;
73c24cb8 1826
a3277d2d 1827 if (!name_only) {
a3277d2d
FW
1828 printf(" %-50s [%s]\n",
1829 "rNNN",
1830 event_type_descriptors[PERF_TYPE_RAW]);
1831 printf(" %-50s [%s]\n",
1832 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
1833 event_type_descriptors[PERF_TYPE_RAW]);
dfc431cb
ACM
1834 if (pager_in_use())
1835 printf(" (see 'man perf-list' on how to encode it)\n\n");
a3277d2d
FW
1836
1837 printf(" %-50s [%s]\n",
3741eb9f 1838 "mem:<addr>[/len][:access]",
41bdcb23 1839 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
dfc431cb
ACM
1840 if (pager_in_use())
1841 printf("\n");
a3277d2d 1842 }
1b290d67 1843
a3277d2d 1844 print_tracepoint_events(NULL, NULL, name_only);
8ad8db37 1845}
8f707d84 1846
6cee6cd3 1847int parse_events__is_hardcoded_term(struct parse_events_term *term)
8f707d84 1848{
16fa7e82 1849 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
8f707d84
JO
1850}
1851
6cee6cd3 1852static int new_term(struct parse_events_term **_term, int type_val,
16fa7e82 1853 int type_term, char *config,
cecf3a2e 1854 char *str, u64 num, int err_term, int err_val)
8f707d84 1855{
6cee6cd3 1856 struct parse_events_term *term;
8f707d84
JO
1857
1858 term = zalloc(sizeof(*term));
1859 if (!term)
1860 return -ENOMEM;
1861
1862 INIT_LIST_HEAD(&term->list);
16fa7e82
JO
1863 term->type_val = type_val;
1864 term->type_term = type_term;
8f707d84 1865 term->config = config;
cecf3a2e
JO
1866 term->err_term = err_term;
1867 term->err_val = err_val;
8f707d84 1868
16fa7e82 1869 switch (type_val) {
8f707d84
JO
1870 case PARSE_EVENTS__TERM_TYPE_NUM:
1871 term->val.num = num;
1872 break;
1873 case PARSE_EVENTS__TERM_TYPE_STR:
1874 term->val.str = str;
1875 break;
1876 default:
4be8be6b 1877 free(term);
8f707d84
JO
1878 return -EINVAL;
1879 }
1880
1881 *_term = term;
1882 return 0;
1883}
1884
6cee6cd3 1885int parse_events_term__num(struct parse_events_term **term,
cecf3a2e 1886 int type_term, char *config, u64 num,
bb78ce7d 1887 void *loc_term_, void *loc_val_)
16fa7e82 1888{
bb78ce7d
AH
1889 YYLTYPE *loc_term = loc_term_;
1890 YYLTYPE *loc_val = loc_val_;
1891
16fa7e82 1892 return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
cecf3a2e
JO
1893 config, NULL, num,
1894 loc_term ? loc_term->first_column : 0,
1895 loc_val ? loc_val->first_column : 0);
16fa7e82
JO
1896}
1897
6cee6cd3 1898int parse_events_term__str(struct parse_events_term **term,
cecf3a2e 1899 int type_term, char *config, char *str,
bb78ce7d 1900 void *loc_term_, void *loc_val_)
16fa7e82 1901{
bb78ce7d
AH
1902 YYLTYPE *loc_term = loc_term_;
1903 YYLTYPE *loc_val = loc_val_;
1904
16fa7e82 1905 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
cecf3a2e
JO
1906 config, str, 0,
1907 loc_term ? loc_term->first_column : 0,
1908 loc_val ? loc_val->first_column : 0);
16fa7e82
JO
1909}
1910
6cee6cd3 1911int parse_events_term__sym_hw(struct parse_events_term **term,
1d33d6dc
JO
1912 char *config, unsigned idx)
1913{
1914 struct event_symbol *sym;
1915
1916 BUG_ON(idx >= PERF_COUNT_HW_MAX);
1917 sym = &event_symbols_hw[idx];
1918
1919 if (config)
1920 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1921 PARSE_EVENTS__TERM_TYPE_USER, config,
cecf3a2e 1922 (char *) sym->symbol, 0, 0, 0);
1d33d6dc
JO
1923 else
1924 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1925 PARSE_EVENTS__TERM_TYPE_USER,
cecf3a2e
JO
1926 (char *) "event", (char *) sym->symbol,
1927 0, 0, 0);
1d33d6dc
JO
1928}
1929
6cee6cd3
ACM
1930int parse_events_term__clone(struct parse_events_term **new,
1931 struct parse_events_term *term)
a6146d50
ZY
1932{
1933 return new_term(new, term->type_val, term->type_term, term->config,
cecf3a2e
JO
1934 term->val.str, term->val.num,
1935 term->err_term, term->err_val);
a6146d50
ZY
1936}
1937
8f707d84
JO
1938void parse_events__free_terms(struct list_head *terms)
1939{
6cee6cd3 1940 struct parse_events_term *term, *h;
8f707d84
JO
1941
1942 list_for_each_entry_safe(term, h, terms, list)
1943 free(term);
8f707d84 1944}
b39b8393
JO
1945
1946void parse_events_evlist_error(struct parse_events_evlist *data,
1947 int idx, const char *str)
1948{
1949 struct parse_events_error *err = data->error;
1950
a6ced2be
AH
1951 if (!err)
1952 return;
b39b8393
JO
1953 err->idx = idx;
1954 err->str = strdup(str);
1955 WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
1956}
ffeb883e
HK
1957
1958/*
1959 * Return string contains valid config terms of an event.
1960 * @additional_terms: For terms such as PMU sysfs terms.
1961 */
1962char *parse_events_formats_error_string(char *additional_terms)
1963{
1964 char *str;
1965 static const char *static_terms = "config,config1,config2,name,"
1966 "period,freq,branch_type,time,"
1967 "call-graph,stack-size\n";
1968
1969 /* valid terms */
1970 if (additional_terms) {
1971 if (!asprintf(&str, "valid terms: %s,%s",
1972 additional_terms, static_terms))
1973 goto fail;
1974 } else {
1975 if (!asprintf(&str, "valid terms: %s", static_terms))
1976 goto fail;
1977 }
1978 return str;
1979
1980fail:
1981 return NULL;
1982}