perf probe: Remove xzalloc() from util/probe-{event, finder}.c
[safe/jmp/linux-2.6] / tools / perf / util / parse-events.c
1 #include "../../../include/linux/hw_breakpoint.h"
2 #include "util.h"
3 #include "../perf.h"
4 #include "parse-options.h"
5 #include "parse-events.h"
6 #include "exec_cmd.h"
7 #include "string.h"
8 #include "symbol.h"
9 #include "cache.h"
10 #include "header.h"
11 #include "debugfs.h"
12
13 int                             nr_counters;
14
15 struct perf_event_attr          attrs[MAX_COUNTERS];
16 char                            *filters[MAX_COUNTERS];
17
18 struct event_symbol {
19         u8              type;
20         u64             config;
21         const char      *symbol;
22         const char      *alias;
23 };
24
25 enum event_result {
26         EVT_FAILED,
27         EVT_HANDLED,
28         EVT_HANDLED_ALL
29 };
30
31 char debugfs_path[MAXPATHLEN];
32
33 #define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
34 #define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
35
36 static struct event_symbol event_symbols[] = {
37   { CHW(CPU_CYCLES),            "cpu-cycles",           "cycles"        },
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
45   { CSW(CPU_CLOCK),             "cpu-clock",            ""              },
46   { CSW(TASK_CLOCK),            "task-clock",           ""              },
47   { CSW(PAGE_FAULTS),           "page-faults",          "faults"        },
48   { CSW(PAGE_FAULTS_MIN),       "minor-faults",         ""              },
49   { CSW(PAGE_FAULTS_MAJ),       "major-faults",         ""              },
50   { CSW(CONTEXT_SWITCHES),      "context-switches",     "cs"            },
51   { CSW(CPU_MIGRATIONS),        "cpu-migrations",       "migrations"    },
52   { CSW(ALIGNMENT_FAULTS),      "alignment-faults",     ""              },
53   { CSW(EMULATION_FAULTS),      "emulation-faults",     ""              },
54 };
55
56 #define __PERF_EVENT_FIELD(config, name) \
57         ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
58
59 #define PERF_EVENT_RAW(config)  __PERF_EVENT_FIELD(config, RAW)
60 #define PERF_EVENT_CONFIG(config)       __PERF_EVENT_FIELD(config, CONFIG)
61 #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
62 #define PERF_EVENT_ID(config)           __PERF_EVENT_FIELD(config, EVENT)
63
64 static const char *hw_event_names[] = {
65         "cycles",
66         "instructions",
67         "cache-references",
68         "cache-misses",
69         "branches",
70         "branch-misses",
71         "bus-cycles",
72 };
73
74 static const char *sw_event_names[] = {
75         "cpu-clock-msecs",
76         "task-clock-msecs",
77         "page-faults",
78         "context-switches",
79         "CPU-migrations",
80         "minor-faults",
81         "major-faults",
82         "alignment-faults",
83         "emulation-faults",
84 };
85
86 #define MAX_ALIASES 8
87
88 static const char *hw_cache[][MAX_ALIASES] = {
89  { "L1-dcache", "l1-d",         "l1d",          "L1-data",              },
90  { "L1-icache", "l1-i",         "l1i",          "L1-instruction",       },
91  { "LLC",       "L2"                                                    },
92  { "dTLB",      "d-tlb",        "Data-TLB",                             },
93  { "iTLB",      "i-tlb",        "Instruction-TLB",                      },
94  { "branch",    "branches",     "bpu",          "btb",          "bpc",  },
95 };
96
97 static const char *hw_cache_op[][MAX_ALIASES] = {
98  { "load",      "loads",        "read",                                 },
99  { "store",     "stores",       "write",                                },
100  { "prefetch",  "prefetches",   "speculative-read", "speculative-load", },
101 };
102
103 static const char *hw_cache_result[][MAX_ALIASES] = {
104  { "refs",      "Reference",    "ops",          "access",               },
105  { "misses",    "miss",                                                 },
106 };
107
108 #define C(x)            PERF_COUNT_HW_CACHE_##x
109 #define CACHE_READ      (1 << C(OP_READ))
110 #define CACHE_WRITE     (1 << C(OP_WRITE))
111 #define CACHE_PREFETCH  (1 << C(OP_PREFETCH))
112 #define COP(x)          (1 << x)
113
114 /*
115  * cache operartion stat
116  * L1I : Read and prefetch only
117  * ITLB and BPU : Read-only
118  */
119 static unsigned long hw_cache_stat[C(MAX)] = {
120  [C(L1D)]       = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
121  [C(L1I)]       = (CACHE_READ | CACHE_PREFETCH),
122  [C(LL)]        = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
123  [C(DTLB)]      = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
124  [C(ITLB)]      = (CACHE_READ),
125  [C(BPU)]       = (CACHE_READ),
126 };
127
128 #define for_each_subsystem(sys_dir, sys_dirent, sys_next)              \
129         while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next)        \
130         if (sys_dirent.d_type == DT_DIR &&                                     \
131            (strcmp(sys_dirent.d_name, ".")) &&                                 \
132            (strcmp(sys_dirent.d_name, "..")))
133
134 static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
135 {
136         char evt_path[MAXPATHLEN];
137         int fd;
138
139         snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
140                         sys_dir->d_name, evt_dir->d_name);
141         fd = open(evt_path, O_RDONLY);
142         if (fd < 0)
143                 return -EINVAL;
144         close(fd);
145
146         return 0;
147 }
148
149 #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next)              \
150         while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next)        \
151         if (evt_dirent.d_type == DT_DIR &&                                     \
152            (strcmp(evt_dirent.d_name, ".")) &&                                 \
153            (strcmp(evt_dirent.d_name, "..")) &&                                \
154            (!tp_event_has_id(&sys_dirent, &evt_dirent)))
155
156 #define MAX_EVENT_LENGTH 512
157
158
159 struct tracepoint_path *tracepoint_id_to_path(u64 config)
160 {
161         struct tracepoint_path *path = NULL;
162         DIR *sys_dir, *evt_dir;
163         struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
164         char id_buf[4];
165         int fd;
166         u64 id;
167         char evt_path[MAXPATHLEN];
168         char dir_path[MAXPATHLEN];
169
170         if (debugfs_valid_mountpoint(debugfs_path))
171                 return NULL;
172
173         sys_dir = opendir(debugfs_path);
174         if (!sys_dir)
175                 return NULL;
176
177         for_each_subsystem(sys_dir, sys_dirent, sys_next) {
178
179                 snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
180                          sys_dirent.d_name);
181                 evt_dir = opendir(dir_path);
182                 if (!evt_dir)
183                         continue;
184
185                 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
186
187                         snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
188                                  evt_dirent.d_name);
189                         fd = open(evt_path, O_RDONLY);
190                         if (fd < 0)
191                                 continue;
192                         if (read(fd, id_buf, sizeof(id_buf)) < 0) {
193                                 close(fd);
194                                 continue;
195                         }
196                         close(fd);
197                         id = atoll(id_buf);
198                         if (id == config) {
199                                 closedir(evt_dir);
200                                 closedir(sys_dir);
201                                 path = zalloc(sizeof(*path));
202                                 path->system = malloc(MAX_EVENT_LENGTH);
203                                 if (!path->system) {
204                                         free(path);
205                                         return NULL;
206                                 }
207                                 path->name = malloc(MAX_EVENT_LENGTH);
208                                 if (!path->name) {
209                                         free(path->system);
210                                         free(path);
211                                         return NULL;
212                                 }
213                                 strncpy(path->system, sys_dirent.d_name,
214                                         MAX_EVENT_LENGTH);
215                                 strncpy(path->name, evt_dirent.d_name,
216                                         MAX_EVENT_LENGTH);
217                                 return path;
218                         }
219                 }
220                 closedir(evt_dir);
221         }
222
223         closedir(sys_dir);
224         return NULL;
225 }
226
227 #define TP_PATH_LEN (MAX_EVENT_LENGTH * 2 + 1)
228 static const char *tracepoint_id_to_name(u64 config)
229 {
230         static char buf[TP_PATH_LEN];
231         struct tracepoint_path *path;
232
233         path = tracepoint_id_to_path(config);
234         if (path) {
235                 snprintf(buf, TP_PATH_LEN, "%s:%s", path->system, path->name);
236                 free(path->name);
237                 free(path->system);
238                 free(path);
239         } else
240                 snprintf(buf, TP_PATH_LEN, "%s:%s", "unknown", "unknown");
241
242         return buf;
243 }
244
245 static int is_cache_op_valid(u8 cache_type, u8 cache_op)
246 {
247         if (hw_cache_stat[cache_type] & COP(cache_op))
248                 return 1;       /* valid */
249         else
250                 return 0;       /* invalid */
251 }
252
253 static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result)
254 {
255         static char name[50];
256
257         if (cache_result) {
258                 sprintf(name, "%s-%s-%s", hw_cache[cache_type][0],
259                         hw_cache_op[cache_op][0],
260                         hw_cache_result[cache_result][0]);
261         } else {
262                 sprintf(name, "%s-%s", hw_cache[cache_type][0],
263                         hw_cache_op[cache_op][1]);
264         }
265
266         return name;
267 }
268
269 const char *event_name(int counter)
270 {
271         u64 config = attrs[counter].config;
272         int type = attrs[counter].type;
273
274         return __event_name(type, config);
275 }
276
277 const char *__event_name(int type, u64 config)
278 {
279         static char buf[32];
280
281         if (type == PERF_TYPE_RAW) {
282                 sprintf(buf, "raw 0x%llx", config);
283                 return buf;
284         }
285
286         switch (type) {
287         case PERF_TYPE_HARDWARE:
288                 if (config < PERF_COUNT_HW_MAX)
289                         return hw_event_names[config];
290                 return "unknown-hardware";
291
292         case PERF_TYPE_HW_CACHE: {
293                 u8 cache_type, cache_op, cache_result;
294
295                 cache_type   = (config >>  0) & 0xff;
296                 if (cache_type > PERF_COUNT_HW_CACHE_MAX)
297                         return "unknown-ext-hardware-cache-type";
298
299                 cache_op     = (config >>  8) & 0xff;
300                 if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
301                         return "unknown-ext-hardware-cache-op";
302
303                 cache_result = (config >> 16) & 0xff;
304                 if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
305                         return "unknown-ext-hardware-cache-result";
306
307                 if (!is_cache_op_valid(cache_type, cache_op))
308                         return "invalid-cache";
309
310                 return event_cache_name(cache_type, cache_op, cache_result);
311         }
312
313         case PERF_TYPE_SOFTWARE:
314                 if (config < PERF_COUNT_SW_MAX)
315                         return sw_event_names[config];
316                 return "unknown-software";
317
318         case PERF_TYPE_TRACEPOINT:
319                 return tracepoint_id_to_name(config);
320
321         default:
322                 break;
323         }
324
325         return "unknown";
326 }
327
328 static int parse_aliases(const char **str, const char *names[][MAX_ALIASES], int size)
329 {
330         int i, j;
331         int n, longest = -1;
332
333         for (i = 0; i < size; i++) {
334                 for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
335                         n = strlen(names[i][j]);
336                         if (n > longest && !strncasecmp(*str, names[i][j], n))
337                                 longest = n;
338                 }
339                 if (longest > 0) {
340                         *str += longest;
341                         return i;
342                 }
343         }
344
345         return -1;
346 }
347
348 static enum event_result
349 parse_generic_hw_event(const char **str, struct perf_event_attr *attr)
350 {
351         const char *s = *str;
352         int cache_type = -1, cache_op = -1, cache_result = -1;
353
354         cache_type = parse_aliases(&s, hw_cache, PERF_COUNT_HW_CACHE_MAX);
355         /*
356          * No fallback - if we cannot get a clear cache type
357          * then bail out:
358          */
359         if (cache_type == -1)
360                 return EVT_FAILED;
361
362         while ((cache_op == -1 || cache_result == -1) && *s == '-') {
363                 ++s;
364
365                 if (cache_op == -1) {
366                         cache_op = parse_aliases(&s, hw_cache_op,
367                                                 PERF_COUNT_HW_CACHE_OP_MAX);
368                         if (cache_op >= 0) {
369                                 if (!is_cache_op_valid(cache_type, cache_op))
370                                         return 0;
371                                 continue;
372                         }
373                 }
374
375                 if (cache_result == -1) {
376                         cache_result = parse_aliases(&s, hw_cache_result,
377                                                 PERF_COUNT_HW_CACHE_RESULT_MAX);
378                         if (cache_result >= 0)
379                                 continue;
380                 }
381
382                 /*
383                  * Can't parse this as a cache op or result, so back up
384                  * to the '-'.
385                  */
386                 --s;
387                 break;
388         }
389
390         /*
391          * Fall back to reads:
392          */
393         if (cache_op == -1)
394                 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
395
396         /*
397          * Fall back to accesses:
398          */
399         if (cache_result == -1)
400                 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
401
402         attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
403         attr->type = PERF_TYPE_HW_CACHE;
404
405         *str = s;
406         return EVT_HANDLED;
407 }
408
409 static enum event_result
410 parse_single_tracepoint_event(char *sys_name,
411                               const char *evt_name,
412                               unsigned int evt_length,
413                               char *flags,
414                               struct perf_event_attr *attr,
415                               const char **strp)
416 {
417         char evt_path[MAXPATHLEN];
418         char id_buf[4];
419         u64 id;
420         int fd;
421
422         if (flags) {
423                 if (!strncmp(flags, "record", strlen(flags))) {
424                         attr->sample_type |= PERF_SAMPLE_RAW;
425                         attr->sample_type |= PERF_SAMPLE_TIME;
426                         attr->sample_type |= PERF_SAMPLE_CPU;
427                 }
428         }
429
430         snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
431                  sys_name, evt_name);
432
433         fd = open(evt_path, O_RDONLY);
434         if (fd < 0)
435                 return EVT_FAILED;
436
437         if (read(fd, id_buf, sizeof(id_buf)) < 0) {
438                 close(fd);
439                 return EVT_FAILED;
440         }
441
442         close(fd);
443         id = atoll(id_buf);
444         attr->config = id;
445         attr->type = PERF_TYPE_TRACEPOINT;
446         *strp = evt_name + evt_length;
447
448         return EVT_HANDLED;
449 }
450
451 /* sys + ':' + event + ':' + flags*/
452 #define MAX_EVOPT_LEN   (MAX_EVENT_LENGTH * 2 + 2 + 128)
453 static enum event_result
454 parse_multiple_tracepoint_event(char *sys_name, const char *evt_exp,
455                                 char *flags)
456 {
457         char evt_path[MAXPATHLEN];
458         struct dirent *evt_ent;
459         DIR *evt_dir;
460
461         snprintf(evt_path, MAXPATHLEN, "%s/%s", debugfs_path, sys_name);
462         evt_dir = opendir(evt_path);
463
464         if (!evt_dir) {
465                 perror("Can't open event dir");
466                 return EVT_FAILED;
467         }
468
469         while ((evt_ent = readdir(evt_dir))) {
470                 char event_opt[MAX_EVOPT_LEN + 1];
471                 int len;
472
473                 if (!strcmp(evt_ent->d_name, ".")
474                     || !strcmp(evt_ent->d_name, "..")
475                     || !strcmp(evt_ent->d_name, "enable")
476                     || !strcmp(evt_ent->d_name, "filter"))
477                         continue;
478
479                 if (!strglobmatch(evt_ent->d_name, evt_exp))
480                         continue;
481
482                 len = snprintf(event_opt, MAX_EVOPT_LEN, "%s:%s%s%s", sys_name,
483                                evt_ent->d_name, flags ? ":" : "",
484                                flags ?: "");
485                 if (len < 0)
486                         return EVT_FAILED;
487
488                 if (parse_events(NULL, event_opt, 0))
489                         return EVT_FAILED;
490         }
491
492         return EVT_HANDLED_ALL;
493 }
494
495
496 static enum event_result parse_tracepoint_event(const char **strp,
497                                     struct perf_event_attr *attr)
498 {
499         const char *evt_name;
500         char *flags;
501         char sys_name[MAX_EVENT_LENGTH];
502         unsigned int sys_length, evt_length;
503
504         if (debugfs_valid_mountpoint(debugfs_path))
505                 return 0;
506
507         evt_name = strchr(*strp, ':');
508         if (!evt_name)
509                 return EVT_FAILED;
510
511         sys_length = evt_name - *strp;
512         if (sys_length >= MAX_EVENT_LENGTH)
513                 return 0;
514
515         strncpy(sys_name, *strp, sys_length);
516         sys_name[sys_length] = '\0';
517         evt_name = evt_name + 1;
518
519         flags = strchr(evt_name, ':');
520         if (flags) {
521                 /* split it out: */
522                 evt_name = strndup(evt_name, flags - evt_name);
523                 flags++;
524         }
525
526         evt_length = strlen(evt_name);
527         if (evt_length >= MAX_EVENT_LENGTH)
528                 return EVT_FAILED;
529
530         if (strpbrk(evt_name, "*?")) {
531                 *strp = evt_name + evt_length;
532                 return parse_multiple_tracepoint_event(sys_name, evt_name,
533                                                        flags);
534         } else
535                 return parse_single_tracepoint_event(sys_name, evt_name,
536                                                      evt_length, flags,
537                                                      attr, strp);
538 }
539
540 static enum event_result
541 parse_breakpoint_type(const char *type, const char **strp,
542                       struct perf_event_attr *attr)
543 {
544         int i;
545
546         for (i = 0; i < 3; i++) {
547                 if (!type[i])
548                         break;
549
550                 switch (type[i]) {
551                 case 'r':
552                         attr->bp_type |= HW_BREAKPOINT_R;
553                         break;
554                 case 'w':
555                         attr->bp_type |= HW_BREAKPOINT_W;
556                         break;
557                 case 'x':
558                         attr->bp_type |= HW_BREAKPOINT_X;
559                         break;
560                 default:
561                         return EVT_FAILED;
562                 }
563         }
564         if (!attr->bp_type) /* Default */
565                 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
566
567         *strp = type + i;
568
569         return EVT_HANDLED;
570 }
571
572 static enum event_result
573 parse_breakpoint_event(const char **strp, struct perf_event_attr *attr)
574 {
575         const char *target;
576         const char *type;
577         char *endaddr;
578         u64 addr;
579         enum event_result err;
580
581         target = strchr(*strp, ':');
582         if (!target)
583                 return EVT_FAILED;
584
585         if (strncmp(*strp, "mem", target - *strp) != 0)
586                 return EVT_FAILED;
587
588         target++;
589
590         addr = strtoull(target, &endaddr, 0);
591         if (target == endaddr)
592                 return EVT_FAILED;
593
594         attr->bp_addr = addr;
595         *strp = endaddr;
596
597         type = strchr(target, ':');
598
599         /* If no type is defined, just rw as default */
600         if (!type) {
601                 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
602         } else {
603                 err = parse_breakpoint_type(++type, strp, attr);
604                 if (err == EVT_FAILED)
605                         return EVT_FAILED;
606         }
607
608         /* We should find a nice way to override the access type */
609         attr->bp_len = HW_BREAKPOINT_LEN_4;
610         attr->type = PERF_TYPE_BREAKPOINT;
611
612         return EVT_HANDLED;
613 }
614
615 static int check_events(const char *str, unsigned int i)
616 {
617         int n;
618
619         n = strlen(event_symbols[i].symbol);
620         if (!strncmp(str, event_symbols[i].symbol, n))
621                 return n;
622
623         n = strlen(event_symbols[i].alias);
624         if (n)
625                 if (!strncmp(str, event_symbols[i].alias, n))
626                         return n;
627         return 0;
628 }
629
630 static enum event_result
631 parse_symbolic_event(const char **strp, struct perf_event_attr *attr)
632 {
633         const char *str = *strp;
634         unsigned int i;
635         int n;
636
637         for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
638                 n = check_events(str, i);
639                 if (n > 0) {
640                         attr->type = event_symbols[i].type;
641                         attr->config = event_symbols[i].config;
642                         *strp = str + n;
643                         return EVT_HANDLED;
644                 }
645         }
646         return EVT_FAILED;
647 }
648
649 static enum event_result
650 parse_raw_event(const char **strp, struct perf_event_attr *attr)
651 {
652         const char *str = *strp;
653         u64 config;
654         int n;
655
656         if (*str != 'r')
657                 return EVT_FAILED;
658         n = hex2u64(str + 1, &config);
659         if (n > 0) {
660                 if (str[n+1] == 'p') {
661                         attr->precise = 1;
662                         n++;
663                 }
664                 *strp = str + n + 1;
665                 attr->type = PERF_TYPE_RAW;
666                 attr->config = config;
667                 return EVT_HANDLED;
668         }
669         return EVT_FAILED;
670 }
671
672 static enum event_result
673 parse_numeric_event(const char **strp, struct perf_event_attr *attr)
674 {
675         const char *str = *strp;
676         char *endp;
677         unsigned long type;
678         u64 config;
679
680         type = strtoul(str, &endp, 0);
681         if (endp > str && type < PERF_TYPE_MAX && *endp == ':') {
682                 str = endp + 1;
683                 config = strtoul(str, &endp, 0);
684                 if (endp > str) {
685                         attr->type = type;
686                         attr->config = config;
687                         *strp = endp;
688                         return EVT_HANDLED;
689                 }
690         }
691         return EVT_FAILED;
692 }
693
694 static enum event_result
695 parse_event_modifier(const char **strp, struct perf_event_attr *attr)
696 {
697         const char *str = *strp;
698         int eu = 1, ek = 1, eh = 1;
699
700         if (*str++ != ':')
701                 return 0;
702         while (*str) {
703                 if (*str == 'u')
704                         eu = 0;
705                 else if (*str == 'k')
706                         ek = 0;
707                 else if (*str == 'h')
708                         eh = 0;
709                 else
710                         break;
711                 ++str;
712         }
713         if (str >= *strp + 2) {
714                 *strp = str;
715                 attr->exclude_user   = eu;
716                 attr->exclude_kernel = ek;
717                 attr->exclude_hv     = eh;
718                 return 1;
719         }
720         return 0;
721 }
722
723 /*
724  * Each event can have multiple symbolic names.
725  * Symbolic names are (almost) exactly matched.
726  */
727 static enum event_result
728 parse_event_symbols(const char **str, struct perf_event_attr *attr)
729 {
730         enum event_result ret;
731
732         ret = parse_tracepoint_event(str, attr);
733         if (ret != EVT_FAILED)
734                 goto modifier;
735
736         ret = parse_raw_event(str, attr);
737         if (ret != EVT_FAILED)
738                 goto modifier;
739
740         ret = parse_numeric_event(str, attr);
741         if (ret != EVT_FAILED)
742                 goto modifier;
743
744         ret = parse_symbolic_event(str, attr);
745         if (ret != EVT_FAILED)
746                 goto modifier;
747
748         ret = parse_generic_hw_event(str, attr);
749         if (ret != EVT_FAILED)
750                 goto modifier;
751
752         ret = parse_breakpoint_event(str, attr);
753         if (ret != EVT_FAILED)
754                 goto modifier;
755
756         fprintf(stderr, "invalid or unsupported event: '%s'\n", *str);
757         fprintf(stderr, "Run 'perf list' for a list of valid events\n");
758         return EVT_FAILED;
759
760 modifier:
761         parse_event_modifier(str, attr);
762
763         return ret;
764 }
765
766 static int store_event_type(const char *orgname)
767 {
768         char filename[PATH_MAX], *c;
769         FILE *file;
770         int id, n;
771
772         sprintf(filename, "%s/", debugfs_path);
773         strncat(filename, orgname, strlen(orgname));
774         strcat(filename, "/id");
775
776         c = strchr(filename, ':');
777         if (c)
778                 *c = '/';
779
780         file = fopen(filename, "r");
781         if (!file)
782                 return 0;
783         n = fscanf(file, "%i", &id);
784         fclose(file);
785         if (n < 1) {
786                 pr_err("cannot store event ID\n");
787                 return -EINVAL;
788         }
789         return perf_header__push_event(id, orgname);
790 }
791
792 int parse_events(const struct option *opt __used, const char *str, int unset __used)
793 {
794         struct perf_event_attr attr;
795         enum event_result ret;
796
797         if (strchr(str, ':'))
798                 if (store_event_type(str) < 0)
799                         return -1;
800
801         for (;;) {
802                 if (nr_counters == MAX_COUNTERS)
803                         return -1;
804
805                 memset(&attr, 0, sizeof(attr));
806                 ret = parse_event_symbols(&str, &attr);
807                 if (ret == EVT_FAILED)
808                         return -1;
809
810                 if (!(*str == 0 || *str == ',' || isspace(*str)))
811                         return -1;
812
813                 if (ret != EVT_HANDLED_ALL) {
814                         attrs[nr_counters] = attr;
815                         nr_counters++;
816                 }
817
818                 if (*str == 0)
819                         break;
820                 if (*str == ',')
821                         ++str;
822                 while (isspace(*str))
823                         ++str;
824         }
825
826         return 0;
827 }
828
829 int parse_filter(const struct option *opt __used, const char *str,
830                  int unset __used)
831 {
832         int i = nr_counters - 1;
833         int len = strlen(str);
834
835         if (i < 0 || attrs[i].type != PERF_TYPE_TRACEPOINT) {
836                 fprintf(stderr,
837                         "-F option should follow a -e tracepoint option\n");
838                 return -1;
839         }
840
841         filters[i] = malloc(len + 1);
842         if (!filters[i]) {
843                 fprintf(stderr, "not enough memory to hold filter string\n");
844                 return -1;
845         }
846         strcpy(filters[i], str);
847
848         return 0;
849 }
850
851 static const char * const event_type_descriptors[] = {
852         "Hardware event",
853         "Software event",
854         "Tracepoint event",
855         "Hardware cache event",
856         "Raw hardware event descriptor",
857         "Hardware breakpoint",
858 };
859
860 /*
861  * Print the events from <debugfs_mount_point>/tracing/events
862  */
863
864 static void print_tracepoint_events(void)
865 {
866         DIR *sys_dir, *evt_dir;
867         struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
868         char evt_path[MAXPATHLEN];
869         char dir_path[MAXPATHLEN];
870
871         if (debugfs_valid_mountpoint(debugfs_path))
872                 return;
873
874         sys_dir = opendir(debugfs_path);
875         if (!sys_dir)
876                 return;
877
878         for_each_subsystem(sys_dir, sys_dirent, sys_next) {
879
880                 snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
881                          sys_dirent.d_name);
882                 evt_dir = opendir(dir_path);
883                 if (!evt_dir)
884                         continue;
885
886                 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
887                         snprintf(evt_path, MAXPATHLEN, "%s:%s",
888                                  sys_dirent.d_name, evt_dirent.d_name);
889                         printf("  %-42s [%s]\n", evt_path,
890                                 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
891                 }
892                 closedir(evt_dir);
893         }
894         closedir(sys_dir);
895 }
896
897 /*
898  * Print the help text for the event symbols:
899  */
900 void print_events(void)
901 {
902         struct event_symbol *syms = event_symbols;
903         unsigned int i, type, op, prev_type = -1;
904         char name[40];
905
906         printf("\n");
907         printf("List of pre-defined events (to be used in -e):\n");
908
909         for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
910                 type = syms->type;
911
912                 if (type != prev_type)
913                         printf("\n");
914
915                 if (strlen(syms->alias))
916                         sprintf(name, "%s OR %s", syms->symbol, syms->alias);
917                 else
918                         strcpy(name, syms->symbol);
919                 printf("  %-42s [%s]\n", name,
920                         event_type_descriptors[type]);
921
922                 prev_type = type;
923         }
924
925         printf("\n");
926         for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
927                 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
928                         /* skip invalid cache type */
929                         if (!is_cache_op_valid(type, op))
930                                 continue;
931
932                         for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
933                                 printf("  %-42s [%s]\n",
934                                         event_cache_name(type, op, i),
935                                         event_type_descriptors[PERF_TYPE_HW_CACHE]);
936                         }
937                 }
938         }
939
940         printf("\n");
941         printf("  %-42s [%s]\n",
942                 "rNNN", event_type_descriptors[PERF_TYPE_RAW]);
943         printf("\n");
944
945         printf("  %-42s [%s]\n",
946                         "mem:<addr>[:access]",
947                         event_type_descriptors[PERF_TYPE_BREAKPOINT]);
948         printf("\n");
949
950         print_tracepoint_events();
951
952         exit(129);
953 }