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