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