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