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