perf tools: Add front-end and back-end stalled cycles support
[linux-2.6.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(STALLED_CYCLES_FRONTEND),       "stalled-cycles-frontend",      "idle-cycles-frontend"  },
36   { CHW(STALLED_CYCLES_BACKEND),        "stalled-cycles-backend",       "idle-cycles-backend"   },
37   { CHW(INSTRUCTIONS),                  "instructions",                 ""                      },
38   { CHW(CACHE_REFERENCES),              "cache-references",             ""                      },
39   { CHW(CACHE_MISSES),                  "cache-misses",                 ""                      },
40   { CHW(BRANCH_INSTRUCTIONS),           "branch-instructions",          "branches"              },
41   { CHW(BRANCH_MISSES),                 "branch-misses",                ""                      },
42   { CHW(BUS_CYCLES),                    "bus-cycles",                   ""                      },
43
44   { CSW(CPU_CLOCK),                     "cpu-clock",                    ""                      },
45   { CSW(TASK_CLOCK),                    "task-clock",                   ""                      },
46   { CSW(PAGE_FAULTS),                   "page-faults",                  "faults"                },
47   { CSW(PAGE_FAULTS_MIN),               "minor-faults",                 ""                      },
48   { CSW(PAGE_FAULTS_MAJ),               "major-faults",                 ""                      },
49   { CSW(CONTEXT_SWITCHES),              "context-switches",             "cs"                    },
50   { CSW(CPU_MIGRATIONS),                "cpu-migrations",               "migrations"            },
51   { CSW(ALIGNMENT_FAULTS),              "alignment-faults",             ""                      },
52   { CSW(EMULATION_FAULTS),              "emulation-faults",             ""                      },
53 };
54
55 #define __PERF_EVENT_FIELD(config, name) \
56         ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
57
58 #define PERF_EVENT_RAW(config)          __PERF_EVENT_FIELD(config, RAW)
59 #define PERF_EVENT_CONFIG(config)       __PERF_EVENT_FIELD(config, CONFIG)
60 #define PERF_EVENT_TYPE(config)         __PERF_EVENT_FIELD(config, TYPE)
61 #define PERF_EVENT_ID(config)           __PERF_EVENT_FIELD(config, EVENT)
62
63 static const char *hw_event_names[] = {
64         "cycles",
65         "instructions",
66         "cache-references",
67         "cache-misses",
68         "branches",
69         "branch-misses",
70         "bus-cycles",
71         "stalled-cycles",
72 };
73
74 static const char *sw_event_names[] = {
75         "cpu-clock",
76         "task-clock",
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_type(int type)
270 {
271         switch (type) {
272         case PERF_TYPE_HARDWARE:
273                 return "hardware";
274
275         case PERF_TYPE_SOFTWARE:
276                 return "software";
277
278         case PERF_TYPE_TRACEPOINT:
279                 return "tracepoint";
280
281         case PERF_TYPE_HW_CACHE:
282                 return "hardware-cache";
283
284         default:
285                 break;
286         }
287
288         return "unknown";
289 }
290
291 const char *event_name(struct perf_evsel *evsel)
292 {
293         u64 config = evsel->attr.config;
294         int type = evsel->attr.type;
295
296         if (evsel->name)
297                 return evsel->name;
298
299         return __event_name(type, config);
300 }
301
302 const char *__event_name(int type, u64 config)
303 {
304         static char buf[32];
305
306         if (type == PERF_TYPE_RAW) {
307                 sprintf(buf, "raw 0x%" PRIx64, config);
308                 return buf;
309         }
310
311         switch (type) {
312         case PERF_TYPE_HARDWARE:
313                 if (config < PERF_COUNT_HW_MAX && hw_event_names[config])
314                         return hw_event_names[config];
315                 return "unknown-hardware";
316
317         case PERF_TYPE_HW_CACHE: {
318                 u8 cache_type, cache_op, cache_result;
319
320                 cache_type   = (config >>  0) & 0xff;
321                 if (cache_type > PERF_COUNT_HW_CACHE_MAX)
322                         return "unknown-ext-hardware-cache-type";
323
324                 cache_op     = (config >>  8) & 0xff;
325                 if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
326                         return "unknown-ext-hardware-cache-op";
327
328                 cache_result = (config >> 16) & 0xff;
329                 if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
330                         return "unknown-ext-hardware-cache-result";
331
332                 if (!is_cache_op_valid(cache_type, cache_op))
333                         return "invalid-cache";
334
335                 return event_cache_name(cache_type, cache_op, cache_result);
336         }
337
338         case PERF_TYPE_SOFTWARE:
339                 if (config < PERF_COUNT_SW_MAX && sw_event_names[config])
340                         return sw_event_names[config];
341                 return "unknown-software";
342
343         case PERF_TYPE_TRACEPOINT:
344                 return tracepoint_id_to_name(config);
345
346         default:
347                 break;
348         }
349
350         return "unknown";
351 }
352
353 static int parse_aliases(const char **str, const char *names[][MAX_ALIASES], int size)
354 {
355         int i, j;
356         int n, longest = -1;
357
358         for (i = 0; i < size; i++) {
359                 for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
360                         n = strlen(names[i][j]);
361                         if (n > longest && !strncasecmp(*str, names[i][j], n))
362                                 longest = n;
363                 }
364                 if (longest > 0) {
365                         *str += longest;
366                         return i;
367                 }
368         }
369
370         return -1;
371 }
372
373 static enum event_result
374 parse_generic_hw_event(const char **str, struct perf_event_attr *attr)
375 {
376         const char *s = *str;
377         int cache_type = -1, cache_op = -1, cache_result = -1;
378
379         cache_type = parse_aliases(&s, hw_cache, PERF_COUNT_HW_CACHE_MAX);
380         /*
381          * No fallback - if we cannot get a clear cache type
382          * then bail out:
383          */
384         if (cache_type == -1)
385                 return EVT_FAILED;
386
387         while ((cache_op == -1 || cache_result == -1) && *s == '-') {
388                 ++s;
389
390                 if (cache_op == -1) {
391                         cache_op = parse_aliases(&s, hw_cache_op,
392                                                 PERF_COUNT_HW_CACHE_OP_MAX);
393                         if (cache_op >= 0) {
394                                 if (!is_cache_op_valid(cache_type, cache_op))
395                                         return 0;
396                                 continue;
397                         }
398                 }
399
400                 if (cache_result == -1) {
401                         cache_result = parse_aliases(&s, hw_cache_result,
402                                                 PERF_COUNT_HW_CACHE_RESULT_MAX);
403                         if (cache_result >= 0)
404                                 continue;
405                 }
406
407                 /*
408                  * Can't parse this as a cache op or result, so back up
409                  * to the '-'.
410                  */
411                 --s;
412                 break;
413         }
414
415         /*
416          * Fall back to reads:
417          */
418         if (cache_op == -1)
419                 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
420
421         /*
422          * Fall back to accesses:
423          */
424         if (cache_result == -1)
425                 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
426
427         attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
428         attr->type = PERF_TYPE_HW_CACHE;
429
430         *str = s;
431         return EVT_HANDLED;
432 }
433
434 static enum event_result
435 parse_single_tracepoint_event(char *sys_name,
436                               const char *evt_name,
437                               unsigned int evt_length,
438                               struct perf_event_attr *attr,
439                               const char **strp)
440 {
441         char evt_path[MAXPATHLEN];
442         char id_buf[4];
443         u64 id;
444         int fd;
445
446         snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
447                  sys_name, evt_name);
448
449         fd = open(evt_path, O_RDONLY);
450         if (fd < 0)
451                 return EVT_FAILED;
452
453         if (read(fd, id_buf, sizeof(id_buf)) < 0) {
454                 close(fd);
455                 return EVT_FAILED;
456         }
457
458         close(fd);
459         id = atoll(id_buf);
460         attr->config = id;
461         attr->type = PERF_TYPE_TRACEPOINT;
462         *strp += strlen(sys_name) + evt_length + 1; /* + 1 for the ':' */
463
464         attr->sample_type |= PERF_SAMPLE_RAW;
465         attr->sample_type |= PERF_SAMPLE_TIME;
466         attr->sample_type |= PERF_SAMPLE_CPU;
467
468         attr->sample_period = 1;
469
470
471         return EVT_HANDLED;
472 }
473
474 /* sys + ':' + event + ':' + flags*/
475 #define MAX_EVOPT_LEN   (MAX_EVENT_LENGTH * 2 + 2 + 128)
476 static enum event_result
477 parse_multiple_tracepoint_event(const struct option *opt, char *sys_name,
478                                 const char *evt_exp, char *flags)
479 {
480         char evt_path[MAXPATHLEN];
481         struct dirent *evt_ent;
482         DIR *evt_dir;
483
484         snprintf(evt_path, MAXPATHLEN, "%s/%s", debugfs_path, sys_name);
485         evt_dir = opendir(evt_path);
486
487         if (!evt_dir) {
488                 perror("Can't open event dir");
489                 return EVT_FAILED;
490         }
491
492         while ((evt_ent = readdir(evt_dir))) {
493                 char event_opt[MAX_EVOPT_LEN + 1];
494                 int len;
495
496                 if (!strcmp(evt_ent->d_name, ".")
497                     || !strcmp(evt_ent->d_name, "..")
498                     || !strcmp(evt_ent->d_name, "enable")
499                     || !strcmp(evt_ent->d_name, "filter"))
500                         continue;
501
502                 if (!strglobmatch(evt_ent->d_name, evt_exp))
503                         continue;
504
505                 len = snprintf(event_opt, MAX_EVOPT_LEN, "%s:%s%s%s", sys_name,
506                                evt_ent->d_name, flags ? ":" : "",
507                                flags ?: "");
508                 if (len < 0)
509                         return EVT_FAILED;
510
511                 if (parse_events(opt, event_opt, 0))
512                         return EVT_FAILED;
513         }
514
515         return EVT_HANDLED_ALL;
516 }
517
518 static enum event_result
519 parse_tracepoint_event(const struct option *opt, const char **strp,
520                        struct perf_event_attr *attr)
521 {
522         const char *evt_name;
523         char *flags = NULL, *comma_loc;
524         char sys_name[MAX_EVENT_LENGTH];
525         unsigned int sys_length, evt_length;
526
527         if (debugfs_valid_mountpoint(debugfs_path))
528                 return 0;
529
530         evt_name = strchr(*strp, ':');
531         if (!evt_name)
532                 return EVT_FAILED;
533
534         sys_length = evt_name - *strp;
535         if (sys_length >= MAX_EVENT_LENGTH)
536                 return 0;
537
538         strncpy(sys_name, *strp, sys_length);
539         sys_name[sys_length] = '\0';
540         evt_name = evt_name + 1;
541
542         comma_loc = strchr(evt_name, ',');
543         if (comma_loc) {
544                 /* take the event name up to the comma */
545                 evt_name = strndup(evt_name, comma_loc - evt_name);
546         }
547         flags = strchr(evt_name, ':');
548         if (flags) {
549                 /* split it out: */
550                 evt_name = strndup(evt_name, flags - evt_name);
551                 flags++;
552         }
553
554         evt_length = strlen(evt_name);
555         if (evt_length >= MAX_EVENT_LENGTH)
556                 return EVT_FAILED;
557         if (strpbrk(evt_name, "*?")) {
558                 *strp += strlen(sys_name) + evt_length + 1; /* 1 == the ':' */
559                 return parse_multiple_tracepoint_event(opt, sys_name, evt_name,
560                                                        flags);
561         } else {
562                 return parse_single_tracepoint_event(sys_name, evt_name,
563                                                      evt_length, attr, strp);
564         }
565 }
566
567 static enum event_result
568 parse_breakpoint_type(const char *type, const char **strp,
569                       struct perf_event_attr *attr)
570 {
571         int i;
572
573         for (i = 0; i < 3; i++) {
574                 if (!type[i])
575                         break;
576
577                 switch (type[i]) {
578                 case 'r':
579                         attr->bp_type |= HW_BREAKPOINT_R;
580                         break;
581                 case 'w':
582                         attr->bp_type |= HW_BREAKPOINT_W;
583                         break;
584                 case 'x':
585                         attr->bp_type |= HW_BREAKPOINT_X;
586                         break;
587                 default:
588                         return EVT_FAILED;
589                 }
590         }
591         if (!attr->bp_type) /* Default */
592                 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
593
594         *strp = type + i;
595
596         return EVT_HANDLED;
597 }
598
599 static enum event_result
600 parse_breakpoint_event(const char **strp, struct perf_event_attr *attr)
601 {
602         const char *target;
603         const char *type;
604         char *endaddr;
605         u64 addr;
606         enum event_result err;
607
608         target = strchr(*strp, ':');
609         if (!target)
610                 return EVT_FAILED;
611
612         if (strncmp(*strp, "mem", target - *strp) != 0)
613                 return EVT_FAILED;
614
615         target++;
616
617         addr = strtoull(target, &endaddr, 0);
618         if (target == endaddr)
619                 return EVT_FAILED;
620
621         attr->bp_addr = addr;
622         *strp = endaddr;
623
624         type = strchr(target, ':');
625
626         /* If no type is defined, just rw as default */
627         if (!type) {
628                 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
629         } else {
630                 err = parse_breakpoint_type(++type, strp, attr);
631                 if (err == EVT_FAILED)
632                         return EVT_FAILED;
633         }
634
635         /*
636          * We should find a nice way to override the access length
637          * Provide some defaults for now
638          */
639         if (attr->bp_type == HW_BREAKPOINT_X)
640                 attr->bp_len = sizeof(long);
641         else
642                 attr->bp_len = HW_BREAKPOINT_LEN_4;
643
644         attr->type = PERF_TYPE_BREAKPOINT;
645
646         return EVT_HANDLED;
647 }
648
649 static int check_events(const char *str, unsigned int i)
650 {
651         int n;
652
653         n = strlen(event_symbols[i].symbol);
654         if (!strncasecmp(str, event_symbols[i].symbol, n))
655                 return n;
656
657         n = strlen(event_symbols[i].alias);
658         if (n) {
659                 if (!strncasecmp(str, event_symbols[i].alias, n))
660                         return n;
661         }
662
663         return 0;
664 }
665
666 static enum event_result
667 parse_symbolic_event(const char **strp, struct perf_event_attr *attr)
668 {
669         const char *str = *strp;
670         unsigned int i;
671         int n;
672
673         for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
674                 n = check_events(str, i);
675                 if (n > 0) {
676                         attr->type = event_symbols[i].type;
677                         attr->config = event_symbols[i].config;
678                         *strp = str + n;
679                         return EVT_HANDLED;
680                 }
681         }
682         return EVT_FAILED;
683 }
684
685 static enum event_result
686 parse_raw_event(const char **strp, struct perf_event_attr *attr)
687 {
688         const char *str = *strp;
689         u64 config;
690         int n;
691
692         if (*str != 'r')
693                 return EVT_FAILED;
694         n = hex2u64(str + 1, &config);
695         if (n > 0) {
696                 *strp = str + n + 1;
697                 attr->type = PERF_TYPE_RAW;
698                 attr->config = config;
699                 return EVT_HANDLED;
700         }
701         return EVT_FAILED;
702 }
703
704 static enum event_result
705 parse_numeric_event(const char **strp, struct perf_event_attr *attr)
706 {
707         const char *str = *strp;
708         char *endp;
709         unsigned long type;
710         u64 config;
711
712         type = strtoul(str, &endp, 0);
713         if (endp > str && type < PERF_TYPE_MAX && *endp == ':') {
714                 str = endp + 1;
715                 config = strtoul(str, &endp, 0);
716                 if (endp > str) {
717                         attr->type = type;
718                         attr->config = config;
719                         *strp = endp;
720                         return EVT_HANDLED;
721                 }
722         }
723         return EVT_FAILED;
724 }
725
726 static int
727 parse_event_modifier(const char **strp, struct perf_event_attr *attr)
728 {
729         const char *str = *strp;
730         int exclude = 0;
731         int eu = 0, ek = 0, eh = 0, precise = 0;
732
733         if (!*str)
734                 return 0;
735
736         if (*str++ != ':')
737                 return -1;
738
739         while (*str) {
740                 if (*str == 'u') {
741                         if (!exclude)
742                                 exclude = eu = ek = eh = 1;
743                         eu = 0;
744                 } else if (*str == 'k') {
745                         if (!exclude)
746                                 exclude = eu = ek = eh = 1;
747                         ek = 0;
748                 } else if (*str == 'h') {
749                         if (!exclude)
750                                 exclude = eu = ek = eh = 1;
751                         eh = 0;
752                 } else if (*str == 'p') {
753                         precise++;
754                 } else
755                         break;
756
757                 ++str;
758         }
759         if (str < *strp + 2)
760                 return -1;
761
762         *strp = str;
763
764         attr->exclude_user   = eu;
765         attr->exclude_kernel = ek;
766         attr->exclude_hv     = eh;
767         attr->precise_ip     = precise;
768
769         return 0;
770 }
771
772 /*
773  * Each event can have multiple symbolic names.
774  * Symbolic names are (almost) exactly matched.
775  */
776 static enum event_result
777 parse_event_symbols(const struct option *opt, const char **str,
778                     struct perf_event_attr *attr)
779 {
780         enum event_result ret;
781
782         ret = parse_tracepoint_event(opt, str, attr);
783         if (ret != EVT_FAILED)
784                 goto modifier;
785
786         ret = parse_raw_event(str, attr);
787         if (ret != EVT_FAILED)
788                 goto modifier;
789
790         ret = parse_numeric_event(str, attr);
791         if (ret != EVT_FAILED)
792                 goto modifier;
793
794         ret = parse_symbolic_event(str, attr);
795         if (ret != EVT_FAILED)
796                 goto modifier;
797
798         ret = parse_generic_hw_event(str, attr);
799         if (ret != EVT_FAILED)
800                 goto modifier;
801
802         ret = parse_breakpoint_event(str, attr);
803         if (ret != EVT_FAILED)
804                 goto modifier;
805
806         fprintf(stderr, "invalid or unsupported event: '%s'\n", *str);
807         fprintf(stderr, "Run 'perf list' for a list of valid events\n");
808         return EVT_FAILED;
809
810 modifier:
811         if (parse_event_modifier(str, attr) < 0) {
812                 fprintf(stderr, "invalid event modifier: '%s'\n", *str);
813                 fprintf(stderr, "Run 'perf list' for a list of valid events and modifiers\n");
814
815                 return EVT_FAILED;
816         }
817
818         return ret;
819 }
820
821 int parse_events(const struct option *opt, const char *str, int unset __used)
822 {
823         struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
824         struct perf_event_attr attr;
825         enum event_result ret;
826         const char *ostr;
827
828         for (;;) {
829                 ostr = str;
830                 memset(&attr, 0, sizeof(attr));
831                 ret = parse_event_symbols(opt, &str, &attr);
832                 if (ret == EVT_FAILED)
833                         return -1;
834
835                 if (!(*str == 0 || *str == ',' || isspace(*str)))
836                         return -1;
837
838                 if (ret != EVT_HANDLED_ALL) {
839                         struct perf_evsel *evsel;
840                         evsel = perf_evsel__new(&attr, evlist->nr_entries);
841                         if (evsel == NULL)
842                                 return -1;
843                         perf_evlist__add(evlist, evsel);
844
845                         evsel->name = calloc(str - ostr + 1, 1);
846                         if (!evsel->name)
847                                 return -1;
848                         strncpy(evsel->name, ostr, str - ostr);
849                 }
850
851                 if (*str == 0)
852                         break;
853                 if (*str == ',')
854                         ++str;
855                 while (isspace(*str))
856                         ++str;
857         }
858
859         return 0;
860 }
861
862 int parse_filter(const struct option *opt, const char *str,
863                  int unset __used)
864 {
865         struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
866         struct perf_evsel *last = NULL;
867
868         if (evlist->nr_entries > 0)
869                 last = list_entry(evlist->entries.prev, struct perf_evsel, node);
870
871         if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
872                 fprintf(stderr,
873                         "-F option should follow a -e tracepoint option\n");
874                 return -1;
875         }
876
877         last->filter = strdup(str);
878         if (last->filter == NULL) {
879                 fprintf(stderr, "not enough memory to hold filter string\n");
880                 return -1;
881         }
882
883         return 0;
884 }
885
886 static const char * const event_type_descriptors[] = {
887         "Hardware event",
888         "Software event",
889         "Tracepoint event",
890         "Hardware cache event",
891         "Raw hardware event descriptor",
892         "Hardware breakpoint",
893 };
894
895 /*
896  * Print the events from <debugfs_mount_point>/tracing/events
897  */
898
899 void print_tracepoint_events(const char *subsys_glob, const char *event_glob)
900 {
901         DIR *sys_dir, *evt_dir;
902         struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
903         char evt_path[MAXPATHLEN];
904         char dir_path[MAXPATHLEN];
905
906         if (debugfs_valid_mountpoint(debugfs_path))
907                 return;
908
909         sys_dir = opendir(debugfs_path);
910         if (!sys_dir)
911                 return;
912
913         for_each_subsystem(sys_dir, sys_dirent, sys_next) {
914                 if (subsys_glob != NULL && 
915                     !strglobmatch(sys_dirent.d_name, subsys_glob))
916                         continue;
917
918                 snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
919                          sys_dirent.d_name);
920                 evt_dir = opendir(dir_path);
921                 if (!evt_dir)
922                         continue;
923
924                 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
925                         if (event_glob != NULL && 
926                             !strglobmatch(evt_dirent.d_name, event_glob))
927                                 continue;
928
929                         snprintf(evt_path, MAXPATHLEN, "%s:%s",
930                                  sys_dirent.d_name, evt_dirent.d_name);
931                         printf("  %-42s [%s]\n", evt_path,
932                                 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
933                 }
934                 closedir(evt_dir);
935         }
936         closedir(sys_dir);
937 }
938
939 /*
940  * Check whether event is in <debugfs_mount_point>/tracing/events
941  */
942
943 int is_valid_tracepoint(const char *event_string)
944 {
945         DIR *sys_dir, *evt_dir;
946         struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
947         char evt_path[MAXPATHLEN];
948         char dir_path[MAXPATHLEN];
949
950         if (debugfs_valid_mountpoint(debugfs_path))
951                 return 0;
952
953         sys_dir = opendir(debugfs_path);
954         if (!sys_dir)
955                 return 0;
956
957         for_each_subsystem(sys_dir, sys_dirent, sys_next) {
958
959                 snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
960                          sys_dirent.d_name);
961                 evt_dir = opendir(dir_path);
962                 if (!evt_dir)
963                         continue;
964
965                 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
966                         snprintf(evt_path, MAXPATHLEN, "%s:%s",
967                                  sys_dirent.d_name, evt_dirent.d_name);
968                         if (!strcmp(evt_path, event_string)) {
969                                 closedir(evt_dir);
970                                 closedir(sys_dir);
971                                 return 1;
972                         }
973                 }
974                 closedir(evt_dir);
975         }
976         closedir(sys_dir);
977         return 0;
978 }
979
980 void print_events_type(u8 type)
981 {
982         struct event_symbol *syms = event_symbols;
983         unsigned int i;
984         char name[64];
985
986         for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
987                 if (type != syms->type)
988                         continue;
989
990                 if (strlen(syms->alias))
991                         snprintf(name, sizeof(name),  "%s OR %s",
992                                  syms->symbol, syms->alias);
993                 else
994                         snprintf(name, sizeof(name), "%s", syms->symbol);
995
996                 printf("  %-42s [%s]\n", name,
997                         event_type_descriptors[type]);
998         }
999 }
1000
1001 int print_hwcache_events(const char *event_glob)
1002 {
1003         unsigned int type, op, i, printed = 0;
1004
1005         for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1006                 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1007                         /* skip invalid cache type */
1008                         if (!is_cache_op_valid(type, op))
1009                                 continue;
1010
1011                         for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
1012                                 char *name = event_cache_name(type, op, i);
1013
1014                                 if (event_glob != NULL && 
1015                                     !strglobmatch(name, event_glob))
1016                                         continue;
1017
1018                                 printf("  %-42s [%s]\n", name,
1019                                         event_type_descriptors[PERF_TYPE_HW_CACHE]);
1020                                 ++printed;
1021                         }
1022                 }
1023         }
1024
1025         return printed;
1026 }
1027
1028 /*
1029  * Print the help text for the event symbols:
1030  */
1031 void print_events(const char *event_glob)
1032 {
1033         struct event_symbol *syms = event_symbols;
1034         unsigned int i, type, prev_type = -1, printed = 0, ntypes_printed = 0;
1035         char name[40];
1036
1037         printf("\n");
1038         printf("List of pre-defined events (to be used in -e):\n");
1039
1040         for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
1041                 type = syms->type;
1042
1043                 if (type != prev_type && printed) {
1044                         printf("\n");
1045                         printed = 0;
1046                         ntypes_printed++;
1047                 }
1048
1049                 if (event_glob != NULL && 
1050                     !(strglobmatch(syms->symbol, event_glob) ||
1051                       (syms->alias && strglobmatch(syms->alias, event_glob))))
1052                         continue;
1053
1054                 if (strlen(syms->alias))
1055                         sprintf(name, "%s OR %s", syms->symbol, syms->alias);
1056                 else
1057                         strcpy(name, syms->symbol);
1058                 printf("  %-42s [%s]\n", name,
1059                         event_type_descriptors[type]);
1060
1061                 prev_type = type;
1062                 ++printed;
1063         }
1064
1065         if (ntypes_printed) {
1066                 printed = 0;
1067                 printf("\n");
1068         }
1069         print_hwcache_events(event_glob);
1070
1071         if (event_glob != NULL)
1072                 return;
1073
1074         printf("\n");
1075         printf("  %-42s [%s]\n",
1076                 "rNNN (see 'perf list --help' on how to encode it)",
1077                event_type_descriptors[PERF_TYPE_RAW]);
1078         printf("\n");
1079
1080         printf("  %-42s [%s]\n",
1081                         "mem:<addr>[:access]",
1082                         event_type_descriptors[PERF_TYPE_BREAKPOINT]);
1083         printf("\n");
1084
1085         print_tracepoint_events(NULL, NULL);
1086
1087         exit(129);
1088 }