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