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