]>
Commit | Line | Data |
---|---|---|
d2709c7c | 1 | #include <linux/hw_breakpoint.h> |
8dd2a131 | 2 | #include <linux/err.h> |
8ad8db37 | 3 | #include "util.h" |
6b58e7f1 | 4 | #include "../perf.h" |
361c99a6 | 5 | #include "evlist.h" |
69aad6f1 | 6 | #include "evsel.h" |
8ad8db37 IM |
7 | #include "parse-options.h" |
8 | #include "parse-events.h" | |
9 | #include "exec_cmd.h" | |
42f60c2d | 10 | #include "string.h" |
5aab621b | 11 | #include "symbol.h" |
5beeded1 | 12 | #include "cache.h" |
8755a8f2 | 13 | #include "header.h" |
6e81c74c | 14 | #include "debug.h" |
592d5a6b | 15 | #include <api/fs/tracing_path.h> |
ac20de6f | 16 | #include "parse-events-bison.h" |
90e2b22d | 17 | #define YY_EXTRA_TYPE int |
89812fc8 | 18 | #include "parse-events-flex.h" |
5f537a26 | 19 | #include "pmu.h" |
b41f1cec | 20 | #include "thread_map.h" |
f30a79b0 | 21 | #include "cpumap.h" |
b39b8393 | 22 | #include "asm/bug.h" |
89812fc8 JO |
23 | |
24 | #define MAX_NAME_LEN 100 | |
8ad8db37 | 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); |
e637d177 HK |
30 | static int get_config_terms(struct list_head *head_config, |
31 | struct list_head *head_terms __maybe_unused); | |
bcd3279f | 32 | |
dcb4e102 KL |
33 | static struct perf_pmu_event_symbol *perf_pmu_events_list; |
34 | /* | |
35 | * The variable indicates the number of supported pmu event symbols. | |
36 | * 0 means not initialized and ready to init | |
37 | * -1 means failed to init, don't try anymore | |
38 | * >0 is the number of supported pmu event symbols | |
39 | */ | |
40 | static int perf_pmu_events_list_num; | |
41 | ||
705750f2 | 42 | struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = { |
1dc12760 JO |
43 | [PERF_COUNT_HW_CPU_CYCLES] = { |
44 | .symbol = "cpu-cycles", | |
45 | .alias = "cycles", | |
46 | }, | |
47 | [PERF_COUNT_HW_INSTRUCTIONS] = { | |
48 | .symbol = "instructions", | |
49 | .alias = "", | |
50 | }, | |
51 | [PERF_COUNT_HW_CACHE_REFERENCES] = { | |
52 | .symbol = "cache-references", | |
53 | .alias = "", | |
54 | }, | |
55 | [PERF_COUNT_HW_CACHE_MISSES] = { | |
56 | .symbol = "cache-misses", | |
57 | .alias = "", | |
58 | }, | |
59 | [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { | |
60 | .symbol = "branch-instructions", | |
61 | .alias = "branches", | |
62 | }, | |
63 | [PERF_COUNT_HW_BRANCH_MISSES] = { | |
64 | .symbol = "branch-misses", | |
65 | .alias = "", | |
66 | }, | |
67 | [PERF_COUNT_HW_BUS_CYCLES] = { | |
68 | .symbol = "bus-cycles", | |
69 | .alias = "", | |
70 | }, | |
71 | [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = { | |
72 | .symbol = "stalled-cycles-frontend", | |
73 | .alias = "idle-cycles-frontend", | |
74 | }, | |
75 | [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = { | |
76 | .symbol = "stalled-cycles-backend", | |
77 | .alias = "idle-cycles-backend", | |
78 | }, | |
79 | [PERF_COUNT_HW_REF_CPU_CYCLES] = { | |
80 | .symbol = "ref-cycles", | |
81 | .alias = "", | |
82 | }, | |
83 | }; | |
84 | ||
705750f2 | 85 | struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = { |
1dc12760 JO |
86 | [PERF_COUNT_SW_CPU_CLOCK] = { |
87 | .symbol = "cpu-clock", | |
88 | .alias = "", | |
89 | }, | |
90 | [PERF_COUNT_SW_TASK_CLOCK] = { | |
91 | .symbol = "task-clock", | |
92 | .alias = "", | |
93 | }, | |
94 | [PERF_COUNT_SW_PAGE_FAULTS] = { | |
95 | .symbol = "page-faults", | |
96 | .alias = "faults", | |
97 | }, | |
98 | [PERF_COUNT_SW_CONTEXT_SWITCHES] = { | |
99 | .symbol = "context-switches", | |
100 | .alias = "cs", | |
101 | }, | |
102 | [PERF_COUNT_SW_CPU_MIGRATIONS] = { | |
103 | .symbol = "cpu-migrations", | |
104 | .alias = "migrations", | |
105 | }, | |
106 | [PERF_COUNT_SW_PAGE_FAULTS_MIN] = { | |
107 | .symbol = "minor-faults", | |
108 | .alias = "", | |
109 | }, | |
110 | [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = { | |
111 | .symbol = "major-faults", | |
112 | .alias = "", | |
113 | }, | |
114 | [PERF_COUNT_SW_ALIGNMENT_FAULTS] = { | |
115 | .symbol = "alignment-faults", | |
116 | .alias = "", | |
117 | }, | |
118 | [PERF_COUNT_SW_EMULATION_FAULTS] = { | |
119 | .symbol = "emulation-faults", | |
120 | .alias = "", | |
121 | }, | |
d22d1a2a AH |
122 | [PERF_COUNT_SW_DUMMY] = { |
123 | .symbol = "dummy", | |
124 | .alias = "", | |
125 | }, | |
8ad8db37 IM |
126 | }; |
127 | ||
cdd6c482 IM |
128 | #define __PERF_EVENT_FIELD(config, name) \ |
129 | ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT) | |
5242519b | 130 | |
1fc570ad | 131 | #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW) |
cdd6c482 | 132 | #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG) |
1fc570ad | 133 | #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE) |
cdd6c482 | 134 | #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT) |
5242519b | 135 | |
6b58e7f1 | 136 | #define for_each_subsystem(sys_dir, sys_dirent, sys_next) \ |
f6bdafef | 137 | while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \ |
6b58e7f1 | 138 | if (sys_dirent.d_type == DT_DIR && \ |
f6bdafef JB |
139 | (strcmp(sys_dirent.d_name, ".")) && \ |
140 | (strcmp(sys_dirent.d_name, ".."))) | |
141 | ||
ae07b63f PZ |
142 | static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir) |
143 | { | |
144 | char evt_path[MAXPATHLEN]; | |
145 | int fd; | |
146 | ||
ebf294bf | 147 | snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path, |
ae07b63f PZ |
148 | sys_dir->d_name, evt_dir->d_name); |
149 | fd = open(evt_path, O_RDONLY); | |
150 | if (fd < 0) | |
151 | return -EINVAL; | |
152 | close(fd); | |
153 | ||
154 | return 0; | |
155 | } | |
156 | ||
6b58e7f1 | 157 | #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \ |
f6bdafef | 158 | while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \ |
6b58e7f1 | 159 | if (evt_dirent.d_type == DT_DIR && \ |
f6bdafef | 160 | (strcmp(evt_dirent.d_name, ".")) && \ |
ae07b63f PZ |
161 | (strcmp(evt_dirent.d_name, "..")) && \ |
162 | (!tp_event_has_id(&sys_dirent, &evt_dirent))) | |
f6bdafef | 163 | |
270bbbe8 | 164 | #define MAX_EVENT_LENGTH 512 |
f6bdafef | 165 | |
f6bdafef | 166 | |
1ef2ed10 | 167 | struct tracepoint_path *tracepoint_id_to_path(u64 config) |
f6bdafef | 168 | { |
1ef2ed10 | 169 | struct tracepoint_path *path = NULL; |
f6bdafef JB |
170 | DIR *sys_dir, *evt_dir; |
171 | struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; | |
8aa8a7c8 | 172 | char id_buf[24]; |
725b1368 | 173 | int fd; |
f6bdafef JB |
174 | u64 id; |
175 | char evt_path[MAXPATHLEN]; | |
725b1368 | 176 | char dir_path[MAXPATHLEN]; |
f6bdafef | 177 | |
ebf294bf | 178 | sys_dir = opendir(tracing_events_path); |
f6bdafef | 179 | if (!sys_dir) |
725b1368 | 180 | return NULL; |
6b58e7f1 UD |
181 | |
182 | for_each_subsystem(sys_dir, sys_dirent, sys_next) { | |
725b1368 | 183 | |
ebf294bf | 184 | snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, |
725b1368 ED |
185 | sys_dirent.d_name); |
186 | evt_dir = opendir(dir_path); | |
187 | if (!evt_dir) | |
6b58e7f1 | 188 | continue; |
725b1368 | 189 | |
6b58e7f1 | 190 | for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) { |
725b1368 ED |
191 | |
192 | snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, | |
f6bdafef | 193 | evt_dirent.d_name); |
725b1368 | 194 | fd = open(evt_path, O_RDONLY); |
f6bdafef JB |
195 | if (fd < 0) |
196 | continue; | |
197 | if (read(fd, id_buf, sizeof(id_buf)) < 0) { | |
198 | close(fd); | |
199 | continue; | |
200 | } | |
201 | close(fd); | |
202 | id = atoll(id_buf); | |
203 | if (id == config) { | |
204 | closedir(evt_dir); | |
205 | closedir(sys_dir); | |
59b4caeb | 206 | path = zalloc(sizeof(*path)); |
1ef2ed10 FW |
207 | path->system = malloc(MAX_EVENT_LENGTH); |
208 | if (!path->system) { | |
209 | free(path); | |
210 | return NULL; | |
211 | } | |
212 | path->name = malloc(MAX_EVENT_LENGTH); | |
213 | if (!path->name) { | |
74cf249d | 214 | zfree(&path->system); |
1ef2ed10 FW |
215 | free(path); |
216 | return NULL; | |
217 | } | |
218 | strncpy(path->system, sys_dirent.d_name, | |
219 | MAX_EVENT_LENGTH); | |
220 | strncpy(path->name, evt_dirent.d_name, | |
221 | MAX_EVENT_LENGTH); | |
222 | return path; | |
f6bdafef JB |
223 | } |
224 | } | |
225 | closedir(evt_dir); | |
226 | } | |
227 | ||
f6bdafef | 228 | closedir(sys_dir); |
1ef2ed10 FW |
229 | return NULL; |
230 | } | |
231 | ||
e7c93f09 NK |
232 | struct tracepoint_path *tracepoint_name_to_path(const char *name) |
233 | { | |
234 | struct tracepoint_path *path = zalloc(sizeof(*path)); | |
235 | char *str = strchr(name, ':'); | |
236 | ||
237 | if (path == NULL || str == NULL) { | |
238 | free(path); | |
239 | return NULL; | |
240 | } | |
241 | ||
242 | path->system = strndup(name, str - name); | |
243 | path->name = strdup(str+1); | |
244 | ||
245 | if (path->system == NULL || path->name == NULL) { | |
74cf249d ACM |
246 | zfree(&path->system); |
247 | zfree(&path->name); | |
e7c93f09 NK |
248 | free(path); |
249 | path = NULL; | |
250 | } | |
251 | ||
252 | return path; | |
253 | } | |
254 | ||
1424dc96 DA |
255 | const char *event_type(int type) |
256 | { | |
257 | switch (type) { | |
258 | case PERF_TYPE_HARDWARE: | |
259 | return "hardware"; | |
260 | ||
261 | case PERF_TYPE_SOFTWARE: | |
262 | return "software"; | |
263 | ||
264 | case PERF_TYPE_TRACEPOINT: | |
265 | return "tracepoint"; | |
266 | ||
267 | case PERF_TYPE_HW_CACHE: | |
268 | return "hardware-cache"; | |
269 | ||
270 | default: | |
271 | break; | |
272 | } | |
273 | ||
274 | return "unknown"; | |
275 | } | |
276 | ||
7ae92e74 YZ |
277 | |
278 | ||
410136f5 SE |
279 | static struct perf_evsel * |
280 | __add_event(struct list_head *list, int *idx, | |
281 | struct perf_event_attr *attr, | |
930a2e29 JO |
282 | char *name, struct cpu_map *cpus, |
283 | struct list_head *config_terms) | |
89812fc8 JO |
284 | { |
285 | struct perf_evsel *evsel; | |
286 | ||
287 | event_attr_init(attr); | |
288 | ||
ef503831 | 289 | evsel = perf_evsel__new_idx(attr, (*idx)++); |
c5cd8ac0 | 290 | if (!evsel) |
410136f5 | 291 | return NULL; |
89812fc8 | 292 | |
fce4d296 AH |
293 | evsel->cpus = cpu_map__get(cpus); |
294 | evsel->own_cpus = cpu_map__get(cpus); | |
f30a79b0 | 295 | |
9db1763c ACM |
296 | if (name) |
297 | evsel->name = strdup(name); | |
930a2e29 JO |
298 | |
299 | if (config_terms) | |
300 | list_splice(config_terms, &evsel->config_terms); | |
301 | ||
b847cbdc | 302 | list_add_tail(&evsel->node, list); |
410136f5 | 303 | return evsel; |
89812fc8 JO |
304 | } |
305 | ||
c5cd8ac0 | 306 | static int add_event(struct list_head *list, int *idx, |
930a2e29 JO |
307 | struct perf_event_attr *attr, char *name, |
308 | struct list_head *config_terms) | |
7ae92e74 | 309 | { |
930a2e29 | 310 | return __add_event(list, idx, attr, name, NULL, config_terms) ? 0 : -ENOMEM; |
7ae92e74 YZ |
311 | } |
312 | ||
0b668bc9 | 313 | static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size) |
8326f44d IM |
314 | { |
315 | int i, j; | |
61c45981 | 316 | int n, longest = -1; |
8326f44d IM |
317 | |
318 | for (i = 0; i < size; i++) { | |
0b668bc9 | 319 | for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) { |
61c45981 | 320 | n = strlen(names[i][j]); |
89812fc8 | 321 | if (n > longest && !strncasecmp(str, names[i][j], n)) |
61c45981 PM |
322 | longest = n; |
323 | } | |
89812fc8 | 324 | if (longest > 0) |
61c45981 | 325 | return i; |
8326f44d IM |
326 | } |
327 | ||
8953645f | 328 | return -1; |
8326f44d IM |
329 | } |
330 | ||
c5cd8ac0 | 331 | int parse_events_add_cache(struct list_head *list, int *idx, |
89812fc8 | 332 | char *type, char *op_result1, char *op_result2) |
8326f44d | 333 | { |
89812fc8 JO |
334 | struct perf_event_attr attr; |
335 | char name[MAX_NAME_LEN]; | |
61c45981 | 336 | int cache_type = -1, cache_op = -1, cache_result = -1; |
89812fc8 JO |
337 | char *op_result[2] = { op_result1, op_result2 }; |
338 | int i, n; | |
8326f44d | 339 | |
8326f44d IM |
340 | /* |
341 | * No fallback - if we cannot get a clear cache type | |
342 | * then bail out: | |
343 | */ | |
0b668bc9 | 344 | cache_type = parse_aliases(type, perf_evsel__hw_cache, |
89812fc8 | 345 | PERF_COUNT_HW_CACHE_MAX); |
8326f44d | 346 | if (cache_type == -1) |
89812fc8 JO |
347 | return -EINVAL; |
348 | ||
349 | n = snprintf(name, MAX_NAME_LEN, "%s", type); | |
61c45981 | 350 | |
89812fc8 JO |
351 | for (i = 0; (i < 2) && (op_result[i]); i++) { |
352 | char *str = op_result[i]; | |
353 | ||
275ef387 | 354 | n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str); |
61c45981 PM |
355 | |
356 | if (cache_op == -1) { | |
0b668bc9 | 357 | cache_op = parse_aliases(str, perf_evsel__hw_cache_op, |
89812fc8 | 358 | PERF_COUNT_HW_CACHE_OP_MAX); |
61c45981 | 359 | if (cache_op >= 0) { |
0b668bc9 | 360 | if (!perf_evsel__is_cache_op_valid(cache_type, cache_op)) |
89812fc8 | 361 | return -EINVAL; |
61c45981 PM |
362 | continue; |
363 | } | |
364 | } | |
365 | ||
366 | if (cache_result == -1) { | |
0b668bc9 ACM |
367 | cache_result = parse_aliases(str, perf_evsel__hw_cache_result, |
368 | PERF_COUNT_HW_CACHE_RESULT_MAX); | |
61c45981 PM |
369 | if (cache_result >= 0) |
370 | continue; | |
371 | } | |
61c45981 | 372 | } |
8326f44d | 373 | |
8326f44d IM |
374 | /* |
375 | * Fall back to reads: | |
376 | */ | |
8953645f IM |
377 | if (cache_op == -1) |
378 | cache_op = PERF_COUNT_HW_CACHE_OP_READ; | |
8326f44d | 379 | |
8326f44d IM |
380 | /* |
381 | * Fall back to accesses: | |
382 | */ | |
383 | if (cache_result == -1) | |
384 | cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS; | |
385 | ||
89812fc8 JO |
386 | memset(&attr, 0, sizeof(attr)); |
387 | attr.config = cache_type | (cache_op << 8) | (cache_result << 16); | |
388 | attr.type = PERF_TYPE_HW_CACHE; | |
930a2e29 | 389 | return add_event(list, idx, &attr, name, NULL); |
bcd3279f FW |
390 | } |
391 | ||
272ed29a | 392 | static void tracepoint_error(struct parse_events_error *e, int err, |
19658171 JO |
393 | char *sys, char *name) |
394 | { | |
395 | char help[BUFSIZ]; | |
396 | ||
397 | /* | |
398 | * We get error directly from syscall errno ( > 0), | |
399 | * or from encoded pointer's error ( < 0). | |
400 | */ | |
401 | err = abs(err); | |
402 | ||
403 | switch (err) { | |
404 | case EACCES: | |
272ed29a | 405 | e->str = strdup("can't access trace events"); |
19658171 JO |
406 | break; |
407 | case ENOENT: | |
272ed29a | 408 | e->str = strdup("unknown tracepoint"); |
19658171 JO |
409 | break; |
410 | default: | |
272ed29a | 411 | e->str = strdup("failed to add tracepoint"); |
19658171 JO |
412 | break; |
413 | } | |
414 | ||
415 | tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name); | |
272ed29a | 416 | e->help = strdup(help); |
19658171 JO |
417 | } |
418 | ||
c5cd8ac0 | 419 | static int add_tracepoint(struct list_head *list, int *idx, |
e2f9f8ea | 420 | char *sys_name, char *evt_name, |
272ed29a | 421 | struct parse_events_error *err, |
e637d177 | 422 | struct list_head *head_config) |
bcd3279f | 423 | { |
82fe1c29 | 424 | struct perf_evsel *evsel; |
bcd3279f | 425 | |
ef503831 | 426 | evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++); |
19658171 | 427 | if (IS_ERR(evsel)) { |
272ed29a | 428 | tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name); |
8dd2a131 | 429 | return PTR_ERR(evsel); |
19658171 | 430 | } |
bcd3279f | 431 | |
e637d177 HK |
432 | if (head_config) { |
433 | LIST_HEAD(config_terms); | |
434 | ||
435 | if (get_config_terms(head_config, &config_terms)) | |
436 | return -ENOMEM; | |
437 | list_splice(&config_terms, &evsel->config_terms); | |
438 | } | |
439 | ||
82fe1c29 | 440 | list_add_tail(&evsel->node, list); |
82fe1c29 | 441 | return 0; |
8326f44d IM |
442 | } |
443 | ||
c5cd8ac0 | 444 | static int add_tracepoint_multi_event(struct list_head *list, int *idx, |
e2f9f8ea | 445 | char *sys_name, char *evt_name, |
272ed29a | 446 | struct parse_events_error *err, |
e637d177 | 447 | struct list_head *head_config) |
bcd3279f FW |
448 | { |
449 | char evt_path[MAXPATHLEN]; | |
450 | struct dirent *evt_ent; | |
451 | DIR *evt_dir; | |
89812fc8 | 452 | int ret = 0; |
bcd3279f | 453 | |
ebf294bf | 454 | snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name); |
bcd3279f | 455 | evt_dir = opendir(evt_path); |
bcd3279f | 456 | if (!evt_dir) { |
272ed29a | 457 | tracepoint_error(err, errno, sys_name, evt_name); |
89812fc8 | 458 | return -1; |
bcd3279f FW |
459 | } |
460 | ||
89812fc8 | 461 | while (!ret && (evt_ent = readdir(evt_dir))) { |
bcd3279f FW |
462 | if (!strcmp(evt_ent->d_name, ".") |
463 | || !strcmp(evt_ent->d_name, "..") | |
464 | || !strcmp(evt_ent->d_name, "enable") | |
465 | || !strcmp(evt_ent->d_name, "filter")) | |
466 | continue; | |
467 | ||
89812fc8 | 468 | if (!strglobmatch(evt_ent->d_name, evt_name)) |
fb1d2edf MH |
469 | continue; |
470 | ||
e637d177 | 471 | ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name, |
272ed29a | 472 | err, head_config); |
bcd3279f FW |
473 | } |
474 | ||
0bd3f084 | 475 | closedir(evt_dir); |
89812fc8 | 476 | return ret; |
bcd3279f FW |
477 | } |
478 | ||
c5cd8ac0 | 479 | static int add_tracepoint_event(struct list_head *list, int *idx, |
e2f9f8ea | 480 | char *sys_name, char *evt_name, |
272ed29a | 481 | struct parse_events_error *err, |
e637d177 | 482 | struct list_head *head_config) |
f35488f9 JO |
483 | { |
484 | return strpbrk(evt_name, "*?") ? | |
e637d177 | 485 | add_tracepoint_multi_event(list, idx, sys_name, evt_name, |
272ed29a | 486 | err, head_config) : |
e637d177 | 487 | add_tracepoint(list, idx, sys_name, evt_name, |
272ed29a | 488 | err, head_config); |
f35488f9 JO |
489 | } |
490 | ||
c5cd8ac0 | 491 | static int add_tracepoint_multi_sys(struct list_head *list, int *idx, |
e2f9f8ea | 492 | char *sys_name, char *evt_name, |
272ed29a | 493 | struct parse_events_error *err, |
e637d177 | 494 | struct list_head *head_config) |
f35488f9 JO |
495 | { |
496 | struct dirent *events_ent; | |
497 | DIR *events_dir; | |
498 | int ret = 0; | |
499 | ||
500 | events_dir = opendir(tracing_events_path); | |
501 | if (!events_dir) { | |
272ed29a | 502 | tracepoint_error(err, errno, sys_name, evt_name); |
f35488f9 JO |
503 | return -1; |
504 | } | |
505 | ||
506 | while (!ret && (events_ent = readdir(events_dir))) { | |
507 | if (!strcmp(events_ent->d_name, ".") | |
508 | || !strcmp(events_ent->d_name, "..") | |
509 | || !strcmp(events_ent->d_name, "enable") | |
510 | || !strcmp(events_ent->d_name, "header_event") | |
511 | || !strcmp(events_ent->d_name, "header_page")) | |
512 | continue; | |
513 | ||
514 | if (!strglobmatch(events_ent->d_name, sys_name)) | |
515 | continue; | |
516 | ||
517 | ret = add_tracepoint_event(list, idx, events_ent->d_name, | |
272ed29a | 518 | evt_name, err, head_config); |
f35488f9 JO |
519 | } |
520 | ||
521 | closedir(events_dir); | |
522 | return ret; | |
523 | } | |
524 | ||
89812fc8 JO |
525 | static int |
526 | parse_breakpoint_type(const char *type, struct perf_event_attr *attr) | |
1b290d67 FW |
527 | { |
528 | int i; | |
529 | ||
530 | for (i = 0; i < 3; i++) { | |
89812fc8 | 531 | if (!type || !type[i]) |
1b290d67 FW |
532 | break; |
533 | ||
7582732f JO |
534 | #define CHECK_SET_TYPE(bit) \ |
535 | do { \ | |
536 | if (attr->bp_type & bit) \ | |
537 | return -EINVAL; \ | |
538 | else \ | |
539 | attr->bp_type |= bit; \ | |
540 | } while (0) | |
541 | ||
1b290d67 FW |
542 | switch (type[i]) { |
543 | case 'r': | |
7582732f | 544 | CHECK_SET_TYPE(HW_BREAKPOINT_R); |
1b290d67 FW |
545 | break; |
546 | case 'w': | |
7582732f | 547 | CHECK_SET_TYPE(HW_BREAKPOINT_W); |
1b290d67 FW |
548 | break; |
549 | case 'x': | |
7582732f | 550 | CHECK_SET_TYPE(HW_BREAKPOINT_X); |
1b290d67 FW |
551 | break; |
552 | default: | |
89812fc8 | 553 | return -EINVAL; |
1b290d67 FW |
554 | } |
555 | } | |
89812fc8 | 556 | |
7582732f JO |
557 | #undef CHECK_SET_TYPE |
558 | ||
1b290d67 FW |
559 | if (!attr->bp_type) /* Default */ |
560 | attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W; | |
561 | ||
89812fc8 | 562 | return 0; |
1b290d67 FW |
563 | } |
564 | ||
c5cd8ac0 | 565 | int parse_events_add_breakpoint(struct list_head *list, int *idx, |
3741eb9f | 566 | void *ptr, char *type, u64 len) |
1b290d67 | 567 | { |
89812fc8 | 568 | struct perf_event_attr attr; |
1b290d67 | 569 | |
89812fc8 | 570 | memset(&attr, 0, sizeof(attr)); |
9fafd98f | 571 | attr.bp_addr = (unsigned long) ptr; |
1b290d67 | 572 | |
89812fc8 JO |
573 | if (parse_breakpoint_type(type, &attr)) |
574 | return -EINVAL; | |
1b290d67 | 575 | |
3741eb9f JS |
576 | /* Provide some defaults if len is not specified */ |
577 | if (!len) { | |
578 | if (attr.bp_type == HW_BREAKPOINT_X) | |
579 | len = sizeof(long); | |
580 | else | |
581 | len = HW_BREAKPOINT_LEN_4; | |
582 | } | |
583 | ||
584 | attr.bp_len = len; | |
61c45981 | 585 | |
89812fc8 | 586 | attr.type = PERF_TYPE_BREAKPOINT; |
4a841d65 | 587 | attr.sample_period = 1; |
b908debd | 588 | |
930a2e29 | 589 | return add_event(list, idx, &attr, NULL, NULL); |
74d5b588 JSR |
590 | } |
591 | ||
3b0e371c JO |
592 | static int check_type_val(struct parse_events_term *term, |
593 | struct parse_events_error *err, | |
594 | int type) | |
595 | { | |
596 | if (type == term->type_val) | |
597 | return 0; | |
598 | ||
599 | if (err) { | |
600 | err->idx = term->err_val; | |
601 | if (type == PARSE_EVENTS__TERM_TYPE_NUM) | |
602 | err->str = strdup("expected numeric value"); | |
603 | else | |
604 | err->str = strdup("expected string value"); | |
605 | } | |
606 | return -EINVAL; | |
607 | } | |
608 | ||
0b8891a8 HK |
609 | typedef int config_term_func_t(struct perf_event_attr *attr, |
610 | struct parse_events_term *term, | |
611 | struct parse_events_error *err); | |
612 | ||
613 | static int config_term_common(struct perf_event_attr *attr, | |
614 | struct parse_events_term *term, | |
615 | struct parse_events_error *err) | |
8f707d84 | 616 | { |
3b0e371c JO |
617 | #define CHECK_TYPE_VAL(type) \ |
618 | do { \ | |
619 | if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \ | |
620 | return -EINVAL; \ | |
16fa7e82 JO |
621 | } while (0) |
622 | ||
623 | switch (term->type_term) { | |
8f707d84 | 624 | case PARSE_EVENTS__TERM_TYPE_CONFIG: |
16fa7e82 | 625 | CHECK_TYPE_VAL(NUM); |
8f707d84 JO |
626 | attr->config = term->val.num; |
627 | break; | |
628 | case PARSE_EVENTS__TERM_TYPE_CONFIG1: | |
16fa7e82 | 629 | CHECK_TYPE_VAL(NUM); |
8f707d84 JO |
630 | attr->config1 = term->val.num; |
631 | break; | |
632 | case PARSE_EVENTS__TERM_TYPE_CONFIG2: | |
16fa7e82 | 633 | CHECK_TYPE_VAL(NUM); |
8f707d84 JO |
634 | attr->config2 = term->val.num; |
635 | break; | |
636 | case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: | |
16fa7e82 | 637 | CHECK_TYPE_VAL(NUM); |
8f707d84 | 638 | break; |
09af2a55 NK |
639 | case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: |
640 | CHECK_TYPE_VAL(NUM); | |
641 | break; | |
8f707d84 JO |
642 | case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: |
643 | /* | |
644 | * TODO uncomment when the field is available | |
645 | * attr->branch_sample_type = term->val.num; | |
646 | */ | |
647 | break; | |
32067712 KL |
648 | case PARSE_EVENTS__TERM_TYPE_TIME: |
649 | CHECK_TYPE_VAL(NUM); | |
650 | if (term->val.num > 1) { | |
651 | err->str = strdup("expected 0 or 1"); | |
652 | err->idx = term->err_val; | |
653 | return -EINVAL; | |
654 | } | |
655 | break; | |
d457c963 KL |
656 | case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: |
657 | CHECK_TYPE_VAL(STR); | |
658 | break; | |
659 | case PARSE_EVENTS__TERM_TYPE_STACKSIZE: | |
660 | CHECK_TYPE_VAL(NUM); | |
661 | break; | |
6b5fc39b JO |
662 | case PARSE_EVENTS__TERM_TYPE_NAME: |
663 | CHECK_TYPE_VAL(STR); | |
664 | break; | |
8f707d84 | 665 | default: |
ffeb883e HK |
666 | err->str = strdup("unknown term"); |
667 | err->idx = term->err_term; | |
668 | err->help = parse_events_formats_error_string(NULL); | |
8f707d84 JO |
669 | return -EINVAL; |
670 | } | |
16fa7e82 | 671 | |
8f707d84 | 672 | return 0; |
16fa7e82 | 673 | #undef CHECK_TYPE_VAL |
8f707d84 JO |
674 | } |
675 | ||
0b8891a8 HK |
676 | static int config_term_pmu(struct perf_event_attr *attr, |
677 | struct parse_events_term *term, | |
678 | struct parse_events_error *err) | |
679 | { | |
680 | if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER) | |
681 | /* | |
682 | * Always succeed for sysfs terms, as we dont know | |
683 | * at this point what type they need to have. | |
684 | */ | |
685 | return 0; | |
686 | else | |
687 | return config_term_common(attr, term, err); | |
688 | } | |
689 | ||
e637d177 HK |
690 | static int config_term_tracepoint(struct perf_event_attr *attr, |
691 | struct parse_events_term *term, | |
692 | struct parse_events_error *err) | |
693 | { | |
694 | switch (term->type_term) { | |
695 | case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: | |
696 | case PARSE_EVENTS__TERM_TYPE_STACKSIZE: | |
697 | return config_term_common(attr, term, err); | |
698 | default: | |
699 | if (err) { | |
700 | err->idx = term->err_term; | |
701 | err->str = strdup("unknown term"); | |
702 | err->help = strdup("valid terms: call-graph,stack-size\n"); | |
703 | } | |
704 | return -EINVAL; | |
705 | } | |
706 | ||
707 | return 0; | |
708 | } | |
709 | ||
8f707d84 | 710 | static int config_attr(struct perf_event_attr *attr, |
3b0e371c | 711 | struct list_head *head, |
0b8891a8 HK |
712 | struct parse_events_error *err, |
713 | config_term_func_t config_term) | |
8f707d84 | 714 | { |
6cee6cd3 | 715 | struct parse_events_term *term; |
8f707d84 JO |
716 | |
717 | list_for_each_entry(term, head, list) | |
3b0e371c | 718 | if (config_term(attr, term, err)) |
8f707d84 JO |
719 | return -EINVAL; |
720 | ||
721 | return 0; | |
722 | } | |
723 | ||
930a2e29 JO |
724 | static int get_config_terms(struct list_head *head_config, |
725 | struct list_head *head_terms __maybe_unused) | |
726 | { | |
727 | #define ADD_CONFIG_TERM(__type, __name, __val) \ | |
728 | do { \ | |
729 | struct perf_evsel_config_term *__t; \ | |
730 | \ | |
731 | __t = zalloc(sizeof(*__t)); \ | |
732 | if (!__t) \ | |
733 | return -ENOMEM; \ | |
734 | \ | |
735 | INIT_LIST_HEAD(&__t->list); \ | |
736 | __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \ | |
737 | __t->val.__name = __val; \ | |
738 | list_add_tail(&__t->list, head_terms); \ | |
739 | } while (0) | |
740 | ||
741 | struct parse_events_term *term; | |
742 | ||
743 | list_for_each_entry(term, head_config, list) { | |
744 | switch (term->type_term) { | |
ee4c7588 JO |
745 | case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: |
746 | ADD_CONFIG_TERM(PERIOD, period, term->val.num); | |
32067712 | 747 | break; |
09af2a55 NK |
748 | case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: |
749 | ADD_CONFIG_TERM(FREQ, freq, term->val.num); | |
750 | break; | |
32067712 KL |
751 | case PARSE_EVENTS__TERM_TYPE_TIME: |
752 | ADD_CONFIG_TERM(TIME, time, term->val.num); | |
753 | break; | |
d457c963 KL |
754 | case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: |
755 | ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str); | |
756 | break; | |
757 | case PARSE_EVENTS__TERM_TYPE_STACKSIZE: | |
758 | ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num); | |
759 | break; | |
930a2e29 JO |
760 | default: |
761 | break; | |
762 | } | |
763 | } | |
764 | #undef ADD_EVSEL_CONFIG | |
765 | return 0; | |
766 | } | |
767 | ||
e637d177 HK |
768 | int parse_events_add_tracepoint(struct list_head *list, int *idx, |
769 | char *sys, char *event, | |
272ed29a | 770 | struct parse_events_error *err, |
e637d177 HK |
771 | struct list_head *head_config) |
772 | { | |
773 | if (head_config) { | |
774 | struct perf_event_attr attr; | |
775 | ||
272ed29a | 776 | if (config_attr(&attr, head_config, err, |
e637d177 HK |
777 | config_term_tracepoint)) |
778 | return -EINVAL; | |
779 | } | |
780 | ||
781 | if (strpbrk(sys, "*?")) | |
782 | return add_tracepoint_multi_sys(list, idx, sys, event, | |
272ed29a | 783 | err, head_config); |
e637d177 HK |
784 | else |
785 | return add_tracepoint_event(list, idx, sys, event, | |
272ed29a | 786 | err, head_config); |
e637d177 HK |
787 | } |
788 | ||
87d650be JO |
789 | int parse_events_add_numeric(struct parse_events_evlist *data, |
790 | struct list_head *list, | |
b527bab5 | 791 | u32 type, u64 config, |
8f707d84 | 792 | struct list_head *head_config) |
8ad8db37 | 793 | { |
89812fc8 | 794 | struct perf_event_attr attr; |
930a2e29 | 795 | LIST_HEAD(config_terms); |
61c45981 | 796 | |
89812fc8 JO |
797 | memset(&attr, 0, sizeof(attr)); |
798 | attr.type = type; | |
799 | attr.config = config; | |
8f707d84 | 800 | |
930a2e29 | 801 | if (head_config) { |
0b8891a8 HK |
802 | if (config_attr(&attr, head_config, data->error, |
803 | config_term_common)) | |
930a2e29 JO |
804 | return -EINVAL; |
805 | ||
806 | if (get_config_terms(head_config, &config_terms)) | |
807 | return -ENOMEM; | |
808 | } | |
8f707d84 | 809 | |
930a2e29 | 810 | return add_event(list, &data->idx, &attr, NULL, &config_terms); |
61c45981 | 811 | } |
8ad8db37 | 812 | |
6cee6cd3 | 813 | static int parse_events__is_name_term(struct parse_events_term *term) |
6b5fc39b JO |
814 | { |
815 | return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME; | |
816 | } | |
817 | ||
9db1763c | 818 | static char *pmu_event_name(struct list_head *head_terms) |
6b5fc39b | 819 | { |
6cee6cd3 | 820 | struct parse_events_term *term; |
6b5fc39b JO |
821 | |
822 | list_for_each_entry(term, head_terms, list) | |
823 | if (parse_events__is_name_term(term)) | |
824 | return term->val.str; | |
825 | ||
9db1763c | 826 | return NULL; |
6b5fc39b JO |
827 | } |
828 | ||
36adec85 JO |
829 | int parse_events_add_pmu(struct parse_events_evlist *data, |
830 | struct list_head *list, char *name, | |
831 | struct list_head *head_config) | |
5f537a26 JO |
832 | { |
833 | struct perf_event_attr attr; | |
46441bdc | 834 | struct perf_pmu_info info; |
5f537a26 | 835 | struct perf_pmu *pmu; |
410136f5 | 836 | struct perf_evsel *evsel; |
930a2e29 | 837 | LIST_HEAD(config_terms); |
5f537a26 JO |
838 | |
839 | pmu = perf_pmu__find(name); | |
840 | if (!pmu) | |
841 | return -EINVAL; | |
842 | ||
dc0a6202 AH |
843 | if (pmu->default_config) { |
844 | memcpy(&attr, pmu->default_config, | |
845 | sizeof(struct perf_event_attr)); | |
846 | } else { | |
847 | memset(&attr, 0, sizeof(attr)); | |
848 | } | |
5f537a26 | 849 | |
ad962273 AH |
850 | if (!head_config) { |
851 | attr.type = pmu->type; | |
930a2e29 | 852 | evsel = __add_event(list, &data->idx, &attr, NULL, pmu->cpus, NULL); |
ad962273 AH |
853 | return evsel ? 0 : -ENOMEM; |
854 | } | |
855 | ||
46441bdc | 856 | if (perf_pmu__check_alias(pmu, head_config, &info)) |
a6146d50 ZY |
857 | return -EINVAL; |
858 | ||
5f537a26 JO |
859 | /* |
860 | * Configure hardcoded terms first, no need to check | |
861 | * return value when called with fail == 0 ;) | |
862 | */ | |
0b8891a8 | 863 | if (config_attr(&attr, head_config, data->error, config_term_pmu)) |
c056ba6a | 864 | return -EINVAL; |
5f537a26 | 865 | |
930a2e29 JO |
866 | if (get_config_terms(head_config, &config_terms)) |
867 | return -ENOMEM; | |
868 | ||
e64b020b | 869 | if (perf_pmu__config(pmu, &attr, head_config, data->error)) |
5f537a26 JO |
870 | return -EINVAL; |
871 | ||
36adec85 | 872 | evsel = __add_event(list, &data->idx, &attr, |
930a2e29 JO |
873 | pmu_event_name(head_config), pmu->cpus, |
874 | &config_terms); | |
410136f5 | 875 | if (evsel) { |
46441bdc MF |
876 | evsel->unit = info.unit; |
877 | evsel->scale = info.scale; | |
044330c1 | 878 | evsel->per_pkg = info.per_pkg; |
1d9e446b | 879 | evsel->snapshot = info.snapshot; |
410136f5 SE |
880 | } |
881 | ||
882 | return evsel ? 0 : -ENOMEM; | |
5f537a26 JO |
883 | } |
884 | ||
6a4bb04c JO |
885 | int parse_events__modifier_group(struct list_head *list, |
886 | char *event_mod) | |
89efb029 | 887 | { |
6a4bb04c JO |
888 | return parse_events__modifier_event(list, event_mod, true); |
889 | } | |
890 | ||
63dab225 | 891 | void parse_events__set_leader(char *name, struct list_head *list) |
6a4bb04c JO |
892 | { |
893 | struct perf_evsel *leader; | |
894 | ||
854f7363 WN |
895 | if (list_empty(list)) { |
896 | WARN_ONCE(true, "WARNING: failed to set leader: empty list"); | |
897 | return; | |
898 | } | |
899 | ||
63dab225 ACM |
900 | __perf_evlist__set_leader(list); |
901 | leader = list_entry(list->next, struct perf_evsel, node); | |
6a4bb04c | 902 | leader->group_name = name ? strdup(name) : NULL; |
89efb029 JO |
903 | } |
904 | ||
c5cd8ac0 | 905 | /* list_event is assumed to point to malloc'ed memory */ |
5d7be90e JO |
906 | void parse_events_update_lists(struct list_head *list_event, |
907 | struct list_head *list_all) | |
908 | { | |
909 | /* | |
910 | * Called for single event definition. Update the | |
89efb029 | 911 | * 'all event' list, and reinit the 'single event' |
5d7be90e JO |
912 | * list, for next event definition. |
913 | */ | |
914 | list_splice_tail(list_event, list_all); | |
b847cbdc | 915 | free(list_event); |
5d7be90e JO |
916 | } |
917 | ||
f5b1135b JO |
918 | struct event_modifier { |
919 | int eu; | |
920 | int ek; | |
921 | int eh; | |
922 | int eH; | |
923 | int eG; | |
a1e12da4 | 924 | int eI; |
f5b1135b | 925 | int precise; |
7f94af7a | 926 | int precise_max; |
f5b1135b | 927 | int exclude_GH; |
3c176311 | 928 | int sample_read; |
e9a7c414 | 929 | int pinned; |
f5b1135b JO |
930 | }; |
931 | ||
932 | static int get_event_modifier(struct event_modifier *mod, char *str, | |
933 | struct perf_evsel *evsel) | |
61c45981 | 934 | { |
f5b1135b JO |
935 | int eu = evsel ? evsel->attr.exclude_user : 0; |
936 | int ek = evsel ? evsel->attr.exclude_kernel : 0; | |
937 | int eh = evsel ? evsel->attr.exclude_hv : 0; | |
938 | int eH = evsel ? evsel->attr.exclude_host : 0; | |
939 | int eG = evsel ? evsel->attr.exclude_guest : 0; | |
a1e12da4 | 940 | int eI = evsel ? evsel->attr.exclude_idle : 0; |
f5b1135b | 941 | int precise = evsel ? evsel->attr.precise_ip : 0; |
7f94af7a | 942 | int precise_max = 0; |
3c176311 | 943 | int sample_read = 0; |
e9a7c414 | 944 | int pinned = evsel ? evsel->attr.pinned : 0; |
a21ca2ca | 945 | |
f5b1135b JO |
946 | int exclude = eu | ek | eh; |
947 | int exclude_GH = evsel ? evsel->exclude_GH : 0; | |
948 | ||
f5b1135b | 949 | memset(mod, 0, sizeof(*mod)); |
ceb53fbf | 950 | |
61c45981 | 951 | while (*str) { |
ab608344 PZ |
952 | if (*str == 'u') { |
953 | if (!exclude) | |
954 | exclude = eu = ek = eh = 1; | |
61c45981 | 955 | eu = 0; |
ab608344 PZ |
956 | } else if (*str == 'k') { |
957 | if (!exclude) | |
958 | exclude = eu = ek = eh = 1; | |
61c45981 | 959 | ek = 0; |
ab608344 PZ |
960 | } else if (*str == 'h') { |
961 | if (!exclude) | |
962 | exclude = eu = ek = eh = 1; | |
61c45981 | 963 | eh = 0; |
99320cc8 JR |
964 | } else if (*str == 'G') { |
965 | if (!exclude_GH) | |
966 | exclude_GH = eG = eH = 1; | |
967 | eG = 0; | |
968 | } else if (*str == 'H') { | |
969 | if (!exclude_GH) | |
970 | exclude_GH = eG = eH = 1; | |
971 | eH = 0; | |
a1e12da4 JO |
972 | } else if (*str == 'I') { |
973 | eI = 1; | |
ab608344 PZ |
974 | } else if (*str == 'p') { |
975 | precise++; | |
1342798c DA |
976 | /* use of precise requires exclude_guest */ |
977 | if (!exclude_GH) | |
978 | eG = 1; | |
7f94af7a JO |
979 | } else if (*str == 'P') { |
980 | precise_max = 1; | |
3c176311 JO |
981 | } else if (*str == 'S') { |
982 | sample_read = 1; | |
e9a7c414 ME |
983 | } else if (*str == 'D') { |
984 | pinned = 1; | |
ab608344 | 985 | } else |
61c45981 | 986 | break; |
ab608344 | 987 | |
61c45981 | 988 | ++str; |
5242519b | 989 | } |
ceb53fbf | 990 | |
89812fc8 JO |
991 | /* |
992 | * precise ip: | |
993 | * | |
994 | * 0 - SAMPLE_IP can have arbitrary skid | |
995 | * 1 - SAMPLE_IP must have constant skid | |
996 | * 2 - SAMPLE_IP requested to have 0 skid | |
997 | * 3 - SAMPLE_IP must have 0 skid | |
998 | * | |
999 | * See also PERF_RECORD_MISC_EXACT_IP | |
1000 | */ | |
1001 | if (precise > 3) | |
1002 | return -EINVAL; | |
ceb53fbf | 1003 | |
f5b1135b JO |
1004 | mod->eu = eu; |
1005 | mod->ek = ek; | |
1006 | mod->eh = eh; | |
1007 | mod->eH = eH; | |
1008 | mod->eG = eG; | |
a1e12da4 | 1009 | mod->eI = eI; |
f5b1135b | 1010 | mod->precise = precise; |
7f94af7a | 1011 | mod->precise_max = precise_max; |
f5b1135b | 1012 | mod->exclude_GH = exclude_GH; |
3c176311 | 1013 | mod->sample_read = sample_read; |
e9a7c414 ME |
1014 | mod->pinned = pinned; |
1015 | ||
f5b1135b JO |
1016 | return 0; |
1017 | } | |
1018 | ||
534123f4 JO |
1019 | /* |
1020 | * Basic modifier sanity check to validate it contains only one | |
1021 | * instance of any modifier (apart from 'p') present. | |
1022 | */ | |
1023 | static int check_modifier(char *str) | |
1024 | { | |
1025 | char *p = str; | |
1026 | ||
1027 | /* The sizeof includes 0 byte as well. */ | |
7f94af7a | 1028 | if (strlen(str) > (sizeof("ukhGHpppPSDI") - 1)) |
534123f4 JO |
1029 | return -1; |
1030 | ||
1031 | while (*p) { | |
1032 | if (*p != 'p' && strchr(p + 1, *p)) | |
1033 | return -1; | |
1034 | p++; | |
1035 | } | |
1036 | ||
1037 | return 0; | |
1038 | } | |
1039 | ||
f5b1135b JO |
1040 | int parse_events__modifier_event(struct list_head *list, char *str, bool add) |
1041 | { | |
1042 | struct perf_evsel *evsel; | |
1043 | struct event_modifier mod; | |
1044 | ||
1045 | if (str == NULL) | |
1046 | return 0; | |
1047 | ||
534123f4 JO |
1048 | if (check_modifier(str)) |
1049 | return -EINVAL; | |
1050 | ||
f5b1135b JO |
1051 | if (!add && get_event_modifier(&mod, str, NULL)) |
1052 | return -EINVAL; | |
1053 | ||
0050f7aa | 1054 | __evlist__for_each(list, evsel) { |
f5b1135b JO |
1055 | if (add && get_event_modifier(&mod, str, evsel)) |
1056 | return -EINVAL; | |
1057 | ||
1058 | evsel->attr.exclude_user = mod.eu; | |
1059 | evsel->attr.exclude_kernel = mod.ek; | |
1060 | evsel->attr.exclude_hv = mod.eh; | |
1061 | evsel->attr.precise_ip = mod.precise; | |
1062 | evsel->attr.exclude_host = mod.eH; | |
1063 | evsel->attr.exclude_guest = mod.eG; | |
a1e12da4 | 1064 | evsel->attr.exclude_idle = mod.eI; |
f5b1135b | 1065 | evsel->exclude_GH = mod.exclude_GH; |
3c176311 | 1066 | evsel->sample_read = mod.sample_read; |
7f94af7a | 1067 | evsel->precise_max = mod.precise_max; |
e9a7c414 ME |
1068 | |
1069 | if (perf_evsel__is_group_leader(evsel)) | |
1070 | evsel->attr.pinned = mod.pinned; | |
89812fc8 | 1071 | } |
ceb53fbf | 1072 | |
61c45981 PM |
1073 | return 0; |
1074 | } | |
8ad8db37 | 1075 | |
ac2ba9f3 RR |
1076 | int parse_events_name(struct list_head *list, char *name) |
1077 | { | |
1078 | struct perf_evsel *evsel; | |
1079 | ||
0050f7aa | 1080 | __evlist__for_each(list, evsel) { |
ac2ba9f3 RR |
1081 | if (!evsel->name) |
1082 | evsel->name = strdup(name); | |
1083 | } | |
1084 | ||
1085 | return 0; | |
1086 | } | |
1087 | ||
dcb4e102 KL |
1088 | static int |
1089 | comp_pmu(const void *p1, const void *p2) | |
1090 | { | |
1091 | struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1; | |
1092 | struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2; | |
1093 | ||
1094 | return strcmp(pmu1->symbol, pmu2->symbol); | |
1095 | } | |
1096 | ||
1097 | static void perf_pmu__parse_cleanup(void) | |
1098 | { | |
1099 | if (perf_pmu_events_list_num > 0) { | |
1100 | struct perf_pmu_event_symbol *p; | |
1101 | int i; | |
1102 | ||
1103 | for (i = 0; i < perf_pmu_events_list_num; i++) { | |
1104 | p = perf_pmu_events_list + i; | |
1105 | free(p->symbol); | |
1106 | } | |
1107 | free(perf_pmu_events_list); | |
1108 | perf_pmu_events_list = NULL; | |
1109 | perf_pmu_events_list_num = 0; | |
1110 | } | |
1111 | } | |
1112 | ||
1113 | #define SET_SYMBOL(str, stype) \ | |
1114 | do { \ | |
1115 | p->symbol = str; \ | |
1116 | if (!p->symbol) \ | |
1117 | goto err; \ | |
1118 | p->type = stype; \ | |
1119 | } while (0) | |
1120 | ||
1121 | /* | |
1122 | * Read the pmu events list from sysfs | |
1123 | * Save it into perf_pmu_events_list | |
1124 | */ | |
1125 | static void perf_pmu__parse_init(void) | |
1126 | { | |
1127 | ||
1128 | struct perf_pmu *pmu = NULL; | |
1129 | struct perf_pmu_alias *alias; | |
1130 | int len = 0; | |
1131 | ||
1132 | pmu = perf_pmu__find("cpu"); | |
1133 | if ((pmu == NULL) || list_empty(&pmu->aliases)) { | |
1134 | perf_pmu_events_list_num = -1; | |
1135 | return; | |
1136 | } | |
1137 | list_for_each_entry(alias, &pmu->aliases, list) { | |
1138 | if (strchr(alias->name, '-')) | |
1139 | len++; | |
1140 | len++; | |
1141 | } | |
1142 | perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len); | |
1143 | if (!perf_pmu_events_list) | |
1144 | return; | |
1145 | perf_pmu_events_list_num = len; | |
1146 | ||
1147 | len = 0; | |
1148 | list_for_each_entry(alias, &pmu->aliases, list) { | |
1149 | struct perf_pmu_event_symbol *p = perf_pmu_events_list + len; | |
1150 | char *tmp = strchr(alias->name, '-'); | |
1151 | ||
1152 | if (tmp != NULL) { | |
1153 | SET_SYMBOL(strndup(alias->name, tmp - alias->name), | |
1154 | PMU_EVENT_SYMBOL_PREFIX); | |
1155 | p++; | |
1156 | SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX); | |
1157 | len += 2; | |
1158 | } else { | |
1159 | SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL); | |
1160 | len++; | |
1161 | } | |
1162 | } | |
1163 | qsort(perf_pmu_events_list, len, | |
1164 | sizeof(struct perf_pmu_event_symbol), comp_pmu); | |
1165 | ||
1166 | return; | |
1167 | err: | |
1168 | perf_pmu__parse_cleanup(); | |
1169 | } | |
1170 | ||
1171 | enum perf_pmu_event_symbol_type | |
1172 | perf_pmu__parse_check(const char *name) | |
1173 | { | |
1174 | struct perf_pmu_event_symbol p, *r; | |
1175 | ||
1176 | /* scan kernel pmu events from sysfs if needed */ | |
1177 | if (perf_pmu_events_list_num == 0) | |
1178 | perf_pmu__parse_init(); | |
1179 | /* | |
1180 | * name "cpu" could be prefix of cpu-cycles or cpu// events. | |
1181 | * cpu-cycles has been handled by hardcode. | |
1182 | * So it must be cpu// events, not kernel pmu event. | |
1183 | */ | |
1184 | if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu")) | |
1185 | return PMU_EVENT_SYMBOL_ERR; | |
1186 | ||
1187 | p.symbol = strdup(name); | |
1188 | r = bsearch(&p, perf_pmu_events_list, | |
1189 | (size_t) perf_pmu_events_list_num, | |
1190 | sizeof(struct perf_pmu_event_symbol), comp_pmu); | |
1191 | free(p.symbol); | |
1192 | return r ? r->type : PMU_EVENT_SYMBOL_ERR; | |
1193 | } | |
1194 | ||
90e2b22d | 1195 | static int parse_events__scanner(const char *str, void *data, int start_token) |
61c45981 | 1196 | { |
89812fc8 | 1197 | YY_BUFFER_STATE buffer; |
ac20de6f | 1198 | void *scanner; |
46010ab2 | 1199 | int ret; |
bcd3279f | 1200 | |
90e2b22d | 1201 | ret = parse_events_lex_init_extra(start_token, &scanner); |
ac20de6f ZY |
1202 | if (ret) |
1203 | return ret; | |
1204 | ||
1205 | buffer = parse_events__scan_string(str, scanner); | |
a21ca2ca | 1206 | |
82ba1f2f JO |
1207 | #ifdef PARSER_DEBUG |
1208 | parse_events_debug = 1; | |
1209 | #endif | |
ac20de6f ZY |
1210 | ret = parse_events_parse(data, scanner); |
1211 | ||
1212 | parse_events__flush_buffer(buffer, scanner); | |
1213 | parse_events__delete_buffer(buffer, scanner); | |
1214 | parse_events_lex_destroy(scanner); | |
1215 | return ret; | |
1216 | } | |
bcd3279f | 1217 | |
90e2b22d JO |
1218 | /* |
1219 | * parse event config string, return a list of event terms. | |
1220 | */ | |
1221 | int parse_events_terms(struct list_head *terms, const char *str) | |
1222 | { | |
23b6339b | 1223 | struct parse_events_terms data = { |
90e2b22d JO |
1224 | .terms = NULL, |
1225 | }; | |
1226 | int ret; | |
1227 | ||
1228 | ret = parse_events__scanner(str, &data, PE_START_TERMS); | |
1229 | if (!ret) { | |
1230 | list_splice(data.terms, terms); | |
74cf249d | 1231 | zfree(&data.terms); |
90e2b22d JO |
1232 | return 0; |
1233 | } | |
1234 | ||
b2c34fde AH |
1235 | if (data.terms) |
1236 | parse_events__free_terms(data.terms); | |
90e2b22d JO |
1237 | return ret; |
1238 | } | |
1239 | ||
b39b8393 JO |
1240 | int parse_events(struct perf_evlist *evlist, const char *str, |
1241 | struct parse_events_error *err) | |
ac20de6f | 1242 | { |
23b6339b | 1243 | struct parse_events_evlist data = { |
b39b8393 JO |
1244 | .list = LIST_HEAD_INIT(data.list), |
1245 | .idx = evlist->nr_entries, | |
1246 | .error = err, | |
ac20de6f ZY |
1247 | }; |
1248 | int ret; | |
bcd3279f | 1249 | |
90e2b22d | 1250 | ret = parse_events__scanner(str, &data, PE_START_EVENTS); |
dcb4e102 | 1251 | perf_pmu__parse_cleanup(); |
89812fc8 | 1252 | if (!ret) { |
15bfd2cc WN |
1253 | struct perf_evsel *last; |
1254 | ||
854f7363 WN |
1255 | if (list_empty(&data.list)) { |
1256 | WARN_ONCE(true, "WARNING: event parser found nothing"); | |
1257 | return -1; | |
1258 | } | |
1259 | ||
f114d6ef | 1260 | perf_evlist__splice_list_tail(evlist, &data.list); |
97f63e4a | 1261 | evlist->nr_groups += data.nr_groups; |
15bfd2cc WN |
1262 | last = perf_evlist__last(evlist); |
1263 | last->cmdline_group_boundary = true; | |
1264 | ||
89812fc8 JO |
1265 | return 0; |
1266 | } | |
bcd3279f | 1267 | |
5d7be90e JO |
1268 | /* |
1269 | * There are 2 users - builtin-record and builtin-test objects. | |
1270 | * Both call perf_evlist__delete in case of error, so we dont | |
1271 | * need to bother. | |
1272 | */ | |
bcd3279f | 1273 | return ret; |
8ad8db37 IM |
1274 | } |
1275 | ||
b39b8393 JO |
1276 | #define MAX_WIDTH 1000 |
1277 | static int get_term_width(void) | |
1278 | { | |
1279 | struct winsize ws; | |
1280 | ||
1281 | get_term_dimensions(&ws); | |
1282 | return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col; | |
1283 | } | |
1284 | ||
1285 | static void parse_events_print_error(struct parse_events_error *err, | |
1286 | const char *event) | |
1287 | { | |
1288 | const char *str = "invalid or unsupported event: "; | |
1289 | char _buf[MAX_WIDTH]; | |
1290 | char *buf = (char *) event; | |
1291 | int idx = 0; | |
1292 | ||
1293 | if (err->str) { | |
1294 | /* -2 for extra '' in the final fprintf */ | |
1295 | int width = get_term_width() - 2; | |
1296 | int len_event = strlen(event); | |
1297 | int len_str, max_len, cut = 0; | |
1298 | ||
1299 | /* | |
1300 | * Maximum error index indent, we will cut | |
1301 | * the event string if it's bigger. | |
1302 | */ | |
141b2d31 | 1303 | int max_err_idx = 13; |
b39b8393 JO |
1304 | |
1305 | /* | |
1306 | * Let's be specific with the message when | |
1307 | * we have the precise error. | |
1308 | */ | |
1309 | str = "event syntax error: "; | |
1310 | len_str = strlen(str); | |
1311 | max_len = width - len_str; | |
1312 | ||
1313 | buf = _buf; | |
1314 | ||
1315 | /* We're cutting from the beggining. */ | |
1316 | if (err->idx > max_err_idx) | |
1317 | cut = err->idx - max_err_idx; | |
1318 | ||
1319 | strncpy(buf, event + cut, max_len); | |
1320 | ||
1321 | /* Mark cut parts with '..' on both sides. */ | |
1322 | if (cut) | |
1323 | buf[0] = buf[1] = '.'; | |
1324 | ||
1325 | if ((len_event - cut) > max_len) { | |
1326 | buf[max_len - 1] = buf[max_len - 2] = '.'; | |
1327 | buf[max_len] = 0; | |
1328 | } | |
1329 | ||
1330 | idx = len_str + err->idx - cut; | |
1331 | } | |
1332 | ||
1333 | fprintf(stderr, "%s'%s'\n", str, buf); | |
1334 | if (idx) { | |
1335 | fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str); | |
1336 | if (err->help) | |
1337 | fprintf(stderr, "\n%s\n", err->help); | |
1338 | free(err->str); | |
1339 | free(err->help); | |
1340 | } | |
1341 | ||
1342 | fprintf(stderr, "Run 'perf list' for a list of valid events\n"); | |
1343 | } | |
1344 | ||
1345 | #undef MAX_WIDTH | |
1346 | ||
f120f9d5 | 1347 | int parse_events_option(const struct option *opt, const char *str, |
1d037ca1 | 1348 | int unset __maybe_unused) |
f120f9d5 JO |
1349 | { |
1350 | struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; | |
b39b8393 JO |
1351 | struct parse_events_error err = { .idx = 0, }; |
1352 | int ret = parse_events(evlist, str, &err); | |
1353 | ||
1354 | if (ret) | |
1355 | parse_events_print_error(&err, str); | |
9175ce1f | 1356 | |
9175ce1f | 1357 | return ret; |
f120f9d5 JO |
1358 | } |
1359 | ||
4ba1faa1 WN |
1360 | static int |
1361 | foreach_evsel_in_last_glob(struct perf_evlist *evlist, | |
1362 | int (*func)(struct perf_evsel *evsel, | |
1363 | const void *arg), | |
1364 | const void *arg) | |
c171b552 | 1365 | { |
69aad6f1 | 1366 | struct perf_evsel *last = NULL; |
4ba1faa1 | 1367 | int err; |
c171b552 | 1368 | |
854f7363 WN |
1369 | /* |
1370 | * Don't return when list_empty, give func a chance to report | |
1371 | * error when it found last == NULL. | |
1372 | * | |
1373 | * So no need to WARN here, let *func do this. | |
1374 | */ | |
361c99a6 | 1375 | if (evlist->nr_entries > 0) |
0c21f736 | 1376 | last = perf_evlist__last(evlist); |
69aad6f1 | 1377 | |
15bfd2cc | 1378 | do { |
4ba1faa1 WN |
1379 | err = (*func)(last, arg); |
1380 | if (err) | |
15bfd2cc | 1381 | return -1; |
4ba1faa1 WN |
1382 | if (!last) |
1383 | return 0; | |
15bfd2cc WN |
1384 | |
1385 | if (last->node.prev == &evlist->entries) | |
1386 | return 0; | |
1387 | last = list_entry(last->node.prev, struct perf_evsel, node); | |
1388 | } while (!last->cmdline_group_boundary); | |
c171b552 LZ |
1389 | |
1390 | return 0; | |
1391 | } | |
1392 | ||
4ba1faa1 WN |
1393 | static int set_filter(struct perf_evsel *evsel, const void *arg) |
1394 | { | |
1395 | const char *str = arg; | |
1396 | ||
1397 | if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) { | |
1398 | fprintf(stderr, | |
1399 | "--filter option should follow a -e tracepoint option\n"); | |
1400 | return -1; | |
1401 | } | |
1402 | ||
1403 | if (perf_evsel__append_filter(evsel, "&&", str) < 0) { | |
1404 | fprintf(stderr, | |
1405 | "not enough memory to hold filter string\n"); | |
1406 | return -1; | |
1407 | } | |
1408 | ||
1409 | return 0; | |
1410 | } | |
1411 | ||
1412 | int parse_filter(const struct option *opt, const char *str, | |
1413 | int unset __maybe_unused) | |
1414 | { | |
1415 | struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; | |
1416 | ||
1417 | return foreach_evsel_in_last_glob(evlist, set_filter, | |
1418 | (const void *)str); | |
1419 | } | |
1420 | ||
1421 | static int add_exclude_perf_filter(struct perf_evsel *evsel, | |
1422 | const void *arg __maybe_unused) | |
1423 | { | |
1424 | char new_filter[64]; | |
1425 | ||
1426 | if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) { | |
1427 | fprintf(stderr, | |
1428 | "--exclude-perf option should follow a -e tracepoint option\n"); | |
1429 | return -1; | |
1430 | } | |
1431 | ||
1432 | snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid()); | |
1433 | ||
1434 | if (perf_evsel__append_filter(evsel, "&&", new_filter) < 0) { | |
1435 | fprintf(stderr, | |
1436 | "not enough memory to hold filter string\n"); | |
1437 | return -1; | |
1438 | } | |
1439 | ||
1440 | return 0; | |
1441 | } | |
1442 | ||
1443 | int exclude_perf(const struct option *opt, | |
1444 | const char *arg __maybe_unused, | |
1445 | int unset __maybe_unused) | |
1446 | { | |
1447 | struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; | |
1448 | ||
1449 | return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter, | |
1450 | NULL); | |
1451 | } | |
1452 | ||
86847b62 | 1453 | static const char * const event_type_descriptors[] = { |
86847b62 TG |
1454 | "Hardware event", |
1455 | "Software event", | |
1456 | "Tracepoint event", | |
1457 | "Hardware cache event", | |
41bdcb23 LW |
1458 | "Raw hardware event descriptor", |
1459 | "Hardware breakpoint", | |
86847b62 TG |
1460 | }; |
1461 | ||
ab0e4800 YS |
1462 | static int cmp_string(const void *a, const void *b) |
1463 | { | |
1464 | const char * const *as = a; | |
1465 | const char * const *bs = b; | |
1466 | ||
1467 | return strcmp(*as, *bs); | |
1468 | } | |
1469 | ||
f6bdafef JB |
1470 | /* |
1471 | * Print the events from <debugfs_mount_point>/tracing/events | |
1472 | */ | |
1473 | ||
a3277d2d FW |
1474 | void print_tracepoint_events(const char *subsys_glob, const char *event_glob, |
1475 | bool name_only) | |
f6bdafef JB |
1476 | { |
1477 | DIR *sys_dir, *evt_dir; | |
1478 | struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; | |
f6bdafef | 1479 | char evt_path[MAXPATHLEN]; |
725b1368 | 1480 | char dir_path[MAXPATHLEN]; |
ab0e4800 YS |
1481 | char **evt_list = NULL; |
1482 | unsigned int evt_i = 0, evt_num = 0; | |
1483 | bool evt_num_known = false; | |
f6bdafef | 1484 | |
ab0e4800 | 1485 | restart: |
ebf294bf | 1486 | sys_dir = opendir(tracing_events_path); |
f6bdafef | 1487 | if (!sys_dir) |
725b1368 | 1488 | return; |
6b58e7f1 | 1489 | |
ab0e4800 YS |
1490 | if (evt_num_known) { |
1491 | evt_list = zalloc(sizeof(char *) * evt_num); | |
1492 | if (!evt_list) | |
1493 | goto out_close_sys_dir; | |
1494 | } | |
1495 | ||
6b58e7f1 | 1496 | for_each_subsystem(sys_dir, sys_dirent, sys_next) { |
48000a1a | 1497 | if (subsys_glob != NULL && |
668b8788 ACM |
1498 | !strglobmatch(sys_dirent.d_name, subsys_glob)) |
1499 | continue; | |
725b1368 | 1500 | |
ebf294bf | 1501 | snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, |
725b1368 ED |
1502 | sys_dirent.d_name); |
1503 | evt_dir = opendir(dir_path); | |
1504 | if (!evt_dir) | |
6b58e7f1 | 1505 | continue; |
725b1368 | 1506 | |
6b58e7f1 | 1507 | for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) { |
48000a1a | 1508 | if (event_glob != NULL && |
668b8788 ACM |
1509 | !strglobmatch(evt_dirent.d_name, event_glob)) |
1510 | continue; | |
1511 | ||
ab0e4800 YS |
1512 | if (!evt_num_known) { |
1513 | evt_num++; | |
a3277d2d FW |
1514 | continue; |
1515 | } | |
1516 | ||
f6bdafef JB |
1517 | snprintf(evt_path, MAXPATHLEN, "%s:%s", |
1518 | sys_dirent.d_name, evt_dirent.d_name); | |
ab0e4800 YS |
1519 | |
1520 | evt_list[evt_i] = strdup(evt_path); | |
1521 | if (evt_list[evt_i] == NULL) | |
1522 | goto out_close_evt_dir; | |
1523 | evt_i++; | |
f6bdafef JB |
1524 | } |
1525 | closedir(evt_dir); | |
1526 | } | |
f6bdafef | 1527 | closedir(sys_dir); |
ab0e4800 YS |
1528 | |
1529 | if (!evt_num_known) { | |
1530 | evt_num_known = true; | |
1531 | goto restart; | |
1532 | } | |
1533 | qsort(evt_list, evt_num, sizeof(char *), cmp_string); | |
1534 | evt_i = 0; | |
1535 | while (evt_i < evt_num) { | |
1536 | if (name_only) { | |
1537 | printf("%s ", evt_list[evt_i++]); | |
1538 | continue; | |
1539 | } | |
1540 | printf(" %-50s [%s]\n", evt_list[evt_i++], | |
1541 | event_type_descriptors[PERF_TYPE_TRACEPOINT]); | |
1542 | } | |
dfc431cb | 1543 | if (evt_num && pager_in_use()) |
ab0e4800 YS |
1544 | printf("\n"); |
1545 | ||
1546 | out_free: | |
1547 | evt_num = evt_i; | |
1548 | for (evt_i = 0; evt_i < evt_num; evt_i++) | |
1549 | zfree(&evt_list[evt_i]); | |
1550 | zfree(&evt_list); | |
1551 | return; | |
1552 | ||
1553 | out_close_evt_dir: | |
1554 | closedir(evt_dir); | |
1555 | out_close_sys_dir: | |
1556 | closedir(sys_dir); | |
1557 | ||
1558 | printf("FATAL: not enough memory to print %s\n", | |
1559 | event_type_descriptors[PERF_TYPE_TRACEPOINT]); | |
1560 | if (evt_list) | |
1561 | goto out_free; | |
f6bdafef JB |
1562 | } |
1563 | ||
20c457b8 TR |
1564 | /* |
1565 | * Check whether event is in <debugfs_mount_point>/tracing/events | |
1566 | */ | |
1567 | ||
1568 | int is_valid_tracepoint(const char *event_string) | |
1569 | { | |
1570 | DIR *sys_dir, *evt_dir; | |
1571 | struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; | |
1572 | char evt_path[MAXPATHLEN]; | |
1573 | char dir_path[MAXPATHLEN]; | |
1574 | ||
ebf294bf | 1575 | sys_dir = opendir(tracing_events_path); |
20c457b8 TR |
1576 | if (!sys_dir) |
1577 | return 0; | |
1578 | ||
1579 | for_each_subsystem(sys_dir, sys_dirent, sys_next) { | |
1580 | ||
ebf294bf | 1581 | snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, |
20c457b8 TR |
1582 | sys_dirent.d_name); |
1583 | evt_dir = opendir(dir_path); | |
1584 | if (!evt_dir) | |
1585 | continue; | |
1586 | ||
1587 | for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) { | |
1588 | snprintf(evt_path, MAXPATHLEN, "%s:%s", | |
1589 | sys_dirent.d_name, evt_dirent.d_name); | |
1590 | if (!strcmp(evt_path, event_string)) { | |
1591 | closedir(evt_dir); | |
1592 | closedir(sys_dir); | |
1593 | return 1; | |
1594 | } | |
1595 | } | |
1596 | closedir(evt_dir); | |
1597 | } | |
1598 | closedir(sys_dir); | |
1599 | return 0; | |
1600 | } | |
1601 | ||
b41f1cec NK |
1602 | static bool is_event_supported(u8 type, unsigned config) |
1603 | { | |
1604 | bool ret = true; | |
88fee52e | 1605 | int open_return; |
b41f1cec NK |
1606 | struct perf_evsel *evsel; |
1607 | struct perf_event_attr attr = { | |
1608 | .type = type, | |
1609 | .config = config, | |
1610 | .disabled = 1, | |
b41f1cec NK |
1611 | }; |
1612 | struct { | |
1613 | struct thread_map map; | |
1614 | int threads[1]; | |
1615 | } tmap = { | |
1616 | .map.nr = 1, | |
1617 | .threads = { 0 }, | |
1618 | }; | |
1619 | ||
ef503831 | 1620 | evsel = perf_evsel__new(&attr); |
b41f1cec | 1621 | if (evsel) { |
88fee52e VW |
1622 | open_return = perf_evsel__open(evsel, NULL, &tmap.map); |
1623 | ret = open_return >= 0; | |
1624 | ||
1625 | if (open_return == -EACCES) { | |
1626 | /* | |
1627 | * This happens if the paranoid value | |
1628 | * /proc/sys/kernel/perf_event_paranoid is set to 2 | |
1629 | * Re-run with exclude_kernel set; we don't do that | |
1630 | * by default as some ARM machines do not support it. | |
1631 | * | |
1632 | */ | |
1633 | evsel->attr.exclude_kernel = 1; | |
1634 | ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0; | |
1635 | } | |
b41f1cec NK |
1636 | perf_evsel__delete(evsel); |
1637 | } | |
1638 | ||
1639 | return ret; | |
1640 | } | |
1641 | ||
a3277d2d | 1642 | int print_hwcache_events(const char *event_glob, bool name_only) |
668b8788 | 1643 | { |
ab0e4800 | 1644 | unsigned int type, op, i, evt_i = 0, evt_num = 0; |
0b668bc9 | 1645 | char name[64]; |
ab0e4800 YS |
1646 | char **evt_list = NULL; |
1647 | bool evt_num_known = false; | |
1648 | ||
1649 | restart: | |
1650 | if (evt_num_known) { | |
1651 | evt_list = zalloc(sizeof(char *) * evt_num); | |
1652 | if (!evt_list) | |
1653 | goto out_enomem; | |
1654 | } | |
668b8788 ACM |
1655 | |
1656 | for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { | |
1657 | for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { | |
1658 | /* skip invalid cache type */ | |
0b668bc9 | 1659 | if (!perf_evsel__is_cache_op_valid(type, op)) |
668b8788 ACM |
1660 | continue; |
1661 | ||
1662 | for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) { | |
0b668bc9 ACM |
1663 | __perf_evsel__hw_cache_type_op_res_name(type, op, i, |
1664 | name, sizeof(name)); | |
947b4ad1 | 1665 | if (event_glob != NULL && !strglobmatch(name, event_glob)) |
668b8788 ACM |
1666 | continue; |
1667 | ||
b41f1cec NK |
1668 | if (!is_event_supported(PERF_TYPE_HW_CACHE, |
1669 | type | (op << 8) | (i << 16))) | |
1670 | continue; | |
1671 | ||
ab0e4800 YS |
1672 | if (!evt_num_known) { |
1673 | evt_num++; | |
1674 | continue; | |
1675 | } | |
1676 | ||
1677 | evt_list[evt_i] = strdup(name); | |
1678 | if (evt_list[evt_i] == NULL) | |
1679 | goto out_enomem; | |
1680 | evt_i++; | |
668b8788 ACM |
1681 | } |
1682 | } | |
1683 | } | |
1684 | ||
ab0e4800 YS |
1685 | if (!evt_num_known) { |
1686 | evt_num_known = true; | |
1687 | goto restart; | |
1688 | } | |
1689 | qsort(evt_list, evt_num, sizeof(char *), cmp_string); | |
1690 | evt_i = 0; | |
1691 | while (evt_i < evt_num) { | |
1692 | if (name_only) { | |
1693 | printf("%s ", evt_list[evt_i++]); | |
1694 | continue; | |
1695 | } | |
1696 | printf(" %-50s [%s]\n", evt_list[evt_i++], | |
1697 | event_type_descriptors[PERF_TYPE_HW_CACHE]); | |
1698 | } | |
dfc431cb | 1699 | if (evt_num && pager_in_use()) |
dc098b35 | 1700 | printf("\n"); |
ab0e4800 YS |
1701 | |
1702 | out_free: | |
1703 | evt_num = evt_i; | |
1704 | for (evt_i = 0; evt_i < evt_num; evt_i++) | |
1705 | zfree(&evt_list[evt_i]); | |
1706 | zfree(&evt_list); | |
1707 | return evt_num; | |
1708 | ||
1709 | out_enomem: | |
1710 | printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]); | |
1711 | if (evt_list) | |
1712 | goto out_free; | |
1713 | return evt_num; | |
668b8788 ACM |
1714 | } |
1715 | ||
705750f2 | 1716 | void print_symbol_events(const char *event_glob, unsigned type, |
a3277d2d FW |
1717 | struct event_symbol *syms, unsigned max, |
1718 | bool name_only) | |
8ad8db37 | 1719 | { |
ab0e4800 | 1720 | unsigned int i, evt_i = 0, evt_num = 0; |
947b4ad1 | 1721 | char name[MAX_NAME_LEN]; |
ab0e4800 YS |
1722 | char **evt_list = NULL; |
1723 | bool evt_num_known = false; | |
1724 | ||
1725 | restart: | |
1726 | if (evt_num_known) { | |
1727 | evt_list = zalloc(sizeof(char *) * evt_num); | |
1728 | if (!evt_list) | |
1729 | goto out_enomem; | |
1730 | syms -= max; | |
1731 | } | |
8ad8db37 | 1732 | |
1dc12760 | 1733 | for (i = 0; i < max; i++, syms++) { |
668b8788 | 1734 | |
48000a1a | 1735 | if (event_glob != NULL && |
668b8788 ACM |
1736 | !(strglobmatch(syms->symbol, event_glob) || |
1737 | (syms->alias && strglobmatch(syms->alias, event_glob)))) | |
1738 | continue; | |
8ad8db37 | 1739 | |
b41f1cec NK |
1740 | if (!is_event_supported(type, i)) |
1741 | continue; | |
1742 | ||
ab0e4800 YS |
1743 | if (!evt_num_known) { |
1744 | evt_num++; | |
a3277d2d FW |
1745 | continue; |
1746 | } | |
1747 | ||
ab0e4800 | 1748 | if (!name_only && strlen(syms->alias)) |
947b4ad1 | 1749 | snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias); |
74d5b588 | 1750 | else |
947b4ad1 | 1751 | strncpy(name, syms->symbol, MAX_NAME_LEN); |
8ad8db37 | 1752 | |
ab0e4800 YS |
1753 | evt_list[evt_i] = strdup(name); |
1754 | if (evt_list[evt_i] == NULL) | |
1755 | goto out_enomem; | |
1756 | evt_i++; | |
8ad8db37 IM |
1757 | } |
1758 | ||
ab0e4800 YS |
1759 | if (!evt_num_known) { |
1760 | evt_num_known = true; | |
1761 | goto restart; | |
1762 | } | |
1763 | qsort(evt_list, evt_num, sizeof(char *), cmp_string); | |
1764 | evt_i = 0; | |
1765 | while (evt_i < evt_num) { | |
1766 | if (name_only) { | |
1767 | printf("%s ", evt_list[evt_i++]); | |
1768 | continue; | |
1769 | } | |
1770 | printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]); | |
1771 | } | |
dfc431cb | 1772 | if (evt_num && pager_in_use()) |
668b8788 | 1773 | printf("\n"); |
ab0e4800 YS |
1774 | |
1775 | out_free: | |
1776 | evt_num = evt_i; | |
1777 | for (evt_i = 0; evt_i < evt_num; evt_i++) | |
1778 | zfree(&evt_list[evt_i]); | |
1779 | zfree(&evt_list); | |
1780 | return; | |
1781 | ||
1782 | out_enomem: | |
1783 | printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]); | |
1784 | if (evt_list) | |
1785 | goto out_free; | |
1dc12760 JO |
1786 | } |
1787 | ||
1788 | /* | |
1789 | * Print the help text for the event symbols: | |
1790 | */ | |
a3277d2d | 1791 | void print_events(const char *event_glob, bool name_only) |
1dc12760 | 1792 | { |
1dc12760 | 1793 | print_symbol_events(event_glob, PERF_TYPE_HARDWARE, |
a3277d2d | 1794 | event_symbols_hw, PERF_COUNT_HW_MAX, name_only); |
1dc12760 JO |
1795 | |
1796 | print_symbol_events(event_glob, PERF_TYPE_SOFTWARE, | |
a3277d2d | 1797 | event_symbols_sw, PERF_COUNT_SW_MAX, name_only); |
1dc12760 | 1798 | |
a3277d2d | 1799 | print_hwcache_events(event_glob, name_only); |
668b8788 | 1800 | |
dc098b35 AK |
1801 | print_pmu_events(event_glob, name_only); |
1802 | ||
668b8788 ACM |
1803 | if (event_glob != NULL) |
1804 | return; | |
73c24cb8 | 1805 | |
a3277d2d | 1806 | if (!name_only) { |
a3277d2d FW |
1807 | printf(" %-50s [%s]\n", |
1808 | "rNNN", | |
1809 | event_type_descriptors[PERF_TYPE_RAW]); | |
1810 | printf(" %-50s [%s]\n", | |
1811 | "cpu/t1=v1[,t2=v2,t3 ...]/modifier", | |
1812 | event_type_descriptors[PERF_TYPE_RAW]); | |
dfc431cb ACM |
1813 | if (pager_in_use()) |
1814 | printf(" (see 'man perf-list' on how to encode it)\n\n"); | |
a3277d2d FW |
1815 | |
1816 | printf(" %-50s [%s]\n", | |
3741eb9f | 1817 | "mem:<addr>[/len][:access]", |
41bdcb23 | 1818 | event_type_descriptors[PERF_TYPE_BREAKPOINT]); |
dfc431cb ACM |
1819 | if (pager_in_use()) |
1820 | printf("\n"); | |
a3277d2d | 1821 | } |
1b290d67 | 1822 | |
a3277d2d | 1823 | print_tracepoint_events(NULL, NULL, name_only); |
8ad8db37 | 1824 | } |
8f707d84 | 1825 | |
6cee6cd3 | 1826 | int parse_events__is_hardcoded_term(struct parse_events_term *term) |
8f707d84 | 1827 | { |
16fa7e82 | 1828 | return term->type_term != PARSE_EVENTS__TERM_TYPE_USER; |
8f707d84 JO |
1829 | } |
1830 | ||
6cee6cd3 | 1831 | static int new_term(struct parse_events_term **_term, int type_val, |
16fa7e82 | 1832 | int type_term, char *config, |
cecf3a2e | 1833 | char *str, u64 num, int err_term, int err_val) |
8f707d84 | 1834 | { |
6cee6cd3 | 1835 | struct parse_events_term *term; |
8f707d84 JO |
1836 | |
1837 | term = zalloc(sizeof(*term)); | |
1838 | if (!term) | |
1839 | return -ENOMEM; | |
1840 | ||
1841 | INIT_LIST_HEAD(&term->list); | |
16fa7e82 JO |
1842 | term->type_val = type_val; |
1843 | term->type_term = type_term; | |
8f707d84 | 1844 | term->config = config; |
cecf3a2e JO |
1845 | term->err_term = err_term; |
1846 | term->err_val = err_val; | |
8f707d84 | 1847 | |
16fa7e82 | 1848 | switch (type_val) { |
8f707d84 JO |
1849 | case PARSE_EVENTS__TERM_TYPE_NUM: |
1850 | term->val.num = num; | |
1851 | break; | |
1852 | case PARSE_EVENTS__TERM_TYPE_STR: | |
1853 | term->val.str = str; | |
1854 | break; | |
1855 | default: | |
4be8be6b | 1856 | free(term); |
8f707d84 JO |
1857 | return -EINVAL; |
1858 | } | |
1859 | ||
1860 | *_term = term; | |
1861 | return 0; | |
1862 | } | |
1863 | ||
6cee6cd3 | 1864 | int parse_events_term__num(struct parse_events_term **term, |
cecf3a2e | 1865 | int type_term, char *config, u64 num, |
bb78ce7d | 1866 | void *loc_term_, void *loc_val_) |
16fa7e82 | 1867 | { |
bb78ce7d AH |
1868 | YYLTYPE *loc_term = loc_term_; |
1869 | YYLTYPE *loc_val = loc_val_; | |
1870 | ||
16fa7e82 | 1871 | return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term, |
cecf3a2e JO |
1872 | config, NULL, num, |
1873 | loc_term ? loc_term->first_column : 0, | |
1874 | loc_val ? loc_val->first_column : 0); | |
16fa7e82 JO |
1875 | } |
1876 | ||
6cee6cd3 | 1877 | int parse_events_term__str(struct parse_events_term **term, |
cecf3a2e | 1878 | int type_term, char *config, char *str, |
bb78ce7d | 1879 | void *loc_term_, void *loc_val_) |
16fa7e82 | 1880 | { |
bb78ce7d AH |
1881 | YYLTYPE *loc_term = loc_term_; |
1882 | YYLTYPE *loc_val = loc_val_; | |
1883 | ||
16fa7e82 | 1884 | return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term, |
cecf3a2e JO |
1885 | config, str, 0, |
1886 | loc_term ? loc_term->first_column : 0, | |
1887 | loc_val ? loc_val->first_column : 0); | |
16fa7e82 JO |
1888 | } |
1889 | ||
6cee6cd3 | 1890 | int parse_events_term__sym_hw(struct parse_events_term **term, |
1d33d6dc JO |
1891 | char *config, unsigned idx) |
1892 | { | |
1893 | struct event_symbol *sym; | |
1894 | ||
1895 | BUG_ON(idx >= PERF_COUNT_HW_MAX); | |
1896 | sym = &event_symbols_hw[idx]; | |
1897 | ||
1898 | if (config) | |
1899 | return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, | |
1900 | PARSE_EVENTS__TERM_TYPE_USER, config, | |
cecf3a2e | 1901 | (char *) sym->symbol, 0, 0, 0); |
1d33d6dc JO |
1902 | else |
1903 | return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, | |
1904 | PARSE_EVENTS__TERM_TYPE_USER, | |
cecf3a2e JO |
1905 | (char *) "event", (char *) sym->symbol, |
1906 | 0, 0, 0); | |
1d33d6dc JO |
1907 | } |
1908 | ||
6cee6cd3 ACM |
1909 | int parse_events_term__clone(struct parse_events_term **new, |
1910 | struct parse_events_term *term) | |
a6146d50 ZY |
1911 | { |
1912 | return new_term(new, term->type_val, term->type_term, term->config, | |
cecf3a2e JO |
1913 | term->val.str, term->val.num, |
1914 | term->err_term, term->err_val); | |
a6146d50 ZY |
1915 | } |
1916 | ||
8f707d84 JO |
1917 | void parse_events__free_terms(struct list_head *terms) |
1918 | { | |
6cee6cd3 | 1919 | struct parse_events_term *term, *h; |
8f707d84 JO |
1920 | |
1921 | list_for_each_entry_safe(term, h, terms, list) | |
1922 | free(term); | |
8f707d84 | 1923 | } |
b39b8393 JO |
1924 | |
1925 | void parse_events_evlist_error(struct parse_events_evlist *data, | |
1926 | int idx, const char *str) | |
1927 | { | |
1928 | struct parse_events_error *err = data->error; | |
1929 | ||
a6ced2be AH |
1930 | if (!err) |
1931 | return; | |
b39b8393 JO |
1932 | err->idx = idx; |
1933 | err->str = strdup(str); | |
1934 | WARN_ONCE(!err->str, "WARNING: failed to allocate error string"); | |
1935 | } | |
ffeb883e HK |
1936 | |
1937 | /* | |
1938 | * Return string contains valid config terms of an event. | |
1939 | * @additional_terms: For terms such as PMU sysfs terms. | |
1940 | */ | |
1941 | char *parse_events_formats_error_string(char *additional_terms) | |
1942 | { | |
1943 | char *str; | |
1944 | static const char *static_terms = "config,config1,config2,name," | |
1945 | "period,freq,branch_type,time," | |
1946 | "call-graph,stack-size\n"; | |
1947 | ||
1948 | /* valid terms */ | |
1949 | if (additional_terms) { | |
1950 | if (!asprintf(&str, "valid terms: %s,%s", | |
1951 | additional_terms, static_terms)) | |
1952 | goto fail; | |
1953 | } else { | |
1954 | if (!asprintf(&str, "valid terms: %s", static_terms)) | |
1955 | goto fail; | |
1956 | } | |
1957 | return str; | |
1958 | ||
1959 | fail: | |
1960 | return NULL; | |
1961 | } |