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