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