]>
Commit | Line | Data |
---|---|---|
d2709c7c | 1 | #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" | |
50e200f0 | 9 | #include "linux/string.h" |
5aab621b | 10 | #include "symbol.h" |
5beeded1 | 11 | #include "cache.h" |
8755a8f2 | 12 | #include "header.h" |
85c66be1 | 13 | #include <lk/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 | 21 | struct event_symbol { |
83a0944f IM |
22 | const char *symbol; |
23 | const char *alias; | |
8ad8db37 IM |
24 | }; |
25 | ||
82ba1f2f JO |
26 | #ifdef PARSER_DEBUG |
27 | extern int parse_events_debug; | |
28 | #endif | |
ac20de6f | 29 | int parse_events_parse(void *data, void *scanner); |
bcd3279f | 30 | |
1dc12760 JO |
31 | static struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = { |
32 | [PERF_COUNT_HW_CPU_CYCLES] = { | |
33 | .symbol = "cpu-cycles", | |
34 | .alias = "cycles", | |
35 | }, | |
36 | [PERF_COUNT_HW_INSTRUCTIONS] = { | |
37 | .symbol = "instructions", | |
38 | .alias = "", | |
39 | }, | |
40 | [PERF_COUNT_HW_CACHE_REFERENCES] = { | |
41 | .symbol = "cache-references", | |
42 | .alias = "", | |
43 | }, | |
44 | [PERF_COUNT_HW_CACHE_MISSES] = { | |
45 | .symbol = "cache-misses", | |
46 | .alias = "", | |
47 | }, | |
48 | [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { | |
49 | .symbol = "branch-instructions", | |
50 | .alias = "branches", | |
51 | }, | |
52 | [PERF_COUNT_HW_BRANCH_MISSES] = { | |
53 | .symbol = "branch-misses", | |
54 | .alias = "", | |
55 | }, | |
56 | [PERF_COUNT_HW_BUS_CYCLES] = { | |
57 | .symbol = "bus-cycles", | |
58 | .alias = "", | |
59 | }, | |
60 | [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = { | |
61 | .symbol = "stalled-cycles-frontend", | |
62 | .alias = "idle-cycles-frontend", | |
63 | }, | |
64 | [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = { | |
65 | .symbol = "stalled-cycles-backend", | |
66 | .alias = "idle-cycles-backend", | |
67 | }, | |
68 | [PERF_COUNT_HW_REF_CPU_CYCLES] = { | |
69 | .symbol = "ref-cycles", | |
70 | .alias = "", | |
71 | }, | |
72 | }; | |
73 | ||
74 | static struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = { | |
75 | [PERF_COUNT_SW_CPU_CLOCK] = { | |
76 | .symbol = "cpu-clock", | |
77 | .alias = "", | |
78 | }, | |
79 | [PERF_COUNT_SW_TASK_CLOCK] = { | |
80 | .symbol = "task-clock", | |
81 | .alias = "", | |
82 | }, | |
83 | [PERF_COUNT_SW_PAGE_FAULTS] = { | |
84 | .symbol = "page-faults", | |
85 | .alias = "faults", | |
86 | }, | |
87 | [PERF_COUNT_SW_CONTEXT_SWITCHES] = { | |
88 | .symbol = "context-switches", | |
89 | .alias = "cs", | |
90 | }, | |
91 | [PERF_COUNT_SW_CPU_MIGRATIONS] = { | |
92 | .symbol = "cpu-migrations", | |
93 | .alias = "migrations", | |
94 | }, | |
95 | [PERF_COUNT_SW_PAGE_FAULTS_MIN] = { | |
96 | .symbol = "minor-faults", | |
97 | .alias = "", | |
98 | }, | |
99 | [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = { | |
100 | .symbol = "major-faults", | |
101 | .alias = "", | |
102 | }, | |
103 | [PERF_COUNT_SW_ALIGNMENT_FAULTS] = { | |
104 | .symbol = "alignment-faults", | |
105 | .alias = "", | |
106 | }, | |
107 | [PERF_COUNT_SW_EMULATION_FAULTS] = { | |
108 | .symbol = "emulation-faults", | |
109 | .alias = "", | |
110 | }, | |
d22d1a2a AH |
111 | [PERF_COUNT_SW_DUMMY] = { |
112 | .symbol = "dummy", | |
113 | .alias = "", | |
114 | }, | |
8ad8db37 IM |
115 | }; |
116 | ||
cdd6c482 IM |
117 | #define __PERF_EVENT_FIELD(config, name) \ |
118 | ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT) | |
5242519b | 119 | |
1fc570ad | 120 | #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW) |
cdd6c482 | 121 | #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG) |
1fc570ad | 122 | #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE) |
cdd6c482 | 123 | #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT) |
5242519b | 124 | |
6b58e7f1 | 125 | #define for_each_subsystem(sys_dir, sys_dirent, sys_next) \ |
f6bdafef | 126 | while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \ |
6b58e7f1 | 127 | if (sys_dirent.d_type == DT_DIR && \ |
f6bdafef JB |
128 | (strcmp(sys_dirent.d_name, ".")) && \ |
129 | (strcmp(sys_dirent.d_name, ".."))) | |
130 | ||
ae07b63f PZ |
131 | static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir) |
132 | { | |
133 | char evt_path[MAXPATHLEN]; | |
134 | int fd; | |
135 | ||
ebf294bf | 136 | snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path, |
ae07b63f PZ |
137 | sys_dir->d_name, evt_dir->d_name); |
138 | fd = open(evt_path, O_RDONLY); | |
139 | if (fd < 0) | |
140 | return -EINVAL; | |
141 | close(fd); | |
142 | ||
143 | return 0; | |
144 | } | |
145 | ||
6b58e7f1 | 146 | #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \ |
f6bdafef | 147 | while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \ |
6b58e7f1 | 148 | if (evt_dirent.d_type == DT_DIR && \ |
f6bdafef | 149 | (strcmp(evt_dirent.d_name, ".")) && \ |
ae07b63f PZ |
150 | (strcmp(evt_dirent.d_name, "..")) && \ |
151 | (!tp_event_has_id(&sys_dirent, &evt_dirent))) | |
f6bdafef | 152 | |
270bbbe8 | 153 | #define MAX_EVENT_LENGTH 512 |
f6bdafef | 154 | |
f6bdafef | 155 | |
1ef2ed10 | 156 | struct tracepoint_path *tracepoint_id_to_path(u64 config) |
f6bdafef | 157 | { |
1ef2ed10 | 158 | struct tracepoint_path *path = NULL; |
f6bdafef JB |
159 | DIR *sys_dir, *evt_dir; |
160 | struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; | |
8aa8a7c8 | 161 | char id_buf[24]; |
725b1368 | 162 | int fd; |
f6bdafef JB |
163 | u64 id; |
164 | char evt_path[MAXPATHLEN]; | |
725b1368 | 165 | char dir_path[MAXPATHLEN]; |
f6bdafef | 166 | |
ebf294bf | 167 | if (debugfs_valid_mountpoint(tracing_events_path)) |
1ef2ed10 | 168 | return NULL; |
f6bdafef | 169 | |
ebf294bf | 170 | sys_dir = opendir(tracing_events_path); |
f6bdafef | 171 | if (!sys_dir) |
725b1368 | 172 | return NULL; |
6b58e7f1 UD |
173 | |
174 | for_each_subsystem(sys_dir, sys_dirent, sys_next) { | |
725b1368 | 175 | |
ebf294bf | 176 | snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, |
725b1368 ED |
177 | sys_dirent.d_name); |
178 | evt_dir = opendir(dir_path); | |
179 | if (!evt_dir) | |
6b58e7f1 | 180 | continue; |
725b1368 | 181 | |
6b58e7f1 | 182 | for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) { |
725b1368 ED |
183 | |
184 | snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, | |
f6bdafef | 185 | evt_dirent.d_name); |
725b1368 | 186 | fd = open(evt_path, O_RDONLY); |
f6bdafef JB |
187 | if (fd < 0) |
188 | continue; | |
189 | if (read(fd, id_buf, sizeof(id_buf)) < 0) { | |
190 | close(fd); | |
191 | continue; | |
192 | } | |
193 | close(fd); | |
194 | id = atoll(id_buf); | |
195 | if (id == config) { | |
196 | closedir(evt_dir); | |
197 | closedir(sys_dir); | |
59b4caeb | 198 | path = zalloc(sizeof(*path)); |
1ef2ed10 FW |
199 | path->system = malloc(MAX_EVENT_LENGTH); |
200 | if (!path->system) { | |
201 | free(path); | |
202 | return NULL; | |
203 | } | |
204 | path->name = malloc(MAX_EVENT_LENGTH); | |
205 | if (!path->name) { | |
206 | free(path->system); | |
207 | free(path); | |
208 | return NULL; | |
209 | } | |
210 | strncpy(path->system, sys_dirent.d_name, | |
211 | MAX_EVENT_LENGTH); | |
212 | strncpy(path->name, evt_dirent.d_name, | |
213 | MAX_EVENT_LENGTH); | |
214 | return path; | |
f6bdafef JB |
215 | } |
216 | } | |
217 | closedir(evt_dir); | |
218 | } | |
219 | ||
f6bdafef | 220 | closedir(sys_dir); |
1ef2ed10 FW |
221 | return NULL; |
222 | } | |
223 | ||
e7c93f09 NK |
224 | struct tracepoint_path *tracepoint_name_to_path(const char *name) |
225 | { | |
226 | struct tracepoint_path *path = zalloc(sizeof(*path)); | |
227 | char *str = strchr(name, ':'); | |
228 | ||
229 | if (path == NULL || str == NULL) { | |
230 | free(path); | |
231 | return NULL; | |
232 | } | |
233 | ||
234 | path->system = strndup(name, str - name); | |
235 | path->name = strdup(str+1); | |
236 | ||
237 | if (path->system == NULL || path->name == NULL) { | |
238 | free(path->system); | |
239 | free(path->name); | |
240 | free(path); | |
241 | path = NULL; | |
242 | } | |
243 | ||
244 | return path; | |
245 | } | |
246 | ||
1424dc96 DA |
247 | const char *event_type(int type) |
248 | { | |
249 | switch (type) { | |
250 | case PERF_TYPE_HARDWARE: | |
251 | return "hardware"; | |
252 | ||
253 | case PERF_TYPE_SOFTWARE: | |
254 | return "software"; | |
255 | ||
256 | case PERF_TYPE_TRACEPOINT: | |
257 | return "tracepoint"; | |
258 | ||
259 | case PERF_TYPE_HW_CACHE: | |
260 | return "hardware-cache"; | |
261 | ||
262 | default: | |
263 | break; | |
264 | } | |
265 | ||
266 | return "unknown"; | |
267 | } | |
268 | ||
7ae92e74 YZ |
269 | |
270 | ||
c5cd8ac0 | 271 | static int __add_event(struct list_head *list, int *idx, |
7ae92e74 YZ |
272 | struct perf_event_attr *attr, |
273 | char *name, struct cpu_map *cpus) | |
89812fc8 JO |
274 | { |
275 | struct perf_evsel *evsel; | |
276 | ||
277 | event_attr_init(attr); | |
278 | ||
279 | evsel = perf_evsel__new(attr, (*idx)++); | |
c5cd8ac0 | 280 | if (!evsel) |
89812fc8 | 281 | return -ENOMEM; |
89812fc8 | 282 | |
7ae92e74 | 283 | evsel->cpus = cpus; |
9db1763c ACM |
284 | if (name) |
285 | evsel->name = strdup(name); | |
b847cbdc | 286 | list_add_tail(&evsel->node, list); |
89812fc8 JO |
287 | return 0; |
288 | } | |
289 | ||
c5cd8ac0 | 290 | static int add_event(struct list_head *list, int *idx, |
7ae92e74 YZ |
291 | struct perf_event_attr *attr, char *name) |
292 | { | |
c5cd8ac0 | 293 | return __add_event(list, idx, attr, name, NULL); |
7ae92e74 YZ |
294 | } |
295 | ||
0b668bc9 | 296 | static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size) |
8326f44d IM |
297 | { |
298 | int i, j; | |
61c45981 | 299 | int n, longest = -1; |
8326f44d IM |
300 | |
301 | for (i = 0; i < size; i++) { | |
0b668bc9 | 302 | for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) { |
61c45981 | 303 | n = strlen(names[i][j]); |
89812fc8 | 304 | if (n > longest && !strncasecmp(str, names[i][j], n)) |
61c45981 PM |
305 | longest = n; |
306 | } | |
89812fc8 | 307 | if (longest > 0) |
61c45981 | 308 | return i; |
8326f44d IM |
309 | } |
310 | ||
8953645f | 311 | return -1; |
8326f44d IM |
312 | } |
313 | ||
c5cd8ac0 | 314 | int parse_events_add_cache(struct list_head *list, int *idx, |
89812fc8 | 315 | char *type, char *op_result1, char *op_result2) |
8326f44d | 316 | { |
89812fc8 JO |
317 | struct perf_event_attr attr; |
318 | char name[MAX_NAME_LEN]; | |
61c45981 | 319 | int cache_type = -1, cache_op = -1, cache_result = -1; |
89812fc8 JO |
320 | char *op_result[2] = { op_result1, op_result2 }; |
321 | int i, n; | |
8326f44d | 322 | |
8326f44d IM |
323 | /* |
324 | * No fallback - if we cannot get a clear cache type | |
325 | * then bail out: | |
326 | */ | |
0b668bc9 | 327 | cache_type = parse_aliases(type, perf_evsel__hw_cache, |
89812fc8 | 328 | PERF_COUNT_HW_CACHE_MAX); |
8326f44d | 329 | if (cache_type == -1) |
89812fc8 JO |
330 | return -EINVAL; |
331 | ||
332 | n = snprintf(name, MAX_NAME_LEN, "%s", type); | |
61c45981 | 333 | |
89812fc8 JO |
334 | for (i = 0; (i < 2) && (op_result[i]); i++) { |
335 | char *str = op_result[i]; | |
336 | ||
275ef387 | 337 | n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str); |
61c45981 PM |
338 | |
339 | if (cache_op == -1) { | |
0b668bc9 | 340 | cache_op = parse_aliases(str, perf_evsel__hw_cache_op, |
89812fc8 | 341 | PERF_COUNT_HW_CACHE_OP_MAX); |
61c45981 | 342 | if (cache_op >= 0) { |
0b668bc9 | 343 | if (!perf_evsel__is_cache_op_valid(cache_type, cache_op)) |
89812fc8 | 344 | return -EINVAL; |
61c45981 PM |
345 | continue; |
346 | } | |
347 | } | |
348 | ||
349 | if (cache_result == -1) { | |
0b668bc9 ACM |
350 | cache_result = parse_aliases(str, perf_evsel__hw_cache_result, |
351 | PERF_COUNT_HW_CACHE_RESULT_MAX); | |
61c45981 PM |
352 | if (cache_result >= 0) |
353 | continue; | |
354 | } | |
61c45981 | 355 | } |
8326f44d | 356 | |
8326f44d IM |
357 | /* |
358 | * Fall back to reads: | |
359 | */ | |
8953645f IM |
360 | if (cache_op == -1) |
361 | cache_op = PERF_COUNT_HW_CACHE_OP_READ; | |
8326f44d | 362 | |
8326f44d IM |
363 | /* |
364 | * Fall back to accesses: | |
365 | */ | |
366 | if (cache_result == -1) | |
367 | cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS; | |
368 | ||
89812fc8 JO |
369 | memset(&attr, 0, sizeof(attr)); |
370 | attr.config = cache_type | (cache_op << 8) | (cache_result << 16); | |
371 | attr.type = PERF_TYPE_HW_CACHE; | |
372 | return add_event(list, idx, &attr, name); | |
bcd3279f FW |
373 | } |
374 | ||
c5cd8ac0 | 375 | static int add_tracepoint(struct list_head *list, int *idx, |
89812fc8 | 376 | char *sys_name, char *evt_name) |
bcd3279f | 377 | { |
82fe1c29 | 378 | struct perf_evsel *evsel; |
bcd3279f | 379 | |
82fe1c29 | 380 | evsel = perf_evsel__newtp(sys_name, evt_name, (*idx)++); |
c5cd8ac0 | 381 | if (!evsel) |
82fe1c29 | 382 | return -ENOMEM; |
bcd3279f | 383 | |
82fe1c29 | 384 | list_add_tail(&evsel->node, list); |
c5cd8ac0 | 385 | |
82fe1c29 | 386 | return 0; |
8326f44d IM |
387 | } |
388 | ||
c5cd8ac0 | 389 | static int add_tracepoint_multi_event(struct list_head *list, int *idx, |
f35488f9 | 390 | char *sys_name, char *evt_name) |
bcd3279f FW |
391 | { |
392 | char evt_path[MAXPATHLEN]; | |
393 | struct dirent *evt_ent; | |
394 | DIR *evt_dir; | |
89812fc8 | 395 | int ret = 0; |
bcd3279f | 396 | |
ebf294bf | 397 | snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name); |
bcd3279f | 398 | evt_dir = opendir(evt_path); |
bcd3279f FW |
399 | if (!evt_dir) { |
400 | perror("Can't open event dir"); | |
89812fc8 | 401 | return -1; |
bcd3279f FW |
402 | } |
403 | ||
89812fc8 | 404 | while (!ret && (evt_ent = readdir(evt_dir))) { |
bcd3279f FW |
405 | if (!strcmp(evt_ent->d_name, ".") |
406 | || !strcmp(evt_ent->d_name, "..") | |
407 | || !strcmp(evt_ent->d_name, "enable") | |
408 | || !strcmp(evt_ent->d_name, "filter")) | |
409 | continue; | |
410 | ||
89812fc8 | 411 | if (!strglobmatch(evt_ent->d_name, evt_name)) |
fb1d2edf MH |
412 | continue; |
413 | ||
89812fc8 | 414 | ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name); |
bcd3279f FW |
415 | } |
416 | ||
0bd3f084 | 417 | closedir(evt_dir); |
89812fc8 | 418 | return ret; |
bcd3279f FW |
419 | } |
420 | ||
c5cd8ac0 | 421 | static int add_tracepoint_event(struct list_head *list, int *idx, |
f35488f9 JO |
422 | char *sys_name, char *evt_name) |
423 | { | |
424 | return strpbrk(evt_name, "*?") ? | |
425 | add_tracepoint_multi_event(list, idx, sys_name, evt_name) : | |
426 | add_tracepoint(list, idx, sys_name, evt_name); | |
427 | } | |
428 | ||
c5cd8ac0 | 429 | static int add_tracepoint_multi_sys(struct list_head *list, int *idx, |
f35488f9 JO |
430 | char *sys_name, char *evt_name) |
431 | { | |
432 | struct dirent *events_ent; | |
433 | DIR *events_dir; | |
434 | int ret = 0; | |
435 | ||
436 | events_dir = opendir(tracing_events_path); | |
437 | if (!events_dir) { | |
438 | perror("Can't open event dir"); | |
439 | return -1; | |
440 | } | |
441 | ||
442 | while (!ret && (events_ent = readdir(events_dir))) { | |
443 | if (!strcmp(events_ent->d_name, ".") | |
444 | || !strcmp(events_ent->d_name, "..") | |
445 | || !strcmp(events_ent->d_name, "enable") | |
446 | || !strcmp(events_ent->d_name, "header_event") | |
447 | || !strcmp(events_ent->d_name, "header_page")) | |
448 | continue; | |
449 | ||
450 | if (!strglobmatch(events_ent->d_name, sys_name)) | |
451 | continue; | |
452 | ||
453 | ret = add_tracepoint_event(list, idx, events_ent->d_name, | |
454 | evt_name); | |
455 | } | |
456 | ||
457 | closedir(events_dir); | |
458 | return ret; | |
459 | } | |
460 | ||
c5cd8ac0 | 461 | int parse_events_add_tracepoint(struct list_head *list, int *idx, |
89812fc8 | 462 | char *sys, char *event) |
f6bdafef | 463 | { |
89812fc8 | 464 | int ret; |
f6bdafef | 465 | |
89812fc8 JO |
466 | ret = debugfs_valid_mountpoint(tracing_events_path); |
467 | if (ret) | |
468 | return ret; | |
f6bdafef | 469 | |
f35488f9 JO |
470 | if (strpbrk(sys, "*?")) |
471 | return add_tracepoint_multi_sys(list, idx, sys, event); | |
472 | else | |
473 | return add_tracepoint_event(list, idx, sys, event); | |
f6bdafef JB |
474 | } |
475 | ||
89812fc8 JO |
476 | static int |
477 | parse_breakpoint_type(const char *type, struct perf_event_attr *attr) | |
1b290d67 FW |
478 | { |
479 | int i; | |
480 | ||
481 | for (i = 0; i < 3; i++) { | |
89812fc8 | 482 | if (!type || !type[i]) |
1b290d67 FW |
483 | break; |
484 | ||
7582732f JO |
485 | #define CHECK_SET_TYPE(bit) \ |
486 | do { \ | |
487 | if (attr->bp_type & bit) \ | |
488 | return -EINVAL; \ | |
489 | else \ | |
490 | attr->bp_type |= bit; \ | |
491 | } while (0) | |
492 | ||
1b290d67 FW |
493 | switch (type[i]) { |
494 | case 'r': | |
7582732f | 495 | CHECK_SET_TYPE(HW_BREAKPOINT_R); |
1b290d67 FW |
496 | break; |
497 | case 'w': | |
7582732f | 498 | CHECK_SET_TYPE(HW_BREAKPOINT_W); |
1b290d67 FW |
499 | break; |
500 | case 'x': | |
7582732f | 501 | CHECK_SET_TYPE(HW_BREAKPOINT_X); |
1b290d67 FW |
502 | break; |
503 | default: | |
89812fc8 | 504 | return -EINVAL; |
1b290d67 FW |
505 | } |
506 | } | |
89812fc8 | 507 | |
7582732f JO |
508 | #undef CHECK_SET_TYPE |
509 | ||
1b290d67 FW |
510 | if (!attr->bp_type) /* Default */ |
511 | attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W; | |
512 | ||
89812fc8 | 513 | return 0; |
1b290d67 FW |
514 | } |
515 | ||
c5cd8ac0 | 516 | int parse_events_add_breakpoint(struct list_head *list, int *idx, |
89812fc8 | 517 | void *ptr, char *type) |
1b290d67 | 518 | { |
89812fc8 | 519 | struct perf_event_attr attr; |
1b290d67 | 520 | |
89812fc8 | 521 | memset(&attr, 0, sizeof(attr)); |
9fafd98f | 522 | attr.bp_addr = (unsigned long) ptr; |
1b290d67 | 523 | |
89812fc8 JO |
524 | if (parse_breakpoint_type(type, &attr)) |
525 | return -EINVAL; | |
1b290d67 | 526 | |
aa59a485 FW |
527 | /* |
528 | * We should find a nice way to override the access length | |
529 | * Provide some defaults for now | |
530 | */ | |
89812fc8 JO |
531 | if (attr.bp_type == HW_BREAKPOINT_X) |
532 | attr.bp_len = sizeof(long); | |
aa59a485 | 533 | else |
89812fc8 | 534 | attr.bp_len = HW_BREAKPOINT_LEN_4; |
61c45981 | 535 | |
89812fc8 | 536 | attr.type = PERF_TYPE_BREAKPOINT; |
4a841d65 | 537 | attr.sample_period = 1; |
b908debd | 538 | |
287e74aa | 539 | return add_event(list, idx, &attr, NULL); |
74d5b588 JSR |
540 | } |
541 | ||
8f707d84 | 542 | static int config_term(struct perf_event_attr *attr, |
6cee6cd3 | 543 | struct parse_events_term *term) |
8f707d84 | 544 | { |
16fa7e82 JO |
545 | #define CHECK_TYPE_VAL(type) \ |
546 | do { \ | |
547 | if (PARSE_EVENTS__TERM_TYPE_ ## type != term->type_val) \ | |
548 | return -EINVAL; \ | |
549 | } while (0) | |
550 | ||
551 | switch (term->type_term) { | |
8f707d84 | 552 | case PARSE_EVENTS__TERM_TYPE_CONFIG: |
16fa7e82 | 553 | CHECK_TYPE_VAL(NUM); |
8f707d84 JO |
554 | attr->config = term->val.num; |
555 | break; | |
556 | case PARSE_EVENTS__TERM_TYPE_CONFIG1: | |
16fa7e82 | 557 | CHECK_TYPE_VAL(NUM); |
8f707d84 JO |
558 | attr->config1 = term->val.num; |
559 | break; | |
560 | case PARSE_EVENTS__TERM_TYPE_CONFIG2: | |
16fa7e82 | 561 | CHECK_TYPE_VAL(NUM); |
8f707d84 JO |
562 | attr->config2 = term->val.num; |
563 | break; | |
564 | case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: | |
16fa7e82 | 565 | CHECK_TYPE_VAL(NUM); |
8f707d84 JO |
566 | attr->sample_period = term->val.num; |
567 | break; | |
568 | case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: | |
569 | /* | |
570 | * TODO uncomment when the field is available | |
571 | * attr->branch_sample_type = term->val.num; | |
572 | */ | |
573 | break; | |
6b5fc39b JO |
574 | case PARSE_EVENTS__TERM_TYPE_NAME: |
575 | CHECK_TYPE_VAL(STR); | |
576 | break; | |
8f707d84 JO |
577 | default: |
578 | return -EINVAL; | |
579 | } | |
16fa7e82 | 580 | |
8f707d84 | 581 | return 0; |
16fa7e82 | 582 | #undef CHECK_TYPE_VAL |
8f707d84 JO |
583 | } |
584 | ||
585 | static int config_attr(struct perf_event_attr *attr, | |
586 | struct list_head *head, int fail) | |
587 | { | |
6cee6cd3 | 588 | struct parse_events_term *term; |
8f707d84 JO |
589 | |
590 | list_for_each_entry(term, head, list) | |
591 | if (config_term(attr, term) && fail) | |
592 | return -EINVAL; | |
593 | ||
594 | return 0; | |
595 | } | |
596 | ||
c5cd8ac0 | 597 | int parse_events_add_numeric(struct list_head *list, int *idx, |
b527bab5 | 598 | u32 type, u64 config, |
8f707d84 | 599 | struct list_head *head_config) |
8ad8db37 | 600 | { |
89812fc8 | 601 | struct perf_event_attr attr; |
61c45981 | 602 | |
89812fc8 JO |
603 | memset(&attr, 0, sizeof(attr)); |
604 | attr.type = type; | |
605 | attr.config = config; | |
8f707d84 JO |
606 | |
607 | if (head_config && | |
608 | config_attr(&attr, head_config, 1)) | |
609 | return -EINVAL; | |
610 | ||
9db1763c | 611 | return add_event(list, idx, &attr, NULL); |
61c45981 | 612 | } |
8ad8db37 | 613 | |
6cee6cd3 | 614 | static int parse_events__is_name_term(struct parse_events_term *term) |
6b5fc39b JO |
615 | { |
616 | return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME; | |
617 | } | |
618 | ||
9db1763c | 619 | static char *pmu_event_name(struct list_head *head_terms) |
6b5fc39b | 620 | { |
6cee6cd3 | 621 | struct parse_events_term *term; |
6b5fc39b JO |
622 | |
623 | list_for_each_entry(term, head_terms, list) | |
624 | if (parse_events__is_name_term(term)) | |
625 | return term->val.str; | |
626 | ||
9db1763c | 627 | return NULL; |
6b5fc39b JO |
628 | } |
629 | ||
c5cd8ac0 | 630 | int parse_events_add_pmu(struct list_head *list, int *idx, |
5f537a26 JO |
631 | char *name, struct list_head *head_config) |
632 | { | |
633 | struct perf_event_attr attr; | |
634 | struct perf_pmu *pmu; | |
635 | ||
636 | pmu = perf_pmu__find(name); | |
637 | if (!pmu) | |
638 | return -EINVAL; | |
639 | ||
640 | memset(&attr, 0, sizeof(attr)); | |
641 | ||
a6146d50 ZY |
642 | if (perf_pmu__check_alias(pmu, head_config)) |
643 | return -EINVAL; | |
644 | ||
5f537a26 JO |
645 | /* |
646 | * Configure hardcoded terms first, no need to check | |
647 | * return value when called with fail == 0 ;) | |
648 | */ | |
649 | config_attr(&attr, head_config, 0); | |
650 | ||
651 | if (perf_pmu__config(pmu, &attr, head_config)) | |
652 | return -EINVAL; | |
653 | ||
7ae92e74 YZ |
654 | return __add_event(list, idx, &attr, pmu_event_name(head_config), |
655 | pmu->cpus); | |
5f537a26 JO |
656 | } |
657 | ||
6a4bb04c JO |
658 | int parse_events__modifier_group(struct list_head *list, |
659 | char *event_mod) | |
89efb029 | 660 | { |
6a4bb04c JO |
661 | return parse_events__modifier_event(list, event_mod, true); |
662 | } | |
663 | ||
63dab225 | 664 | void parse_events__set_leader(char *name, struct list_head *list) |
6a4bb04c JO |
665 | { |
666 | struct perf_evsel *leader; | |
667 | ||
63dab225 ACM |
668 | __perf_evlist__set_leader(list); |
669 | leader = list_entry(list->next, struct perf_evsel, node); | |
6a4bb04c | 670 | leader->group_name = name ? strdup(name) : NULL; |
89efb029 JO |
671 | } |
672 | ||
c5cd8ac0 | 673 | /* list_event is assumed to point to malloc'ed memory */ |
5d7be90e JO |
674 | void parse_events_update_lists(struct list_head *list_event, |
675 | struct list_head *list_all) | |
676 | { | |
677 | /* | |
678 | * Called for single event definition. Update the | |
89efb029 | 679 | * 'all event' list, and reinit the 'single event' |
5d7be90e JO |
680 | * list, for next event definition. |
681 | */ | |
682 | list_splice_tail(list_event, list_all); | |
b847cbdc | 683 | free(list_event); |
5d7be90e JO |
684 | } |
685 | ||
f5b1135b JO |
686 | struct event_modifier { |
687 | int eu; | |
688 | int ek; | |
689 | int eh; | |
690 | int eH; | |
691 | int eG; | |
692 | int precise; | |
693 | int exclude_GH; | |
3c176311 | 694 | int sample_read; |
e9a7c414 | 695 | int pinned; |
f5b1135b JO |
696 | }; |
697 | ||
698 | static int get_event_modifier(struct event_modifier *mod, char *str, | |
699 | struct perf_evsel *evsel) | |
61c45981 | 700 | { |
f5b1135b JO |
701 | int eu = evsel ? evsel->attr.exclude_user : 0; |
702 | int ek = evsel ? evsel->attr.exclude_kernel : 0; | |
703 | int eh = evsel ? evsel->attr.exclude_hv : 0; | |
704 | int eH = evsel ? evsel->attr.exclude_host : 0; | |
705 | int eG = evsel ? evsel->attr.exclude_guest : 0; | |
706 | int precise = evsel ? evsel->attr.precise_ip : 0; | |
3c176311 | 707 | int sample_read = 0; |
e9a7c414 | 708 | int pinned = evsel ? evsel->attr.pinned : 0; |
a21ca2ca | 709 | |
f5b1135b JO |
710 | int exclude = eu | ek | eh; |
711 | int exclude_GH = evsel ? evsel->exclude_GH : 0; | |
712 | ||
f5b1135b | 713 | memset(mod, 0, sizeof(*mod)); |
ceb53fbf | 714 | |
61c45981 | 715 | while (*str) { |
ab608344 PZ |
716 | if (*str == 'u') { |
717 | if (!exclude) | |
718 | exclude = eu = ek = eh = 1; | |
61c45981 | 719 | eu = 0; |
ab608344 PZ |
720 | } else if (*str == 'k') { |
721 | if (!exclude) | |
722 | exclude = eu = ek = eh = 1; | |
61c45981 | 723 | ek = 0; |
ab608344 PZ |
724 | } else if (*str == 'h') { |
725 | if (!exclude) | |
726 | exclude = eu = ek = eh = 1; | |
61c45981 | 727 | eh = 0; |
99320cc8 JR |
728 | } else if (*str == 'G') { |
729 | if (!exclude_GH) | |
730 | exclude_GH = eG = eH = 1; | |
731 | eG = 0; | |
732 | } else if (*str == 'H') { | |
733 | if (!exclude_GH) | |
734 | exclude_GH = eG = eH = 1; | |
735 | eH = 0; | |
ab608344 PZ |
736 | } else if (*str == 'p') { |
737 | precise++; | |
1342798c DA |
738 | /* use of precise requires exclude_guest */ |
739 | if (!exclude_GH) | |
740 | eG = 1; | |
3c176311 JO |
741 | } else if (*str == 'S') { |
742 | sample_read = 1; | |
e9a7c414 ME |
743 | } else if (*str == 'D') { |
744 | pinned = 1; | |
ab608344 | 745 | } else |
61c45981 | 746 | break; |
ab608344 | 747 | |
61c45981 | 748 | ++str; |
5242519b | 749 | } |
ceb53fbf | 750 | |
89812fc8 JO |
751 | /* |
752 | * precise ip: | |
753 | * | |
754 | * 0 - SAMPLE_IP can have arbitrary skid | |
755 | * 1 - SAMPLE_IP must have constant skid | |
756 | * 2 - SAMPLE_IP requested to have 0 skid | |
757 | * 3 - SAMPLE_IP must have 0 skid | |
758 | * | |
759 | * See also PERF_RECORD_MISC_EXACT_IP | |
760 | */ | |
761 | if (precise > 3) | |
762 | return -EINVAL; | |
ceb53fbf | 763 | |
f5b1135b JO |
764 | mod->eu = eu; |
765 | mod->ek = ek; | |
766 | mod->eh = eh; | |
767 | mod->eH = eH; | |
768 | mod->eG = eG; | |
769 | mod->precise = precise; | |
770 | mod->exclude_GH = exclude_GH; | |
3c176311 | 771 | mod->sample_read = sample_read; |
e9a7c414 ME |
772 | mod->pinned = pinned; |
773 | ||
f5b1135b JO |
774 | return 0; |
775 | } | |
776 | ||
534123f4 JO |
777 | /* |
778 | * Basic modifier sanity check to validate it contains only one | |
779 | * instance of any modifier (apart from 'p') present. | |
780 | */ | |
781 | static int check_modifier(char *str) | |
782 | { | |
783 | char *p = str; | |
784 | ||
785 | /* The sizeof includes 0 byte as well. */ | |
e9a7c414 | 786 | if (strlen(str) > (sizeof("ukhGHpppSD") - 1)) |
534123f4 JO |
787 | return -1; |
788 | ||
789 | while (*p) { | |
790 | if (*p != 'p' && strchr(p + 1, *p)) | |
791 | return -1; | |
792 | p++; | |
793 | } | |
794 | ||
795 | return 0; | |
796 | } | |
797 | ||
f5b1135b JO |
798 | int parse_events__modifier_event(struct list_head *list, char *str, bool add) |
799 | { | |
800 | struct perf_evsel *evsel; | |
801 | struct event_modifier mod; | |
802 | ||
803 | if (str == NULL) | |
804 | return 0; | |
805 | ||
534123f4 JO |
806 | if (check_modifier(str)) |
807 | return -EINVAL; | |
808 | ||
f5b1135b JO |
809 | if (!add && get_event_modifier(&mod, str, NULL)) |
810 | return -EINVAL; | |
811 | ||
89812fc8 | 812 | list_for_each_entry(evsel, list, node) { |
f5b1135b JO |
813 | |
814 | if (add && get_event_modifier(&mod, str, evsel)) | |
815 | return -EINVAL; | |
816 | ||
817 | evsel->attr.exclude_user = mod.eu; | |
818 | evsel->attr.exclude_kernel = mod.ek; | |
819 | evsel->attr.exclude_hv = mod.eh; | |
820 | evsel->attr.precise_ip = mod.precise; | |
821 | evsel->attr.exclude_host = mod.eH; | |
822 | evsel->attr.exclude_guest = mod.eG; | |
823 | evsel->exclude_GH = mod.exclude_GH; | |
3c176311 | 824 | evsel->sample_read = mod.sample_read; |
e9a7c414 ME |
825 | |
826 | if (perf_evsel__is_group_leader(evsel)) | |
827 | evsel->attr.pinned = mod.pinned; | |
89812fc8 | 828 | } |
ceb53fbf | 829 | |
61c45981 PM |
830 | return 0; |
831 | } | |
8ad8db37 | 832 | |
ac2ba9f3 RR |
833 | int parse_events_name(struct list_head *list, char *name) |
834 | { | |
835 | struct perf_evsel *evsel; | |
836 | ||
837 | list_for_each_entry(evsel, list, node) { | |
838 | if (!evsel->name) | |
839 | evsel->name = strdup(name); | |
840 | } | |
841 | ||
842 | return 0; | |
843 | } | |
844 | ||
50e200f0 AK |
845 | static int parse_events__scanner(const char *str, void *data, int start_token); |
846 | ||
847 | static int parse_events_fixup(int ret, const char *str, void *data, | |
848 | int start_token) | |
849 | { | |
850 | char *o = strdup(str); | |
851 | char *s = NULL; | |
852 | char *t = o; | |
853 | char *p; | |
854 | int len = 0; | |
855 | ||
856 | if (!o) | |
857 | return ret; | |
858 | while ((p = strsep(&t, ",")) != NULL) { | |
859 | if (s) | |
860 | str_append(&s, &len, ","); | |
861 | str_append(&s, &len, "cpu/"); | |
862 | str_append(&s, &len, p); | |
863 | str_append(&s, &len, "/"); | |
864 | } | |
865 | free(o); | |
866 | if (!s) | |
867 | return -ENOMEM; | |
868 | return parse_events__scanner(s, data, start_token); | |
869 | } | |
870 | ||
90e2b22d | 871 | static int parse_events__scanner(const char *str, void *data, int start_token) |
61c45981 | 872 | { |
89812fc8 | 873 | YY_BUFFER_STATE buffer; |
ac20de6f | 874 | void *scanner; |
46010ab2 | 875 | int ret; |
bcd3279f | 876 | |
90e2b22d | 877 | ret = parse_events_lex_init_extra(start_token, &scanner); |
ac20de6f ZY |
878 | if (ret) |
879 | return ret; | |
880 | ||
881 | buffer = parse_events__scan_string(str, scanner); | |
a21ca2ca | 882 | |
82ba1f2f JO |
883 | #ifdef PARSER_DEBUG |
884 | parse_events_debug = 1; | |
885 | #endif | |
ac20de6f ZY |
886 | ret = parse_events_parse(data, scanner); |
887 | ||
888 | parse_events__flush_buffer(buffer, scanner); | |
889 | parse_events__delete_buffer(buffer, scanner); | |
890 | parse_events_lex_destroy(scanner); | |
50e200f0 AK |
891 | if (ret && !strchr(str, '/')) |
892 | ret = parse_events_fixup(ret, str, data, start_token); | |
ac20de6f ZY |
893 | return ret; |
894 | } | |
bcd3279f | 895 | |
90e2b22d JO |
896 | /* |
897 | * parse event config string, return a list of event terms. | |
898 | */ | |
899 | int parse_events_terms(struct list_head *terms, const char *str) | |
900 | { | |
23b6339b | 901 | struct parse_events_terms data = { |
90e2b22d JO |
902 | .terms = NULL, |
903 | }; | |
904 | int ret; | |
905 | ||
906 | ret = parse_events__scanner(str, &data, PE_START_TERMS); | |
907 | if (!ret) { | |
908 | list_splice(data.terms, terms); | |
909 | free(data.terms); | |
910 | return 0; | |
911 | } | |
912 | ||
b2c34fde AH |
913 | if (data.terms) |
914 | parse_events__free_terms(data.terms); | |
90e2b22d JO |
915 | return ret; |
916 | } | |
917 | ||
d8f7bbc9 | 918 | int parse_events(struct perf_evlist *evlist, const char *str) |
ac20de6f | 919 | { |
23b6339b | 920 | struct parse_events_evlist data = { |
ac20de6f ZY |
921 | .list = LIST_HEAD_INIT(data.list), |
922 | .idx = evlist->nr_entries, | |
923 | }; | |
924 | int ret; | |
bcd3279f | 925 | |
90e2b22d | 926 | ret = parse_events__scanner(str, &data, PE_START_EVENTS); |
89812fc8 | 927 | if (!ret) { |
46010ab2 JO |
928 | int entries = data.idx - evlist->nr_entries; |
929 | perf_evlist__splice_list_tail(evlist, &data.list, entries); | |
97f63e4a | 930 | evlist->nr_groups += data.nr_groups; |
89812fc8 JO |
931 | return 0; |
932 | } | |
bcd3279f | 933 | |
5d7be90e JO |
934 | /* |
935 | * There are 2 users - builtin-record and builtin-test objects. | |
936 | * Both call perf_evlist__delete in case of error, so we dont | |
937 | * need to bother. | |
938 | */ | |
bcd3279f | 939 | return ret; |
8ad8db37 IM |
940 | } |
941 | ||
f120f9d5 | 942 | int parse_events_option(const struct option *opt, const char *str, |
1d037ca1 | 943 | int unset __maybe_unused) |
f120f9d5 JO |
944 | { |
945 | struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; | |
d8f7bbc9 | 946 | int ret = parse_events(evlist, str); |
9175ce1f AK |
947 | |
948 | if (ret) { | |
949 | fprintf(stderr, "invalid or unsupported event: '%s'\n", str); | |
950 | fprintf(stderr, "Run 'perf list' for a list of valid events\n"); | |
951 | } | |
952 | return ret; | |
f120f9d5 JO |
953 | } |
954 | ||
361c99a6 | 955 | int parse_filter(const struct option *opt, const char *str, |
1d037ca1 | 956 | int unset __maybe_unused) |
c171b552 | 957 | { |
361c99a6 | 958 | struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; |
69aad6f1 | 959 | struct perf_evsel *last = NULL; |
c171b552 | 960 | |
361c99a6 | 961 | if (evlist->nr_entries > 0) |
0c21f736 | 962 | last = perf_evlist__last(evlist); |
69aad6f1 ACM |
963 | |
964 | if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) { | |
c171b552 LZ |
965 | fprintf(stderr, |
966 | "-F option should follow a -e tracepoint option\n"); | |
967 | return -1; | |
968 | } | |
969 | ||
69aad6f1 ACM |
970 | last->filter = strdup(str); |
971 | if (last->filter == NULL) { | |
c171b552 LZ |
972 | fprintf(stderr, "not enough memory to hold filter string\n"); |
973 | return -1; | |
974 | } | |
c171b552 LZ |
975 | |
976 | return 0; | |
977 | } | |
978 | ||
86847b62 | 979 | static const char * const event_type_descriptors[] = { |
86847b62 TG |
980 | "Hardware event", |
981 | "Software event", | |
982 | "Tracepoint event", | |
983 | "Hardware cache event", | |
41bdcb23 LW |
984 | "Raw hardware event descriptor", |
985 | "Hardware breakpoint", | |
86847b62 TG |
986 | }; |
987 | ||
f6bdafef JB |
988 | /* |
989 | * Print the events from <debugfs_mount_point>/tracing/events | |
990 | */ | |
991 | ||
a3277d2d FW |
992 | void print_tracepoint_events(const char *subsys_glob, const char *event_glob, |
993 | bool name_only) | |
f6bdafef JB |
994 | { |
995 | DIR *sys_dir, *evt_dir; | |
996 | struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; | |
f6bdafef | 997 | char evt_path[MAXPATHLEN]; |
725b1368 | 998 | char dir_path[MAXPATHLEN]; |
f6bdafef | 999 | |
ebf294bf | 1000 | if (debugfs_valid_mountpoint(tracing_events_path)) |
f6bdafef JB |
1001 | return; |
1002 | ||
ebf294bf | 1003 | sys_dir = opendir(tracing_events_path); |
f6bdafef | 1004 | if (!sys_dir) |
725b1368 | 1005 | return; |
6b58e7f1 UD |
1006 | |
1007 | for_each_subsystem(sys_dir, sys_dirent, sys_next) { | |
668b8788 ACM |
1008 | if (subsys_glob != NULL && |
1009 | !strglobmatch(sys_dirent.d_name, subsys_glob)) | |
1010 | continue; | |
725b1368 | 1011 | |
ebf294bf | 1012 | snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, |
725b1368 ED |
1013 | sys_dirent.d_name); |
1014 | evt_dir = opendir(dir_path); | |
1015 | if (!evt_dir) | |
6b58e7f1 | 1016 | continue; |
725b1368 | 1017 | |
6b58e7f1 | 1018 | for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) { |
668b8788 ACM |
1019 | if (event_glob != NULL && |
1020 | !strglobmatch(evt_dirent.d_name, event_glob)) | |
1021 | continue; | |
1022 | ||
a3277d2d FW |
1023 | if (name_only) { |
1024 | printf("%s:%s ", sys_dirent.d_name, evt_dirent.d_name); | |
1025 | continue; | |
1026 | } | |
1027 | ||
f6bdafef JB |
1028 | snprintf(evt_path, MAXPATHLEN, "%s:%s", |
1029 | sys_dirent.d_name, evt_dirent.d_name); | |
947b4ad1 | 1030 | printf(" %-50s [%s]\n", evt_path, |
41bdcb23 | 1031 | event_type_descriptors[PERF_TYPE_TRACEPOINT]); |
f6bdafef JB |
1032 | } |
1033 | closedir(evt_dir); | |
1034 | } | |
f6bdafef JB |
1035 | closedir(sys_dir); |
1036 | } | |
1037 | ||
20c457b8 TR |
1038 | /* |
1039 | * Check whether event is in <debugfs_mount_point>/tracing/events | |
1040 | */ | |
1041 | ||
1042 | int is_valid_tracepoint(const char *event_string) | |
1043 | { | |
1044 | DIR *sys_dir, *evt_dir; | |
1045 | struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; | |
1046 | char evt_path[MAXPATHLEN]; | |
1047 | char dir_path[MAXPATHLEN]; | |
1048 | ||
ebf294bf | 1049 | if (debugfs_valid_mountpoint(tracing_events_path)) |
20c457b8 TR |
1050 | return 0; |
1051 | ||
ebf294bf | 1052 | sys_dir = opendir(tracing_events_path); |
20c457b8 TR |
1053 | if (!sys_dir) |
1054 | return 0; | |
1055 | ||
1056 | for_each_subsystem(sys_dir, sys_dirent, sys_next) { | |
1057 | ||
ebf294bf | 1058 | snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, |
20c457b8 TR |
1059 | sys_dirent.d_name); |
1060 | evt_dir = opendir(dir_path); | |
1061 | if (!evt_dir) | |
1062 | continue; | |
1063 | ||
1064 | for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) { | |
1065 | snprintf(evt_path, MAXPATHLEN, "%s:%s", | |
1066 | sys_dirent.d_name, evt_dirent.d_name); | |
1067 | if (!strcmp(evt_path, event_string)) { | |
1068 | closedir(evt_dir); | |
1069 | closedir(sys_dir); | |
1070 | return 1; | |
1071 | } | |
1072 | } | |
1073 | closedir(evt_dir); | |
1074 | } | |
1075 | closedir(sys_dir); | |
1076 | return 0; | |
1077 | } | |
1078 | ||
1dc12760 JO |
1079 | static void __print_events_type(u8 type, struct event_symbol *syms, |
1080 | unsigned max) | |
668b8788 | 1081 | { |
668b8788 | 1082 | char name[64]; |
1dc12760 | 1083 | unsigned i; |
668b8788 | 1084 | |
1dc12760 | 1085 | for (i = 0; i < max ; i++, syms++) { |
668b8788 ACM |
1086 | if (strlen(syms->alias)) |
1087 | snprintf(name, sizeof(name), "%s OR %s", | |
1088 | syms->symbol, syms->alias); | |
1089 | else | |
1090 | snprintf(name, sizeof(name), "%s", syms->symbol); | |
1091 | ||
947b4ad1 | 1092 | printf(" %-50s [%s]\n", name, |
668b8788 ACM |
1093 | event_type_descriptors[type]); |
1094 | } | |
1095 | } | |
1096 | ||
1dc12760 JO |
1097 | void print_events_type(u8 type) |
1098 | { | |
1099 | if (type == PERF_TYPE_SOFTWARE) | |
1100 | __print_events_type(type, event_symbols_sw, PERF_COUNT_SW_MAX); | |
1101 | else | |
1102 | __print_events_type(type, event_symbols_hw, PERF_COUNT_HW_MAX); | |
1103 | } | |
1104 | ||
a3277d2d | 1105 | int print_hwcache_events(const char *event_glob, bool name_only) |
668b8788 ACM |
1106 | { |
1107 | unsigned int type, op, i, printed = 0; | |
0b668bc9 | 1108 | char name[64]; |
668b8788 ACM |
1109 | |
1110 | for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { | |
1111 | for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { | |
1112 | /* skip invalid cache type */ | |
0b668bc9 | 1113 | if (!perf_evsel__is_cache_op_valid(type, op)) |
668b8788 ACM |
1114 | continue; |
1115 | ||
1116 | for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) { | |
0b668bc9 ACM |
1117 | __perf_evsel__hw_cache_type_op_res_name(type, op, i, |
1118 | name, sizeof(name)); | |
947b4ad1 | 1119 | if (event_glob != NULL && !strglobmatch(name, event_glob)) |
668b8788 ACM |
1120 | continue; |
1121 | ||
a3277d2d FW |
1122 | if (name_only) |
1123 | printf("%s ", name); | |
1124 | else | |
1125 | printf(" %-50s [%s]\n", name, | |
1126 | event_type_descriptors[PERF_TYPE_HW_CACHE]); | |
668b8788 ACM |
1127 | ++printed; |
1128 | } | |
1129 | } | |
1130 | } | |
1131 | ||
dc098b35 AK |
1132 | if (printed) |
1133 | printf("\n"); | |
668b8788 ACM |
1134 | return printed; |
1135 | } | |
1136 | ||
1dc12760 | 1137 | static void print_symbol_events(const char *event_glob, unsigned type, |
a3277d2d FW |
1138 | struct event_symbol *syms, unsigned max, |
1139 | bool name_only) | |
8ad8db37 | 1140 | { |
1dc12760 | 1141 | unsigned i, printed = 0; |
947b4ad1 | 1142 | char name[MAX_NAME_LEN]; |
8ad8db37 | 1143 | |
1dc12760 | 1144 | for (i = 0; i < max; i++, syms++) { |
668b8788 ACM |
1145 | |
1146 | if (event_glob != NULL && | |
1147 | !(strglobmatch(syms->symbol, event_glob) || | |
1148 | (syms->alias && strglobmatch(syms->alias, event_glob)))) | |
1149 | continue; | |
8ad8db37 | 1150 | |
a3277d2d FW |
1151 | if (name_only) { |
1152 | printf("%s ", syms->symbol); | |
1153 | continue; | |
1154 | } | |
1155 | ||
74d5b588 | 1156 | if (strlen(syms->alias)) |
947b4ad1 | 1157 | snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias); |
74d5b588 | 1158 | else |
947b4ad1 | 1159 | strncpy(name, syms->symbol, MAX_NAME_LEN); |
8ad8db37 | 1160 | |
1dc12760 JO |
1161 | printf(" %-50s [%s]\n", name, event_type_descriptors[type]); |
1162 | ||
1163 | printed++; | |
8ad8db37 IM |
1164 | } |
1165 | ||
1dc12760 | 1166 | if (printed) |
668b8788 | 1167 | printf("\n"); |
1dc12760 JO |
1168 | } |
1169 | ||
1170 | /* | |
1171 | * Print the help text for the event symbols: | |
1172 | */ | |
a3277d2d | 1173 | void print_events(const char *event_glob, bool name_only) |
1dc12760 | 1174 | { |
a3277d2d FW |
1175 | if (!name_only) { |
1176 | printf("\n"); | |
1177 | printf("List of pre-defined events (to be used in -e):\n"); | |
1178 | } | |
1dc12760 JO |
1179 | |
1180 | print_symbol_events(event_glob, PERF_TYPE_HARDWARE, | |
a3277d2d | 1181 | event_symbols_hw, PERF_COUNT_HW_MAX, name_only); |
1dc12760 JO |
1182 | |
1183 | print_symbol_events(event_glob, PERF_TYPE_SOFTWARE, | |
a3277d2d | 1184 | event_symbols_sw, PERF_COUNT_SW_MAX, name_only); |
1dc12760 | 1185 | |
a3277d2d | 1186 | print_hwcache_events(event_glob, name_only); |
668b8788 | 1187 | |
dc098b35 AK |
1188 | print_pmu_events(event_glob, name_only); |
1189 | ||
668b8788 ACM |
1190 | if (event_glob != NULL) |
1191 | return; | |
73c24cb8 | 1192 | |
a3277d2d | 1193 | if (!name_only) { |
a3277d2d FW |
1194 | printf(" %-50s [%s]\n", |
1195 | "rNNN", | |
1196 | event_type_descriptors[PERF_TYPE_RAW]); | |
1197 | printf(" %-50s [%s]\n", | |
1198 | "cpu/t1=v1[,t2=v2,t3 ...]/modifier", | |
1199 | event_type_descriptors[PERF_TYPE_RAW]); | |
af3df2cf | 1200 | printf(" (see 'man perf-list' on how to encode it)\n"); |
a3277d2d FW |
1201 | printf("\n"); |
1202 | ||
1203 | printf(" %-50s [%s]\n", | |
1204 | "mem:<addr>[:access]", | |
41bdcb23 | 1205 | event_type_descriptors[PERF_TYPE_BREAKPOINT]); |
a3277d2d FW |
1206 | printf("\n"); |
1207 | } | |
1b290d67 | 1208 | |
a3277d2d | 1209 | print_tracepoint_events(NULL, NULL, name_only); |
8ad8db37 | 1210 | } |
8f707d84 | 1211 | |
6cee6cd3 | 1212 | int parse_events__is_hardcoded_term(struct parse_events_term *term) |
8f707d84 | 1213 | { |
16fa7e82 | 1214 | return term->type_term != PARSE_EVENTS__TERM_TYPE_USER; |
8f707d84 JO |
1215 | } |
1216 | ||
6cee6cd3 | 1217 | static int new_term(struct parse_events_term **_term, int type_val, |
16fa7e82 | 1218 | int type_term, char *config, |
b527bab5 | 1219 | char *str, u64 num) |
8f707d84 | 1220 | { |
6cee6cd3 | 1221 | struct parse_events_term *term; |
8f707d84 JO |
1222 | |
1223 | term = zalloc(sizeof(*term)); | |
1224 | if (!term) | |
1225 | return -ENOMEM; | |
1226 | ||
1227 | INIT_LIST_HEAD(&term->list); | |
16fa7e82 JO |
1228 | term->type_val = type_val; |
1229 | term->type_term = type_term; | |
8f707d84 JO |
1230 | term->config = config; |
1231 | ||
16fa7e82 | 1232 | switch (type_val) { |
8f707d84 JO |
1233 | case PARSE_EVENTS__TERM_TYPE_NUM: |
1234 | term->val.num = num; | |
1235 | break; | |
1236 | case PARSE_EVENTS__TERM_TYPE_STR: | |
1237 | term->val.str = str; | |
1238 | break; | |
1239 | default: | |
4be8be6b | 1240 | free(term); |
8f707d84 JO |
1241 | return -EINVAL; |
1242 | } | |
1243 | ||
1244 | *_term = term; | |
1245 | return 0; | |
1246 | } | |
1247 | ||
6cee6cd3 | 1248 | int parse_events_term__num(struct parse_events_term **term, |
b527bab5 | 1249 | int type_term, char *config, u64 num) |
16fa7e82 JO |
1250 | { |
1251 | return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term, | |
1252 | config, NULL, num); | |
1253 | } | |
1254 | ||
6cee6cd3 | 1255 | int parse_events_term__str(struct parse_events_term **term, |
16fa7e82 JO |
1256 | int type_term, char *config, char *str) |
1257 | { | |
1258 | return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term, | |
1259 | config, str, 0); | |
1260 | } | |
1261 | ||
6cee6cd3 | 1262 | int parse_events_term__sym_hw(struct parse_events_term **term, |
1d33d6dc JO |
1263 | char *config, unsigned idx) |
1264 | { | |
1265 | struct event_symbol *sym; | |
1266 | ||
1267 | BUG_ON(idx >= PERF_COUNT_HW_MAX); | |
1268 | sym = &event_symbols_hw[idx]; | |
1269 | ||
1270 | if (config) | |
1271 | return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, | |
1272 | PARSE_EVENTS__TERM_TYPE_USER, config, | |
1273 | (char *) sym->symbol, 0); | |
1274 | else | |
1275 | return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, | |
1276 | PARSE_EVENTS__TERM_TYPE_USER, | |
1277 | (char *) "event", (char *) sym->symbol, 0); | |
1278 | } | |
1279 | ||
6cee6cd3 ACM |
1280 | int parse_events_term__clone(struct parse_events_term **new, |
1281 | struct parse_events_term *term) | |
a6146d50 ZY |
1282 | { |
1283 | return new_term(new, term->type_val, term->type_term, term->config, | |
1284 | term->val.str, term->val.num); | |
1285 | } | |
1286 | ||
8f707d84 JO |
1287 | void parse_events__free_terms(struct list_head *terms) |
1288 | { | |
6cee6cd3 | 1289 | struct parse_events_term *term, *h; |
8f707d84 JO |
1290 | |
1291 | list_for_each_entry_safe(term, h, terms, list) | |
1292 | free(term); | |
8f707d84 | 1293 | } |