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