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