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