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