perf tools: Add support for event post configuration
[linux-2.6-block.git] / tools / perf / util / parse-events.c
CommitLineData
d2709c7c 1#include <linux/hw_breakpoint.h>
8ad8db37 2#include "util.h"
6b58e7f1 3#include "../perf.h"
361c99a6 4#include "evlist.h"
69aad6f1 5#include "evsel.h"
8ad8db37
IM
6#include "parse-options.h"
7#include "parse-events.h"
8#include "exec_cmd.h"
42f60c2d 9#include "string.h"
5aab621b 10#include "symbol.h"
5beeded1 11#include "cache.h"
8755a8f2 12#include "header.h"
6e81c74c 13#include "debug.h"
553873e1 14#include <api/fs/debugfs.h>
ac20de6f 15#include "parse-events-bison.h"
90e2b22d 16#define YY_EXTRA_TYPE int
89812fc8 17#include "parse-events-flex.h"
5f537a26 18#include "pmu.h"
b41f1cec 19#include "thread_map.h"
f30a79b0 20#include "cpumap.h"
b39b8393 21#include "asm/bug.h"
89812fc8
JO
22
23#define MAX_NAME_LEN 100
8ad8db37 24
82ba1f2f
JO
25#ifdef PARSER_DEBUG
26extern int parse_events_debug;
27#endif
ac20de6f 28int parse_events_parse(void *data, void *scanner);
bcd3279f 29
dcb4e102
KL
30static struct perf_pmu_event_symbol *perf_pmu_events_list;
31/*
32 * The variable indicates the number of supported pmu event symbols.
33 * 0 means not initialized and ready to init
34 * -1 means failed to init, don't try anymore
35 * >0 is the number of supported pmu event symbols
36 */
37static int perf_pmu_events_list_num;
38
705750f2 39struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
1dc12760
JO
40 [PERF_COUNT_HW_CPU_CYCLES] = {
41 .symbol = "cpu-cycles",
42 .alias = "cycles",
43 },
44 [PERF_COUNT_HW_INSTRUCTIONS] = {
45 .symbol = "instructions",
46 .alias = "",
47 },
48 [PERF_COUNT_HW_CACHE_REFERENCES] = {
49 .symbol = "cache-references",
50 .alias = "",
51 },
52 [PERF_COUNT_HW_CACHE_MISSES] = {
53 .symbol = "cache-misses",
54 .alias = "",
55 },
56 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
57 .symbol = "branch-instructions",
58 .alias = "branches",
59 },
60 [PERF_COUNT_HW_BRANCH_MISSES] = {
61 .symbol = "branch-misses",
62 .alias = "",
63 },
64 [PERF_COUNT_HW_BUS_CYCLES] = {
65 .symbol = "bus-cycles",
66 .alias = "",
67 },
68 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
69 .symbol = "stalled-cycles-frontend",
70 .alias = "idle-cycles-frontend",
71 },
72 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
73 .symbol = "stalled-cycles-backend",
74 .alias = "idle-cycles-backend",
75 },
76 [PERF_COUNT_HW_REF_CPU_CYCLES] = {
77 .symbol = "ref-cycles",
78 .alias = "",
79 },
80};
81
705750f2 82struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
1dc12760
JO
83 [PERF_COUNT_SW_CPU_CLOCK] = {
84 .symbol = "cpu-clock",
85 .alias = "",
86 },
87 [PERF_COUNT_SW_TASK_CLOCK] = {
88 .symbol = "task-clock",
89 .alias = "",
90 },
91 [PERF_COUNT_SW_PAGE_FAULTS] = {
92 .symbol = "page-faults",
93 .alias = "faults",
94 },
95 [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
96 .symbol = "context-switches",
97 .alias = "cs",
98 },
99 [PERF_COUNT_SW_CPU_MIGRATIONS] = {
100 .symbol = "cpu-migrations",
101 .alias = "migrations",
102 },
103 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
104 .symbol = "minor-faults",
105 .alias = "",
106 },
107 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
108 .symbol = "major-faults",
109 .alias = "",
110 },
111 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
112 .symbol = "alignment-faults",
113 .alias = "",
114 },
115 [PERF_COUNT_SW_EMULATION_FAULTS] = {
116 .symbol = "emulation-faults",
117 .alias = "",
118 },
d22d1a2a
AH
119 [PERF_COUNT_SW_DUMMY] = {
120 .symbol = "dummy",
121 .alias = "",
122 },
8ad8db37
IM
123};
124
cdd6c482
IM
125#define __PERF_EVENT_FIELD(config, name) \
126 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
5242519b 127
1fc570ad 128#define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
cdd6c482 129#define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
1fc570ad 130#define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
cdd6c482 131#define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
5242519b 132
6b58e7f1 133#define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
f6bdafef 134 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
6b58e7f1 135 if (sys_dirent.d_type == DT_DIR && \
f6bdafef
JB
136 (strcmp(sys_dirent.d_name, ".")) && \
137 (strcmp(sys_dirent.d_name, "..")))
138
ae07b63f
PZ
139static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
140{
141 char evt_path[MAXPATHLEN];
142 int fd;
143
ebf294bf 144 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
ae07b63f
PZ
145 sys_dir->d_name, evt_dir->d_name);
146 fd = open(evt_path, O_RDONLY);
147 if (fd < 0)
148 return -EINVAL;
149 close(fd);
150
151 return 0;
152}
153
6b58e7f1 154#define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
f6bdafef 155 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
6b58e7f1 156 if (evt_dirent.d_type == DT_DIR && \
f6bdafef 157 (strcmp(evt_dirent.d_name, ".")) && \
ae07b63f
PZ
158 (strcmp(evt_dirent.d_name, "..")) && \
159 (!tp_event_has_id(&sys_dirent, &evt_dirent)))
f6bdafef 160
270bbbe8 161#define MAX_EVENT_LENGTH 512
f6bdafef 162
f6bdafef 163
1ef2ed10 164struct tracepoint_path *tracepoint_id_to_path(u64 config)
f6bdafef 165{
1ef2ed10 166 struct tracepoint_path *path = NULL;
f6bdafef
JB
167 DIR *sys_dir, *evt_dir;
168 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
8aa8a7c8 169 char id_buf[24];
725b1368 170 int fd;
f6bdafef
JB
171 u64 id;
172 char evt_path[MAXPATHLEN];
725b1368 173 char dir_path[MAXPATHLEN];
f6bdafef 174
ebf294bf 175 sys_dir = opendir(tracing_events_path);
f6bdafef 176 if (!sys_dir)
725b1368 177 return NULL;
6b58e7f1
UD
178
179 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
725b1368 180
ebf294bf 181 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
725b1368
ED
182 sys_dirent.d_name);
183 evt_dir = opendir(dir_path);
184 if (!evt_dir)
6b58e7f1 185 continue;
725b1368 186
6b58e7f1 187 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
725b1368
ED
188
189 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
f6bdafef 190 evt_dirent.d_name);
725b1368 191 fd = open(evt_path, O_RDONLY);
f6bdafef
JB
192 if (fd < 0)
193 continue;
194 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
195 close(fd);
196 continue;
197 }
198 close(fd);
199 id = atoll(id_buf);
200 if (id == config) {
201 closedir(evt_dir);
202 closedir(sys_dir);
59b4caeb 203 path = zalloc(sizeof(*path));
1ef2ed10
FW
204 path->system = malloc(MAX_EVENT_LENGTH);
205 if (!path->system) {
206 free(path);
207 return NULL;
208 }
209 path->name = malloc(MAX_EVENT_LENGTH);
210 if (!path->name) {
74cf249d 211 zfree(&path->system);
1ef2ed10
FW
212 free(path);
213 return NULL;
214 }
215 strncpy(path->system, sys_dirent.d_name,
216 MAX_EVENT_LENGTH);
217 strncpy(path->name, evt_dirent.d_name,
218 MAX_EVENT_LENGTH);
219 return path;
f6bdafef
JB
220 }
221 }
222 closedir(evt_dir);
223 }
224
f6bdafef 225 closedir(sys_dir);
1ef2ed10
FW
226 return NULL;
227}
228
e7c93f09
NK
229struct tracepoint_path *tracepoint_name_to_path(const char *name)
230{
231 struct tracepoint_path *path = zalloc(sizeof(*path));
232 char *str = strchr(name, ':');
233
234 if (path == NULL || str == NULL) {
235 free(path);
236 return NULL;
237 }
238
239 path->system = strndup(name, str - name);
240 path->name = strdup(str+1);
241
242 if (path->system == NULL || path->name == NULL) {
74cf249d
ACM
243 zfree(&path->system);
244 zfree(&path->name);
e7c93f09
NK
245 free(path);
246 path = NULL;
247 }
248
249 return path;
250}
251
1424dc96
DA
252const char *event_type(int type)
253{
254 switch (type) {
255 case PERF_TYPE_HARDWARE:
256 return "hardware";
257
258 case PERF_TYPE_SOFTWARE:
259 return "software";
260
261 case PERF_TYPE_TRACEPOINT:
262 return "tracepoint";
263
264 case PERF_TYPE_HW_CACHE:
265 return "hardware-cache";
266
267 default:
268 break;
269 }
270
271 return "unknown";
272}
273
7ae92e74
YZ
274
275
410136f5
SE
276static struct perf_evsel *
277__add_event(struct list_head *list, int *idx,
278 struct perf_event_attr *attr,
930a2e29
JO
279 char *name, struct cpu_map *cpus,
280 struct list_head *config_terms)
89812fc8
JO
281{
282 struct perf_evsel *evsel;
283
284 event_attr_init(attr);
285
ef503831 286 evsel = perf_evsel__new_idx(attr, (*idx)++);
c5cd8ac0 287 if (!evsel)
410136f5 288 return NULL;
89812fc8 289
f30a79b0
JO
290 if (cpus)
291 evsel->cpus = cpu_map__get(cpus);
292
9db1763c
ACM
293 if (name)
294 evsel->name = strdup(name);
930a2e29
JO
295
296 if (config_terms)
297 list_splice(config_terms, &evsel->config_terms);
298
b847cbdc 299 list_add_tail(&evsel->node, list);
410136f5 300 return evsel;
89812fc8
JO
301}
302
c5cd8ac0 303static int add_event(struct list_head *list, int *idx,
930a2e29
JO
304 struct perf_event_attr *attr, char *name,
305 struct list_head *config_terms)
7ae92e74 306{
930a2e29 307 return __add_event(list, idx, attr, name, NULL, config_terms) ? 0 : -ENOMEM;
7ae92e74
YZ
308}
309
0b668bc9 310static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
8326f44d
IM
311{
312 int i, j;
61c45981 313 int n, longest = -1;
8326f44d
IM
314
315 for (i = 0; i < size; i++) {
0b668bc9 316 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
61c45981 317 n = strlen(names[i][j]);
89812fc8 318 if (n > longest && !strncasecmp(str, names[i][j], n))
61c45981
PM
319 longest = n;
320 }
89812fc8 321 if (longest > 0)
61c45981 322 return i;
8326f44d
IM
323 }
324
8953645f 325 return -1;
8326f44d
IM
326}
327
c5cd8ac0 328int parse_events_add_cache(struct list_head *list, int *idx,
89812fc8 329 char *type, char *op_result1, char *op_result2)
8326f44d 330{
89812fc8
JO
331 struct perf_event_attr attr;
332 char name[MAX_NAME_LEN];
61c45981 333 int cache_type = -1, cache_op = -1, cache_result = -1;
89812fc8
JO
334 char *op_result[2] = { op_result1, op_result2 };
335 int i, n;
8326f44d 336
8326f44d
IM
337 /*
338 * No fallback - if we cannot get a clear cache type
339 * then bail out:
340 */
0b668bc9 341 cache_type = parse_aliases(type, perf_evsel__hw_cache,
89812fc8 342 PERF_COUNT_HW_CACHE_MAX);
8326f44d 343 if (cache_type == -1)
89812fc8
JO
344 return -EINVAL;
345
346 n = snprintf(name, MAX_NAME_LEN, "%s", type);
61c45981 347
89812fc8
JO
348 for (i = 0; (i < 2) && (op_result[i]); i++) {
349 char *str = op_result[i];
350
275ef387 351 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
61c45981
PM
352
353 if (cache_op == -1) {
0b668bc9 354 cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
89812fc8 355 PERF_COUNT_HW_CACHE_OP_MAX);
61c45981 356 if (cache_op >= 0) {
0b668bc9 357 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
89812fc8 358 return -EINVAL;
61c45981
PM
359 continue;
360 }
361 }
362
363 if (cache_result == -1) {
0b668bc9
ACM
364 cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
365 PERF_COUNT_HW_CACHE_RESULT_MAX);
61c45981
PM
366 if (cache_result >= 0)
367 continue;
368 }
61c45981 369 }
8326f44d 370
8326f44d
IM
371 /*
372 * Fall back to reads:
373 */
8953645f
IM
374 if (cache_op == -1)
375 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
8326f44d 376
8326f44d
IM
377 /*
378 * Fall back to accesses:
379 */
380 if (cache_result == -1)
381 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
382
89812fc8
JO
383 memset(&attr, 0, sizeof(attr));
384 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
385 attr.type = PERF_TYPE_HW_CACHE;
930a2e29 386 return add_event(list, idx, &attr, name, NULL);
bcd3279f
FW
387}
388
c5cd8ac0 389static int add_tracepoint(struct list_head *list, int *idx,
89812fc8 390 char *sys_name, char *evt_name)
bcd3279f 391{
82fe1c29 392 struct perf_evsel *evsel;
bcd3279f 393
ef503831 394 evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
c5cd8ac0 395 if (!evsel)
82fe1c29 396 return -ENOMEM;
bcd3279f 397
82fe1c29 398 list_add_tail(&evsel->node, list);
c5cd8ac0 399
82fe1c29 400 return 0;
8326f44d
IM
401}
402
c5cd8ac0 403static int add_tracepoint_multi_event(struct list_head *list, int *idx,
f35488f9 404 char *sys_name, char *evt_name)
bcd3279f
FW
405{
406 char evt_path[MAXPATHLEN];
407 struct dirent *evt_ent;
408 DIR *evt_dir;
89812fc8 409 int ret = 0;
bcd3279f 410
ebf294bf 411 snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
bcd3279f 412 evt_dir = opendir(evt_path);
bcd3279f
FW
413 if (!evt_dir) {
414 perror("Can't open event dir");
89812fc8 415 return -1;
bcd3279f
FW
416 }
417
89812fc8 418 while (!ret && (evt_ent = readdir(evt_dir))) {
bcd3279f
FW
419 if (!strcmp(evt_ent->d_name, ".")
420 || !strcmp(evt_ent->d_name, "..")
421 || !strcmp(evt_ent->d_name, "enable")
422 || !strcmp(evt_ent->d_name, "filter"))
423 continue;
424
89812fc8 425 if (!strglobmatch(evt_ent->d_name, evt_name))
fb1d2edf
MH
426 continue;
427
89812fc8 428 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name);
bcd3279f
FW
429 }
430
0bd3f084 431 closedir(evt_dir);
89812fc8 432 return ret;
bcd3279f
FW
433}
434
c5cd8ac0 435static int add_tracepoint_event(struct list_head *list, int *idx,
f35488f9
JO
436 char *sys_name, char *evt_name)
437{
438 return strpbrk(evt_name, "*?") ?
439 add_tracepoint_multi_event(list, idx, sys_name, evt_name) :
440 add_tracepoint(list, idx, sys_name, evt_name);
441}
442
c5cd8ac0 443static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
f35488f9
JO
444 char *sys_name, char *evt_name)
445{
446 struct dirent *events_ent;
447 DIR *events_dir;
448 int ret = 0;
449
450 events_dir = opendir(tracing_events_path);
451 if (!events_dir) {
452 perror("Can't open event dir");
453 return -1;
454 }
455
456 while (!ret && (events_ent = readdir(events_dir))) {
457 if (!strcmp(events_ent->d_name, ".")
458 || !strcmp(events_ent->d_name, "..")
459 || !strcmp(events_ent->d_name, "enable")
460 || !strcmp(events_ent->d_name, "header_event")
461 || !strcmp(events_ent->d_name, "header_page"))
462 continue;
463
464 if (!strglobmatch(events_ent->d_name, sys_name))
465 continue;
466
467 ret = add_tracepoint_event(list, idx, events_ent->d_name,
468 evt_name);
469 }
470
471 closedir(events_dir);
472 return ret;
473}
474
c5cd8ac0 475int parse_events_add_tracepoint(struct list_head *list, int *idx,
89812fc8 476 char *sys, char *event)
f6bdafef 477{
f35488f9
JO
478 if (strpbrk(sys, "*?"))
479 return add_tracepoint_multi_sys(list, idx, sys, event);
480 else
481 return add_tracepoint_event(list, idx, sys, event);
f6bdafef
JB
482}
483
89812fc8
JO
484static int
485parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
1b290d67
FW
486{
487 int i;
488
489 for (i = 0; i < 3; i++) {
89812fc8 490 if (!type || !type[i])
1b290d67
FW
491 break;
492
7582732f
JO
493#define CHECK_SET_TYPE(bit) \
494do { \
495 if (attr->bp_type & bit) \
496 return -EINVAL; \
497 else \
498 attr->bp_type |= bit; \
499} while (0)
500
1b290d67
FW
501 switch (type[i]) {
502 case 'r':
7582732f 503 CHECK_SET_TYPE(HW_BREAKPOINT_R);
1b290d67
FW
504 break;
505 case 'w':
7582732f 506 CHECK_SET_TYPE(HW_BREAKPOINT_W);
1b290d67
FW
507 break;
508 case 'x':
7582732f 509 CHECK_SET_TYPE(HW_BREAKPOINT_X);
1b290d67
FW
510 break;
511 default:
89812fc8 512 return -EINVAL;
1b290d67
FW
513 }
514 }
89812fc8 515
7582732f
JO
516#undef CHECK_SET_TYPE
517
1b290d67
FW
518 if (!attr->bp_type) /* Default */
519 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
520
89812fc8 521 return 0;
1b290d67
FW
522}
523
c5cd8ac0 524int parse_events_add_breakpoint(struct list_head *list, int *idx,
3741eb9f 525 void *ptr, char *type, u64 len)
1b290d67 526{
89812fc8 527 struct perf_event_attr attr;
1b290d67 528
89812fc8 529 memset(&attr, 0, sizeof(attr));
9fafd98f 530 attr.bp_addr = (unsigned long) ptr;
1b290d67 531
89812fc8
JO
532 if (parse_breakpoint_type(type, &attr))
533 return -EINVAL;
1b290d67 534
3741eb9f
JS
535 /* Provide some defaults if len is not specified */
536 if (!len) {
537 if (attr.bp_type == HW_BREAKPOINT_X)
538 len = sizeof(long);
539 else
540 len = HW_BREAKPOINT_LEN_4;
541 }
542
543 attr.bp_len = len;
61c45981 544
89812fc8 545 attr.type = PERF_TYPE_BREAKPOINT;
4a841d65 546 attr.sample_period = 1;
b908debd 547
930a2e29 548 return add_event(list, idx, &attr, NULL, NULL);
74d5b588
JSR
549}
550
3b0e371c
JO
551static int check_type_val(struct parse_events_term *term,
552 struct parse_events_error *err,
553 int type)
554{
555 if (type == term->type_val)
556 return 0;
557
558 if (err) {
559 err->idx = term->err_val;
560 if (type == PARSE_EVENTS__TERM_TYPE_NUM)
561 err->str = strdup("expected numeric value");
562 else
563 err->str = strdup("expected string value");
564 }
565 return -EINVAL;
566}
567
8f707d84 568static int config_term(struct perf_event_attr *attr,
3b0e371c
JO
569 struct parse_events_term *term,
570 struct parse_events_error *err)
8f707d84 571{
3b0e371c
JO
572#define CHECK_TYPE_VAL(type) \
573do { \
574 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
575 return -EINVAL; \
16fa7e82
JO
576} while (0)
577
578 switch (term->type_term) {
c056ba6a
JO
579 case PARSE_EVENTS__TERM_TYPE_USER:
580 /*
581 * Always succeed for sysfs terms, as we dont know
582 * at this point what type they need to have.
583 */
584 return 0;
8f707d84 585 case PARSE_EVENTS__TERM_TYPE_CONFIG:
16fa7e82 586 CHECK_TYPE_VAL(NUM);
8f707d84
JO
587 attr->config = term->val.num;
588 break;
589 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
16fa7e82 590 CHECK_TYPE_VAL(NUM);
8f707d84
JO
591 attr->config1 = term->val.num;
592 break;
593 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
16fa7e82 594 CHECK_TYPE_VAL(NUM);
8f707d84
JO
595 attr->config2 = term->val.num;
596 break;
597 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
16fa7e82 598 CHECK_TYPE_VAL(NUM);
8f707d84
JO
599 attr->sample_period = term->val.num;
600 break;
601 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
602 /*
603 * TODO uncomment when the field is available
604 * attr->branch_sample_type = term->val.num;
605 */
606 break;
6b5fc39b
JO
607 case PARSE_EVENTS__TERM_TYPE_NAME:
608 CHECK_TYPE_VAL(STR);
609 break;
8f707d84
JO
610 default:
611 return -EINVAL;
612 }
16fa7e82 613
8f707d84 614 return 0;
16fa7e82 615#undef CHECK_TYPE_VAL
8f707d84
JO
616}
617
618static int config_attr(struct perf_event_attr *attr,
3b0e371c
JO
619 struct list_head *head,
620 struct parse_events_error *err)
8f707d84 621{
6cee6cd3 622 struct parse_events_term *term;
8f707d84
JO
623
624 list_for_each_entry(term, head, list)
3b0e371c 625 if (config_term(attr, term, err))
8f707d84
JO
626 return -EINVAL;
627
628 return 0;
629}
630
930a2e29
JO
631static int get_config_terms(struct list_head *head_config,
632 struct list_head *head_terms __maybe_unused)
633{
634#define ADD_CONFIG_TERM(__type, __name, __val) \
635do { \
636 struct perf_evsel_config_term *__t; \
637 \
638 __t = zalloc(sizeof(*__t)); \
639 if (!__t) \
640 return -ENOMEM; \
641 \
642 INIT_LIST_HEAD(&__t->list); \
643 __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \
644 __t->val.__name = __val; \
645 list_add_tail(&__t->list, head_terms); \
646} while (0)
647
648 struct parse_events_term *term;
649
650 list_for_each_entry(term, head_config, list) {
651 switch (term->type_term) {
652 default:
653 break;
654 }
655 }
656#undef ADD_EVSEL_CONFIG
657 return 0;
658}
659
87d650be
JO
660int parse_events_add_numeric(struct parse_events_evlist *data,
661 struct list_head *list,
b527bab5 662 u32 type, u64 config,
8f707d84 663 struct list_head *head_config)
8ad8db37 664{
89812fc8 665 struct perf_event_attr attr;
930a2e29 666 LIST_HEAD(config_terms);
61c45981 667
89812fc8
JO
668 memset(&attr, 0, sizeof(attr));
669 attr.type = type;
670 attr.config = config;
8f707d84 671
930a2e29
JO
672 if (head_config) {
673 if (config_attr(&attr, head_config, data->error))
674 return -EINVAL;
675
676 if (get_config_terms(head_config, &config_terms))
677 return -ENOMEM;
678 }
8f707d84 679
930a2e29 680 return add_event(list, &data->idx, &attr, NULL, &config_terms);
61c45981 681}
8ad8db37 682
6cee6cd3 683static int parse_events__is_name_term(struct parse_events_term *term)
6b5fc39b
JO
684{
685 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
686}
687
9db1763c 688static char *pmu_event_name(struct list_head *head_terms)
6b5fc39b 689{
6cee6cd3 690 struct parse_events_term *term;
6b5fc39b
JO
691
692 list_for_each_entry(term, head_terms, list)
693 if (parse_events__is_name_term(term))
694 return term->val.str;
695
9db1763c 696 return NULL;
6b5fc39b
JO
697}
698
36adec85
JO
699int parse_events_add_pmu(struct parse_events_evlist *data,
700 struct list_head *list, char *name,
701 struct list_head *head_config)
5f537a26
JO
702{
703 struct perf_event_attr attr;
46441bdc 704 struct perf_pmu_info info;
5f537a26 705 struct perf_pmu *pmu;
410136f5 706 struct perf_evsel *evsel;
930a2e29 707 LIST_HEAD(config_terms);
5f537a26
JO
708
709 pmu = perf_pmu__find(name);
710 if (!pmu)
711 return -EINVAL;
712
dc0a6202
AH
713 if (pmu->default_config) {
714 memcpy(&attr, pmu->default_config,
715 sizeof(struct perf_event_attr));
716 } else {
717 memset(&attr, 0, sizeof(attr));
718 }
5f537a26 719
ad962273
AH
720 if (!head_config) {
721 attr.type = pmu->type;
930a2e29 722 evsel = __add_event(list, &data->idx, &attr, NULL, pmu->cpus, NULL);
ad962273
AH
723 return evsel ? 0 : -ENOMEM;
724 }
725
46441bdc 726 if (perf_pmu__check_alias(pmu, head_config, &info))
a6146d50
ZY
727 return -EINVAL;
728
5f537a26
JO
729 /*
730 * Configure hardcoded terms first, no need to check
731 * return value when called with fail == 0 ;)
732 */
3b0e371c 733 if (config_attr(&attr, head_config, data->error))
c056ba6a 734 return -EINVAL;
5f537a26 735
930a2e29
JO
736 if (get_config_terms(head_config, &config_terms))
737 return -ENOMEM;
738
e64b020b 739 if (perf_pmu__config(pmu, &attr, head_config, data->error))
5f537a26
JO
740 return -EINVAL;
741
36adec85 742 evsel = __add_event(list, &data->idx, &attr,
930a2e29
JO
743 pmu_event_name(head_config), pmu->cpus,
744 &config_terms);
410136f5 745 if (evsel) {
46441bdc
MF
746 evsel->unit = info.unit;
747 evsel->scale = info.scale;
044330c1 748 evsel->per_pkg = info.per_pkg;
1d9e446b 749 evsel->snapshot = info.snapshot;
410136f5
SE
750 }
751
752 return evsel ? 0 : -ENOMEM;
5f537a26
JO
753}
754
6a4bb04c
JO
755int parse_events__modifier_group(struct list_head *list,
756 char *event_mod)
89efb029 757{
6a4bb04c
JO
758 return parse_events__modifier_event(list, event_mod, true);
759}
760
63dab225 761void parse_events__set_leader(char *name, struct list_head *list)
6a4bb04c
JO
762{
763 struct perf_evsel *leader;
764
63dab225
ACM
765 __perf_evlist__set_leader(list);
766 leader = list_entry(list->next, struct perf_evsel, node);
6a4bb04c 767 leader->group_name = name ? strdup(name) : NULL;
89efb029
JO
768}
769
c5cd8ac0 770/* list_event is assumed to point to malloc'ed memory */
5d7be90e
JO
771void parse_events_update_lists(struct list_head *list_event,
772 struct list_head *list_all)
773{
774 /*
775 * Called for single event definition. Update the
89efb029 776 * 'all event' list, and reinit the 'single event'
5d7be90e
JO
777 * list, for next event definition.
778 */
779 list_splice_tail(list_event, list_all);
b847cbdc 780 free(list_event);
5d7be90e
JO
781}
782
f5b1135b
JO
783struct event_modifier {
784 int eu;
785 int ek;
786 int eh;
787 int eH;
788 int eG;
a1e12da4 789 int eI;
f5b1135b
JO
790 int precise;
791 int exclude_GH;
3c176311 792 int sample_read;
e9a7c414 793 int pinned;
f5b1135b
JO
794};
795
796static int get_event_modifier(struct event_modifier *mod, char *str,
797 struct perf_evsel *evsel)
61c45981 798{
f5b1135b
JO
799 int eu = evsel ? evsel->attr.exclude_user : 0;
800 int ek = evsel ? evsel->attr.exclude_kernel : 0;
801 int eh = evsel ? evsel->attr.exclude_hv : 0;
802 int eH = evsel ? evsel->attr.exclude_host : 0;
803 int eG = evsel ? evsel->attr.exclude_guest : 0;
a1e12da4 804 int eI = evsel ? evsel->attr.exclude_idle : 0;
f5b1135b 805 int precise = evsel ? evsel->attr.precise_ip : 0;
3c176311 806 int sample_read = 0;
e9a7c414 807 int pinned = evsel ? evsel->attr.pinned : 0;
a21ca2ca 808
f5b1135b
JO
809 int exclude = eu | ek | eh;
810 int exclude_GH = evsel ? evsel->exclude_GH : 0;
811
f5b1135b 812 memset(mod, 0, sizeof(*mod));
ceb53fbf 813
61c45981 814 while (*str) {
ab608344
PZ
815 if (*str == 'u') {
816 if (!exclude)
817 exclude = eu = ek = eh = 1;
61c45981 818 eu = 0;
ab608344
PZ
819 } else if (*str == 'k') {
820 if (!exclude)
821 exclude = eu = ek = eh = 1;
61c45981 822 ek = 0;
ab608344
PZ
823 } else if (*str == 'h') {
824 if (!exclude)
825 exclude = eu = ek = eh = 1;
61c45981 826 eh = 0;
99320cc8
JR
827 } else if (*str == 'G') {
828 if (!exclude_GH)
829 exclude_GH = eG = eH = 1;
830 eG = 0;
831 } else if (*str == 'H') {
832 if (!exclude_GH)
833 exclude_GH = eG = eH = 1;
834 eH = 0;
a1e12da4
JO
835 } else if (*str == 'I') {
836 eI = 1;
ab608344
PZ
837 } else if (*str == 'p') {
838 precise++;
1342798c
DA
839 /* use of precise requires exclude_guest */
840 if (!exclude_GH)
841 eG = 1;
3c176311
JO
842 } else if (*str == 'S') {
843 sample_read = 1;
e9a7c414
ME
844 } else if (*str == 'D') {
845 pinned = 1;
ab608344 846 } else
61c45981 847 break;
ab608344 848
61c45981 849 ++str;
5242519b 850 }
ceb53fbf 851
89812fc8
JO
852 /*
853 * precise ip:
854 *
855 * 0 - SAMPLE_IP can have arbitrary skid
856 * 1 - SAMPLE_IP must have constant skid
857 * 2 - SAMPLE_IP requested to have 0 skid
858 * 3 - SAMPLE_IP must have 0 skid
859 *
860 * See also PERF_RECORD_MISC_EXACT_IP
861 */
862 if (precise > 3)
863 return -EINVAL;
ceb53fbf 864
f5b1135b
JO
865 mod->eu = eu;
866 mod->ek = ek;
867 mod->eh = eh;
868 mod->eH = eH;
869 mod->eG = eG;
a1e12da4 870 mod->eI = eI;
f5b1135b
JO
871 mod->precise = precise;
872 mod->exclude_GH = exclude_GH;
3c176311 873 mod->sample_read = sample_read;
e9a7c414
ME
874 mod->pinned = pinned;
875
f5b1135b
JO
876 return 0;
877}
878
534123f4
JO
879/*
880 * Basic modifier sanity check to validate it contains only one
881 * instance of any modifier (apart from 'p') present.
882 */
883static int check_modifier(char *str)
884{
885 char *p = str;
886
887 /* The sizeof includes 0 byte as well. */
a1e12da4 888 if (strlen(str) > (sizeof("ukhGHpppSDI") - 1))
534123f4
JO
889 return -1;
890
891 while (*p) {
892 if (*p != 'p' && strchr(p + 1, *p))
893 return -1;
894 p++;
895 }
896
897 return 0;
898}
899
f5b1135b
JO
900int parse_events__modifier_event(struct list_head *list, char *str, bool add)
901{
902 struct perf_evsel *evsel;
903 struct event_modifier mod;
904
905 if (str == NULL)
906 return 0;
907
534123f4
JO
908 if (check_modifier(str))
909 return -EINVAL;
910
f5b1135b
JO
911 if (!add && get_event_modifier(&mod, str, NULL))
912 return -EINVAL;
913
0050f7aa 914 __evlist__for_each(list, evsel) {
f5b1135b
JO
915 if (add && get_event_modifier(&mod, str, evsel))
916 return -EINVAL;
917
918 evsel->attr.exclude_user = mod.eu;
919 evsel->attr.exclude_kernel = mod.ek;
920 evsel->attr.exclude_hv = mod.eh;
921 evsel->attr.precise_ip = mod.precise;
922 evsel->attr.exclude_host = mod.eH;
923 evsel->attr.exclude_guest = mod.eG;
a1e12da4 924 evsel->attr.exclude_idle = mod.eI;
f5b1135b 925 evsel->exclude_GH = mod.exclude_GH;
3c176311 926 evsel->sample_read = mod.sample_read;
e9a7c414
ME
927
928 if (perf_evsel__is_group_leader(evsel))
929 evsel->attr.pinned = mod.pinned;
89812fc8 930 }
ceb53fbf 931
61c45981
PM
932 return 0;
933}
8ad8db37 934
ac2ba9f3
RR
935int parse_events_name(struct list_head *list, char *name)
936{
937 struct perf_evsel *evsel;
938
0050f7aa 939 __evlist__for_each(list, evsel) {
ac2ba9f3
RR
940 if (!evsel->name)
941 evsel->name = strdup(name);
942 }
943
944 return 0;
945}
946
dcb4e102
KL
947static int
948comp_pmu(const void *p1, const void *p2)
949{
950 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
951 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
952
953 return strcmp(pmu1->symbol, pmu2->symbol);
954}
955
956static void perf_pmu__parse_cleanup(void)
957{
958 if (perf_pmu_events_list_num > 0) {
959 struct perf_pmu_event_symbol *p;
960 int i;
961
962 for (i = 0; i < perf_pmu_events_list_num; i++) {
963 p = perf_pmu_events_list + i;
964 free(p->symbol);
965 }
966 free(perf_pmu_events_list);
967 perf_pmu_events_list = NULL;
968 perf_pmu_events_list_num = 0;
969 }
970}
971
972#define SET_SYMBOL(str, stype) \
973do { \
974 p->symbol = str; \
975 if (!p->symbol) \
976 goto err; \
977 p->type = stype; \
978} while (0)
979
980/*
981 * Read the pmu events list from sysfs
982 * Save it into perf_pmu_events_list
983 */
984static void perf_pmu__parse_init(void)
985{
986
987 struct perf_pmu *pmu = NULL;
988 struct perf_pmu_alias *alias;
989 int len = 0;
990
991 pmu = perf_pmu__find("cpu");
992 if ((pmu == NULL) || list_empty(&pmu->aliases)) {
993 perf_pmu_events_list_num = -1;
994 return;
995 }
996 list_for_each_entry(alias, &pmu->aliases, list) {
997 if (strchr(alias->name, '-'))
998 len++;
999 len++;
1000 }
1001 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1002 if (!perf_pmu_events_list)
1003 return;
1004 perf_pmu_events_list_num = len;
1005
1006 len = 0;
1007 list_for_each_entry(alias, &pmu->aliases, list) {
1008 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1009 char *tmp = strchr(alias->name, '-');
1010
1011 if (tmp != NULL) {
1012 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1013 PMU_EVENT_SYMBOL_PREFIX);
1014 p++;
1015 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1016 len += 2;
1017 } else {
1018 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1019 len++;
1020 }
1021 }
1022 qsort(perf_pmu_events_list, len,
1023 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1024
1025 return;
1026err:
1027 perf_pmu__parse_cleanup();
1028}
1029
1030enum perf_pmu_event_symbol_type
1031perf_pmu__parse_check(const char *name)
1032{
1033 struct perf_pmu_event_symbol p, *r;
1034
1035 /* scan kernel pmu events from sysfs if needed */
1036 if (perf_pmu_events_list_num == 0)
1037 perf_pmu__parse_init();
1038 /*
1039 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1040 * cpu-cycles has been handled by hardcode.
1041 * So it must be cpu// events, not kernel pmu event.
1042 */
1043 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1044 return PMU_EVENT_SYMBOL_ERR;
1045
1046 p.symbol = strdup(name);
1047 r = bsearch(&p, perf_pmu_events_list,
1048 (size_t) perf_pmu_events_list_num,
1049 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1050 free(p.symbol);
1051 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1052}
1053
90e2b22d 1054static int parse_events__scanner(const char *str, void *data, int start_token)
61c45981 1055{
89812fc8 1056 YY_BUFFER_STATE buffer;
ac20de6f 1057 void *scanner;
46010ab2 1058 int ret;
bcd3279f 1059
90e2b22d 1060 ret = parse_events_lex_init_extra(start_token, &scanner);
ac20de6f
ZY
1061 if (ret)
1062 return ret;
1063
1064 buffer = parse_events__scan_string(str, scanner);
a21ca2ca 1065
82ba1f2f
JO
1066#ifdef PARSER_DEBUG
1067 parse_events_debug = 1;
1068#endif
ac20de6f
ZY
1069 ret = parse_events_parse(data, scanner);
1070
1071 parse_events__flush_buffer(buffer, scanner);
1072 parse_events__delete_buffer(buffer, scanner);
1073 parse_events_lex_destroy(scanner);
1074 return ret;
1075}
bcd3279f 1076
90e2b22d
JO
1077/*
1078 * parse event config string, return a list of event terms.
1079 */
1080int parse_events_terms(struct list_head *terms, const char *str)
1081{
23b6339b 1082 struct parse_events_terms data = {
90e2b22d
JO
1083 .terms = NULL,
1084 };
1085 int ret;
1086
1087 ret = parse_events__scanner(str, &data, PE_START_TERMS);
1088 if (!ret) {
1089 list_splice(data.terms, terms);
74cf249d 1090 zfree(&data.terms);
90e2b22d
JO
1091 return 0;
1092 }
1093
b2c34fde
AH
1094 if (data.terms)
1095 parse_events__free_terms(data.terms);
90e2b22d
JO
1096 return ret;
1097}
1098
b39b8393
JO
1099int parse_events(struct perf_evlist *evlist, const char *str,
1100 struct parse_events_error *err)
ac20de6f 1101{
23b6339b 1102 struct parse_events_evlist data = {
b39b8393
JO
1103 .list = LIST_HEAD_INIT(data.list),
1104 .idx = evlist->nr_entries,
1105 .error = err,
ac20de6f
ZY
1106 };
1107 int ret;
bcd3279f 1108
90e2b22d 1109 ret = parse_events__scanner(str, &data, PE_START_EVENTS);
dcb4e102 1110 perf_pmu__parse_cleanup();
89812fc8 1111 if (!ret) {
46010ab2 1112 int entries = data.idx - evlist->nr_entries;
15bfd2cc
WN
1113 struct perf_evsel *last;
1114
46010ab2 1115 perf_evlist__splice_list_tail(evlist, &data.list, entries);
97f63e4a 1116 evlist->nr_groups += data.nr_groups;
15bfd2cc
WN
1117 last = perf_evlist__last(evlist);
1118 last->cmdline_group_boundary = true;
1119
89812fc8
JO
1120 return 0;
1121 }
bcd3279f 1122
5d7be90e
JO
1123 /*
1124 * There are 2 users - builtin-record and builtin-test objects.
1125 * Both call perf_evlist__delete in case of error, so we dont
1126 * need to bother.
1127 */
bcd3279f 1128 return ret;
8ad8db37
IM
1129}
1130
b39b8393
JO
1131#define MAX_WIDTH 1000
1132static int get_term_width(void)
1133{
1134 struct winsize ws;
1135
1136 get_term_dimensions(&ws);
1137 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1138}
1139
1140static void parse_events_print_error(struct parse_events_error *err,
1141 const char *event)
1142{
1143 const char *str = "invalid or unsupported event: ";
1144 char _buf[MAX_WIDTH];
1145 char *buf = (char *) event;
1146 int idx = 0;
1147
1148 if (err->str) {
1149 /* -2 for extra '' in the final fprintf */
1150 int width = get_term_width() - 2;
1151 int len_event = strlen(event);
1152 int len_str, max_len, cut = 0;
1153
1154 /*
1155 * Maximum error index indent, we will cut
1156 * the event string if it's bigger.
1157 */
1158 int max_err_idx = 10;
1159
1160 /*
1161 * Let's be specific with the message when
1162 * we have the precise error.
1163 */
1164 str = "event syntax error: ";
1165 len_str = strlen(str);
1166 max_len = width - len_str;
1167
1168 buf = _buf;
1169
1170 /* We're cutting from the beggining. */
1171 if (err->idx > max_err_idx)
1172 cut = err->idx - max_err_idx;
1173
1174 strncpy(buf, event + cut, max_len);
1175
1176 /* Mark cut parts with '..' on both sides. */
1177 if (cut)
1178 buf[0] = buf[1] = '.';
1179
1180 if ((len_event - cut) > max_len) {
1181 buf[max_len - 1] = buf[max_len - 2] = '.';
1182 buf[max_len] = 0;
1183 }
1184
1185 idx = len_str + err->idx - cut;
1186 }
1187
1188 fprintf(stderr, "%s'%s'\n", str, buf);
1189 if (idx) {
1190 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
1191 if (err->help)
1192 fprintf(stderr, "\n%s\n", err->help);
1193 free(err->str);
1194 free(err->help);
1195 }
1196
1197 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
1198}
1199
1200#undef MAX_WIDTH
1201
f120f9d5 1202int parse_events_option(const struct option *opt, const char *str,
1d037ca1 1203 int unset __maybe_unused)
f120f9d5
JO
1204{
1205 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
b39b8393
JO
1206 struct parse_events_error err = { .idx = 0, };
1207 int ret = parse_events(evlist, str, &err);
1208
1209 if (ret)
1210 parse_events_print_error(&err, str);
9175ce1f 1211
9175ce1f 1212 return ret;
f120f9d5
JO
1213}
1214
4ba1faa1
WN
1215static int
1216foreach_evsel_in_last_glob(struct perf_evlist *evlist,
1217 int (*func)(struct perf_evsel *evsel,
1218 const void *arg),
1219 const void *arg)
c171b552 1220{
69aad6f1 1221 struct perf_evsel *last = NULL;
4ba1faa1 1222 int err;
c171b552 1223
361c99a6 1224 if (evlist->nr_entries > 0)
0c21f736 1225 last = perf_evlist__last(evlist);
69aad6f1 1226
15bfd2cc 1227 do {
4ba1faa1
WN
1228 err = (*func)(last, arg);
1229 if (err)
15bfd2cc 1230 return -1;
4ba1faa1
WN
1231 if (!last)
1232 return 0;
15bfd2cc
WN
1233
1234 if (last->node.prev == &evlist->entries)
1235 return 0;
1236 last = list_entry(last->node.prev, struct perf_evsel, node);
1237 } while (!last->cmdline_group_boundary);
c171b552
LZ
1238
1239 return 0;
1240}
1241
4ba1faa1
WN
1242static int set_filter(struct perf_evsel *evsel, const void *arg)
1243{
1244 const char *str = arg;
1245
1246 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
1247 fprintf(stderr,
1248 "--filter option should follow a -e tracepoint option\n");
1249 return -1;
1250 }
1251
1252 if (perf_evsel__append_filter(evsel, "&&", str) < 0) {
1253 fprintf(stderr,
1254 "not enough memory to hold filter string\n");
1255 return -1;
1256 }
1257
1258 return 0;
1259}
1260
1261int parse_filter(const struct option *opt, const char *str,
1262 int unset __maybe_unused)
1263{
1264 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1265
1266 return foreach_evsel_in_last_glob(evlist, set_filter,
1267 (const void *)str);
1268}
1269
1270static int add_exclude_perf_filter(struct perf_evsel *evsel,
1271 const void *arg __maybe_unused)
1272{
1273 char new_filter[64];
1274
1275 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
1276 fprintf(stderr,
1277 "--exclude-perf option should follow a -e tracepoint option\n");
1278 return -1;
1279 }
1280
1281 snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
1282
1283 if (perf_evsel__append_filter(evsel, "&&", new_filter) < 0) {
1284 fprintf(stderr,
1285 "not enough memory to hold filter string\n");
1286 return -1;
1287 }
1288
1289 return 0;
1290}
1291
1292int exclude_perf(const struct option *opt,
1293 const char *arg __maybe_unused,
1294 int unset __maybe_unused)
1295{
1296 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1297
1298 return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
1299 NULL);
1300}
1301
86847b62 1302static const char * const event_type_descriptors[] = {
86847b62
TG
1303 "Hardware event",
1304 "Software event",
1305 "Tracepoint event",
1306 "Hardware cache event",
41bdcb23
LW
1307 "Raw hardware event descriptor",
1308 "Hardware breakpoint",
86847b62
TG
1309};
1310
ab0e4800
YS
1311static int cmp_string(const void *a, const void *b)
1312{
1313 const char * const *as = a;
1314 const char * const *bs = b;
1315
1316 return strcmp(*as, *bs);
1317}
1318
f6bdafef
JB
1319/*
1320 * Print the events from <debugfs_mount_point>/tracing/events
1321 */
1322
a3277d2d
FW
1323void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
1324 bool name_only)
f6bdafef
JB
1325{
1326 DIR *sys_dir, *evt_dir;
1327 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
f6bdafef 1328 char evt_path[MAXPATHLEN];
725b1368 1329 char dir_path[MAXPATHLEN];
ab0e4800
YS
1330 char **evt_list = NULL;
1331 unsigned int evt_i = 0, evt_num = 0;
1332 bool evt_num_known = false;
f6bdafef 1333
ab0e4800 1334restart:
ebf294bf 1335 sys_dir = opendir(tracing_events_path);
f6bdafef 1336 if (!sys_dir)
725b1368 1337 return;
6b58e7f1 1338
ab0e4800
YS
1339 if (evt_num_known) {
1340 evt_list = zalloc(sizeof(char *) * evt_num);
1341 if (!evt_list)
1342 goto out_close_sys_dir;
1343 }
1344
6b58e7f1 1345 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
48000a1a 1346 if (subsys_glob != NULL &&
668b8788
ACM
1347 !strglobmatch(sys_dirent.d_name, subsys_glob))
1348 continue;
725b1368 1349
ebf294bf 1350 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
725b1368
ED
1351 sys_dirent.d_name);
1352 evt_dir = opendir(dir_path);
1353 if (!evt_dir)
6b58e7f1 1354 continue;
725b1368 1355
6b58e7f1 1356 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
48000a1a 1357 if (event_glob != NULL &&
668b8788
ACM
1358 !strglobmatch(evt_dirent.d_name, event_glob))
1359 continue;
1360
ab0e4800
YS
1361 if (!evt_num_known) {
1362 evt_num++;
a3277d2d
FW
1363 continue;
1364 }
1365
f6bdafef
JB
1366 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1367 sys_dirent.d_name, evt_dirent.d_name);
ab0e4800
YS
1368
1369 evt_list[evt_i] = strdup(evt_path);
1370 if (evt_list[evt_i] == NULL)
1371 goto out_close_evt_dir;
1372 evt_i++;
f6bdafef
JB
1373 }
1374 closedir(evt_dir);
1375 }
f6bdafef 1376 closedir(sys_dir);
ab0e4800
YS
1377
1378 if (!evt_num_known) {
1379 evt_num_known = true;
1380 goto restart;
1381 }
1382 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1383 evt_i = 0;
1384 while (evt_i < evt_num) {
1385 if (name_only) {
1386 printf("%s ", evt_list[evt_i++]);
1387 continue;
1388 }
1389 printf(" %-50s [%s]\n", evt_list[evt_i++],
1390 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1391 }
1392 if (evt_num)
1393 printf("\n");
1394
1395out_free:
1396 evt_num = evt_i;
1397 for (evt_i = 0; evt_i < evt_num; evt_i++)
1398 zfree(&evt_list[evt_i]);
1399 zfree(&evt_list);
1400 return;
1401
1402out_close_evt_dir:
1403 closedir(evt_dir);
1404out_close_sys_dir:
1405 closedir(sys_dir);
1406
1407 printf("FATAL: not enough memory to print %s\n",
1408 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1409 if (evt_list)
1410 goto out_free;
f6bdafef
JB
1411}
1412
20c457b8
TR
1413/*
1414 * Check whether event is in <debugfs_mount_point>/tracing/events
1415 */
1416
1417int is_valid_tracepoint(const char *event_string)
1418{
1419 DIR *sys_dir, *evt_dir;
1420 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
1421 char evt_path[MAXPATHLEN];
1422 char dir_path[MAXPATHLEN];
1423
ebf294bf 1424 sys_dir = opendir(tracing_events_path);
20c457b8
TR
1425 if (!sys_dir)
1426 return 0;
1427
1428 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1429
ebf294bf 1430 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
20c457b8
TR
1431 sys_dirent.d_name);
1432 evt_dir = opendir(dir_path);
1433 if (!evt_dir)
1434 continue;
1435
1436 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1437 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1438 sys_dirent.d_name, evt_dirent.d_name);
1439 if (!strcmp(evt_path, event_string)) {
1440 closedir(evt_dir);
1441 closedir(sys_dir);
1442 return 1;
1443 }
1444 }
1445 closedir(evt_dir);
1446 }
1447 closedir(sys_dir);
1448 return 0;
1449}
1450
b41f1cec
NK
1451static bool is_event_supported(u8 type, unsigned config)
1452{
1453 bool ret = true;
88fee52e 1454 int open_return;
b41f1cec
NK
1455 struct perf_evsel *evsel;
1456 struct perf_event_attr attr = {
1457 .type = type,
1458 .config = config,
1459 .disabled = 1,
b41f1cec
NK
1460 };
1461 struct {
1462 struct thread_map map;
1463 int threads[1];
1464 } tmap = {
1465 .map.nr = 1,
1466 .threads = { 0 },
1467 };
1468
ef503831 1469 evsel = perf_evsel__new(&attr);
b41f1cec 1470 if (evsel) {
88fee52e
VW
1471 open_return = perf_evsel__open(evsel, NULL, &tmap.map);
1472 ret = open_return >= 0;
1473
1474 if (open_return == -EACCES) {
1475 /*
1476 * This happens if the paranoid value
1477 * /proc/sys/kernel/perf_event_paranoid is set to 2
1478 * Re-run with exclude_kernel set; we don't do that
1479 * by default as some ARM machines do not support it.
1480 *
1481 */
1482 evsel->attr.exclude_kernel = 1;
1483 ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0;
1484 }
b41f1cec
NK
1485 perf_evsel__delete(evsel);
1486 }
1487
1488 return ret;
1489}
1490
a3277d2d 1491int print_hwcache_events(const char *event_glob, bool name_only)
668b8788 1492{
ab0e4800 1493 unsigned int type, op, i, evt_i = 0, evt_num = 0;
0b668bc9 1494 char name[64];
ab0e4800
YS
1495 char **evt_list = NULL;
1496 bool evt_num_known = false;
1497
1498restart:
1499 if (evt_num_known) {
1500 evt_list = zalloc(sizeof(char *) * evt_num);
1501 if (!evt_list)
1502 goto out_enomem;
1503 }
668b8788
ACM
1504
1505 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1506 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1507 /* skip invalid cache type */
0b668bc9 1508 if (!perf_evsel__is_cache_op_valid(type, op))
668b8788
ACM
1509 continue;
1510
1511 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
0b668bc9
ACM
1512 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
1513 name, sizeof(name));
947b4ad1 1514 if (event_glob != NULL && !strglobmatch(name, event_glob))
668b8788
ACM
1515 continue;
1516
b41f1cec
NK
1517 if (!is_event_supported(PERF_TYPE_HW_CACHE,
1518 type | (op << 8) | (i << 16)))
1519 continue;
1520
ab0e4800
YS
1521 if (!evt_num_known) {
1522 evt_num++;
1523 continue;
1524 }
1525
1526 evt_list[evt_i] = strdup(name);
1527 if (evt_list[evt_i] == NULL)
1528 goto out_enomem;
1529 evt_i++;
668b8788
ACM
1530 }
1531 }
1532 }
1533
ab0e4800
YS
1534 if (!evt_num_known) {
1535 evt_num_known = true;
1536 goto restart;
1537 }
1538 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1539 evt_i = 0;
1540 while (evt_i < evt_num) {
1541 if (name_only) {
1542 printf("%s ", evt_list[evt_i++]);
1543 continue;
1544 }
1545 printf(" %-50s [%s]\n", evt_list[evt_i++],
1546 event_type_descriptors[PERF_TYPE_HW_CACHE]);
1547 }
1548 if (evt_num)
dc098b35 1549 printf("\n");
ab0e4800
YS
1550
1551out_free:
1552 evt_num = evt_i;
1553 for (evt_i = 0; evt_i < evt_num; evt_i++)
1554 zfree(&evt_list[evt_i]);
1555 zfree(&evt_list);
1556 return evt_num;
1557
1558out_enomem:
1559 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
1560 if (evt_list)
1561 goto out_free;
1562 return evt_num;
668b8788
ACM
1563}
1564
705750f2 1565void print_symbol_events(const char *event_glob, unsigned type,
a3277d2d
FW
1566 struct event_symbol *syms, unsigned max,
1567 bool name_only)
8ad8db37 1568{
ab0e4800 1569 unsigned int i, evt_i = 0, evt_num = 0;
947b4ad1 1570 char name[MAX_NAME_LEN];
ab0e4800
YS
1571 char **evt_list = NULL;
1572 bool evt_num_known = false;
1573
1574restart:
1575 if (evt_num_known) {
1576 evt_list = zalloc(sizeof(char *) * evt_num);
1577 if (!evt_list)
1578 goto out_enomem;
1579 syms -= max;
1580 }
8ad8db37 1581
1dc12760 1582 for (i = 0; i < max; i++, syms++) {
668b8788 1583
48000a1a 1584 if (event_glob != NULL &&
668b8788
ACM
1585 !(strglobmatch(syms->symbol, event_glob) ||
1586 (syms->alias && strglobmatch(syms->alias, event_glob))))
1587 continue;
8ad8db37 1588
b41f1cec
NK
1589 if (!is_event_supported(type, i))
1590 continue;
1591
ab0e4800
YS
1592 if (!evt_num_known) {
1593 evt_num++;
a3277d2d
FW
1594 continue;
1595 }
1596
ab0e4800 1597 if (!name_only && strlen(syms->alias))
947b4ad1 1598 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
74d5b588 1599 else
947b4ad1 1600 strncpy(name, syms->symbol, MAX_NAME_LEN);
8ad8db37 1601
ab0e4800
YS
1602 evt_list[evt_i] = strdup(name);
1603 if (evt_list[evt_i] == NULL)
1604 goto out_enomem;
1605 evt_i++;
8ad8db37
IM
1606 }
1607
ab0e4800
YS
1608 if (!evt_num_known) {
1609 evt_num_known = true;
1610 goto restart;
1611 }
1612 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1613 evt_i = 0;
1614 while (evt_i < evt_num) {
1615 if (name_only) {
1616 printf("%s ", evt_list[evt_i++]);
1617 continue;
1618 }
1619 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
1620 }
1621 if (evt_num)
668b8788 1622 printf("\n");
ab0e4800
YS
1623
1624out_free:
1625 evt_num = evt_i;
1626 for (evt_i = 0; evt_i < evt_num; evt_i++)
1627 zfree(&evt_list[evt_i]);
1628 zfree(&evt_list);
1629 return;
1630
1631out_enomem:
1632 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
1633 if (evt_list)
1634 goto out_free;
1dc12760
JO
1635}
1636
1637/*
1638 * Print the help text for the event symbols:
1639 */
a3277d2d 1640void print_events(const char *event_glob, bool name_only)
1dc12760 1641{
1dc12760 1642 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
a3277d2d 1643 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
1dc12760
JO
1644
1645 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
a3277d2d 1646 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
1dc12760 1647
a3277d2d 1648 print_hwcache_events(event_glob, name_only);
668b8788 1649
dc098b35
AK
1650 print_pmu_events(event_glob, name_only);
1651
668b8788
ACM
1652 if (event_glob != NULL)
1653 return;
73c24cb8 1654
a3277d2d 1655 if (!name_only) {
a3277d2d
FW
1656 printf(" %-50s [%s]\n",
1657 "rNNN",
1658 event_type_descriptors[PERF_TYPE_RAW]);
1659 printf(" %-50s [%s]\n",
1660 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
1661 event_type_descriptors[PERF_TYPE_RAW]);
af3df2cf 1662 printf(" (see 'man perf-list' on how to encode it)\n");
a3277d2d
FW
1663 printf("\n");
1664
1665 printf(" %-50s [%s]\n",
3741eb9f 1666 "mem:<addr>[/len][:access]",
41bdcb23 1667 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
a3277d2d
FW
1668 printf("\n");
1669 }
1b290d67 1670
a3277d2d 1671 print_tracepoint_events(NULL, NULL, name_only);
8ad8db37 1672}
8f707d84 1673
6cee6cd3 1674int parse_events__is_hardcoded_term(struct parse_events_term *term)
8f707d84 1675{
16fa7e82 1676 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
8f707d84
JO
1677}
1678
6cee6cd3 1679static int new_term(struct parse_events_term **_term, int type_val,
16fa7e82 1680 int type_term, char *config,
cecf3a2e 1681 char *str, u64 num, int err_term, int err_val)
8f707d84 1682{
6cee6cd3 1683 struct parse_events_term *term;
8f707d84
JO
1684
1685 term = zalloc(sizeof(*term));
1686 if (!term)
1687 return -ENOMEM;
1688
1689 INIT_LIST_HEAD(&term->list);
16fa7e82
JO
1690 term->type_val = type_val;
1691 term->type_term = type_term;
8f707d84 1692 term->config = config;
cecf3a2e
JO
1693 term->err_term = err_term;
1694 term->err_val = err_val;
8f707d84 1695
16fa7e82 1696 switch (type_val) {
8f707d84
JO
1697 case PARSE_EVENTS__TERM_TYPE_NUM:
1698 term->val.num = num;
1699 break;
1700 case PARSE_EVENTS__TERM_TYPE_STR:
1701 term->val.str = str;
1702 break;
1703 default:
4be8be6b 1704 free(term);
8f707d84
JO
1705 return -EINVAL;
1706 }
1707
1708 *_term = term;
1709 return 0;
1710}
1711
6cee6cd3 1712int parse_events_term__num(struct parse_events_term **term,
cecf3a2e 1713 int type_term, char *config, u64 num,
bb78ce7d 1714 void *loc_term_, void *loc_val_)
16fa7e82 1715{
bb78ce7d
AH
1716 YYLTYPE *loc_term = loc_term_;
1717 YYLTYPE *loc_val = loc_val_;
1718
16fa7e82 1719 return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
cecf3a2e
JO
1720 config, NULL, num,
1721 loc_term ? loc_term->first_column : 0,
1722 loc_val ? loc_val->first_column : 0);
16fa7e82
JO
1723}
1724
6cee6cd3 1725int parse_events_term__str(struct parse_events_term **term,
cecf3a2e 1726 int type_term, char *config, char *str,
bb78ce7d 1727 void *loc_term_, void *loc_val_)
16fa7e82 1728{
bb78ce7d
AH
1729 YYLTYPE *loc_term = loc_term_;
1730 YYLTYPE *loc_val = loc_val_;
1731
16fa7e82 1732 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
cecf3a2e
JO
1733 config, str, 0,
1734 loc_term ? loc_term->first_column : 0,
1735 loc_val ? loc_val->first_column : 0);
16fa7e82
JO
1736}
1737
6cee6cd3 1738int parse_events_term__sym_hw(struct parse_events_term **term,
1d33d6dc
JO
1739 char *config, unsigned idx)
1740{
1741 struct event_symbol *sym;
1742
1743 BUG_ON(idx >= PERF_COUNT_HW_MAX);
1744 sym = &event_symbols_hw[idx];
1745
1746 if (config)
1747 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1748 PARSE_EVENTS__TERM_TYPE_USER, config,
cecf3a2e 1749 (char *) sym->symbol, 0, 0, 0);
1d33d6dc
JO
1750 else
1751 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1752 PARSE_EVENTS__TERM_TYPE_USER,
cecf3a2e
JO
1753 (char *) "event", (char *) sym->symbol,
1754 0, 0, 0);
1d33d6dc
JO
1755}
1756
6cee6cd3
ACM
1757int parse_events_term__clone(struct parse_events_term **new,
1758 struct parse_events_term *term)
a6146d50
ZY
1759{
1760 return new_term(new, term->type_val, term->type_term, term->config,
cecf3a2e
JO
1761 term->val.str, term->val.num,
1762 term->err_term, term->err_val);
a6146d50
ZY
1763}
1764
8f707d84
JO
1765void parse_events__free_terms(struct list_head *terms)
1766{
6cee6cd3 1767 struct parse_events_term *term, *h;
8f707d84
JO
1768
1769 list_for_each_entry_safe(term, h, terms, list)
1770 free(term);
8f707d84 1771}
b39b8393
JO
1772
1773void parse_events_evlist_error(struct parse_events_evlist *data,
1774 int idx, const char *str)
1775{
1776 struct parse_events_error *err = data->error;
1777
a6ced2be
AH
1778 if (!err)
1779 return;
b39b8393
JO
1780 err->idx = idx;
1781 err->str = strdup(str);
1782 WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
1783}