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