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