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