blob: 5a373483f0e4a2bd285461331e354d8dd9066527 [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 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 Yanac20de6f2012-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 Olsa1dc127602012-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 Olsa1dc127602012-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,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100529 void *ptr, char *type)
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
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200539 /*
540 * We should find a nice way to override the access length
541 * Provide some defaults for now
542 */
Jiri Olsa89812fc2012-03-15 20:09:15 +0100543 if (attr.bp_type == HW_BREAKPOINT_X)
544 attr.bp_len = sizeof(long);
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200545 else
Jiri Olsa89812fc2012-03-15 20:09:15 +0100546 attr.bp_len = HW_BREAKPOINT_LEN_4;
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200547
Jiri Olsa89812fc2012-03-15 20:09:15 +0100548 attr.type = PERF_TYPE_BREAKPOINT;
Jovi Zhang4a841d62012-07-15 03:03:10 +0800549 attr.sample_period = 1;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100550
Jiri Olsa287e74a2012-06-28 23:18:49 +0200551 return add_event(list, idx, &attr, NULL);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100552}
553
Jiri Olsa8f707d82012-03-15 20:09:16 +0100554static int config_term(struct perf_event_attr *attr,
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300555 struct parse_events_term *term)
Jiri Olsa8f707d82012-03-15 20:09:16 +0100556{
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200557#define CHECK_TYPE_VAL(type) \
558do { \
559 if (PARSE_EVENTS__TERM_TYPE_ ## type != term->type_val) \
560 return -EINVAL; \
561} while (0)
562
563 switch (term->type_term) {
Jiri Olsa8f707d82012-03-15 20:09:16 +0100564 case PARSE_EVENTS__TERM_TYPE_CONFIG:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200565 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100566 attr->config = term->val.num;
567 break;
568 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200569 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100570 attr->config1 = term->val.num;
571 break;
572 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200573 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100574 attr->config2 = term->val.num;
575 break;
576 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200577 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100578 attr->sample_period = term->val.num;
579 break;
580 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
581 /*
582 * TODO uncomment when the field is available
583 * attr->branch_sample_type = term->val.num;
584 */
585 break;
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200586 case PARSE_EVENTS__TERM_TYPE_NAME:
587 CHECK_TYPE_VAL(STR);
588 break;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100589 default:
590 return -EINVAL;
591 }
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200592
Jiri Olsa8f707d82012-03-15 20:09:16 +0100593 return 0;
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200594#undef CHECK_TYPE_VAL
Jiri Olsa8f707d82012-03-15 20:09:16 +0100595}
596
597static int config_attr(struct perf_event_attr *attr,
598 struct list_head *head, int fail)
599{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300600 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100601
602 list_for_each_entry(term, head, list)
603 if (config_term(attr, term) && fail)
604 return -EINVAL;
605
606 return 0;
607}
608
David Ahernc5cd8ac2013-07-02 13:27:25 -0600609int parse_events_add_numeric(struct list_head *list, int *idx,
Robert Richterb527bab2012-08-07 19:43:13 +0200610 u32 type, u64 config,
Jiri Olsa8f707d82012-03-15 20:09:16 +0100611 struct list_head *head_config)
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530612{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100613 struct perf_event_attr attr;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530614
Jiri Olsa89812fc2012-03-15 20:09:15 +0100615 memset(&attr, 0, sizeof(attr));
616 attr.type = type;
617 attr.config = config;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100618
619 if (head_config &&
620 config_attr(&attr, head_config, 1))
621 return -EINVAL;
622
Arnaldo Carvalho de Melo9db17632012-06-12 13:45:00 -0300623 return add_event(list, idx, &attr, NULL);
Paul Mackerras61c45982009-07-01 13:04:34 +1000624}
625
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300626static int parse_events__is_name_term(struct parse_events_term *term)
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200627{
628 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
629}
630
Arnaldo Carvalho de Melo9db17632012-06-12 13:45:00 -0300631static char *pmu_event_name(struct list_head *head_terms)
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200632{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300633 struct parse_events_term *term;
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200634
635 list_for_each_entry(term, head_terms, list)
636 if (parse_events__is_name_term(term))
637 return term->val.str;
638
Arnaldo Carvalho de Melo9db17632012-06-12 13:45:00 -0300639 return NULL;
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200640}
641
David Ahernc5cd8ac2013-07-02 13:27:25 -0600642int parse_events_add_pmu(struct list_head *list, int *idx,
Jiri Olsa5f537a22012-03-15 20:09:18 +0100643 char *name, struct list_head *head_config)
644{
645 struct perf_event_attr attr;
Matt Fleming46441bd2014-09-24 15:04:06 +0100646 struct perf_pmu_info info;
Jiri Olsa5f537a22012-03-15 20:09:18 +0100647 struct perf_pmu *pmu;
Stephane Eranian410136f2013-11-12 17:58:49 +0100648 struct perf_evsel *evsel;
Jiri Olsa5f537a22012-03-15 20:09:18 +0100649
650 pmu = perf_pmu__find(name);
651 if (!pmu)
652 return -EINVAL;
653
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300654 if (pmu->default_config) {
655 memcpy(&attr, pmu->default_config,
656 sizeof(struct perf_event_attr));
657 } else {
658 memset(&attr, 0, sizeof(attr));
659 }
Jiri Olsa5f537a22012-03-15 20:09:18 +0100660
Adrian Hunterad962272014-08-15 22:08:40 +0300661 if (!head_config) {
662 attr.type = pmu->type;
663 evsel = __add_event(list, idx, &attr, NULL, pmu->cpus);
664 return evsel ? 0 : -ENOMEM;
665 }
666
Matt Fleming46441bd2014-09-24 15:04:06 +0100667 if (perf_pmu__check_alias(pmu, head_config, &info))
Zheng Yana6146d52012-06-15 14:31:41 +0800668 return -EINVAL;
669
Jiri Olsa5f537a22012-03-15 20:09:18 +0100670 /*
671 * Configure hardcoded terms first, no need to check
672 * return value when called with fail == 0 ;)
673 */
674 config_attr(&attr, head_config, 0);
675
676 if (perf_pmu__config(pmu, &attr, head_config))
677 return -EINVAL;
678
Stephane Eranian410136f2013-11-12 17:58:49 +0100679 evsel = __add_event(list, idx, &attr, pmu_event_name(head_config),
680 pmu->cpus);
681 if (evsel) {
Matt Fleming46441bd2014-09-24 15:04:06 +0100682 evsel->unit = info.unit;
683 evsel->scale = info.scale;
Matt Fleming044330c2014-11-21 10:31:12 +0100684 evsel->per_pkg = info.per_pkg;
Stephane Eranian410136f2013-11-12 17:58:49 +0100685 }
686
687 return evsel ? 0 : -ENOMEM;
Jiri Olsa5f537a22012-03-15 20:09:18 +0100688}
689
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200690int parse_events__modifier_group(struct list_head *list,
691 char *event_mod)
Jiri Olsa89efb022012-08-08 12:14:14 +0200692{
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200693 return parse_events__modifier_event(list, event_mod, true);
694}
695
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300696void parse_events__set_leader(char *name, struct list_head *list)
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200697{
698 struct perf_evsel *leader;
699
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300700 __perf_evlist__set_leader(list);
701 leader = list_entry(list->next, struct perf_evsel, node);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200702 leader->group_name = name ? strdup(name) : NULL;
Jiri Olsa89efb022012-08-08 12:14:14 +0200703}
704
David Ahernc5cd8ac2013-07-02 13:27:25 -0600705/* list_event is assumed to point to malloc'ed memory */
Jiri Olsa5d7be902012-03-20 19:15:40 +0100706void parse_events_update_lists(struct list_head *list_event,
707 struct list_head *list_all)
708{
709 /*
710 * Called for single event definition. Update the
Jiri Olsa89efb022012-08-08 12:14:14 +0200711 * 'all event' list, and reinit the 'single event'
Jiri Olsa5d7be902012-03-20 19:15:40 +0100712 * list, for next event definition.
713 */
714 list_splice_tail(list_event, list_all);
Jiri Olsab847cbd2012-05-21 09:12:51 +0200715 free(list_event);
Jiri Olsa5d7be902012-03-20 19:15:40 +0100716}
717
Jiri Olsaf5b11352012-08-08 12:21:54 +0200718struct event_modifier {
719 int eu;
720 int ek;
721 int eh;
722 int eH;
723 int eG;
724 int precise;
725 int exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +0200726 int sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +1000727 int pinned;
Jiri Olsaf5b11352012-08-08 12:21:54 +0200728};
Paul Mackerras61c45982009-07-01 13:04:34 +1000729
Jiri Olsaf5b11352012-08-08 12:21:54 +0200730static int get_event_modifier(struct event_modifier *mod, char *str,
731 struct perf_evsel *evsel)
732{
733 int eu = evsel ? evsel->attr.exclude_user : 0;
734 int ek = evsel ? evsel->attr.exclude_kernel : 0;
735 int eh = evsel ? evsel->attr.exclude_hv : 0;
736 int eH = evsel ? evsel->attr.exclude_host : 0;
737 int eG = evsel ? evsel->attr.exclude_guest : 0;
738 int precise = evsel ? evsel->attr.precise_ip : 0;
Jiri Olsa3c176312012-10-10 17:39:03 +0200739 int sample_read = 0;
Michael Ellermane9a7c412013-08-06 23:28:05 +1000740 int pinned = evsel ? evsel->attr.pinned : 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +0200741
742 int exclude = eu | ek | eh;
743 int exclude_GH = evsel ? evsel->exclude_GH : 0;
744
Jiri Olsaf5b11352012-08-08 12:21:54 +0200745 memset(mod, 0, sizeof(*mod));
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200746
Paul Mackerras61c45982009-07-01 13:04:34 +1000747 while (*str) {
Peter Zijlstraab608342010-04-08 23:03:20 +0200748 if (*str == 'u') {
749 if (!exclude)
750 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000751 eu = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200752 } else if (*str == 'k') {
753 if (!exclude)
754 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000755 ek = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200756 } else if (*str == 'h') {
757 if (!exclude)
758 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000759 eh = 0;
Joerg Roedel99320cc2012-01-04 17:54:19 +0100760 } else if (*str == 'G') {
761 if (!exclude_GH)
762 exclude_GH = eG = eH = 1;
763 eG = 0;
764 } else if (*str == 'H') {
765 if (!exclude_GH)
766 exclude_GH = eG = eH = 1;
767 eH = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200768 } else if (*str == 'p') {
769 precise++;
David Ahern13427982012-09-13 14:59:13 -0600770 /* use of precise requires exclude_guest */
771 if (!exclude_GH)
772 eG = 1;
Jiri Olsa3c176312012-10-10 17:39:03 +0200773 } else if (*str == 'S') {
774 sample_read = 1;
Michael Ellermane9a7c412013-08-06 23:28:05 +1000775 } else if (*str == 'D') {
776 pinned = 1;
Peter Zijlstraab608342010-04-08 23:03:20 +0200777 } else
Paul Mackerras61c45982009-07-01 13:04:34 +1000778 break;
Peter Zijlstraab608342010-04-08 23:03:20 +0200779
Paul Mackerras61c45982009-07-01 13:04:34 +1000780 ++str;
781 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200782
Jiri Olsa89812fc2012-03-15 20:09:15 +0100783 /*
784 * precise ip:
785 *
786 * 0 - SAMPLE_IP can have arbitrary skid
787 * 1 - SAMPLE_IP must have constant skid
788 * 2 - SAMPLE_IP requested to have 0 skid
789 * 3 - SAMPLE_IP must have 0 skid
790 *
791 * See also PERF_RECORD_MISC_EXACT_IP
792 */
793 if (precise > 3)
794 return -EINVAL;
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200795
Jiri Olsaf5b11352012-08-08 12:21:54 +0200796 mod->eu = eu;
797 mod->ek = ek;
798 mod->eh = eh;
799 mod->eH = eH;
800 mod->eG = eG;
801 mod->precise = precise;
802 mod->exclude_GH = exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +0200803 mod->sample_read = sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +1000804 mod->pinned = pinned;
805
Jiri Olsaf5b11352012-08-08 12:21:54 +0200806 return 0;
807}
808
Jiri Olsa534123f2012-11-13 15:32:58 +0100809/*
810 * Basic modifier sanity check to validate it contains only one
811 * instance of any modifier (apart from 'p') present.
812 */
813static int check_modifier(char *str)
814{
815 char *p = str;
816
817 /* The sizeof includes 0 byte as well. */
Michael Ellermane9a7c412013-08-06 23:28:05 +1000818 if (strlen(str) > (sizeof("ukhGHpppSD") - 1))
Jiri Olsa534123f2012-11-13 15:32:58 +0100819 return -1;
820
821 while (*p) {
822 if (*p != 'p' && strchr(p + 1, *p))
823 return -1;
824 p++;
825 }
826
827 return 0;
828}
829
Jiri Olsaf5b11352012-08-08 12:21:54 +0200830int parse_events__modifier_event(struct list_head *list, char *str, bool add)
831{
832 struct perf_evsel *evsel;
833 struct event_modifier mod;
834
835 if (str == NULL)
836 return 0;
837
Jiri Olsa534123f2012-11-13 15:32:58 +0100838 if (check_modifier(str))
839 return -EINVAL;
840
Jiri Olsaf5b11352012-08-08 12:21:54 +0200841 if (!add && get_event_modifier(&mod, str, NULL))
842 return -EINVAL;
843
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300844 __evlist__for_each(list, evsel) {
Jiri Olsaf5b11352012-08-08 12:21:54 +0200845 if (add && get_event_modifier(&mod, str, evsel))
846 return -EINVAL;
847
848 evsel->attr.exclude_user = mod.eu;
849 evsel->attr.exclude_kernel = mod.ek;
850 evsel->attr.exclude_hv = mod.eh;
851 evsel->attr.precise_ip = mod.precise;
852 evsel->attr.exclude_host = mod.eH;
853 evsel->attr.exclude_guest = mod.eG;
854 evsel->exclude_GH = mod.exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +0200855 evsel->sample_read = mod.sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +1000856
857 if (perf_evsel__is_group_leader(evsel))
858 evsel->attr.pinned = mod.pinned;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100859 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200860
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530861 return 0;
862}
863
Robert Richterac2ba9f2012-08-16 21:10:21 +0200864int parse_events_name(struct list_head *list, char *name)
865{
866 struct perf_evsel *evsel;
867
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300868 __evlist__for_each(list, evsel) {
Robert Richterac2ba9f2012-08-16 21:10:21 +0200869 if (!evsel->name)
870 evsel->name = strdup(name);
871 }
872
873 return 0;
874}
875
Kan Liangdcb4e102014-10-07 11:08:50 -0400876static int
877comp_pmu(const void *p1, const void *p2)
878{
879 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
880 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
881
882 return strcmp(pmu1->symbol, pmu2->symbol);
883}
884
885static void perf_pmu__parse_cleanup(void)
886{
887 if (perf_pmu_events_list_num > 0) {
888 struct perf_pmu_event_symbol *p;
889 int i;
890
891 for (i = 0; i < perf_pmu_events_list_num; i++) {
892 p = perf_pmu_events_list + i;
893 free(p->symbol);
894 }
895 free(perf_pmu_events_list);
896 perf_pmu_events_list = NULL;
897 perf_pmu_events_list_num = 0;
898 }
899}
900
901#define SET_SYMBOL(str, stype) \
902do { \
903 p->symbol = str; \
904 if (!p->symbol) \
905 goto err; \
906 p->type = stype; \
907} while (0)
908
909/*
910 * Read the pmu events list from sysfs
911 * Save it into perf_pmu_events_list
912 */
913static void perf_pmu__parse_init(void)
914{
915
916 struct perf_pmu *pmu = NULL;
917 struct perf_pmu_alias *alias;
918 int len = 0;
919
920 pmu = perf_pmu__find("cpu");
921 if ((pmu == NULL) || list_empty(&pmu->aliases)) {
922 perf_pmu_events_list_num = -1;
923 return;
924 }
925 list_for_each_entry(alias, &pmu->aliases, list) {
926 if (strchr(alias->name, '-'))
927 len++;
928 len++;
929 }
930 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
931 if (!perf_pmu_events_list)
932 return;
933 perf_pmu_events_list_num = len;
934
935 len = 0;
936 list_for_each_entry(alias, &pmu->aliases, list) {
937 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
938 char *tmp = strchr(alias->name, '-');
939
940 if (tmp != NULL) {
941 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
942 PMU_EVENT_SYMBOL_PREFIX);
943 p++;
944 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
945 len += 2;
946 } else {
947 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
948 len++;
949 }
950 }
951 qsort(perf_pmu_events_list, len,
952 sizeof(struct perf_pmu_event_symbol), comp_pmu);
953
954 return;
955err:
956 perf_pmu__parse_cleanup();
957}
958
959enum perf_pmu_event_symbol_type
960perf_pmu__parse_check(const char *name)
961{
962 struct perf_pmu_event_symbol p, *r;
963
964 /* scan kernel pmu events from sysfs if needed */
965 if (perf_pmu_events_list_num == 0)
966 perf_pmu__parse_init();
967 /*
968 * name "cpu" could be prefix of cpu-cycles or cpu// events.
969 * cpu-cycles has been handled by hardcode.
970 * So it must be cpu// events, not kernel pmu event.
971 */
972 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
973 return PMU_EVENT_SYMBOL_ERR;
974
975 p.symbol = strdup(name);
976 r = bsearch(&p, perf_pmu_events_list,
977 (size_t) perf_pmu_events_list_num,
978 sizeof(struct perf_pmu_event_symbol), comp_pmu);
979 free(p.symbol);
980 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
981}
982
Jiri Olsa90e2b222012-06-15 14:31:40 +0800983static int parse_events__scanner(const char *str, void *data, int start_token)
Zheng Yanac20de6f2012-06-15 14:31:39 +0800984{
985 YY_BUFFER_STATE buffer;
986 void *scanner;
987 int ret;
988
Jiri Olsa90e2b222012-06-15 14:31:40 +0800989 ret = parse_events_lex_init_extra(start_token, &scanner);
Zheng Yanac20de6f2012-06-15 14:31:39 +0800990 if (ret)
991 return ret;
992
993 buffer = parse_events__scan_string(str, scanner);
994
995#ifdef PARSER_DEBUG
996 parse_events_debug = 1;
997#endif
998 ret = parse_events_parse(data, scanner);
999
1000 parse_events__flush_buffer(buffer, scanner);
1001 parse_events__delete_buffer(buffer, scanner);
1002 parse_events_lex_destroy(scanner);
1003 return ret;
1004}
1005
Jiri Olsa90e2b222012-06-15 14:31:40 +08001006/*
1007 * parse event config string, return a list of event terms.
1008 */
1009int parse_events_terms(struct list_head *terms, const char *str)
1010{
Arnaldo Carvalho de Melo23b63392013-01-18 16:56:57 -03001011 struct parse_events_terms data = {
Jiri Olsa90e2b222012-06-15 14:31:40 +08001012 .terms = NULL,
1013 };
1014 int ret;
1015
1016 ret = parse_events__scanner(str, &data, PE_START_TERMS);
1017 if (!ret) {
1018 list_splice(data.terms, terms);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001019 zfree(&data.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001020 return 0;
1021 }
1022
Adrian Hunterb2c34fd2013-07-04 16:20:23 +03001023 if (data.terms)
1024 parse_events__free_terms(data.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001025 return ret;
1026}
1027
Jiri Olsad8f7bbc2013-01-15 14:39:51 +01001028int parse_events(struct perf_evlist *evlist, const char *str)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001029{
Arnaldo Carvalho de Melo23b63392013-01-18 16:56:57 -03001030 struct parse_events_evlist data = {
Jiri Olsa46010ab2012-06-15 14:31:38 +08001031 .list = LIST_HEAD_INIT(data.list),
1032 .idx = evlist->nr_entries,
1033 };
Jiri Olsa46010ab2012-06-15 14:31:38 +08001034 int ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001035
Jiri Olsa90e2b222012-06-15 14:31:40 +08001036 ret = parse_events__scanner(str, &data, PE_START_EVENTS);
Kan Liangdcb4e102014-10-07 11:08:50 -04001037 perf_pmu__parse_cleanup();
Jiri Olsa89812fc2012-03-15 20:09:15 +01001038 if (!ret) {
Jiri Olsa46010ab2012-06-15 14:31:38 +08001039 int entries = data.idx - evlist->nr_entries;
1040 perf_evlist__splice_list_tail(evlist, &data.list, entries);
Namhyung Kim97f63e42013-01-22 18:09:29 +09001041 evlist->nr_groups += data.nr_groups;
Jiri Olsa89812fc2012-03-15 20:09:15 +01001042 return 0;
1043 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02001044
Jiri Olsa5d7be902012-03-20 19:15:40 +01001045 /*
1046 * There are 2 users - builtin-record and builtin-test objects.
1047 * Both call perf_evlist__delete in case of error, so we dont
1048 * need to bother.
1049 */
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02001050 return ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001051}
1052
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001053int parse_events_option(const struct option *opt, const char *str,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001054 int unset __maybe_unused)
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001055{
1056 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
Jiri Olsad8f7bbc2013-01-15 14:39:51 +01001057 int ret = parse_events(evlist, str);
Andi Kleen9175ce12012-10-26 13:30:06 -07001058
1059 if (ret) {
1060 fprintf(stderr, "invalid or unsupported event: '%s'\n", str);
1061 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
1062 }
1063 return ret;
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001064}
1065
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001066int parse_filter(const struct option *opt, const char *str,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001067 int unset __maybe_unused)
Li Zefanc171b552009-10-15 11:22:07 +08001068{
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001069 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001070 struct perf_evsel *last = NULL;
Li Zefanc171b552009-10-15 11:22:07 +08001071
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001072 if (evlist->nr_entries > 0)
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001073 last = perf_evlist__last(evlist);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001074
1075 if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
Li Zefanc171b552009-10-15 11:22:07 +08001076 fprintf(stderr,
Arnaldo Carvalho de Melo281f92f2014-10-01 15:05:32 -03001077 "--filter option should follow a -e tracepoint option\n");
Li Zefanc171b552009-10-15 11:22:07 +08001078 return -1;
1079 }
1080
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001081 last->filter = strdup(str);
1082 if (last->filter == NULL) {
Li Zefanc171b552009-10-15 11:22:07 +08001083 fprintf(stderr, "not enough memory to hold filter string\n");
1084 return -1;
1085 }
Li Zefanc171b552009-10-15 11:22:07 +08001086
1087 return 0;
1088}
1089
Thomas Gleixner86847b62009-06-06 12:24:17 +02001090static const char * const event_type_descriptors[] = {
Thomas Gleixner86847b62009-06-06 12:24:17 +02001091 "Hardware event",
1092 "Software event",
1093 "Tracepoint event",
1094 "Hardware cache event",
Liming Wang41bdcb22009-12-29 16:37:07 +08001095 "Raw hardware event descriptor",
1096 "Hardware breakpoint",
Thomas Gleixner86847b62009-06-06 12:24:17 +02001097};
1098
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001099/*
Jason Baronf6bdafe2009-07-21 12:20:22 -04001100 * Print the events from <debugfs_mount_point>/tracing/events
1101 */
1102
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001103void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
1104 bool name_only)
Jason Baronf6bdafe2009-07-21 12:20:22 -04001105{
1106 DIR *sys_dir, *evt_dir;
1107 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001108 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +02001109 char dir_path[MAXPATHLEN];
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00001110 char sbuf[STRERR_BUFSIZE];
Jason Baronf6bdafe2009-07-21 12:20:22 -04001111
Pekka Enbergf11cfc62013-10-15 23:07:27 +03001112 if (debugfs_valid_mountpoint(tracing_events_path)) {
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00001113 printf(" [ Tracepoints not available: %s ]\n",
1114 strerror_r(errno, sbuf, sizeof(sbuf)));
Jason Baronf6bdafe2009-07-21 12:20:22 -04001115 return;
Pekka Enbergf11cfc62013-10-15 23:07:27 +03001116 }
Jason Baronf6bdafe2009-07-21 12:20:22 -04001117
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001118 sys_dir = opendir(tracing_events_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -04001119 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +02001120 return;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001121
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03001122 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001123 if (subsys_glob != NULL &&
1124 !strglobmatch(sys_dirent.d_name, subsys_glob))
1125 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02001126
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001127 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Eric Dumazet725b1362009-09-24 15:39:09 +02001128 sys_dirent.d_name);
1129 evt_dir = opendir(dir_path);
1130 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03001131 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02001132
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03001133 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001134 if (event_glob != NULL &&
1135 !strglobmatch(evt_dirent.d_name, event_glob))
1136 continue;
1137
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001138 if (name_only) {
1139 printf("%s:%s ", sys_dirent.d_name, evt_dirent.d_name);
1140 continue;
1141 }
1142
Jason Baronf6bdafe2009-07-21 12:20:22 -04001143 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1144 sys_dirent.d_name, evt_dirent.d_name);
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001145 printf(" %-50s [%s]\n", evt_path,
Liming Wang41bdcb22009-12-29 16:37:07 +08001146 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
Jason Baronf6bdafe2009-07-21 12:20:22 -04001147 }
1148 closedir(evt_dir);
1149 }
Jason Baronf6bdafe2009-07-21 12:20:22 -04001150 closedir(sys_dir);
1151}
1152
1153/*
Thomas Renninger20c457b2011-01-03 17:50:45 +01001154 * Check whether event is in <debugfs_mount_point>/tracing/events
1155 */
1156
1157int is_valid_tracepoint(const char *event_string)
1158{
1159 DIR *sys_dir, *evt_dir;
1160 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
1161 char evt_path[MAXPATHLEN];
1162 char dir_path[MAXPATHLEN];
1163
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001164 if (debugfs_valid_mountpoint(tracing_events_path))
Thomas Renninger20c457b2011-01-03 17:50:45 +01001165 return 0;
1166
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001167 sys_dir = opendir(tracing_events_path);
Thomas Renninger20c457b2011-01-03 17:50:45 +01001168 if (!sys_dir)
1169 return 0;
1170
1171 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1172
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001173 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Thomas Renninger20c457b2011-01-03 17:50:45 +01001174 sys_dirent.d_name);
1175 evt_dir = opendir(dir_path);
1176 if (!evt_dir)
1177 continue;
1178
1179 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1180 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1181 sys_dirent.d_name, evt_dirent.d_name);
1182 if (!strcmp(evt_path, event_string)) {
1183 closedir(evt_dir);
1184 closedir(sys_dir);
1185 return 1;
1186 }
1187 }
1188 closedir(evt_dir);
1189 }
1190 closedir(sys_dir);
1191 return 0;
1192}
1193
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001194static bool is_event_supported(u8 type, unsigned config)
1195{
1196 bool ret = true;
Vince Weaver88fee522013-12-30 15:39:45 -05001197 int open_return;
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001198 struct perf_evsel *evsel;
1199 struct perf_event_attr attr = {
1200 .type = type,
1201 .config = config,
1202 .disabled = 1,
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001203 };
1204 struct {
1205 struct thread_map map;
1206 int threads[1];
1207 } tmap = {
1208 .map.nr = 1,
1209 .threads = { 0 },
1210 };
1211
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03001212 evsel = perf_evsel__new(&attr);
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001213 if (evsel) {
Vince Weaver88fee522013-12-30 15:39:45 -05001214 open_return = perf_evsel__open(evsel, NULL, &tmap.map);
1215 ret = open_return >= 0;
1216
1217 if (open_return == -EACCES) {
1218 /*
1219 * This happens if the paranoid value
1220 * /proc/sys/kernel/perf_event_paranoid is set to 2
1221 * Re-run with exclude_kernel set; we don't do that
1222 * by default as some ARM machines do not support it.
1223 *
1224 */
1225 evsel->attr.exclude_kernel = 1;
1226 ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0;
1227 }
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001228 perf_evsel__delete(evsel);
1229 }
1230
1231 return ret;
1232}
1233
Jiri Olsa1dc127602012-07-04 00:00:44 +02001234static void __print_events_type(u8 type, struct event_symbol *syms,
1235 unsigned max)
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001236{
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001237 char name[64];
Jiri Olsa1dc127602012-07-04 00:00:44 +02001238 unsigned i;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001239
Jiri Olsa1dc127602012-07-04 00:00:44 +02001240 for (i = 0; i < max ; i++, syms++) {
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001241 if (!is_event_supported(type, i))
1242 continue;
1243
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001244 if (strlen(syms->alias))
1245 snprintf(name, sizeof(name), "%s OR %s",
1246 syms->symbol, syms->alias);
1247 else
1248 snprintf(name, sizeof(name), "%s", syms->symbol);
1249
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001250 printf(" %-50s [%s]\n", name, event_type_descriptors[type]);
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001251 }
1252}
1253
Jiri Olsa1dc127602012-07-04 00:00:44 +02001254void print_events_type(u8 type)
1255{
1256 if (type == PERF_TYPE_SOFTWARE)
1257 __print_events_type(type, event_symbols_sw, PERF_COUNT_SW_MAX);
1258 else
1259 __print_events_type(type, event_symbols_hw, PERF_COUNT_HW_MAX);
1260}
1261
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001262int print_hwcache_events(const char *event_glob, bool name_only)
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001263{
1264 unsigned int type, op, i, printed = 0;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03001265 char name[64];
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001266
1267 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1268 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1269 /* skip invalid cache type */
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03001270 if (!perf_evsel__is_cache_op_valid(type, op))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001271 continue;
1272
1273 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03001274 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
1275 name, sizeof(name));
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001276 if (event_glob != NULL && !strglobmatch(name, event_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001277 continue;
1278
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001279 if (!is_event_supported(PERF_TYPE_HW_CACHE,
1280 type | (op << 8) | (i << 16)))
1281 continue;
1282
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001283 if (name_only)
1284 printf("%s ", name);
1285 else
1286 printf(" %-50s [%s]\n", name,
1287 event_type_descriptors[PERF_TYPE_HW_CACHE]);
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001288 ++printed;
1289 }
1290 }
1291 }
1292
Andi Kleendc098b32013-04-20 11:02:29 -07001293 if (printed)
1294 printf("\n");
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001295 return printed;
1296}
1297
Jiri Olsa1dc127602012-07-04 00:00:44 +02001298static void print_symbol_events(const char *event_glob, unsigned type,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001299 struct event_symbol *syms, unsigned max,
1300 bool name_only)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001301{
Jiri Olsa1dc127602012-07-04 00:00:44 +02001302 unsigned i, printed = 0;
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001303 char name[MAX_NAME_LEN];
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001304
Jiri Olsa1dc127602012-07-04 00:00:44 +02001305 for (i = 0; i < max; i++, syms++) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001306
1307 if (event_glob != NULL &&
1308 !(strglobmatch(syms->symbol, event_glob) ||
1309 (syms->alias && strglobmatch(syms->alias, event_glob))))
1310 continue;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001311
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001312 if (!is_event_supported(type, i))
1313 continue;
1314
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001315 if (name_only) {
1316 printf("%s ", syms->symbol);
1317 continue;
1318 }
1319
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301320 if (strlen(syms->alias))
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001321 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301322 else
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001323 strncpy(name, syms->symbol, MAX_NAME_LEN);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001324
Jiri Olsa1dc127602012-07-04 00:00:44 +02001325 printf(" %-50s [%s]\n", name, event_type_descriptors[type]);
1326
1327 printed++;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001328 }
1329
Jiri Olsa1dc127602012-07-04 00:00:44 +02001330 if (printed)
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001331 printf("\n");
Jiri Olsa1dc127602012-07-04 00:00:44 +02001332}
1333
1334/*
1335 * Print the help text for the event symbols:
1336 */
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001337void print_events(const char *event_glob, bool name_only)
Jiri Olsa1dc127602012-07-04 00:00:44 +02001338{
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001339 if (!name_only) {
1340 printf("\n");
1341 printf("List of pre-defined events (to be used in -e):\n");
1342 }
Jiri Olsa1dc127602012-07-04 00:00:44 +02001343
1344 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001345 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
Jiri Olsa1dc127602012-07-04 00:00:44 +02001346
1347 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001348 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
Jiri Olsa1dc127602012-07-04 00:00:44 +02001349
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001350 print_hwcache_events(event_glob, name_only);
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001351
Andi Kleendc098b32013-04-20 11:02:29 -07001352 print_pmu_events(event_glob, name_only);
1353
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001354 if (event_glob != NULL)
1355 return;
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +05301356
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001357 if (!name_only) {
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001358 printf(" %-50s [%s]\n",
1359 "rNNN",
1360 event_type_descriptors[PERF_TYPE_RAW]);
1361 printf(" %-50s [%s]\n",
1362 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
1363 event_type_descriptors[PERF_TYPE_RAW]);
Borislav Petkovaf3df2c2012-09-28 19:47:07 +02001364 printf(" (see 'man perf-list' on how to encode it)\n");
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001365 printf("\n");
Thomas Gleixner86847b62009-06-06 12:24:17 +02001366
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001367 printf(" %-50s [%s]\n",
1368 "mem:<addr>[:access]",
Liming Wang41bdcb22009-12-29 16:37:07 +08001369 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001370 printf("\n");
1371 }
Frederic Weisbecker1b290d62009-11-23 15:42:35 +01001372
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001373 print_tracepoint_events(NULL, NULL, name_only);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001374}
Jiri Olsa8f707d82012-03-15 20:09:16 +01001375
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001376int parse_events__is_hardcoded_term(struct parse_events_term *term)
Jiri Olsa8f707d82012-03-15 20:09:16 +01001377{
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001378 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001379}
1380
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001381static int new_term(struct parse_events_term **_term, int type_val,
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001382 int type_term, char *config,
Robert Richterb527bab2012-08-07 19:43:13 +02001383 char *str, u64 num)
Jiri Olsa8f707d82012-03-15 20:09:16 +01001384{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001385 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001386
1387 term = zalloc(sizeof(*term));
1388 if (!term)
1389 return -ENOMEM;
1390
1391 INIT_LIST_HEAD(&term->list);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001392 term->type_val = type_val;
1393 term->type_term = type_term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001394 term->config = config;
1395
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001396 switch (type_val) {
Jiri Olsa8f707d82012-03-15 20:09:16 +01001397 case PARSE_EVENTS__TERM_TYPE_NUM:
1398 term->val.num = num;
1399 break;
1400 case PARSE_EVENTS__TERM_TYPE_STR:
1401 term->val.str = str;
1402 break;
1403 default:
Adrian Hunter4be8be62013-07-04 16:20:24 +03001404 free(term);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001405 return -EINVAL;
1406 }
1407
1408 *_term = term;
1409 return 0;
1410}
1411
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001412int parse_events_term__num(struct parse_events_term **term,
Robert Richterb527bab2012-08-07 19:43:13 +02001413 int type_term, char *config, u64 num)
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001414{
1415 return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
1416 config, NULL, num);
1417}
1418
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001419int parse_events_term__str(struct parse_events_term **term,
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001420 int type_term, char *config, char *str)
1421{
1422 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
1423 config, str, 0);
1424}
1425
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001426int parse_events_term__sym_hw(struct parse_events_term **term,
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02001427 char *config, unsigned idx)
1428{
1429 struct event_symbol *sym;
1430
1431 BUG_ON(idx >= PERF_COUNT_HW_MAX);
1432 sym = &event_symbols_hw[idx];
1433
1434 if (config)
1435 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1436 PARSE_EVENTS__TERM_TYPE_USER, config,
1437 (char *) sym->symbol, 0);
1438 else
1439 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1440 PARSE_EVENTS__TERM_TYPE_USER,
1441 (char *) "event", (char *) sym->symbol, 0);
1442}
1443
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001444int parse_events_term__clone(struct parse_events_term **new,
1445 struct parse_events_term *term)
Zheng Yana6146d52012-06-15 14:31:41 +08001446{
1447 return new_term(new, term->type_val, term->type_term, term->config,
1448 term->val.str, term->val.num);
1449}
1450
Jiri Olsa8f707d82012-03-15 20:09:16 +01001451void parse_events__free_terms(struct list_head *terms)
1452{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001453 struct parse_events_term *term, *h;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001454
1455 list_for_each_entry_safe(term, h, terms, list)
1456 free(term);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001457}