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