perf tools: Add support for pinned modifier
[pandora-kernel.git] / tools / perf / util / parse-events.c
1 #include <linux/hw_breakpoint.h>
2 #include "util.h"
3 #include "../perf.h"
4 #include "evlist.h"
5 #include "evsel.h"
6 #include "parse-options.h"
7 #include "parse-events.h"
8 #include "exec_cmd.h"
9 #include "linux/string.h"
10 #include "symbol.h"
11 #include "cache.h"
12 #include "header.h"
13 #include <lk/debugfs.h>
14 #include "parse-events-bison.h"
15 #define YY_EXTRA_TYPE int
16 #include "parse-events-flex.h"
17 #include "pmu.h"
18
19 #define MAX_NAME_LEN 100
20
21 struct event_symbol {
22         const char      *symbol;
23         const char      *alias;
24 };
25
26 #ifdef PARSER_DEBUG
27 extern int parse_events_debug;
28 #endif
29 int parse_events_parse(void *data, void *scanner);
30
31 static 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
74 static 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         },
111 };
112
113 #define __PERF_EVENT_FIELD(config, name) \
114         ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
115
116 #define PERF_EVENT_RAW(config)          __PERF_EVENT_FIELD(config, RAW)
117 #define PERF_EVENT_CONFIG(config)       __PERF_EVENT_FIELD(config, CONFIG)
118 #define PERF_EVENT_TYPE(config)         __PERF_EVENT_FIELD(config, TYPE)
119 #define PERF_EVENT_ID(config)           __PERF_EVENT_FIELD(config, EVENT)
120
121 #define for_each_subsystem(sys_dir, sys_dirent, sys_next)              \
122         while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next)        \
123         if (sys_dirent.d_type == DT_DIR &&                                     \
124            (strcmp(sys_dirent.d_name, ".")) &&                                 \
125            (strcmp(sys_dirent.d_name, "..")))
126
127 static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
128 {
129         char evt_path[MAXPATHLEN];
130         int fd;
131
132         snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
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
142 #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next)              \
143         while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next)        \
144         if (evt_dirent.d_type == DT_DIR &&                                     \
145            (strcmp(evt_dirent.d_name, ".")) &&                                 \
146            (strcmp(evt_dirent.d_name, "..")) &&                                \
147            (!tp_event_has_id(&sys_dirent, &evt_dirent)))
148
149 #define MAX_EVENT_LENGTH 512
150
151
152 struct tracepoint_path *tracepoint_id_to_path(u64 config)
153 {
154         struct tracepoint_path *path = NULL;
155         DIR *sys_dir, *evt_dir;
156         struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
157         char id_buf[24];
158         int fd;
159         u64 id;
160         char evt_path[MAXPATHLEN];
161         char dir_path[MAXPATHLEN];
162
163         if (debugfs_valid_mountpoint(tracing_events_path))
164                 return NULL;
165
166         sys_dir = opendir(tracing_events_path);
167         if (!sys_dir)
168                 return NULL;
169
170         for_each_subsystem(sys_dir, sys_dirent, sys_next) {
171
172                 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
173                          sys_dirent.d_name);
174                 evt_dir = opendir(dir_path);
175                 if (!evt_dir)
176                         continue;
177
178                 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
179
180                         snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
181                                  evt_dirent.d_name);
182                         fd = open(evt_path, O_RDONLY);
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);
194                                 path = zalloc(sizeof(*path));
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;
211                         }
212                 }
213                 closedir(evt_dir);
214         }
215
216         closedir(sys_dir);
217         return NULL;
218 }
219
220 struct tracepoint_path *tracepoint_name_to_path(const char *name)
221 {
222         struct tracepoint_path *path = zalloc(sizeof(*path));
223         char *str = strchr(name, ':');
224
225         if (path == NULL || str == NULL) {
226                 free(path);
227                 return NULL;
228         }
229
230         path->system = strndup(name, str - name);
231         path->name = strdup(str+1);
232
233         if (path->system == NULL || path->name == NULL) {
234                 free(path->system);
235                 free(path->name);
236                 free(path);
237                 path = NULL;
238         }
239
240         return path;
241 }
242
243 const char *event_type(int type)
244 {
245         switch (type) {
246         case PERF_TYPE_HARDWARE:
247                 return "hardware";
248
249         case PERF_TYPE_SOFTWARE:
250                 return "software";
251
252         case PERF_TYPE_TRACEPOINT:
253                 return "tracepoint";
254
255         case PERF_TYPE_HW_CACHE:
256                 return "hardware-cache";
257
258         default:
259                 break;
260         }
261
262         return "unknown";
263 }
264
265
266
267 static int __add_event(struct list_head *list, int *idx,
268                        struct perf_event_attr *attr,
269                        char *name, struct cpu_map *cpus)
270 {
271         struct perf_evsel *evsel;
272
273         event_attr_init(attr);
274
275         evsel = perf_evsel__new(attr, (*idx)++);
276         if (!evsel)
277                 return -ENOMEM;
278
279         evsel->cpus = cpus;
280         if (name)
281                 evsel->name = strdup(name);
282         list_add_tail(&evsel->node, list);
283         return 0;
284 }
285
286 static int add_event(struct list_head *list, int *idx,
287                      struct perf_event_attr *attr, char *name)
288 {
289         return __add_event(list, idx, attr, name, NULL);
290 }
291
292 static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
293 {
294         int i, j;
295         int n, longest = -1;
296
297         for (i = 0; i < size; i++) {
298                 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
299                         n = strlen(names[i][j]);
300                         if (n > longest && !strncasecmp(str, names[i][j], n))
301                                 longest = n;
302                 }
303                 if (longest > 0)
304                         return i;
305         }
306
307         return -1;
308 }
309
310 int parse_events_add_cache(struct list_head *list, int *idx,
311                            char *type, char *op_result1, char *op_result2)
312 {
313         struct perf_event_attr attr;
314         char name[MAX_NAME_LEN];
315         int cache_type = -1, cache_op = -1, cache_result = -1;
316         char *op_result[2] = { op_result1, op_result2 };
317         int i, n;
318
319         /*
320          * No fallback - if we cannot get a clear cache type
321          * then bail out:
322          */
323         cache_type = parse_aliases(type, perf_evsel__hw_cache,
324                                    PERF_COUNT_HW_CACHE_MAX);
325         if (cache_type == -1)
326                 return -EINVAL;
327
328         n = snprintf(name, MAX_NAME_LEN, "%s", type);
329
330         for (i = 0; (i < 2) && (op_result[i]); i++) {
331                 char *str = op_result[i];
332
333                 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
334
335                 if (cache_op == -1) {
336                         cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
337                                                  PERF_COUNT_HW_CACHE_OP_MAX);
338                         if (cache_op >= 0) {
339                                 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
340                                         return -EINVAL;
341                                 continue;
342                         }
343                 }
344
345                 if (cache_result == -1) {
346                         cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
347                                                      PERF_COUNT_HW_CACHE_RESULT_MAX);
348                         if (cache_result >= 0)
349                                 continue;
350                 }
351         }
352
353         /*
354          * Fall back to reads:
355          */
356         if (cache_op == -1)
357                 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
358
359         /*
360          * Fall back to accesses:
361          */
362         if (cache_result == -1)
363                 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
364
365         memset(&attr, 0, sizeof(attr));
366         attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
367         attr.type = PERF_TYPE_HW_CACHE;
368         return add_event(list, idx, &attr, name);
369 }
370
371 static int add_tracepoint(struct list_head *list, int *idx,
372                           char *sys_name, char *evt_name)
373 {
374         struct perf_evsel *evsel;
375
376         evsel = perf_evsel__newtp(sys_name, evt_name, (*idx)++);
377         if (!evsel)
378                 return -ENOMEM;
379
380         list_add_tail(&evsel->node, list);
381
382         return 0;
383 }
384
385 static int add_tracepoint_multi_event(struct list_head *list, int *idx,
386                                       char *sys_name, char *evt_name)
387 {
388         char evt_path[MAXPATHLEN];
389         struct dirent *evt_ent;
390         DIR *evt_dir;
391         int ret = 0;
392
393         snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
394         evt_dir = opendir(evt_path);
395         if (!evt_dir) {
396                 perror("Can't open event dir");
397                 return -1;
398         }
399
400         while (!ret && (evt_ent = readdir(evt_dir))) {
401                 if (!strcmp(evt_ent->d_name, ".")
402                     || !strcmp(evt_ent->d_name, "..")
403                     || !strcmp(evt_ent->d_name, "enable")
404                     || !strcmp(evt_ent->d_name, "filter"))
405                         continue;
406
407                 if (!strglobmatch(evt_ent->d_name, evt_name))
408                         continue;
409
410                 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name);
411         }
412
413         closedir(evt_dir);
414         return ret;
415 }
416
417 static int add_tracepoint_event(struct list_head *list, int *idx,
418                                 char *sys_name, char *evt_name)
419 {
420         return strpbrk(evt_name, "*?") ?
421                add_tracepoint_multi_event(list, idx, sys_name, evt_name) :
422                add_tracepoint(list, idx, sys_name, evt_name);
423 }
424
425 static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
426                                     char *sys_name, char *evt_name)
427 {
428         struct dirent *events_ent;
429         DIR *events_dir;
430         int ret = 0;
431
432         events_dir = opendir(tracing_events_path);
433         if (!events_dir) {
434                 perror("Can't open event dir");
435                 return -1;
436         }
437
438         while (!ret && (events_ent = readdir(events_dir))) {
439                 if (!strcmp(events_ent->d_name, ".")
440                     || !strcmp(events_ent->d_name, "..")
441                     || !strcmp(events_ent->d_name, "enable")
442                     || !strcmp(events_ent->d_name, "header_event")
443                     || !strcmp(events_ent->d_name, "header_page"))
444                         continue;
445
446                 if (!strglobmatch(events_ent->d_name, sys_name))
447                         continue;
448
449                 ret = add_tracepoint_event(list, idx, events_ent->d_name,
450                                            evt_name);
451         }
452
453         closedir(events_dir);
454         return ret;
455 }
456
457 int parse_events_add_tracepoint(struct list_head *list, int *idx,
458                                 char *sys, char *event)
459 {
460         int ret;
461
462         ret = debugfs_valid_mountpoint(tracing_events_path);
463         if (ret)
464                 return ret;
465
466         if (strpbrk(sys, "*?"))
467                 return add_tracepoint_multi_sys(list, idx, sys, event);
468         else
469                 return add_tracepoint_event(list, idx, sys, event);
470 }
471
472 static int
473 parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
474 {
475         int i;
476
477         for (i = 0; i < 3; i++) {
478                 if (!type || !type[i])
479                         break;
480
481 #define CHECK_SET_TYPE(bit)             \
482 do {                                    \
483         if (attr->bp_type & bit)        \
484                 return -EINVAL;         \
485         else                            \
486                 attr->bp_type |= bit;   \
487 } while (0)
488
489                 switch (type[i]) {
490                 case 'r':
491                         CHECK_SET_TYPE(HW_BREAKPOINT_R);
492                         break;
493                 case 'w':
494                         CHECK_SET_TYPE(HW_BREAKPOINT_W);
495                         break;
496                 case 'x':
497                         CHECK_SET_TYPE(HW_BREAKPOINT_X);
498                         break;
499                 default:
500                         return -EINVAL;
501                 }
502         }
503
504 #undef CHECK_SET_TYPE
505
506         if (!attr->bp_type) /* Default */
507                 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
508
509         return 0;
510 }
511
512 int parse_events_add_breakpoint(struct list_head *list, int *idx,
513                                 void *ptr, char *type)
514 {
515         struct perf_event_attr attr;
516
517         memset(&attr, 0, sizeof(attr));
518         attr.bp_addr = (unsigned long) ptr;
519
520         if (parse_breakpoint_type(type, &attr))
521                 return -EINVAL;
522
523         /*
524          * We should find a nice way to override the access length
525          * Provide some defaults for now
526          */
527         if (attr.bp_type == HW_BREAKPOINT_X)
528                 attr.bp_len = sizeof(long);
529         else
530                 attr.bp_len = HW_BREAKPOINT_LEN_4;
531
532         attr.type = PERF_TYPE_BREAKPOINT;
533         attr.sample_period = 1;
534
535         return add_event(list, idx, &attr, NULL);
536 }
537
538 static int config_term(struct perf_event_attr *attr,
539                        struct parse_events_term *term)
540 {
541 #define CHECK_TYPE_VAL(type)                                    \
542 do {                                                            \
543         if (PARSE_EVENTS__TERM_TYPE_ ## type != term->type_val) \
544                 return -EINVAL;                                 \
545 } while (0)
546
547         switch (term->type_term) {
548         case PARSE_EVENTS__TERM_TYPE_CONFIG:
549                 CHECK_TYPE_VAL(NUM);
550                 attr->config = term->val.num;
551                 break;
552         case PARSE_EVENTS__TERM_TYPE_CONFIG1:
553                 CHECK_TYPE_VAL(NUM);
554                 attr->config1 = term->val.num;
555                 break;
556         case PARSE_EVENTS__TERM_TYPE_CONFIG2:
557                 CHECK_TYPE_VAL(NUM);
558                 attr->config2 = term->val.num;
559                 break;
560         case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
561                 CHECK_TYPE_VAL(NUM);
562                 attr->sample_period = term->val.num;
563                 break;
564         case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
565                 /*
566                  * TODO uncomment when the field is available
567                  * attr->branch_sample_type = term->val.num;
568                  */
569                 break;
570         case PARSE_EVENTS__TERM_TYPE_NAME:
571                 CHECK_TYPE_VAL(STR);
572                 break;
573         default:
574                 return -EINVAL;
575         }
576
577         return 0;
578 #undef CHECK_TYPE_VAL
579 }
580
581 static int config_attr(struct perf_event_attr *attr,
582                        struct list_head *head, int fail)
583 {
584         struct parse_events_term *term;
585
586         list_for_each_entry(term, head, list)
587                 if (config_term(attr, term) && fail)
588                         return -EINVAL;
589
590         return 0;
591 }
592
593 int parse_events_add_numeric(struct list_head *list, int *idx,
594                              u32 type, u64 config,
595                              struct list_head *head_config)
596 {
597         struct perf_event_attr attr;
598
599         memset(&attr, 0, sizeof(attr));
600         attr.type = type;
601         attr.config = config;
602
603         if (head_config &&
604             config_attr(&attr, head_config, 1))
605                 return -EINVAL;
606
607         return add_event(list, idx, &attr, NULL);
608 }
609
610 static int parse_events__is_name_term(struct parse_events_term *term)
611 {
612         return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
613 }
614
615 static char *pmu_event_name(struct list_head *head_terms)
616 {
617         struct parse_events_term *term;
618
619         list_for_each_entry(term, head_terms, list)
620                 if (parse_events__is_name_term(term))
621                         return term->val.str;
622
623         return NULL;
624 }
625
626 int parse_events_add_pmu(struct list_head *list, int *idx,
627                          char *name, struct list_head *head_config)
628 {
629         struct perf_event_attr attr;
630         struct perf_pmu *pmu;
631
632         pmu = perf_pmu__find(name);
633         if (!pmu)
634                 return -EINVAL;
635
636         memset(&attr, 0, sizeof(attr));
637
638         if (perf_pmu__check_alias(pmu, head_config))
639                 return -EINVAL;
640
641         /*
642          * Configure hardcoded terms first, no need to check
643          * return value when called with fail == 0 ;)
644          */
645         config_attr(&attr, head_config, 0);
646
647         if (perf_pmu__config(pmu, &attr, head_config))
648                 return -EINVAL;
649
650         return __add_event(list, idx, &attr, pmu_event_name(head_config),
651                            pmu->cpus);
652 }
653
654 int parse_events__modifier_group(struct list_head *list,
655                                  char *event_mod)
656 {
657         return parse_events__modifier_event(list, event_mod, true);
658 }
659
660 void parse_events__set_leader(char *name, struct list_head *list)
661 {
662         struct perf_evsel *leader;
663
664         __perf_evlist__set_leader(list);
665         leader = list_entry(list->next, struct perf_evsel, node);
666         leader->group_name = name ? strdup(name) : NULL;
667 }
668
669 /* list_event is assumed to point to malloc'ed memory */
670 void parse_events_update_lists(struct list_head *list_event,
671                                struct list_head *list_all)
672 {
673         /*
674          * Called for single event definition. Update the
675          * 'all event' list, and reinit the 'single event'
676          * list, for next event definition.
677          */
678         list_splice_tail(list_event, list_all);
679         free(list_event);
680 }
681
682 struct event_modifier {
683         int eu;
684         int ek;
685         int eh;
686         int eH;
687         int eG;
688         int precise;
689         int exclude_GH;
690         int sample_read;
691         int pinned;
692 };
693
694 static int get_event_modifier(struct event_modifier *mod, char *str,
695                                struct perf_evsel *evsel)
696 {
697         int eu = evsel ? evsel->attr.exclude_user : 0;
698         int ek = evsel ? evsel->attr.exclude_kernel : 0;
699         int eh = evsel ? evsel->attr.exclude_hv : 0;
700         int eH = evsel ? evsel->attr.exclude_host : 0;
701         int eG = evsel ? evsel->attr.exclude_guest : 0;
702         int precise = evsel ? evsel->attr.precise_ip : 0;
703         int sample_read = 0;
704         int pinned = evsel ? evsel->attr.pinned : 0;
705
706         int exclude = eu | ek | eh;
707         int exclude_GH = evsel ? evsel->exclude_GH : 0;
708
709         memset(mod, 0, sizeof(*mod));
710
711         while (*str) {
712                 if (*str == 'u') {
713                         if (!exclude)
714                                 exclude = eu = ek = eh = 1;
715                         eu = 0;
716                 } else if (*str == 'k') {
717                         if (!exclude)
718                                 exclude = eu = ek = eh = 1;
719                         ek = 0;
720                 } else if (*str == 'h') {
721                         if (!exclude)
722                                 exclude = eu = ek = eh = 1;
723                         eh = 0;
724                 } else if (*str == 'G') {
725                         if (!exclude_GH)
726                                 exclude_GH = eG = eH = 1;
727                         eG = 0;
728                 } else if (*str == 'H') {
729                         if (!exclude_GH)
730                                 exclude_GH = eG = eH = 1;
731                         eH = 0;
732                 } else if (*str == 'p') {
733                         precise++;
734                         /* use of precise requires exclude_guest */
735                         if (!exclude_GH)
736                                 eG = 1;
737                 } else if (*str == 'S') {
738                         sample_read = 1;
739                 } else if (*str == 'D') {
740                         pinned = 1;
741                 } else
742                         break;
743
744                 ++str;
745         }
746
747         /*
748          * precise ip:
749          *
750          *  0 - SAMPLE_IP can have arbitrary skid
751          *  1 - SAMPLE_IP must have constant skid
752          *  2 - SAMPLE_IP requested to have 0 skid
753          *  3 - SAMPLE_IP must have 0 skid
754          *
755          *  See also PERF_RECORD_MISC_EXACT_IP
756          */
757         if (precise > 3)
758                 return -EINVAL;
759
760         mod->eu = eu;
761         mod->ek = ek;
762         mod->eh = eh;
763         mod->eH = eH;
764         mod->eG = eG;
765         mod->precise = precise;
766         mod->exclude_GH = exclude_GH;
767         mod->sample_read = sample_read;
768         mod->pinned = pinned;
769
770         return 0;
771 }
772
773 /*
774  * Basic modifier sanity check to validate it contains only one
775  * instance of any modifier (apart from 'p') present.
776  */
777 static int check_modifier(char *str)
778 {
779         char *p = str;
780
781         /* The sizeof includes 0 byte as well. */
782         if (strlen(str) > (sizeof("ukhGHpppSD") - 1))
783                 return -1;
784
785         while (*p) {
786                 if (*p != 'p' && strchr(p + 1, *p))
787                         return -1;
788                 p++;
789         }
790
791         return 0;
792 }
793
794 int parse_events__modifier_event(struct list_head *list, char *str, bool add)
795 {
796         struct perf_evsel *evsel;
797         struct event_modifier mod;
798
799         if (str == NULL)
800                 return 0;
801
802         if (check_modifier(str))
803                 return -EINVAL;
804
805         if (!add && get_event_modifier(&mod, str, NULL))
806                 return -EINVAL;
807
808         list_for_each_entry(evsel, list, node) {
809
810                 if (add && get_event_modifier(&mod, str, evsel))
811                         return -EINVAL;
812
813                 evsel->attr.exclude_user   = mod.eu;
814                 evsel->attr.exclude_kernel = mod.ek;
815                 evsel->attr.exclude_hv     = mod.eh;
816                 evsel->attr.precise_ip     = mod.precise;
817                 evsel->attr.exclude_host   = mod.eH;
818                 evsel->attr.exclude_guest  = mod.eG;
819                 evsel->exclude_GH          = mod.exclude_GH;
820                 evsel->sample_read         = mod.sample_read;
821
822                 if (perf_evsel__is_group_leader(evsel))
823                         evsel->attr.pinned = mod.pinned;
824         }
825
826         return 0;
827 }
828
829 int parse_events_name(struct list_head *list, char *name)
830 {
831         struct perf_evsel *evsel;
832
833         list_for_each_entry(evsel, list, node) {
834                 if (!evsel->name)
835                         evsel->name = strdup(name);
836         }
837
838         return 0;
839 }
840
841 static int parse_events__scanner(const char *str, void *data, int start_token);
842
843 static int parse_events_fixup(int ret, const char *str, void *data,
844                               int start_token)
845 {
846         char *o = strdup(str);
847         char *s = NULL;
848         char *t = o;
849         char *p;
850         int len = 0;
851
852         if (!o)
853                 return ret;
854         while ((p = strsep(&t, ",")) != NULL) {
855                 if (s)
856                         str_append(&s, &len, ",");
857                 str_append(&s, &len, "cpu/");
858                 str_append(&s, &len, p);
859                 str_append(&s, &len, "/");
860         }
861         free(o);
862         if (!s)
863                 return -ENOMEM;
864         return parse_events__scanner(s, data, start_token);
865 }
866
867 static int parse_events__scanner(const char *str, void *data, int start_token)
868 {
869         YY_BUFFER_STATE buffer;
870         void *scanner;
871         int ret;
872
873         ret = parse_events_lex_init_extra(start_token, &scanner);
874         if (ret)
875                 return ret;
876
877         buffer = parse_events__scan_string(str, scanner);
878
879 #ifdef PARSER_DEBUG
880         parse_events_debug = 1;
881 #endif
882         ret = parse_events_parse(data, scanner);
883
884         parse_events__flush_buffer(buffer, scanner);
885         parse_events__delete_buffer(buffer, scanner);
886         parse_events_lex_destroy(scanner);
887         if (ret && !strchr(str, '/'))
888                 ret = parse_events_fixup(ret, str, data, start_token);
889         return ret;
890 }
891
892 /*
893  * parse event config string, return a list of event terms.
894  */
895 int parse_events_terms(struct list_head *terms, const char *str)
896 {
897         struct parse_events_terms data = {
898                 .terms = NULL,
899         };
900         int ret;
901
902         ret = parse_events__scanner(str, &data, PE_START_TERMS);
903         if (!ret) {
904                 list_splice(data.terms, terms);
905                 free(data.terms);
906                 return 0;
907         }
908
909         if (data.terms)
910                 parse_events__free_terms(data.terms);
911         return ret;
912 }
913
914 int parse_events(struct perf_evlist *evlist, const char *str)
915 {
916         struct parse_events_evlist data = {
917                 .list = LIST_HEAD_INIT(data.list),
918                 .idx  = evlist->nr_entries,
919         };
920         int ret;
921
922         ret = parse_events__scanner(str, &data, PE_START_EVENTS);
923         if (!ret) {
924                 int entries = data.idx - evlist->nr_entries;
925                 perf_evlist__splice_list_tail(evlist, &data.list, entries);
926                 evlist->nr_groups += data.nr_groups;
927                 return 0;
928         }
929
930         /*
931          * There are 2 users - builtin-record and builtin-test objects.
932          * Both call perf_evlist__delete in case of error, so we dont
933          * need to bother.
934          */
935         return ret;
936 }
937
938 int parse_events_option(const struct option *opt, const char *str,
939                         int unset __maybe_unused)
940 {
941         struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
942         int ret = parse_events(evlist, str);
943
944         if (ret) {
945                 fprintf(stderr, "invalid or unsupported event: '%s'\n", str);
946                 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
947         }
948         return ret;
949 }
950
951 int parse_filter(const struct option *opt, const char *str,
952                  int unset __maybe_unused)
953 {
954         struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
955         struct perf_evsel *last = NULL;
956
957         if (evlist->nr_entries > 0)
958                 last = perf_evlist__last(evlist);
959
960         if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
961                 fprintf(stderr,
962                         "-F option should follow a -e tracepoint option\n");
963                 return -1;
964         }
965
966         last->filter = strdup(str);
967         if (last->filter == NULL) {
968                 fprintf(stderr, "not enough memory to hold filter string\n");
969                 return -1;
970         }
971
972         return 0;
973 }
974
975 static const char * const event_type_descriptors[] = {
976         "Hardware event",
977         "Software event",
978         "Tracepoint event",
979         "Hardware cache event",
980         "Raw hardware event descriptor",
981         "Hardware breakpoint",
982 };
983
984 /*
985  * Print the events from <debugfs_mount_point>/tracing/events
986  */
987
988 void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
989                              bool name_only)
990 {
991         DIR *sys_dir, *evt_dir;
992         struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
993         char evt_path[MAXPATHLEN];
994         char dir_path[MAXPATHLEN];
995
996         if (debugfs_valid_mountpoint(tracing_events_path))
997                 return;
998
999         sys_dir = opendir(tracing_events_path);
1000         if (!sys_dir)
1001                 return;
1002
1003         for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1004                 if (subsys_glob != NULL && 
1005                     !strglobmatch(sys_dirent.d_name, subsys_glob))
1006                         continue;
1007
1008                 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
1009                          sys_dirent.d_name);
1010                 evt_dir = opendir(dir_path);
1011                 if (!evt_dir)
1012                         continue;
1013
1014                 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1015                         if (event_glob != NULL && 
1016                             !strglobmatch(evt_dirent.d_name, event_glob))
1017                                 continue;
1018
1019                         if (name_only) {
1020                                 printf("%s:%s ", sys_dirent.d_name, evt_dirent.d_name);
1021                                 continue;
1022                         }
1023
1024                         snprintf(evt_path, MAXPATHLEN, "%s:%s",
1025                                  sys_dirent.d_name, evt_dirent.d_name);
1026                         printf("  %-50s [%s]\n", evt_path,
1027                                 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1028                 }
1029                 closedir(evt_dir);
1030         }
1031         closedir(sys_dir);
1032 }
1033
1034 /*
1035  * Check whether event is in <debugfs_mount_point>/tracing/events
1036  */
1037
1038 int is_valid_tracepoint(const char *event_string)
1039 {
1040         DIR *sys_dir, *evt_dir;
1041         struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
1042         char evt_path[MAXPATHLEN];
1043         char dir_path[MAXPATHLEN];
1044
1045         if (debugfs_valid_mountpoint(tracing_events_path))
1046                 return 0;
1047
1048         sys_dir = opendir(tracing_events_path);
1049         if (!sys_dir)
1050                 return 0;
1051
1052         for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1053
1054                 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
1055                          sys_dirent.d_name);
1056                 evt_dir = opendir(dir_path);
1057                 if (!evt_dir)
1058                         continue;
1059
1060                 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1061                         snprintf(evt_path, MAXPATHLEN, "%s:%s",
1062                                  sys_dirent.d_name, evt_dirent.d_name);
1063                         if (!strcmp(evt_path, event_string)) {
1064                                 closedir(evt_dir);
1065                                 closedir(sys_dir);
1066                                 return 1;
1067                         }
1068                 }
1069                 closedir(evt_dir);
1070         }
1071         closedir(sys_dir);
1072         return 0;
1073 }
1074
1075 static void __print_events_type(u8 type, struct event_symbol *syms,
1076                                 unsigned max)
1077 {
1078         char name[64];
1079         unsigned i;
1080
1081         for (i = 0; i < max ; i++, syms++) {
1082                 if (strlen(syms->alias))
1083                         snprintf(name, sizeof(name),  "%s OR %s",
1084                                  syms->symbol, syms->alias);
1085                 else
1086                         snprintf(name, sizeof(name), "%s", syms->symbol);
1087
1088                 printf("  %-50s [%s]\n", name,
1089                         event_type_descriptors[type]);
1090         }
1091 }
1092
1093 void print_events_type(u8 type)
1094 {
1095         if (type == PERF_TYPE_SOFTWARE)
1096                 __print_events_type(type, event_symbols_sw, PERF_COUNT_SW_MAX);
1097         else
1098                 __print_events_type(type, event_symbols_hw, PERF_COUNT_HW_MAX);
1099 }
1100
1101 int print_hwcache_events(const char *event_glob, bool name_only)
1102 {
1103         unsigned int type, op, i, printed = 0;
1104         char name[64];
1105
1106         for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1107                 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1108                         /* skip invalid cache type */
1109                         if (!perf_evsel__is_cache_op_valid(type, op))
1110                                 continue;
1111
1112                         for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
1113                                 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
1114                                                                         name, sizeof(name));
1115                                 if (event_glob != NULL && !strglobmatch(name, event_glob))
1116                                         continue;
1117
1118                                 if (name_only)
1119                                         printf("%s ", name);
1120                                 else
1121                                         printf("  %-50s [%s]\n", name,
1122                                                event_type_descriptors[PERF_TYPE_HW_CACHE]);
1123                                 ++printed;
1124                         }
1125                 }
1126         }
1127
1128         if (printed)
1129                 printf("\n");
1130         return printed;
1131 }
1132
1133 static void print_symbol_events(const char *event_glob, unsigned type,
1134                                 struct event_symbol *syms, unsigned max,
1135                                 bool name_only)
1136 {
1137         unsigned i, printed = 0;
1138         char name[MAX_NAME_LEN];
1139
1140         for (i = 0; i < max; i++, syms++) {
1141
1142                 if (event_glob != NULL && 
1143                     !(strglobmatch(syms->symbol, event_glob) ||
1144                       (syms->alias && strglobmatch(syms->alias, event_glob))))
1145                         continue;
1146
1147                 if (name_only) {
1148                         printf("%s ", syms->symbol);
1149                         continue;
1150                 }
1151
1152                 if (strlen(syms->alias))
1153                         snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
1154                 else
1155                         strncpy(name, syms->symbol, MAX_NAME_LEN);
1156
1157                 printf("  %-50s [%s]\n", name, event_type_descriptors[type]);
1158
1159                 printed++;
1160         }
1161
1162         if (printed)
1163                 printf("\n");
1164 }
1165
1166 /*
1167  * Print the help text for the event symbols:
1168  */
1169 void print_events(const char *event_glob, bool name_only)
1170 {
1171         if (!name_only) {
1172                 printf("\n");
1173                 printf("List of pre-defined events (to be used in -e):\n");
1174         }
1175
1176         print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
1177                             event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
1178
1179         print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
1180                             event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
1181
1182         print_hwcache_events(event_glob, name_only);
1183
1184         print_pmu_events(event_glob, name_only);
1185
1186         if (event_glob != NULL)
1187                 return;
1188
1189         if (!name_only) {
1190                 printf("  %-50s [%s]\n",
1191                        "rNNN",
1192                        event_type_descriptors[PERF_TYPE_RAW]);
1193                 printf("  %-50s [%s]\n",
1194                        "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
1195                        event_type_descriptors[PERF_TYPE_RAW]);
1196                 printf("   (see 'man perf-list' on how to encode it)\n");
1197                 printf("\n");
1198
1199                 printf("  %-50s [%s]\n",
1200                        "mem:<addr>[:access]",
1201                         event_type_descriptors[PERF_TYPE_BREAKPOINT]);
1202                 printf("\n");
1203         }
1204
1205         print_tracepoint_events(NULL, NULL, name_only);
1206 }
1207
1208 int parse_events__is_hardcoded_term(struct parse_events_term *term)
1209 {
1210         return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
1211 }
1212
1213 static int new_term(struct parse_events_term **_term, int type_val,
1214                     int type_term, char *config,
1215                     char *str, u64 num)
1216 {
1217         struct parse_events_term *term;
1218
1219         term = zalloc(sizeof(*term));
1220         if (!term)
1221                 return -ENOMEM;
1222
1223         INIT_LIST_HEAD(&term->list);
1224         term->type_val  = type_val;
1225         term->type_term = type_term;
1226         term->config = config;
1227
1228         switch (type_val) {
1229         case PARSE_EVENTS__TERM_TYPE_NUM:
1230                 term->val.num = num;
1231                 break;
1232         case PARSE_EVENTS__TERM_TYPE_STR:
1233                 term->val.str = str;
1234                 break;
1235         default:
1236                 free(term);
1237                 return -EINVAL;
1238         }
1239
1240         *_term = term;
1241         return 0;
1242 }
1243
1244 int parse_events_term__num(struct parse_events_term **term,
1245                            int type_term, char *config, u64 num)
1246 {
1247         return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
1248                         config, NULL, num);
1249 }
1250
1251 int parse_events_term__str(struct parse_events_term **term,
1252                            int type_term, char *config, char *str)
1253 {
1254         return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
1255                         config, str, 0);
1256 }
1257
1258 int parse_events_term__sym_hw(struct parse_events_term **term,
1259                               char *config, unsigned idx)
1260 {
1261         struct event_symbol *sym;
1262
1263         BUG_ON(idx >= PERF_COUNT_HW_MAX);
1264         sym = &event_symbols_hw[idx];
1265
1266         if (config)
1267                 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1268                                 PARSE_EVENTS__TERM_TYPE_USER, config,
1269                                 (char *) sym->symbol, 0);
1270         else
1271                 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1272                                 PARSE_EVENTS__TERM_TYPE_USER,
1273                                 (char *) "event", (char *) sym->symbol, 0);
1274 }
1275
1276 int parse_events_term__clone(struct parse_events_term **new,
1277                              struct parse_events_term *term)
1278 {
1279         return new_term(new, term->type_val, term->type_term, term->config,
1280                         term->val.str, term->val.num);
1281 }
1282
1283 void parse_events__free_terms(struct list_head *terms)
1284 {
1285         struct parse_events_term *term, *h;
1286
1287         list_for_each_entry_safe(term, h, terms, list)
1288                 free(term);
1289 }