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