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