blob: 7f8ec6ce2823c4652e04f11618af1b5cfcb4b952 [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 Yanac20de62012-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 Olsa89812fc2012-03-15 20:09:15 +010020
21#define MAX_NAME_LEN 100
Ingo Molnar8ad8db32009-05-26 11:10:09 +020022
Ingo Molnar8ad8db32009-05-26 11:10:09 +020023struct event_symbol {
Ingo Molnar83a09442009-08-15 12:26:57 +020024 const char *symbol;
25 const char *alias;
Ingo Molnar8ad8db32009-05-26 11:10:09 +020026};
27
Jiri Olsa82ba1f22012-05-21 09:12:50 +020028#ifdef PARSER_DEBUG
29extern int parse_events_debug;
30#endif
Zheng Yanac20de62012-06-15 14:31:39 +080031int parse_events_parse(void *data, void *scanner);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +020032
Kan Liangdcb4e102014-10-07 11:08:50 -040033static struct perf_pmu_event_symbol *perf_pmu_events_list;
34/*
35 * The variable indicates the number of supported pmu event symbols.
36 * 0 means not initialized and ready to init
37 * -1 means failed to init, don't try anymore
38 * >0 is the number of supported pmu event symbols
39 */
40static int perf_pmu_events_list_num;
41
Jiri Olsa1dc12762012-07-04 00:00:44 +020042static struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
43 [PERF_COUNT_HW_CPU_CYCLES] = {
44 .symbol = "cpu-cycles",
45 .alias = "cycles",
46 },
47 [PERF_COUNT_HW_INSTRUCTIONS] = {
48 .symbol = "instructions",
49 .alias = "",
50 },
51 [PERF_COUNT_HW_CACHE_REFERENCES] = {
52 .symbol = "cache-references",
53 .alias = "",
54 },
55 [PERF_COUNT_HW_CACHE_MISSES] = {
56 .symbol = "cache-misses",
57 .alias = "",
58 },
59 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
60 .symbol = "branch-instructions",
61 .alias = "branches",
62 },
63 [PERF_COUNT_HW_BRANCH_MISSES] = {
64 .symbol = "branch-misses",
65 .alias = "",
66 },
67 [PERF_COUNT_HW_BUS_CYCLES] = {
68 .symbol = "bus-cycles",
69 .alias = "",
70 },
71 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
72 .symbol = "stalled-cycles-frontend",
73 .alias = "idle-cycles-frontend",
74 },
75 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
76 .symbol = "stalled-cycles-backend",
77 .alias = "idle-cycles-backend",
78 },
79 [PERF_COUNT_HW_REF_CPU_CYCLES] = {
80 .symbol = "ref-cycles",
81 .alias = "",
82 },
83};
Ingo Molnar8ad8db32009-05-26 11:10:09 +020084
Jiri Olsa1dc12762012-07-04 00:00:44 +020085static struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
86 [PERF_COUNT_SW_CPU_CLOCK] = {
87 .symbol = "cpu-clock",
88 .alias = "",
89 },
90 [PERF_COUNT_SW_TASK_CLOCK] = {
91 .symbol = "task-clock",
92 .alias = "",
93 },
94 [PERF_COUNT_SW_PAGE_FAULTS] = {
95 .symbol = "page-faults",
96 .alias = "faults",
97 },
98 [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
99 .symbol = "context-switches",
100 .alias = "cs",
101 },
102 [PERF_COUNT_SW_CPU_MIGRATIONS] = {
103 .symbol = "cpu-migrations",
104 .alias = "migrations",
105 },
106 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
107 .symbol = "minor-faults",
108 .alias = "",
109 },
110 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
111 .symbol = "major-faults",
112 .alias = "",
113 },
114 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
115 .symbol = "alignment-faults",
116 .alias = "",
117 },
118 [PERF_COUNT_SW_EMULATION_FAULTS] = {
119 .symbol = "emulation-faults",
120 .alias = "",
121 },
Adrian Hunterd22d1a22013-08-31 21:50:52 +0300122 [PERF_COUNT_SW_DUMMY] = {
123 .symbol = "dummy",
124 .alias = "",
125 },
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200126};
127
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200128#define __PERF_EVENT_FIELD(config, name) \
129 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
Ingo Molnar52425192009-05-26 09:17:18 +0200130
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200131#define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200132#define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200133#define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200134#define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
Ingo Molnar52425192009-05-26 09:17:18 +0200135
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300136#define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400137 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300138 if (sys_dirent.d_type == DT_DIR && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400139 (strcmp(sys_dirent.d_name, ".")) && \
140 (strcmp(sys_dirent.d_name, "..")))
141
Peter Zijlstraae07b632009-08-06 16:48:54 +0200142static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
143{
144 char evt_path[MAXPATHLEN];
145 int fd;
146
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200147 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
Peter Zijlstraae07b632009-08-06 16:48:54 +0200148 sys_dir->d_name, evt_dir->d_name);
149 fd = open(evt_path, O_RDONLY);
150 if (fd < 0)
151 return -EINVAL;
152 close(fd);
153
154 return 0;
155}
156
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300157#define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400158 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300159 if (evt_dirent.d_type == DT_DIR && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400160 (strcmp(evt_dirent.d_name, ".")) && \
Peter Zijlstraae07b632009-08-06 16:48:54 +0200161 (strcmp(evt_dirent.d_name, "..")) && \
162 (!tp_event_has_id(&sys_dirent, &evt_dirent)))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400163
Li Zefan270bbbe2009-09-17 16:34:51 +0800164#define MAX_EVENT_LENGTH 512
Jason Baronf6bdafe2009-07-21 12:20:22 -0400165
Jason Baronf6bdafe2009-07-21 12:20:22 -0400166
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200167struct tracepoint_path *tracepoint_id_to_path(u64 config)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400168{
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200169 struct tracepoint_path *path = NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400170 DIR *sys_dir, *evt_dir;
171 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Stephane Eranian8aa8a7c2012-03-13 16:51:02 +0100172 char id_buf[24];
Eric Dumazet725b1362009-09-24 15:39:09 +0200173 int fd;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400174 u64 id;
175 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +0200176 char dir_path[MAXPATHLEN];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400177
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200178 if (debugfs_valid_mountpoint(tracing_events_path))
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200179 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400180
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200181 sys_dir = opendir(tracing_events_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400182 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200183 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400184
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300185 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200186
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200187 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Eric Dumazet725b1362009-09-24 15:39:09 +0200188 sys_dirent.d_name);
189 evt_dir = opendir(dir_path);
190 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300191 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200192
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300193 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200194
195 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
Jason Baronf6bdafe2009-07-21 12:20:22 -0400196 evt_dirent.d_name);
Eric Dumazet725b1362009-09-24 15:39:09 +0200197 fd = open(evt_path, O_RDONLY);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400198 if (fd < 0)
199 continue;
200 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
201 close(fd);
202 continue;
203 }
204 close(fd);
205 id = atoll(id_buf);
206 if (id == config) {
207 closedir(evt_dir);
208 closedir(sys_dir);
Julia Lawall59b4cae2009-12-06 10:16:30 +0100209 path = zalloc(sizeof(*path));
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200210 path->system = malloc(MAX_EVENT_LENGTH);
211 if (!path->system) {
212 free(path);
213 return NULL;
214 }
215 path->name = malloc(MAX_EVENT_LENGTH);
216 if (!path->name) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300217 zfree(&path->system);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200218 free(path);
219 return NULL;
220 }
221 strncpy(path->system, sys_dirent.d_name,
222 MAX_EVENT_LENGTH);
223 strncpy(path->name, evt_dirent.d_name,
224 MAX_EVENT_LENGTH);
225 return path;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400226 }
227 }
228 closedir(evt_dir);
229 }
230
Jason Baronf6bdafe2009-07-21 12:20:22 -0400231 closedir(sys_dir);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200232 return NULL;
233}
234
Namhyung Kime7c93f02013-06-26 16:14:05 +0900235struct tracepoint_path *tracepoint_name_to_path(const char *name)
236{
237 struct tracepoint_path *path = zalloc(sizeof(*path));
238 char *str = strchr(name, ':');
239
240 if (path == NULL || str == NULL) {
241 free(path);
242 return NULL;
243 }
244
245 path->system = strndup(name, str - name);
246 path->name = strdup(str+1);
247
248 if (path->system == NULL || path->name == NULL) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300249 zfree(&path->system);
250 zfree(&path->name);
Namhyung Kime7c93f02013-06-26 16:14:05 +0900251 free(path);
252 path = NULL;
253 }
254
255 return path;
256}
257
David Ahern1424dc92011-03-09 22:23:28 -0700258const char *event_type(int type)
259{
260 switch (type) {
261 case PERF_TYPE_HARDWARE:
262 return "hardware";
263
264 case PERF_TYPE_SOFTWARE:
265 return "software";
266
267 case PERF_TYPE_TRACEPOINT:
268 return "tracepoint";
269
270 case PERF_TYPE_HW_CACHE:
271 return "hardware-cache";
272
273 default:
274 break;
275 }
276
277 return "unknown";
278}
279
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800280
281
Stephane Eranian410136f2013-11-12 17:58:49 +0100282static struct perf_evsel *
283__add_event(struct list_head *list, int *idx,
284 struct perf_event_attr *attr,
285 char *name, struct cpu_map *cpus)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100286{
287 struct perf_evsel *evsel;
288
289 event_attr_init(attr);
290
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300291 evsel = perf_evsel__new_idx(attr, (*idx)++);
David Ahernc5cd8ac2013-07-02 13:27:25 -0600292 if (!evsel)
Stephane Eranian410136f2013-11-12 17:58:49 +0100293 return NULL;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100294
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800295 evsel->cpus = cpus;
Arnaldo Carvalho de Melo9db17632012-06-12 13:45:00 -0300296 if (name)
297 evsel->name = strdup(name);
Jiri Olsab847cbd2012-05-21 09:12:51 +0200298 list_add_tail(&evsel->node, list);
Stephane Eranian410136f2013-11-12 17:58:49 +0100299 return evsel;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100300}
301
David Ahernc5cd8ac2013-07-02 13:27:25 -0600302static int add_event(struct list_head *list, int *idx,
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800303 struct perf_event_attr *attr, char *name)
304{
Stephane Eranian410136f2013-11-12 17:58:49 +0100305 return __add_event(list, idx, attr, name, NULL) ? 0 : -ENOMEM;
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800306}
307
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300308static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
Ingo Molnar8326f442009-06-05 20:22:46 +0200309{
310 int i, j;
Paul Mackerras61c45982009-07-01 13:04:34 +1000311 int n, longest = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200312
313 for (i = 0; i < size; i++) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300314 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
Paul Mackerras61c45982009-07-01 13:04:34 +1000315 n = strlen(names[i][j]);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100316 if (n > longest && !strncasecmp(str, names[i][j], n))
Paul Mackerras61c45982009-07-01 13:04:34 +1000317 longest = n;
318 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100319 if (longest > 0)
Paul Mackerras61c45982009-07-01 13:04:34 +1000320 return i;
Ingo Molnar8326f442009-06-05 20:22:46 +0200321 }
322
Ingo Molnar89536452009-06-06 21:04:17 +0200323 return -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200324}
325
David Ahernc5cd8ac2013-07-02 13:27:25 -0600326int parse_events_add_cache(struct list_head *list, int *idx,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100327 char *type, char *op_result1, char *op_result2)
Ingo Molnar8326f442009-06-05 20:22:46 +0200328{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100329 struct perf_event_attr attr;
330 char name[MAX_NAME_LEN];
Paul Mackerras61c45982009-07-01 13:04:34 +1000331 int cache_type = -1, cache_op = -1, cache_result = -1;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100332 char *op_result[2] = { op_result1, op_result2 };
333 int i, n;
Ingo Molnar8326f442009-06-05 20:22:46 +0200334
Ingo Molnar8326f442009-06-05 20:22:46 +0200335 /*
336 * No fallback - if we cannot get a clear cache type
337 * then bail out:
338 */
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300339 cache_type = parse_aliases(type, perf_evsel__hw_cache,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100340 PERF_COUNT_HW_CACHE_MAX);
Ingo Molnar8326f442009-06-05 20:22:46 +0200341 if (cache_type == -1)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100342 return -EINVAL;
Ingo Molnar8326f442009-06-05 20:22:46 +0200343
Jiri Olsa89812fc2012-03-15 20:09:15 +0100344 n = snprintf(name, MAX_NAME_LEN, "%s", type);
345
346 for (i = 0; (i < 2) && (op_result[i]); i++) {
347 char *str = op_result[i];
348
Jiri Olsa275ef382012-09-05 19:51:33 +0200349 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
Paul Mackerras61c45982009-07-01 13:04:34 +1000350
351 if (cache_op == -1) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300352 cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100353 PERF_COUNT_HW_CACHE_OP_MAX);
Paul Mackerras61c45982009-07-01 13:04:34 +1000354 if (cache_op >= 0) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300355 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
Jiri Olsa89812fc2012-03-15 20:09:15 +0100356 return -EINVAL;
Paul Mackerras61c45982009-07-01 13:04:34 +1000357 continue;
358 }
359 }
360
361 if (cache_result == -1) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300362 cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
363 PERF_COUNT_HW_CACHE_RESULT_MAX);
Paul Mackerras61c45982009-07-01 13:04:34 +1000364 if (cache_result >= 0)
365 continue;
366 }
Paul Mackerras61c45982009-07-01 13:04:34 +1000367 }
368
Ingo Molnar8326f442009-06-05 20:22:46 +0200369 /*
370 * Fall back to reads:
371 */
Ingo Molnar89536452009-06-06 21:04:17 +0200372 if (cache_op == -1)
373 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
Ingo Molnar8326f442009-06-05 20:22:46 +0200374
Ingo Molnar8326f442009-06-05 20:22:46 +0200375 /*
376 * Fall back to accesses:
377 */
378 if (cache_result == -1)
379 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
380
Jiri Olsa89812fc2012-03-15 20:09:15 +0100381 memset(&attr, 0, sizeof(attr));
382 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
383 attr.type = PERF_TYPE_HW_CACHE;
384 return add_event(list, idx, &attr, name);
Ingo Molnar8326f442009-06-05 20:22:46 +0200385}
386
David Ahernc5cd8ac2013-07-02 13:27:25 -0600387static int add_tracepoint(struct list_head *list, int *idx,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100388 char *sys_name, char *evt_name)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200389{
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300390 struct perf_evsel *evsel;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200391
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300392 evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
David Ahernc5cd8ac2013-07-02 13:27:25 -0600393 if (!evsel)
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300394 return -ENOMEM;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200395
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300396 list_add_tail(&evsel->node, list);
David Ahernc5cd8ac2013-07-02 13:27:25 -0600397
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300398 return 0;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200399}
400
David Ahernc5cd8ac2013-07-02 13:27:25 -0600401static int add_tracepoint_multi_event(struct list_head *list, int *idx,
Jiri Olsaf35488f2012-12-17 14:08:37 +0100402 char *sys_name, char *evt_name)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200403{
404 char evt_path[MAXPATHLEN];
405 struct dirent *evt_ent;
406 DIR *evt_dir;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100407 int ret = 0;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200408
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200409 snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200410 evt_dir = opendir(evt_path);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200411 if (!evt_dir) {
412 perror("Can't open event dir");
Jiri Olsa89812fc2012-03-15 20:09:15 +0100413 return -1;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200414 }
415
Jiri Olsa89812fc2012-03-15 20:09:15 +0100416 while (!ret && (evt_ent = readdir(evt_dir))) {
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200417 if (!strcmp(evt_ent->d_name, ".")
418 || !strcmp(evt_ent->d_name, "..")
419 || !strcmp(evt_ent->d_name, "enable")
420 || !strcmp(evt_ent->d_name, "filter"))
421 continue;
422
Jiri Olsa89812fc2012-03-15 20:09:15 +0100423 if (!strglobmatch(evt_ent->d_name, evt_name))
Masami Hiramatsufb1d2ed2010-01-05 17:47:17 -0500424 continue;
425
Jiri Olsa89812fc2012-03-15 20:09:15 +0100426 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200427 }
428
Jiri Olsa0bd3f082012-12-17 14:08:36 +0100429 closedir(evt_dir);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100430 return ret;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200431}
432
David Ahernc5cd8ac2013-07-02 13:27:25 -0600433static int add_tracepoint_event(struct list_head *list, int *idx,
Jiri Olsaf35488f2012-12-17 14:08:37 +0100434 char *sys_name, char *evt_name)
435{
436 return strpbrk(evt_name, "*?") ?
437 add_tracepoint_multi_event(list, idx, sys_name, evt_name) :
438 add_tracepoint(list, idx, sys_name, evt_name);
439}
440
David Ahernc5cd8ac2013-07-02 13:27:25 -0600441static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
Jiri Olsaf35488f2012-12-17 14:08:37 +0100442 char *sys_name, char *evt_name)
443{
444 struct dirent *events_ent;
445 DIR *events_dir;
446 int ret = 0;
447
448 events_dir = opendir(tracing_events_path);
449 if (!events_dir) {
450 perror("Can't open event dir");
451 return -1;
452 }
453
454 while (!ret && (events_ent = readdir(events_dir))) {
455 if (!strcmp(events_ent->d_name, ".")
456 || !strcmp(events_ent->d_name, "..")
457 || !strcmp(events_ent->d_name, "enable")
458 || !strcmp(events_ent->d_name, "header_event")
459 || !strcmp(events_ent->d_name, "header_page"))
460 continue;
461
462 if (!strglobmatch(events_ent->d_name, sys_name))
463 continue;
464
465 ret = add_tracepoint_event(list, idx, events_ent->d_name,
466 evt_name);
467 }
468
469 closedir(events_dir);
470 return ret;
471}
472
David Ahernc5cd8ac2013-07-02 13:27:25 -0600473int parse_events_add_tracepoint(struct list_head *list, int *idx,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100474 char *sys, char *event)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400475{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100476 int ret;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400477
Jiri Olsa89812fc2012-03-15 20:09:15 +0100478 ret = debugfs_valid_mountpoint(tracing_events_path);
479 if (ret)
480 return ret;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400481
Jiri Olsaf35488f2012-12-17 14:08:37 +0100482 if (strpbrk(sys, "*?"))
483 return add_tracepoint_multi_sys(list, idx, sys, event);
484 else
485 return add_tracepoint_event(list, idx, sys, event);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400486}
487
Jiri Olsa89812fc2012-03-15 20:09:15 +0100488static int
489parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100490{
491 int i;
492
493 for (i = 0; i < 3; i++) {
Jiri Olsa89812fc2012-03-15 20:09:15 +0100494 if (!type || !type[i])
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100495 break;
496
Jiri Olsa75827322012-06-29 09:22:54 +0200497#define CHECK_SET_TYPE(bit) \
498do { \
499 if (attr->bp_type & bit) \
500 return -EINVAL; \
501 else \
502 attr->bp_type |= bit; \
503} while (0)
504
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100505 switch (type[i]) {
506 case 'r':
Jiri Olsa75827322012-06-29 09:22:54 +0200507 CHECK_SET_TYPE(HW_BREAKPOINT_R);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100508 break;
509 case 'w':
Jiri Olsa75827322012-06-29 09:22:54 +0200510 CHECK_SET_TYPE(HW_BREAKPOINT_W);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100511 break;
512 case 'x':
Jiri Olsa75827322012-06-29 09:22:54 +0200513 CHECK_SET_TYPE(HW_BREAKPOINT_X);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100514 break;
515 default:
Jiri Olsa89812fc2012-03-15 20:09:15 +0100516 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100517 }
518 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100519
Jiri Olsa75827322012-06-29 09:22:54 +0200520#undef CHECK_SET_TYPE
521
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100522 if (!attr->bp_type) /* Default */
523 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
524
Jiri Olsa89812fc2012-03-15 20:09:15 +0100525 return 0;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100526}
527
David Ahernc5cd8ac2013-07-02 13:27:25 -0600528int parse_events_add_breakpoint(struct list_head *list, int *idx,
Jacob Shin3741eb92014-05-29 17:26:51 +0200529 void *ptr, char *type, u64 len)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100530{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100531 struct perf_event_attr attr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100532
Jiri Olsa89812fc2012-03-15 20:09:15 +0100533 memset(&attr, 0, sizeof(attr));
Jiri Olsa9fafd982012-03-20 19:15:39 +0100534 attr.bp_addr = (unsigned long) ptr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100535
Jiri Olsa89812fc2012-03-15 20:09:15 +0100536 if (parse_breakpoint_type(type, &attr))
537 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100538
Jacob Shin3741eb92014-05-29 17:26:51 +0200539 /* Provide some defaults if len is not specified */
540 if (!len) {
541 if (attr.bp_type == HW_BREAKPOINT_X)
542 len = sizeof(long);
543 else
544 len = HW_BREAKPOINT_LEN_4;
545 }
546
547 attr.bp_len = len;
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200548
Jiri Olsa89812fc2012-03-15 20:09:15 +0100549 attr.type = PERF_TYPE_BREAKPOINT;
Jovi Zhang4a841d62012-07-15 03:03:10 +0800550 attr.sample_period = 1;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100551
Jiri Olsa287e74a2012-06-28 23:18:49 +0200552 return add_event(list, idx, &attr, NULL);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100553}
554
Jiri Olsa8f707d82012-03-15 20:09:16 +0100555static int config_term(struct perf_event_attr *attr,
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300556 struct parse_events_term *term)
Jiri Olsa8f707d82012-03-15 20:09:16 +0100557{
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200558#define CHECK_TYPE_VAL(type) \
559do { \
560 if (PARSE_EVENTS__TERM_TYPE_ ## type != term->type_val) \
561 return -EINVAL; \
562} while (0)
563
564 switch (term->type_term) {
Jiri Olsa8f707d82012-03-15 20:09:16 +0100565 case PARSE_EVENTS__TERM_TYPE_CONFIG:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200566 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100567 attr->config = term->val.num;
568 break;
569 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200570 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100571 attr->config1 = term->val.num;
572 break;
573 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200574 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100575 attr->config2 = term->val.num;
576 break;
577 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200578 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100579 attr->sample_period = term->val.num;
580 break;
581 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
582 /*
583 * TODO uncomment when the field is available
584 * attr->branch_sample_type = term->val.num;
585 */
586 break;
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200587 case PARSE_EVENTS__TERM_TYPE_NAME:
588 CHECK_TYPE_VAL(STR);
589 break;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100590 default:
591 return -EINVAL;
592 }
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200593
Jiri Olsa8f707d82012-03-15 20:09:16 +0100594 return 0;
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200595#undef CHECK_TYPE_VAL
Jiri Olsa8f707d82012-03-15 20:09:16 +0100596}
597
598static int config_attr(struct perf_event_attr *attr,
599 struct list_head *head, int fail)
600{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300601 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100602
603 list_for_each_entry(term, head, list)
604 if (config_term(attr, term) && fail)
605 return -EINVAL;
606
607 return 0;
608}
609
David Ahernc5cd8ac2013-07-02 13:27:25 -0600610int parse_events_add_numeric(struct list_head *list, int *idx,
Robert Richterb527bab2012-08-07 19:43:13 +0200611 u32 type, u64 config,
Jiri Olsa8f707d82012-03-15 20:09:16 +0100612 struct list_head *head_config)
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530613{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100614 struct perf_event_attr attr;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530615
Jiri Olsa89812fc2012-03-15 20:09:15 +0100616 memset(&attr, 0, sizeof(attr));
617 attr.type = type;
618 attr.config = config;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100619
620 if (head_config &&
621 config_attr(&attr, head_config, 1))
622 return -EINVAL;
623
Arnaldo Carvalho de Melo9db17632012-06-12 13:45:00 -0300624 return add_event(list, idx, &attr, NULL);
Paul Mackerras61c45982009-07-01 13:04:34 +1000625}
626
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300627static int parse_events__is_name_term(struct parse_events_term *term)
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200628{
629 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
630}
631
Arnaldo Carvalho de Melo9db17632012-06-12 13:45:00 -0300632static char *pmu_event_name(struct list_head *head_terms)
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200633{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300634 struct parse_events_term *term;
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200635
636 list_for_each_entry(term, head_terms, list)
637 if (parse_events__is_name_term(term))
638 return term->val.str;
639
Arnaldo Carvalho de Melo9db17632012-06-12 13:45:00 -0300640 return NULL;
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200641}
642
David Ahernc5cd8ac2013-07-02 13:27:25 -0600643int parse_events_add_pmu(struct list_head *list, int *idx,
Jiri Olsa5f537a22012-03-15 20:09:18 +0100644 char *name, struct list_head *head_config)
645{
646 struct perf_event_attr attr;
Matt Fleming46441bd2014-09-24 15:04:06 +0100647 struct perf_pmu_info info;
Jiri Olsa5f537a22012-03-15 20:09:18 +0100648 struct perf_pmu *pmu;
Stephane Eranian410136f2013-11-12 17:58:49 +0100649 struct perf_evsel *evsel;
Jiri Olsa5f537a22012-03-15 20:09:18 +0100650
651 pmu = perf_pmu__find(name);
652 if (!pmu)
653 return -EINVAL;
654
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300655 if (pmu->default_config) {
656 memcpy(&attr, pmu->default_config,
657 sizeof(struct perf_event_attr));
658 } else {
659 memset(&attr, 0, sizeof(attr));
660 }
Jiri Olsa5f537a22012-03-15 20:09:18 +0100661
Adrian Hunterad962272014-08-15 22:08:40 +0300662 if (!head_config) {
663 attr.type = pmu->type;
664 evsel = __add_event(list, idx, &attr, NULL, pmu->cpus);
665 return evsel ? 0 : -ENOMEM;
666 }
667
Matt Fleming46441bd2014-09-24 15:04:06 +0100668 if (perf_pmu__check_alias(pmu, head_config, &info))
Zheng Yana6146d52012-06-15 14:31:41 +0800669 return -EINVAL;
670
Jiri Olsa5f537a22012-03-15 20:09:18 +0100671 /*
672 * Configure hardcoded terms first, no need to check
673 * return value when called with fail == 0 ;)
674 */
675 config_attr(&attr, head_config, 0);
676
677 if (perf_pmu__config(pmu, &attr, head_config))
678 return -EINVAL;
679
Stephane Eranian410136f2013-11-12 17:58:49 +0100680 evsel = __add_event(list, idx, &attr, pmu_event_name(head_config),
681 pmu->cpus);
682 if (evsel) {
Matt Fleming46441bd2014-09-24 15:04:06 +0100683 evsel->unit = info.unit;
684 evsel->scale = info.scale;
Matt Fleming044330c2014-11-21 10:31:12 +0100685 evsel->per_pkg = info.per_pkg;
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100686 evsel->snapshot = info.snapshot;
Stephane Eranian410136f2013-11-12 17:58:49 +0100687 }
688
689 return evsel ? 0 : -ENOMEM;
Jiri Olsa5f537a22012-03-15 20:09:18 +0100690}
691
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200692int parse_events__modifier_group(struct list_head *list,
693 char *event_mod)
Jiri Olsa89efb022012-08-08 12:14:14 +0200694{
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200695 return parse_events__modifier_event(list, event_mod, true);
696}
697
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300698void parse_events__set_leader(char *name, struct list_head *list)
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200699{
700 struct perf_evsel *leader;
701
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300702 __perf_evlist__set_leader(list);
703 leader = list_entry(list->next, struct perf_evsel, node);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200704 leader->group_name = name ? strdup(name) : NULL;
Jiri Olsa89efb022012-08-08 12:14:14 +0200705}
706
David Ahernc5cd8ac2013-07-02 13:27:25 -0600707/* list_event is assumed to point to malloc'ed memory */
Jiri Olsa5d7be902012-03-20 19:15:40 +0100708void parse_events_update_lists(struct list_head *list_event,
709 struct list_head *list_all)
710{
711 /*
712 * Called for single event definition. Update the
Jiri Olsa89efb022012-08-08 12:14:14 +0200713 * 'all event' list, and reinit the 'single event'
Jiri Olsa5d7be902012-03-20 19:15:40 +0100714 * list, for next event definition.
715 */
716 list_splice_tail(list_event, list_all);
Jiri Olsab847cbd2012-05-21 09:12:51 +0200717 free(list_event);
Jiri Olsa5d7be902012-03-20 19:15:40 +0100718}
719
Jiri Olsaf5b11352012-08-08 12:21:54 +0200720struct event_modifier {
721 int eu;
722 int ek;
723 int eh;
724 int eH;
725 int eG;
726 int precise;
727 int exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +0200728 int sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +1000729 int pinned;
Jiri Olsaf5b11352012-08-08 12:21:54 +0200730};
Paul Mackerras61c45982009-07-01 13:04:34 +1000731
Jiri Olsaf5b11352012-08-08 12:21:54 +0200732static int get_event_modifier(struct event_modifier *mod, char *str,
733 struct perf_evsel *evsel)
734{
735 int eu = evsel ? evsel->attr.exclude_user : 0;
736 int ek = evsel ? evsel->attr.exclude_kernel : 0;
737 int eh = evsel ? evsel->attr.exclude_hv : 0;
738 int eH = evsel ? evsel->attr.exclude_host : 0;
739 int eG = evsel ? evsel->attr.exclude_guest : 0;
740 int precise = evsel ? evsel->attr.precise_ip : 0;
Jiri Olsa3c176312012-10-10 17:39:03 +0200741 int sample_read = 0;
Michael Ellermane9a7c412013-08-06 23:28:05 +1000742 int pinned = evsel ? evsel->attr.pinned : 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +0200743
744 int exclude = eu | ek | eh;
745 int exclude_GH = evsel ? evsel->exclude_GH : 0;
746
Jiri Olsaf5b11352012-08-08 12:21:54 +0200747 memset(mod, 0, sizeof(*mod));
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200748
Paul Mackerras61c45982009-07-01 13:04:34 +1000749 while (*str) {
Peter Zijlstraab608342010-04-08 23:03:20 +0200750 if (*str == 'u') {
751 if (!exclude)
752 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000753 eu = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200754 } else if (*str == 'k') {
755 if (!exclude)
756 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000757 ek = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200758 } else if (*str == 'h') {
759 if (!exclude)
760 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000761 eh = 0;
Joerg Roedel99320cc2012-01-04 17:54:19 +0100762 } else if (*str == 'G') {
763 if (!exclude_GH)
764 exclude_GH = eG = eH = 1;
765 eG = 0;
766 } else if (*str == 'H') {
767 if (!exclude_GH)
768 exclude_GH = eG = eH = 1;
769 eH = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200770 } else if (*str == 'p') {
771 precise++;
David Ahern13427982012-09-13 14:59:13 -0600772 /* use of precise requires exclude_guest */
773 if (!exclude_GH)
774 eG = 1;
Jiri Olsa3c176312012-10-10 17:39:03 +0200775 } else if (*str == 'S') {
776 sample_read = 1;
Michael Ellermane9a7c412013-08-06 23:28:05 +1000777 } else if (*str == 'D') {
778 pinned = 1;
Peter Zijlstraab608342010-04-08 23:03:20 +0200779 } else
Paul Mackerras61c45982009-07-01 13:04:34 +1000780 break;
Peter Zijlstraab608342010-04-08 23:03:20 +0200781
Paul Mackerras61c45982009-07-01 13:04:34 +1000782 ++str;
783 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200784
Jiri Olsa89812fc2012-03-15 20:09:15 +0100785 /*
786 * precise ip:
787 *
788 * 0 - SAMPLE_IP can have arbitrary skid
789 * 1 - SAMPLE_IP must have constant skid
790 * 2 - SAMPLE_IP requested to have 0 skid
791 * 3 - SAMPLE_IP must have 0 skid
792 *
793 * See also PERF_RECORD_MISC_EXACT_IP
794 */
795 if (precise > 3)
796 return -EINVAL;
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200797
Jiri Olsaf5b11352012-08-08 12:21:54 +0200798 mod->eu = eu;
799 mod->ek = ek;
800 mod->eh = eh;
801 mod->eH = eH;
802 mod->eG = eG;
803 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. */
Michael Ellermane9a7c412013-08-06 23:28:05 +1000820 if (strlen(str) > (sizeof("ukhGHpppSD") - 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;
856 evsel->exclude_GH = mod.exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +0200857 evsel->sample_read = mod.sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +1000858
859 if (perf_evsel__is_group_leader(evsel))
860 evsel->attr.pinned = mod.pinned;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100861 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200862
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530863 return 0;
864}
865
Robert Richterac2ba9f2012-08-16 21:10:21 +0200866int parse_events_name(struct list_head *list, char *name)
867{
868 struct perf_evsel *evsel;
869
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300870 __evlist__for_each(list, evsel) {
Robert Richterac2ba9f2012-08-16 21:10:21 +0200871 if (!evsel->name)
872 evsel->name = strdup(name);
873 }
874
875 return 0;
876}
877
Kan Liangdcb4e102014-10-07 11:08:50 -0400878static int
879comp_pmu(const void *p1, const void *p2)
880{
881 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
882 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
883
884 return strcmp(pmu1->symbol, pmu2->symbol);
885}
886
887static void perf_pmu__parse_cleanup(void)
888{
889 if (perf_pmu_events_list_num > 0) {
890 struct perf_pmu_event_symbol *p;
891 int i;
892
893 for (i = 0; i < perf_pmu_events_list_num; i++) {
894 p = perf_pmu_events_list + i;
895 free(p->symbol);
896 }
897 free(perf_pmu_events_list);
898 perf_pmu_events_list = NULL;
899 perf_pmu_events_list_num = 0;
900 }
901}
902
903#define SET_SYMBOL(str, stype) \
904do { \
905 p->symbol = str; \
906 if (!p->symbol) \
907 goto err; \
908 p->type = stype; \
909} while (0)
910
911/*
912 * Read the pmu events list from sysfs
913 * Save it into perf_pmu_events_list
914 */
915static void perf_pmu__parse_init(void)
916{
917
918 struct perf_pmu *pmu = NULL;
919 struct perf_pmu_alias *alias;
920 int len = 0;
921
922 pmu = perf_pmu__find("cpu");
923 if ((pmu == NULL) || list_empty(&pmu->aliases)) {
924 perf_pmu_events_list_num = -1;
925 return;
926 }
927 list_for_each_entry(alias, &pmu->aliases, list) {
928 if (strchr(alias->name, '-'))
929 len++;
930 len++;
931 }
932 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
933 if (!perf_pmu_events_list)
934 return;
935 perf_pmu_events_list_num = len;
936
937 len = 0;
938 list_for_each_entry(alias, &pmu->aliases, list) {
939 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
940 char *tmp = strchr(alias->name, '-');
941
942 if (tmp != NULL) {
943 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
944 PMU_EVENT_SYMBOL_PREFIX);
945 p++;
946 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
947 len += 2;
948 } else {
949 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
950 len++;
951 }
952 }
953 qsort(perf_pmu_events_list, len,
954 sizeof(struct perf_pmu_event_symbol), comp_pmu);
955
956 return;
957err:
958 perf_pmu__parse_cleanup();
959}
960
961enum perf_pmu_event_symbol_type
962perf_pmu__parse_check(const char *name)
963{
964 struct perf_pmu_event_symbol p, *r;
965
966 /* scan kernel pmu events from sysfs if needed */
967 if (perf_pmu_events_list_num == 0)
968 perf_pmu__parse_init();
969 /*
970 * name "cpu" could be prefix of cpu-cycles or cpu// events.
971 * cpu-cycles has been handled by hardcode.
972 * So it must be cpu// events, not kernel pmu event.
973 */
974 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
975 return PMU_EVENT_SYMBOL_ERR;
976
977 p.symbol = strdup(name);
978 r = bsearch(&p, perf_pmu_events_list,
979 (size_t) perf_pmu_events_list_num,
980 sizeof(struct perf_pmu_event_symbol), comp_pmu);
981 free(p.symbol);
982 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
983}
984
Jiri Olsa90e2b222012-06-15 14:31:40 +0800985static int parse_events__scanner(const char *str, void *data, int start_token)
Zheng Yanac20de62012-06-15 14:31:39 +0800986{
987 YY_BUFFER_STATE buffer;
988 void *scanner;
989 int ret;
990
Jiri Olsa90e2b222012-06-15 14:31:40 +0800991 ret = parse_events_lex_init_extra(start_token, &scanner);
Zheng Yanac20de62012-06-15 14:31:39 +0800992 if (ret)
993 return ret;
994
995 buffer = parse_events__scan_string(str, scanner);
996
997#ifdef PARSER_DEBUG
998 parse_events_debug = 1;
999#endif
1000 ret = parse_events_parse(data, scanner);
1001
1002 parse_events__flush_buffer(buffer, scanner);
1003 parse_events__delete_buffer(buffer, scanner);
1004 parse_events_lex_destroy(scanner);
1005 return ret;
1006}
1007
Jiri Olsa90e2b222012-06-15 14:31:40 +08001008/*
1009 * parse event config string, return a list of event terms.
1010 */
1011int parse_events_terms(struct list_head *terms, const char *str)
1012{
Arnaldo Carvalho de Melo23b63392013-01-18 16:56:57 -03001013 struct parse_events_terms data = {
Jiri Olsa90e2b222012-06-15 14:31:40 +08001014 .terms = NULL,
1015 };
1016 int ret;
1017
1018 ret = parse_events__scanner(str, &data, PE_START_TERMS);
1019 if (!ret) {
1020 list_splice(data.terms, terms);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001021 zfree(&data.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001022 return 0;
1023 }
1024
Adrian Hunterb2c34fd2013-07-04 16:20:23 +03001025 if (data.terms)
1026 parse_events__free_terms(data.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001027 return ret;
1028}
1029
Jiri Olsad8f7bbc2013-01-15 14:39:51 +01001030int parse_events(struct perf_evlist *evlist, const char *str)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001031{
Arnaldo Carvalho de Melo23b63392013-01-18 16:56:57 -03001032 struct parse_events_evlist data = {
Jiri Olsa46010ab2012-06-15 14:31:38 +08001033 .list = LIST_HEAD_INIT(data.list),
1034 .idx = evlist->nr_entries,
1035 };
Jiri Olsa46010ab2012-06-15 14:31:38 +08001036 int ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001037
Jiri Olsa90e2b222012-06-15 14:31:40 +08001038 ret = parse_events__scanner(str, &data, PE_START_EVENTS);
Kan Liangdcb4e102014-10-07 11:08:50 -04001039 perf_pmu__parse_cleanup();
Jiri Olsa89812fc2012-03-15 20:09:15 +01001040 if (!ret) {
Jiri Olsa46010ab2012-06-15 14:31:38 +08001041 int entries = data.idx - evlist->nr_entries;
1042 perf_evlist__splice_list_tail(evlist, &data.list, entries);
Namhyung Kim97f63e42013-01-22 18:09:29 +09001043 evlist->nr_groups += data.nr_groups;
Jiri Olsa89812fc2012-03-15 20:09:15 +01001044 return 0;
1045 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02001046
Jiri Olsa5d7be902012-03-20 19:15:40 +01001047 /*
1048 * There are 2 users - builtin-record and builtin-test objects.
1049 * Both call perf_evlist__delete in case of error, so we dont
1050 * need to bother.
1051 */
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02001052 return ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001053}
1054
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001055int parse_events_option(const struct option *opt, const char *str,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001056 int unset __maybe_unused)
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001057{
1058 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
Jiri Olsad8f7bbc2013-01-15 14:39:51 +01001059 int ret = parse_events(evlist, str);
Andi Kleen9175ce12012-10-26 13:30:06 -07001060
1061 if (ret) {
1062 fprintf(stderr, "invalid or unsupported event: '%s'\n", str);
1063 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
1064 }
1065 return ret;
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001066}
1067
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001068int parse_filter(const struct option *opt, const char *str,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001069 int unset __maybe_unused)
Li Zefanc171b552009-10-15 11:22:07 +08001070{
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001071 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001072 struct perf_evsel *last = NULL;
Li Zefanc171b552009-10-15 11:22:07 +08001073
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001074 if (evlist->nr_entries > 0)
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001075 last = perf_evlist__last(evlist);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001076
1077 if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
Li Zefanc171b552009-10-15 11:22:07 +08001078 fprintf(stderr,
Arnaldo Carvalho de Melo281f92f2014-10-01 15:05:32 -03001079 "--filter option should follow a -e tracepoint option\n");
Li Zefanc171b552009-10-15 11:22:07 +08001080 return -1;
1081 }
1082
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001083 last->filter = strdup(str);
1084 if (last->filter == NULL) {
Li Zefanc171b552009-10-15 11:22:07 +08001085 fprintf(stderr, "not enough memory to hold filter string\n");
1086 return -1;
1087 }
Li Zefanc171b552009-10-15 11:22:07 +08001088
1089 return 0;
1090}
1091
Thomas Gleixner86847b62009-06-06 12:24:17 +02001092static const char * const event_type_descriptors[] = {
Thomas Gleixner86847b62009-06-06 12:24:17 +02001093 "Hardware event",
1094 "Software event",
1095 "Tracepoint event",
1096 "Hardware cache event",
Liming Wang41bdcb22009-12-29 16:37:07 +08001097 "Raw hardware event descriptor",
1098 "Hardware breakpoint",
Thomas Gleixner86847b62009-06-06 12:24:17 +02001099};
1100
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001101/*
Jason Baronf6bdafe2009-07-21 12:20:22 -04001102 * Print the events from <debugfs_mount_point>/tracing/events
1103 */
1104
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001105void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
1106 bool name_only)
Jason Baronf6bdafe2009-07-21 12:20:22 -04001107{
1108 DIR *sys_dir, *evt_dir;
1109 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001110 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +02001111 char dir_path[MAXPATHLEN];
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00001112 char sbuf[STRERR_BUFSIZE];
Jason Baronf6bdafe2009-07-21 12:20:22 -04001113
Pekka Enbergf11cfc62013-10-15 23:07:27 +03001114 if (debugfs_valid_mountpoint(tracing_events_path)) {
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00001115 printf(" [ Tracepoints not available: %s ]\n",
1116 strerror_r(errno, sbuf, sizeof(sbuf)));
Jason Baronf6bdafe2009-07-21 12:20:22 -04001117 return;
Pekka Enbergf11cfc62013-10-15 23:07:27 +03001118 }
Jason Baronf6bdafe2009-07-21 12:20:22 -04001119
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001120 sys_dir = opendir(tracing_events_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -04001121 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +02001122 return;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001123
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03001124 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001125 if (subsys_glob != NULL &&
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001126 !strglobmatch(sys_dirent.d_name, subsys_glob))
1127 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02001128
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001129 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Eric Dumazet725b1362009-09-24 15:39:09 +02001130 sys_dirent.d_name);
1131 evt_dir = opendir(dir_path);
1132 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03001133 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02001134
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03001135 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001136 if (event_glob != NULL &&
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001137 !strglobmatch(evt_dirent.d_name, event_glob))
1138 continue;
1139
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001140 if (name_only) {
1141 printf("%s:%s ", sys_dirent.d_name, evt_dirent.d_name);
1142 continue;
1143 }
1144
Jason Baronf6bdafe2009-07-21 12:20:22 -04001145 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1146 sys_dirent.d_name, evt_dirent.d_name);
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001147 printf(" %-50s [%s]\n", evt_path,
Liming Wang41bdcb22009-12-29 16:37:07 +08001148 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
Jason Baronf6bdafe2009-07-21 12:20:22 -04001149 }
1150 closedir(evt_dir);
1151 }
Jason Baronf6bdafe2009-07-21 12:20:22 -04001152 closedir(sys_dir);
1153}
1154
1155/*
Thomas Renninger20c457b2011-01-03 17:50:45 +01001156 * Check whether event is in <debugfs_mount_point>/tracing/events
1157 */
1158
1159int is_valid_tracepoint(const char *event_string)
1160{
1161 DIR *sys_dir, *evt_dir;
1162 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
1163 char evt_path[MAXPATHLEN];
1164 char dir_path[MAXPATHLEN];
1165
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001166 if (debugfs_valid_mountpoint(tracing_events_path))
Thomas Renninger20c457b2011-01-03 17:50:45 +01001167 return 0;
1168
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001169 sys_dir = opendir(tracing_events_path);
Thomas Renninger20c457b2011-01-03 17:50:45 +01001170 if (!sys_dir)
1171 return 0;
1172
1173 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1174
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001175 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Thomas Renninger20c457b2011-01-03 17:50:45 +01001176 sys_dirent.d_name);
1177 evt_dir = opendir(dir_path);
1178 if (!evt_dir)
1179 continue;
1180
1181 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1182 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1183 sys_dirent.d_name, evt_dirent.d_name);
1184 if (!strcmp(evt_path, event_string)) {
1185 closedir(evt_dir);
1186 closedir(sys_dir);
1187 return 1;
1188 }
1189 }
1190 closedir(evt_dir);
1191 }
1192 closedir(sys_dir);
1193 return 0;
1194}
1195
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001196static bool is_event_supported(u8 type, unsigned config)
1197{
1198 bool ret = true;
Vince Weaver88fee522013-12-30 15:39:45 -05001199 int open_return;
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001200 struct perf_evsel *evsel;
1201 struct perf_event_attr attr = {
1202 .type = type,
1203 .config = config,
1204 .disabled = 1,
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001205 };
1206 struct {
1207 struct thread_map map;
1208 int threads[1];
1209 } tmap = {
1210 .map.nr = 1,
1211 .threads = { 0 },
1212 };
1213
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03001214 evsel = perf_evsel__new(&attr);
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001215 if (evsel) {
Vince Weaver88fee522013-12-30 15:39:45 -05001216 open_return = perf_evsel__open(evsel, NULL, &tmap.map);
1217 ret = open_return >= 0;
1218
1219 if (open_return == -EACCES) {
1220 /*
1221 * This happens if the paranoid value
1222 * /proc/sys/kernel/perf_event_paranoid is set to 2
1223 * Re-run with exclude_kernel set; we don't do that
1224 * by default as some ARM machines do not support it.
1225 *
1226 */
1227 evsel->attr.exclude_kernel = 1;
1228 ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0;
1229 }
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001230 perf_evsel__delete(evsel);
1231 }
1232
1233 return ret;
1234}
1235
Jiri Olsa1dc12762012-07-04 00:00:44 +02001236static void __print_events_type(u8 type, struct event_symbol *syms,
1237 unsigned max)
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001238{
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001239 char name[64];
Jiri Olsa1dc12762012-07-04 00:00:44 +02001240 unsigned i;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001241
Jiri Olsa1dc12762012-07-04 00:00:44 +02001242 for (i = 0; i < max ; i++, syms++) {
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001243 if (!is_event_supported(type, i))
1244 continue;
1245
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001246 if (strlen(syms->alias))
1247 snprintf(name, sizeof(name), "%s OR %s",
1248 syms->symbol, syms->alias);
1249 else
1250 snprintf(name, sizeof(name), "%s", syms->symbol);
1251
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001252 printf(" %-50s [%s]\n", name, event_type_descriptors[type]);
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001253 }
1254}
1255
Jiri Olsa1dc12762012-07-04 00:00:44 +02001256void print_events_type(u8 type)
1257{
1258 if (type == PERF_TYPE_SOFTWARE)
1259 __print_events_type(type, event_symbols_sw, PERF_COUNT_SW_MAX);
1260 else
1261 __print_events_type(type, event_symbols_hw, PERF_COUNT_HW_MAX);
1262}
1263
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001264int print_hwcache_events(const char *event_glob, bool name_only)
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001265{
1266 unsigned int type, op, i, printed = 0;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03001267 char name[64];
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001268
1269 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1270 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1271 /* skip invalid cache type */
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03001272 if (!perf_evsel__is_cache_op_valid(type, op))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001273 continue;
1274
1275 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03001276 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
1277 name, sizeof(name));
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001278 if (event_glob != NULL && !strglobmatch(name, event_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001279 continue;
1280
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001281 if (!is_event_supported(PERF_TYPE_HW_CACHE,
1282 type | (op << 8) | (i << 16)))
1283 continue;
1284
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001285 if (name_only)
1286 printf("%s ", name);
1287 else
1288 printf(" %-50s [%s]\n", name,
1289 event_type_descriptors[PERF_TYPE_HW_CACHE]);
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001290 ++printed;
1291 }
1292 }
1293 }
1294
Andi Kleendc098b32013-04-20 11:02:29 -07001295 if (printed)
1296 printf("\n");
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001297 return printed;
1298}
1299
Jiri Olsa1dc12762012-07-04 00:00:44 +02001300static void print_symbol_events(const char *event_glob, unsigned type,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001301 struct event_symbol *syms, unsigned max,
1302 bool name_only)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001303{
Jiri Olsa1dc12762012-07-04 00:00:44 +02001304 unsigned i, printed = 0;
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001305 char name[MAX_NAME_LEN];
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001306
Jiri Olsa1dc12762012-07-04 00:00:44 +02001307 for (i = 0; i < max; i++, syms++) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001308
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001309 if (event_glob != NULL &&
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001310 !(strglobmatch(syms->symbol, event_glob) ||
1311 (syms->alias && strglobmatch(syms->alias, event_glob))))
1312 continue;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001313
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001314 if (!is_event_supported(type, i))
1315 continue;
1316
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001317 if (name_only) {
1318 printf("%s ", syms->symbol);
1319 continue;
1320 }
1321
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301322 if (strlen(syms->alias))
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001323 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301324 else
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001325 strncpy(name, syms->symbol, MAX_NAME_LEN);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001326
Jiri Olsa1dc12762012-07-04 00:00:44 +02001327 printf(" %-50s [%s]\n", name, event_type_descriptors[type]);
1328
1329 printed++;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001330 }
1331
Jiri Olsa1dc12762012-07-04 00:00:44 +02001332 if (printed)
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001333 printf("\n");
Jiri Olsa1dc12762012-07-04 00:00:44 +02001334}
1335
1336/*
1337 * Print the help text for the event symbols:
1338 */
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001339void print_events(const char *event_glob, bool name_only)
Jiri Olsa1dc12762012-07-04 00:00:44 +02001340{
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001341 if (!name_only) {
1342 printf("\n");
1343 printf("List of pre-defined events (to be used in -e):\n");
1344 }
Jiri Olsa1dc12762012-07-04 00:00:44 +02001345
1346 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001347 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
Jiri Olsa1dc12762012-07-04 00:00:44 +02001348
1349 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001350 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
Jiri Olsa1dc12762012-07-04 00:00:44 +02001351
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001352 print_hwcache_events(event_glob, name_only);
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001353
Andi Kleendc098b32013-04-20 11:02:29 -07001354 print_pmu_events(event_glob, name_only);
1355
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001356 if (event_glob != NULL)
1357 return;
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +05301358
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001359 if (!name_only) {
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001360 printf(" %-50s [%s]\n",
1361 "rNNN",
1362 event_type_descriptors[PERF_TYPE_RAW]);
1363 printf(" %-50s [%s]\n",
1364 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
1365 event_type_descriptors[PERF_TYPE_RAW]);
Borislav Petkovaf3df2c2012-09-28 19:47:07 +02001366 printf(" (see 'man perf-list' on how to encode it)\n");
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001367 printf("\n");
Thomas Gleixner86847b62009-06-06 12:24:17 +02001368
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001369 printf(" %-50s [%s]\n",
Jacob Shin3741eb92014-05-29 17:26:51 +02001370 "mem:<addr>[/len][:access]",
Liming Wang41bdcb22009-12-29 16:37:07 +08001371 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001372 printf("\n");
1373 }
Frederic Weisbecker1b290d62009-11-23 15:42:35 +01001374
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001375 print_tracepoint_events(NULL, NULL, name_only);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001376}
Jiri Olsa8f707d82012-03-15 20:09:16 +01001377
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001378int parse_events__is_hardcoded_term(struct parse_events_term *term)
Jiri Olsa8f707d82012-03-15 20:09:16 +01001379{
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001380 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001381}
1382
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001383static int new_term(struct parse_events_term **_term, int type_val,
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001384 int type_term, char *config,
Robert Richterb527bab2012-08-07 19:43:13 +02001385 char *str, u64 num)
Jiri Olsa8f707d82012-03-15 20:09:16 +01001386{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001387 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001388
1389 term = zalloc(sizeof(*term));
1390 if (!term)
1391 return -ENOMEM;
1392
1393 INIT_LIST_HEAD(&term->list);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001394 term->type_val = type_val;
1395 term->type_term = type_term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001396 term->config = config;
1397
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001398 switch (type_val) {
Jiri Olsa8f707d82012-03-15 20:09:16 +01001399 case PARSE_EVENTS__TERM_TYPE_NUM:
1400 term->val.num = num;
1401 break;
1402 case PARSE_EVENTS__TERM_TYPE_STR:
1403 term->val.str = str;
1404 break;
1405 default:
Adrian Hunter4be8be62013-07-04 16:20:24 +03001406 free(term);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001407 return -EINVAL;
1408 }
1409
1410 *_term = term;
1411 return 0;
1412}
1413
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001414int parse_events_term__num(struct parse_events_term **term,
Robert Richterb527bab2012-08-07 19:43:13 +02001415 int type_term, char *config, u64 num)
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001416{
1417 return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
1418 config, NULL, num);
1419}
1420
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001421int parse_events_term__str(struct parse_events_term **term,
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001422 int type_term, char *config, char *str)
1423{
1424 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
1425 config, str, 0);
1426}
1427
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001428int parse_events_term__sym_hw(struct parse_events_term **term,
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02001429 char *config, unsigned idx)
1430{
1431 struct event_symbol *sym;
1432
1433 BUG_ON(idx >= PERF_COUNT_HW_MAX);
1434 sym = &event_symbols_hw[idx];
1435
1436 if (config)
1437 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1438 PARSE_EVENTS__TERM_TYPE_USER, config,
1439 (char *) sym->symbol, 0);
1440 else
1441 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1442 PARSE_EVENTS__TERM_TYPE_USER,
1443 (char *) "event", (char *) sym->symbol, 0);
1444}
1445
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001446int parse_events_term__clone(struct parse_events_term **new,
1447 struct parse_events_term *term)
Zheng Yana6146d52012-06-15 14:31:41 +08001448{
1449 return new_term(new, term->type_val, term->type_term, term->config,
1450 term->val.str, term->val.num);
1451}
1452
Jiri Olsa8f707d82012-03-15 20:09:16 +01001453void parse_events__free_terms(struct list_head *terms)
1454{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001455 struct parse_events_term *term, *h;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001456
1457 list_for_each_entry_safe(term, h, terms, list)
1458 free(term);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001459}