blob: 749af0db94e7d6d8b4243e824ab77733df1f8a9f [file] [log] [blame]
David Howellsd2709c72012-11-19 22:21:03 +00001#include <linux/hw_breakpoint.h>
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002#include "util.h"
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03003#include "../perf.h"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02004#include "evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02005#include "evsel.h"
Ingo Molnar8ad8db32009-05-26 11:10:09 +02006#include "parse-options.h"
7#include "parse-events.h"
8#include "exec_cmd.h"
Kan Liang42f60c22014-10-07 11:08:49 -04009#include "string.h"
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -030010#include "symbol.h"
Jason Baron5beeded2009-07-21 14:16:29 -040011#include "cache.h"
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020012#include "header.h"
Masami Hiramatsu6e81c742014-08-14 02:22:36 +000013#include "debug.h"
Borislav Petkov553873e2013-12-09 17:14:23 +010014#include <api/fs/debugfs.h>
Zheng Yanac20de6f2012-06-15 14:31:39 +080015#include "parse-events-bison.h"
Jiri Olsa90e2b222012-06-15 14:31:40 +080016#define YY_EXTRA_TYPE int
Jiri Olsa89812fc2012-03-15 20:09:15 +010017#include "parse-events-flex.h"
Jiri Olsa5f537a22012-03-15 20:09:18 +010018#include "pmu.h"
Namhyung Kimb41f1ce2013-08-27 11:41:53 +090019#include "thread_map.h"
Jiri Olsab39b8392015-04-22 21:10:16 +020020#include "asm/bug.h"
Jiri Olsa89812fc2012-03-15 20:09:15 +010021
22#define MAX_NAME_LEN 100
Ingo Molnar8ad8db32009-05-26 11:10:09 +020023
Jiri Olsa82ba1f22012-05-21 09:12:50 +020024#ifdef PARSER_DEBUG
25extern int parse_events_debug;
26#endif
Zheng Yanac20de6f2012-06-15 14:31:39 +080027int parse_events_parse(void *data, void *scanner);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +020028
Kan Liangdcb4e102014-10-07 11:08:50 -040029static struct perf_pmu_event_symbol *perf_pmu_events_list;
30/*
31 * The variable indicates the number of supported pmu event symbols.
32 * 0 means not initialized and ready to init
33 * -1 means failed to init, don't try anymore
34 * >0 is the number of supported pmu event symbols
35 */
36static int perf_pmu_events_list_num;
37
Yunlong Song705750f2015-02-27 18:21:27 +080038struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
Jiri Olsa1dc127602012-07-04 00:00:44 +020039 [PERF_COUNT_HW_CPU_CYCLES] = {
40 .symbol = "cpu-cycles",
41 .alias = "cycles",
42 },
43 [PERF_COUNT_HW_INSTRUCTIONS] = {
44 .symbol = "instructions",
45 .alias = "",
46 },
47 [PERF_COUNT_HW_CACHE_REFERENCES] = {
48 .symbol = "cache-references",
49 .alias = "",
50 },
51 [PERF_COUNT_HW_CACHE_MISSES] = {
52 .symbol = "cache-misses",
53 .alias = "",
54 },
55 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
56 .symbol = "branch-instructions",
57 .alias = "branches",
58 },
59 [PERF_COUNT_HW_BRANCH_MISSES] = {
60 .symbol = "branch-misses",
61 .alias = "",
62 },
63 [PERF_COUNT_HW_BUS_CYCLES] = {
64 .symbol = "bus-cycles",
65 .alias = "",
66 },
67 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
68 .symbol = "stalled-cycles-frontend",
69 .alias = "idle-cycles-frontend",
70 },
71 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
72 .symbol = "stalled-cycles-backend",
73 .alias = "idle-cycles-backend",
74 },
75 [PERF_COUNT_HW_REF_CPU_CYCLES] = {
76 .symbol = "ref-cycles",
77 .alias = "",
78 },
79};
Ingo Molnar8ad8db32009-05-26 11:10:09 +020080
Yunlong Song705750f2015-02-27 18:21:27 +080081struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
Jiri Olsa1dc127602012-07-04 00:00:44 +020082 [PERF_COUNT_SW_CPU_CLOCK] = {
83 .symbol = "cpu-clock",
84 .alias = "",
85 },
86 [PERF_COUNT_SW_TASK_CLOCK] = {
87 .symbol = "task-clock",
88 .alias = "",
89 },
90 [PERF_COUNT_SW_PAGE_FAULTS] = {
91 .symbol = "page-faults",
92 .alias = "faults",
93 },
94 [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
95 .symbol = "context-switches",
96 .alias = "cs",
97 },
98 [PERF_COUNT_SW_CPU_MIGRATIONS] = {
99 .symbol = "cpu-migrations",
100 .alias = "migrations",
101 },
102 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
103 .symbol = "minor-faults",
104 .alias = "",
105 },
106 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
107 .symbol = "major-faults",
108 .alias = "",
109 },
110 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
111 .symbol = "alignment-faults",
112 .alias = "",
113 },
114 [PERF_COUNT_SW_EMULATION_FAULTS] = {
115 .symbol = "emulation-faults",
116 .alias = "",
117 },
Adrian Hunterd22d1a22013-08-31 21:50:52 +0300118 [PERF_COUNT_SW_DUMMY] = {
119 .symbol = "dummy",
120 .alias = "",
121 },
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200122};
123
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200124#define __PERF_EVENT_FIELD(config, name) \
125 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
Ingo Molnar52425192009-05-26 09:17:18 +0200126
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200127#define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200128#define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200129#define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200130#define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
Ingo Molnar52425192009-05-26 09:17:18 +0200131
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300132#define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400133 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300134 if (sys_dirent.d_type == DT_DIR && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400135 (strcmp(sys_dirent.d_name, ".")) && \
136 (strcmp(sys_dirent.d_name, "..")))
137
Peter Zijlstraae07b632009-08-06 16:48:54 +0200138static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
139{
140 char evt_path[MAXPATHLEN];
141 int fd;
142
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200143 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
Peter Zijlstraae07b632009-08-06 16:48:54 +0200144 sys_dir->d_name, evt_dir->d_name);
145 fd = open(evt_path, O_RDONLY);
146 if (fd < 0)
147 return -EINVAL;
148 close(fd);
149
150 return 0;
151}
152
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300153#define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400154 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300155 if (evt_dirent.d_type == DT_DIR && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400156 (strcmp(evt_dirent.d_name, ".")) && \
Peter Zijlstraae07b632009-08-06 16:48:54 +0200157 (strcmp(evt_dirent.d_name, "..")) && \
158 (!tp_event_has_id(&sys_dirent, &evt_dirent)))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400159
Li Zefan270bbbe2009-09-17 16:34:51 +0800160#define MAX_EVENT_LENGTH 512
Jason Baronf6bdafe2009-07-21 12:20:22 -0400161
Jason Baronf6bdafe2009-07-21 12:20:22 -0400162
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200163struct tracepoint_path *tracepoint_id_to_path(u64 config)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400164{
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200165 struct tracepoint_path *path = NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400166 DIR *sys_dir, *evt_dir;
167 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Stephane Eranian8aa8a7c2012-03-13 16:51:02 +0100168 char id_buf[24];
Eric Dumazet725b1362009-09-24 15:39:09 +0200169 int fd;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400170 u64 id;
171 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +0200172 char dir_path[MAXPATHLEN];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400173
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200174 sys_dir = opendir(tracing_events_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400175 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200176 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400177
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300178 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200179
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200180 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Eric Dumazet725b1362009-09-24 15:39:09 +0200181 sys_dirent.d_name);
182 evt_dir = opendir(dir_path);
183 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300184 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200185
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300186 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200187
188 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
Jason Baronf6bdafe2009-07-21 12:20:22 -0400189 evt_dirent.d_name);
Eric Dumazet725b1362009-09-24 15:39:09 +0200190 fd = open(evt_path, O_RDONLY);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400191 if (fd < 0)
192 continue;
193 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
194 close(fd);
195 continue;
196 }
197 close(fd);
198 id = atoll(id_buf);
199 if (id == config) {
200 closedir(evt_dir);
201 closedir(sys_dir);
Julia Lawall59b4cae2009-12-06 10:16:30 +0100202 path = zalloc(sizeof(*path));
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200203 path->system = malloc(MAX_EVENT_LENGTH);
204 if (!path->system) {
205 free(path);
206 return NULL;
207 }
208 path->name = malloc(MAX_EVENT_LENGTH);
209 if (!path->name) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300210 zfree(&path->system);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200211 free(path);
212 return NULL;
213 }
214 strncpy(path->system, sys_dirent.d_name,
215 MAX_EVENT_LENGTH);
216 strncpy(path->name, evt_dirent.d_name,
217 MAX_EVENT_LENGTH);
218 return path;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400219 }
220 }
221 closedir(evt_dir);
222 }
223
Jason Baronf6bdafe2009-07-21 12:20:22 -0400224 closedir(sys_dir);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200225 return NULL;
226}
227
Namhyung Kime7c93f02013-06-26 16:14:05 +0900228struct tracepoint_path *tracepoint_name_to_path(const char *name)
229{
230 struct tracepoint_path *path = zalloc(sizeof(*path));
231 char *str = strchr(name, ':');
232
233 if (path == NULL || str == NULL) {
234 free(path);
235 return NULL;
236 }
237
238 path->system = strndup(name, str - name);
239 path->name = strdup(str+1);
240
241 if (path->system == NULL || path->name == NULL) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300242 zfree(&path->system);
243 zfree(&path->name);
Namhyung Kime7c93f02013-06-26 16:14:05 +0900244 free(path);
245 path = NULL;
246 }
247
248 return path;
249}
250
David Ahern1424dc92011-03-09 22:23:28 -0700251const char *event_type(int type)
252{
253 switch (type) {
254 case PERF_TYPE_HARDWARE:
255 return "hardware";
256
257 case PERF_TYPE_SOFTWARE:
258 return "software";
259
260 case PERF_TYPE_TRACEPOINT:
261 return "tracepoint";
262
263 case PERF_TYPE_HW_CACHE:
264 return "hardware-cache";
265
266 default:
267 break;
268 }
269
270 return "unknown";
271}
272
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800273
274
Stephane Eranian410136f2013-11-12 17:58:49 +0100275static struct perf_evsel *
276__add_event(struct list_head *list, int *idx,
277 struct perf_event_attr *attr,
278 char *name, struct cpu_map *cpus)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100279{
280 struct perf_evsel *evsel;
281
282 event_attr_init(attr);
283
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300284 evsel = perf_evsel__new_idx(attr, (*idx)++);
David Ahernc5cd8ac2013-07-02 13:27:25 -0600285 if (!evsel)
Stephane Eranian410136f2013-11-12 17:58:49 +0100286 return NULL;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100287
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800288 evsel->cpus = cpus;
Arnaldo Carvalho de Melo9db17632012-06-12 13:45:00 -0300289 if (name)
290 evsel->name = strdup(name);
Jiri Olsab847cbd2012-05-21 09:12:51 +0200291 list_add_tail(&evsel->node, list);
Stephane Eranian410136f2013-11-12 17:58:49 +0100292 return evsel;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100293}
294
David Ahernc5cd8ac2013-07-02 13:27:25 -0600295static int add_event(struct list_head *list, int *idx,
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800296 struct perf_event_attr *attr, char *name)
297{
Stephane Eranian410136f2013-11-12 17:58:49 +0100298 return __add_event(list, idx, attr, name, NULL) ? 0 : -ENOMEM;
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800299}
300
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300301static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
Ingo Molnar8326f442009-06-05 20:22:46 +0200302{
303 int i, j;
Paul Mackerras61c45982009-07-01 13:04:34 +1000304 int n, longest = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200305
306 for (i = 0; i < size; i++) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300307 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
Paul Mackerras61c45982009-07-01 13:04:34 +1000308 n = strlen(names[i][j]);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100309 if (n > longest && !strncasecmp(str, names[i][j], n))
Paul Mackerras61c45982009-07-01 13:04:34 +1000310 longest = n;
311 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100312 if (longest > 0)
Paul Mackerras61c45982009-07-01 13:04:34 +1000313 return i;
Ingo Molnar8326f442009-06-05 20:22:46 +0200314 }
315
Ingo Molnar89536452009-06-06 21:04:17 +0200316 return -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200317}
318
David Ahernc5cd8ac2013-07-02 13:27:25 -0600319int parse_events_add_cache(struct list_head *list, int *idx,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100320 char *type, char *op_result1, char *op_result2)
Ingo Molnar8326f442009-06-05 20:22:46 +0200321{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100322 struct perf_event_attr attr;
323 char name[MAX_NAME_LEN];
Paul Mackerras61c45982009-07-01 13:04:34 +1000324 int cache_type = -1, cache_op = -1, cache_result = -1;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100325 char *op_result[2] = { op_result1, op_result2 };
326 int i, n;
Ingo Molnar8326f442009-06-05 20:22:46 +0200327
Ingo Molnar8326f442009-06-05 20:22:46 +0200328 /*
329 * No fallback - if we cannot get a clear cache type
330 * then bail out:
331 */
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300332 cache_type = parse_aliases(type, perf_evsel__hw_cache,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100333 PERF_COUNT_HW_CACHE_MAX);
Ingo Molnar8326f442009-06-05 20:22:46 +0200334 if (cache_type == -1)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100335 return -EINVAL;
Ingo Molnar8326f442009-06-05 20:22:46 +0200336
Jiri Olsa89812fc2012-03-15 20:09:15 +0100337 n = snprintf(name, MAX_NAME_LEN, "%s", type);
338
339 for (i = 0; (i < 2) && (op_result[i]); i++) {
340 char *str = op_result[i];
341
Jiri Olsa275ef382012-09-05 19:51:33 +0200342 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
Paul Mackerras61c45982009-07-01 13:04:34 +1000343
344 if (cache_op == -1) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300345 cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100346 PERF_COUNT_HW_CACHE_OP_MAX);
Paul Mackerras61c45982009-07-01 13:04:34 +1000347 if (cache_op >= 0) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300348 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
Jiri Olsa89812fc2012-03-15 20:09:15 +0100349 return -EINVAL;
Paul Mackerras61c45982009-07-01 13:04:34 +1000350 continue;
351 }
352 }
353
354 if (cache_result == -1) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300355 cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
356 PERF_COUNT_HW_CACHE_RESULT_MAX);
Paul Mackerras61c45982009-07-01 13:04:34 +1000357 if (cache_result >= 0)
358 continue;
359 }
Paul Mackerras61c45982009-07-01 13:04:34 +1000360 }
361
Ingo Molnar8326f442009-06-05 20:22:46 +0200362 /*
363 * Fall back to reads:
364 */
Ingo Molnar89536452009-06-06 21:04:17 +0200365 if (cache_op == -1)
366 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
Ingo Molnar8326f442009-06-05 20:22:46 +0200367
Ingo Molnar8326f442009-06-05 20:22:46 +0200368 /*
369 * Fall back to accesses:
370 */
371 if (cache_result == -1)
372 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
373
Jiri Olsa89812fc2012-03-15 20:09:15 +0100374 memset(&attr, 0, sizeof(attr));
375 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
376 attr.type = PERF_TYPE_HW_CACHE;
377 return add_event(list, idx, &attr, name);
Ingo Molnar8326f442009-06-05 20:22:46 +0200378}
379
David Ahernc5cd8ac2013-07-02 13:27:25 -0600380static int add_tracepoint(struct list_head *list, int *idx,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100381 char *sys_name, char *evt_name)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200382{
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300383 struct perf_evsel *evsel;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200384
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300385 evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
David Ahernc5cd8ac2013-07-02 13:27:25 -0600386 if (!evsel)
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300387 return -ENOMEM;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200388
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300389 list_add_tail(&evsel->node, list);
David Ahernc5cd8ac2013-07-02 13:27:25 -0600390
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300391 return 0;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200392}
393
David Ahernc5cd8ac2013-07-02 13:27:25 -0600394static int add_tracepoint_multi_event(struct list_head *list, int *idx,
Jiri Olsaf35488f2012-12-17 14:08:37 +0100395 char *sys_name, char *evt_name)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200396{
397 char evt_path[MAXPATHLEN];
398 struct dirent *evt_ent;
399 DIR *evt_dir;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100400 int ret = 0;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200401
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200402 snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200403 evt_dir = opendir(evt_path);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200404 if (!evt_dir) {
405 perror("Can't open event dir");
Jiri Olsa89812fc2012-03-15 20:09:15 +0100406 return -1;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200407 }
408
Jiri Olsa89812fc2012-03-15 20:09:15 +0100409 while (!ret && (evt_ent = readdir(evt_dir))) {
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200410 if (!strcmp(evt_ent->d_name, ".")
411 || !strcmp(evt_ent->d_name, "..")
412 || !strcmp(evt_ent->d_name, "enable")
413 || !strcmp(evt_ent->d_name, "filter"))
414 continue;
415
Jiri Olsa89812fc2012-03-15 20:09:15 +0100416 if (!strglobmatch(evt_ent->d_name, evt_name))
Masami Hiramatsufb1d2ed2010-01-05 17:47:17 -0500417 continue;
418
Jiri Olsa89812fc2012-03-15 20:09:15 +0100419 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200420 }
421
Jiri Olsa0bd3f082012-12-17 14:08:36 +0100422 closedir(evt_dir);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100423 return ret;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200424}
425
David Ahernc5cd8ac2013-07-02 13:27:25 -0600426static int add_tracepoint_event(struct list_head *list, int *idx,
Jiri Olsaf35488f2012-12-17 14:08:37 +0100427 char *sys_name, char *evt_name)
428{
429 return strpbrk(evt_name, "*?") ?
430 add_tracepoint_multi_event(list, idx, sys_name, evt_name) :
431 add_tracepoint(list, idx, sys_name, evt_name);
432}
433
David Ahernc5cd8ac2013-07-02 13:27:25 -0600434static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
Jiri Olsaf35488f2012-12-17 14:08:37 +0100435 char *sys_name, char *evt_name)
436{
437 struct dirent *events_ent;
438 DIR *events_dir;
439 int ret = 0;
440
441 events_dir = opendir(tracing_events_path);
442 if (!events_dir) {
443 perror("Can't open event dir");
444 return -1;
445 }
446
447 while (!ret && (events_ent = readdir(events_dir))) {
448 if (!strcmp(events_ent->d_name, ".")
449 || !strcmp(events_ent->d_name, "..")
450 || !strcmp(events_ent->d_name, "enable")
451 || !strcmp(events_ent->d_name, "header_event")
452 || !strcmp(events_ent->d_name, "header_page"))
453 continue;
454
455 if (!strglobmatch(events_ent->d_name, sys_name))
456 continue;
457
458 ret = add_tracepoint_event(list, idx, events_ent->d_name,
459 evt_name);
460 }
461
462 closedir(events_dir);
463 return ret;
464}
465
David Ahernc5cd8ac2013-07-02 13:27:25 -0600466int parse_events_add_tracepoint(struct list_head *list, int *idx,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100467 char *sys, char *event)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400468{
Jiri Olsaf35488f2012-12-17 14:08:37 +0100469 if (strpbrk(sys, "*?"))
470 return add_tracepoint_multi_sys(list, idx, sys, event);
471 else
472 return add_tracepoint_event(list, idx, sys, event);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400473}
474
Jiri Olsa89812fc2012-03-15 20:09:15 +0100475static int
476parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100477{
478 int i;
479
480 for (i = 0; i < 3; i++) {
Jiri Olsa89812fc2012-03-15 20:09:15 +0100481 if (!type || !type[i])
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100482 break;
483
Jiri Olsa75827322012-06-29 09:22:54 +0200484#define CHECK_SET_TYPE(bit) \
485do { \
486 if (attr->bp_type & bit) \
487 return -EINVAL; \
488 else \
489 attr->bp_type |= bit; \
490} while (0)
491
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100492 switch (type[i]) {
493 case 'r':
Jiri Olsa75827322012-06-29 09:22:54 +0200494 CHECK_SET_TYPE(HW_BREAKPOINT_R);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100495 break;
496 case 'w':
Jiri Olsa75827322012-06-29 09:22:54 +0200497 CHECK_SET_TYPE(HW_BREAKPOINT_W);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100498 break;
499 case 'x':
Jiri Olsa75827322012-06-29 09:22:54 +0200500 CHECK_SET_TYPE(HW_BREAKPOINT_X);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100501 break;
502 default:
Jiri Olsa89812fc2012-03-15 20:09:15 +0100503 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100504 }
505 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100506
Jiri Olsa75827322012-06-29 09:22:54 +0200507#undef CHECK_SET_TYPE
508
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100509 if (!attr->bp_type) /* Default */
510 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
511
Jiri Olsa89812fc2012-03-15 20:09:15 +0100512 return 0;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100513}
514
David Ahernc5cd8ac2013-07-02 13:27:25 -0600515int parse_events_add_breakpoint(struct list_head *list, int *idx,
Jacob Shin3741eb92014-05-29 17:26:51 +0200516 void *ptr, char *type, u64 len)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100517{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100518 struct perf_event_attr attr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100519
Jiri Olsa89812fc2012-03-15 20:09:15 +0100520 memset(&attr, 0, sizeof(attr));
Jiri Olsa9fafd982012-03-20 19:15:39 +0100521 attr.bp_addr = (unsigned long) ptr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100522
Jiri Olsa89812fc2012-03-15 20:09:15 +0100523 if (parse_breakpoint_type(type, &attr))
524 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100525
Jacob Shin3741eb92014-05-29 17:26:51 +0200526 /* Provide some defaults if len is not specified */
527 if (!len) {
528 if (attr.bp_type == HW_BREAKPOINT_X)
529 len = sizeof(long);
530 else
531 len = HW_BREAKPOINT_LEN_4;
532 }
533
534 attr.bp_len = len;
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200535
Jiri Olsa89812fc2012-03-15 20:09:15 +0100536 attr.type = PERF_TYPE_BREAKPOINT;
Jovi Zhang4a841d62012-07-15 03:03:10 +0800537 attr.sample_period = 1;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100538
Jiri Olsa287e74a2012-06-28 23:18:49 +0200539 return add_event(list, idx, &attr, NULL);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100540}
541
Jiri Olsa8f707d82012-03-15 20:09:16 +0100542static int config_term(struct perf_event_attr *attr,
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300543 struct parse_events_term *term)
Jiri Olsa8f707d82012-03-15 20:09:16 +0100544{
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200545#define CHECK_TYPE_VAL(type) \
546do { \
547 if (PARSE_EVENTS__TERM_TYPE_ ## type != term->type_val) \
548 return -EINVAL; \
549} while (0)
550
551 switch (term->type_term) {
Jiri Olsac056ba6a2015-04-22 21:10:18 +0200552 case PARSE_EVENTS__TERM_TYPE_USER:
553 /*
554 * Always succeed for sysfs terms, as we dont know
555 * at this point what type they need to have.
556 */
557 return 0;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100558 case PARSE_EVENTS__TERM_TYPE_CONFIG:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200559 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100560 attr->config = term->val.num;
561 break;
562 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200563 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100564 attr->config1 = term->val.num;
565 break;
566 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200567 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100568 attr->config2 = term->val.num;
569 break;
570 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200571 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100572 attr->sample_period = term->val.num;
573 break;
574 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
575 /*
576 * TODO uncomment when the field is available
577 * attr->branch_sample_type = term->val.num;
578 */
579 break;
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200580 case PARSE_EVENTS__TERM_TYPE_NAME:
581 CHECK_TYPE_VAL(STR);
582 break;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100583 default:
584 return -EINVAL;
585 }
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200586
Jiri Olsa8f707d82012-03-15 20:09:16 +0100587 return 0;
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200588#undef CHECK_TYPE_VAL
Jiri Olsa8f707d82012-03-15 20:09:16 +0100589}
590
591static int config_attr(struct perf_event_attr *attr,
Jiri Olsac056ba6a2015-04-22 21:10:18 +0200592 struct list_head *head)
Jiri Olsa8f707d82012-03-15 20:09:16 +0100593{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300594 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100595
596 list_for_each_entry(term, head, list)
Jiri Olsac056ba6a2015-04-22 21:10:18 +0200597 if (config_term(attr, term))
Jiri Olsa8f707d82012-03-15 20:09:16 +0100598 return -EINVAL;
599
600 return 0;
601}
602
David Ahernc5cd8ac2013-07-02 13:27:25 -0600603int parse_events_add_numeric(struct list_head *list, int *idx,
Robert Richterb527bab2012-08-07 19:43:13 +0200604 u32 type, u64 config,
Jiri Olsa8f707d82012-03-15 20:09:16 +0100605 struct list_head *head_config)
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530606{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100607 struct perf_event_attr attr;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530608
Jiri Olsa89812fc2012-03-15 20:09:15 +0100609 memset(&attr, 0, sizeof(attr));
610 attr.type = type;
611 attr.config = config;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100612
613 if (head_config &&
Jiri Olsac056ba6a2015-04-22 21:10:18 +0200614 config_attr(&attr, head_config))
Jiri Olsa8f707d82012-03-15 20:09:16 +0100615 return -EINVAL;
616
Arnaldo Carvalho de Melo9db17632012-06-12 13:45:00 -0300617 return add_event(list, idx, &attr, NULL);
Paul Mackerras61c45982009-07-01 13:04:34 +1000618}
619
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300620static int parse_events__is_name_term(struct parse_events_term *term)
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200621{
622 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
623}
624
Arnaldo Carvalho de Melo9db17632012-06-12 13:45:00 -0300625static char *pmu_event_name(struct list_head *head_terms)
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200626{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300627 struct parse_events_term *term;
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200628
629 list_for_each_entry(term, head_terms, list)
630 if (parse_events__is_name_term(term))
631 return term->val.str;
632
Arnaldo Carvalho de Melo9db17632012-06-12 13:45:00 -0300633 return NULL;
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200634}
635
Jiri Olsa36adec82015-04-22 21:10:19 +0200636int parse_events_add_pmu(struct parse_events_evlist *data,
637 struct list_head *list, char *name,
638 struct list_head *head_config)
Jiri Olsa5f537a22012-03-15 20:09:18 +0100639{
640 struct perf_event_attr attr;
Matt Fleming46441bd2014-09-24 15:04:06 +0100641 struct perf_pmu_info info;
Jiri Olsa5f537a22012-03-15 20:09:18 +0100642 struct perf_pmu *pmu;
Stephane Eranian410136f2013-11-12 17:58:49 +0100643 struct perf_evsel *evsel;
Jiri Olsa5f537a22012-03-15 20:09:18 +0100644
645 pmu = perf_pmu__find(name);
646 if (!pmu)
647 return -EINVAL;
648
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300649 if (pmu->default_config) {
650 memcpy(&attr, pmu->default_config,
651 sizeof(struct perf_event_attr));
652 } else {
653 memset(&attr, 0, sizeof(attr));
654 }
Jiri Olsa5f537a22012-03-15 20:09:18 +0100655
Adrian Hunterad962272014-08-15 22:08:40 +0300656 if (!head_config) {
657 attr.type = pmu->type;
Jiri Olsa36adec82015-04-22 21:10:19 +0200658 evsel = __add_event(list, &data->idx, &attr, NULL, pmu->cpus);
Adrian Hunterad962272014-08-15 22:08:40 +0300659 return evsel ? 0 : -ENOMEM;
660 }
661
Matt Fleming46441bd2014-09-24 15:04:06 +0100662 if (perf_pmu__check_alias(pmu, head_config, &info))
Zheng Yana6146d52012-06-15 14:31:41 +0800663 return -EINVAL;
664
Jiri Olsa5f537a22012-03-15 20:09:18 +0100665 /*
666 * Configure hardcoded terms first, no need to check
667 * return value when called with fail == 0 ;)
668 */
Jiri Olsac056ba6a2015-04-22 21:10:18 +0200669 if (config_attr(&attr, head_config))
670 return -EINVAL;
Jiri Olsa5f537a22012-03-15 20:09:18 +0100671
672 if (perf_pmu__config(pmu, &attr, head_config))
673 return -EINVAL;
674
Jiri Olsa36adec82015-04-22 21:10:19 +0200675 evsel = __add_event(list, &data->idx, &attr,
676 pmu_event_name(head_config), pmu->cpus);
Stephane Eranian410136f2013-11-12 17:58:49 +0100677 if (evsel) {
Matt Fleming46441bd2014-09-24 15:04:06 +0100678 evsel->unit = info.unit;
679 evsel->scale = info.scale;
Matt Fleming044330c2014-11-21 10:31:12 +0100680 evsel->per_pkg = info.per_pkg;
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100681 evsel->snapshot = info.snapshot;
Stephane Eranian410136f2013-11-12 17:58:49 +0100682 }
683
684 return evsel ? 0 : -ENOMEM;
Jiri Olsa5f537a22012-03-15 20:09:18 +0100685}
686
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200687int parse_events__modifier_group(struct list_head *list,
688 char *event_mod)
Jiri Olsa89efb022012-08-08 12:14:14 +0200689{
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200690 return parse_events__modifier_event(list, event_mod, true);
691}
692
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300693void parse_events__set_leader(char *name, struct list_head *list)
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200694{
695 struct perf_evsel *leader;
696
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300697 __perf_evlist__set_leader(list);
698 leader = list_entry(list->next, struct perf_evsel, node);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200699 leader->group_name = name ? strdup(name) : NULL;
Jiri Olsa89efb022012-08-08 12:14:14 +0200700}
701
David Ahernc5cd8ac2013-07-02 13:27:25 -0600702/* list_event is assumed to point to malloc'ed memory */
Jiri Olsa5d7be902012-03-20 19:15:40 +0100703void parse_events_update_lists(struct list_head *list_event,
704 struct list_head *list_all)
705{
706 /*
707 * Called for single event definition. Update the
Jiri Olsa89efb022012-08-08 12:14:14 +0200708 * 'all event' list, and reinit the 'single event'
Jiri Olsa5d7be902012-03-20 19:15:40 +0100709 * list, for next event definition.
710 */
711 list_splice_tail(list_event, list_all);
Jiri Olsab847cbd2012-05-21 09:12:51 +0200712 free(list_event);
Jiri Olsa5d7be902012-03-20 19:15:40 +0100713}
714
Jiri Olsaf5b11352012-08-08 12:21:54 +0200715struct event_modifier {
716 int eu;
717 int ek;
718 int eh;
719 int eH;
720 int eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +0200721 int eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +0200722 int precise;
723 int exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +0200724 int sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +1000725 int pinned;
Jiri Olsaf5b11352012-08-08 12:21:54 +0200726};
Paul Mackerras61c45982009-07-01 13:04:34 +1000727
Jiri Olsaf5b11352012-08-08 12:21:54 +0200728static int get_event_modifier(struct event_modifier *mod, char *str,
729 struct perf_evsel *evsel)
730{
731 int eu = evsel ? evsel->attr.exclude_user : 0;
732 int ek = evsel ? evsel->attr.exclude_kernel : 0;
733 int eh = evsel ? evsel->attr.exclude_hv : 0;
734 int eH = evsel ? evsel->attr.exclude_host : 0;
735 int eG = evsel ? evsel->attr.exclude_guest : 0;
Jiri Olsaa1e12da2015-04-07 23:25:14 +0200736 int eI = evsel ? evsel->attr.exclude_idle : 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +0200737 int precise = evsel ? evsel->attr.precise_ip : 0;
Jiri Olsa3c176312012-10-10 17:39:03 +0200738 int sample_read = 0;
Michael Ellermane9a7c412013-08-06 23:28:05 +1000739 int pinned = evsel ? evsel->attr.pinned : 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +0200740
741 int exclude = eu | ek | eh;
742 int exclude_GH = evsel ? evsel->exclude_GH : 0;
743
Jiri Olsaf5b11352012-08-08 12:21:54 +0200744 memset(mod, 0, sizeof(*mod));
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200745
Paul Mackerras61c45982009-07-01 13:04:34 +1000746 while (*str) {
Peter Zijlstraab608342010-04-08 23:03:20 +0200747 if (*str == 'u') {
748 if (!exclude)
749 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000750 eu = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200751 } else if (*str == 'k') {
752 if (!exclude)
753 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000754 ek = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200755 } else if (*str == 'h') {
756 if (!exclude)
757 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000758 eh = 0;
Joerg Roedel99320cc2012-01-04 17:54:19 +0100759 } else if (*str == 'G') {
760 if (!exclude_GH)
761 exclude_GH = eG = eH = 1;
762 eG = 0;
763 } else if (*str == 'H') {
764 if (!exclude_GH)
765 exclude_GH = eG = eH = 1;
766 eH = 0;
Jiri Olsaa1e12da2015-04-07 23:25:14 +0200767 } else if (*str == 'I') {
768 eI = 1;
Peter Zijlstraab608342010-04-08 23:03:20 +0200769 } else if (*str == 'p') {
770 precise++;
David Ahern13427982012-09-13 14:59:13 -0600771 /* use of precise requires exclude_guest */
772 if (!exclude_GH)
773 eG = 1;
Jiri Olsa3c176312012-10-10 17:39:03 +0200774 } else if (*str == 'S') {
775 sample_read = 1;
Michael Ellermane9a7c412013-08-06 23:28:05 +1000776 } else if (*str == 'D') {
777 pinned = 1;
Peter Zijlstraab608342010-04-08 23:03:20 +0200778 } else
Paul Mackerras61c45982009-07-01 13:04:34 +1000779 break;
Peter Zijlstraab608342010-04-08 23:03:20 +0200780
Paul Mackerras61c45982009-07-01 13:04:34 +1000781 ++str;
782 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200783
Jiri Olsa89812fc2012-03-15 20:09:15 +0100784 /*
785 * precise ip:
786 *
787 * 0 - SAMPLE_IP can have arbitrary skid
788 * 1 - SAMPLE_IP must have constant skid
789 * 2 - SAMPLE_IP requested to have 0 skid
790 * 3 - SAMPLE_IP must have 0 skid
791 *
792 * See also PERF_RECORD_MISC_EXACT_IP
793 */
794 if (precise > 3)
795 return -EINVAL;
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200796
Jiri Olsaf5b11352012-08-08 12:21:54 +0200797 mod->eu = eu;
798 mod->ek = ek;
799 mod->eh = eh;
800 mod->eH = eH;
801 mod->eG = eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +0200802 mod->eI = eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +0200803 mod->precise = precise;
804 mod->exclude_GH = exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +0200805 mod->sample_read = sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +1000806 mod->pinned = pinned;
807
Jiri Olsaf5b11352012-08-08 12:21:54 +0200808 return 0;
809}
810
Jiri Olsa534123f2012-11-13 15:32:58 +0100811/*
812 * Basic modifier sanity check to validate it contains only one
813 * instance of any modifier (apart from 'p') present.
814 */
815static int check_modifier(char *str)
816{
817 char *p = str;
818
819 /* The sizeof includes 0 byte as well. */
Jiri Olsaa1e12da2015-04-07 23:25:14 +0200820 if (strlen(str) > (sizeof("ukhGHpppSDI") - 1))
Jiri Olsa534123f2012-11-13 15:32:58 +0100821 return -1;
822
823 while (*p) {
824 if (*p != 'p' && strchr(p + 1, *p))
825 return -1;
826 p++;
827 }
828
829 return 0;
830}
831
Jiri Olsaf5b11352012-08-08 12:21:54 +0200832int parse_events__modifier_event(struct list_head *list, char *str, bool add)
833{
834 struct perf_evsel *evsel;
835 struct event_modifier mod;
836
837 if (str == NULL)
838 return 0;
839
Jiri Olsa534123f2012-11-13 15:32:58 +0100840 if (check_modifier(str))
841 return -EINVAL;
842
Jiri Olsaf5b11352012-08-08 12:21:54 +0200843 if (!add && get_event_modifier(&mod, str, NULL))
844 return -EINVAL;
845
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300846 __evlist__for_each(list, evsel) {
Jiri Olsaf5b11352012-08-08 12:21:54 +0200847 if (add && get_event_modifier(&mod, str, evsel))
848 return -EINVAL;
849
850 evsel->attr.exclude_user = mod.eu;
851 evsel->attr.exclude_kernel = mod.ek;
852 evsel->attr.exclude_hv = mod.eh;
853 evsel->attr.precise_ip = mod.precise;
854 evsel->attr.exclude_host = mod.eH;
855 evsel->attr.exclude_guest = mod.eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +0200856 evsel->attr.exclude_idle = mod.eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +0200857 evsel->exclude_GH = mod.exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +0200858 evsel->sample_read = mod.sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +1000859
860 if (perf_evsel__is_group_leader(evsel))
861 evsel->attr.pinned = mod.pinned;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100862 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200863
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530864 return 0;
865}
866
Robert Richterac2ba9f2012-08-16 21:10:21 +0200867int parse_events_name(struct list_head *list, char *name)
868{
869 struct perf_evsel *evsel;
870
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300871 __evlist__for_each(list, evsel) {
Robert Richterac2ba9f2012-08-16 21:10:21 +0200872 if (!evsel->name)
873 evsel->name = strdup(name);
874 }
875
876 return 0;
877}
878
Kan Liangdcb4e102014-10-07 11:08:50 -0400879static int
880comp_pmu(const void *p1, const void *p2)
881{
882 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
883 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
884
885 return strcmp(pmu1->symbol, pmu2->symbol);
886}
887
888static void perf_pmu__parse_cleanup(void)
889{
890 if (perf_pmu_events_list_num > 0) {
891 struct perf_pmu_event_symbol *p;
892 int i;
893
894 for (i = 0; i < perf_pmu_events_list_num; i++) {
895 p = perf_pmu_events_list + i;
896 free(p->symbol);
897 }
898 free(perf_pmu_events_list);
899 perf_pmu_events_list = NULL;
900 perf_pmu_events_list_num = 0;
901 }
902}
903
904#define SET_SYMBOL(str, stype) \
905do { \
906 p->symbol = str; \
907 if (!p->symbol) \
908 goto err; \
909 p->type = stype; \
910} while (0)
911
912/*
913 * Read the pmu events list from sysfs
914 * Save it into perf_pmu_events_list
915 */
916static void perf_pmu__parse_init(void)
917{
918
919 struct perf_pmu *pmu = NULL;
920 struct perf_pmu_alias *alias;
921 int len = 0;
922
923 pmu = perf_pmu__find("cpu");
924 if ((pmu == NULL) || list_empty(&pmu->aliases)) {
925 perf_pmu_events_list_num = -1;
926 return;
927 }
928 list_for_each_entry(alias, &pmu->aliases, list) {
929 if (strchr(alias->name, '-'))
930 len++;
931 len++;
932 }
933 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
934 if (!perf_pmu_events_list)
935 return;
936 perf_pmu_events_list_num = len;
937
938 len = 0;
939 list_for_each_entry(alias, &pmu->aliases, list) {
940 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
941 char *tmp = strchr(alias->name, '-');
942
943 if (tmp != NULL) {
944 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
945 PMU_EVENT_SYMBOL_PREFIX);
946 p++;
947 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
948 len += 2;
949 } else {
950 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
951 len++;
952 }
953 }
954 qsort(perf_pmu_events_list, len,
955 sizeof(struct perf_pmu_event_symbol), comp_pmu);
956
957 return;
958err:
959 perf_pmu__parse_cleanup();
960}
961
962enum perf_pmu_event_symbol_type
963perf_pmu__parse_check(const char *name)
964{
965 struct perf_pmu_event_symbol p, *r;
966
967 /* scan kernel pmu events from sysfs if needed */
968 if (perf_pmu_events_list_num == 0)
969 perf_pmu__parse_init();
970 /*
971 * name "cpu" could be prefix of cpu-cycles or cpu// events.
972 * cpu-cycles has been handled by hardcode.
973 * So it must be cpu// events, not kernel pmu event.
974 */
975 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
976 return PMU_EVENT_SYMBOL_ERR;
977
978 p.symbol = strdup(name);
979 r = bsearch(&p, perf_pmu_events_list,
980 (size_t) perf_pmu_events_list_num,
981 sizeof(struct perf_pmu_event_symbol), comp_pmu);
982 free(p.symbol);
983 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
984}
985
Jiri Olsa90e2b222012-06-15 14:31:40 +0800986static int parse_events__scanner(const char *str, void *data, int start_token)
Zheng Yanac20de6f2012-06-15 14:31:39 +0800987{
988 YY_BUFFER_STATE buffer;
989 void *scanner;
990 int ret;
991
Jiri Olsa90e2b222012-06-15 14:31:40 +0800992 ret = parse_events_lex_init_extra(start_token, &scanner);
Zheng Yanac20de6f2012-06-15 14:31:39 +0800993 if (ret)
994 return ret;
995
996 buffer = parse_events__scan_string(str, scanner);
997
998#ifdef PARSER_DEBUG
999 parse_events_debug = 1;
1000#endif
1001 ret = parse_events_parse(data, scanner);
1002
1003 parse_events__flush_buffer(buffer, scanner);
1004 parse_events__delete_buffer(buffer, scanner);
1005 parse_events_lex_destroy(scanner);
1006 return ret;
1007}
1008
Jiri Olsa90e2b222012-06-15 14:31:40 +08001009/*
1010 * parse event config string, return a list of event terms.
1011 */
1012int parse_events_terms(struct list_head *terms, const char *str)
1013{
Arnaldo Carvalho de Melo23b63392013-01-18 16:56:57 -03001014 struct parse_events_terms data = {
Jiri Olsa90e2b222012-06-15 14:31:40 +08001015 .terms = NULL,
1016 };
1017 int ret;
1018
1019 ret = parse_events__scanner(str, &data, PE_START_TERMS);
1020 if (!ret) {
1021 list_splice(data.terms, terms);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001022 zfree(&data.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001023 return 0;
1024 }
1025
Adrian Hunterb2c34fd2013-07-04 16:20:23 +03001026 if (data.terms)
1027 parse_events__free_terms(data.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001028 return ret;
1029}
1030
Jiri Olsab39b8392015-04-22 21:10:16 +02001031int parse_events(struct perf_evlist *evlist, const char *str,
1032 struct parse_events_error *err)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001033{
Arnaldo Carvalho de Melo23b63392013-01-18 16:56:57 -03001034 struct parse_events_evlist data = {
Jiri Olsab39b8392015-04-22 21:10:16 +02001035 .list = LIST_HEAD_INIT(data.list),
1036 .idx = evlist->nr_entries,
1037 .error = err,
Jiri Olsa46010ab2012-06-15 14:31:38 +08001038 };
Jiri Olsa46010ab2012-06-15 14:31:38 +08001039 int ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001040
Jiri Olsa90e2b222012-06-15 14:31:40 +08001041 ret = parse_events__scanner(str, &data, PE_START_EVENTS);
Kan Liangdcb4e102014-10-07 11:08:50 -04001042 perf_pmu__parse_cleanup();
Jiri Olsa89812fc2012-03-15 20:09:15 +01001043 if (!ret) {
Jiri Olsa46010ab2012-06-15 14:31:38 +08001044 int entries = data.idx - evlist->nr_entries;
1045 perf_evlist__splice_list_tail(evlist, &data.list, entries);
Namhyung Kim97f63e42013-01-22 18:09:29 +09001046 evlist->nr_groups += data.nr_groups;
Jiri Olsa89812fc2012-03-15 20:09:15 +01001047 return 0;
1048 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02001049
Jiri Olsa5d7be902012-03-20 19:15:40 +01001050 /*
1051 * There are 2 users - builtin-record and builtin-test objects.
1052 * Both call perf_evlist__delete in case of error, so we dont
1053 * need to bother.
1054 */
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02001055 return ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001056}
1057
Jiri Olsab39b8392015-04-22 21:10:16 +02001058#define MAX_WIDTH 1000
1059static int get_term_width(void)
1060{
1061 struct winsize ws;
1062
1063 get_term_dimensions(&ws);
1064 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1065}
1066
1067static void parse_events_print_error(struct parse_events_error *err,
1068 const char *event)
1069{
1070 const char *str = "invalid or unsupported event: ";
1071 char _buf[MAX_WIDTH];
1072 char *buf = (char *) event;
1073 int idx = 0;
1074
1075 if (err->str) {
1076 /* -2 for extra '' in the final fprintf */
1077 int width = get_term_width() - 2;
1078 int len_event = strlen(event);
1079 int len_str, max_len, cut = 0;
1080
1081 /*
1082 * Maximum error index indent, we will cut
1083 * the event string if it's bigger.
1084 */
1085 int max_err_idx = 10;
1086
1087 /*
1088 * Let's be specific with the message when
1089 * we have the precise error.
1090 */
1091 str = "event syntax error: ";
1092 len_str = strlen(str);
1093 max_len = width - len_str;
1094
1095 buf = _buf;
1096
1097 /* We're cutting from the beggining. */
1098 if (err->idx > max_err_idx)
1099 cut = err->idx - max_err_idx;
1100
1101 strncpy(buf, event + cut, max_len);
1102
1103 /* Mark cut parts with '..' on both sides. */
1104 if (cut)
1105 buf[0] = buf[1] = '.';
1106
1107 if ((len_event - cut) > max_len) {
1108 buf[max_len - 1] = buf[max_len - 2] = '.';
1109 buf[max_len] = 0;
1110 }
1111
1112 idx = len_str + err->idx - cut;
1113 }
1114
1115 fprintf(stderr, "%s'%s'\n", str, buf);
1116 if (idx) {
1117 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
1118 if (err->help)
1119 fprintf(stderr, "\n%s\n", err->help);
1120 free(err->str);
1121 free(err->help);
1122 }
1123
1124 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
1125}
1126
1127#undef MAX_WIDTH
1128
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001129int parse_events_option(const struct option *opt, const char *str,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001130 int unset __maybe_unused)
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001131{
1132 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
Jiri Olsab39b8392015-04-22 21:10:16 +02001133 struct parse_events_error err = { .idx = 0, };
1134 int ret = parse_events(evlist, str, &err);
Andi Kleen9175ce12012-10-26 13:30:06 -07001135
Jiri Olsab39b8392015-04-22 21:10:16 +02001136 if (ret)
1137 parse_events_print_error(&err, str);
1138
Andi Kleen9175ce12012-10-26 13:30:06 -07001139 return ret;
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001140}
1141
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001142int parse_filter(const struct option *opt, const char *str,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001143 int unset __maybe_unused)
Li Zefanc171b552009-10-15 11:22:07 +08001144{
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001145 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001146 struct perf_evsel *last = NULL;
Li Zefanc171b552009-10-15 11:22:07 +08001147
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001148 if (evlist->nr_entries > 0)
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001149 last = perf_evlist__last(evlist);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001150
1151 if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
Li Zefanc171b552009-10-15 11:22:07 +08001152 fprintf(stderr,
Arnaldo Carvalho de Melo281f92f2014-10-01 15:05:32 -03001153 "--filter option should follow a -e tracepoint option\n");
Li Zefanc171b552009-10-15 11:22:07 +08001154 return -1;
1155 }
1156
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001157 last->filter = strdup(str);
1158 if (last->filter == NULL) {
Li Zefanc171b552009-10-15 11:22:07 +08001159 fprintf(stderr, "not enough memory to hold filter string\n");
1160 return -1;
1161 }
Li Zefanc171b552009-10-15 11:22:07 +08001162
1163 return 0;
1164}
1165
Thomas Gleixner86847b62009-06-06 12:24:17 +02001166static const char * const event_type_descriptors[] = {
Thomas Gleixner86847b62009-06-06 12:24:17 +02001167 "Hardware event",
1168 "Software event",
1169 "Tracepoint event",
1170 "Hardware cache event",
Liming Wang41bdcb22009-12-29 16:37:07 +08001171 "Raw hardware event descriptor",
1172 "Hardware breakpoint",
Thomas Gleixner86847b62009-06-06 12:24:17 +02001173};
1174
Yunlong Songab0e4802015-02-27 18:21:25 +08001175static int cmp_string(const void *a, const void *b)
1176{
1177 const char * const *as = a;
1178 const char * const *bs = b;
1179
1180 return strcmp(*as, *bs);
1181}
1182
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001183/*
Jason Baronf6bdafe2009-07-21 12:20:22 -04001184 * Print the events from <debugfs_mount_point>/tracing/events
1185 */
1186
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001187void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
1188 bool name_only)
Jason Baronf6bdafe2009-07-21 12:20:22 -04001189{
1190 DIR *sys_dir, *evt_dir;
1191 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001192 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +02001193 char dir_path[MAXPATHLEN];
Yunlong Songab0e4802015-02-27 18:21:25 +08001194 char **evt_list = NULL;
1195 unsigned int evt_i = 0, evt_num = 0;
1196 bool evt_num_known = false;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001197
Yunlong Songab0e4802015-02-27 18:21:25 +08001198restart:
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001199 sys_dir = opendir(tracing_events_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -04001200 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +02001201 return;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001202
Yunlong Songab0e4802015-02-27 18:21:25 +08001203 if (evt_num_known) {
1204 evt_list = zalloc(sizeof(char *) * evt_num);
1205 if (!evt_list)
1206 goto out_close_sys_dir;
1207 }
1208
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03001209 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001210 if (subsys_glob != NULL &&
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001211 !strglobmatch(sys_dirent.d_name, subsys_glob))
1212 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02001213
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001214 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Eric Dumazet725b1362009-09-24 15:39:09 +02001215 sys_dirent.d_name);
1216 evt_dir = opendir(dir_path);
1217 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03001218 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02001219
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03001220 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001221 if (event_glob != NULL &&
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001222 !strglobmatch(evt_dirent.d_name, event_glob))
1223 continue;
1224
Yunlong Songab0e4802015-02-27 18:21:25 +08001225 if (!evt_num_known) {
1226 evt_num++;
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001227 continue;
1228 }
1229
Jason Baronf6bdafe2009-07-21 12:20:22 -04001230 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1231 sys_dirent.d_name, evt_dirent.d_name);
Yunlong Songab0e4802015-02-27 18:21:25 +08001232
1233 evt_list[evt_i] = strdup(evt_path);
1234 if (evt_list[evt_i] == NULL)
1235 goto out_close_evt_dir;
1236 evt_i++;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001237 }
1238 closedir(evt_dir);
1239 }
Jason Baronf6bdafe2009-07-21 12:20:22 -04001240 closedir(sys_dir);
Yunlong Songab0e4802015-02-27 18:21:25 +08001241
1242 if (!evt_num_known) {
1243 evt_num_known = true;
1244 goto restart;
1245 }
1246 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1247 evt_i = 0;
1248 while (evt_i < evt_num) {
1249 if (name_only) {
1250 printf("%s ", evt_list[evt_i++]);
1251 continue;
1252 }
1253 printf(" %-50s [%s]\n", evt_list[evt_i++],
1254 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1255 }
1256 if (evt_num)
1257 printf("\n");
1258
1259out_free:
1260 evt_num = evt_i;
1261 for (evt_i = 0; evt_i < evt_num; evt_i++)
1262 zfree(&evt_list[evt_i]);
1263 zfree(&evt_list);
1264 return;
1265
1266out_close_evt_dir:
1267 closedir(evt_dir);
1268out_close_sys_dir:
1269 closedir(sys_dir);
1270
1271 printf("FATAL: not enough memory to print %s\n",
1272 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1273 if (evt_list)
1274 goto out_free;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001275}
1276
1277/*
Thomas Renninger20c457b2011-01-03 17:50:45 +01001278 * Check whether event is in <debugfs_mount_point>/tracing/events
1279 */
1280
1281int is_valid_tracepoint(const char *event_string)
1282{
1283 DIR *sys_dir, *evt_dir;
1284 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
1285 char evt_path[MAXPATHLEN];
1286 char dir_path[MAXPATHLEN];
1287
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001288 sys_dir = opendir(tracing_events_path);
Thomas Renninger20c457b2011-01-03 17:50:45 +01001289 if (!sys_dir)
1290 return 0;
1291
1292 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1293
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001294 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Thomas Renninger20c457b2011-01-03 17:50:45 +01001295 sys_dirent.d_name);
1296 evt_dir = opendir(dir_path);
1297 if (!evt_dir)
1298 continue;
1299
1300 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1301 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1302 sys_dirent.d_name, evt_dirent.d_name);
1303 if (!strcmp(evt_path, event_string)) {
1304 closedir(evt_dir);
1305 closedir(sys_dir);
1306 return 1;
1307 }
1308 }
1309 closedir(evt_dir);
1310 }
1311 closedir(sys_dir);
1312 return 0;
1313}
1314
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001315static bool is_event_supported(u8 type, unsigned config)
1316{
1317 bool ret = true;
Vince Weaver88fee522013-12-30 15:39:45 -05001318 int open_return;
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001319 struct perf_evsel *evsel;
1320 struct perf_event_attr attr = {
1321 .type = type,
1322 .config = config,
1323 .disabled = 1,
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001324 };
1325 struct {
1326 struct thread_map map;
1327 int threads[1];
1328 } tmap = {
1329 .map.nr = 1,
1330 .threads = { 0 },
1331 };
1332
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03001333 evsel = perf_evsel__new(&attr);
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001334 if (evsel) {
Vince Weaver88fee522013-12-30 15:39:45 -05001335 open_return = perf_evsel__open(evsel, NULL, &tmap.map);
1336 ret = open_return >= 0;
1337
1338 if (open_return == -EACCES) {
1339 /*
1340 * This happens if the paranoid value
1341 * /proc/sys/kernel/perf_event_paranoid is set to 2
1342 * Re-run with exclude_kernel set; we don't do that
1343 * by default as some ARM machines do not support it.
1344 *
1345 */
1346 evsel->attr.exclude_kernel = 1;
1347 ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0;
1348 }
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001349 perf_evsel__delete(evsel);
1350 }
1351
1352 return ret;
1353}
1354
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001355int print_hwcache_events(const char *event_glob, bool name_only)
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001356{
Yunlong Songab0e4802015-02-27 18:21:25 +08001357 unsigned int type, op, i, evt_i = 0, evt_num = 0;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03001358 char name[64];
Yunlong Songab0e4802015-02-27 18:21:25 +08001359 char **evt_list = NULL;
1360 bool evt_num_known = false;
1361
1362restart:
1363 if (evt_num_known) {
1364 evt_list = zalloc(sizeof(char *) * evt_num);
1365 if (!evt_list)
1366 goto out_enomem;
1367 }
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001368
1369 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1370 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1371 /* skip invalid cache type */
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03001372 if (!perf_evsel__is_cache_op_valid(type, op))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001373 continue;
1374
1375 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03001376 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
1377 name, sizeof(name));
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001378 if (event_glob != NULL && !strglobmatch(name, event_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001379 continue;
1380
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001381 if (!is_event_supported(PERF_TYPE_HW_CACHE,
1382 type | (op << 8) | (i << 16)))
1383 continue;
1384
Yunlong Songab0e4802015-02-27 18:21:25 +08001385 if (!evt_num_known) {
1386 evt_num++;
1387 continue;
1388 }
1389
1390 evt_list[evt_i] = strdup(name);
1391 if (evt_list[evt_i] == NULL)
1392 goto out_enomem;
1393 evt_i++;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001394 }
1395 }
1396 }
1397
Yunlong Songab0e4802015-02-27 18:21:25 +08001398 if (!evt_num_known) {
1399 evt_num_known = true;
1400 goto restart;
1401 }
1402 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1403 evt_i = 0;
1404 while (evt_i < evt_num) {
1405 if (name_only) {
1406 printf("%s ", evt_list[evt_i++]);
1407 continue;
1408 }
1409 printf(" %-50s [%s]\n", evt_list[evt_i++],
1410 event_type_descriptors[PERF_TYPE_HW_CACHE]);
1411 }
1412 if (evt_num)
Andi Kleendc098b32013-04-20 11:02:29 -07001413 printf("\n");
Yunlong Songab0e4802015-02-27 18:21:25 +08001414
1415out_free:
1416 evt_num = evt_i;
1417 for (evt_i = 0; evt_i < evt_num; evt_i++)
1418 zfree(&evt_list[evt_i]);
1419 zfree(&evt_list);
1420 return evt_num;
1421
1422out_enomem:
1423 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
1424 if (evt_list)
1425 goto out_free;
1426 return evt_num;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001427}
1428
Yunlong Song705750f2015-02-27 18:21:27 +08001429void print_symbol_events(const char *event_glob, unsigned type,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001430 struct event_symbol *syms, unsigned max,
1431 bool name_only)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001432{
Yunlong Songab0e4802015-02-27 18:21:25 +08001433 unsigned int i, evt_i = 0, evt_num = 0;
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001434 char name[MAX_NAME_LEN];
Yunlong Songab0e4802015-02-27 18:21:25 +08001435 char **evt_list = NULL;
1436 bool evt_num_known = false;
1437
1438restart:
1439 if (evt_num_known) {
1440 evt_list = zalloc(sizeof(char *) * evt_num);
1441 if (!evt_list)
1442 goto out_enomem;
1443 syms -= max;
1444 }
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001445
Jiri Olsa1dc127602012-07-04 00:00:44 +02001446 for (i = 0; i < max; i++, syms++) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001447
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001448 if (event_glob != NULL &&
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001449 !(strglobmatch(syms->symbol, event_glob) ||
1450 (syms->alias && strglobmatch(syms->alias, event_glob))))
1451 continue;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001452
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001453 if (!is_event_supported(type, i))
1454 continue;
1455
Yunlong Songab0e4802015-02-27 18:21:25 +08001456 if (!evt_num_known) {
1457 evt_num++;
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001458 continue;
1459 }
1460
Yunlong Songab0e4802015-02-27 18:21:25 +08001461 if (!name_only && strlen(syms->alias))
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001462 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301463 else
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001464 strncpy(name, syms->symbol, MAX_NAME_LEN);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001465
Yunlong Songab0e4802015-02-27 18:21:25 +08001466 evt_list[evt_i] = strdup(name);
1467 if (evt_list[evt_i] == NULL)
1468 goto out_enomem;
1469 evt_i++;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001470 }
1471
Yunlong Songab0e4802015-02-27 18:21:25 +08001472 if (!evt_num_known) {
1473 evt_num_known = true;
1474 goto restart;
1475 }
1476 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1477 evt_i = 0;
1478 while (evt_i < evt_num) {
1479 if (name_only) {
1480 printf("%s ", evt_list[evt_i++]);
1481 continue;
1482 }
1483 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
1484 }
1485 if (evt_num)
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001486 printf("\n");
Yunlong Songab0e4802015-02-27 18:21:25 +08001487
1488out_free:
1489 evt_num = evt_i;
1490 for (evt_i = 0; evt_i < evt_num; evt_i++)
1491 zfree(&evt_list[evt_i]);
1492 zfree(&evt_list);
1493 return;
1494
1495out_enomem:
1496 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
1497 if (evt_list)
1498 goto out_free;
Jiri Olsa1dc127602012-07-04 00:00:44 +02001499}
1500
1501/*
1502 * Print the help text for the event symbols:
1503 */
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001504void print_events(const char *event_glob, bool name_only)
Jiri Olsa1dc127602012-07-04 00:00:44 +02001505{
Jiri Olsa1dc127602012-07-04 00:00:44 +02001506 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001507 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
Jiri Olsa1dc127602012-07-04 00:00:44 +02001508
1509 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001510 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
Jiri Olsa1dc127602012-07-04 00:00:44 +02001511
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001512 print_hwcache_events(event_glob, name_only);
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001513
Andi Kleendc098b32013-04-20 11:02:29 -07001514 print_pmu_events(event_glob, name_only);
1515
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001516 if (event_glob != NULL)
1517 return;
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +05301518
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001519 if (!name_only) {
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001520 printf(" %-50s [%s]\n",
1521 "rNNN",
1522 event_type_descriptors[PERF_TYPE_RAW]);
1523 printf(" %-50s [%s]\n",
1524 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
1525 event_type_descriptors[PERF_TYPE_RAW]);
Borislav Petkovaf3df2c2012-09-28 19:47:07 +02001526 printf(" (see 'man perf-list' on how to encode it)\n");
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001527 printf("\n");
Thomas Gleixner86847b62009-06-06 12:24:17 +02001528
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001529 printf(" %-50s [%s]\n",
Jacob Shin3741eb92014-05-29 17:26:51 +02001530 "mem:<addr>[/len][:access]",
Liming Wang41bdcb22009-12-29 16:37:07 +08001531 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001532 printf("\n");
1533 }
Frederic Weisbecker1b290d62009-11-23 15:42:35 +01001534
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001535 print_tracepoint_events(NULL, NULL, name_only);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001536}
Jiri Olsa8f707d82012-03-15 20:09:16 +01001537
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001538int parse_events__is_hardcoded_term(struct parse_events_term *term)
Jiri Olsa8f707d82012-03-15 20:09:16 +01001539{
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001540 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001541}
1542
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001543static int new_term(struct parse_events_term **_term, int type_val,
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001544 int type_term, char *config,
Robert Richterb527bab2012-08-07 19:43:13 +02001545 char *str, u64 num)
Jiri Olsa8f707d82012-03-15 20:09:16 +01001546{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001547 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001548
1549 term = zalloc(sizeof(*term));
1550 if (!term)
1551 return -ENOMEM;
1552
1553 INIT_LIST_HEAD(&term->list);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001554 term->type_val = type_val;
1555 term->type_term = type_term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001556 term->config = config;
1557
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001558 switch (type_val) {
Jiri Olsa8f707d82012-03-15 20:09:16 +01001559 case PARSE_EVENTS__TERM_TYPE_NUM:
1560 term->val.num = num;
1561 break;
1562 case PARSE_EVENTS__TERM_TYPE_STR:
1563 term->val.str = str;
1564 break;
1565 default:
Adrian Hunter4be8be62013-07-04 16:20:24 +03001566 free(term);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001567 return -EINVAL;
1568 }
1569
1570 *_term = term;
1571 return 0;
1572}
1573
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001574int parse_events_term__num(struct parse_events_term **term,
Robert Richterb527bab2012-08-07 19:43:13 +02001575 int type_term, char *config, u64 num)
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001576{
1577 return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
1578 config, NULL, num);
1579}
1580
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001581int parse_events_term__str(struct parse_events_term **term,
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001582 int type_term, char *config, char *str)
1583{
1584 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
1585 config, str, 0);
1586}
1587
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001588int parse_events_term__sym_hw(struct parse_events_term **term,
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02001589 char *config, unsigned idx)
1590{
1591 struct event_symbol *sym;
1592
1593 BUG_ON(idx >= PERF_COUNT_HW_MAX);
1594 sym = &event_symbols_hw[idx];
1595
1596 if (config)
1597 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1598 PARSE_EVENTS__TERM_TYPE_USER, config,
1599 (char *) sym->symbol, 0);
1600 else
1601 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1602 PARSE_EVENTS__TERM_TYPE_USER,
1603 (char *) "event", (char *) sym->symbol, 0);
1604}
1605
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001606int parse_events_term__clone(struct parse_events_term **new,
1607 struct parse_events_term *term)
Zheng Yana6146d52012-06-15 14:31:41 +08001608{
1609 return new_term(new, term->type_val, term->type_term, term->config,
1610 term->val.str, term->val.num);
1611}
1612
Jiri Olsa8f707d82012-03-15 20:09:16 +01001613void parse_events__free_terms(struct list_head *terms)
1614{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001615 struct parse_events_term *term, *h;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001616
1617 list_for_each_entry_safe(term, h, terms, list)
1618 free(term);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001619}
Jiri Olsab39b8392015-04-22 21:10:16 +02001620
1621void parse_events_evlist_error(struct parse_events_evlist *data,
1622 int idx, const char *str)
1623{
1624 struct parse_events_error *err = data->error;
1625
1626 err->idx = idx;
1627 err->str = strdup(str);
1628 WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
1629}