perf tools: Validate config term maximum value
[deliverable/linux.git] / tools / perf / util / parse-events.c
CommitLineData
d2709c7c 1#include <linux/hw_breakpoint.h>
8ad8db37 2#include "util.h"
6b58e7f1 3#include "../perf.h"
361c99a6 4#include "evlist.h"
69aad6f1 5#include "evsel.h"
8ad8db37
IM
6#include "parse-options.h"
7#include "parse-events.h"
8#include "exec_cmd.h"
42f60c2d 9#include "string.h"
5aab621b 10#include "symbol.h"
5beeded1 11#include "cache.h"
8755a8f2 12#include "header.h"
6e81c74c 13#include "debug.h"
553873e1 14#include <api/fs/debugfs.h>
ac20de6f 15#include "parse-events-bison.h"
90e2b22d 16#define YY_EXTRA_TYPE int
89812fc8 17#include "parse-events-flex.h"
5f537a26 18#include "pmu.h"
b41f1cec 19#include "thread_map.h"
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
JO
599 break;
600 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
601 /*
602 * TODO uncomment when the field is available
603 * attr->branch_sample_type = term->val.num;
604 */
605 break;
32067712
KL
606 case PARSE_EVENTS__TERM_TYPE_TIME:
607 CHECK_TYPE_VAL(NUM);
608 if (term->val.num > 1) {
609 err->str = strdup("expected 0 or 1");
610 err->idx = term->err_val;
611 return -EINVAL;
612 }
613 break;
6b5fc39b
JO
614 case PARSE_EVENTS__TERM_TYPE_NAME:
615 CHECK_TYPE_VAL(STR);
616 break;
8f707d84
JO
617 default:
618 return -EINVAL;
619 }
16fa7e82 620
8f707d84 621 return 0;
16fa7e82 622#undef CHECK_TYPE_VAL
8f707d84
JO
623}
624
625static int config_attr(struct perf_event_attr *attr,
3b0e371c
JO
626 struct list_head *head,
627 struct parse_events_error *err)
8f707d84 628{
6cee6cd3 629 struct parse_events_term *term;
8f707d84
JO
630
631 list_for_each_entry(term, head, list)
3b0e371c 632 if (config_term(attr, term, err))
8f707d84
JO
633 return -EINVAL;
634
635 return 0;
636}
637
930a2e29
JO
638static int get_config_terms(struct list_head *head_config,
639 struct list_head *head_terms __maybe_unused)
640{
641#define ADD_CONFIG_TERM(__type, __name, __val) \
642do { \
643 struct perf_evsel_config_term *__t; \
644 \
645 __t = zalloc(sizeof(*__t)); \
646 if (!__t) \
647 return -ENOMEM; \
648 \
649 INIT_LIST_HEAD(&__t->list); \
650 __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \
651 __t->val.__name = __val; \
652 list_add_tail(&__t->list, head_terms); \
653} while (0)
654
655 struct parse_events_term *term;
656
657 list_for_each_entry(term, head_config, list) {
658 switch (term->type_term) {
ee4c7588
JO
659 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
660 ADD_CONFIG_TERM(PERIOD, period, term->val.num);
32067712
KL
661 break;
662 case PARSE_EVENTS__TERM_TYPE_TIME:
663 ADD_CONFIG_TERM(TIME, time, term->val.num);
664 break;
930a2e29
JO
665 default:
666 break;
667 }
668 }
669#undef ADD_EVSEL_CONFIG
670 return 0;
671}
672
87d650be
JO
673int parse_events_add_numeric(struct parse_events_evlist *data,
674 struct list_head *list,
b527bab5 675 u32 type, u64 config,
8f707d84 676 struct list_head *head_config)
8ad8db37 677{
89812fc8 678 struct perf_event_attr attr;
930a2e29 679 LIST_HEAD(config_terms);
61c45981 680
89812fc8
JO
681 memset(&attr, 0, sizeof(attr));
682 attr.type = type;
683 attr.config = config;
8f707d84 684
930a2e29
JO
685 if (head_config) {
686 if (config_attr(&attr, head_config, data->error))
687 return -EINVAL;
688
689 if (get_config_terms(head_config, &config_terms))
690 return -ENOMEM;
691 }
8f707d84 692
930a2e29 693 return add_event(list, &data->idx, &attr, NULL, &config_terms);
61c45981 694}
8ad8db37 695
6cee6cd3 696static int parse_events__is_name_term(struct parse_events_term *term)
6b5fc39b
JO
697{
698 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
699}
700
9db1763c 701static char *pmu_event_name(struct list_head *head_terms)
6b5fc39b 702{
6cee6cd3 703 struct parse_events_term *term;
6b5fc39b
JO
704
705 list_for_each_entry(term, head_terms, list)
706 if (parse_events__is_name_term(term))
707 return term->val.str;
708
9db1763c 709 return NULL;
6b5fc39b
JO
710}
711
36adec85
JO
712int parse_events_add_pmu(struct parse_events_evlist *data,
713 struct list_head *list, char *name,
714 struct list_head *head_config)
5f537a26
JO
715{
716 struct perf_event_attr attr;
46441bdc 717 struct perf_pmu_info info;
5f537a26 718 struct perf_pmu *pmu;
410136f5 719 struct perf_evsel *evsel;
930a2e29 720 LIST_HEAD(config_terms);
5f537a26
JO
721
722 pmu = perf_pmu__find(name);
723 if (!pmu)
724 return -EINVAL;
725
dc0a6202
AH
726 if (pmu->default_config) {
727 memcpy(&attr, pmu->default_config,
728 sizeof(struct perf_event_attr));
729 } else {
730 memset(&attr, 0, sizeof(attr));
731 }
5f537a26 732
ad962273
AH
733 if (!head_config) {
734 attr.type = pmu->type;
930a2e29 735 evsel = __add_event(list, &data->idx, &attr, NULL, pmu->cpus, NULL);
ad962273
AH
736 return evsel ? 0 : -ENOMEM;
737 }
738
46441bdc 739 if (perf_pmu__check_alias(pmu, head_config, &info))
a6146d50
ZY
740 return -EINVAL;
741
5f537a26
JO
742 /*
743 * Configure hardcoded terms first, no need to check
744 * return value when called with fail == 0 ;)
745 */
3b0e371c 746 if (config_attr(&attr, head_config, data->error))
c056ba6a 747 return -EINVAL;
5f537a26 748
930a2e29
JO
749 if (get_config_terms(head_config, &config_terms))
750 return -ENOMEM;
751
e64b020b 752 if (perf_pmu__config(pmu, &attr, head_config, data->error))
5f537a26
JO
753 return -EINVAL;
754
36adec85 755 evsel = __add_event(list, &data->idx, &attr,
930a2e29
JO
756 pmu_event_name(head_config), pmu->cpus,
757 &config_terms);
410136f5 758 if (evsel) {
46441bdc
MF
759 evsel->unit = info.unit;
760 evsel->scale = info.scale;
044330c1 761 evsel->per_pkg = info.per_pkg;
1d9e446b 762 evsel->snapshot = info.snapshot;
410136f5
SE
763 }
764
765 return evsel ? 0 : -ENOMEM;
5f537a26
JO
766}
767
6a4bb04c
JO
768int parse_events__modifier_group(struct list_head *list,
769 char *event_mod)
89efb029 770{
6a4bb04c
JO
771 return parse_events__modifier_event(list, event_mod, true);
772}
773
63dab225 774void parse_events__set_leader(char *name, struct list_head *list)
6a4bb04c
JO
775{
776 struct perf_evsel *leader;
777
63dab225
ACM
778 __perf_evlist__set_leader(list);
779 leader = list_entry(list->next, struct perf_evsel, node);
6a4bb04c 780 leader->group_name = name ? strdup(name) : NULL;
89efb029
JO
781}
782
c5cd8ac0 783/* list_event is assumed to point to malloc'ed memory */
5d7be90e
JO
784void parse_events_update_lists(struct list_head *list_event,
785 struct list_head *list_all)
786{
787 /*
788 * Called for single event definition. Update the
89efb029 789 * 'all event' list, and reinit the 'single event'
5d7be90e
JO
790 * list, for next event definition.
791 */
792 list_splice_tail(list_event, list_all);
b847cbdc 793 free(list_event);
5d7be90e
JO
794}
795
f5b1135b
JO
796struct event_modifier {
797 int eu;
798 int ek;
799 int eh;
800 int eH;
801 int eG;
a1e12da4 802 int eI;
f5b1135b
JO
803 int precise;
804 int exclude_GH;
3c176311 805 int sample_read;
e9a7c414 806 int pinned;
f5b1135b
JO
807};
808
809static int get_event_modifier(struct event_modifier *mod, char *str,
810 struct perf_evsel *evsel)
61c45981 811{
f5b1135b
JO
812 int eu = evsel ? evsel->attr.exclude_user : 0;
813 int ek = evsel ? evsel->attr.exclude_kernel : 0;
814 int eh = evsel ? evsel->attr.exclude_hv : 0;
815 int eH = evsel ? evsel->attr.exclude_host : 0;
816 int eG = evsel ? evsel->attr.exclude_guest : 0;
a1e12da4 817 int eI = evsel ? evsel->attr.exclude_idle : 0;
f5b1135b 818 int precise = evsel ? evsel->attr.precise_ip : 0;
3c176311 819 int sample_read = 0;
e9a7c414 820 int pinned = evsel ? evsel->attr.pinned : 0;
a21ca2ca 821
f5b1135b
JO
822 int exclude = eu | ek | eh;
823 int exclude_GH = evsel ? evsel->exclude_GH : 0;
824
f5b1135b 825 memset(mod, 0, sizeof(*mod));
ceb53fbf 826
61c45981 827 while (*str) {
ab608344
PZ
828 if (*str == 'u') {
829 if (!exclude)
830 exclude = eu = ek = eh = 1;
61c45981 831 eu = 0;
ab608344
PZ
832 } else if (*str == 'k') {
833 if (!exclude)
834 exclude = eu = ek = eh = 1;
61c45981 835 ek = 0;
ab608344
PZ
836 } else if (*str == 'h') {
837 if (!exclude)
838 exclude = eu = ek = eh = 1;
61c45981 839 eh = 0;
99320cc8
JR
840 } else if (*str == 'G') {
841 if (!exclude_GH)
842 exclude_GH = eG = eH = 1;
843 eG = 0;
844 } else if (*str == 'H') {
845 if (!exclude_GH)
846 exclude_GH = eG = eH = 1;
847 eH = 0;
a1e12da4
JO
848 } else if (*str == 'I') {
849 eI = 1;
ab608344
PZ
850 } else if (*str == 'p') {
851 precise++;
1342798c
DA
852 /* use of precise requires exclude_guest */
853 if (!exclude_GH)
854 eG = 1;
3c176311
JO
855 } else if (*str == 'S') {
856 sample_read = 1;
e9a7c414
ME
857 } else if (*str == 'D') {
858 pinned = 1;
ab608344 859 } else
61c45981 860 break;
ab608344 861
61c45981 862 ++str;
5242519b 863 }
ceb53fbf 864
89812fc8
JO
865 /*
866 * precise ip:
867 *
868 * 0 - SAMPLE_IP can have arbitrary skid
869 * 1 - SAMPLE_IP must have constant skid
870 * 2 - SAMPLE_IP requested to have 0 skid
871 * 3 - SAMPLE_IP must have 0 skid
872 *
873 * See also PERF_RECORD_MISC_EXACT_IP
874 */
875 if (precise > 3)
876 return -EINVAL;
ceb53fbf 877
f5b1135b
JO
878 mod->eu = eu;
879 mod->ek = ek;
880 mod->eh = eh;
881 mod->eH = eH;
882 mod->eG = eG;
a1e12da4 883 mod->eI = eI;
f5b1135b
JO
884 mod->precise = precise;
885 mod->exclude_GH = exclude_GH;
3c176311 886 mod->sample_read = sample_read;
e9a7c414
ME
887 mod->pinned = pinned;
888
f5b1135b
JO
889 return 0;
890}
891
534123f4
JO
892/*
893 * Basic modifier sanity check to validate it contains only one
894 * instance of any modifier (apart from 'p') present.
895 */
896static int check_modifier(char *str)
897{
898 char *p = str;
899
900 /* The sizeof includes 0 byte as well. */
a1e12da4 901 if (strlen(str) > (sizeof("ukhGHpppSDI") - 1))
534123f4
JO
902 return -1;
903
904 while (*p) {
905 if (*p != 'p' && strchr(p + 1, *p))
906 return -1;
907 p++;
908 }
909
910 return 0;
911}
912
f5b1135b
JO
913int parse_events__modifier_event(struct list_head *list, char *str, bool add)
914{
915 struct perf_evsel *evsel;
916 struct event_modifier mod;
917
918 if (str == NULL)
919 return 0;
920
534123f4
JO
921 if (check_modifier(str))
922 return -EINVAL;
923
f5b1135b
JO
924 if (!add && get_event_modifier(&mod, str, NULL))
925 return -EINVAL;
926
0050f7aa 927 __evlist__for_each(list, evsel) {
f5b1135b
JO
928 if (add && get_event_modifier(&mod, str, evsel))
929 return -EINVAL;
930
931 evsel->attr.exclude_user = mod.eu;
932 evsel->attr.exclude_kernel = mod.ek;
933 evsel->attr.exclude_hv = mod.eh;
934 evsel->attr.precise_ip = mod.precise;
935 evsel->attr.exclude_host = mod.eH;
936 evsel->attr.exclude_guest = mod.eG;
a1e12da4 937 evsel->attr.exclude_idle = mod.eI;
f5b1135b 938 evsel->exclude_GH = mod.exclude_GH;
3c176311 939 evsel->sample_read = mod.sample_read;
e9a7c414
ME
940
941 if (perf_evsel__is_group_leader(evsel))
942 evsel->attr.pinned = mod.pinned;
89812fc8 943 }
ceb53fbf 944
61c45981
PM
945 return 0;
946}
8ad8db37 947
ac2ba9f3
RR
948int parse_events_name(struct list_head *list, char *name)
949{
950 struct perf_evsel *evsel;
951
0050f7aa 952 __evlist__for_each(list, evsel) {
ac2ba9f3
RR
953 if (!evsel->name)
954 evsel->name = strdup(name);
955 }
956
957 return 0;
958}
959
dcb4e102
KL
960static int
961comp_pmu(const void *p1, const void *p2)
962{
963 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
964 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
965
966 return strcmp(pmu1->symbol, pmu2->symbol);
967}
968
969static void perf_pmu__parse_cleanup(void)
970{
971 if (perf_pmu_events_list_num > 0) {
972 struct perf_pmu_event_symbol *p;
973 int i;
974
975 for (i = 0; i < perf_pmu_events_list_num; i++) {
976 p = perf_pmu_events_list + i;
977 free(p->symbol);
978 }
979 free(perf_pmu_events_list);
980 perf_pmu_events_list = NULL;
981 perf_pmu_events_list_num = 0;
982 }
983}
984
985#define SET_SYMBOL(str, stype) \
986do { \
987 p->symbol = str; \
988 if (!p->symbol) \
989 goto err; \
990 p->type = stype; \
991} while (0)
992
993/*
994 * Read the pmu events list from sysfs
995 * Save it into perf_pmu_events_list
996 */
997static void perf_pmu__parse_init(void)
998{
999
1000 struct perf_pmu *pmu = NULL;
1001 struct perf_pmu_alias *alias;
1002 int len = 0;
1003
1004 pmu = perf_pmu__find("cpu");
1005 if ((pmu == NULL) || list_empty(&pmu->aliases)) {
1006 perf_pmu_events_list_num = -1;
1007 return;
1008 }
1009 list_for_each_entry(alias, &pmu->aliases, list) {
1010 if (strchr(alias->name, '-'))
1011 len++;
1012 len++;
1013 }
1014 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1015 if (!perf_pmu_events_list)
1016 return;
1017 perf_pmu_events_list_num = len;
1018
1019 len = 0;
1020 list_for_each_entry(alias, &pmu->aliases, list) {
1021 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1022 char *tmp = strchr(alias->name, '-');
1023
1024 if (tmp != NULL) {
1025 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1026 PMU_EVENT_SYMBOL_PREFIX);
1027 p++;
1028 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1029 len += 2;
1030 } else {
1031 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1032 len++;
1033 }
1034 }
1035 qsort(perf_pmu_events_list, len,
1036 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1037
1038 return;
1039err:
1040 perf_pmu__parse_cleanup();
1041}
1042
1043enum perf_pmu_event_symbol_type
1044perf_pmu__parse_check(const char *name)
1045{
1046 struct perf_pmu_event_symbol p, *r;
1047
1048 /* scan kernel pmu events from sysfs if needed */
1049 if (perf_pmu_events_list_num == 0)
1050 perf_pmu__parse_init();
1051 /*
1052 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1053 * cpu-cycles has been handled by hardcode.
1054 * So it must be cpu// events, not kernel pmu event.
1055 */
1056 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1057 return PMU_EVENT_SYMBOL_ERR;
1058
1059 p.symbol = strdup(name);
1060 r = bsearch(&p, perf_pmu_events_list,
1061 (size_t) perf_pmu_events_list_num,
1062 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1063 free(p.symbol);
1064 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1065}
1066
90e2b22d 1067static int parse_events__scanner(const char *str, void *data, int start_token)
61c45981 1068{
89812fc8 1069 YY_BUFFER_STATE buffer;
ac20de6f 1070 void *scanner;
46010ab2 1071 int ret;
bcd3279f 1072
90e2b22d 1073 ret = parse_events_lex_init_extra(start_token, &scanner);
ac20de6f
ZY
1074 if (ret)
1075 return ret;
1076
1077 buffer = parse_events__scan_string(str, scanner);
a21ca2ca 1078
82ba1f2f
JO
1079#ifdef PARSER_DEBUG
1080 parse_events_debug = 1;
1081#endif
ac20de6f
ZY
1082 ret = parse_events_parse(data, scanner);
1083
1084 parse_events__flush_buffer(buffer, scanner);
1085 parse_events__delete_buffer(buffer, scanner);
1086 parse_events_lex_destroy(scanner);
1087 return ret;
1088}
bcd3279f 1089
90e2b22d
JO
1090/*
1091 * parse event config string, return a list of event terms.
1092 */
1093int parse_events_terms(struct list_head *terms, const char *str)
1094{
23b6339b 1095 struct parse_events_terms data = {
90e2b22d
JO
1096 .terms = NULL,
1097 };
1098 int ret;
1099
1100 ret = parse_events__scanner(str, &data, PE_START_TERMS);
1101 if (!ret) {
1102 list_splice(data.terms, terms);
74cf249d 1103 zfree(&data.terms);
90e2b22d
JO
1104 return 0;
1105 }
1106
b2c34fde
AH
1107 if (data.terms)
1108 parse_events__free_terms(data.terms);
90e2b22d
JO
1109 return ret;
1110}
1111
b39b8393
JO
1112int parse_events(struct perf_evlist *evlist, const char *str,
1113 struct parse_events_error *err)
ac20de6f 1114{
23b6339b 1115 struct parse_events_evlist data = {
b39b8393
JO
1116 .list = LIST_HEAD_INIT(data.list),
1117 .idx = evlist->nr_entries,
1118 .error = err,
ac20de6f
ZY
1119 };
1120 int ret;
bcd3279f 1121
90e2b22d 1122 ret = parse_events__scanner(str, &data, PE_START_EVENTS);
dcb4e102 1123 perf_pmu__parse_cleanup();
89812fc8 1124 if (!ret) {
46010ab2 1125 int entries = data.idx - evlist->nr_entries;
15bfd2cc
WN
1126 struct perf_evsel *last;
1127
46010ab2 1128 perf_evlist__splice_list_tail(evlist, &data.list, entries);
97f63e4a 1129 evlist->nr_groups += data.nr_groups;
15bfd2cc
WN
1130 last = perf_evlist__last(evlist);
1131 last->cmdline_group_boundary = true;
1132
89812fc8
JO
1133 return 0;
1134 }
bcd3279f 1135
5d7be90e
JO
1136 /*
1137 * There are 2 users - builtin-record and builtin-test objects.
1138 * Both call perf_evlist__delete in case of error, so we dont
1139 * need to bother.
1140 */
bcd3279f 1141 return ret;
8ad8db37
IM
1142}
1143
b39b8393
JO
1144#define MAX_WIDTH 1000
1145static int get_term_width(void)
1146{
1147 struct winsize ws;
1148
1149 get_term_dimensions(&ws);
1150 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1151}
1152
1153static void parse_events_print_error(struct parse_events_error *err,
1154 const char *event)
1155{
1156 const char *str = "invalid or unsupported event: ";
1157 char _buf[MAX_WIDTH];
1158 char *buf = (char *) event;
1159 int idx = 0;
1160
1161 if (err->str) {
1162 /* -2 for extra '' in the final fprintf */
1163 int width = get_term_width() - 2;
1164 int len_event = strlen(event);
1165 int len_str, max_len, cut = 0;
1166
1167 /*
1168 * Maximum error index indent, we will cut
1169 * the event string if it's bigger.
1170 */
1171 int max_err_idx = 10;
1172
1173 /*
1174 * Let's be specific with the message when
1175 * we have the precise error.
1176 */
1177 str = "event syntax error: ";
1178 len_str = strlen(str);
1179 max_len = width - len_str;
1180
1181 buf = _buf;
1182
1183 /* We're cutting from the beggining. */
1184 if (err->idx > max_err_idx)
1185 cut = err->idx - max_err_idx;
1186
1187 strncpy(buf, event + cut, max_len);
1188
1189 /* Mark cut parts with '..' on both sides. */
1190 if (cut)
1191 buf[0] = buf[1] = '.';
1192
1193 if ((len_event - cut) > max_len) {
1194 buf[max_len - 1] = buf[max_len - 2] = '.';
1195 buf[max_len] = 0;
1196 }
1197
1198 idx = len_str + err->idx - cut;
1199 }
1200
1201 fprintf(stderr, "%s'%s'\n", str, buf);
1202 if (idx) {
1203 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
1204 if (err->help)
1205 fprintf(stderr, "\n%s\n", err->help);
1206 free(err->str);
1207 free(err->help);
1208 }
1209
1210 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
1211}
1212
1213#undef MAX_WIDTH
1214
f120f9d5 1215int parse_events_option(const struct option *opt, const char *str,
1d037ca1 1216 int unset __maybe_unused)
f120f9d5
JO
1217{
1218 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
b39b8393
JO
1219 struct parse_events_error err = { .idx = 0, };
1220 int ret = parse_events(evlist, str, &err);
1221
1222 if (ret)
1223 parse_events_print_error(&err, str);
9175ce1f 1224
9175ce1f 1225 return ret;
f120f9d5
JO
1226}
1227
4ba1faa1
WN
1228static int
1229foreach_evsel_in_last_glob(struct perf_evlist *evlist,
1230 int (*func)(struct perf_evsel *evsel,
1231 const void *arg),
1232 const void *arg)
c171b552 1233{
69aad6f1 1234 struct perf_evsel *last = NULL;
4ba1faa1 1235 int err;
c171b552 1236
361c99a6 1237 if (evlist->nr_entries > 0)
0c21f736 1238 last = perf_evlist__last(evlist);
69aad6f1 1239
15bfd2cc 1240 do {
4ba1faa1
WN
1241 err = (*func)(last, arg);
1242 if (err)
15bfd2cc 1243 return -1;
4ba1faa1
WN
1244 if (!last)
1245 return 0;
15bfd2cc
WN
1246
1247 if (last->node.prev == &evlist->entries)
1248 return 0;
1249 last = list_entry(last->node.prev, struct perf_evsel, node);
1250 } while (!last->cmdline_group_boundary);
c171b552
LZ
1251
1252 return 0;
1253}
1254
4ba1faa1
WN
1255static int set_filter(struct perf_evsel *evsel, const void *arg)
1256{
1257 const char *str = arg;
1258
1259 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
1260 fprintf(stderr,
1261 "--filter option should follow a -e tracepoint option\n");
1262 return -1;
1263 }
1264
1265 if (perf_evsel__append_filter(evsel, "&&", str) < 0) {
1266 fprintf(stderr,
1267 "not enough memory to hold filter string\n");
1268 return -1;
1269 }
1270
1271 return 0;
1272}
1273
1274int parse_filter(const struct option *opt, const char *str,
1275 int unset __maybe_unused)
1276{
1277 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1278
1279 return foreach_evsel_in_last_glob(evlist, set_filter,
1280 (const void *)str);
1281}
1282
1283static int add_exclude_perf_filter(struct perf_evsel *evsel,
1284 const void *arg __maybe_unused)
1285{
1286 char new_filter[64];
1287
1288 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
1289 fprintf(stderr,
1290 "--exclude-perf option should follow a -e tracepoint option\n");
1291 return -1;
1292 }
1293
1294 snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
1295
1296 if (perf_evsel__append_filter(evsel, "&&", new_filter) < 0) {
1297 fprintf(stderr,
1298 "not enough memory to hold filter string\n");
1299 return -1;
1300 }
1301
1302 return 0;
1303}
1304
1305int exclude_perf(const struct option *opt,
1306 const char *arg __maybe_unused,
1307 int unset __maybe_unused)
1308{
1309 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1310
1311 return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
1312 NULL);
1313}
1314
86847b62 1315static const char * const event_type_descriptors[] = {
86847b62
TG
1316 "Hardware event",
1317 "Software event",
1318 "Tracepoint event",
1319 "Hardware cache event",
41bdcb23
LW
1320 "Raw hardware event descriptor",
1321 "Hardware breakpoint",
86847b62
TG
1322};
1323
ab0e4800
YS
1324static int cmp_string(const void *a, const void *b)
1325{
1326 const char * const *as = a;
1327 const char * const *bs = b;
1328
1329 return strcmp(*as, *bs);
1330}
1331
f6bdafef
JB
1332/*
1333 * Print the events from <debugfs_mount_point>/tracing/events
1334 */
1335
a3277d2d
FW
1336void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
1337 bool name_only)
f6bdafef
JB
1338{
1339 DIR *sys_dir, *evt_dir;
1340 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
f6bdafef 1341 char evt_path[MAXPATHLEN];
725b1368 1342 char dir_path[MAXPATHLEN];
ab0e4800
YS
1343 char **evt_list = NULL;
1344 unsigned int evt_i = 0, evt_num = 0;
1345 bool evt_num_known = false;
f6bdafef 1346
ab0e4800 1347restart:
ebf294bf 1348 sys_dir = opendir(tracing_events_path);
f6bdafef 1349 if (!sys_dir)
725b1368 1350 return;
6b58e7f1 1351
ab0e4800
YS
1352 if (evt_num_known) {
1353 evt_list = zalloc(sizeof(char *) * evt_num);
1354 if (!evt_list)
1355 goto out_close_sys_dir;
1356 }
1357
6b58e7f1 1358 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
48000a1a 1359 if (subsys_glob != NULL &&
668b8788
ACM
1360 !strglobmatch(sys_dirent.d_name, subsys_glob))
1361 continue;
725b1368 1362
ebf294bf 1363 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
725b1368
ED
1364 sys_dirent.d_name);
1365 evt_dir = opendir(dir_path);
1366 if (!evt_dir)
6b58e7f1 1367 continue;
725b1368 1368
6b58e7f1 1369 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
48000a1a 1370 if (event_glob != NULL &&
668b8788
ACM
1371 !strglobmatch(evt_dirent.d_name, event_glob))
1372 continue;
1373
ab0e4800
YS
1374 if (!evt_num_known) {
1375 evt_num++;
a3277d2d
FW
1376 continue;
1377 }
1378
f6bdafef
JB
1379 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1380 sys_dirent.d_name, evt_dirent.d_name);
ab0e4800
YS
1381
1382 evt_list[evt_i] = strdup(evt_path);
1383 if (evt_list[evt_i] == NULL)
1384 goto out_close_evt_dir;
1385 evt_i++;
f6bdafef
JB
1386 }
1387 closedir(evt_dir);
1388 }
f6bdafef 1389 closedir(sys_dir);
ab0e4800
YS
1390
1391 if (!evt_num_known) {
1392 evt_num_known = true;
1393 goto restart;
1394 }
1395 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1396 evt_i = 0;
1397 while (evt_i < evt_num) {
1398 if (name_only) {
1399 printf("%s ", evt_list[evt_i++]);
1400 continue;
1401 }
1402 printf(" %-50s [%s]\n", evt_list[evt_i++],
1403 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1404 }
1405 if (evt_num)
1406 printf("\n");
1407
1408out_free:
1409 evt_num = evt_i;
1410 for (evt_i = 0; evt_i < evt_num; evt_i++)
1411 zfree(&evt_list[evt_i]);
1412 zfree(&evt_list);
1413 return;
1414
1415out_close_evt_dir:
1416 closedir(evt_dir);
1417out_close_sys_dir:
1418 closedir(sys_dir);
1419
1420 printf("FATAL: not enough memory to print %s\n",
1421 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1422 if (evt_list)
1423 goto out_free;
f6bdafef
JB
1424}
1425
20c457b8
TR
1426/*
1427 * Check whether event is in <debugfs_mount_point>/tracing/events
1428 */
1429
1430int is_valid_tracepoint(const char *event_string)
1431{
1432 DIR *sys_dir, *evt_dir;
1433 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
1434 char evt_path[MAXPATHLEN];
1435 char dir_path[MAXPATHLEN];
1436
ebf294bf 1437 sys_dir = opendir(tracing_events_path);
20c457b8
TR
1438 if (!sys_dir)
1439 return 0;
1440
1441 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1442
ebf294bf 1443 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
20c457b8
TR
1444 sys_dirent.d_name);
1445 evt_dir = opendir(dir_path);
1446 if (!evt_dir)
1447 continue;
1448
1449 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1450 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1451 sys_dirent.d_name, evt_dirent.d_name);
1452 if (!strcmp(evt_path, event_string)) {
1453 closedir(evt_dir);
1454 closedir(sys_dir);
1455 return 1;
1456 }
1457 }
1458 closedir(evt_dir);
1459 }
1460 closedir(sys_dir);
1461 return 0;
1462}
1463
b41f1cec
NK
1464static bool is_event_supported(u8 type, unsigned config)
1465{
1466 bool ret = true;
88fee52e 1467 int open_return;
b41f1cec
NK
1468 struct perf_evsel *evsel;
1469 struct perf_event_attr attr = {
1470 .type = type,
1471 .config = config,
1472 .disabled = 1,
b41f1cec
NK
1473 };
1474 struct {
1475 struct thread_map map;
1476 int threads[1];
1477 } tmap = {
1478 .map.nr = 1,
1479 .threads = { 0 },
1480 };
1481
ef503831 1482 evsel = perf_evsel__new(&attr);
b41f1cec 1483 if (evsel) {
88fee52e
VW
1484 open_return = perf_evsel__open(evsel, NULL, &tmap.map);
1485 ret = open_return >= 0;
1486
1487 if (open_return == -EACCES) {
1488 /*
1489 * This happens if the paranoid value
1490 * /proc/sys/kernel/perf_event_paranoid is set to 2
1491 * Re-run with exclude_kernel set; we don't do that
1492 * by default as some ARM machines do not support it.
1493 *
1494 */
1495 evsel->attr.exclude_kernel = 1;
1496 ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0;
1497 }
b41f1cec
NK
1498 perf_evsel__delete(evsel);
1499 }
1500
1501 return ret;
1502}
1503
a3277d2d 1504int print_hwcache_events(const char *event_glob, bool name_only)
668b8788 1505{
ab0e4800 1506 unsigned int type, op, i, evt_i = 0, evt_num = 0;
0b668bc9 1507 char name[64];
ab0e4800
YS
1508 char **evt_list = NULL;
1509 bool evt_num_known = false;
1510
1511restart:
1512 if (evt_num_known) {
1513 evt_list = zalloc(sizeof(char *) * evt_num);
1514 if (!evt_list)
1515 goto out_enomem;
1516 }
668b8788
ACM
1517
1518 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1519 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1520 /* skip invalid cache type */
0b668bc9 1521 if (!perf_evsel__is_cache_op_valid(type, op))
668b8788
ACM
1522 continue;
1523
1524 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
0b668bc9
ACM
1525 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
1526 name, sizeof(name));
947b4ad1 1527 if (event_glob != NULL && !strglobmatch(name, event_glob))
668b8788
ACM
1528 continue;
1529
b41f1cec
NK
1530 if (!is_event_supported(PERF_TYPE_HW_CACHE,
1531 type | (op << 8) | (i << 16)))
1532 continue;
1533
ab0e4800
YS
1534 if (!evt_num_known) {
1535 evt_num++;
1536 continue;
1537 }
1538
1539 evt_list[evt_i] = strdup(name);
1540 if (evt_list[evt_i] == NULL)
1541 goto out_enomem;
1542 evt_i++;
668b8788
ACM
1543 }
1544 }
1545 }
1546
ab0e4800
YS
1547 if (!evt_num_known) {
1548 evt_num_known = true;
1549 goto restart;
1550 }
1551 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1552 evt_i = 0;
1553 while (evt_i < evt_num) {
1554 if (name_only) {
1555 printf("%s ", evt_list[evt_i++]);
1556 continue;
1557 }
1558 printf(" %-50s [%s]\n", evt_list[evt_i++],
1559 event_type_descriptors[PERF_TYPE_HW_CACHE]);
1560 }
1561 if (evt_num)
dc098b35 1562 printf("\n");
ab0e4800
YS
1563
1564out_free:
1565 evt_num = evt_i;
1566 for (evt_i = 0; evt_i < evt_num; evt_i++)
1567 zfree(&evt_list[evt_i]);
1568 zfree(&evt_list);
1569 return evt_num;
1570
1571out_enomem:
1572 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
1573 if (evt_list)
1574 goto out_free;
1575 return evt_num;
668b8788
ACM
1576}
1577
705750f2 1578void print_symbol_events(const char *event_glob, unsigned type,
a3277d2d
FW
1579 struct event_symbol *syms, unsigned max,
1580 bool name_only)
8ad8db37 1581{
ab0e4800 1582 unsigned int i, evt_i = 0, evt_num = 0;
947b4ad1 1583 char name[MAX_NAME_LEN];
ab0e4800
YS
1584 char **evt_list = NULL;
1585 bool evt_num_known = false;
1586
1587restart:
1588 if (evt_num_known) {
1589 evt_list = zalloc(sizeof(char *) * evt_num);
1590 if (!evt_list)
1591 goto out_enomem;
1592 syms -= max;
1593 }
8ad8db37 1594
1dc12760 1595 for (i = 0; i < max; i++, syms++) {
668b8788 1596
48000a1a 1597 if (event_glob != NULL &&
668b8788
ACM
1598 !(strglobmatch(syms->symbol, event_glob) ||
1599 (syms->alias && strglobmatch(syms->alias, event_glob))))
1600 continue;
8ad8db37 1601
b41f1cec
NK
1602 if (!is_event_supported(type, i))
1603 continue;
1604
ab0e4800
YS
1605 if (!evt_num_known) {
1606 evt_num++;
a3277d2d
FW
1607 continue;
1608 }
1609
ab0e4800 1610 if (!name_only && strlen(syms->alias))
947b4ad1 1611 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
74d5b588 1612 else
947b4ad1 1613 strncpy(name, syms->symbol, MAX_NAME_LEN);
8ad8db37 1614
ab0e4800
YS
1615 evt_list[evt_i] = strdup(name);
1616 if (evt_list[evt_i] == NULL)
1617 goto out_enomem;
1618 evt_i++;
8ad8db37
IM
1619 }
1620
ab0e4800
YS
1621 if (!evt_num_known) {
1622 evt_num_known = true;
1623 goto restart;
1624 }
1625 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1626 evt_i = 0;
1627 while (evt_i < evt_num) {
1628 if (name_only) {
1629 printf("%s ", evt_list[evt_i++]);
1630 continue;
1631 }
1632 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
1633 }
1634 if (evt_num)
668b8788 1635 printf("\n");
ab0e4800
YS
1636
1637out_free:
1638 evt_num = evt_i;
1639 for (evt_i = 0; evt_i < evt_num; evt_i++)
1640 zfree(&evt_list[evt_i]);
1641 zfree(&evt_list);
1642 return;
1643
1644out_enomem:
1645 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
1646 if (evt_list)
1647 goto out_free;
1dc12760
JO
1648}
1649
1650/*
1651 * Print the help text for the event symbols:
1652 */
a3277d2d 1653void print_events(const char *event_glob, bool name_only)
1dc12760 1654{
1dc12760 1655 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
a3277d2d 1656 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
1dc12760
JO
1657
1658 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
a3277d2d 1659 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
1dc12760 1660
a3277d2d 1661 print_hwcache_events(event_glob, name_only);
668b8788 1662
dc098b35
AK
1663 print_pmu_events(event_glob, name_only);
1664
668b8788
ACM
1665 if (event_glob != NULL)
1666 return;
73c24cb8 1667
a3277d2d 1668 if (!name_only) {
a3277d2d
FW
1669 printf(" %-50s [%s]\n",
1670 "rNNN",
1671 event_type_descriptors[PERF_TYPE_RAW]);
1672 printf(" %-50s [%s]\n",
1673 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
1674 event_type_descriptors[PERF_TYPE_RAW]);
af3df2cf 1675 printf(" (see 'man perf-list' on how to encode it)\n");
a3277d2d
FW
1676 printf("\n");
1677
1678 printf(" %-50s [%s]\n",
3741eb9f 1679 "mem:<addr>[/len][:access]",
41bdcb23 1680 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
a3277d2d
FW
1681 printf("\n");
1682 }
1b290d67 1683
a3277d2d 1684 print_tracepoint_events(NULL, NULL, name_only);
8ad8db37 1685}
8f707d84 1686
6cee6cd3 1687int parse_events__is_hardcoded_term(struct parse_events_term *term)
8f707d84 1688{
16fa7e82 1689 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
8f707d84
JO
1690}
1691
6cee6cd3 1692static int new_term(struct parse_events_term **_term, int type_val,
16fa7e82 1693 int type_term, char *config,
cecf3a2e 1694 char *str, u64 num, int err_term, int err_val)
8f707d84 1695{
6cee6cd3 1696 struct parse_events_term *term;
8f707d84
JO
1697
1698 term = zalloc(sizeof(*term));
1699 if (!term)
1700 return -ENOMEM;
1701
1702 INIT_LIST_HEAD(&term->list);
16fa7e82
JO
1703 term->type_val = type_val;
1704 term->type_term = type_term;
8f707d84 1705 term->config = config;
cecf3a2e
JO
1706 term->err_term = err_term;
1707 term->err_val = err_val;
8f707d84 1708
16fa7e82 1709 switch (type_val) {
8f707d84
JO
1710 case PARSE_EVENTS__TERM_TYPE_NUM:
1711 term->val.num = num;
1712 break;
1713 case PARSE_EVENTS__TERM_TYPE_STR:
1714 term->val.str = str;
1715 break;
1716 default:
4be8be6b 1717 free(term);
8f707d84
JO
1718 return -EINVAL;
1719 }
1720
1721 *_term = term;
1722 return 0;
1723}
1724
6cee6cd3 1725int parse_events_term__num(struct parse_events_term **term,
cecf3a2e 1726 int type_term, char *config, u64 num,
bb78ce7d 1727 void *loc_term_, void *loc_val_)
16fa7e82 1728{
bb78ce7d
AH
1729 YYLTYPE *loc_term = loc_term_;
1730 YYLTYPE *loc_val = loc_val_;
1731
16fa7e82 1732 return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
cecf3a2e
JO
1733 config, NULL, num,
1734 loc_term ? loc_term->first_column : 0,
1735 loc_val ? loc_val->first_column : 0);
16fa7e82
JO
1736}
1737
6cee6cd3 1738int parse_events_term__str(struct parse_events_term **term,
cecf3a2e 1739 int type_term, char *config, char *str,
bb78ce7d 1740 void *loc_term_, void *loc_val_)
16fa7e82 1741{
bb78ce7d
AH
1742 YYLTYPE *loc_term = loc_term_;
1743 YYLTYPE *loc_val = loc_val_;
1744
16fa7e82 1745 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
cecf3a2e
JO
1746 config, str, 0,
1747 loc_term ? loc_term->first_column : 0,
1748 loc_val ? loc_val->first_column : 0);
16fa7e82
JO
1749}
1750
6cee6cd3 1751int parse_events_term__sym_hw(struct parse_events_term **term,
1d33d6dc
JO
1752 char *config, unsigned idx)
1753{
1754 struct event_symbol *sym;
1755
1756 BUG_ON(idx >= PERF_COUNT_HW_MAX);
1757 sym = &event_symbols_hw[idx];
1758
1759 if (config)
1760 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1761 PARSE_EVENTS__TERM_TYPE_USER, config,
cecf3a2e 1762 (char *) sym->symbol, 0, 0, 0);
1d33d6dc
JO
1763 else
1764 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1765 PARSE_EVENTS__TERM_TYPE_USER,
cecf3a2e
JO
1766 (char *) "event", (char *) sym->symbol,
1767 0, 0, 0);
1d33d6dc
JO
1768}
1769
6cee6cd3
ACM
1770int parse_events_term__clone(struct parse_events_term **new,
1771 struct parse_events_term *term)
a6146d50
ZY
1772{
1773 return new_term(new, term->type_val, term->type_term, term->config,
cecf3a2e
JO
1774 term->val.str, term->val.num,
1775 term->err_term, term->err_val);
a6146d50
ZY
1776}
1777
8f707d84
JO
1778void parse_events__free_terms(struct list_head *terms)
1779{
6cee6cd3 1780 struct parse_events_term *term, *h;
8f707d84
JO
1781
1782 list_for_each_entry_safe(term, h, terms, list)
1783 free(term);
8f707d84 1784}
b39b8393
JO
1785
1786void parse_events_evlist_error(struct parse_events_evlist *data,
1787 int idx, const char *str)
1788{
1789 struct parse_events_error *err = data->error;
1790
a6ced2be
AH
1791 if (!err)
1792 return;
b39b8393
JO
1793 err->idx = idx;
1794 err->str = strdup(str);
1795 WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
1796}
This page took 0.395589 seconds and 5 git commands to generate.