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