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