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