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