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