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