perf symbols: we can now read separate debug-info files based on a build ID
[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;
6b5fc39b
JO
669 case PARSE_EVENTS__TERM_TYPE_NAME:
670 CHECK_TYPE_VAL(STR);
671 break;
8f707d84 672 default:
ffeb883e
HK
673 err->str = strdup("unknown term");
674 err->idx = term->err_term;
675 err->help = parse_events_formats_error_string(NULL);
8f707d84
JO
676 return -EINVAL;
677 }
16fa7e82 678
8f707d84 679 return 0;
16fa7e82 680#undef CHECK_TYPE_VAL
8f707d84
JO
681}
682
0b8891a8
HK
683static int config_term_pmu(struct perf_event_attr *attr,
684 struct parse_events_term *term,
685 struct parse_events_error *err)
686{
687 if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER)
688 /*
689 * Always succeed for sysfs terms, as we dont know
690 * at this point what type they need to have.
691 */
692 return 0;
693 else
694 return config_term_common(attr, term, err);
695}
696
e637d177
HK
697static int config_term_tracepoint(struct perf_event_attr *attr,
698 struct parse_events_term *term,
699 struct parse_events_error *err)
700{
701 switch (term->type_term) {
702 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
703 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
704 return config_term_common(attr, term, err);
705 default:
706 if (err) {
707 err->idx = term->err_term;
708 err->str = strdup("unknown term");
709 err->help = strdup("valid terms: call-graph,stack-size\n");
710 }
711 return -EINVAL;
712 }
713
714 return 0;
715}
716
8f707d84 717static int config_attr(struct perf_event_attr *attr,
3b0e371c 718 struct list_head *head,
0b8891a8
HK
719 struct parse_events_error *err,
720 config_term_func_t config_term)
8f707d84 721{
6cee6cd3 722 struct parse_events_term *term;
8f707d84
JO
723
724 list_for_each_entry(term, head, list)
3b0e371c 725 if (config_term(attr, term, err))
8f707d84
JO
726 return -EINVAL;
727
728 return 0;
729}
730
930a2e29
JO
731static int get_config_terms(struct list_head *head_config,
732 struct list_head *head_terms __maybe_unused)
733{
734#define ADD_CONFIG_TERM(__type, __name, __val) \
735do { \
736 struct perf_evsel_config_term *__t; \
737 \
738 __t = zalloc(sizeof(*__t)); \
739 if (!__t) \
740 return -ENOMEM; \
741 \
742 INIT_LIST_HEAD(&__t->list); \
743 __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \
744 __t->val.__name = __val; \
745 list_add_tail(&__t->list, head_terms); \
746} while (0)
747
748 struct parse_events_term *term;
749
750 list_for_each_entry(term, head_config, list) {
751 switch (term->type_term) {
ee4c7588
JO
752 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
753 ADD_CONFIG_TERM(PERIOD, period, term->val.num);
32067712 754 break;
09af2a55
NK
755 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
756 ADD_CONFIG_TERM(FREQ, freq, term->val.num);
757 break;
32067712
KL
758 case PARSE_EVENTS__TERM_TYPE_TIME:
759 ADD_CONFIG_TERM(TIME, time, term->val.num);
760 break;
d457c963
KL
761 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
762 ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
763 break;
764 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
765 ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
766 break;
930a2e29
JO
767 default:
768 break;
769 }
770 }
771#undef ADD_EVSEL_CONFIG
772 return 0;
773}
774
e637d177
HK
775int parse_events_add_tracepoint(struct list_head *list, int *idx,
776 char *sys, char *event,
272ed29a 777 struct parse_events_error *err,
e637d177
HK
778 struct list_head *head_config)
779{
780 if (head_config) {
781 struct perf_event_attr attr;
782
272ed29a 783 if (config_attr(&attr, head_config, err,
e637d177
HK
784 config_term_tracepoint))
785 return -EINVAL;
786 }
787
788 if (strpbrk(sys, "*?"))
789 return add_tracepoint_multi_sys(list, idx, sys, event,
272ed29a 790 err, head_config);
e637d177
HK
791 else
792 return add_tracepoint_event(list, idx, sys, event,
272ed29a 793 err, head_config);
e637d177
HK
794}
795
87d650be
JO
796int parse_events_add_numeric(struct parse_events_evlist *data,
797 struct list_head *list,
b527bab5 798 u32 type, u64 config,
8f707d84 799 struct list_head *head_config)
8ad8db37 800{
89812fc8 801 struct perf_event_attr attr;
930a2e29 802 LIST_HEAD(config_terms);
61c45981 803
89812fc8
JO
804 memset(&attr, 0, sizeof(attr));
805 attr.type = type;
806 attr.config = config;
8f707d84 807
930a2e29 808 if (head_config) {
0b8891a8
HK
809 if (config_attr(&attr, head_config, data->error,
810 config_term_common))
930a2e29
JO
811 return -EINVAL;
812
813 if (get_config_terms(head_config, &config_terms))
814 return -ENOMEM;
815 }
8f707d84 816
930a2e29 817 return add_event(list, &data->idx, &attr, NULL, &config_terms);
61c45981 818}
8ad8db37 819
6cee6cd3 820static int parse_events__is_name_term(struct parse_events_term *term)
6b5fc39b
JO
821{
822 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
823}
824
9db1763c 825static char *pmu_event_name(struct list_head *head_terms)
6b5fc39b 826{
6cee6cd3 827 struct parse_events_term *term;
6b5fc39b
JO
828
829 list_for_each_entry(term, head_terms, list)
830 if (parse_events__is_name_term(term))
831 return term->val.str;
832
9db1763c 833 return NULL;
6b5fc39b
JO
834}
835
36adec85
JO
836int parse_events_add_pmu(struct parse_events_evlist *data,
837 struct list_head *list, char *name,
838 struct list_head *head_config)
5f537a26
JO
839{
840 struct perf_event_attr attr;
46441bdc 841 struct perf_pmu_info info;
5f537a26 842 struct perf_pmu *pmu;
410136f5 843 struct perf_evsel *evsel;
930a2e29 844 LIST_HEAD(config_terms);
5f537a26
JO
845
846 pmu = perf_pmu__find(name);
847 if (!pmu)
848 return -EINVAL;
849
dc0a6202
AH
850 if (pmu->default_config) {
851 memcpy(&attr, pmu->default_config,
852 sizeof(struct perf_event_attr));
853 } else {
854 memset(&attr, 0, sizeof(attr));
855 }
5f537a26 856
ad962273
AH
857 if (!head_config) {
858 attr.type = pmu->type;
930a2e29 859 evsel = __add_event(list, &data->idx, &attr, NULL, pmu->cpus, NULL);
ad962273
AH
860 return evsel ? 0 : -ENOMEM;
861 }
862
46441bdc 863 if (perf_pmu__check_alias(pmu, head_config, &info))
a6146d50
ZY
864 return -EINVAL;
865
5f537a26
JO
866 /*
867 * Configure hardcoded terms first, no need to check
868 * return value when called with fail == 0 ;)
869 */
0b8891a8 870 if (config_attr(&attr, head_config, data->error, config_term_pmu))
c056ba6a 871 return -EINVAL;
5f537a26 872
930a2e29
JO
873 if (get_config_terms(head_config, &config_terms))
874 return -ENOMEM;
875
e64b020b 876 if (perf_pmu__config(pmu, &attr, head_config, data->error))
5f537a26
JO
877 return -EINVAL;
878
36adec85 879 evsel = __add_event(list, &data->idx, &attr,
930a2e29
JO
880 pmu_event_name(head_config), pmu->cpus,
881 &config_terms);
410136f5 882 if (evsel) {
46441bdc
MF
883 evsel->unit = info.unit;
884 evsel->scale = info.scale;
044330c1 885 evsel->per_pkg = info.per_pkg;
1d9e446b 886 evsel->snapshot = info.snapshot;
410136f5
SE
887 }
888
889 return evsel ? 0 : -ENOMEM;
5f537a26
JO
890}
891
6a4bb04c
JO
892int parse_events__modifier_group(struct list_head *list,
893 char *event_mod)
89efb029 894{
6a4bb04c
JO
895 return parse_events__modifier_event(list, event_mod, true);
896}
897
63dab225 898void parse_events__set_leader(char *name, struct list_head *list)
6a4bb04c
JO
899{
900 struct perf_evsel *leader;
901
854f7363
WN
902 if (list_empty(list)) {
903 WARN_ONCE(true, "WARNING: failed to set leader: empty list");
904 return;
905 }
906
63dab225
ACM
907 __perf_evlist__set_leader(list);
908 leader = list_entry(list->next, struct perf_evsel, node);
6a4bb04c 909 leader->group_name = name ? strdup(name) : NULL;
89efb029
JO
910}
911
c5cd8ac0 912/* list_event is assumed to point to malloc'ed memory */
5d7be90e
JO
913void parse_events_update_lists(struct list_head *list_event,
914 struct list_head *list_all)
915{
916 /*
917 * Called for single event definition. Update the
89efb029 918 * 'all event' list, and reinit the 'single event'
5d7be90e
JO
919 * list, for next event definition.
920 */
921 list_splice_tail(list_event, list_all);
b847cbdc 922 free(list_event);
5d7be90e
JO
923}
924
f5b1135b
JO
925struct event_modifier {
926 int eu;
927 int ek;
928 int eh;
929 int eH;
930 int eG;
a1e12da4 931 int eI;
f5b1135b 932 int precise;
7f94af7a 933 int precise_max;
f5b1135b 934 int exclude_GH;
3c176311 935 int sample_read;
e9a7c414 936 int pinned;
f5b1135b
JO
937};
938
939static int get_event_modifier(struct event_modifier *mod, char *str,
940 struct perf_evsel *evsel)
61c45981 941{
f5b1135b
JO
942 int eu = evsel ? evsel->attr.exclude_user : 0;
943 int ek = evsel ? evsel->attr.exclude_kernel : 0;
944 int eh = evsel ? evsel->attr.exclude_hv : 0;
945 int eH = evsel ? evsel->attr.exclude_host : 0;
946 int eG = evsel ? evsel->attr.exclude_guest : 0;
a1e12da4 947 int eI = evsel ? evsel->attr.exclude_idle : 0;
f5b1135b 948 int precise = evsel ? evsel->attr.precise_ip : 0;
7f94af7a 949 int precise_max = 0;
3c176311 950 int sample_read = 0;
e9a7c414 951 int pinned = evsel ? evsel->attr.pinned : 0;
a21ca2ca 952
f5b1135b
JO
953 int exclude = eu | ek | eh;
954 int exclude_GH = evsel ? evsel->exclude_GH : 0;
955
f5b1135b 956 memset(mod, 0, sizeof(*mod));
ceb53fbf 957
61c45981 958 while (*str) {
ab608344
PZ
959 if (*str == 'u') {
960 if (!exclude)
961 exclude = eu = ek = eh = 1;
61c45981 962 eu = 0;
ab608344
PZ
963 } else if (*str == 'k') {
964 if (!exclude)
965 exclude = eu = ek = eh = 1;
61c45981 966 ek = 0;
ab608344
PZ
967 } else if (*str == 'h') {
968 if (!exclude)
969 exclude = eu = ek = eh = 1;
61c45981 970 eh = 0;
99320cc8
JR
971 } else if (*str == 'G') {
972 if (!exclude_GH)
973 exclude_GH = eG = eH = 1;
974 eG = 0;
975 } else if (*str == 'H') {
976 if (!exclude_GH)
977 exclude_GH = eG = eH = 1;
978 eH = 0;
a1e12da4
JO
979 } else if (*str == 'I') {
980 eI = 1;
ab608344
PZ
981 } else if (*str == 'p') {
982 precise++;
1342798c
DA
983 /* use of precise requires exclude_guest */
984 if (!exclude_GH)
985 eG = 1;
7f94af7a
JO
986 } else if (*str == 'P') {
987 precise_max = 1;
3c176311
JO
988 } else if (*str == 'S') {
989 sample_read = 1;
e9a7c414
ME
990 } else if (*str == 'D') {
991 pinned = 1;
ab608344 992 } else
61c45981 993 break;
ab608344 994
61c45981 995 ++str;
5242519b 996 }
ceb53fbf 997
89812fc8
JO
998 /*
999 * precise ip:
1000 *
1001 * 0 - SAMPLE_IP can have arbitrary skid
1002 * 1 - SAMPLE_IP must have constant skid
1003 * 2 - SAMPLE_IP requested to have 0 skid
1004 * 3 - SAMPLE_IP must have 0 skid
1005 *
1006 * See also PERF_RECORD_MISC_EXACT_IP
1007 */
1008 if (precise > 3)
1009 return -EINVAL;
ceb53fbf 1010
f5b1135b
JO
1011 mod->eu = eu;
1012 mod->ek = ek;
1013 mod->eh = eh;
1014 mod->eH = eH;
1015 mod->eG = eG;
a1e12da4 1016 mod->eI = eI;
f5b1135b 1017 mod->precise = precise;
7f94af7a 1018 mod->precise_max = precise_max;
f5b1135b 1019 mod->exclude_GH = exclude_GH;
3c176311 1020 mod->sample_read = sample_read;
e9a7c414
ME
1021 mod->pinned = pinned;
1022
f5b1135b
JO
1023 return 0;
1024}
1025
534123f4
JO
1026/*
1027 * Basic modifier sanity check to validate it contains only one
1028 * instance of any modifier (apart from 'p') present.
1029 */
1030static int check_modifier(char *str)
1031{
1032 char *p = str;
1033
1034 /* The sizeof includes 0 byte as well. */
7f94af7a 1035 if (strlen(str) > (sizeof("ukhGHpppPSDI") - 1))
534123f4
JO
1036 return -1;
1037
1038 while (*p) {
1039 if (*p != 'p' && strchr(p + 1, *p))
1040 return -1;
1041 p++;
1042 }
1043
1044 return 0;
1045}
1046
f5b1135b
JO
1047int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1048{
1049 struct perf_evsel *evsel;
1050 struct event_modifier mod;
1051
1052 if (str == NULL)
1053 return 0;
1054
534123f4
JO
1055 if (check_modifier(str))
1056 return -EINVAL;
1057
f5b1135b
JO
1058 if (!add && get_event_modifier(&mod, str, NULL))
1059 return -EINVAL;
1060
0050f7aa 1061 __evlist__for_each(list, evsel) {
f5b1135b
JO
1062 if (add && get_event_modifier(&mod, str, evsel))
1063 return -EINVAL;
1064
1065 evsel->attr.exclude_user = mod.eu;
1066 evsel->attr.exclude_kernel = mod.ek;
1067 evsel->attr.exclude_hv = mod.eh;
1068 evsel->attr.precise_ip = mod.precise;
1069 evsel->attr.exclude_host = mod.eH;
1070 evsel->attr.exclude_guest = mod.eG;
a1e12da4 1071 evsel->attr.exclude_idle = mod.eI;
f5b1135b 1072 evsel->exclude_GH = mod.exclude_GH;
3c176311 1073 evsel->sample_read = mod.sample_read;
7f94af7a 1074 evsel->precise_max = mod.precise_max;
e9a7c414
ME
1075
1076 if (perf_evsel__is_group_leader(evsel))
1077 evsel->attr.pinned = mod.pinned;
89812fc8 1078 }
ceb53fbf 1079
61c45981
PM
1080 return 0;
1081}
8ad8db37 1082
ac2ba9f3
RR
1083int parse_events_name(struct list_head *list, char *name)
1084{
1085 struct perf_evsel *evsel;
1086
0050f7aa 1087 __evlist__for_each(list, evsel) {
ac2ba9f3
RR
1088 if (!evsel->name)
1089 evsel->name = strdup(name);
1090 }
1091
1092 return 0;
1093}
1094
dcb4e102
KL
1095static int
1096comp_pmu(const void *p1, const void *p2)
1097{
1098 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
1099 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
1100
1101 return strcmp(pmu1->symbol, pmu2->symbol);
1102}
1103
1104static void perf_pmu__parse_cleanup(void)
1105{
1106 if (perf_pmu_events_list_num > 0) {
1107 struct perf_pmu_event_symbol *p;
1108 int i;
1109
1110 for (i = 0; i < perf_pmu_events_list_num; i++) {
1111 p = perf_pmu_events_list + i;
1112 free(p->symbol);
1113 }
1114 free(perf_pmu_events_list);
1115 perf_pmu_events_list = NULL;
1116 perf_pmu_events_list_num = 0;
1117 }
1118}
1119
1120#define SET_SYMBOL(str, stype) \
1121do { \
1122 p->symbol = str; \
1123 if (!p->symbol) \
1124 goto err; \
1125 p->type = stype; \
1126} while (0)
1127
1128/*
1129 * Read the pmu events list from sysfs
1130 * Save it into perf_pmu_events_list
1131 */
1132static void perf_pmu__parse_init(void)
1133{
1134
1135 struct perf_pmu *pmu = NULL;
1136 struct perf_pmu_alias *alias;
1137 int len = 0;
1138
1139 pmu = perf_pmu__find("cpu");
1140 if ((pmu == NULL) || list_empty(&pmu->aliases)) {
1141 perf_pmu_events_list_num = -1;
1142 return;
1143 }
1144 list_for_each_entry(alias, &pmu->aliases, list) {
1145 if (strchr(alias->name, '-'))
1146 len++;
1147 len++;
1148 }
1149 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1150 if (!perf_pmu_events_list)
1151 return;
1152 perf_pmu_events_list_num = len;
1153
1154 len = 0;
1155 list_for_each_entry(alias, &pmu->aliases, list) {
1156 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1157 char *tmp = strchr(alias->name, '-');
1158
1159 if (tmp != NULL) {
1160 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1161 PMU_EVENT_SYMBOL_PREFIX);
1162 p++;
1163 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1164 len += 2;
1165 } else {
1166 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1167 len++;
1168 }
1169 }
1170 qsort(perf_pmu_events_list, len,
1171 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1172
1173 return;
1174err:
1175 perf_pmu__parse_cleanup();
1176}
1177
1178enum perf_pmu_event_symbol_type
1179perf_pmu__parse_check(const char *name)
1180{
1181 struct perf_pmu_event_symbol p, *r;
1182
1183 /* scan kernel pmu events from sysfs if needed */
1184 if (perf_pmu_events_list_num == 0)
1185 perf_pmu__parse_init();
1186 /*
1187 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1188 * cpu-cycles has been handled by hardcode.
1189 * So it must be cpu// events, not kernel pmu event.
1190 */
1191 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1192 return PMU_EVENT_SYMBOL_ERR;
1193
1194 p.symbol = strdup(name);
1195 r = bsearch(&p, perf_pmu_events_list,
1196 (size_t) perf_pmu_events_list_num,
1197 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1198 free(p.symbol);
1199 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1200}
1201
90e2b22d 1202static int parse_events__scanner(const char *str, void *data, int start_token)
61c45981 1203{
89812fc8 1204 YY_BUFFER_STATE buffer;
ac20de6f 1205 void *scanner;
46010ab2 1206 int ret;
bcd3279f 1207
90e2b22d 1208 ret = parse_events_lex_init_extra(start_token, &scanner);
ac20de6f
ZY
1209 if (ret)
1210 return ret;
1211
1212 buffer = parse_events__scan_string(str, scanner);
a21ca2ca 1213
82ba1f2f
JO
1214#ifdef PARSER_DEBUG
1215 parse_events_debug = 1;
1216#endif
ac20de6f
ZY
1217 ret = parse_events_parse(data, scanner);
1218
1219 parse_events__flush_buffer(buffer, scanner);
1220 parse_events__delete_buffer(buffer, scanner);
1221 parse_events_lex_destroy(scanner);
1222 return ret;
1223}
bcd3279f 1224
90e2b22d
JO
1225/*
1226 * parse event config string, return a list of event terms.
1227 */
1228int parse_events_terms(struct list_head *terms, const char *str)
1229{
23b6339b 1230 struct parse_events_terms data = {
90e2b22d
JO
1231 .terms = NULL,
1232 };
1233 int ret;
1234
1235 ret = parse_events__scanner(str, &data, PE_START_TERMS);
1236 if (!ret) {
1237 list_splice(data.terms, terms);
74cf249d 1238 zfree(&data.terms);
90e2b22d
JO
1239 return 0;
1240 }
1241
b2c34fde
AH
1242 if (data.terms)
1243 parse_events__free_terms(data.terms);
90e2b22d
JO
1244 return ret;
1245}
1246
b39b8393
JO
1247int parse_events(struct perf_evlist *evlist, const char *str,
1248 struct parse_events_error *err)
ac20de6f 1249{
23b6339b 1250 struct parse_events_evlist data = {
b39b8393
JO
1251 .list = LIST_HEAD_INIT(data.list),
1252 .idx = evlist->nr_entries,
1253 .error = err,
ac20de6f
ZY
1254 };
1255 int ret;
bcd3279f 1256
90e2b22d 1257 ret = parse_events__scanner(str, &data, PE_START_EVENTS);
dcb4e102 1258 perf_pmu__parse_cleanup();
89812fc8 1259 if (!ret) {
15bfd2cc
WN
1260 struct perf_evsel *last;
1261
854f7363
WN
1262 if (list_empty(&data.list)) {
1263 WARN_ONCE(true, "WARNING: event parser found nothing");
1264 return -1;
1265 }
1266
f114d6ef 1267 perf_evlist__splice_list_tail(evlist, &data.list);
97f63e4a 1268 evlist->nr_groups += data.nr_groups;
15bfd2cc
WN
1269 last = perf_evlist__last(evlist);
1270 last->cmdline_group_boundary = true;
1271
89812fc8
JO
1272 return 0;
1273 }
bcd3279f 1274
5d7be90e
JO
1275 /*
1276 * There are 2 users - builtin-record and builtin-test objects.
1277 * Both call perf_evlist__delete in case of error, so we dont
1278 * need to bother.
1279 */
bcd3279f 1280 return ret;
8ad8db37
IM
1281}
1282
b39b8393
JO
1283#define MAX_WIDTH 1000
1284static int get_term_width(void)
1285{
1286 struct winsize ws;
1287
1288 get_term_dimensions(&ws);
1289 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1290}
1291
1292static void parse_events_print_error(struct parse_events_error *err,
1293 const char *event)
1294{
1295 const char *str = "invalid or unsupported event: ";
1296 char _buf[MAX_WIDTH];
1297 char *buf = (char *) event;
1298 int idx = 0;
1299
1300 if (err->str) {
1301 /* -2 for extra '' in the final fprintf */
1302 int width = get_term_width() - 2;
1303 int len_event = strlen(event);
1304 int len_str, max_len, cut = 0;
1305
1306 /*
1307 * Maximum error index indent, we will cut
1308 * the event string if it's bigger.
1309 */
141b2d31 1310 int max_err_idx = 13;
b39b8393
JO
1311
1312 /*
1313 * Let's be specific with the message when
1314 * we have the precise error.
1315 */
1316 str = "event syntax error: ";
1317 len_str = strlen(str);
1318 max_len = width - len_str;
1319
1320 buf = _buf;
1321
1322 /* We're cutting from the beggining. */
1323 if (err->idx > max_err_idx)
1324 cut = err->idx - max_err_idx;
1325
1326 strncpy(buf, event + cut, max_len);
1327
1328 /* Mark cut parts with '..' on both sides. */
1329 if (cut)
1330 buf[0] = buf[1] = '.';
1331
1332 if ((len_event - cut) > max_len) {
1333 buf[max_len - 1] = buf[max_len - 2] = '.';
1334 buf[max_len] = 0;
1335 }
1336
1337 idx = len_str + err->idx - cut;
1338 }
1339
1340 fprintf(stderr, "%s'%s'\n", str, buf);
1341 if (idx) {
1342 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
1343 if (err->help)
1344 fprintf(stderr, "\n%s\n", err->help);
1345 free(err->str);
1346 free(err->help);
1347 }
1348
1349 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
1350}
1351
1352#undef MAX_WIDTH
1353
f120f9d5 1354int parse_events_option(const struct option *opt, const char *str,
1d037ca1 1355 int unset __maybe_unused)
f120f9d5
JO
1356{
1357 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
b39b8393
JO
1358 struct parse_events_error err = { .idx = 0, };
1359 int ret = parse_events(evlist, str, &err);
1360
1361 if (ret)
1362 parse_events_print_error(&err, str);
9175ce1f 1363
9175ce1f 1364 return ret;
f120f9d5
JO
1365}
1366
4ba1faa1
WN
1367static int
1368foreach_evsel_in_last_glob(struct perf_evlist *evlist,
1369 int (*func)(struct perf_evsel *evsel,
1370 const void *arg),
1371 const void *arg)
c171b552 1372{
69aad6f1 1373 struct perf_evsel *last = NULL;
4ba1faa1 1374 int err;
c171b552 1375
854f7363
WN
1376 /*
1377 * Don't return when list_empty, give func a chance to report
1378 * error when it found last == NULL.
1379 *
1380 * So no need to WARN here, let *func do this.
1381 */
361c99a6 1382 if (evlist->nr_entries > 0)
0c21f736 1383 last = perf_evlist__last(evlist);
69aad6f1 1384
15bfd2cc 1385 do {
4ba1faa1
WN
1386 err = (*func)(last, arg);
1387 if (err)
15bfd2cc 1388 return -1;
4ba1faa1
WN
1389 if (!last)
1390 return 0;
15bfd2cc
WN
1391
1392 if (last->node.prev == &evlist->entries)
1393 return 0;
1394 last = list_entry(last->node.prev, struct perf_evsel, node);
1395 } while (!last->cmdline_group_boundary);
c171b552
LZ
1396
1397 return 0;
1398}
1399
4ba1faa1
WN
1400static int set_filter(struct perf_evsel *evsel, const void *arg)
1401{
1402 const char *str = arg;
1403
1404 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
1405 fprintf(stderr,
1406 "--filter option should follow a -e tracepoint option\n");
1407 return -1;
1408 }
1409
1410 if (perf_evsel__append_filter(evsel, "&&", str) < 0) {
1411 fprintf(stderr,
1412 "not enough memory to hold filter string\n");
1413 return -1;
1414 }
1415
1416 return 0;
1417}
1418
1419int parse_filter(const struct option *opt, const char *str,
1420 int unset __maybe_unused)
1421{
1422 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1423
1424 return foreach_evsel_in_last_glob(evlist, set_filter,
1425 (const void *)str);
1426}
1427
1428static int add_exclude_perf_filter(struct perf_evsel *evsel,
1429 const void *arg __maybe_unused)
1430{
1431 char new_filter[64];
1432
1433 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
1434 fprintf(stderr,
1435 "--exclude-perf option should follow a -e tracepoint option\n");
1436 return -1;
1437 }
1438
1439 snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
1440
1441 if (perf_evsel__append_filter(evsel, "&&", new_filter) < 0) {
1442 fprintf(stderr,
1443 "not enough memory to hold filter string\n");
1444 return -1;
1445 }
1446
1447 return 0;
1448}
1449
1450int exclude_perf(const struct option *opt,
1451 const char *arg __maybe_unused,
1452 int unset __maybe_unused)
1453{
1454 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1455
1456 return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
1457 NULL);
1458}
1459
86847b62 1460static const char * const event_type_descriptors[] = {
86847b62
TG
1461 "Hardware event",
1462 "Software event",
1463 "Tracepoint event",
1464 "Hardware cache event",
41bdcb23
LW
1465 "Raw hardware event descriptor",
1466 "Hardware breakpoint",
86847b62
TG
1467};
1468
ab0e4800
YS
1469static int cmp_string(const void *a, const void *b)
1470{
1471 const char * const *as = a;
1472 const char * const *bs = b;
1473
1474 return strcmp(*as, *bs);
1475}
1476
f6bdafef
JB
1477/*
1478 * Print the events from <debugfs_mount_point>/tracing/events
1479 */
1480
a3277d2d
FW
1481void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
1482 bool name_only)
f6bdafef
JB
1483{
1484 DIR *sys_dir, *evt_dir;
1485 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
f6bdafef 1486 char evt_path[MAXPATHLEN];
725b1368 1487 char dir_path[MAXPATHLEN];
ab0e4800
YS
1488 char **evt_list = NULL;
1489 unsigned int evt_i = 0, evt_num = 0;
1490 bool evt_num_known = false;
f6bdafef 1491
ab0e4800 1492restart:
ebf294bf 1493 sys_dir = opendir(tracing_events_path);
f6bdafef 1494 if (!sys_dir)
725b1368 1495 return;
6b58e7f1 1496
ab0e4800
YS
1497 if (evt_num_known) {
1498 evt_list = zalloc(sizeof(char *) * evt_num);
1499 if (!evt_list)
1500 goto out_close_sys_dir;
1501 }
1502
6b58e7f1 1503 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
48000a1a 1504 if (subsys_glob != NULL &&
668b8788
ACM
1505 !strglobmatch(sys_dirent.d_name, subsys_glob))
1506 continue;
725b1368 1507
ebf294bf 1508 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
725b1368
ED
1509 sys_dirent.d_name);
1510 evt_dir = opendir(dir_path);
1511 if (!evt_dir)
6b58e7f1 1512 continue;
725b1368 1513
6b58e7f1 1514 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
48000a1a 1515 if (event_glob != NULL &&
668b8788
ACM
1516 !strglobmatch(evt_dirent.d_name, event_glob))
1517 continue;
1518
ab0e4800
YS
1519 if (!evt_num_known) {
1520 evt_num++;
a3277d2d
FW
1521 continue;
1522 }
1523
f6bdafef
JB
1524 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1525 sys_dirent.d_name, evt_dirent.d_name);
ab0e4800
YS
1526
1527 evt_list[evt_i] = strdup(evt_path);
1528 if (evt_list[evt_i] == NULL)
1529 goto out_close_evt_dir;
1530 evt_i++;
f6bdafef
JB
1531 }
1532 closedir(evt_dir);
1533 }
f6bdafef 1534 closedir(sys_dir);
ab0e4800
YS
1535
1536 if (!evt_num_known) {
1537 evt_num_known = true;
1538 goto restart;
1539 }
1540 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1541 evt_i = 0;
1542 while (evt_i < evt_num) {
1543 if (name_only) {
1544 printf("%s ", evt_list[evt_i++]);
1545 continue;
1546 }
1547 printf(" %-50s [%s]\n", evt_list[evt_i++],
1548 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1549 }
dfc431cb 1550 if (evt_num && pager_in_use())
ab0e4800
YS
1551 printf("\n");
1552
1553out_free:
1554 evt_num = evt_i;
1555 for (evt_i = 0; evt_i < evt_num; evt_i++)
1556 zfree(&evt_list[evt_i]);
1557 zfree(&evt_list);
1558 return;
1559
1560out_close_evt_dir:
1561 closedir(evt_dir);
1562out_close_sys_dir:
1563 closedir(sys_dir);
1564
1565 printf("FATAL: not enough memory to print %s\n",
1566 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1567 if (evt_list)
1568 goto out_free;
f6bdafef
JB
1569}
1570
20c457b8
TR
1571/*
1572 * Check whether event is in <debugfs_mount_point>/tracing/events
1573 */
1574
1575int is_valid_tracepoint(const char *event_string)
1576{
1577 DIR *sys_dir, *evt_dir;
1578 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
1579 char evt_path[MAXPATHLEN];
1580 char dir_path[MAXPATHLEN];
1581
ebf294bf 1582 sys_dir = opendir(tracing_events_path);
20c457b8
TR
1583 if (!sys_dir)
1584 return 0;
1585
1586 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1587
ebf294bf 1588 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
20c457b8
TR
1589 sys_dirent.d_name);
1590 evt_dir = opendir(dir_path);
1591 if (!evt_dir)
1592 continue;
1593
1594 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1595 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1596 sys_dirent.d_name, evt_dirent.d_name);
1597 if (!strcmp(evt_path, event_string)) {
1598 closedir(evt_dir);
1599 closedir(sys_dir);
1600 return 1;
1601 }
1602 }
1603 closedir(evt_dir);
1604 }
1605 closedir(sys_dir);
1606 return 0;
1607}
1608
b41f1cec
NK
1609static bool is_event_supported(u8 type, unsigned config)
1610{
1611 bool ret = true;
88fee52e 1612 int open_return;
b41f1cec
NK
1613 struct perf_evsel *evsel;
1614 struct perf_event_attr attr = {
1615 .type = type,
1616 .config = config,
1617 .disabled = 1,
b41f1cec
NK
1618 };
1619 struct {
1620 struct thread_map map;
1621 int threads[1];
1622 } tmap = {
1623 .map.nr = 1,
1624 .threads = { 0 },
1625 };
1626
ef503831 1627 evsel = perf_evsel__new(&attr);
b41f1cec 1628 if (evsel) {
88fee52e
VW
1629 open_return = perf_evsel__open(evsel, NULL, &tmap.map);
1630 ret = open_return >= 0;
1631
1632 if (open_return == -EACCES) {
1633 /*
1634 * This happens if the paranoid value
1635 * /proc/sys/kernel/perf_event_paranoid is set to 2
1636 * Re-run with exclude_kernel set; we don't do that
1637 * by default as some ARM machines do not support it.
1638 *
1639 */
1640 evsel->attr.exclude_kernel = 1;
1641 ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0;
1642 }
b41f1cec
NK
1643 perf_evsel__delete(evsel);
1644 }
1645
1646 return ret;
1647}
1648
a3277d2d 1649int print_hwcache_events(const char *event_glob, bool name_only)
668b8788 1650{
ab0e4800 1651 unsigned int type, op, i, evt_i = 0, evt_num = 0;
0b668bc9 1652 char name[64];
ab0e4800
YS
1653 char **evt_list = NULL;
1654 bool evt_num_known = false;
1655
1656restart:
1657 if (evt_num_known) {
1658 evt_list = zalloc(sizeof(char *) * evt_num);
1659 if (!evt_list)
1660 goto out_enomem;
1661 }
668b8788
ACM
1662
1663 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1664 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1665 /* skip invalid cache type */
0b668bc9 1666 if (!perf_evsel__is_cache_op_valid(type, op))
668b8788
ACM
1667 continue;
1668
1669 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
0b668bc9
ACM
1670 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
1671 name, sizeof(name));
947b4ad1 1672 if (event_glob != NULL && !strglobmatch(name, event_glob))
668b8788
ACM
1673 continue;
1674
b41f1cec
NK
1675 if (!is_event_supported(PERF_TYPE_HW_CACHE,
1676 type | (op << 8) | (i << 16)))
1677 continue;
1678
ab0e4800
YS
1679 if (!evt_num_known) {
1680 evt_num++;
1681 continue;
1682 }
1683
1684 evt_list[evt_i] = strdup(name);
1685 if (evt_list[evt_i] == NULL)
1686 goto out_enomem;
1687 evt_i++;
668b8788
ACM
1688 }
1689 }
1690 }
1691
ab0e4800
YS
1692 if (!evt_num_known) {
1693 evt_num_known = true;
1694 goto restart;
1695 }
1696 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1697 evt_i = 0;
1698 while (evt_i < evt_num) {
1699 if (name_only) {
1700 printf("%s ", evt_list[evt_i++]);
1701 continue;
1702 }
1703 printf(" %-50s [%s]\n", evt_list[evt_i++],
1704 event_type_descriptors[PERF_TYPE_HW_CACHE]);
1705 }
dfc431cb 1706 if (evt_num && pager_in_use())
dc098b35 1707 printf("\n");
ab0e4800
YS
1708
1709out_free:
1710 evt_num = evt_i;
1711 for (evt_i = 0; evt_i < evt_num; evt_i++)
1712 zfree(&evt_list[evt_i]);
1713 zfree(&evt_list);
1714 return evt_num;
1715
1716out_enomem:
1717 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
1718 if (evt_list)
1719 goto out_free;
1720 return evt_num;
668b8788
ACM
1721}
1722
705750f2 1723void print_symbol_events(const char *event_glob, unsigned type,
a3277d2d
FW
1724 struct event_symbol *syms, unsigned max,
1725 bool name_only)
8ad8db37 1726{
ab0e4800 1727 unsigned int i, evt_i = 0, evt_num = 0;
947b4ad1 1728 char name[MAX_NAME_LEN];
ab0e4800
YS
1729 char **evt_list = NULL;
1730 bool evt_num_known = false;
1731
1732restart:
1733 if (evt_num_known) {
1734 evt_list = zalloc(sizeof(char *) * evt_num);
1735 if (!evt_list)
1736 goto out_enomem;
1737 syms -= max;
1738 }
8ad8db37 1739
1dc12760 1740 for (i = 0; i < max; i++, syms++) {
668b8788 1741
48000a1a 1742 if (event_glob != NULL &&
668b8788
ACM
1743 !(strglobmatch(syms->symbol, event_glob) ||
1744 (syms->alias && strglobmatch(syms->alias, event_glob))))
1745 continue;
8ad8db37 1746
b41f1cec
NK
1747 if (!is_event_supported(type, i))
1748 continue;
1749
ab0e4800
YS
1750 if (!evt_num_known) {
1751 evt_num++;
a3277d2d
FW
1752 continue;
1753 }
1754
ab0e4800 1755 if (!name_only && strlen(syms->alias))
947b4ad1 1756 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
74d5b588 1757 else
947b4ad1 1758 strncpy(name, syms->symbol, MAX_NAME_LEN);
8ad8db37 1759
ab0e4800
YS
1760 evt_list[evt_i] = strdup(name);
1761 if (evt_list[evt_i] == NULL)
1762 goto out_enomem;
1763 evt_i++;
8ad8db37
IM
1764 }
1765
ab0e4800
YS
1766 if (!evt_num_known) {
1767 evt_num_known = true;
1768 goto restart;
1769 }
1770 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1771 evt_i = 0;
1772 while (evt_i < evt_num) {
1773 if (name_only) {
1774 printf("%s ", evt_list[evt_i++]);
1775 continue;
1776 }
1777 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
1778 }
dfc431cb 1779 if (evt_num && pager_in_use())
668b8788 1780 printf("\n");
ab0e4800
YS
1781
1782out_free:
1783 evt_num = evt_i;
1784 for (evt_i = 0; evt_i < evt_num; evt_i++)
1785 zfree(&evt_list[evt_i]);
1786 zfree(&evt_list);
1787 return;
1788
1789out_enomem:
1790 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
1791 if (evt_list)
1792 goto out_free;
1dc12760
JO
1793}
1794
1795/*
1796 * Print the help text for the event symbols:
1797 */
a3277d2d 1798void print_events(const char *event_glob, bool name_only)
1dc12760 1799{
1dc12760 1800 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
a3277d2d 1801 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
1dc12760
JO
1802
1803 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
a3277d2d 1804 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
1dc12760 1805
a3277d2d 1806 print_hwcache_events(event_glob, name_only);
668b8788 1807
dc098b35
AK
1808 print_pmu_events(event_glob, name_only);
1809
668b8788
ACM
1810 if (event_glob != NULL)
1811 return;
73c24cb8 1812
a3277d2d 1813 if (!name_only) {
a3277d2d
FW
1814 printf(" %-50s [%s]\n",
1815 "rNNN",
1816 event_type_descriptors[PERF_TYPE_RAW]);
1817 printf(" %-50s [%s]\n",
1818 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
1819 event_type_descriptors[PERF_TYPE_RAW]);
dfc431cb
ACM
1820 if (pager_in_use())
1821 printf(" (see 'man perf-list' on how to encode it)\n\n");
a3277d2d
FW
1822
1823 printf(" %-50s [%s]\n",
3741eb9f 1824 "mem:<addr>[/len][:access]",
41bdcb23 1825 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
dfc431cb
ACM
1826 if (pager_in_use())
1827 printf("\n");
a3277d2d 1828 }
1b290d67 1829
a3277d2d 1830 print_tracepoint_events(NULL, NULL, name_only);
8ad8db37 1831}
8f707d84 1832
6cee6cd3 1833int parse_events__is_hardcoded_term(struct parse_events_term *term)
8f707d84 1834{
16fa7e82 1835 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
8f707d84
JO
1836}
1837
6cee6cd3 1838static int new_term(struct parse_events_term **_term, int type_val,
16fa7e82 1839 int type_term, char *config,
cecf3a2e 1840 char *str, u64 num, int err_term, int err_val)
8f707d84 1841{
6cee6cd3 1842 struct parse_events_term *term;
8f707d84
JO
1843
1844 term = zalloc(sizeof(*term));
1845 if (!term)
1846 return -ENOMEM;
1847
1848 INIT_LIST_HEAD(&term->list);
16fa7e82
JO
1849 term->type_val = type_val;
1850 term->type_term = type_term;
8f707d84 1851 term->config = config;
cecf3a2e
JO
1852 term->err_term = err_term;
1853 term->err_val = err_val;
8f707d84 1854
16fa7e82 1855 switch (type_val) {
8f707d84
JO
1856 case PARSE_EVENTS__TERM_TYPE_NUM:
1857 term->val.num = num;
1858 break;
1859 case PARSE_EVENTS__TERM_TYPE_STR:
1860 term->val.str = str;
1861 break;
1862 default:
4be8be6b 1863 free(term);
8f707d84
JO
1864 return -EINVAL;
1865 }
1866
1867 *_term = term;
1868 return 0;
1869}
1870
6cee6cd3 1871int parse_events_term__num(struct parse_events_term **term,
cecf3a2e 1872 int type_term, char *config, u64 num,
bb78ce7d 1873 void *loc_term_, void *loc_val_)
16fa7e82 1874{
bb78ce7d
AH
1875 YYLTYPE *loc_term = loc_term_;
1876 YYLTYPE *loc_val = loc_val_;
1877
16fa7e82 1878 return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
cecf3a2e
JO
1879 config, NULL, num,
1880 loc_term ? loc_term->first_column : 0,
1881 loc_val ? loc_val->first_column : 0);
16fa7e82
JO
1882}
1883
6cee6cd3 1884int parse_events_term__str(struct parse_events_term **term,
cecf3a2e 1885 int type_term, char *config, char *str,
bb78ce7d 1886 void *loc_term_, void *loc_val_)
16fa7e82 1887{
bb78ce7d
AH
1888 YYLTYPE *loc_term = loc_term_;
1889 YYLTYPE *loc_val = loc_val_;
1890
16fa7e82 1891 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
cecf3a2e
JO
1892 config, str, 0,
1893 loc_term ? loc_term->first_column : 0,
1894 loc_val ? loc_val->first_column : 0);
16fa7e82
JO
1895}
1896
6cee6cd3 1897int parse_events_term__sym_hw(struct parse_events_term **term,
1d33d6dc
JO
1898 char *config, unsigned idx)
1899{
1900 struct event_symbol *sym;
1901
1902 BUG_ON(idx >= PERF_COUNT_HW_MAX);
1903 sym = &event_symbols_hw[idx];
1904
1905 if (config)
1906 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1907 PARSE_EVENTS__TERM_TYPE_USER, config,
cecf3a2e 1908 (char *) sym->symbol, 0, 0, 0);
1d33d6dc
JO
1909 else
1910 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1911 PARSE_EVENTS__TERM_TYPE_USER,
cecf3a2e
JO
1912 (char *) "event", (char *) sym->symbol,
1913 0, 0, 0);
1d33d6dc
JO
1914}
1915
6cee6cd3
ACM
1916int parse_events_term__clone(struct parse_events_term **new,
1917 struct parse_events_term *term)
a6146d50
ZY
1918{
1919 return new_term(new, term->type_val, term->type_term, term->config,
cecf3a2e
JO
1920 term->val.str, term->val.num,
1921 term->err_term, term->err_val);
a6146d50
ZY
1922}
1923
8f707d84
JO
1924void parse_events__free_terms(struct list_head *terms)
1925{
6cee6cd3 1926 struct parse_events_term *term, *h;
8f707d84
JO
1927
1928 list_for_each_entry_safe(term, h, terms, list)
1929 free(term);
8f707d84 1930}
b39b8393
JO
1931
1932void parse_events_evlist_error(struct parse_events_evlist *data,
1933 int idx, const char *str)
1934{
1935 struct parse_events_error *err = data->error;
1936
a6ced2be
AH
1937 if (!err)
1938 return;
b39b8393
JO
1939 err->idx = idx;
1940 err->str = strdup(str);
1941 WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
1942}
ffeb883e
HK
1943
1944/*
1945 * Return string contains valid config terms of an event.
1946 * @additional_terms: For terms such as PMU sysfs terms.
1947 */
1948char *parse_events_formats_error_string(char *additional_terms)
1949{
1950 char *str;
1951 static const char *static_terms = "config,config1,config2,name,"
1952 "period,freq,branch_type,time,"
1953 "call-graph,stack-size\n";
1954
1955 /* valid terms */
1956 if (additional_terms) {
1957 if (!asprintf(&str, "valid terms: %s,%s",
1958 additional_terms, static_terms))
1959 goto fail;
1960 } else {
1961 if (!asprintf(&str, "valid terms: %s", static_terms))
1962 goto fail;
1963 }
1964 return str;
1965
1966fail:
1967 return NULL;
1968}