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