perf tools: Reconstruct hw cache event with modifiers from perf_event_attr
[firefly-linux-kernel-4.4.55.git] / tools / perf / util / parse-events.c
1 #include "../../../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 "string.h"
10 #include "symbol.h"
11 #include "cache.h"
12 #include "header.h"
13 #include "debugfs.h"
14 #include "parse-events-flex.h"
15 #include "pmu.h"
16
17 #define MAX_NAME_LEN 100
18
19 struct event_symbol {
20         u8              type;
21         u64             config;
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(struct list_head *list, int *idx);
30
31 #define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
32 #define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
33
34 static struct event_symbol event_symbols[] = {
35   { CHW(CPU_CYCLES),                    "cpu-cycles",                   "cycles"                },
36   { CHW(STALLED_CYCLES_FRONTEND),       "stalled-cycles-frontend",      "idle-cycles-frontend"  },
37   { CHW(STALLED_CYCLES_BACKEND),        "stalled-cycles-backend",       "idle-cycles-backend"   },
38   { CHW(INSTRUCTIONS),                  "instructions",                 ""                      },
39   { CHW(CACHE_REFERENCES),              "cache-references",             ""                      },
40   { CHW(CACHE_MISSES),                  "cache-misses",                 ""                      },
41   { CHW(BRANCH_INSTRUCTIONS),           "branch-instructions",          "branches"              },
42   { CHW(BRANCH_MISSES),                 "branch-misses",                ""                      },
43   { CHW(BUS_CYCLES),                    "bus-cycles",                   ""                      },
44   { CHW(REF_CPU_CYCLES),                "ref-cycles",                   ""                      },
45
46   { CSW(CPU_CLOCK),                     "cpu-clock",                    ""                      },
47   { CSW(TASK_CLOCK),                    "task-clock",                   ""                      },
48   { CSW(PAGE_FAULTS),                   "page-faults",                  "faults"                },
49   { CSW(PAGE_FAULTS_MIN),               "minor-faults",                 ""                      },
50   { CSW(PAGE_FAULTS_MAJ),               "major-faults",                 ""                      },
51   { CSW(CONTEXT_SWITCHES),              "context-switches",             "cs"                    },
52   { CSW(CPU_MIGRATIONS),                "cpu-migrations",               "migrations"            },
53   { CSW(ALIGNMENT_FAULTS),              "alignment-faults",             ""                      },
54   { CSW(EMULATION_FAULTS),              "emulation-faults",             ""                      },
55 };
56
57 #define __PERF_EVENT_FIELD(config, name) \
58         ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
59
60 #define PERF_EVENT_RAW(config)          __PERF_EVENT_FIELD(config, RAW)
61 #define PERF_EVENT_CONFIG(config)       __PERF_EVENT_FIELD(config, CONFIG)
62 #define PERF_EVENT_TYPE(config)         __PERF_EVENT_FIELD(config, TYPE)
63 #define PERF_EVENT_ID(config)           __PERF_EVENT_FIELD(config, EVENT)
64
65 static const char *sw_event_names[PERF_COUNT_SW_MAX] = {
66         "cpu-clock",
67         "task-clock",
68         "page-faults",
69         "context-switches",
70         "CPU-migrations",
71         "minor-faults",
72         "major-faults",
73         "alignment-faults",
74         "emulation-faults",
75 };
76
77 #define for_each_subsystem(sys_dir, sys_dirent, sys_next)              \
78         while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next)        \
79         if (sys_dirent.d_type == DT_DIR &&                                     \
80            (strcmp(sys_dirent.d_name, ".")) &&                                 \
81            (strcmp(sys_dirent.d_name, "..")))
82
83 static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
84 {
85         char evt_path[MAXPATHLEN];
86         int fd;
87
88         snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
89                         sys_dir->d_name, evt_dir->d_name);
90         fd = open(evt_path, O_RDONLY);
91         if (fd < 0)
92                 return -EINVAL;
93         close(fd);
94
95         return 0;
96 }
97
98 #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next)              \
99         while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next)        \
100         if (evt_dirent.d_type == DT_DIR &&                                     \
101            (strcmp(evt_dirent.d_name, ".")) &&                                 \
102            (strcmp(evt_dirent.d_name, "..")) &&                                \
103            (!tp_event_has_id(&sys_dirent, &evt_dirent)))
104
105 #define MAX_EVENT_LENGTH 512
106
107
108 struct tracepoint_path *tracepoint_id_to_path(u64 config)
109 {
110         struct tracepoint_path *path = NULL;
111         DIR *sys_dir, *evt_dir;
112         struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
113         char id_buf[24];
114         int fd;
115         u64 id;
116         char evt_path[MAXPATHLEN];
117         char dir_path[MAXPATHLEN];
118
119         if (debugfs_valid_mountpoint(tracing_events_path))
120                 return NULL;
121
122         sys_dir = opendir(tracing_events_path);
123         if (!sys_dir)
124                 return NULL;
125
126         for_each_subsystem(sys_dir, sys_dirent, sys_next) {
127
128                 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
129                          sys_dirent.d_name);
130                 evt_dir = opendir(dir_path);
131                 if (!evt_dir)
132                         continue;
133
134                 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
135
136                         snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
137                                  evt_dirent.d_name);
138                         fd = open(evt_path, O_RDONLY);
139                         if (fd < 0)
140                                 continue;
141                         if (read(fd, id_buf, sizeof(id_buf)) < 0) {
142                                 close(fd);
143                                 continue;
144                         }
145                         close(fd);
146                         id = atoll(id_buf);
147                         if (id == config) {
148                                 closedir(evt_dir);
149                                 closedir(sys_dir);
150                                 path = zalloc(sizeof(*path));
151                                 path->system = malloc(MAX_EVENT_LENGTH);
152                                 if (!path->system) {
153                                         free(path);
154                                         return NULL;
155                                 }
156                                 path->name = malloc(MAX_EVENT_LENGTH);
157                                 if (!path->name) {
158                                         free(path->system);
159                                         free(path);
160                                         return NULL;
161                                 }
162                                 strncpy(path->system, sys_dirent.d_name,
163                                         MAX_EVENT_LENGTH);
164                                 strncpy(path->name, evt_dirent.d_name,
165                                         MAX_EVENT_LENGTH);
166                                 return path;
167                         }
168                 }
169                 closedir(evt_dir);
170         }
171
172         closedir(sys_dir);
173         return NULL;
174 }
175
176 #define TP_PATH_LEN (MAX_EVENT_LENGTH * 2 + 1)
177 static const char *tracepoint_id_to_name(u64 config)
178 {
179         static char buf[TP_PATH_LEN];
180         struct tracepoint_path *path;
181
182         path = tracepoint_id_to_path(config);
183         if (path) {
184                 snprintf(buf, TP_PATH_LEN, "%s:%s", path->system, path->name);
185                 free(path->name);
186                 free(path->system);
187                 free(path);
188         } else
189                 snprintf(buf, TP_PATH_LEN, "%s:%s", "unknown", "unknown");
190
191         return buf;
192 }
193
194 const char *event_type(int type)
195 {
196         switch (type) {
197         case PERF_TYPE_HARDWARE:
198                 return "hardware";
199
200         case PERF_TYPE_SOFTWARE:
201                 return "software";
202
203         case PERF_TYPE_TRACEPOINT:
204                 return "tracepoint";
205
206         case PERF_TYPE_HW_CACHE:
207                 return "hardware-cache";
208
209         default:
210                 break;
211         }
212
213         return "unknown";
214 }
215
216 const char *event_name(struct perf_evsel *evsel)
217 {
218         u64 config = evsel->attr.config;
219         int type = evsel->attr.type;
220
221         if (type == PERF_TYPE_RAW || type == PERF_TYPE_HARDWARE || type == PERF_TYPE_HW_CACHE) {
222                 /*
223                  * XXX minimal fix, see comment on perf_evsen__name, this static buffer
224                  * will go away together with event_name in the next devel cycle.
225                  */
226                 static char bf[128];
227                 perf_evsel__name(evsel, bf, sizeof(bf));
228                 return bf;
229         }
230
231         if (evsel->name)
232                 return evsel->name;
233
234         return __event_name(type, config);
235 }
236
237 const char *__event_name(int type, u64 config)
238 {
239         static char buf[32];
240
241         if (type == PERF_TYPE_RAW) {
242                 sprintf(buf, "raw 0x%" PRIx64, config);
243                 return buf;
244         }
245
246         switch (type) {
247         case PERF_TYPE_HARDWARE:
248                 return __perf_evsel__hw_name(config);
249
250         case PERF_TYPE_HW_CACHE:
251                 __perf_evsel__hw_cache_name(config, buf, sizeof(buf));
252                 return buf;
253
254         case PERF_TYPE_SOFTWARE:
255                 if (config < PERF_COUNT_SW_MAX && sw_event_names[config])
256                         return sw_event_names[config];
257                 return "unknown-software";
258
259         case PERF_TYPE_TRACEPOINT:
260                 return tracepoint_id_to_name(config);
261
262         default:
263                 break;
264         }
265
266         return "unknown";
267 }
268
269 static int add_event(struct list_head **_list, int *idx,
270                      struct perf_event_attr *attr, char *name)
271 {
272         struct perf_evsel *evsel;
273         struct list_head *list = *_list;
274
275         if (!list) {
276                 list = malloc(sizeof(*list));
277                 if (!list)
278                         return -ENOMEM;
279                 INIT_LIST_HEAD(list);
280         }
281
282         event_attr_init(attr);
283
284         evsel = perf_evsel__new(attr, (*idx)++);
285         if (!evsel) {
286                 free(list);
287                 return -ENOMEM;
288         }
289
290         evsel->name = strdup(name);
291         list_add_tail(&evsel->node, list);
292         *_list = list;
293         return 0;
294 }
295
296 static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
297 {
298         int i, j;
299         int n, longest = -1;
300
301         for (i = 0; i < size; i++) {
302                 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
303                         n = strlen(names[i][j]);
304                         if (n > longest && !strncasecmp(str, names[i][j], n))
305                                 longest = n;
306                 }
307                 if (longest > 0)
308                         return i;
309         }
310
311         return -1;
312 }
313
314 int parse_events_add_cache(struct list_head **list, int *idx,
315                            char *type, char *op_result1, char *op_result2)
316 {
317         struct perf_event_attr attr;
318         char name[MAX_NAME_LEN];
319         int cache_type = -1, cache_op = -1, cache_result = -1;
320         char *op_result[2] = { op_result1, op_result2 };
321         int i, n;
322
323         /*
324          * No fallback - if we cannot get a clear cache type
325          * then bail out:
326          */
327         cache_type = parse_aliases(type, perf_evsel__hw_cache,
328                                    PERF_COUNT_HW_CACHE_MAX);
329         if (cache_type == -1)
330                 return -EINVAL;
331
332         n = snprintf(name, MAX_NAME_LEN, "%s", type);
333
334         for (i = 0; (i < 2) && (op_result[i]); i++) {
335                 char *str = op_result[i];
336
337                 snprintf(name + n, MAX_NAME_LEN - n, "-%s\n", str);
338
339                 if (cache_op == -1) {
340                         cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
341                                                  PERF_COUNT_HW_CACHE_OP_MAX);
342                         if (cache_op >= 0) {
343                                 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
344                                         return -EINVAL;
345                                 continue;
346                         }
347                 }
348
349                 if (cache_result == -1) {
350                         cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
351                                                      PERF_COUNT_HW_CACHE_RESULT_MAX);
352                         if (cache_result >= 0)
353                                 continue;
354                 }
355         }
356
357         /*
358          * Fall back to reads:
359          */
360         if (cache_op == -1)
361                 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
362
363         /*
364          * Fall back to accesses:
365          */
366         if (cache_result == -1)
367                 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
368
369         memset(&attr, 0, sizeof(attr));
370         attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
371         attr.type = PERF_TYPE_HW_CACHE;
372         return add_event(list, idx, &attr, name);
373 }
374
375 static int add_tracepoint(struct list_head **list, int *idx,
376                           char *sys_name, char *evt_name)
377 {
378         struct perf_event_attr attr;
379         char name[MAX_NAME_LEN];
380         char evt_path[MAXPATHLEN];
381         char id_buf[4];
382         u64 id;
383         int fd;
384
385         snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
386                  sys_name, evt_name);
387
388         fd = open(evt_path, O_RDONLY);
389         if (fd < 0)
390                 return -1;
391
392         if (read(fd, id_buf, sizeof(id_buf)) < 0) {
393                 close(fd);
394                 return -1;
395         }
396
397         close(fd);
398         id = atoll(id_buf);
399
400         memset(&attr, 0, sizeof(attr));
401         attr.config = id;
402         attr.type = PERF_TYPE_TRACEPOINT;
403         attr.sample_type |= PERF_SAMPLE_RAW;
404         attr.sample_type |= PERF_SAMPLE_TIME;
405         attr.sample_type |= PERF_SAMPLE_CPU;
406         attr.sample_period = 1;
407
408         snprintf(name, MAX_NAME_LEN, "%s:%s", sys_name, evt_name);
409         return add_event(list, idx, &attr, name);
410 }
411
412 static int add_tracepoint_multi(struct list_head **list, int *idx,
413                                 char *sys_name, char *evt_name)
414 {
415         char evt_path[MAXPATHLEN];
416         struct dirent *evt_ent;
417         DIR *evt_dir;
418         int ret = 0;
419
420         snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
421         evt_dir = opendir(evt_path);
422         if (!evt_dir) {
423                 perror("Can't open event dir");
424                 return -1;
425         }
426
427         while (!ret && (evt_ent = readdir(evt_dir))) {
428                 if (!strcmp(evt_ent->d_name, ".")
429                     || !strcmp(evt_ent->d_name, "..")
430                     || !strcmp(evt_ent->d_name, "enable")
431                     || !strcmp(evt_ent->d_name, "filter"))
432                         continue;
433
434                 if (!strglobmatch(evt_ent->d_name, evt_name))
435                         continue;
436
437                 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name);
438         }
439
440         return ret;
441 }
442
443 int parse_events_add_tracepoint(struct list_head **list, int *idx,
444                                 char *sys, char *event)
445 {
446         int ret;
447
448         ret = debugfs_valid_mountpoint(tracing_events_path);
449         if (ret)
450                 return ret;
451
452         return strpbrk(event, "*?") ?
453                add_tracepoint_multi(list, idx, sys, event) :
454                add_tracepoint(list, idx, sys, event);
455 }
456
457 static int
458 parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
459 {
460         int i;
461
462         for (i = 0; i < 3; i++) {
463                 if (!type || !type[i])
464                         break;
465
466                 switch (type[i]) {
467                 case 'r':
468                         attr->bp_type |= HW_BREAKPOINT_R;
469                         break;
470                 case 'w':
471                         attr->bp_type |= HW_BREAKPOINT_W;
472                         break;
473                 case 'x':
474                         attr->bp_type |= HW_BREAKPOINT_X;
475                         break;
476                 default:
477                         return -EINVAL;
478                 }
479         }
480
481         if (!attr->bp_type) /* Default */
482                 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
483
484         return 0;
485 }
486
487 int parse_events_add_breakpoint(struct list_head **list, int *idx,
488                                 void *ptr, char *type)
489 {
490         struct perf_event_attr attr;
491         char name[MAX_NAME_LEN];
492
493         memset(&attr, 0, sizeof(attr));
494         attr.bp_addr = (unsigned long) ptr;
495
496         if (parse_breakpoint_type(type, &attr))
497                 return -EINVAL;
498
499         /*
500          * We should find a nice way to override the access length
501          * Provide some defaults for now
502          */
503         if (attr.bp_type == HW_BREAKPOINT_X)
504                 attr.bp_len = sizeof(long);
505         else
506                 attr.bp_len = HW_BREAKPOINT_LEN_4;
507
508         attr.type = PERF_TYPE_BREAKPOINT;
509
510         snprintf(name, MAX_NAME_LEN, "mem:%p:%s", ptr, type ? type : "rw");
511         return add_event(list, idx, &attr, name);
512 }
513
514 static int config_term(struct perf_event_attr *attr,
515                        struct parse_events__term *term)
516 {
517 #define CHECK_TYPE_VAL(type)                                    \
518 do {                                                            \
519         if (PARSE_EVENTS__TERM_TYPE_ ## type != term->type_val) \
520                 return -EINVAL;                                 \
521 } while (0)
522
523         switch (term->type_term) {
524         case PARSE_EVENTS__TERM_TYPE_CONFIG:
525                 CHECK_TYPE_VAL(NUM);
526                 attr->config = term->val.num;
527                 break;
528         case PARSE_EVENTS__TERM_TYPE_CONFIG1:
529                 CHECK_TYPE_VAL(NUM);
530                 attr->config1 = term->val.num;
531                 break;
532         case PARSE_EVENTS__TERM_TYPE_CONFIG2:
533                 CHECK_TYPE_VAL(NUM);
534                 attr->config2 = term->val.num;
535                 break;
536         case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
537                 CHECK_TYPE_VAL(NUM);
538                 attr->sample_period = term->val.num;
539                 break;
540         case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
541                 /*
542                  * TODO uncomment when the field is available
543                  * attr->branch_sample_type = term->val.num;
544                  */
545                 break;
546         case PARSE_EVENTS__TERM_TYPE_NAME:
547                 CHECK_TYPE_VAL(STR);
548                 break;
549         default:
550                 return -EINVAL;
551         }
552
553         return 0;
554 #undef CHECK_TYPE_VAL
555 }
556
557 static int config_attr(struct perf_event_attr *attr,
558                        struct list_head *head, int fail)
559 {
560         struct parse_events__term *term;
561
562         list_for_each_entry(term, head, list)
563                 if (config_term(attr, term) && fail)
564                         return -EINVAL;
565
566         return 0;
567 }
568
569 int parse_events_add_numeric(struct list_head **list, int *idx,
570                              unsigned long type, unsigned long config,
571                              struct list_head *head_config)
572 {
573         struct perf_event_attr attr;
574
575         memset(&attr, 0, sizeof(attr));
576         attr.type = type;
577         attr.config = config;
578
579         if (head_config &&
580             config_attr(&attr, head_config, 1))
581                 return -EINVAL;
582
583         return add_event(list, idx, &attr,
584                          (char *) __event_name(type, config));
585 }
586
587 static int parse_events__is_name_term(struct parse_events__term *term)
588 {
589         return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
590 }
591
592 static char *pmu_event_name(struct perf_event_attr *attr,
593                             struct list_head *head_terms)
594 {
595         struct parse_events__term *term;
596
597         list_for_each_entry(term, head_terms, list)
598                 if (parse_events__is_name_term(term))
599                         return term->val.str;
600
601         return (char *) __event_name(PERF_TYPE_RAW, attr->config);
602 }
603
604 int parse_events_add_pmu(struct list_head **list, int *idx,
605                          char *name, struct list_head *head_config)
606 {
607         struct perf_event_attr attr;
608         struct perf_pmu *pmu;
609
610         pmu = perf_pmu__find(name);
611         if (!pmu)
612                 return -EINVAL;
613
614         memset(&attr, 0, sizeof(attr));
615
616         /*
617          * Configure hardcoded terms first, no need to check
618          * return value when called with fail == 0 ;)
619          */
620         config_attr(&attr, head_config, 0);
621
622         if (perf_pmu__config(pmu, &attr, head_config))
623                 return -EINVAL;
624
625         return add_event(list, idx, &attr,
626                          pmu_event_name(&attr, head_config));
627 }
628
629 void parse_events_update_lists(struct list_head *list_event,
630                                struct list_head *list_all)
631 {
632         /*
633          * Called for single event definition. Update the
634          * 'all event' list, and reinit the 'signle event'
635          * list, for next event definition.
636          */
637         list_splice_tail(list_event, list_all);
638         free(list_event);
639 }
640
641 int parse_events_modifier(struct list_head *list, char *str)
642 {
643         struct perf_evsel *evsel;
644         int exclude = 0, exclude_GH = 0;
645         int eu = 0, ek = 0, eh = 0, eH = 0, eG = 0, precise = 0;
646
647         if (str == NULL)
648                 return 0;
649
650         while (*str) {
651                 if (*str == 'u') {
652                         if (!exclude)
653                                 exclude = eu = ek = eh = 1;
654                         eu = 0;
655                 } else if (*str == 'k') {
656                         if (!exclude)
657                                 exclude = eu = ek = eh = 1;
658                         ek = 0;
659                 } else if (*str == 'h') {
660                         if (!exclude)
661                                 exclude = eu = ek = eh = 1;
662                         eh = 0;
663                 } else if (*str == 'G') {
664                         if (!exclude_GH)
665                                 exclude_GH = eG = eH = 1;
666                         eG = 0;
667                 } else if (*str == 'H') {
668                         if (!exclude_GH)
669                                 exclude_GH = eG = eH = 1;
670                         eH = 0;
671                 } else if (*str == 'p') {
672                         precise++;
673                 } else
674                         break;
675
676                 ++str;
677         }
678
679         /*
680          * precise ip:
681          *
682          *  0 - SAMPLE_IP can have arbitrary skid
683          *  1 - SAMPLE_IP must have constant skid
684          *  2 - SAMPLE_IP requested to have 0 skid
685          *  3 - SAMPLE_IP must have 0 skid
686          *
687          *  See also PERF_RECORD_MISC_EXACT_IP
688          */
689         if (precise > 3)
690                 return -EINVAL;
691
692         list_for_each_entry(evsel, list, node) {
693                 evsel->attr.exclude_user   = eu;
694                 evsel->attr.exclude_kernel = ek;
695                 evsel->attr.exclude_hv     = eh;
696                 evsel->attr.precise_ip     = precise;
697                 evsel->attr.exclude_host   = eH;
698                 evsel->attr.exclude_guest  = eG;
699         }
700
701         return 0;
702 }
703
704 int parse_events(struct perf_evlist *evlist, const char *str, int unset __used)
705 {
706         LIST_HEAD(list);
707         LIST_HEAD(list_tmp);
708         YY_BUFFER_STATE buffer;
709         int ret, idx = evlist->nr_entries;
710
711         buffer = parse_events__scan_string(str);
712
713 #ifdef PARSER_DEBUG
714         parse_events_debug = 1;
715 #endif
716         ret = parse_events_parse(&list, &idx);
717
718         parse_events__flush_buffer(buffer);
719         parse_events__delete_buffer(buffer);
720         parse_events_lex_destroy();
721
722         if (!ret) {
723                 int entries = idx - evlist->nr_entries;
724                 perf_evlist__splice_list_tail(evlist, &list, entries);
725                 return 0;
726         }
727
728         /*
729          * There are 2 users - builtin-record and builtin-test objects.
730          * Both call perf_evlist__delete in case of error, so we dont
731          * need to bother.
732          */
733         fprintf(stderr, "invalid or unsupported event: '%s'\n", str);
734         fprintf(stderr, "Run 'perf list' for a list of valid events\n");
735         return ret;
736 }
737
738 int parse_events_option(const struct option *opt, const char *str,
739                         int unset __used)
740 {
741         struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
742         return parse_events(evlist, str, unset);
743 }
744
745 int parse_filter(const struct option *opt, const char *str,
746                  int unset __used)
747 {
748         struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
749         struct perf_evsel *last = NULL;
750
751         if (evlist->nr_entries > 0)
752                 last = list_entry(evlist->entries.prev, struct perf_evsel, node);
753
754         if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
755                 fprintf(stderr,
756                         "-F option should follow a -e tracepoint option\n");
757                 return -1;
758         }
759
760         last->filter = strdup(str);
761         if (last->filter == NULL) {
762                 fprintf(stderr, "not enough memory to hold filter string\n");
763                 return -1;
764         }
765
766         return 0;
767 }
768
769 static const char * const event_type_descriptors[] = {
770         "Hardware event",
771         "Software event",
772         "Tracepoint event",
773         "Hardware cache event",
774         "Raw hardware event descriptor",
775         "Hardware breakpoint",
776 };
777
778 /*
779  * Print the events from <debugfs_mount_point>/tracing/events
780  */
781
782 void print_tracepoint_events(const char *subsys_glob, const char *event_glob)
783 {
784         DIR *sys_dir, *evt_dir;
785         struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
786         char evt_path[MAXPATHLEN];
787         char dir_path[MAXPATHLEN];
788
789         if (debugfs_valid_mountpoint(tracing_events_path))
790                 return;
791
792         sys_dir = opendir(tracing_events_path);
793         if (!sys_dir)
794                 return;
795
796         for_each_subsystem(sys_dir, sys_dirent, sys_next) {
797                 if (subsys_glob != NULL && 
798                     !strglobmatch(sys_dirent.d_name, subsys_glob))
799                         continue;
800
801                 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
802                          sys_dirent.d_name);
803                 evt_dir = opendir(dir_path);
804                 if (!evt_dir)
805                         continue;
806
807                 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
808                         if (event_glob != NULL && 
809                             !strglobmatch(evt_dirent.d_name, event_glob))
810                                 continue;
811
812                         snprintf(evt_path, MAXPATHLEN, "%s:%s",
813                                  sys_dirent.d_name, evt_dirent.d_name);
814                         printf("  %-50s [%s]\n", evt_path,
815                                 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
816                 }
817                 closedir(evt_dir);
818         }
819         closedir(sys_dir);
820 }
821
822 /*
823  * Check whether event is in <debugfs_mount_point>/tracing/events
824  */
825
826 int is_valid_tracepoint(const char *event_string)
827 {
828         DIR *sys_dir, *evt_dir;
829         struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
830         char evt_path[MAXPATHLEN];
831         char dir_path[MAXPATHLEN];
832
833         if (debugfs_valid_mountpoint(tracing_events_path))
834                 return 0;
835
836         sys_dir = opendir(tracing_events_path);
837         if (!sys_dir)
838                 return 0;
839
840         for_each_subsystem(sys_dir, sys_dirent, sys_next) {
841
842                 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
843                          sys_dirent.d_name);
844                 evt_dir = opendir(dir_path);
845                 if (!evt_dir)
846                         continue;
847
848                 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
849                         snprintf(evt_path, MAXPATHLEN, "%s:%s",
850                                  sys_dirent.d_name, evt_dirent.d_name);
851                         if (!strcmp(evt_path, event_string)) {
852                                 closedir(evt_dir);
853                                 closedir(sys_dir);
854                                 return 1;
855                         }
856                 }
857                 closedir(evt_dir);
858         }
859         closedir(sys_dir);
860         return 0;
861 }
862
863 void print_events_type(u8 type)
864 {
865         struct event_symbol *syms = event_symbols;
866         unsigned int i;
867         char name[64];
868
869         for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
870                 if (type != syms->type)
871                         continue;
872
873                 if (strlen(syms->alias))
874                         snprintf(name, sizeof(name),  "%s OR %s",
875                                  syms->symbol, syms->alias);
876                 else
877                         snprintf(name, sizeof(name), "%s", syms->symbol);
878
879                 printf("  %-50s [%s]\n", name,
880                         event_type_descriptors[type]);
881         }
882 }
883
884 int print_hwcache_events(const char *event_glob)
885 {
886         unsigned int type, op, i, printed = 0;
887         char name[64];
888
889         for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
890                 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
891                         /* skip invalid cache type */
892                         if (!perf_evsel__is_cache_op_valid(type, op))
893                                 continue;
894
895                         for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
896                                 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
897                                                                         name, sizeof(name));
898                                 if (event_glob != NULL && !strglobmatch(name, event_glob))
899                                         continue;
900
901                                 printf("  %-50s [%s]\n", name,
902                                         event_type_descriptors[PERF_TYPE_HW_CACHE]);
903                                 ++printed;
904                         }
905                 }
906         }
907
908         return printed;
909 }
910
911 /*
912  * Print the help text for the event symbols:
913  */
914 void print_events(const char *event_glob)
915 {
916         unsigned int i, type, prev_type = -1, printed = 0, ntypes_printed = 0;
917         struct event_symbol *syms = event_symbols;
918         char name[MAX_NAME_LEN];
919
920         printf("\n");
921         printf("List of pre-defined events (to be used in -e):\n");
922
923         for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
924                 type = syms->type;
925
926                 if (type != prev_type && printed) {
927                         printf("\n");
928                         printed = 0;
929                         ntypes_printed++;
930                 }
931
932                 if (event_glob != NULL && 
933                     !(strglobmatch(syms->symbol, event_glob) ||
934                       (syms->alias && strglobmatch(syms->alias, event_glob))))
935                         continue;
936
937                 if (strlen(syms->alias))
938                         snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
939                 else
940                         strncpy(name, syms->symbol, MAX_NAME_LEN);
941                 printf("  %-50s [%s]\n", name,
942                         event_type_descriptors[type]);
943
944                 prev_type = type;
945                 ++printed;
946         }
947
948         if (ntypes_printed) {
949                 printed = 0;
950                 printf("\n");
951         }
952         print_hwcache_events(event_glob);
953
954         if (event_glob != NULL)
955                 return;
956
957         printf("\n");
958         printf("  %-50s [%s]\n",
959                "rNNN",
960                event_type_descriptors[PERF_TYPE_RAW]);
961         printf("  %-50s [%s]\n",
962                "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
963                event_type_descriptors[PERF_TYPE_RAW]);
964         printf("   (see 'perf list --help' on how to encode it)\n");
965         printf("\n");
966
967         printf("  %-50s [%s]\n",
968                         "mem:<addr>[:access]",
969                         event_type_descriptors[PERF_TYPE_BREAKPOINT]);
970         printf("\n");
971
972         print_tracepoint_events(NULL, NULL);
973 }
974
975 int parse_events__is_hardcoded_term(struct parse_events__term *term)
976 {
977         return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
978 }
979
980 static int new_term(struct parse_events__term **_term, int type_val,
981                     int type_term, char *config,
982                     char *str, long num)
983 {
984         struct parse_events__term *term;
985
986         term = zalloc(sizeof(*term));
987         if (!term)
988                 return -ENOMEM;
989
990         INIT_LIST_HEAD(&term->list);
991         term->type_val  = type_val;
992         term->type_term = type_term;
993         term->config = config;
994
995         switch (type_val) {
996         case PARSE_EVENTS__TERM_TYPE_NUM:
997                 term->val.num = num;
998                 break;
999         case PARSE_EVENTS__TERM_TYPE_STR:
1000                 term->val.str = str;
1001                 break;
1002         default:
1003                 return -EINVAL;
1004         }
1005
1006         *_term = term;
1007         return 0;
1008 }
1009
1010 int parse_events__term_num(struct parse_events__term **term,
1011                            int type_term, char *config, long num)
1012 {
1013         return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
1014                         config, NULL, num);
1015 }
1016
1017 int parse_events__term_str(struct parse_events__term **term,
1018                            int type_term, char *config, char *str)
1019 {
1020         return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
1021                         config, str, 0);
1022 }
1023
1024 void parse_events__free_terms(struct list_head *terms)
1025 {
1026         struct parse_events__term *term, *h;
1027
1028         list_for_each_entry_safe(term, h, terms, list)
1029                 free(term);
1030
1031         free(terms);
1032 }