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