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