License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[linux-2.6-block.git] / tools / perf / util / parse-events.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
d2709c7c 2#include <linux/hw_breakpoint.h>
8dd2a131 3#include <linux/err.h>
76b31a29 4#include <dirent.h>
a43783ae 5#include <errno.h>
86a5e0c2 6#include <sys/ioctl.h>
391e4206 7#include <sys/param.h>
b0742e90 8#include "term.h"
6b58e7f1 9#include "../perf.h"
361c99a6 10#include "evlist.h"
69aad6f1 11#include "evsel.h"
4b6ab94e 12#include <subcmd/parse-options.h>
8ad8db37 13#include "parse-events.h"
4b6ab94e 14#include <subcmd/exec-cmd.h>
a067558e 15#include "string2.h"
8ec20b17 16#include "strlist.h"
5aab621b 17#include "symbol.h"
5beeded1 18#include "cache.h"
8755a8f2 19#include "header.h"
84c86ca1 20#include "bpf-loader.h"
6e81c74c 21#include "debug.h"
592d5a6b 22#include <api/fs/tracing_path.h>
ac20de6f 23#include "parse-events-bison.h"
90e2b22d 24#define YY_EXTRA_TYPE int
89812fc8 25#include "parse-events-flex.h"
5f537a26 26#include "pmu.h"
b41f1cec 27#include "thread_map.h"
f30a79b0 28#include "cpumap.h"
40218dae 29#include "probe-file.h"
b39b8393 30#include "asm/bug.h"
ac12f676 31#include "util/parse-branch-options.h"
89812fc8
JO
32
33#define MAX_NAME_LEN 100
8ad8db37 34
82ba1f2f
JO
35#ifdef PARSER_DEBUG
36extern int parse_events_debug;
37#endif
5d9cdc11 38int parse_events_parse(void *parse_state, void *scanner);
e637d177
HK
39static int get_config_terms(struct list_head *head_config,
40 struct list_head *head_terms __maybe_unused);
bcd3279f 41
dcb4e102
KL
42static struct perf_pmu_event_symbol *perf_pmu_events_list;
43/*
44 * The variable indicates the number of supported pmu event symbols.
45 * 0 means not initialized and ready to init
46 * -1 means failed to init, don't try anymore
47 * >0 is the number of supported pmu event symbols
48 */
49static int perf_pmu_events_list_num;
50
705750f2 51struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
1dc12760
JO
52 [PERF_COUNT_HW_CPU_CYCLES] = {
53 .symbol = "cpu-cycles",
54 .alias = "cycles",
55 },
56 [PERF_COUNT_HW_INSTRUCTIONS] = {
57 .symbol = "instructions",
58 .alias = "",
59 },
60 [PERF_COUNT_HW_CACHE_REFERENCES] = {
61 .symbol = "cache-references",
62 .alias = "",
63 },
64 [PERF_COUNT_HW_CACHE_MISSES] = {
65 .symbol = "cache-misses",
66 .alias = "",
67 },
68 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
69 .symbol = "branch-instructions",
70 .alias = "branches",
71 },
72 [PERF_COUNT_HW_BRANCH_MISSES] = {
73 .symbol = "branch-misses",
74 .alias = "",
75 },
76 [PERF_COUNT_HW_BUS_CYCLES] = {
77 .symbol = "bus-cycles",
78 .alias = "",
79 },
80 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
81 .symbol = "stalled-cycles-frontend",
82 .alias = "idle-cycles-frontend",
83 },
84 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
85 .symbol = "stalled-cycles-backend",
86 .alias = "idle-cycles-backend",
87 },
88 [PERF_COUNT_HW_REF_CPU_CYCLES] = {
89 .symbol = "ref-cycles",
90 .alias = "",
91 },
92};
93
705750f2 94struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
1dc12760
JO
95 [PERF_COUNT_SW_CPU_CLOCK] = {
96 .symbol = "cpu-clock",
97 .alias = "",
98 },
99 [PERF_COUNT_SW_TASK_CLOCK] = {
100 .symbol = "task-clock",
101 .alias = "",
102 },
103 [PERF_COUNT_SW_PAGE_FAULTS] = {
104 .symbol = "page-faults",
105 .alias = "faults",
106 },
107 [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
108 .symbol = "context-switches",
109 .alias = "cs",
110 },
111 [PERF_COUNT_SW_CPU_MIGRATIONS] = {
112 .symbol = "cpu-migrations",
113 .alias = "migrations",
114 },
115 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
116 .symbol = "minor-faults",
117 .alias = "",
118 },
119 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
120 .symbol = "major-faults",
121 .alias = "",
122 },
123 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
124 .symbol = "alignment-faults",
125 .alias = "",
126 },
127 [PERF_COUNT_SW_EMULATION_FAULTS] = {
128 .symbol = "emulation-faults",
129 .alias = "",
130 },
d22d1a2a
AH
131 [PERF_COUNT_SW_DUMMY] = {
132 .symbol = "dummy",
133 .alias = "",
134 },
bae9cc41
ACM
135 [PERF_COUNT_SW_BPF_OUTPUT] = {
136 .symbol = "bpf-output",
137 .alias = "",
138 },
8ad8db37
IM
139};
140
cdd6c482
IM
141#define __PERF_EVENT_FIELD(config, name) \
142 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
5242519b 143
1fc570ad 144#define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
cdd6c482 145#define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
1fc570ad 146#define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
cdd6c482 147#define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
5242519b 148
22a9f41b
ACM
149#define for_each_subsystem(sys_dir, sys_dirent) \
150 while ((sys_dirent = readdir(sys_dir)) != NULL) \
151 if (sys_dirent->d_type == DT_DIR && \
152 (strcmp(sys_dirent->d_name, ".")) && \
153 (strcmp(sys_dirent->d_name, "..")))
f6bdafef 154
ae07b63f
PZ
155static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
156{
157 char evt_path[MAXPATHLEN];
158 int fd;
159
ebf294bf 160 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
ae07b63f
PZ
161 sys_dir->d_name, evt_dir->d_name);
162 fd = open(evt_path, O_RDONLY);
163 if (fd < 0)
164 return -EINVAL;
165 close(fd);
166
167 return 0;
168}
169
22a9f41b
ACM
170#define for_each_event(sys_dirent, evt_dir, evt_dirent) \
171 while ((evt_dirent = readdir(evt_dir)) != NULL) \
172 if (evt_dirent->d_type == DT_DIR && \
173 (strcmp(evt_dirent->d_name, ".")) && \
174 (strcmp(evt_dirent->d_name, "..")) && \
175 (!tp_event_has_id(sys_dirent, evt_dirent)))
f6bdafef 176
270bbbe8 177#define MAX_EVENT_LENGTH 512
f6bdafef 178
f6bdafef 179
1ef2ed10 180struct tracepoint_path *tracepoint_id_to_path(u64 config)
f6bdafef 181{
1ef2ed10 182 struct tracepoint_path *path = NULL;
f6bdafef 183 DIR *sys_dir, *evt_dir;
22a9f41b 184 struct dirent *sys_dirent, *evt_dirent;
8aa8a7c8 185 char id_buf[24];
725b1368 186 int fd;
f6bdafef
JB
187 u64 id;
188 char evt_path[MAXPATHLEN];
725b1368 189 char dir_path[MAXPATHLEN];
f6bdafef 190
ebf294bf 191 sys_dir = opendir(tracing_events_path);
f6bdafef 192 if (!sys_dir)
725b1368 193 return NULL;
6b58e7f1 194
22a9f41b 195 for_each_subsystem(sys_dir, sys_dirent) {
725b1368 196
ebf294bf 197 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
22a9f41b 198 sys_dirent->d_name);
725b1368
ED
199 evt_dir = opendir(dir_path);
200 if (!evt_dir)
6b58e7f1 201 continue;
725b1368 202
22a9f41b 203 for_each_event(sys_dirent, evt_dir, evt_dirent) {
725b1368
ED
204
205 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
22a9f41b 206 evt_dirent->d_name);
725b1368 207 fd = open(evt_path, O_RDONLY);
f6bdafef
JB
208 if (fd < 0)
209 continue;
210 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
211 close(fd);
212 continue;
213 }
214 close(fd);
215 id = atoll(id_buf);
216 if (id == config) {
217 closedir(evt_dir);
218 closedir(sys_dir);
59b4caeb 219 path = zalloc(sizeof(*path));
5aa365f2
TS
220 if (!path)
221 return NULL;
1ef2ed10
FW
222 path->system = malloc(MAX_EVENT_LENGTH);
223 if (!path->system) {
224 free(path);
225 return NULL;
226 }
227 path->name = malloc(MAX_EVENT_LENGTH);
228 if (!path->name) {
74cf249d 229 zfree(&path->system);
1ef2ed10
FW
230 free(path);
231 return NULL;
232 }
22a9f41b 233 strncpy(path->system, sys_dirent->d_name,
1ef2ed10 234 MAX_EVENT_LENGTH);
22a9f41b 235 strncpy(path->name, evt_dirent->d_name,
1ef2ed10
FW
236 MAX_EVENT_LENGTH);
237 return path;
f6bdafef
JB
238 }
239 }
240 closedir(evt_dir);
241 }
242
f6bdafef 243 closedir(sys_dir);
1ef2ed10
FW
244 return NULL;
245}
246
e7c93f09
NK
247struct tracepoint_path *tracepoint_name_to_path(const char *name)
248{
249 struct tracepoint_path *path = zalloc(sizeof(*path));
250 char *str = strchr(name, ':');
251
252 if (path == NULL || str == NULL) {
253 free(path);
254 return NULL;
255 }
256
257 path->system = strndup(name, str - name);
258 path->name = strdup(str+1);
259
260 if (path->system == NULL || path->name == NULL) {
74cf249d
ACM
261 zfree(&path->system);
262 zfree(&path->name);
506fde11 263 zfree(&path);
e7c93f09
NK
264 }
265
266 return path;
267}
268
1424dc96
DA
269const char *event_type(int type)
270{
271 switch (type) {
272 case PERF_TYPE_HARDWARE:
273 return "hardware";
274
275 case PERF_TYPE_SOFTWARE:
276 return "software";
277
278 case PERF_TYPE_TRACEPOINT:
279 return "tracepoint";
280
281 case PERF_TYPE_HW_CACHE:
282 return "hardware-cache";
283
284 default:
285 break;
286 }
287
288 return "unknown";
289}
290
e814fddd
WN
291static int parse_events__is_name_term(struct parse_events_term *term)
292{
293 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
294}
7ae92e74 295
e814fddd
WN
296static char *get_config_name(struct list_head *head_terms)
297{
298 struct parse_events_term *term;
299
300 if (!head_terms)
301 return NULL;
302
303 list_for_each_entry(term, head_terms, list)
304 if (parse_events__is_name_term(term))
305 return term->val.str;
306
307 return NULL;
308}
7ae92e74 309
410136f5
SE
310static struct perf_evsel *
311__add_event(struct list_head *list, int *idx,
312 struct perf_event_attr *attr,
66ec1191 313 char *name, struct perf_pmu *pmu,
63ce8449 314 struct list_head *config_terms, bool auto_merge_stats)
89812fc8
JO
315{
316 struct perf_evsel *evsel;
66ec1191 317 struct cpu_map *cpus = pmu ? pmu->cpus : NULL;
89812fc8
JO
318
319 event_attr_init(attr);
320
75fc5ae5 321 evsel = perf_evsel__new_idx(attr, *idx);
c5cd8ac0 322 if (!evsel)
410136f5 323 return NULL;
89812fc8 324
75fc5ae5 325 (*idx)++;
e3ba76de
JO
326 evsel->cpus = cpu_map__get(cpus);
327 evsel->own_cpus = cpu_map__get(cpus);
66ec1191 328 evsel->system_wide = pmu ? pmu->is_uncore : false;
63ce8449 329 evsel->auto_merge_stats = auto_merge_stats;
f30a79b0 330
9db1763c
ACM
331 if (name)
332 evsel->name = strdup(name);
930a2e29
JO
333
334 if (config_terms)
335 list_splice(config_terms, &evsel->config_terms);
336
b847cbdc 337 list_add_tail(&evsel->node, list);
410136f5 338 return evsel;
89812fc8
JO
339}
340
c5cd8ac0 341static int add_event(struct list_head *list, int *idx,
930a2e29
JO
342 struct perf_event_attr *attr, char *name,
343 struct list_head *config_terms)
7ae92e74 344{
63ce8449 345 return __add_event(list, idx, attr, name, NULL, config_terms, false) ? 0 : -ENOMEM;
7ae92e74
YZ
346}
347
0b668bc9 348static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
8326f44d
IM
349{
350 int i, j;
61c45981 351 int n, longest = -1;
8326f44d
IM
352
353 for (i = 0; i < size; i++) {
0b668bc9 354 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
61c45981 355 n = strlen(names[i][j]);
89812fc8 356 if (n > longest && !strncasecmp(str, names[i][j], n))
61c45981
PM
357 longest = n;
358 }
89812fc8 359 if (longest > 0)
61c45981 360 return i;
8326f44d
IM
361 }
362
8953645f 363 return -1;
8326f44d
IM
364}
365
43d0b978
WN
366typedef int config_term_func_t(struct perf_event_attr *attr,
367 struct parse_events_term *term,
368 struct parse_events_error *err);
369static int config_term_common(struct perf_event_attr *attr,
370 struct parse_events_term *term,
371 struct parse_events_error *err);
372static int config_attr(struct perf_event_attr *attr,
373 struct list_head *head,
374 struct parse_events_error *err,
375 config_term_func_t config_term);
376
c5cd8ac0 377int parse_events_add_cache(struct list_head *list, int *idx,
43d0b978 378 char *type, char *op_result1, char *op_result2,
2c97b0d4 379 struct parse_events_error *err,
43d0b978 380 struct list_head *head_config)
8326f44d 381{
89812fc8 382 struct perf_event_attr attr;
43d0b978
WN
383 LIST_HEAD(config_terms);
384 char name[MAX_NAME_LEN], *config_name;
61c45981 385 int cache_type = -1, cache_op = -1, cache_result = -1;
89812fc8
JO
386 char *op_result[2] = { op_result1, op_result2 };
387 int i, n;
8326f44d 388
8326f44d
IM
389 /*
390 * No fallback - if we cannot get a clear cache type
391 * then bail out:
392 */
0b668bc9 393 cache_type = parse_aliases(type, perf_evsel__hw_cache,
89812fc8 394 PERF_COUNT_HW_CACHE_MAX);
8326f44d 395 if (cache_type == -1)
89812fc8
JO
396 return -EINVAL;
397
43d0b978 398 config_name = get_config_name(head_config);
89812fc8 399 n = snprintf(name, MAX_NAME_LEN, "%s", type);
61c45981 400
89812fc8
JO
401 for (i = 0; (i < 2) && (op_result[i]); i++) {
402 char *str = op_result[i];
403
275ef387 404 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
61c45981
PM
405
406 if (cache_op == -1) {
0b668bc9 407 cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
89812fc8 408 PERF_COUNT_HW_CACHE_OP_MAX);
61c45981 409 if (cache_op >= 0) {
0b668bc9 410 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
89812fc8 411 return -EINVAL;
61c45981
PM
412 continue;
413 }
414 }
415
416 if (cache_result == -1) {
0b668bc9
ACM
417 cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
418 PERF_COUNT_HW_CACHE_RESULT_MAX);
61c45981
PM
419 if (cache_result >= 0)
420 continue;
421 }
61c45981 422 }
8326f44d 423
8326f44d
IM
424 /*
425 * Fall back to reads:
426 */
8953645f
IM
427 if (cache_op == -1)
428 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
8326f44d 429
8326f44d
IM
430 /*
431 * Fall back to accesses:
432 */
433 if (cache_result == -1)
434 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
435
89812fc8
JO
436 memset(&attr, 0, sizeof(attr));
437 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
438 attr.type = PERF_TYPE_HW_CACHE;
43d0b978
WN
439
440 if (head_config) {
2c97b0d4 441 if (config_attr(&attr, head_config, err,
43d0b978
WN
442 config_term_common))
443 return -EINVAL;
444
445 if (get_config_terms(head_config, &config_terms))
446 return -ENOMEM;
447 }
448 return add_event(list, idx, &attr, config_name ? : name, &config_terms);
bcd3279f
FW
449}
450
272ed29a 451static void tracepoint_error(struct parse_events_error *e, int err,
8c619d6a 452 const char *sys, const char *name)
19658171
JO
453{
454 char help[BUFSIZ];
455
ec183d22
AH
456 if (!e)
457 return;
458
19658171
JO
459 /*
460 * We get error directly from syscall errno ( > 0),
461 * or from encoded pointer's error ( < 0).
462 */
463 err = abs(err);
464
465 switch (err) {
466 case EACCES:
272ed29a 467 e->str = strdup("can't access trace events");
19658171
JO
468 break;
469 case ENOENT:
272ed29a 470 e->str = strdup("unknown tracepoint");
19658171
JO
471 break;
472 default:
272ed29a 473 e->str = strdup("failed to add tracepoint");
19658171
JO
474 break;
475 }
476
477 tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
272ed29a 478 e->help = strdup(help);
19658171
JO
479}
480
c5cd8ac0 481static int add_tracepoint(struct list_head *list, int *idx,
8c619d6a 482 const char *sys_name, const char *evt_name,
272ed29a 483 struct parse_events_error *err,
e637d177 484 struct list_head *head_config)
bcd3279f 485{
82fe1c29 486 struct perf_evsel *evsel;
bcd3279f 487
ef503831 488 evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
19658171 489 if (IS_ERR(evsel)) {
272ed29a 490 tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
8dd2a131 491 return PTR_ERR(evsel);
19658171 492 }
bcd3279f 493
e637d177
HK
494 if (head_config) {
495 LIST_HEAD(config_terms);
496
497 if (get_config_terms(head_config, &config_terms))
498 return -ENOMEM;
499 list_splice(&config_terms, &evsel->config_terms);
500 }
501
82fe1c29 502 list_add_tail(&evsel->node, list);
82fe1c29 503 return 0;
8326f44d
IM
504}
505
c5cd8ac0 506static int add_tracepoint_multi_event(struct list_head *list, int *idx,
8c619d6a 507 const char *sys_name, const char *evt_name,
272ed29a 508 struct parse_events_error *err,
e637d177 509 struct list_head *head_config)
bcd3279f
FW
510{
511 char evt_path[MAXPATHLEN];
512 struct dirent *evt_ent;
513 DIR *evt_dir;
27bf90bf 514 int ret = 0, found = 0;
bcd3279f 515
ebf294bf 516 snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
bcd3279f 517 evt_dir = opendir(evt_path);
bcd3279f 518 if (!evt_dir) {
272ed29a 519 tracepoint_error(err, errno, sys_name, evt_name);
89812fc8 520 return -1;
bcd3279f
FW
521 }
522
89812fc8 523 while (!ret && (evt_ent = readdir(evt_dir))) {
bcd3279f
FW
524 if (!strcmp(evt_ent->d_name, ".")
525 || !strcmp(evt_ent->d_name, "..")
526 || !strcmp(evt_ent->d_name, "enable")
527 || !strcmp(evt_ent->d_name, "filter"))
528 continue;
529
89812fc8 530 if (!strglobmatch(evt_ent->d_name, evt_name))
fb1d2edf
MH
531 continue;
532
27bf90bf
JO
533 found++;
534
e637d177 535 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
272ed29a 536 err, head_config);
bcd3279f
FW
537 }
538
27bf90bf
JO
539 if (!found) {
540 tracepoint_error(err, ENOENT, sys_name, evt_name);
541 ret = -1;
542 }
543
0bd3f084 544 closedir(evt_dir);
89812fc8 545 return ret;
bcd3279f
FW
546}
547
c5cd8ac0 548static int add_tracepoint_event(struct list_head *list, int *idx,
8c619d6a 549 const char *sys_name, const char *evt_name,
272ed29a 550 struct parse_events_error *err,
e637d177 551 struct list_head *head_config)
f35488f9
JO
552{
553 return strpbrk(evt_name, "*?") ?
e637d177 554 add_tracepoint_multi_event(list, idx, sys_name, evt_name,
272ed29a 555 err, head_config) :
e637d177 556 add_tracepoint(list, idx, sys_name, evt_name,
272ed29a 557 err, head_config);
f35488f9
JO
558}
559
c5cd8ac0 560static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
8c619d6a 561 const char *sys_name, const char *evt_name,
272ed29a 562 struct parse_events_error *err,
e637d177 563 struct list_head *head_config)
f35488f9
JO
564{
565 struct dirent *events_ent;
566 DIR *events_dir;
567 int ret = 0;
568
569 events_dir = opendir(tracing_events_path);
570 if (!events_dir) {
272ed29a 571 tracepoint_error(err, errno, sys_name, evt_name);
f35488f9
JO
572 return -1;
573 }
574
575 while (!ret && (events_ent = readdir(events_dir))) {
576 if (!strcmp(events_ent->d_name, ".")
577 || !strcmp(events_ent->d_name, "..")
578 || !strcmp(events_ent->d_name, "enable")
579 || !strcmp(events_ent->d_name, "header_event")
580 || !strcmp(events_ent->d_name, "header_page"))
581 continue;
582
583 if (!strglobmatch(events_ent->d_name, sys_name))
584 continue;
585
586 ret = add_tracepoint_event(list, idx, events_ent->d_name,
272ed29a 587 evt_name, err, head_config);
f35488f9
JO
588 }
589
590 closedir(events_dir);
591 return ret;
592}
593
4edf30e3 594struct __add_bpf_event_param {
5d9cdc11 595 struct parse_events_state *parse_state;
4edf30e3 596 struct list_head *list;
95088a59 597 struct list_head *head_config;
4edf30e3
WN
598};
599
cd102d70 600static int add_bpf_event(const char *group, const char *event, int fd,
4edf30e3
WN
601 void *_param)
602{
603 LIST_HEAD(new_evsels);
604 struct __add_bpf_event_param *param = _param;
5d9cdc11 605 struct parse_events_state *parse_state = param->parse_state;
4edf30e3 606 struct list_head *list = param->list;
1f45b1d4 607 struct perf_evsel *pos;
4edf30e3
WN
608 int err;
609
610 pr_debug("add bpf event %s:%s and attach bpf program %d\n",
cd102d70 611 group, event, fd);
4edf30e3 612
5d369a75
ACM
613 err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group,
614 event, parse_state->error,
95088a59 615 param->head_config);
4edf30e3
WN
616 if (err) {
617 struct perf_evsel *evsel, *tmp;
618
619 pr_debug("Failed to add BPF event %s:%s\n",
cd102d70 620 group, event);
4edf30e3
WN
621 list_for_each_entry_safe(evsel, tmp, &new_evsels, node) {
622 list_del(&evsel->node);
623 perf_evsel__delete(evsel);
624 }
625 return err;
626 }
cd102d70 627 pr_debug("adding %s:%s\n", group, event);
4edf30e3 628
1f45b1d4
WN
629 list_for_each_entry(pos, &new_evsels, node) {
630 pr_debug("adding %s:%s to %p\n",
cd102d70 631 group, event, pos);
1f45b1d4
WN
632 pos->bpf_fd = fd;
633 }
4edf30e3
WN
634 list_splice(&new_evsels, list);
635 return 0;
636}
637
5d9cdc11 638int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
84c86ca1 639 struct list_head *list,
95088a59
WN
640 struct bpf_object *obj,
641 struct list_head *head_config)
84c86ca1
WN
642{
643 int err;
644 char errbuf[BUFSIZ];
5d9cdc11 645 struct __add_bpf_event_param param = {parse_state, list, head_config};
aa3abf30 646 static bool registered_unprobe_atexit = false;
84c86ca1
WN
647
648 if (IS_ERR(obj) || !obj) {
649 snprintf(errbuf, sizeof(errbuf),
650 "Internal error: load bpf obj with NULL");
651 err = -EINVAL;
652 goto errout;
653 }
654
aa3abf30
WN
655 /*
656 * Register atexit handler before calling bpf__probe() so
657 * bpf__probe() don't need to unprobe probe points its already
658 * created when failure.
659 */
660 if (!registered_unprobe_atexit) {
661 atexit(bpf__clear);
662 registered_unprobe_atexit = true;
663 }
664
665 err = bpf__probe(obj);
666 if (err) {
667 bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
668 goto errout;
669 }
670
1e5e3ee8
WN
671 err = bpf__load(obj);
672 if (err) {
673 bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
674 goto errout;
675 }
676
cd102d70 677 err = bpf__foreach_event(obj, add_bpf_event, &param);
4edf30e3
WN
678 if (err) {
679 snprintf(errbuf, sizeof(errbuf),
680 "Attach events in BPF object failed");
681 goto errout;
682 }
683
684 return 0;
84c86ca1 685errout:
5d9cdc11
ACM
686 parse_state->error->help = strdup("(add -v to see detail)");
687 parse_state->error->str = strdup(errbuf);
84c86ca1
WN
688 return err;
689}
690
a34f3be7 691static int
5d9cdc11 692parse_events_config_bpf(struct parse_events_state *parse_state,
a34f3be7
WN
693 struct bpf_object *obj,
694 struct list_head *head_config)
695{
696 struct parse_events_term *term;
697 int error_pos;
698
699 if (!head_config || list_empty(head_config))
700 return 0;
701
702 list_for_each_entry(term, head_config, list) {
703 char errbuf[BUFSIZ];
704 int err;
705
706 if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) {
707 snprintf(errbuf, sizeof(errbuf),
708 "Invalid config term for BPF object");
709 errbuf[BUFSIZ - 1] = '\0';
710
5d9cdc11
ACM
711 parse_state->error->idx = term->err_term;
712 parse_state->error->str = strdup(errbuf);
a34f3be7
WN
713 return -EINVAL;
714 }
715
5d9cdc11 716 err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos);
a34f3be7 717 if (err) {
5d9cdc11 718 bpf__strerror_config_obj(obj, term, parse_state->evlist,
a34f3be7
WN
719 &error_pos, err, errbuf,
720 sizeof(errbuf));
5d9cdc11 721 parse_state->error->help = strdup(
7630b3e2 722"Hint:\tValid config terms:\n"
e571e029
WN
723" \tmap:[<arraymap>].value<indices>=[value]\n"
724" \tmap:[<eventmap>].event<indices>=[event]\n"
7630b3e2 725"\n"
e571e029 726" \twhere <indices> is something like [0,3...5] or [all]\n"
a34f3be7 727" \t(add -v to see detail)");
5d9cdc11 728 parse_state->error->str = strdup(errbuf);
a34f3be7 729 if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE)
5d9cdc11 730 parse_state->error->idx = term->err_val;
a34f3be7 731 else
5d9cdc11 732 parse_state->error->idx = term->err_term + error_pos;
a34f3be7
WN
733 return err;
734 }
735 }
736 return 0;
737}
738
95088a59
WN
739/*
740 * Split config terms:
741 * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
742 * 'call-graph=fp' is 'evt config', should be applied to each
743 * events in bpf.c.
744 * 'map:array.value[0]=1' is 'obj config', should be processed
745 * with parse_events_config_bpf.
746 *
747 * Move object config terms from the first list to obj_head_config.
748 */
749static void
750split_bpf_config_terms(struct list_head *evt_head_config,
751 struct list_head *obj_head_config)
752{
753 struct parse_events_term *term, *temp;
754
755 /*
756 * Currectly, all possible user config term
757 * belong to bpf object. parse_events__is_hardcoded_term()
758 * happends to be a good flag.
759 *
760 * See parse_events_config_bpf() and
761 * config_term_tracepoint().
762 */
763 list_for_each_entry_safe(term, temp, evt_head_config, list)
764 if (!parse_events__is_hardcoded_term(term))
765 list_move_tail(&term->list, obj_head_config);
766}
767
5d9cdc11 768int parse_events_load_bpf(struct parse_events_state *parse_state,
84c86ca1 769 struct list_head *list,
d509db04 770 char *bpf_file_name,
a34f3be7
WN
771 bool source,
772 struct list_head *head_config)
84c86ca1 773{
a34f3be7 774 int err;
95088a59
WN
775 struct bpf_object *obj;
776 LIST_HEAD(obj_head_config);
777
778 if (head_config)
779 split_bpf_config_terms(head_config, &obj_head_config);
84c86ca1 780
d509db04 781 obj = bpf__prepare_load(bpf_file_name, source);
6371ca3b 782 if (IS_ERR(obj)) {
84c86ca1 783 char errbuf[BUFSIZ];
84c86ca1 784
6371ca3b 785 err = PTR_ERR(obj);
84c86ca1
WN
786
787 if (err == -ENOTSUP)
788 snprintf(errbuf, sizeof(errbuf),
789 "BPF support is not compiled");
790 else
d3e0ce39
WN
791 bpf__strerror_prepare_load(bpf_file_name,
792 source,
793 -err, errbuf,
794 sizeof(errbuf));
84c86ca1 795
5d9cdc11
ACM
796 parse_state->error->help = strdup("(add -v to see detail)");
797 parse_state->error->str = strdup(errbuf);
84c86ca1
WN
798 return err;
799 }
800
5d9cdc11 801 err = parse_events_load_bpf_obj(parse_state, list, obj, head_config);
a34f3be7
WN
802 if (err)
803 return err;
5d9cdc11 804 err = parse_events_config_bpf(parse_state, obj, &obj_head_config);
95088a59
WN
805
806 /*
807 * Caller doesn't know anything about obj_head_config,
808 * so combine them together again before returnning.
809 */
810 if (head_config)
811 list_splice_tail(&obj_head_config, head_config);
812 return err;
84c86ca1
WN
813}
814
89812fc8
JO
815static int
816parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
1b290d67
FW
817{
818 int i;
819
820 for (i = 0; i < 3; i++) {
89812fc8 821 if (!type || !type[i])
1b290d67
FW
822 break;
823
7582732f
JO
824#define CHECK_SET_TYPE(bit) \
825do { \
826 if (attr->bp_type & bit) \
827 return -EINVAL; \
828 else \
829 attr->bp_type |= bit; \
830} while (0)
831
1b290d67
FW
832 switch (type[i]) {
833 case 'r':
7582732f 834 CHECK_SET_TYPE(HW_BREAKPOINT_R);
1b290d67
FW
835 break;
836 case 'w':
7582732f 837 CHECK_SET_TYPE(HW_BREAKPOINT_W);
1b290d67
FW
838 break;
839 case 'x':
7582732f 840 CHECK_SET_TYPE(HW_BREAKPOINT_X);
1b290d67
FW
841 break;
842 default:
89812fc8 843 return -EINVAL;
1b290d67
FW
844 }
845 }
89812fc8 846
7582732f
JO
847#undef CHECK_SET_TYPE
848
1b290d67
FW
849 if (!attr->bp_type) /* Default */
850 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
851
89812fc8 852 return 0;
1b290d67
FW
853}
854
c5cd8ac0 855int parse_events_add_breakpoint(struct list_head *list, int *idx,
3741eb9f 856 void *ptr, char *type, u64 len)
1b290d67 857{
89812fc8 858 struct perf_event_attr attr;
1b290d67 859
89812fc8 860 memset(&attr, 0, sizeof(attr));
9fafd98f 861 attr.bp_addr = (unsigned long) ptr;
1b290d67 862
89812fc8
JO
863 if (parse_breakpoint_type(type, &attr))
864 return -EINVAL;
1b290d67 865
3741eb9f
JS
866 /* Provide some defaults if len is not specified */
867 if (!len) {
868 if (attr.bp_type == HW_BREAKPOINT_X)
869 len = sizeof(long);
870 else
871 len = HW_BREAKPOINT_LEN_4;
872 }
873
874 attr.bp_len = len;
61c45981 875
89812fc8 876 attr.type = PERF_TYPE_BREAKPOINT;
4a841d65 877 attr.sample_period = 1;
b908debd 878
930a2e29 879 return add_event(list, idx, &attr, NULL, NULL);
74d5b588
JSR
880}
881
3b0e371c
JO
882static int check_type_val(struct parse_events_term *term,
883 struct parse_events_error *err,
884 int type)
885{
886 if (type == term->type_val)
887 return 0;
888
889 if (err) {
890 err->idx = term->err_val;
891 if (type == PARSE_EVENTS__TERM_TYPE_NUM)
892 err->str = strdup("expected numeric value");
893 else
894 err->str = strdup("expected string value");
895 }
896 return -EINVAL;
897}
898
17cb5f84
WN
899/*
900 * Update according to parse-events.l
901 */
902static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
903 [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>",
904 [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config",
905 [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1",
906 [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2",
907 [PARSE_EVENTS__TERM_TYPE_NAME] = "name",
908 [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period",
909 [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq",
910 [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type",
911 [PARSE_EVENTS__TERM_TYPE_TIME] = "time",
912 [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph",
913 [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size",
914 [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit",
915 [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit",
792d48b4 916 [PARSE_EVENTS__TERM_TYPE_MAX_STACK] = "max-stack",
626a6b78
WN
917 [PARSE_EVENTS__TERM_TYPE_OVERWRITE] = "overwrite",
918 [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE] = "no-overwrite",
dd60fba7 919 [PARSE_EVENTS__TERM_TYPE_DRV_CFG] = "driver-config",
17cb5f84
WN
920};
921
1669e509
WN
922static bool config_term_shrinked;
923
924static bool
925config_term_avail(int term_type, struct parse_events_error *err)
926{
927 if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
928 err->str = strdup("Invalid term_type");
929 return false;
930 }
931 if (!config_term_shrinked)
932 return true;
933
934 switch (term_type) {
935 case PARSE_EVENTS__TERM_TYPE_CONFIG:
936 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
937 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
938 case PARSE_EVENTS__TERM_TYPE_NAME:
06835545 939 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1669e509
WN
940 return true;
941 default:
942 if (!err)
943 return false;
944
945 /* term_type is validated so indexing is safe */
946 if (asprintf(&err->str, "'%s' is not usable in 'perf stat'",
947 config_term_names[term_type]) < 0)
948 err->str = NULL;
949 return false;
950 }
951}
952
953void parse_events__shrink_config_terms(void)
954{
955 config_term_shrinked = true;
956}
957
0b8891a8
HK
958static int config_term_common(struct perf_event_attr *attr,
959 struct parse_events_term *term,
960 struct parse_events_error *err)
8f707d84 961{
3b0e371c
JO
962#define CHECK_TYPE_VAL(type) \
963do { \
964 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
965 return -EINVAL; \
16fa7e82
JO
966} while (0)
967
968 switch (term->type_term) {
8f707d84 969 case PARSE_EVENTS__TERM_TYPE_CONFIG:
16fa7e82 970 CHECK_TYPE_VAL(NUM);
8f707d84
JO
971 attr->config = term->val.num;
972 break;
973 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
16fa7e82 974 CHECK_TYPE_VAL(NUM);
8f707d84
JO
975 attr->config1 = term->val.num;
976 break;
977 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
16fa7e82 978 CHECK_TYPE_VAL(NUM);
8f707d84
JO
979 attr->config2 = term->val.num;
980 break;
981 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
16fa7e82 982 CHECK_TYPE_VAL(NUM);
8f707d84 983 break;
09af2a55
NK
984 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
985 CHECK_TYPE_VAL(NUM);
986 break;
8f707d84 987 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
ac12f676
AK
988 CHECK_TYPE_VAL(STR);
989 if (strcmp(term->val.str, "no") &&
990 parse_branch_str(term->val.str, &attr->branch_sample_type)) {
991 err->str = strdup("invalid branch sample type");
992 err->idx = term->err_val;
993 return -EINVAL;
994 }
8f707d84 995 break;
32067712
KL
996 case PARSE_EVENTS__TERM_TYPE_TIME:
997 CHECK_TYPE_VAL(NUM);
998 if (term->val.num > 1) {
999 err->str = strdup("expected 0 or 1");
1000 err->idx = term->err_val;
1001 return -EINVAL;
1002 }
1003 break;
d457c963
KL
1004 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1005 CHECK_TYPE_VAL(STR);
1006 break;
1007 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1008 CHECK_TYPE_VAL(NUM);
1009 break;
374ce938
WN
1010 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1011 CHECK_TYPE_VAL(NUM);
1012 break;
1013 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1014 CHECK_TYPE_VAL(NUM);
1015 break;
626a6b78
WN
1016 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1017 CHECK_TYPE_VAL(NUM);
1018 break;
1019 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1020 CHECK_TYPE_VAL(NUM);
1021 break;
6b5fc39b
JO
1022 case PARSE_EVENTS__TERM_TYPE_NAME:
1023 CHECK_TYPE_VAL(STR);
1024 break;
792d48b4
ACM
1025 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1026 CHECK_TYPE_VAL(NUM);
1027 break;
8f707d84 1028 default:
ffeb883e
HK
1029 err->str = strdup("unknown term");
1030 err->idx = term->err_term;
1031 err->help = parse_events_formats_error_string(NULL);
8f707d84
JO
1032 return -EINVAL;
1033 }
16fa7e82 1034
1669e509
WN
1035 /*
1036 * Check term availbility after basic checking so
1037 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
1038 *
1039 * If check availbility at the entry of this function,
1040 * user will see "'<sysfs term>' is not usable in 'perf stat'"
1041 * if an invalid config term is provided for legacy events
1042 * (for example, instructions/badterm/...), which is confusing.
1043 */
1044 if (!config_term_avail(term->type_term, err))
1045 return -EINVAL;
8f707d84 1046 return 0;
16fa7e82 1047#undef CHECK_TYPE_VAL
8f707d84
JO
1048}
1049
0b8891a8
HK
1050static int config_term_pmu(struct perf_event_attr *attr,
1051 struct parse_events_term *term,
1052 struct parse_events_error *err)
1053{
dd60fba7
MP
1054 if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER ||
1055 term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG)
0b8891a8
HK
1056 /*
1057 * Always succeed for sysfs terms, as we dont know
1058 * at this point what type they need to have.
1059 */
1060 return 0;
1061 else
1062 return config_term_common(attr, term, err);
1063}
1064
e637d177
HK
1065static int config_term_tracepoint(struct perf_event_attr *attr,
1066 struct parse_events_term *term,
1067 struct parse_events_error *err)
1068{
1069 switch (term->type_term) {
1070 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1071 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
374ce938
WN
1072 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1073 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
792d48b4 1074 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
626a6b78
WN
1075 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1076 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
e637d177
HK
1077 return config_term_common(attr, term, err);
1078 default:
1079 if (err) {
1080 err->idx = term->err_term;
1081 err->str = strdup("unknown term");
1082 err->help = strdup("valid terms: call-graph,stack-size\n");
1083 }
1084 return -EINVAL;
1085 }
1086
1087 return 0;
1088}
1089
8f707d84 1090static int config_attr(struct perf_event_attr *attr,
3b0e371c 1091 struct list_head *head,
0b8891a8
HK
1092 struct parse_events_error *err,
1093 config_term_func_t config_term)
8f707d84 1094{
6cee6cd3 1095 struct parse_events_term *term;
8f707d84
JO
1096
1097 list_for_each_entry(term, head, list)
3b0e371c 1098 if (config_term(attr, term, err))
8f707d84
JO
1099 return -EINVAL;
1100
1101 return 0;
1102}
1103
930a2e29
JO
1104static int get_config_terms(struct list_head *head_config,
1105 struct list_head *head_terms __maybe_unused)
1106{
1107#define ADD_CONFIG_TERM(__type, __name, __val) \
1108do { \
1109 struct perf_evsel_config_term *__t; \
1110 \
1111 __t = zalloc(sizeof(*__t)); \
1112 if (!__t) \
1113 return -ENOMEM; \
1114 \
1115 INIT_LIST_HEAD(&__t->list); \
1116 __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \
1117 __t->val.__name = __val; \
1118 list_add_tail(&__t->list, head_terms); \
1119} while (0)
1120
1121 struct parse_events_term *term;
1122
1123 list_for_each_entry(term, head_config, list) {
1124 switch (term->type_term) {
ee4c7588
JO
1125 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1126 ADD_CONFIG_TERM(PERIOD, period, term->val.num);
32067712 1127 break;
09af2a55
NK
1128 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1129 ADD_CONFIG_TERM(FREQ, freq, term->val.num);
1130 break;
32067712
KL
1131 case PARSE_EVENTS__TERM_TYPE_TIME:
1132 ADD_CONFIG_TERM(TIME, time, term->val.num);
1133 break;
d457c963
KL
1134 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1135 ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
1136 break;
ac12f676
AK
1137 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1138 ADD_CONFIG_TERM(BRANCH, branch, term->val.str);
1139 break;
d457c963
KL
1140 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1141 ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
1142 break;
374ce938
WN
1143 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1144 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0);
1145 break;
1146 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1147 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1);
1148 break;
792d48b4
ACM
1149 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1150 ADD_CONFIG_TERM(MAX_STACK, max_stack, term->val.num);
1151 break;
626a6b78
WN
1152 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1153 ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 1 : 0);
1154 break;
1155 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1156 ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 0 : 1);
1157 break;
dd60fba7
MP
1158 case PARSE_EVENTS__TERM_TYPE_DRV_CFG:
1159 ADD_CONFIG_TERM(DRV_CFG, drv_cfg, term->val.str);
1160 break;
930a2e29
JO
1161 default:
1162 break;
1163 }
1164 }
1165#undef ADD_EVSEL_CONFIG
1166 return 0;
1167}
1168
e637d177 1169int parse_events_add_tracepoint(struct list_head *list, int *idx,
8c619d6a 1170 const char *sys, const char *event,
272ed29a 1171 struct parse_events_error *err,
e637d177
HK
1172 struct list_head *head_config)
1173{
1174 if (head_config) {
1175 struct perf_event_attr attr;
1176
272ed29a 1177 if (config_attr(&attr, head_config, err,
e637d177
HK
1178 config_term_tracepoint))
1179 return -EINVAL;
1180 }
1181
1182 if (strpbrk(sys, "*?"))
1183 return add_tracepoint_multi_sys(list, idx, sys, event,
272ed29a 1184 err, head_config);
e637d177
HK
1185 else
1186 return add_tracepoint_event(list, idx, sys, event,
272ed29a 1187 err, head_config);
e637d177
HK
1188}
1189
5d9cdc11 1190int parse_events_add_numeric(struct parse_events_state *parse_state,
87d650be 1191 struct list_head *list,
b527bab5 1192 u32 type, u64 config,
8f707d84 1193 struct list_head *head_config)
8ad8db37 1194{
89812fc8 1195 struct perf_event_attr attr;
930a2e29 1196 LIST_HEAD(config_terms);
61c45981 1197
89812fc8
JO
1198 memset(&attr, 0, sizeof(attr));
1199 attr.type = type;
1200 attr.config = config;
8f707d84 1201
930a2e29 1202 if (head_config) {
5d9cdc11 1203 if (config_attr(&attr, head_config, parse_state->error,
0b8891a8 1204 config_term_common))
930a2e29
JO
1205 return -EINVAL;
1206
1207 if (get_config_terms(head_config, &config_terms))
1208 return -ENOMEM;
1209 }
8f707d84 1210
5d9cdc11 1211 return add_event(list, &parse_state->idx, &attr,
10bf358a 1212 get_config_name(head_config), &config_terms);
61c45981 1213}
8ad8db37 1214
63ce8449 1215static int __parse_events_add_pmu(struct parse_events_state *parse_state,
36adec85 1216 struct list_head *list, char *name,
63ce8449 1217 struct list_head *head_config, bool auto_merge_stats)
5f537a26
JO
1218{
1219 struct perf_event_attr attr;
46441bdc 1220 struct perf_pmu_info info;
5f537a26 1221 struct perf_pmu *pmu;
410136f5 1222 struct perf_evsel *evsel;
930a2e29 1223 LIST_HEAD(config_terms);
5f537a26
JO
1224
1225 pmu = perf_pmu__find(name);
1226 if (!pmu)
1227 return -EINVAL;
1228
dc0a6202
AH
1229 if (pmu->default_config) {
1230 memcpy(&attr, pmu->default_config,
1231 sizeof(struct perf_event_attr));
1232 } else {
1233 memset(&attr, 0, sizeof(attr));
1234 }
5f537a26 1235
ad962273
AH
1236 if (!head_config) {
1237 attr.type = pmu->type;
66ec1191 1238 evsel = __add_event(list, &parse_state->idx, &attr, NULL, pmu, NULL, auto_merge_stats);
ad962273
AH
1239 return evsel ? 0 : -ENOMEM;
1240 }
1241
46441bdc 1242 if (perf_pmu__check_alias(pmu, head_config, &info))
a6146d50
ZY
1243 return -EINVAL;
1244
5f537a26
JO
1245 /*
1246 * Configure hardcoded terms first, no need to check
1247 * return value when called with fail == 0 ;)
1248 */
5d9cdc11 1249 if (config_attr(&attr, head_config, parse_state->error, config_term_pmu))
c056ba6a 1250 return -EINVAL;
5f537a26 1251
930a2e29
JO
1252 if (get_config_terms(head_config, &config_terms))
1253 return -ENOMEM;
1254
5d9cdc11 1255 if (perf_pmu__config(pmu, &attr, head_config, parse_state->error))
5f537a26
JO
1256 return -EINVAL;
1257
5d9cdc11 1258 evsel = __add_event(list, &parse_state->idx, &attr,
66ec1191 1259 get_config_name(head_config), pmu,
63ce8449 1260 &config_terms, auto_merge_stats);
410136f5 1261 if (evsel) {
46441bdc
MF
1262 evsel->unit = info.unit;
1263 evsel->scale = info.scale;
044330c1 1264 evsel->per_pkg = info.per_pkg;
1d9e446b 1265 evsel->snapshot = info.snapshot;
37932c18 1266 evsel->metric_expr = info.metric_expr;
96284814 1267 evsel->metric_name = info.metric_name;
410136f5
SE
1268 }
1269
1270 return evsel ? 0 : -ENOMEM;
5f537a26
JO
1271}
1272
63ce8449
ACM
1273int parse_events_add_pmu(struct parse_events_state *parse_state,
1274 struct list_head *list, char *name,
1275 struct list_head *head_config)
1276{
1277 return __parse_events_add_pmu(parse_state, list, name, head_config, false);
1278}
1279
5d9cdc11 1280int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
2073ad33
AK
1281 char *str, struct list_head **listp)
1282{
1283 struct list_head *head;
1284 struct parse_events_term *term;
1285 struct list_head *list;
1286 struct perf_pmu *pmu = NULL;
1287 int ok = 0;
1288
1289 *listp = NULL;
1290 /* Add it for all PMUs that support the alias */
1291 list = malloc(sizeof(struct list_head));
1292 if (!list)
1293 return -1;
1294 INIT_LIST_HEAD(list);
1295 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1296 struct perf_pmu_alias *alias;
1297
1298 list_for_each_entry(alias, &pmu->aliases, list) {
1299 if (!strcasecmp(alias->name, str)) {
1300 head = malloc(sizeof(struct list_head));
1301 if (!head)
1302 return -1;
1303 INIT_LIST_HEAD(head);
1304 if (parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
1305 str, 1, false, &str, NULL) < 0)
1306 return -1;
1307 list_add_tail(&term->list, head);
1308
63ce8449
ACM
1309 if (!__parse_events_add_pmu(parse_state, list,
1310 pmu->name, head, true)) {
2073ad33
AK
1311 pr_debug("%s -> %s/%s/\n", str,
1312 pmu->name, alias->str);
1313 ok++;
1314 }
1315
1316 parse_events_terms__delete(head);
1317 }
1318 }
1319 }
1320 if (!ok)
1321 return -1;
1322 *listp = list;
1323 return 0;
1324}
1325
6a4bb04c
JO
1326int parse_events__modifier_group(struct list_head *list,
1327 char *event_mod)
89efb029 1328{
6a4bb04c
JO
1329 return parse_events__modifier_event(list, event_mod, true);
1330}
1331
63dab225 1332void parse_events__set_leader(char *name, struct list_head *list)
6a4bb04c
JO
1333{
1334 struct perf_evsel *leader;
1335
854f7363
WN
1336 if (list_empty(list)) {
1337 WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1338 return;
1339 }
1340
63dab225
ACM
1341 __perf_evlist__set_leader(list);
1342 leader = list_entry(list->next, struct perf_evsel, node);
6a4bb04c 1343 leader->group_name = name ? strdup(name) : NULL;
89efb029
JO
1344}
1345
c5cd8ac0 1346/* list_event is assumed to point to malloc'ed memory */
5d7be90e
JO
1347void parse_events_update_lists(struct list_head *list_event,
1348 struct list_head *list_all)
1349{
1350 /*
1351 * Called for single event definition. Update the
89efb029 1352 * 'all event' list, and reinit the 'single event'
5d7be90e
JO
1353 * list, for next event definition.
1354 */
1355 list_splice_tail(list_event, list_all);
b847cbdc 1356 free(list_event);
5d7be90e
JO
1357}
1358
f5b1135b
JO
1359struct event_modifier {
1360 int eu;
1361 int ek;
1362 int eh;
1363 int eH;
1364 int eG;
a1e12da4 1365 int eI;
f5b1135b 1366 int precise;
7f94af7a 1367 int precise_max;
f5b1135b 1368 int exclude_GH;
3c176311 1369 int sample_read;
e9a7c414 1370 int pinned;
f5b1135b
JO
1371};
1372
1373static int get_event_modifier(struct event_modifier *mod, char *str,
1374 struct perf_evsel *evsel)
61c45981 1375{
f5b1135b
JO
1376 int eu = evsel ? evsel->attr.exclude_user : 0;
1377 int ek = evsel ? evsel->attr.exclude_kernel : 0;
1378 int eh = evsel ? evsel->attr.exclude_hv : 0;
1379 int eH = evsel ? evsel->attr.exclude_host : 0;
1380 int eG = evsel ? evsel->attr.exclude_guest : 0;
a1e12da4 1381 int eI = evsel ? evsel->attr.exclude_idle : 0;
f5b1135b 1382 int precise = evsel ? evsel->attr.precise_ip : 0;
7f94af7a 1383 int precise_max = 0;
3c176311 1384 int sample_read = 0;
e9a7c414 1385 int pinned = evsel ? evsel->attr.pinned : 0;
a21ca2ca 1386
f5b1135b
JO
1387 int exclude = eu | ek | eh;
1388 int exclude_GH = evsel ? evsel->exclude_GH : 0;
1389
f5b1135b 1390 memset(mod, 0, sizeof(*mod));
ceb53fbf 1391
61c45981 1392 while (*str) {
ab608344
PZ
1393 if (*str == 'u') {
1394 if (!exclude)
1395 exclude = eu = ek = eh = 1;
61c45981 1396 eu = 0;
ab608344
PZ
1397 } else if (*str == 'k') {
1398 if (!exclude)
1399 exclude = eu = ek = eh = 1;
61c45981 1400 ek = 0;
ab608344
PZ
1401 } else if (*str == 'h') {
1402 if (!exclude)
1403 exclude = eu = ek = eh = 1;
61c45981 1404 eh = 0;
99320cc8
JR
1405 } else if (*str == 'G') {
1406 if (!exclude_GH)
1407 exclude_GH = eG = eH = 1;
1408 eG = 0;
1409 } else if (*str == 'H') {
1410 if (!exclude_GH)
1411 exclude_GH = eG = eH = 1;
1412 eH = 0;
a1e12da4
JO
1413 } else if (*str == 'I') {
1414 eI = 1;
ab608344
PZ
1415 } else if (*str == 'p') {
1416 precise++;
1342798c
DA
1417 /* use of precise requires exclude_guest */
1418 if (!exclude_GH)
1419 eG = 1;
7f94af7a
JO
1420 } else if (*str == 'P') {
1421 precise_max = 1;
3c176311
JO
1422 } else if (*str == 'S') {
1423 sample_read = 1;
e9a7c414
ME
1424 } else if (*str == 'D') {
1425 pinned = 1;
ab608344 1426 } else
61c45981 1427 break;
ab608344 1428
61c45981 1429 ++str;
5242519b 1430 }
ceb53fbf 1431
89812fc8
JO
1432 /*
1433 * precise ip:
1434 *
1435 * 0 - SAMPLE_IP can have arbitrary skid
1436 * 1 - SAMPLE_IP must have constant skid
1437 * 2 - SAMPLE_IP requested to have 0 skid
1438 * 3 - SAMPLE_IP must have 0 skid
1439 *
1440 * See also PERF_RECORD_MISC_EXACT_IP
1441 */
1442 if (precise > 3)
1443 return -EINVAL;
ceb53fbf 1444
f5b1135b
JO
1445 mod->eu = eu;
1446 mod->ek = ek;
1447 mod->eh = eh;
1448 mod->eH = eH;
1449 mod->eG = eG;
a1e12da4 1450 mod->eI = eI;
f5b1135b 1451 mod->precise = precise;
7f94af7a 1452 mod->precise_max = precise_max;
f5b1135b 1453 mod->exclude_GH = exclude_GH;
3c176311 1454 mod->sample_read = sample_read;
e9a7c414
ME
1455 mod->pinned = pinned;
1456
f5b1135b
JO
1457 return 0;
1458}
1459
534123f4
JO
1460/*
1461 * Basic modifier sanity check to validate it contains only one
1462 * instance of any modifier (apart from 'p') present.
1463 */
1464static int check_modifier(char *str)
1465{
1466 char *p = str;
1467
1468 /* The sizeof includes 0 byte as well. */
7f94af7a 1469 if (strlen(str) > (sizeof("ukhGHpppPSDI") - 1))
534123f4
JO
1470 return -1;
1471
1472 while (*p) {
1473 if (*p != 'p' && strchr(p + 1, *p))
1474 return -1;
1475 p++;
1476 }
1477
1478 return 0;
1479}
1480
f5b1135b
JO
1481int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1482{
1483 struct perf_evsel *evsel;
1484 struct event_modifier mod;
1485
1486 if (str == NULL)
1487 return 0;
1488
534123f4
JO
1489 if (check_modifier(str))
1490 return -EINVAL;
1491
f5b1135b
JO
1492 if (!add && get_event_modifier(&mod, str, NULL))
1493 return -EINVAL;
1494
e5cadb93 1495 __evlist__for_each_entry(list, evsel) {
f5b1135b
JO
1496 if (add && get_event_modifier(&mod, str, evsel))
1497 return -EINVAL;
1498
1499 evsel->attr.exclude_user = mod.eu;
1500 evsel->attr.exclude_kernel = mod.ek;
1501 evsel->attr.exclude_hv = mod.eh;
1502 evsel->attr.precise_ip = mod.precise;
1503 evsel->attr.exclude_host = mod.eH;
1504 evsel->attr.exclude_guest = mod.eG;
a1e12da4 1505 evsel->attr.exclude_idle = mod.eI;
f5b1135b 1506 evsel->exclude_GH = mod.exclude_GH;
3c176311 1507 evsel->sample_read = mod.sample_read;
7f94af7a 1508 evsel->precise_max = mod.precise_max;
e9a7c414
ME
1509
1510 if (perf_evsel__is_group_leader(evsel))
1511 evsel->attr.pinned = mod.pinned;
89812fc8 1512 }
ceb53fbf 1513
61c45981
PM
1514 return 0;
1515}
8ad8db37 1516
ac2ba9f3
RR
1517int parse_events_name(struct list_head *list, char *name)
1518{
1519 struct perf_evsel *evsel;
1520
e5cadb93 1521 __evlist__for_each_entry(list, evsel) {
ac2ba9f3
RR
1522 if (!evsel->name)
1523 evsel->name = strdup(name);
1524 }
1525
1526 return 0;
1527}
1528
dcb4e102
KL
1529static int
1530comp_pmu(const void *p1, const void *p2)
1531{
1532 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
1533 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
1534
e312bcf1 1535 return strcasecmp(pmu1->symbol, pmu2->symbol);
dcb4e102
KL
1536}
1537
1538static void perf_pmu__parse_cleanup(void)
1539{
1540 if (perf_pmu_events_list_num > 0) {
1541 struct perf_pmu_event_symbol *p;
1542 int i;
1543
1544 for (i = 0; i < perf_pmu_events_list_num; i++) {
1545 p = perf_pmu_events_list + i;
360e071b 1546 zfree(&p->symbol);
dcb4e102 1547 }
506fde11 1548 zfree(&perf_pmu_events_list);
dcb4e102
KL
1549 perf_pmu_events_list_num = 0;
1550 }
1551}
1552
1553#define SET_SYMBOL(str, stype) \
1554do { \
1555 p->symbol = str; \
1556 if (!p->symbol) \
1557 goto err; \
1558 p->type = stype; \
1559} while (0)
1560
1561/*
1562 * Read the pmu events list from sysfs
1563 * Save it into perf_pmu_events_list
1564 */
1565static void perf_pmu__parse_init(void)
1566{
1567
1568 struct perf_pmu *pmu = NULL;
1569 struct perf_pmu_alias *alias;
1570 int len = 0;
1571
231bb2aa
AK
1572 pmu = NULL;
1573 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1574 list_for_each_entry(alias, &pmu->aliases, list) {
1575 if (strchr(alias->name, '-'))
1576 len++;
1577 len++;
1578 }
1579 }
1580
1581 if (len == 0) {
dcb4e102
KL
1582 perf_pmu_events_list_num = -1;
1583 return;
1584 }
dcb4e102
KL
1585 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1586 if (!perf_pmu_events_list)
1587 return;
1588 perf_pmu_events_list_num = len;
1589
1590 len = 0;
231bb2aa
AK
1591 pmu = NULL;
1592 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1593 list_for_each_entry(alias, &pmu->aliases, list) {
1594 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1595 char *tmp = strchr(alias->name, '-');
1596
1597 if (tmp != NULL) {
1598 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1599 PMU_EVENT_SYMBOL_PREFIX);
1600 p++;
1601 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1602 len += 2;
1603 } else {
1604 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1605 len++;
1606 }
dcb4e102
KL
1607 }
1608 }
1609 qsort(perf_pmu_events_list, len,
1610 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1611
1612 return;
1613err:
1614 perf_pmu__parse_cleanup();
1615}
1616
1617enum perf_pmu_event_symbol_type
1618perf_pmu__parse_check(const char *name)
1619{
1620 struct perf_pmu_event_symbol p, *r;
1621
1622 /* scan kernel pmu events from sysfs if needed */
1623 if (perf_pmu_events_list_num == 0)
1624 perf_pmu__parse_init();
1625 /*
1626 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1627 * cpu-cycles has been handled by hardcode.
1628 * So it must be cpu// events, not kernel pmu event.
1629 */
1630 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1631 return PMU_EVENT_SYMBOL_ERR;
1632
1633 p.symbol = strdup(name);
1634 r = bsearch(&p, perf_pmu_events_list,
1635 (size_t) perf_pmu_events_list_num,
1636 sizeof(struct perf_pmu_event_symbol), comp_pmu);
360e071b 1637 zfree(&p.symbol);
dcb4e102
KL
1638 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1639}
1640
5d9cdc11 1641static int parse_events__scanner(const char *str, void *parse_state, int start_token)
61c45981 1642{
89812fc8 1643 YY_BUFFER_STATE buffer;
ac20de6f 1644 void *scanner;
46010ab2 1645 int ret;
bcd3279f 1646
90e2b22d 1647 ret = parse_events_lex_init_extra(start_token, &scanner);
ac20de6f
ZY
1648 if (ret)
1649 return ret;
1650
1651 buffer = parse_events__scan_string(str, scanner);
a21ca2ca 1652
82ba1f2f
JO
1653#ifdef PARSER_DEBUG
1654 parse_events_debug = 1;
1655#endif
5d9cdc11 1656 ret = parse_events_parse(parse_state, scanner);
ac20de6f
ZY
1657
1658 parse_events__flush_buffer(buffer, scanner);
1659 parse_events__delete_buffer(buffer, scanner);
1660 parse_events_lex_destroy(scanner);
1661 return ret;
1662}
bcd3279f 1663
90e2b22d
JO
1664/*
1665 * parse event config string, return a list of event terms.
1666 */
1667int parse_events_terms(struct list_head *terms, const char *str)
1668{
5d9cdc11 1669 struct parse_events_state parse_state = {
90e2b22d
JO
1670 .terms = NULL,
1671 };
1672 int ret;
1673
5d9cdc11 1674 ret = parse_events__scanner(str, &parse_state, PE_START_TERMS);
90e2b22d 1675 if (!ret) {
5d9cdc11
ACM
1676 list_splice(parse_state.terms, terms);
1677 zfree(&parse_state.terms);
90e2b22d
JO
1678 return 0;
1679 }
1680
5d9cdc11 1681 parse_events_terms__delete(parse_state.terms);
90e2b22d
JO
1682 return ret;
1683}
1684
b39b8393
JO
1685int parse_events(struct perf_evlist *evlist, const char *str,
1686 struct parse_events_error *err)
ac20de6f 1687{
5d9cdc11
ACM
1688 struct parse_events_state parse_state = {
1689 .list = LIST_HEAD_INIT(parse_state.list),
7630b3e2
WN
1690 .idx = evlist->nr_entries,
1691 .error = err,
1692 .evlist = evlist,
ac20de6f
ZY
1693 };
1694 int ret;
bcd3279f 1695
5d9cdc11 1696 ret = parse_events__scanner(str, &parse_state, PE_START_EVENTS);
dcb4e102 1697 perf_pmu__parse_cleanup();
89812fc8 1698 if (!ret) {
15bfd2cc
WN
1699 struct perf_evsel *last;
1700
5d9cdc11 1701 if (list_empty(&parse_state.list)) {
854f7363
WN
1702 WARN_ONCE(true, "WARNING: event parser found nothing");
1703 return -1;
1704 }
1705
5d9cdc11
ACM
1706 perf_evlist__splice_list_tail(evlist, &parse_state.list);
1707 evlist->nr_groups += parse_state.nr_groups;
15bfd2cc
WN
1708 last = perf_evlist__last(evlist);
1709 last->cmdline_group_boundary = true;
1710
89812fc8
JO
1711 return 0;
1712 }
bcd3279f 1713
5d7be90e
JO
1714 /*
1715 * There are 2 users - builtin-record and builtin-test objects.
1716 * Both call perf_evlist__delete in case of error, so we dont
1717 * need to bother.
1718 */
bcd3279f 1719 return ret;
8ad8db37
IM
1720}
1721
b39b8393
JO
1722#define MAX_WIDTH 1000
1723static int get_term_width(void)
1724{
1725 struct winsize ws;
1726
1727 get_term_dimensions(&ws);
1728 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1729}
1730
1731static void parse_events_print_error(struct parse_events_error *err,
1732 const char *event)
1733{
1734 const char *str = "invalid or unsupported event: ";
1735 char _buf[MAX_WIDTH];
1736 char *buf = (char *) event;
1737 int idx = 0;
1738
1739 if (err->str) {
1740 /* -2 for extra '' in the final fprintf */
1741 int width = get_term_width() - 2;
1742 int len_event = strlen(event);
1743 int len_str, max_len, cut = 0;
1744
1745 /*
1746 * Maximum error index indent, we will cut
1747 * the event string if it's bigger.
1748 */
141b2d31 1749 int max_err_idx = 13;
b39b8393
JO
1750
1751 /*
1752 * Let's be specific with the message when
1753 * we have the precise error.
1754 */
1755 str = "event syntax error: ";
1756 len_str = strlen(str);
1757 max_len = width - len_str;
1758
1759 buf = _buf;
1760
bd1a0be5 1761 /* We're cutting from the beginning. */
b39b8393
JO
1762 if (err->idx > max_err_idx)
1763 cut = err->idx - max_err_idx;
1764
1765 strncpy(buf, event + cut, max_len);
1766
1767 /* Mark cut parts with '..' on both sides. */
1768 if (cut)
1769 buf[0] = buf[1] = '.';
1770
1771 if ((len_event - cut) > max_len) {
1772 buf[max_len - 1] = buf[max_len - 2] = '.';
1773 buf[max_len] = 0;
1774 }
1775
1776 idx = len_str + err->idx - cut;
1777 }
1778
1779 fprintf(stderr, "%s'%s'\n", str, buf);
1780 if (idx) {
1781 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
1782 if (err->help)
1783 fprintf(stderr, "\n%s\n", err->help);
360e071b
TS
1784 zfree(&err->str);
1785 zfree(&err->help);
b39b8393
JO
1786 }
1787
1788 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
1789}
1790
1791#undef MAX_WIDTH
1792
f120f9d5 1793int parse_events_option(const struct option *opt, const char *str,
1d037ca1 1794 int unset __maybe_unused)
f120f9d5
JO
1795{
1796 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
b39b8393
JO
1797 struct parse_events_error err = { .idx = 0, };
1798 int ret = parse_events(evlist, str, &err);
1799
1800 if (ret)
1801 parse_events_print_error(&err, str);
9175ce1f 1802
9175ce1f 1803 return ret;
f120f9d5
JO
1804}
1805
4ba1faa1
WN
1806static int
1807foreach_evsel_in_last_glob(struct perf_evlist *evlist,
1808 int (*func)(struct perf_evsel *evsel,
1809 const void *arg),
1810 const void *arg)
c171b552 1811{
69aad6f1 1812 struct perf_evsel *last = NULL;
4ba1faa1 1813 int err;
c171b552 1814
854f7363
WN
1815 /*
1816 * Don't return when list_empty, give func a chance to report
1817 * error when it found last == NULL.
1818 *
1819 * So no need to WARN here, let *func do this.
1820 */
361c99a6 1821 if (evlist->nr_entries > 0)
0c21f736 1822 last = perf_evlist__last(evlist);
69aad6f1 1823
15bfd2cc 1824 do {
4ba1faa1
WN
1825 err = (*func)(last, arg);
1826 if (err)
15bfd2cc 1827 return -1;
4ba1faa1
WN
1828 if (!last)
1829 return 0;
15bfd2cc
WN
1830
1831 if (last->node.prev == &evlist->entries)
1832 return 0;
1833 last = list_entry(last->node.prev, struct perf_evsel, node);
1834 } while (!last->cmdline_group_boundary);
c171b552
LZ
1835
1836 return 0;
1837}
1838
4ba1faa1
WN
1839static int set_filter(struct perf_evsel *evsel, const void *arg)
1840{
1841 const char *str = arg;
1e857484
MP
1842 bool found = false;
1843 int nr_addr_filters = 0;
1844 struct perf_pmu *pmu = NULL;
4ba1faa1 1845
1e857484
MP
1846 if (evsel == NULL)
1847 goto err;
1848
1849 if (evsel->attr.type == PERF_TYPE_TRACEPOINT) {
1850 if (perf_evsel__append_tp_filter(evsel, str) < 0) {
1851 fprintf(stderr,
1852 "not enough memory to hold filter string\n");
1853 return -1;
1854 }
1855
1856 return 0;
4ba1faa1
WN
1857 }
1858
1e857484
MP
1859 while ((pmu = perf_pmu__scan(pmu)) != NULL)
1860 if (pmu->type == evsel->attr.type) {
1861 found = true;
1862 break;
1863 }
1864
1865 if (found)
1866 perf_pmu__scan_file(pmu, "nr_addr_filters",
1867 "%d", &nr_addr_filters);
1868
1869 if (!nr_addr_filters)
1870 goto err;
1871
1872 if (perf_evsel__append_addr_filter(evsel, str) < 0) {
4ba1faa1
WN
1873 fprintf(stderr,
1874 "not enough memory to hold filter string\n");
1875 return -1;
1876 }
1877
1878 return 0;
1e857484
MP
1879
1880err:
1881 fprintf(stderr,
1882 "--filter option should follow a -e tracepoint or HW tracer option\n");
1883
1884 return -1;
4ba1faa1
WN
1885}
1886
1887int parse_filter(const struct option *opt, const char *str,
1888 int unset __maybe_unused)
1889{
1890 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1891
1892 return foreach_evsel_in_last_glob(evlist, set_filter,
1893 (const void *)str);
1894}
1895
1896static int add_exclude_perf_filter(struct perf_evsel *evsel,
1897 const void *arg __maybe_unused)
1898{
1899 char new_filter[64];
1900
1901 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
1902 fprintf(stderr,
1903 "--exclude-perf option should follow a -e tracepoint option\n");
1904 return -1;
1905 }
1906
1907 snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
1908
3541c034 1909 if (perf_evsel__append_tp_filter(evsel, new_filter) < 0) {
4ba1faa1
WN
1910 fprintf(stderr,
1911 "not enough memory to hold filter string\n");
1912 return -1;
1913 }
1914
1915 return 0;
1916}
1917
1918int exclude_perf(const struct option *opt,
1919 const char *arg __maybe_unused,
1920 int unset __maybe_unused)
1921{
1922 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1923
1924 return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
1925 NULL);
1926}
1927
86847b62 1928static const char * const event_type_descriptors[] = {
86847b62
TG
1929 "Hardware event",
1930 "Software event",
1931 "Tracepoint event",
1932 "Hardware cache event",
41bdcb23
LW
1933 "Raw hardware event descriptor",
1934 "Hardware breakpoint",
86847b62
TG
1935};
1936
ab0e4800
YS
1937static int cmp_string(const void *a, const void *b)
1938{
1939 const char * const *as = a;
1940 const char * const *bs = b;
1941
1942 return strcmp(*as, *bs);
1943}
1944
f6bdafef
JB
1945/*
1946 * Print the events from <debugfs_mount_point>/tracing/events
1947 */
1948
a3277d2d
FW
1949void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
1950 bool name_only)
f6bdafef
JB
1951{
1952 DIR *sys_dir, *evt_dir;
22a9f41b 1953 struct dirent *sys_dirent, *evt_dirent;
f6bdafef 1954 char evt_path[MAXPATHLEN];
725b1368 1955 char dir_path[MAXPATHLEN];
ab0e4800
YS
1956 char **evt_list = NULL;
1957 unsigned int evt_i = 0, evt_num = 0;
1958 bool evt_num_known = false;
f6bdafef 1959
ab0e4800 1960restart:
ebf294bf 1961 sys_dir = opendir(tracing_events_path);
f6bdafef 1962 if (!sys_dir)
725b1368 1963 return;
6b58e7f1 1964
ab0e4800
YS
1965 if (evt_num_known) {
1966 evt_list = zalloc(sizeof(char *) * evt_num);
1967 if (!evt_list)
1968 goto out_close_sys_dir;
1969 }
1970
22a9f41b 1971 for_each_subsystem(sys_dir, sys_dirent) {
48000a1a 1972 if (subsys_glob != NULL &&
22a9f41b 1973 !strglobmatch(sys_dirent->d_name, subsys_glob))
668b8788 1974 continue;
725b1368 1975
ebf294bf 1976 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
22a9f41b 1977 sys_dirent->d_name);
725b1368
ED
1978 evt_dir = opendir(dir_path);
1979 if (!evt_dir)
6b58e7f1 1980 continue;
725b1368 1981
22a9f41b 1982 for_each_event(sys_dirent, evt_dir, evt_dirent) {
48000a1a 1983 if (event_glob != NULL &&
22a9f41b 1984 !strglobmatch(evt_dirent->d_name, event_glob))
668b8788
ACM
1985 continue;
1986
ab0e4800
YS
1987 if (!evt_num_known) {
1988 evt_num++;
a3277d2d
FW
1989 continue;
1990 }
1991
f6bdafef 1992 snprintf(evt_path, MAXPATHLEN, "%s:%s",
22a9f41b 1993 sys_dirent->d_name, evt_dirent->d_name);
ab0e4800
YS
1994
1995 evt_list[evt_i] = strdup(evt_path);
1996 if (evt_list[evt_i] == NULL)
1997 goto out_close_evt_dir;
1998 evt_i++;
f6bdafef
JB
1999 }
2000 closedir(evt_dir);
2001 }
f6bdafef 2002 closedir(sys_dir);
ab0e4800
YS
2003
2004 if (!evt_num_known) {
2005 evt_num_known = true;
2006 goto restart;
2007 }
2008 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2009 evt_i = 0;
2010 while (evt_i < evt_num) {
2011 if (name_only) {
2012 printf("%s ", evt_list[evt_i++]);
2013 continue;
2014 }
2015 printf(" %-50s [%s]\n", evt_list[evt_i++],
2016 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2017 }
dfc431cb 2018 if (evt_num && pager_in_use())
ab0e4800
YS
2019 printf("\n");
2020
2021out_free:
2022 evt_num = evt_i;
2023 for (evt_i = 0; evt_i < evt_num; evt_i++)
2024 zfree(&evt_list[evt_i]);
2025 zfree(&evt_list);
2026 return;
2027
2028out_close_evt_dir:
2029 closedir(evt_dir);
2030out_close_sys_dir:
2031 closedir(sys_dir);
2032
2033 printf("FATAL: not enough memory to print %s\n",
2034 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2035 if (evt_list)
2036 goto out_free;
f6bdafef
JB
2037}
2038
20c457b8
TR
2039/*
2040 * Check whether event is in <debugfs_mount_point>/tracing/events
2041 */
2042
2043int is_valid_tracepoint(const char *event_string)
2044{
2045 DIR *sys_dir, *evt_dir;
22a9f41b 2046 struct dirent *sys_dirent, *evt_dirent;
20c457b8
TR
2047 char evt_path[MAXPATHLEN];
2048 char dir_path[MAXPATHLEN];
2049
ebf294bf 2050 sys_dir = opendir(tracing_events_path);
20c457b8
TR
2051 if (!sys_dir)
2052 return 0;
2053
22a9f41b 2054 for_each_subsystem(sys_dir, sys_dirent) {
20c457b8 2055
ebf294bf 2056 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
22a9f41b 2057 sys_dirent->d_name);
20c457b8
TR
2058 evt_dir = opendir(dir_path);
2059 if (!evt_dir)
2060 continue;
2061
22a9f41b 2062 for_each_event(sys_dirent, evt_dir, evt_dirent) {
20c457b8 2063 snprintf(evt_path, MAXPATHLEN, "%s:%s",
22a9f41b 2064 sys_dirent->d_name, evt_dirent->d_name);
20c457b8
TR
2065 if (!strcmp(evt_path, event_string)) {
2066 closedir(evt_dir);
2067 closedir(sys_dir);
2068 return 1;
2069 }
2070 }
2071 closedir(evt_dir);
2072 }
2073 closedir(sys_dir);
2074 return 0;
2075}
2076
b41f1cec
NK
2077static bool is_event_supported(u8 type, unsigned config)
2078{
2079 bool ret = true;
88fee52e 2080 int open_return;
b41f1cec
NK
2081 struct perf_evsel *evsel;
2082 struct perf_event_attr attr = {
2083 .type = type,
2084 .config = config,
2085 .disabled = 1,
b41f1cec 2086 };
89896051
ACM
2087 struct thread_map *tmap = thread_map__new_by_tid(0);
2088
2089 if (tmap == NULL)
2090 return false;
b41f1cec 2091
ef503831 2092 evsel = perf_evsel__new(&attr);
b41f1cec 2093 if (evsel) {
89896051 2094 open_return = perf_evsel__open(evsel, NULL, tmap);
88fee52e
VW
2095 ret = open_return >= 0;
2096
2097 if (open_return == -EACCES) {
2098 /*
2099 * This happens if the paranoid value
2100 * /proc/sys/kernel/perf_event_paranoid is set to 2
2101 * Re-run with exclude_kernel set; we don't do that
2102 * by default as some ARM machines do not support it.
2103 *
2104 */
2105 evsel->attr.exclude_kernel = 1;
89896051 2106 ret = perf_evsel__open(evsel, NULL, tmap) >= 0;
88fee52e 2107 }
b41f1cec
NK
2108 perf_evsel__delete(evsel);
2109 }
2110
2111 return ret;
2112}
2113
40218dae
MH
2114void print_sdt_events(const char *subsys_glob, const char *event_glob,
2115 bool name_only)
2116{
2117 struct probe_cache *pcache;
2118 struct probe_cache_entry *ent;
2119 struct strlist *bidlist, *sdtlist;
2120 struct strlist_config cfg = {.dont_dupstr = true};
2121 struct str_node *nd, *nd2;
2122 char *buf, *path, *ptr = NULL;
2123 bool show_detail = false;
2124 int ret;
2125
2126 sdtlist = strlist__new(NULL, &cfg);
2127 if (!sdtlist) {
2128 pr_debug("Failed to allocate new strlist for SDT\n");
2129 return;
2130 }
2131 bidlist = build_id_cache__list_all(true);
2132 if (!bidlist) {
2133 pr_debug("Failed to get buildids: %d\n", errno);
2134 return;
2135 }
2136 strlist__for_each_entry(nd, bidlist) {
f045b8c4 2137 pcache = probe_cache__new(nd->s, NULL);
40218dae
MH
2138 if (!pcache)
2139 continue;
2140 list_for_each_entry(ent, &pcache->entries, node) {
2141 if (!ent->sdt)
2142 continue;
2143 if (subsys_glob &&
2144 !strglobmatch(ent->pev.group, subsys_glob))
2145 continue;
2146 if (event_glob &&
2147 !strglobmatch(ent->pev.event, event_glob))
2148 continue;
2149 ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
2150 ent->pev.event, nd->s);
2151 if (ret > 0)
2152 strlist__add(sdtlist, buf);
2153 }
2154 probe_cache__delete(pcache);
2155 }
2156 strlist__delete(bidlist);
2157
2158 strlist__for_each_entry(nd, sdtlist) {
2159 buf = strchr(nd->s, '@');
2160 if (buf)
2161 *(buf++) = '\0';
2162 if (name_only) {
2163 printf("%s ", nd->s);
2164 continue;
2165 }
2166 nd2 = strlist__next(nd);
2167 if (nd2) {
2168 ptr = strchr(nd2->s, '@');
2169 if (ptr)
2170 *ptr = '\0';
2171 if (strcmp(nd->s, nd2->s) == 0)
2172 show_detail = true;
2173 }
2174 if (show_detail) {
2175 path = build_id_cache__origname(buf);
2176 ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf);
2177 if (ret > 0) {
2178 printf(" %-50s [%s]\n", buf, "SDT event");
2179 free(buf);
2180 }
2181 } else
2182 printf(" %-50s [%s]\n", nd->s, "SDT event");
2183 if (nd2) {
2184 if (strcmp(nd->s, nd2->s) != 0)
2185 show_detail = false;
2186 if (ptr)
2187 *ptr = '@';
2188 }
2189 }
2190 strlist__delete(sdtlist);
2191}
2192
a3277d2d 2193int print_hwcache_events(const char *event_glob, bool name_only)
668b8788 2194{
ab0e4800 2195 unsigned int type, op, i, evt_i = 0, evt_num = 0;
0b668bc9 2196 char name[64];
ab0e4800
YS
2197 char **evt_list = NULL;
2198 bool evt_num_known = false;
2199
2200restart:
2201 if (evt_num_known) {
2202 evt_list = zalloc(sizeof(char *) * evt_num);
2203 if (!evt_list)
2204 goto out_enomem;
2205 }
668b8788
ACM
2206
2207 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
2208 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
2209 /* skip invalid cache type */
0b668bc9 2210 if (!perf_evsel__is_cache_op_valid(type, op))
668b8788
ACM
2211 continue;
2212
2213 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
0b668bc9
ACM
2214 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
2215 name, sizeof(name));
947b4ad1 2216 if (event_glob != NULL && !strglobmatch(name, event_glob))
668b8788
ACM
2217 continue;
2218
b41f1cec
NK
2219 if (!is_event_supported(PERF_TYPE_HW_CACHE,
2220 type | (op << 8) | (i << 16)))
2221 continue;
2222
ab0e4800
YS
2223 if (!evt_num_known) {
2224 evt_num++;
2225 continue;
2226 }
2227
2228 evt_list[evt_i] = strdup(name);
2229 if (evt_list[evt_i] == NULL)
2230 goto out_enomem;
2231 evt_i++;
668b8788
ACM
2232 }
2233 }
2234 }
2235
ab0e4800
YS
2236 if (!evt_num_known) {
2237 evt_num_known = true;
2238 goto restart;
2239 }
2240 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2241 evt_i = 0;
2242 while (evt_i < evt_num) {
2243 if (name_only) {
2244 printf("%s ", evt_list[evt_i++]);
2245 continue;
2246 }
2247 printf(" %-50s [%s]\n", evt_list[evt_i++],
2248 event_type_descriptors[PERF_TYPE_HW_CACHE]);
2249 }
dfc431cb 2250 if (evt_num && pager_in_use())
dc098b35 2251 printf("\n");
ab0e4800
YS
2252
2253out_free:
2254 evt_num = evt_i;
2255 for (evt_i = 0; evt_i < evt_num; evt_i++)
2256 zfree(&evt_list[evt_i]);
2257 zfree(&evt_list);
2258 return evt_num;
2259
2260out_enomem:
2261 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
2262 if (evt_list)
2263 goto out_free;
2264 return evt_num;
668b8788
ACM
2265}
2266
705750f2 2267void print_symbol_events(const char *event_glob, unsigned type,
a3277d2d
FW
2268 struct event_symbol *syms, unsigned max,
2269 bool name_only)
8ad8db37 2270{
ab0e4800 2271 unsigned int i, evt_i = 0, evt_num = 0;
947b4ad1 2272 char name[MAX_NAME_LEN];
ab0e4800
YS
2273 char **evt_list = NULL;
2274 bool evt_num_known = false;
2275
2276restart:
2277 if (evt_num_known) {
2278 evt_list = zalloc(sizeof(char *) * evt_num);
2279 if (!evt_list)
2280 goto out_enomem;
2281 syms -= max;
2282 }
8ad8db37 2283
1dc12760 2284 for (i = 0; i < max; i++, syms++) {
668b8788 2285
e37df6c7 2286 if (event_glob != NULL && syms->symbol != NULL &&
668b8788
ACM
2287 !(strglobmatch(syms->symbol, event_glob) ||
2288 (syms->alias && strglobmatch(syms->alias, event_glob))))
2289 continue;
8ad8db37 2290
b41f1cec
NK
2291 if (!is_event_supported(type, i))
2292 continue;
2293
ab0e4800
YS
2294 if (!evt_num_known) {
2295 evt_num++;
a3277d2d
FW
2296 continue;
2297 }
2298
ab0e4800 2299 if (!name_only && strlen(syms->alias))
947b4ad1 2300 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
74d5b588 2301 else
947b4ad1 2302 strncpy(name, syms->symbol, MAX_NAME_LEN);
8ad8db37 2303
ab0e4800
YS
2304 evt_list[evt_i] = strdup(name);
2305 if (evt_list[evt_i] == NULL)
2306 goto out_enomem;
2307 evt_i++;
8ad8db37
IM
2308 }
2309
ab0e4800
YS
2310 if (!evt_num_known) {
2311 evt_num_known = true;
2312 goto restart;
2313 }
2314 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2315 evt_i = 0;
2316 while (evt_i < evt_num) {
2317 if (name_only) {
2318 printf("%s ", evt_list[evt_i++]);
2319 continue;
2320 }
2321 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
2322 }
dfc431cb 2323 if (evt_num && pager_in_use())
668b8788 2324 printf("\n");
ab0e4800
YS
2325
2326out_free:
2327 evt_num = evt_i;
2328 for (evt_i = 0; evt_i < evt_num; evt_i++)
2329 zfree(&evt_list[evt_i]);
2330 zfree(&evt_list);
2331 return;
2332
2333out_enomem:
2334 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
2335 if (evt_list)
2336 goto out_free;
1dc12760
JO
2337}
2338
2339/*
2340 * Print the help text for the event symbols:
2341 */
c8d6828a 2342void print_events(const char *event_glob, bool name_only, bool quiet_flag,
bf874fcf 2343 bool long_desc, bool details_flag)
1dc12760 2344{
1dc12760 2345 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
a3277d2d 2346 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
1dc12760
JO
2347
2348 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
a3277d2d 2349 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
1dc12760 2350
a3277d2d 2351 print_hwcache_events(event_glob, name_only);
668b8788 2352
bf874fcf
AK
2353 print_pmu_events(event_glob, name_only, quiet_flag, long_desc,
2354 details_flag);
dc098b35 2355
668b8788
ACM
2356 if (event_glob != NULL)
2357 return;
73c24cb8 2358
a3277d2d 2359 if (!name_only) {
a3277d2d
FW
2360 printf(" %-50s [%s]\n",
2361 "rNNN",
2362 event_type_descriptors[PERF_TYPE_RAW]);
2363 printf(" %-50s [%s]\n",
2364 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
2365 event_type_descriptors[PERF_TYPE_RAW]);
dfc431cb
ACM
2366 if (pager_in_use())
2367 printf(" (see 'man perf-list' on how to encode it)\n\n");
a3277d2d
FW
2368
2369 printf(" %-50s [%s]\n",
3741eb9f 2370 "mem:<addr>[/len][:access]",
41bdcb23 2371 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
dfc431cb
ACM
2372 if (pager_in_use())
2373 printf("\n");
a3277d2d 2374 }
1b290d67 2375
a3277d2d 2376 print_tracepoint_events(NULL, NULL, name_only);
40218dae
MH
2377
2378 print_sdt_events(NULL, NULL, name_only);
8ad8db37 2379}
8f707d84 2380
6cee6cd3 2381int parse_events__is_hardcoded_term(struct parse_events_term *term)
8f707d84 2382{
16fa7e82 2383 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
8f707d84
JO
2384}
2385
67b49b38
JO
2386static int new_term(struct parse_events_term **_term,
2387 struct parse_events_term *temp,
2388 char *str, u64 num)
8f707d84 2389{
6cee6cd3 2390 struct parse_events_term *term;
8f707d84 2391
67b49b38 2392 term = malloc(sizeof(*term));
8f707d84
JO
2393 if (!term)
2394 return -ENOMEM;
2395
67b49b38 2396 *term = *temp;
8f707d84 2397 INIT_LIST_HEAD(&term->list);
8f707d84 2398
67b49b38 2399 switch (term->type_val) {
8f707d84
JO
2400 case PARSE_EVENTS__TERM_TYPE_NUM:
2401 term->val.num = num;
2402 break;
2403 case PARSE_EVENTS__TERM_TYPE_STR:
2404 term->val.str = str;
2405 break;
2406 default:
4be8be6b 2407 free(term);
8f707d84
JO
2408 return -EINVAL;
2409 }
2410
2411 *_term = term;
2412 return 0;
2413}
2414
6cee6cd3 2415int parse_events_term__num(struct parse_events_term **term,
cecf3a2e 2416 int type_term, char *config, u64 num,
99e7138e 2417 bool no_value,
bb78ce7d 2418 void *loc_term_, void *loc_val_)
16fa7e82 2419{
bb78ce7d
AH
2420 YYLTYPE *loc_term = loc_term_;
2421 YYLTYPE *loc_val = loc_val_;
2422
67b49b38
JO
2423 struct parse_events_term temp = {
2424 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
2425 .type_term = type_term,
2426 .config = config,
99e7138e 2427 .no_value = no_value,
67b49b38
JO
2428 .err_term = loc_term ? loc_term->first_column : 0,
2429 .err_val = loc_val ? loc_val->first_column : 0,
2430 };
2431
2432 return new_term(term, &temp, NULL, num);
16fa7e82
JO
2433}
2434
6cee6cd3 2435int parse_events_term__str(struct parse_events_term **term,
cecf3a2e 2436 int type_term, char *config, char *str,
bb78ce7d 2437 void *loc_term_, void *loc_val_)
16fa7e82 2438{
bb78ce7d
AH
2439 YYLTYPE *loc_term = loc_term_;
2440 YYLTYPE *loc_val = loc_val_;
2441
67b49b38
JO
2442 struct parse_events_term temp = {
2443 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
2444 .type_term = type_term,
2445 .config = config,
2446 .err_term = loc_term ? loc_term->first_column : 0,
2447 .err_val = loc_val ? loc_val->first_column : 0,
2448 };
2449
2450 return new_term(term, &temp, str, 0);
16fa7e82
JO
2451}
2452
6cee6cd3 2453int parse_events_term__sym_hw(struct parse_events_term **term,
1d33d6dc
JO
2454 char *config, unsigned idx)
2455{
2456 struct event_symbol *sym;
67b49b38
JO
2457 struct parse_events_term temp = {
2458 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
2459 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
2460 .config = config ?: (char *) "event",
2461 };
1d33d6dc
JO
2462
2463 BUG_ON(idx >= PERF_COUNT_HW_MAX);
2464 sym = &event_symbols_hw[idx];
2465
67b49b38 2466 return new_term(term, &temp, (char *) sym->symbol, 0);
1d33d6dc
JO
2467}
2468
6cee6cd3
ACM
2469int parse_events_term__clone(struct parse_events_term **new,
2470 struct parse_events_term *term)
a6146d50 2471{
67b49b38
JO
2472 struct parse_events_term temp = {
2473 .type_val = term->type_val,
2474 .type_term = term->type_term,
2475 .config = term->config,
2476 .err_term = term->err_term,
2477 .err_val = term->err_val,
2478 };
2479
2480 return new_term(new, &temp, term->val.str, term->val.num);
a6146d50
ZY
2481}
2482
8255718f
AK
2483int parse_events_copy_term_list(struct list_head *old,
2484 struct list_head **new)
2485{
2486 struct parse_events_term *term, *n;
2487 int ret;
2488
2489 if (!old) {
2490 *new = NULL;
2491 return 0;
2492 }
2493
2494 *new = malloc(sizeof(struct list_head));
2495 if (!*new)
2496 return -ENOMEM;
2497 INIT_LIST_HEAD(*new);
2498
2499 list_for_each_entry (term, old, list) {
2500 ret = parse_events_term__clone(&n, term);
2501 if (ret)
2502 return ret;
2503 list_add_tail(&n->list, *new);
2504 }
2505 return 0;
2506}
2507
fc0a2c1d 2508void parse_events_terms__purge(struct list_head *terms)
8f707d84 2509{
6cee6cd3 2510 struct parse_events_term *term, *h;
8f707d84 2511
a8adfceb 2512 list_for_each_entry_safe(term, h, terms, list) {
2d055bf2 2513 if (term->array.nr_ranges)
360e071b 2514 zfree(&term->array.ranges);
a8adfceb 2515 list_del_init(&term->list);
8f707d84 2516 free(term);
a8adfceb 2517 }
8f707d84 2518}
b39b8393 2519
2146afc6 2520void parse_events_terms__delete(struct list_head *terms)
fc0a2c1d 2521{
2146afc6
ACM
2522 if (!terms)
2523 return;
fc0a2c1d 2524 parse_events_terms__purge(terms);
d20a5f2b 2525 free(terms);
fc0a2c1d
ACM
2526}
2527
2d055bf2
WN
2528void parse_events__clear_array(struct parse_events_array *a)
2529{
360e071b 2530 zfree(&a->ranges);
2d055bf2
WN
2531}
2532
5d9cdc11 2533void parse_events_evlist_error(struct parse_events_state *parse_state,
b39b8393
JO
2534 int idx, const char *str)
2535{
5d9cdc11 2536 struct parse_events_error *err = parse_state->error;
b39b8393 2537
a6ced2be
AH
2538 if (!err)
2539 return;
b39b8393
JO
2540 err->idx = idx;
2541 err->str = strdup(str);
2542 WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
2543}
ffeb883e 2544
17cb5f84
WN
2545static void config_terms_list(char *buf, size_t buf_sz)
2546{
2547 int i;
2548 bool first = true;
2549
2550 buf[0] = '\0';
2551 for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
2552 const char *name = config_term_names[i];
2553
1669e509
WN
2554 if (!config_term_avail(i, NULL))
2555 continue;
17cb5f84
WN
2556 if (!name)
2557 continue;
2558 if (name[0] == '<')
2559 continue;
2560
2561 if (strlen(buf) + strlen(name) + 2 >= buf_sz)
2562 return;
2563
2564 if (!first)
2565 strcat(buf, ",");
2566 else
2567 first = false;
2568 strcat(buf, name);
2569 }
2570}
2571
ffeb883e
HK
2572/*
2573 * Return string contains valid config terms of an event.
2574 * @additional_terms: For terms such as PMU sysfs terms.
2575 */
2576char *parse_events_formats_error_string(char *additional_terms)
2577{
2578 char *str;
626a6b78 2579 /* "no-overwrite" is the longest name */
17cb5f84 2580 char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
626a6b78 2581 (sizeof("no-overwrite") - 1)];
ffeb883e 2582
17cb5f84 2583 config_terms_list(static_terms, sizeof(static_terms));
ffeb883e
HK
2584 /* valid terms */
2585 if (additional_terms) {
26dee028
WN
2586 if (asprintf(&str, "valid terms: %s,%s",
2587 additional_terms, static_terms) < 0)
ffeb883e
HK
2588 goto fail;
2589 } else {
26dee028 2590 if (asprintf(&str, "valid terms: %s", static_terms) < 0)
ffeb883e
HK
2591 goto fail;
2592 }
2593 return str;
2594
2595fail:
2596 return NULL;
2597}