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