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