perf evsel: The tracepoint constructor should store sys:name
[deliverable/linux.git] / tools / perf / util / parse-events.c
CommitLineData
1b290d67 1#include "../../../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"
a0055ae2 9#include "string.h"
5aab621b 10#include "symbol.h"
5beeded1 11#include "cache.h"
8755a8f2 12#include "header.h"
549104f2 13#include "debugfs.h"
ac20de6f 14#include "parse-events-bison.h"
90e2b22d 15#define YY_EXTRA_TYPE int
89812fc8 16#include "parse-events-flex.h"
5f537a26 17#include "pmu.h"
89812fc8
JO
18
19#define MAX_NAME_LEN 100
8ad8db37 20
8ad8db37 21struct event_symbol {
83a0944f
IM
22 const char *symbol;
23 const char *alias;
8ad8db37
IM
24};
25
82ba1f2f
JO
26#ifdef PARSER_DEBUG
27extern int parse_events_debug;
28#endif
ac20de6f 29int parse_events_parse(void *data, void *scanner);
bcd3279f 30
1dc12760
JO
31static struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
32 [PERF_COUNT_HW_CPU_CYCLES] = {
33 .symbol = "cpu-cycles",
34 .alias = "cycles",
35 },
36 [PERF_COUNT_HW_INSTRUCTIONS] = {
37 .symbol = "instructions",
38 .alias = "",
39 },
40 [PERF_COUNT_HW_CACHE_REFERENCES] = {
41 .symbol = "cache-references",
42 .alias = "",
43 },
44 [PERF_COUNT_HW_CACHE_MISSES] = {
45 .symbol = "cache-misses",
46 .alias = "",
47 },
48 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
49 .symbol = "branch-instructions",
50 .alias = "branches",
51 },
52 [PERF_COUNT_HW_BRANCH_MISSES] = {
53 .symbol = "branch-misses",
54 .alias = "",
55 },
56 [PERF_COUNT_HW_BUS_CYCLES] = {
57 .symbol = "bus-cycles",
58 .alias = "",
59 },
60 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
61 .symbol = "stalled-cycles-frontend",
62 .alias = "idle-cycles-frontend",
63 },
64 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
65 .symbol = "stalled-cycles-backend",
66 .alias = "idle-cycles-backend",
67 },
68 [PERF_COUNT_HW_REF_CPU_CYCLES] = {
69 .symbol = "ref-cycles",
70 .alias = "",
71 },
72};
73
74static struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
75 [PERF_COUNT_SW_CPU_CLOCK] = {
76 .symbol = "cpu-clock",
77 .alias = "",
78 },
79 [PERF_COUNT_SW_TASK_CLOCK] = {
80 .symbol = "task-clock",
81 .alias = "",
82 },
83 [PERF_COUNT_SW_PAGE_FAULTS] = {
84 .symbol = "page-faults",
85 .alias = "faults",
86 },
87 [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
88 .symbol = "context-switches",
89 .alias = "cs",
90 },
91 [PERF_COUNT_SW_CPU_MIGRATIONS] = {
92 .symbol = "cpu-migrations",
93 .alias = "migrations",
94 },
95 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
96 .symbol = "minor-faults",
97 .alias = "",
98 },
99 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
100 .symbol = "major-faults",
101 .alias = "",
102 },
103 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
104 .symbol = "alignment-faults",
105 .alias = "",
106 },
107 [PERF_COUNT_SW_EMULATION_FAULTS] = {
108 .symbol = "emulation-faults",
109 .alias = "",
110 },
8ad8db37
IM
111};
112
cdd6c482
IM
113#define __PERF_EVENT_FIELD(config, name) \
114 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
5242519b 115
1fc570ad 116#define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
cdd6c482 117#define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
1fc570ad 118#define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
cdd6c482 119#define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
5242519b 120
6b58e7f1 121#define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
f6bdafef 122 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
6b58e7f1 123 if (sys_dirent.d_type == DT_DIR && \
f6bdafef
JB
124 (strcmp(sys_dirent.d_name, ".")) && \
125 (strcmp(sys_dirent.d_name, "..")))
126
ae07b63f
PZ
127static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
128{
129 char evt_path[MAXPATHLEN];
130 int fd;
131
ebf294bf 132 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
ae07b63f
PZ
133 sys_dir->d_name, evt_dir->d_name);
134 fd = open(evt_path, O_RDONLY);
135 if (fd < 0)
136 return -EINVAL;
137 close(fd);
138
139 return 0;
140}
141
6b58e7f1 142#define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
f6bdafef 143 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
6b58e7f1 144 if (evt_dirent.d_type == DT_DIR && \
f6bdafef 145 (strcmp(evt_dirent.d_name, ".")) && \
ae07b63f
PZ
146 (strcmp(evt_dirent.d_name, "..")) && \
147 (!tp_event_has_id(&sys_dirent, &evt_dirent)))
f6bdafef 148
270bbbe8 149#define MAX_EVENT_LENGTH 512
f6bdafef 150
f6bdafef 151
1ef2ed10 152struct tracepoint_path *tracepoint_id_to_path(u64 config)
f6bdafef 153{
1ef2ed10 154 struct tracepoint_path *path = NULL;
f6bdafef
JB
155 DIR *sys_dir, *evt_dir;
156 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
8aa8a7c8 157 char id_buf[24];
725b1368 158 int fd;
f6bdafef
JB
159 u64 id;
160 char evt_path[MAXPATHLEN];
725b1368 161 char dir_path[MAXPATHLEN];
f6bdafef 162
ebf294bf 163 if (debugfs_valid_mountpoint(tracing_events_path))
1ef2ed10 164 return NULL;
f6bdafef 165
ebf294bf 166 sys_dir = opendir(tracing_events_path);
f6bdafef 167 if (!sys_dir)
725b1368 168 return NULL;
6b58e7f1
UD
169
170 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
725b1368 171
ebf294bf 172 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
725b1368
ED
173 sys_dirent.d_name);
174 evt_dir = opendir(dir_path);
175 if (!evt_dir)
6b58e7f1 176 continue;
725b1368 177
6b58e7f1 178 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
725b1368
ED
179
180 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
f6bdafef 181 evt_dirent.d_name);
725b1368 182 fd = open(evt_path, O_RDONLY);
f6bdafef
JB
183 if (fd < 0)
184 continue;
185 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
186 close(fd);
187 continue;
188 }
189 close(fd);
190 id = atoll(id_buf);
191 if (id == config) {
192 closedir(evt_dir);
193 closedir(sys_dir);
59b4caeb 194 path = zalloc(sizeof(*path));
1ef2ed10
FW
195 path->system = malloc(MAX_EVENT_LENGTH);
196 if (!path->system) {
197 free(path);
198 return NULL;
199 }
200 path->name = malloc(MAX_EVENT_LENGTH);
201 if (!path->name) {
202 free(path->system);
203 free(path);
204 return NULL;
205 }
206 strncpy(path->system, sys_dirent.d_name,
207 MAX_EVENT_LENGTH);
208 strncpy(path->name, evt_dirent.d_name,
209 MAX_EVENT_LENGTH);
210 return path;
f6bdafef
JB
211 }
212 }
213 closedir(evt_dir);
214 }
215
f6bdafef 216 closedir(sys_dir);
1ef2ed10
FW
217 return NULL;
218}
219
1424dc96
DA
220const char *event_type(int type)
221{
222 switch (type) {
223 case PERF_TYPE_HARDWARE:
224 return "hardware";
225
226 case PERF_TYPE_SOFTWARE:
227 return "software";
228
229 case PERF_TYPE_TRACEPOINT:
230 return "tracepoint";
231
232 case PERF_TYPE_HW_CACHE:
233 return "hardware-cache";
234
235 default:
236 break;
237 }
238
239 return "unknown";
240}
241
7ae92e74
YZ
242
243
244static int __add_event(struct list_head **_list, int *idx,
245 struct perf_event_attr *attr,
246 char *name, struct cpu_map *cpus)
89812fc8
JO
247{
248 struct perf_evsel *evsel;
b847cbdc
JO
249 struct list_head *list = *_list;
250
251 if (!list) {
252 list = malloc(sizeof(*list));
253 if (!list)
254 return -ENOMEM;
255 INIT_LIST_HEAD(list);
256 }
89812fc8
JO
257
258 event_attr_init(attr);
259
260 evsel = perf_evsel__new(attr, (*idx)++);
b847cbdc
JO
261 if (!evsel) {
262 free(list);
89812fc8 263 return -ENOMEM;
b847cbdc 264 }
89812fc8 265
7ae92e74 266 evsel->cpus = cpus;
9db1763c
ACM
267 if (name)
268 evsel->name = strdup(name);
b847cbdc
JO
269 list_add_tail(&evsel->node, list);
270 *_list = list;
89812fc8
JO
271 return 0;
272}
273
7ae92e74
YZ
274static int add_event(struct list_head **_list, int *idx,
275 struct perf_event_attr *attr, char *name)
276{
277 return __add_event(_list, idx, attr, name, NULL);
278}
279
0b668bc9 280static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
8326f44d
IM
281{
282 int i, j;
61c45981 283 int n, longest = -1;
8326f44d
IM
284
285 for (i = 0; i < size; i++) {
0b668bc9 286 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
61c45981 287 n = strlen(names[i][j]);
89812fc8 288 if (n > longest && !strncasecmp(str, names[i][j], n))
61c45981
PM
289 longest = n;
290 }
89812fc8 291 if (longest > 0)
61c45981 292 return i;
8326f44d
IM
293 }
294
8953645f 295 return -1;
8326f44d
IM
296}
297
b847cbdc 298int parse_events_add_cache(struct list_head **list, int *idx,
89812fc8 299 char *type, char *op_result1, char *op_result2)
8326f44d 300{
89812fc8
JO
301 struct perf_event_attr attr;
302 char name[MAX_NAME_LEN];
61c45981 303 int cache_type = -1, cache_op = -1, cache_result = -1;
89812fc8
JO
304 char *op_result[2] = { op_result1, op_result2 };
305 int i, n;
8326f44d 306
8326f44d
IM
307 /*
308 * No fallback - if we cannot get a clear cache type
309 * then bail out:
310 */
0b668bc9 311 cache_type = parse_aliases(type, perf_evsel__hw_cache,
89812fc8 312 PERF_COUNT_HW_CACHE_MAX);
8326f44d 313 if (cache_type == -1)
89812fc8
JO
314 return -EINVAL;
315
316 n = snprintf(name, MAX_NAME_LEN, "%s", type);
61c45981 317
89812fc8
JO
318 for (i = 0; (i < 2) && (op_result[i]); i++) {
319 char *str = op_result[i];
320
275ef387 321 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
61c45981
PM
322
323 if (cache_op == -1) {
0b668bc9 324 cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
89812fc8 325 PERF_COUNT_HW_CACHE_OP_MAX);
61c45981 326 if (cache_op >= 0) {
0b668bc9 327 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
89812fc8 328 return -EINVAL;
61c45981
PM
329 continue;
330 }
331 }
332
333 if (cache_result == -1) {
0b668bc9
ACM
334 cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
335 PERF_COUNT_HW_CACHE_RESULT_MAX);
61c45981
PM
336 if (cache_result >= 0)
337 continue;
338 }
61c45981 339 }
8326f44d 340
8326f44d
IM
341 /*
342 * Fall back to reads:
343 */
8953645f
IM
344 if (cache_op == -1)
345 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
8326f44d 346
8326f44d
IM
347 /*
348 * Fall back to accesses:
349 */
350 if (cache_result == -1)
351 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
352
89812fc8
JO
353 memset(&attr, 0, sizeof(attr));
354 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
355 attr.type = PERF_TYPE_HW_CACHE;
356 return add_event(list, idx, &attr, name);
bcd3279f
FW
357}
358
b847cbdc 359static int add_tracepoint(struct list_head **list, int *idx,
89812fc8 360 char *sys_name, char *evt_name)
bcd3279f 361{
89812fc8
JO
362 struct perf_event_attr attr;
363 char name[MAX_NAME_LEN];
bcd3279f
FW
364 char evt_path[MAXPATHLEN];
365 char id_buf[4];
366 u64 id;
367 int fd;
368
ebf294bf 369 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
bcd3279f
FW
370 sys_name, evt_name);
371
372 fd = open(evt_path, O_RDONLY);
373 if (fd < 0)
89812fc8 374 return -1;
bcd3279f
FW
375
376 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
377 close(fd);
89812fc8 378 return -1;
bcd3279f
FW
379 }
380
381 close(fd);
382 id = atoll(id_buf);
bcd3279f 383
89812fc8
JO
384 memset(&attr, 0, sizeof(attr));
385 attr.config = id;
386 attr.type = PERF_TYPE_TRACEPOINT;
387 attr.sample_type |= PERF_SAMPLE_RAW;
388 attr.sample_type |= PERF_SAMPLE_TIME;
389 attr.sample_type |= PERF_SAMPLE_CPU;
0983cc0d 390 attr.sample_type |= PERF_SAMPLE_PERIOD;
89812fc8 391 attr.sample_period = 1;
5710fcad 392
89812fc8
JO
393 snprintf(name, MAX_NAME_LEN, "%s:%s", sys_name, evt_name);
394 return add_event(list, idx, &attr, name);
8326f44d
IM
395}
396
b847cbdc 397static int add_tracepoint_multi(struct list_head **list, int *idx,
89812fc8 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
89812fc8 425 return ret;
bcd3279f
FW
426}
427
b847cbdc 428int parse_events_add_tracepoint(struct list_head **list, int *idx,
89812fc8 429 char *sys, char *event)
f6bdafef 430{
89812fc8 431 int ret;
f6bdafef 432
89812fc8
JO
433 ret = debugfs_valid_mountpoint(tracing_events_path);
434 if (ret)
435 return ret;
f6bdafef 436
89812fc8
JO
437 return strpbrk(event, "*?") ?
438 add_tracepoint_multi(list, idx, sys, event) :
439 add_tracepoint(list, idx, sys, event);
f6bdafef
JB
440}
441
89812fc8
JO
442static int
443parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
1b290d67
FW
444{
445 int i;
446
447 for (i = 0; i < 3; i++) {
89812fc8 448 if (!type || !type[i])
1b290d67
FW
449 break;
450
7582732f
JO
451#define CHECK_SET_TYPE(bit) \
452do { \
453 if (attr->bp_type & bit) \
454 return -EINVAL; \
455 else \
456 attr->bp_type |= bit; \
457} while (0)
458
1b290d67
FW
459 switch (type[i]) {
460 case 'r':
7582732f 461 CHECK_SET_TYPE(HW_BREAKPOINT_R);
1b290d67
FW
462 break;
463 case 'w':
7582732f 464 CHECK_SET_TYPE(HW_BREAKPOINT_W);
1b290d67
FW
465 break;
466 case 'x':
7582732f 467 CHECK_SET_TYPE(HW_BREAKPOINT_X);
1b290d67
FW
468 break;
469 default:
89812fc8 470 return -EINVAL;
1b290d67
FW
471 }
472 }
89812fc8 473
7582732f
JO
474#undef CHECK_SET_TYPE
475
1b290d67
FW
476 if (!attr->bp_type) /* Default */
477 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
478
89812fc8 479 return 0;
1b290d67
FW
480}
481
b847cbdc 482int parse_events_add_breakpoint(struct list_head **list, int *idx,
89812fc8 483 void *ptr, char *type)
1b290d67 484{
89812fc8 485 struct perf_event_attr attr;
1b290d67 486
89812fc8 487 memset(&attr, 0, sizeof(attr));
9fafd98f 488 attr.bp_addr = (unsigned long) ptr;
1b290d67 489
89812fc8
JO
490 if (parse_breakpoint_type(type, &attr))
491 return -EINVAL;
1b290d67 492
aa59a485
FW
493 /*
494 * We should find a nice way to override the access length
495 * Provide some defaults for now
496 */
89812fc8
JO
497 if (attr.bp_type == HW_BREAKPOINT_X)
498 attr.bp_len = sizeof(long);
aa59a485 499 else
89812fc8 500 attr.bp_len = HW_BREAKPOINT_LEN_4;
61c45981 501
89812fc8 502 attr.type = PERF_TYPE_BREAKPOINT;
4a841d65 503 attr.sample_period = 1;
b908debd 504
287e74aa 505 return add_event(list, idx, &attr, NULL);
74d5b588
JSR
506}
507
8f707d84
JO
508static int config_term(struct perf_event_attr *attr,
509 struct parse_events__term *term)
510{
16fa7e82
JO
511#define CHECK_TYPE_VAL(type) \
512do { \
513 if (PARSE_EVENTS__TERM_TYPE_ ## type != term->type_val) \
514 return -EINVAL; \
515} while (0)
516
517 switch (term->type_term) {
8f707d84 518 case PARSE_EVENTS__TERM_TYPE_CONFIG:
16fa7e82 519 CHECK_TYPE_VAL(NUM);
8f707d84
JO
520 attr->config = term->val.num;
521 break;
522 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
16fa7e82 523 CHECK_TYPE_VAL(NUM);
8f707d84
JO
524 attr->config1 = term->val.num;
525 break;
526 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
16fa7e82 527 CHECK_TYPE_VAL(NUM);
8f707d84
JO
528 attr->config2 = term->val.num;
529 break;
530 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
16fa7e82 531 CHECK_TYPE_VAL(NUM);
8f707d84
JO
532 attr->sample_period = term->val.num;
533 break;
534 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
535 /*
536 * TODO uncomment when the field is available
537 * attr->branch_sample_type = term->val.num;
538 */
539 break;
6b5fc39b
JO
540 case PARSE_EVENTS__TERM_TYPE_NAME:
541 CHECK_TYPE_VAL(STR);
542 break;
8f707d84
JO
543 default:
544 return -EINVAL;
545 }
16fa7e82 546
8f707d84 547 return 0;
16fa7e82 548#undef CHECK_TYPE_VAL
8f707d84
JO
549}
550
551static int config_attr(struct perf_event_attr *attr,
552 struct list_head *head, int fail)
553{
554 struct parse_events__term *term;
555
556 list_for_each_entry(term, head, list)
557 if (config_term(attr, term) && fail)
558 return -EINVAL;
559
560 return 0;
561}
562
b847cbdc 563int parse_events_add_numeric(struct list_head **list, int *idx,
b527bab5 564 u32 type, u64 config,
8f707d84 565 struct list_head *head_config)
8ad8db37 566{
89812fc8 567 struct perf_event_attr attr;
61c45981 568
89812fc8
JO
569 memset(&attr, 0, sizeof(attr));
570 attr.type = type;
571 attr.config = config;
8f707d84
JO
572
573 if (head_config &&
574 config_attr(&attr, head_config, 1))
575 return -EINVAL;
576
9db1763c 577 return add_event(list, idx, &attr, NULL);
61c45981 578}
8ad8db37 579
6b5fc39b
JO
580static int parse_events__is_name_term(struct parse_events__term *term)
581{
582 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
583}
584
9db1763c 585static char *pmu_event_name(struct list_head *head_terms)
6b5fc39b
JO
586{
587 struct parse_events__term *term;
588
589 list_for_each_entry(term, head_terms, list)
590 if (parse_events__is_name_term(term))
591 return term->val.str;
592
9db1763c 593 return NULL;
6b5fc39b
JO
594}
595
b847cbdc 596int parse_events_add_pmu(struct list_head **list, int *idx,
5f537a26
JO
597 char *name, struct list_head *head_config)
598{
599 struct perf_event_attr attr;
600 struct perf_pmu *pmu;
601
602 pmu = perf_pmu__find(name);
603 if (!pmu)
604 return -EINVAL;
605
606 memset(&attr, 0, sizeof(attr));
607
a6146d50
ZY
608 if (perf_pmu__check_alias(pmu, head_config))
609 return -EINVAL;
610
5f537a26
JO
611 /*
612 * Configure hardcoded terms first, no need to check
613 * return value when called with fail == 0 ;)
614 */
615 config_attr(&attr, head_config, 0);
616
617 if (perf_pmu__config(pmu, &attr, head_config))
618 return -EINVAL;
619
7ae92e74
YZ
620 return __add_event(list, idx, &attr, pmu_event_name(head_config),
621 pmu->cpus);
5f537a26
JO
622}
623
6a4bb04c
JO
624int parse_events__modifier_group(struct list_head *list,
625 char *event_mod)
89efb029 626{
6a4bb04c
JO
627 return parse_events__modifier_event(list, event_mod, true);
628}
629
63dab225 630void parse_events__set_leader(char *name, struct list_head *list)
6a4bb04c
JO
631{
632 struct perf_evsel *leader;
633
63dab225
ACM
634 __perf_evlist__set_leader(list);
635 leader = list_entry(list->next, struct perf_evsel, node);
6a4bb04c 636 leader->group_name = name ? strdup(name) : NULL;
89efb029
JO
637}
638
5d7be90e
JO
639void parse_events_update_lists(struct list_head *list_event,
640 struct list_head *list_all)
641{
642 /*
643 * Called for single event definition. Update the
89efb029 644 * 'all event' list, and reinit the 'single event'
5d7be90e
JO
645 * list, for next event definition.
646 */
647 list_splice_tail(list_event, list_all);
b847cbdc 648 free(list_event);
5d7be90e
JO
649}
650
f5b1135b
JO
651struct event_modifier {
652 int eu;
653 int ek;
654 int eh;
655 int eH;
656 int eG;
657 int precise;
658 int exclude_GH;
659};
660
661static int get_event_modifier(struct event_modifier *mod, char *str,
662 struct perf_evsel *evsel)
61c45981 663{
f5b1135b
JO
664 int eu = evsel ? evsel->attr.exclude_user : 0;
665 int ek = evsel ? evsel->attr.exclude_kernel : 0;
666 int eh = evsel ? evsel->attr.exclude_hv : 0;
667 int eH = evsel ? evsel->attr.exclude_host : 0;
668 int eG = evsel ? evsel->attr.exclude_guest : 0;
669 int precise = evsel ? evsel->attr.precise_ip : 0;
a21ca2ca 670
f5b1135b
JO
671 int exclude = eu | ek | eh;
672 int exclude_GH = evsel ? evsel->exclude_GH : 0;
673
674 /*
675 * We are here for group and 'GH' was not set as event
676 * modifier and whatever event/group modifier override
677 * default 'GH' setup.
678 */
679 if (evsel && !exclude_GH)
680 eH = eG = 0;
681
682 memset(mod, 0, sizeof(*mod));
ceb53fbf 683
61c45981 684 while (*str) {
ab608344
PZ
685 if (*str == 'u') {
686 if (!exclude)
687 exclude = eu = ek = eh = 1;
61c45981 688 eu = 0;
ab608344
PZ
689 } else if (*str == 'k') {
690 if (!exclude)
691 exclude = eu = ek = eh = 1;
61c45981 692 ek = 0;
ab608344
PZ
693 } else if (*str == 'h') {
694 if (!exclude)
695 exclude = eu = ek = eh = 1;
61c45981 696 eh = 0;
99320cc8
JR
697 } else if (*str == 'G') {
698 if (!exclude_GH)
699 exclude_GH = eG = eH = 1;
700 eG = 0;
701 } else if (*str == 'H') {
702 if (!exclude_GH)
703 exclude_GH = eG = eH = 1;
704 eH = 0;
ab608344
PZ
705 } else if (*str == 'p') {
706 precise++;
707 } else
61c45981 708 break;
ab608344 709
61c45981 710 ++str;
5242519b 711 }
ceb53fbf 712
89812fc8
JO
713 /*
714 * precise ip:
715 *
716 * 0 - SAMPLE_IP can have arbitrary skid
717 * 1 - SAMPLE_IP must have constant skid
718 * 2 - SAMPLE_IP requested to have 0 skid
719 * 3 - SAMPLE_IP must have 0 skid
720 *
721 * See also PERF_RECORD_MISC_EXACT_IP
722 */
723 if (precise > 3)
724 return -EINVAL;
ceb53fbf 725
f5b1135b
JO
726 mod->eu = eu;
727 mod->ek = ek;
728 mod->eh = eh;
729 mod->eH = eH;
730 mod->eG = eG;
731 mod->precise = precise;
732 mod->exclude_GH = exclude_GH;
733 return 0;
734}
735
736int parse_events__modifier_event(struct list_head *list, char *str, bool add)
737{
738 struct perf_evsel *evsel;
739 struct event_modifier mod;
740
741 if (str == NULL)
742 return 0;
743
744 if (!add && get_event_modifier(&mod, str, NULL))
745 return -EINVAL;
746
89812fc8 747 list_for_each_entry(evsel, list, node) {
f5b1135b
JO
748
749 if (add && get_event_modifier(&mod, str, evsel))
750 return -EINVAL;
751
752 evsel->attr.exclude_user = mod.eu;
753 evsel->attr.exclude_kernel = mod.ek;
754 evsel->attr.exclude_hv = mod.eh;
755 evsel->attr.precise_ip = mod.precise;
756 evsel->attr.exclude_host = mod.eH;
757 evsel->attr.exclude_guest = mod.eG;
758 evsel->exclude_GH = mod.exclude_GH;
89812fc8 759 }
ceb53fbf 760
61c45981
PM
761 return 0;
762}
8ad8db37 763
ac2ba9f3
RR
764int parse_events_name(struct list_head *list, char *name)
765{
766 struct perf_evsel *evsel;
767
768 list_for_each_entry(evsel, list, node) {
769 if (!evsel->name)
770 evsel->name = strdup(name);
771 }
772
773 return 0;
774}
775
90e2b22d 776static int parse_events__scanner(const char *str, void *data, int start_token)
61c45981 777{
89812fc8 778 YY_BUFFER_STATE buffer;
ac20de6f 779 void *scanner;
46010ab2 780 int ret;
bcd3279f 781
90e2b22d 782 ret = parse_events_lex_init_extra(start_token, &scanner);
ac20de6f
ZY
783 if (ret)
784 return ret;
785
786 buffer = parse_events__scan_string(str, scanner);
a21ca2ca 787
82ba1f2f
JO
788#ifdef PARSER_DEBUG
789 parse_events_debug = 1;
790#endif
ac20de6f
ZY
791 ret = parse_events_parse(data, scanner);
792
793 parse_events__flush_buffer(buffer, scanner);
794 parse_events__delete_buffer(buffer, scanner);
795 parse_events_lex_destroy(scanner);
796 return ret;
797}
bcd3279f 798
90e2b22d
JO
799/*
800 * parse event config string, return a list of event terms.
801 */
802int parse_events_terms(struct list_head *terms, const char *str)
803{
804 struct parse_events_data__terms data = {
805 .terms = NULL,
806 };
807 int ret;
808
809 ret = parse_events__scanner(str, &data, PE_START_TERMS);
810 if (!ret) {
811 list_splice(data.terms, terms);
812 free(data.terms);
813 return 0;
814 }
815
816 parse_events__free_terms(data.terms);
817 return ret;
818}
819
1d037ca1
IT
820int parse_events(struct perf_evlist *evlist, const char *str,
821 int unset __maybe_unused)
ac20de6f
ZY
822{
823 struct parse_events_data__events data = {
824 .list = LIST_HEAD_INIT(data.list),
825 .idx = evlist->nr_entries,
826 };
827 int ret;
bcd3279f 828
90e2b22d 829 ret = parse_events__scanner(str, &data, PE_START_EVENTS);
89812fc8 830 if (!ret) {
46010ab2
JO
831 int entries = data.idx - evlist->nr_entries;
832 perf_evlist__splice_list_tail(evlist, &data.list, entries);
89812fc8
JO
833 return 0;
834 }
bcd3279f 835
5d7be90e
JO
836 /*
837 * There are 2 users - builtin-record and builtin-test objects.
838 * Both call perf_evlist__delete in case of error, so we dont
839 * need to bother.
840 */
89812fc8 841 fprintf(stderr, "invalid or unsupported event: '%s'\n", str);
85df6f68 842 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
bcd3279f 843 return ret;
8ad8db37
IM
844}
845
f120f9d5 846int parse_events_option(const struct option *opt, const char *str,
1d037ca1 847 int unset __maybe_unused)
f120f9d5
JO
848{
849 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
850 return parse_events(evlist, str, unset);
851}
852
361c99a6 853int parse_filter(const struct option *opt, const char *str,
1d037ca1 854 int unset __maybe_unused)
c171b552 855{
361c99a6 856 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
69aad6f1 857 struct perf_evsel *last = NULL;
c171b552 858
361c99a6 859 if (evlist->nr_entries > 0)
0c21f736 860 last = perf_evlist__last(evlist);
69aad6f1
ACM
861
862 if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
c171b552
LZ
863 fprintf(stderr,
864 "-F option should follow a -e tracepoint option\n");
865 return -1;
866 }
867
69aad6f1
ACM
868 last->filter = strdup(str);
869 if (last->filter == NULL) {
c171b552
LZ
870 fprintf(stderr, "not enough memory to hold filter string\n");
871 return -1;
872 }
c171b552
LZ
873
874 return 0;
875}
876
86847b62 877static const char * const event_type_descriptors[] = {
86847b62
TG
878 "Hardware event",
879 "Software event",
880 "Tracepoint event",
881 "Hardware cache event",
41bdcb23
LW
882 "Raw hardware event descriptor",
883 "Hardware breakpoint",
86847b62
TG
884};
885
f6bdafef
JB
886/*
887 * Print the events from <debugfs_mount_point>/tracing/events
888 */
889
a3277d2d
FW
890void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
891 bool name_only)
f6bdafef
JB
892{
893 DIR *sys_dir, *evt_dir;
894 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
f6bdafef 895 char evt_path[MAXPATHLEN];
725b1368 896 char dir_path[MAXPATHLEN];
f6bdafef 897
ebf294bf 898 if (debugfs_valid_mountpoint(tracing_events_path))
f6bdafef
JB
899 return;
900
ebf294bf 901 sys_dir = opendir(tracing_events_path);
f6bdafef 902 if (!sys_dir)
725b1368 903 return;
6b58e7f1
UD
904
905 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
668b8788
ACM
906 if (subsys_glob != NULL &&
907 !strglobmatch(sys_dirent.d_name, subsys_glob))
908 continue;
725b1368 909
ebf294bf 910 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
725b1368
ED
911 sys_dirent.d_name);
912 evt_dir = opendir(dir_path);
913 if (!evt_dir)
6b58e7f1 914 continue;
725b1368 915
6b58e7f1 916 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
668b8788
ACM
917 if (event_glob != NULL &&
918 !strglobmatch(evt_dirent.d_name, event_glob))
919 continue;
920
a3277d2d
FW
921 if (name_only) {
922 printf("%s:%s ", sys_dirent.d_name, evt_dirent.d_name);
923 continue;
924 }
925
f6bdafef
JB
926 snprintf(evt_path, MAXPATHLEN, "%s:%s",
927 sys_dirent.d_name, evt_dirent.d_name);
947b4ad1 928 printf(" %-50s [%s]\n", evt_path,
41bdcb23 929 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
f6bdafef
JB
930 }
931 closedir(evt_dir);
932 }
f6bdafef
JB
933 closedir(sys_dir);
934}
935
20c457b8
TR
936/*
937 * Check whether event is in <debugfs_mount_point>/tracing/events
938 */
939
940int is_valid_tracepoint(const char *event_string)
941{
942 DIR *sys_dir, *evt_dir;
943 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
944 char evt_path[MAXPATHLEN];
945 char dir_path[MAXPATHLEN];
946
ebf294bf 947 if (debugfs_valid_mountpoint(tracing_events_path))
20c457b8
TR
948 return 0;
949
ebf294bf 950 sys_dir = opendir(tracing_events_path);
20c457b8
TR
951 if (!sys_dir)
952 return 0;
953
954 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
955
ebf294bf 956 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
20c457b8
TR
957 sys_dirent.d_name);
958 evt_dir = opendir(dir_path);
959 if (!evt_dir)
960 continue;
961
962 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
963 snprintf(evt_path, MAXPATHLEN, "%s:%s",
964 sys_dirent.d_name, evt_dirent.d_name);
965 if (!strcmp(evt_path, event_string)) {
966 closedir(evt_dir);
967 closedir(sys_dir);
968 return 1;
969 }
970 }
971 closedir(evt_dir);
972 }
973 closedir(sys_dir);
974 return 0;
975}
976
1dc12760
JO
977static void __print_events_type(u8 type, struct event_symbol *syms,
978 unsigned max)
668b8788 979{
668b8788 980 char name[64];
1dc12760 981 unsigned i;
668b8788 982
1dc12760 983 for (i = 0; i < max ; i++, syms++) {
668b8788
ACM
984 if (strlen(syms->alias))
985 snprintf(name, sizeof(name), "%s OR %s",
986 syms->symbol, syms->alias);
987 else
988 snprintf(name, sizeof(name), "%s", syms->symbol);
989
947b4ad1 990 printf(" %-50s [%s]\n", name,
668b8788
ACM
991 event_type_descriptors[type]);
992 }
993}
994
1dc12760
JO
995void print_events_type(u8 type)
996{
997 if (type == PERF_TYPE_SOFTWARE)
998 __print_events_type(type, event_symbols_sw, PERF_COUNT_SW_MAX);
999 else
1000 __print_events_type(type, event_symbols_hw, PERF_COUNT_HW_MAX);
1001}
1002
a3277d2d 1003int print_hwcache_events(const char *event_glob, bool name_only)
668b8788
ACM
1004{
1005 unsigned int type, op, i, printed = 0;
0b668bc9 1006 char name[64];
668b8788
ACM
1007
1008 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1009 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1010 /* skip invalid cache type */
0b668bc9 1011 if (!perf_evsel__is_cache_op_valid(type, op))
668b8788
ACM
1012 continue;
1013
1014 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
0b668bc9
ACM
1015 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
1016 name, sizeof(name));
947b4ad1 1017 if (event_glob != NULL && !strglobmatch(name, event_glob))
668b8788
ACM
1018 continue;
1019
a3277d2d
FW
1020 if (name_only)
1021 printf("%s ", name);
1022 else
1023 printf(" %-50s [%s]\n", name,
1024 event_type_descriptors[PERF_TYPE_HW_CACHE]);
668b8788
ACM
1025 ++printed;
1026 }
1027 }
1028 }
1029
1030 return printed;
1031}
1032
1dc12760 1033static void print_symbol_events(const char *event_glob, unsigned type,
a3277d2d
FW
1034 struct event_symbol *syms, unsigned max,
1035 bool name_only)
8ad8db37 1036{
1dc12760 1037 unsigned i, printed = 0;
947b4ad1 1038 char name[MAX_NAME_LEN];
8ad8db37 1039
1dc12760 1040 for (i = 0; i < max; i++, syms++) {
668b8788
ACM
1041
1042 if (event_glob != NULL &&
1043 !(strglobmatch(syms->symbol, event_glob) ||
1044 (syms->alias && strglobmatch(syms->alias, event_glob))))
1045 continue;
8ad8db37 1046
a3277d2d
FW
1047 if (name_only) {
1048 printf("%s ", syms->symbol);
1049 continue;
1050 }
1051
74d5b588 1052 if (strlen(syms->alias))
947b4ad1 1053 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
74d5b588 1054 else
947b4ad1 1055 strncpy(name, syms->symbol, MAX_NAME_LEN);
8ad8db37 1056
1dc12760
JO
1057 printf(" %-50s [%s]\n", name, event_type_descriptors[type]);
1058
1059 printed++;
8ad8db37
IM
1060 }
1061
1dc12760 1062 if (printed)
668b8788 1063 printf("\n");
1dc12760
JO
1064}
1065
1066/*
1067 * Print the help text for the event symbols:
1068 */
a3277d2d 1069void print_events(const char *event_glob, bool name_only)
1dc12760 1070{
a3277d2d
FW
1071 if (!name_only) {
1072 printf("\n");
1073 printf("List of pre-defined events (to be used in -e):\n");
1074 }
1dc12760
JO
1075
1076 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
a3277d2d 1077 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
1dc12760
JO
1078
1079 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
a3277d2d 1080 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
1dc12760 1081
a3277d2d 1082 print_hwcache_events(event_glob, name_only);
668b8788
ACM
1083
1084 if (event_glob != NULL)
1085 return;
73c24cb8 1086
a3277d2d
FW
1087 if (!name_only) {
1088 printf("\n");
1089 printf(" %-50s [%s]\n",
1090 "rNNN",
1091 event_type_descriptors[PERF_TYPE_RAW]);
1092 printf(" %-50s [%s]\n",
1093 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
1094 event_type_descriptors[PERF_TYPE_RAW]);
1095 printf(" (see 'perf list --help' on how to encode it)\n");
1096 printf("\n");
1097
1098 printf(" %-50s [%s]\n",
1099 "mem:<addr>[:access]",
41bdcb23 1100 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
a3277d2d
FW
1101 printf("\n");
1102 }
1b290d67 1103
a3277d2d 1104 print_tracepoint_events(NULL, NULL, name_only);
8ad8db37 1105}
8f707d84
JO
1106
1107int parse_events__is_hardcoded_term(struct parse_events__term *term)
1108{
16fa7e82 1109 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
8f707d84
JO
1110}
1111
16fa7e82
JO
1112static int new_term(struct parse_events__term **_term, int type_val,
1113 int type_term, char *config,
b527bab5 1114 char *str, u64 num)
8f707d84
JO
1115{
1116 struct parse_events__term *term;
1117
1118 term = zalloc(sizeof(*term));
1119 if (!term)
1120 return -ENOMEM;
1121
1122 INIT_LIST_HEAD(&term->list);
16fa7e82
JO
1123 term->type_val = type_val;
1124 term->type_term = type_term;
8f707d84
JO
1125 term->config = config;
1126
16fa7e82 1127 switch (type_val) {
8f707d84
JO
1128 case PARSE_EVENTS__TERM_TYPE_NUM:
1129 term->val.num = num;
1130 break;
1131 case PARSE_EVENTS__TERM_TYPE_STR:
1132 term->val.str = str;
1133 break;
1134 default:
1135 return -EINVAL;
1136 }
1137
1138 *_term = term;
1139 return 0;
1140}
1141
16fa7e82 1142int parse_events__term_num(struct parse_events__term **term,
b527bab5 1143 int type_term, char *config, u64 num)
16fa7e82
JO
1144{
1145 return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
1146 config, NULL, num);
1147}
1148
1149int parse_events__term_str(struct parse_events__term **term,
1150 int type_term, char *config, char *str)
1151{
1152 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
1153 config, str, 0);
1154}
1155
a6146d50
ZY
1156int parse_events__term_clone(struct parse_events__term **new,
1157 struct parse_events__term *term)
1158{
1159 return new_term(new, term->type_val, term->type_term, term->config,
1160 term->val.str, term->val.num);
1161}
1162
8f707d84
JO
1163void parse_events__free_terms(struct list_head *terms)
1164{
1165 struct parse_events__term *term, *h;
1166
1167 list_for_each_entry_safe(term, h, terms, list)
1168 free(term);
1169
1170 free(terms);
1171}
This page took 0.195021 seconds and 5 git commands to generate.