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