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