blob: f6822d9b2b53d71f4f0bfd8dc1d71e37e9c02d60 [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 sys_dir = opendir(tracing_events_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400179 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200180 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400181
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300182 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200183
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200184 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Eric Dumazet725b1362009-09-24 15:39:09 +0200185 sys_dirent.d_name);
186 evt_dir = opendir(dir_path);
187 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300188 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200189
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300190 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200191
192 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
Jason Baronf6bdafe2009-07-21 12:20:22 -0400193 evt_dirent.d_name);
Eric Dumazet725b1362009-09-24 15:39:09 +0200194 fd = open(evt_path, O_RDONLY);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400195 if (fd < 0)
196 continue;
197 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
198 close(fd);
199 continue;
200 }
201 close(fd);
202 id = atoll(id_buf);
203 if (id == config) {
204 closedir(evt_dir);
205 closedir(sys_dir);
Julia Lawall59b4cae2009-12-06 10:16:30 +0100206 path = zalloc(sizeof(*path));
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200207 path->system = malloc(MAX_EVENT_LENGTH);
208 if (!path->system) {
209 free(path);
210 return NULL;
211 }
212 path->name = malloc(MAX_EVENT_LENGTH);
213 if (!path->name) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300214 zfree(&path->system);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200215 free(path);
216 return NULL;
217 }
218 strncpy(path->system, sys_dirent.d_name,
219 MAX_EVENT_LENGTH);
220 strncpy(path->name, evt_dirent.d_name,
221 MAX_EVENT_LENGTH);
222 return path;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400223 }
224 }
225 closedir(evt_dir);
226 }
227
Jason Baronf6bdafe2009-07-21 12:20:22 -0400228 closedir(sys_dir);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200229 return NULL;
230}
231
Namhyung Kime7c93f02013-06-26 16:14:05 +0900232struct tracepoint_path *tracepoint_name_to_path(const char *name)
233{
234 struct tracepoint_path *path = zalloc(sizeof(*path));
235 char *str = strchr(name, ':');
236
237 if (path == NULL || str == NULL) {
238 free(path);
239 return NULL;
240 }
241
242 path->system = strndup(name, str - name);
243 path->name = strdup(str+1);
244
245 if (path->system == NULL || path->name == NULL) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300246 zfree(&path->system);
247 zfree(&path->name);
Namhyung Kime7c93f02013-06-26 16:14:05 +0900248 free(path);
249 path = NULL;
250 }
251
252 return path;
253}
254
David Ahern1424dc92011-03-09 22:23:28 -0700255const char *event_type(int type)
256{
257 switch (type) {
258 case PERF_TYPE_HARDWARE:
259 return "hardware";
260
261 case PERF_TYPE_SOFTWARE:
262 return "software";
263
264 case PERF_TYPE_TRACEPOINT:
265 return "tracepoint";
266
267 case PERF_TYPE_HW_CACHE:
268 return "hardware-cache";
269
270 default:
271 break;
272 }
273
274 return "unknown";
275}
276
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800277
278
Stephane Eranian410136f2013-11-12 17:58:49 +0100279static struct perf_evsel *
280__add_event(struct list_head *list, int *idx,
281 struct perf_event_attr *attr,
282 char *name, struct cpu_map *cpus)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100283{
284 struct perf_evsel *evsel;
285
286 event_attr_init(attr);
287
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300288 evsel = perf_evsel__new_idx(attr, (*idx)++);
David Ahernc5cd8ac2013-07-02 13:27:25 -0600289 if (!evsel)
Stephane Eranian410136f2013-11-12 17:58:49 +0100290 return NULL;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100291
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800292 evsel->cpus = cpus;
Arnaldo Carvalho de Melo9db17632012-06-12 13:45:00 -0300293 if (name)
294 evsel->name = strdup(name);
Jiri Olsab847cbd2012-05-21 09:12:51 +0200295 list_add_tail(&evsel->node, list);
Stephane Eranian410136f2013-11-12 17:58:49 +0100296 return evsel;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100297}
298
David Ahernc5cd8ac2013-07-02 13:27:25 -0600299static int add_event(struct list_head *list, int *idx,
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800300 struct perf_event_attr *attr, char *name)
301{
Stephane Eranian410136f2013-11-12 17:58:49 +0100302 return __add_event(list, idx, attr, name, NULL) ? 0 : -ENOMEM;
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800303}
304
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300305static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
Ingo Molnar8326f442009-06-05 20:22:46 +0200306{
307 int i, j;
Paul Mackerras61c45982009-07-01 13:04:34 +1000308 int n, longest = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200309
310 for (i = 0; i < size; i++) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300311 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
Paul Mackerras61c45982009-07-01 13:04:34 +1000312 n = strlen(names[i][j]);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100313 if (n > longest && !strncasecmp(str, names[i][j], n))
Paul Mackerras61c45982009-07-01 13:04:34 +1000314 longest = n;
315 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100316 if (longest > 0)
Paul Mackerras61c45982009-07-01 13:04:34 +1000317 return i;
Ingo Molnar8326f442009-06-05 20:22:46 +0200318 }
319
Ingo Molnar89536452009-06-06 21:04:17 +0200320 return -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200321}
322
David Ahernc5cd8ac2013-07-02 13:27:25 -0600323int parse_events_add_cache(struct list_head *list, int *idx,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100324 char *type, char *op_result1, char *op_result2)
Ingo Molnar8326f442009-06-05 20:22:46 +0200325{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100326 struct perf_event_attr attr;
327 char name[MAX_NAME_LEN];
Paul Mackerras61c45982009-07-01 13:04:34 +1000328 int cache_type = -1, cache_op = -1, cache_result = -1;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100329 char *op_result[2] = { op_result1, op_result2 };
330 int i, n;
Ingo Molnar8326f442009-06-05 20:22:46 +0200331
Ingo Molnar8326f442009-06-05 20:22:46 +0200332 /*
333 * No fallback - if we cannot get a clear cache type
334 * then bail out:
335 */
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300336 cache_type = parse_aliases(type, perf_evsel__hw_cache,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100337 PERF_COUNT_HW_CACHE_MAX);
Ingo Molnar8326f442009-06-05 20:22:46 +0200338 if (cache_type == -1)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100339 return -EINVAL;
Ingo Molnar8326f442009-06-05 20:22:46 +0200340
Jiri Olsa89812fc2012-03-15 20:09:15 +0100341 n = snprintf(name, MAX_NAME_LEN, "%s", type);
342
343 for (i = 0; (i < 2) && (op_result[i]); i++) {
344 char *str = op_result[i];
345
Jiri Olsa275ef382012-09-05 19:51:33 +0200346 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
Paul Mackerras61c45982009-07-01 13:04:34 +1000347
348 if (cache_op == -1) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300349 cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100350 PERF_COUNT_HW_CACHE_OP_MAX);
Paul Mackerras61c45982009-07-01 13:04:34 +1000351 if (cache_op >= 0) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300352 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
Jiri Olsa89812fc2012-03-15 20:09:15 +0100353 return -EINVAL;
Paul Mackerras61c45982009-07-01 13:04:34 +1000354 continue;
355 }
356 }
357
358 if (cache_result == -1) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300359 cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
360 PERF_COUNT_HW_CACHE_RESULT_MAX);
Paul Mackerras61c45982009-07-01 13:04:34 +1000361 if (cache_result >= 0)
362 continue;
363 }
Paul Mackerras61c45982009-07-01 13:04:34 +1000364 }
365
Ingo Molnar8326f442009-06-05 20:22:46 +0200366 /*
367 * Fall back to reads:
368 */
Ingo Molnar89536452009-06-06 21:04:17 +0200369 if (cache_op == -1)
370 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
Ingo Molnar8326f442009-06-05 20:22:46 +0200371
Ingo Molnar8326f442009-06-05 20:22:46 +0200372 /*
373 * Fall back to accesses:
374 */
375 if (cache_result == -1)
376 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
377
Jiri Olsa89812fc2012-03-15 20:09:15 +0100378 memset(&attr, 0, sizeof(attr));
379 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
380 attr.type = PERF_TYPE_HW_CACHE;
381 return add_event(list, idx, &attr, name);
Ingo Molnar8326f442009-06-05 20:22:46 +0200382}
383
David Ahernc5cd8ac2013-07-02 13:27:25 -0600384static int add_tracepoint(struct list_head *list, int *idx,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100385 char *sys_name, char *evt_name)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200386{
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300387 struct perf_evsel *evsel;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200388
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300389 evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
David Ahernc5cd8ac2013-07-02 13:27:25 -0600390 if (!evsel)
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300391 return -ENOMEM;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200392
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300393 list_add_tail(&evsel->node, list);
David Ahernc5cd8ac2013-07-02 13:27:25 -0600394
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300395 return 0;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200396}
397
David Ahernc5cd8ac2013-07-02 13:27:25 -0600398static int add_tracepoint_multi_event(struct list_head *list, int *idx,
Jiri Olsaf35488f2012-12-17 14:08:37 +0100399 char *sys_name, char *evt_name)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200400{
401 char evt_path[MAXPATHLEN];
402 struct dirent *evt_ent;
403 DIR *evt_dir;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100404 int ret = 0;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200405
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200406 snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200407 evt_dir = opendir(evt_path);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200408 if (!evt_dir) {
409 perror("Can't open event dir");
Jiri Olsa89812fc2012-03-15 20:09:15 +0100410 return -1;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200411 }
412
Jiri Olsa89812fc2012-03-15 20:09:15 +0100413 while (!ret && (evt_ent = readdir(evt_dir))) {
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200414 if (!strcmp(evt_ent->d_name, ".")
415 || !strcmp(evt_ent->d_name, "..")
416 || !strcmp(evt_ent->d_name, "enable")
417 || !strcmp(evt_ent->d_name, "filter"))
418 continue;
419
Jiri Olsa89812fc2012-03-15 20:09:15 +0100420 if (!strglobmatch(evt_ent->d_name, evt_name))
Masami Hiramatsufb1d2ed2010-01-05 17:47:17 -0500421 continue;
422
Jiri Olsa89812fc2012-03-15 20:09:15 +0100423 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200424 }
425
Jiri Olsa0bd3f082012-12-17 14:08:36 +0100426 closedir(evt_dir);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100427 return ret;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200428}
429
David Ahernc5cd8ac2013-07-02 13:27:25 -0600430static int add_tracepoint_event(struct list_head *list, int *idx,
Jiri Olsaf35488f2012-12-17 14:08:37 +0100431 char *sys_name, char *evt_name)
432{
433 return strpbrk(evt_name, "*?") ?
434 add_tracepoint_multi_event(list, idx, sys_name, evt_name) :
435 add_tracepoint(list, idx, sys_name, evt_name);
436}
437
David Ahernc5cd8ac2013-07-02 13:27:25 -0600438static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
Jiri Olsaf35488f2012-12-17 14:08:37 +0100439 char *sys_name, char *evt_name)
440{
441 struct dirent *events_ent;
442 DIR *events_dir;
443 int ret = 0;
444
445 events_dir = opendir(tracing_events_path);
446 if (!events_dir) {
447 perror("Can't open event dir");
448 return -1;
449 }
450
451 while (!ret && (events_ent = readdir(events_dir))) {
452 if (!strcmp(events_ent->d_name, ".")
453 || !strcmp(events_ent->d_name, "..")
454 || !strcmp(events_ent->d_name, "enable")
455 || !strcmp(events_ent->d_name, "header_event")
456 || !strcmp(events_ent->d_name, "header_page"))
457 continue;
458
459 if (!strglobmatch(events_ent->d_name, sys_name))
460 continue;
461
462 ret = add_tracepoint_event(list, idx, events_ent->d_name,
463 evt_name);
464 }
465
466 closedir(events_dir);
467 return ret;
468}
469
David Ahernc5cd8ac2013-07-02 13:27:25 -0600470int parse_events_add_tracepoint(struct list_head *list, int *idx,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100471 char *sys, char *event)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400472{
Jiri Olsaf35488f2012-12-17 14:08:37 +0100473 if (strpbrk(sys, "*?"))
474 return add_tracepoint_multi_sys(list, idx, sys, event);
475 else
476 return add_tracepoint_event(list, idx, sys, event);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400477}
478
Jiri Olsa89812fc2012-03-15 20:09:15 +0100479static int
480parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100481{
482 int i;
483
484 for (i = 0; i < 3; i++) {
Jiri Olsa89812fc2012-03-15 20:09:15 +0100485 if (!type || !type[i])
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100486 break;
487
Jiri Olsa75827322012-06-29 09:22:54 +0200488#define CHECK_SET_TYPE(bit) \
489do { \
490 if (attr->bp_type & bit) \
491 return -EINVAL; \
492 else \
493 attr->bp_type |= bit; \
494} while (0)
495
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100496 switch (type[i]) {
497 case 'r':
Jiri Olsa75827322012-06-29 09:22:54 +0200498 CHECK_SET_TYPE(HW_BREAKPOINT_R);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100499 break;
500 case 'w':
Jiri Olsa75827322012-06-29 09:22:54 +0200501 CHECK_SET_TYPE(HW_BREAKPOINT_W);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100502 break;
503 case 'x':
Jiri Olsa75827322012-06-29 09:22:54 +0200504 CHECK_SET_TYPE(HW_BREAKPOINT_X);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100505 break;
506 default:
Jiri Olsa89812fc2012-03-15 20:09:15 +0100507 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100508 }
509 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100510
Jiri Olsa75827322012-06-29 09:22:54 +0200511#undef CHECK_SET_TYPE
512
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100513 if (!attr->bp_type) /* Default */
514 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
515
Jiri Olsa89812fc2012-03-15 20:09:15 +0100516 return 0;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100517}
518
David Ahernc5cd8ac2013-07-02 13:27:25 -0600519int parse_events_add_breakpoint(struct list_head *list, int *idx,
Jacob Shin3741eb92014-05-29 17:26:51 +0200520 void *ptr, char *type, u64 len)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100521{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100522 struct perf_event_attr attr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100523
Jiri Olsa89812fc2012-03-15 20:09:15 +0100524 memset(&attr, 0, sizeof(attr));
Jiri Olsa9fafd982012-03-20 19:15:39 +0100525 attr.bp_addr = (unsigned long) ptr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100526
Jiri Olsa89812fc2012-03-15 20:09:15 +0100527 if (parse_breakpoint_type(type, &attr))
528 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100529
Jacob Shin3741eb92014-05-29 17:26:51 +0200530 /* Provide some defaults if len is not specified */
531 if (!len) {
532 if (attr.bp_type == HW_BREAKPOINT_X)
533 len = sizeof(long);
534 else
535 len = HW_BREAKPOINT_LEN_4;
536 }
537
538 attr.bp_len = len;
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200539
Jiri Olsa89812fc2012-03-15 20:09:15 +0100540 attr.type = PERF_TYPE_BREAKPOINT;
Jovi Zhang4a841d62012-07-15 03:03:10 +0800541 attr.sample_period = 1;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100542
Jiri Olsa287e74a2012-06-28 23:18:49 +0200543 return add_event(list, idx, &attr, NULL);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100544}
545
Jiri Olsa8f707d82012-03-15 20:09:16 +0100546static int config_term(struct perf_event_attr *attr,
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300547 struct parse_events_term *term)
Jiri Olsa8f707d82012-03-15 20:09:16 +0100548{
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200549#define CHECK_TYPE_VAL(type) \
550do { \
551 if (PARSE_EVENTS__TERM_TYPE_ ## type != term->type_val) \
552 return -EINVAL; \
553} while (0)
554
555 switch (term->type_term) {
Jiri Olsa8f707d82012-03-15 20:09:16 +0100556 case PARSE_EVENTS__TERM_TYPE_CONFIG:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200557 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100558 attr->config = term->val.num;
559 break;
560 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200561 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100562 attr->config1 = term->val.num;
563 break;
564 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200565 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100566 attr->config2 = term->val.num;
567 break;
568 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200569 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100570 attr->sample_period = term->val.num;
571 break;
572 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
573 /*
574 * TODO uncomment when the field is available
575 * attr->branch_sample_type = term->val.num;
576 */
577 break;
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200578 case PARSE_EVENTS__TERM_TYPE_NAME:
579 CHECK_TYPE_VAL(STR);
580 break;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100581 default:
582 return -EINVAL;
583 }
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200584
Jiri Olsa8f707d82012-03-15 20:09:16 +0100585 return 0;
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200586#undef CHECK_TYPE_VAL
Jiri Olsa8f707d82012-03-15 20:09:16 +0100587}
588
589static int config_attr(struct perf_event_attr *attr,
590 struct list_head *head, int fail)
591{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300592 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100593
594 list_for_each_entry(term, head, list)
595 if (config_term(attr, term) && fail)
596 return -EINVAL;
597
598 return 0;
599}
600
David Ahernc5cd8ac2013-07-02 13:27:25 -0600601int parse_events_add_numeric(struct list_head *list, int *idx,
Robert Richterb527bab2012-08-07 19:43:13 +0200602 u32 type, u64 config,
Jiri Olsa8f707d82012-03-15 20:09:16 +0100603 struct list_head *head_config)
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530604{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100605 struct perf_event_attr attr;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530606
Jiri Olsa89812fc2012-03-15 20:09:15 +0100607 memset(&attr, 0, sizeof(attr));
608 attr.type = type;
609 attr.config = config;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100610
611 if (head_config &&
612 config_attr(&attr, head_config, 1))
613 return -EINVAL;
614
Arnaldo Carvalho de Melo9db17632012-06-12 13:45:00 -0300615 return add_event(list, idx, &attr, NULL);
Paul Mackerras61c45982009-07-01 13:04:34 +1000616}
617
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300618static int parse_events__is_name_term(struct parse_events_term *term)
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200619{
620 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
621}
622
Arnaldo Carvalho de Melo9db17632012-06-12 13:45:00 -0300623static char *pmu_event_name(struct list_head *head_terms)
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200624{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300625 struct parse_events_term *term;
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200626
627 list_for_each_entry(term, head_terms, list)
628 if (parse_events__is_name_term(term))
629 return term->val.str;
630
Arnaldo Carvalho de Melo9db17632012-06-12 13:45:00 -0300631 return NULL;
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200632}
633
David Ahernc5cd8ac2013-07-02 13:27:25 -0600634int parse_events_add_pmu(struct list_head *list, int *idx,
Jiri Olsa5f537a22012-03-15 20:09:18 +0100635 char *name, struct list_head *head_config)
636{
637 struct perf_event_attr attr;
Matt Fleming46441bd2014-09-24 15:04:06 +0100638 struct perf_pmu_info info;
Jiri Olsa5f537a22012-03-15 20:09:18 +0100639 struct perf_pmu *pmu;
Stephane Eranian410136f2013-11-12 17:58:49 +0100640 struct perf_evsel *evsel;
Jiri Olsa5f537a22012-03-15 20:09:18 +0100641
642 pmu = perf_pmu__find(name);
643 if (!pmu)
644 return -EINVAL;
645
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300646 if (pmu->default_config) {
647 memcpy(&attr, pmu->default_config,
648 sizeof(struct perf_event_attr));
649 } else {
650 memset(&attr, 0, sizeof(attr));
651 }
Jiri Olsa5f537a22012-03-15 20:09:18 +0100652
Adrian Hunterad962272014-08-15 22:08:40 +0300653 if (!head_config) {
654 attr.type = pmu->type;
655 evsel = __add_event(list, idx, &attr, NULL, pmu->cpus);
656 return evsel ? 0 : -ENOMEM;
657 }
658
Matt Fleming46441bd2014-09-24 15:04:06 +0100659 if (perf_pmu__check_alias(pmu, head_config, &info))
Zheng Yana6146d52012-06-15 14:31:41 +0800660 return -EINVAL;
661
Jiri Olsa5f537a22012-03-15 20:09:18 +0100662 /*
663 * Configure hardcoded terms first, no need to check
664 * return value when called with fail == 0 ;)
665 */
666 config_attr(&attr, head_config, 0);
667
668 if (perf_pmu__config(pmu, &attr, head_config))
669 return -EINVAL;
670
Stephane Eranian410136f2013-11-12 17:58:49 +0100671 evsel = __add_event(list, idx, &attr, pmu_event_name(head_config),
672 pmu->cpus);
673 if (evsel) {
Matt Fleming46441bd2014-09-24 15:04:06 +0100674 evsel->unit = info.unit;
675 evsel->scale = info.scale;
Matt Fleming044330c2014-11-21 10:31:12 +0100676 evsel->per_pkg = info.per_pkg;
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100677 evsel->snapshot = info.snapshot;
Stephane Eranian410136f2013-11-12 17:58:49 +0100678 }
679
680 return evsel ? 0 : -ENOMEM;
Jiri Olsa5f537a22012-03-15 20:09:18 +0100681}
682
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200683int parse_events__modifier_group(struct list_head *list,
684 char *event_mod)
Jiri Olsa89efb022012-08-08 12:14:14 +0200685{
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200686 return parse_events__modifier_event(list, event_mod, true);
687}
688
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300689void parse_events__set_leader(char *name, struct list_head *list)
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200690{
691 struct perf_evsel *leader;
692
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300693 __perf_evlist__set_leader(list);
694 leader = list_entry(list->next, struct perf_evsel, node);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200695 leader->group_name = name ? strdup(name) : NULL;
Jiri Olsa89efb022012-08-08 12:14:14 +0200696}
697
David Ahernc5cd8ac2013-07-02 13:27:25 -0600698/* list_event is assumed to point to malloc'ed memory */
Jiri Olsa5d7be902012-03-20 19:15:40 +0100699void parse_events_update_lists(struct list_head *list_event,
700 struct list_head *list_all)
701{
702 /*
703 * Called for single event definition. Update the
Jiri Olsa89efb022012-08-08 12:14:14 +0200704 * 'all event' list, and reinit the 'single event'
Jiri Olsa5d7be902012-03-20 19:15:40 +0100705 * list, for next event definition.
706 */
707 list_splice_tail(list_event, list_all);
Jiri Olsab847cbd2012-05-21 09:12:51 +0200708 free(list_event);
Jiri Olsa5d7be902012-03-20 19:15:40 +0100709}
710
Jiri Olsaf5b11352012-08-08 12:21:54 +0200711struct event_modifier {
712 int eu;
713 int ek;
714 int eh;
715 int eH;
716 int eG;
717 int precise;
718 int exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +0200719 int sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +1000720 int pinned;
Jiri Olsaf5b11352012-08-08 12:21:54 +0200721};
Paul Mackerras61c45982009-07-01 13:04:34 +1000722
Jiri Olsaf5b11352012-08-08 12:21:54 +0200723static int get_event_modifier(struct event_modifier *mod, char *str,
724 struct perf_evsel *evsel)
725{
726 int eu = evsel ? evsel->attr.exclude_user : 0;
727 int ek = evsel ? evsel->attr.exclude_kernel : 0;
728 int eh = evsel ? evsel->attr.exclude_hv : 0;
729 int eH = evsel ? evsel->attr.exclude_host : 0;
730 int eG = evsel ? evsel->attr.exclude_guest : 0;
731 int precise = evsel ? evsel->attr.precise_ip : 0;
Jiri Olsa3c176312012-10-10 17:39:03 +0200732 int sample_read = 0;
Michael Ellermane9a7c412013-08-06 23:28:05 +1000733 int pinned = evsel ? evsel->attr.pinned : 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +0200734
735 int exclude = eu | ek | eh;
736 int exclude_GH = evsel ? evsel->exclude_GH : 0;
737
Jiri Olsaf5b11352012-08-08 12:21:54 +0200738 memset(mod, 0, sizeof(*mod));
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200739
Paul Mackerras61c45982009-07-01 13:04:34 +1000740 while (*str) {
Peter Zijlstraab608342010-04-08 23:03:20 +0200741 if (*str == 'u') {
742 if (!exclude)
743 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000744 eu = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200745 } else if (*str == 'k') {
746 if (!exclude)
747 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000748 ek = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200749 } else if (*str == 'h') {
750 if (!exclude)
751 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000752 eh = 0;
Joerg Roedel99320cc2012-01-04 17:54:19 +0100753 } else if (*str == 'G') {
754 if (!exclude_GH)
755 exclude_GH = eG = eH = 1;
756 eG = 0;
757 } else if (*str == 'H') {
758 if (!exclude_GH)
759 exclude_GH = eG = eH = 1;
760 eH = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200761 } else if (*str == 'p') {
762 precise++;
David Ahern13427982012-09-13 14:59:13 -0600763 /* use of precise requires exclude_guest */
764 if (!exclude_GH)
765 eG = 1;
Jiri Olsa3c176312012-10-10 17:39:03 +0200766 } else if (*str == 'S') {
767 sample_read = 1;
Michael Ellermane9a7c412013-08-06 23:28:05 +1000768 } else if (*str == 'D') {
769 pinned = 1;
Peter Zijlstraab608342010-04-08 23:03:20 +0200770 } else
Paul Mackerras61c45982009-07-01 13:04:34 +1000771 break;
Peter Zijlstraab608342010-04-08 23:03:20 +0200772
Paul Mackerras61c45982009-07-01 13:04:34 +1000773 ++str;
774 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200775
Jiri Olsa89812fc2012-03-15 20:09:15 +0100776 /*
777 * precise ip:
778 *
779 * 0 - SAMPLE_IP can have arbitrary skid
780 * 1 - SAMPLE_IP must have constant skid
781 * 2 - SAMPLE_IP requested to have 0 skid
782 * 3 - SAMPLE_IP must have 0 skid
783 *
784 * See also PERF_RECORD_MISC_EXACT_IP
785 */
786 if (precise > 3)
787 return -EINVAL;
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200788
Jiri Olsaf5b11352012-08-08 12:21:54 +0200789 mod->eu = eu;
790 mod->ek = ek;
791 mod->eh = eh;
792 mod->eH = eH;
793 mod->eG = eG;
794 mod->precise = precise;
795 mod->exclude_GH = exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +0200796 mod->sample_read = sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +1000797 mod->pinned = pinned;
798
Jiri Olsaf5b11352012-08-08 12:21:54 +0200799 return 0;
800}
801
Jiri Olsa534123f2012-11-13 15:32:58 +0100802/*
803 * Basic modifier sanity check to validate it contains only one
804 * instance of any modifier (apart from 'p') present.
805 */
806static int check_modifier(char *str)
807{
808 char *p = str;
809
810 /* The sizeof includes 0 byte as well. */
Michael Ellermane9a7c412013-08-06 23:28:05 +1000811 if (strlen(str) > (sizeof("ukhGHpppSD") - 1))
Jiri Olsa534123f2012-11-13 15:32:58 +0100812 return -1;
813
814 while (*p) {
815 if (*p != 'p' && strchr(p + 1, *p))
816 return -1;
817 p++;
818 }
819
820 return 0;
821}
822
Jiri Olsaf5b11352012-08-08 12:21:54 +0200823int parse_events__modifier_event(struct list_head *list, char *str, bool add)
824{
825 struct perf_evsel *evsel;
826 struct event_modifier mod;
827
828 if (str == NULL)
829 return 0;
830
Jiri Olsa534123f2012-11-13 15:32:58 +0100831 if (check_modifier(str))
832 return -EINVAL;
833
Jiri Olsaf5b11352012-08-08 12:21:54 +0200834 if (!add && get_event_modifier(&mod, str, NULL))
835 return -EINVAL;
836
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300837 __evlist__for_each(list, evsel) {
Jiri Olsaf5b11352012-08-08 12:21:54 +0200838 if (add && get_event_modifier(&mod, str, evsel))
839 return -EINVAL;
840
841 evsel->attr.exclude_user = mod.eu;
842 evsel->attr.exclude_kernel = mod.ek;
843 evsel->attr.exclude_hv = mod.eh;
844 evsel->attr.precise_ip = mod.precise;
845 evsel->attr.exclude_host = mod.eH;
846 evsel->attr.exclude_guest = mod.eG;
847 evsel->exclude_GH = mod.exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +0200848 evsel->sample_read = mod.sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +1000849
850 if (perf_evsel__is_group_leader(evsel))
851 evsel->attr.pinned = mod.pinned;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100852 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200853
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530854 return 0;
855}
856
Robert Richterac2ba9f2012-08-16 21:10:21 +0200857int parse_events_name(struct list_head *list, char *name)
858{
859 struct perf_evsel *evsel;
860
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300861 __evlist__for_each(list, evsel) {
Robert Richterac2ba9f2012-08-16 21:10:21 +0200862 if (!evsel->name)
863 evsel->name = strdup(name);
864 }
865
866 return 0;
867}
868
Kan Liangdcb4e102014-10-07 11:08:50 -0400869static int
870comp_pmu(const void *p1, const void *p2)
871{
872 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
873 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
874
875 return strcmp(pmu1->symbol, pmu2->symbol);
876}
877
878static void perf_pmu__parse_cleanup(void)
879{
880 if (perf_pmu_events_list_num > 0) {
881 struct perf_pmu_event_symbol *p;
882 int i;
883
884 for (i = 0; i < perf_pmu_events_list_num; i++) {
885 p = perf_pmu_events_list + i;
886 free(p->symbol);
887 }
888 free(perf_pmu_events_list);
889 perf_pmu_events_list = NULL;
890 perf_pmu_events_list_num = 0;
891 }
892}
893
894#define SET_SYMBOL(str, stype) \
895do { \
896 p->symbol = str; \
897 if (!p->symbol) \
898 goto err; \
899 p->type = stype; \
900} while (0)
901
902/*
903 * Read the pmu events list from sysfs
904 * Save it into perf_pmu_events_list
905 */
906static void perf_pmu__parse_init(void)
907{
908
909 struct perf_pmu *pmu = NULL;
910 struct perf_pmu_alias *alias;
911 int len = 0;
912
913 pmu = perf_pmu__find("cpu");
914 if ((pmu == NULL) || list_empty(&pmu->aliases)) {
915 perf_pmu_events_list_num = -1;
916 return;
917 }
918 list_for_each_entry(alias, &pmu->aliases, list) {
919 if (strchr(alias->name, '-'))
920 len++;
921 len++;
922 }
923 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
924 if (!perf_pmu_events_list)
925 return;
926 perf_pmu_events_list_num = len;
927
928 len = 0;
929 list_for_each_entry(alias, &pmu->aliases, list) {
930 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
931 char *tmp = strchr(alias->name, '-');
932
933 if (tmp != NULL) {
934 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
935 PMU_EVENT_SYMBOL_PREFIX);
936 p++;
937 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
938 len += 2;
939 } else {
940 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
941 len++;
942 }
943 }
944 qsort(perf_pmu_events_list, len,
945 sizeof(struct perf_pmu_event_symbol), comp_pmu);
946
947 return;
948err:
949 perf_pmu__parse_cleanup();
950}
951
952enum perf_pmu_event_symbol_type
953perf_pmu__parse_check(const char *name)
954{
955 struct perf_pmu_event_symbol p, *r;
956
957 /* scan kernel pmu events from sysfs if needed */
958 if (perf_pmu_events_list_num == 0)
959 perf_pmu__parse_init();
960 /*
961 * name "cpu" could be prefix of cpu-cycles or cpu// events.
962 * cpu-cycles has been handled by hardcode.
963 * So it must be cpu// events, not kernel pmu event.
964 */
965 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
966 return PMU_EVENT_SYMBOL_ERR;
967
968 p.symbol = strdup(name);
969 r = bsearch(&p, perf_pmu_events_list,
970 (size_t) perf_pmu_events_list_num,
971 sizeof(struct perf_pmu_event_symbol), comp_pmu);
972 free(p.symbol);
973 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
974}
975
Jiri Olsa90e2b222012-06-15 14:31:40 +0800976static int parse_events__scanner(const char *str, void *data, int start_token)
Zheng Yanac20de6f2012-06-15 14:31:39 +0800977{
978 YY_BUFFER_STATE buffer;
979 void *scanner;
980 int ret;
981
Jiri Olsa90e2b222012-06-15 14:31:40 +0800982 ret = parse_events_lex_init_extra(start_token, &scanner);
Zheng Yanac20de6f2012-06-15 14:31:39 +0800983 if (ret)
984 return ret;
985
986 buffer = parse_events__scan_string(str, scanner);
987
988#ifdef PARSER_DEBUG
989 parse_events_debug = 1;
990#endif
991 ret = parse_events_parse(data, scanner);
992
993 parse_events__flush_buffer(buffer, scanner);
994 parse_events__delete_buffer(buffer, scanner);
995 parse_events_lex_destroy(scanner);
996 return ret;
997}
998
Jiri Olsa90e2b222012-06-15 14:31:40 +0800999/*
1000 * parse event config string, return a list of event terms.
1001 */
1002int parse_events_terms(struct list_head *terms, const char *str)
1003{
Arnaldo Carvalho de Melo23b63392013-01-18 16:56:57 -03001004 struct parse_events_terms data = {
Jiri Olsa90e2b222012-06-15 14:31:40 +08001005 .terms = NULL,
1006 };
1007 int ret;
1008
1009 ret = parse_events__scanner(str, &data, PE_START_TERMS);
1010 if (!ret) {
1011 list_splice(data.terms, terms);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001012 zfree(&data.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001013 return 0;
1014 }
1015
Adrian Hunterb2c34fd2013-07-04 16:20:23 +03001016 if (data.terms)
1017 parse_events__free_terms(data.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001018 return ret;
1019}
1020
Jiri Olsad8f7bbc2013-01-15 14:39:51 +01001021int parse_events(struct perf_evlist *evlist, const char *str)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001022{
Arnaldo Carvalho de Melo23b63392013-01-18 16:56:57 -03001023 struct parse_events_evlist data = {
Jiri Olsa46010ab2012-06-15 14:31:38 +08001024 .list = LIST_HEAD_INIT(data.list),
1025 .idx = evlist->nr_entries,
1026 };
Jiri Olsa46010ab2012-06-15 14:31:38 +08001027 int ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001028
Jiri Olsa90e2b222012-06-15 14:31:40 +08001029 ret = parse_events__scanner(str, &data, PE_START_EVENTS);
Kan Liangdcb4e102014-10-07 11:08:50 -04001030 perf_pmu__parse_cleanup();
Jiri Olsa89812fc2012-03-15 20:09:15 +01001031 if (!ret) {
Jiri Olsa46010ab2012-06-15 14:31:38 +08001032 int entries = data.idx - evlist->nr_entries;
1033 perf_evlist__splice_list_tail(evlist, &data.list, entries);
Namhyung Kim97f63e42013-01-22 18:09:29 +09001034 evlist->nr_groups += data.nr_groups;
Jiri Olsa89812fc2012-03-15 20:09:15 +01001035 return 0;
1036 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02001037
Jiri Olsa5d7be902012-03-20 19:15:40 +01001038 /*
1039 * There are 2 users - builtin-record and builtin-test objects.
1040 * Both call perf_evlist__delete in case of error, so we dont
1041 * need to bother.
1042 */
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02001043 return ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001044}
1045
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001046int parse_events_option(const struct option *opt, const char *str,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001047 int unset __maybe_unused)
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001048{
1049 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
Jiri Olsad8f7bbc2013-01-15 14:39:51 +01001050 int ret = parse_events(evlist, str);
Andi Kleen9175ce12012-10-26 13:30:06 -07001051
1052 if (ret) {
1053 fprintf(stderr, "invalid or unsupported event: '%s'\n", str);
1054 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
1055 }
1056 return ret;
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001057}
1058
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001059int parse_filter(const struct option *opt, const char *str,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001060 int unset __maybe_unused)
Li Zefanc171b552009-10-15 11:22:07 +08001061{
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001062 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001063 struct perf_evsel *last = NULL;
Li Zefanc171b552009-10-15 11:22:07 +08001064
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001065 if (evlist->nr_entries > 0)
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001066 last = perf_evlist__last(evlist);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001067
1068 if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
Li Zefanc171b552009-10-15 11:22:07 +08001069 fprintf(stderr,
Arnaldo Carvalho de Melo281f92f2014-10-01 15:05:32 -03001070 "--filter option should follow a -e tracepoint option\n");
Li Zefanc171b552009-10-15 11:22:07 +08001071 return -1;
1072 }
1073
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001074 last->filter = strdup(str);
1075 if (last->filter == NULL) {
Li Zefanc171b552009-10-15 11:22:07 +08001076 fprintf(stderr, "not enough memory to hold filter string\n");
1077 return -1;
1078 }
Li Zefanc171b552009-10-15 11:22:07 +08001079
1080 return 0;
1081}
1082
Thomas Gleixner86847b62009-06-06 12:24:17 +02001083static const char * const event_type_descriptors[] = {
Thomas Gleixner86847b62009-06-06 12:24:17 +02001084 "Hardware event",
1085 "Software event",
1086 "Tracepoint event",
1087 "Hardware cache event",
Liming Wang41bdcb22009-12-29 16:37:07 +08001088 "Raw hardware event descriptor",
1089 "Hardware breakpoint",
Thomas Gleixner86847b62009-06-06 12:24:17 +02001090};
1091
Yunlong Songab0e4802015-02-27 18:21:25 +08001092static int cmp_string(const void *a, const void *b)
1093{
1094 const char * const *as = a;
1095 const char * const *bs = b;
1096
1097 return strcmp(*as, *bs);
1098}
1099
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001100/*
Jason Baronf6bdafe2009-07-21 12:20:22 -04001101 * Print the events from <debugfs_mount_point>/tracing/events
1102 */
1103
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001104void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
1105 bool name_only)
Jason Baronf6bdafe2009-07-21 12:20:22 -04001106{
1107 DIR *sys_dir, *evt_dir;
1108 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001109 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +02001110 char dir_path[MAXPATHLEN];
Yunlong Songab0e4802015-02-27 18:21:25 +08001111 char **evt_list = NULL;
1112 unsigned int evt_i = 0, evt_num = 0;
1113 bool evt_num_known = false;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001114
Yunlong Songab0e4802015-02-27 18:21:25 +08001115restart:
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001116 sys_dir = opendir(tracing_events_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -04001117 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +02001118 return;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001119
Yunlong Songab0e4802015-02-27 18:21:25 +08001120 if (evt_num_known) {
1121 evt_list = zalloc(sizeof(char *) * evt_num);
1122 if (!evt_list)
1123 goto out_close_sys_dir;
1124 }
1125
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03001126 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001127 if (subsys_glob != NULL &&
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001128 !strglobmatch(sys_dirent.d_name, subsys_glob))
1129 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02001130
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001131 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Eric Dumazet725b1362009-09-24 15:39:09 +02001132 sys_dirent.d_name);
1133 evt_dir = opendir(dir_path);
1134 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03001135 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02001136
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03001137 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001138 if (event_glob != NULL &&
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001139 !strglobmatch(evt_dirent.d_name, event_glob))
1140 continue;
1141
Yunlong Songab0e4802015-02-27 18:21:25 +08001142 if (!evt_num_known) {
1143 evt_num++;
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001144 continue;
1145 }
1146
Jason Baronf6bdafe2009-07-21 12:20:22 -04001147 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1148 sys_dirent.d_name, evt_dirent.d_name);
Yunlong Songab0e4802015-02-27 18:21:25 +08001149
1150 evt_list[evt_i] = strdup(evt_path);
1151 if (evt_list[evt_i] == NULL)
1152 goto out_close_evt_dir;
1153 evt_i++;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001154 }
1155 closedir(evt_dir);
1156 }
Jason Baronf6bdafe2009-07-21 12:20:22 -04001157 closedir(sys_dir);
Yunlong Songab0e4802015-02-27 18:21:25 +08001158
1159 if (!evt_num_known) {
1160 evt_num_known = true;
1161 goto restart;
1162 }
1163 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1164 evt_i = 0;
1165 while (evt_i < evt_num) {
1166 if (name_only) {
1167 printf("%s ", evt_list[evt_i++]);
1168 continue;
1169 }
1170 printf(" %-50s [%s]\n", evt_list[evt_i++],
1171 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1172 }
1173 if (evt_num)
1174 printf("\n");
1175
1176out_free:
1177 evt_num = evt_i;
1178 for (evt_i = 0; evt_i < evt_num; evt_i++)
1179 zfree(&evt_list[evt_i]);
1180 zfree(&evt_list);
1181 return;
1182
1183out_close_evt_dir:
1184 closedir(evt_dir);
1185out_close_sys_dir:
1186 closedir(sys_dir);
1187
1188 printf("FATAL: not enough memory to print %s\n",
1189 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1190 if (evt_list)
1191 goto out_free;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001192}
1193
1194/*
Thomas Renninger20c457b2011-01-03 17:50:45 +01001195 * Check whether event is in <debugfs_mount_point>/tracing/events
1196 */
1197
1198int is_valid_tracepoint(const char *event_string)
1199{
1200 DIR *sys_dir, *evt_dir;
1201 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
1202 char evt_path[MAXPATHLEN];
1203 char dir_path[MAXPATHLEN];
1204
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001205 sys_dir = opendir(tracing_events_path);
Thomas Renninger20c457b2011-01-03 17:50:45 +01001206 if (!sys_dir)
1207 return 0;
1208
1209 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1210
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001211 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Thomas Renninger20c457b2011-01-03 17:50:45 +01001212 sys_dirent.d_name);
1213 evt_dir = opendir(dir_path);
1214 if (!evt_dir)
1215 continue;
1216
1217 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1218 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1219 sys_dirent.d_name, evt_dirent.d_name);
1220 if (!strcmp(evt_path, event_string)) {
1221 closedir(evt_dir);
1222 closedir(sys_dir);
1223 return 1;
1224 }
1225 }
1226 closedir(evt_dir);
1227 }
1228 closedir(sys_dir);
1229 return 0;
1230}
1231
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001232static bool is_event_supported(u8 type, unsigned config)
1233{
1234 bool ret = true;
Vince Weaver88fee522013-12-30 15:39:45 -05001235 int open_return;
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001236 struct perf_evsel *evsel;
1237 struct perf_event_attr attr = {
1238 .type = type,
1239 .config = config,
1240 .disabled = 1,
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001241 };
1242 struct {
1243 struct thread_map map;
1244 int threads[1];
1245 } tmap = {
1246 .map.nr = 1,
1247 .threads = { 0 },
1248 };
1249
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03001250 evsel = perf_evsel__new(&attr);
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001251 if (evsel) {
Vince Weaver88fee522013-12-30 15:39:45 -05001252 open_return = perf_evsel__open(evsel, NULL, &tmap.map);
1253 ret = open_return >= 0;
1254
1255 if (open_return == -EACCES) {
1256 /*
1257 * This happens if the paranoid value
1258 * /proc/sys/kernel/perf_event_paranoid is set to 2
1259 * Re-run with exclude_kernel set; we don't do that
1260 * by default as some ARM machines do not support it.
1261 *
1262 */
1263 evsel->attr.exclude_kernel = 1;
1264 ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0;
1265 }
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001266 perf_evsel__delete(evsel);
1267 }
1268
1269 return ret;
1270}
1271
Jiri Olsa1dc127602012-07-04 00:00:44 +02001272static void __print_events_type(u8 type, struct event_symbol *syms,
1273 unsigned max)
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001274{
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001275 char name[64];
Yunlong Songab0e4802015-02-27 18:21:25 +08001276 unsigned int i, evt_i = 0, evt_num = 0;
1277 char **evt_list = NULL;
1278 bool evt_num_known = false;
1279
1280restart:
1281 if (evt_num_known) {
1282 evt_list = zalloc(sizeof(char *) * evt_num);
1283 if (!evt_list)
1284 goto out_enomem;
1285 syms -= max;
1286 }
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001287
Jiri Olsa1dc127602012-07-04 00:00:44 +02001288 for (i = 0; i < max ; i++, syms++) {
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001289 if (!is_event_supported(type, i))
1290 continue;
1291
Yunlong Songab0e4802015-02-27 18:21:25 +08001292 if (!evt_num_known) {
1293 evt_num++;
1294 continue;
1295 }
1296
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001297 if (strlen(syms->alias))
1298 snprintf(name, sizeof(name), "%s OR %s",
1299 syms->symbol, syms->alias);
1300 else
1301 snprintf(name, sizeof(name), "%s", syms->symbol);
1302
Yunlong Songab0e4802015-02-27 18:21:25 +08001303 evt_list[evt_i] = strdup(name);
1304 if (evt_list[evt_i] == NULL)
1305 goto out_enomem;
1306 evt_i++;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001307 }
Yunlong Songab0e4802015-02-27 18:21:25 +08001308
1309 if (!evt_num_known) {
1310 evt_num_known = true;
1311 goto restart;
1312 }
1313 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1314 evt_i = 0;
1315 while (evt_i < evt_num)
1316 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
1317 if (evt_num)
1318 printf("\n");
1319
1320out_free:
1321 evt_num = evt_i;
1322 for (evt_i = 0; evt_i < evt_num; evt_i++)
1323 zfree(&evt_list[evt_i]);
1324 zfree(&evt_list);
1325 return;
1326
1327out_enomem:
1328 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
1329 if (evt_list)
1330 goto out_free;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001331}
1332
Jiri Olsa1dc127602012-07-04 00:00:44 +02001333void print_events_type(u8 type)
1334{
1335 if (type == PERF_TYPE_SOFTWARE)
1336 __print_events_type(type, event_symbols_sw, PERF_COUNT_SW_MAX);
1337 else
1338 __print_events_type(type, event_symbols_hw, PERF_COUNT_HW_MAX);
1339}
1340
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001341int print_hwcache_events(const char *event_glob, bool name_only)
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001342{
Yunlong Songab0e4802015-02-27 18:21:25 +08001343 unsigned int type, op, i, evt_i = 0, evt_num = 0;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03001344 char name[64];
Yunlong Songab0e4802015-02-27 18:21:25 +08001345 char **evt_list = NULL;
1346 bool evt_num_known = false;
1347
1348restart:
1349 if (evt_num_known) {
1350 evt_list = zalloc(sizeof(char *) * evt_num);
1351 if (!evt_list)
1352 goto out_enomem;
1353 }
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001354
1355 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1356 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1357 /* skip invalid cache type */
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03001358 if (!perf_evsel__is_cache_op_valid(type, op))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001359 continue;
1360
1361 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03001362 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
1363 name, sizeof(name));
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001364 if (event_glob != NULL && !strglobmatch(name, event_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001365 continue;
1366
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001367 if (!is_event_supported(PERF_TYPE_HW_CACHE,
1368 type | (op << 8) | (i << 16)))
1369 continue;
1370
Yunlong Songab0e4802015-02-27 18:21:25 +08001371 if (!evt_num_known) {
1372 evt_num++;
1373 continue;
1374 }
1375
1376 evt_list[evt_i] = strdup(name);
1377 if (evt_list[evt_i] == NULL)
1378 goto out_enomem;
1379 evt_i++;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001380 }
1381 }
1382 }
1383
Yunlong Songab0e4802015-02-27 18:21:25 +08001384 if (!evt_num_known) {
1385 evt_num_known = true;
1386 goto restart;
1387 }
1388 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1389 evt_i = 0;
1390 while (evt_i < evt_num) {
1391 if (name_only) {
1392 printf("%s ", evt_list[evt_i++]);
1393 continue;
1394 }
1395 printf(" %-50s [%s]\n", evt_list[evt_i++],
1396 event_type_descriptors[PERF_TYPE_HW_CACHE]);
1397 }
1398 if (evt_num)
Andi Kleendc098b32013-04-20 11:02:29 -07001399 printf("\n");
Yunlong Songab0e4802015-02-27 18:21:25 +08001400
1401out_free:
1402 evt_num = evt_i;
1403 for (evt_i = 0; evt_i < evt_num; evt_i++)
1404 zfree(&evt_list[evt_i]);
1405 zfree(&evt_list);
1406 return evt_num;
1407
1408out_enomem:
1409 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
1410 if (evt_list)
1411 goto out_free;
1412 return evt_num;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001413}
1414
Jiri Olsa1dc127602012-07-04 00:00:44 +02001415static void print_symbol_events(const char *event_glob, unsigned type,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001416 struct event_symbol *syms, unsigned max,
1417 bool name_only)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001418{
Yunlong Songab0e4802015-02-27 18:21:25 +08001419 unsigned int i, evt_i = 0, evt_num = 0;
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001420 char name[MAX_NAME_LEN];
Yunlong Songab0e4802015-02-27 18:21:25 +08001421 char **evt_list = NULL;
1422 bool evt_num_known = false;
1423
1424restart:
1425 if (evt_num_known) {
1426 evt_list = zalloc(sizeof(char *) * evt_num);
1427 if (!evt_list)
1428 goto out_enomem;
1429 syms -= max;
1430 }
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001431
Jiri Olsa1dc127602012-07-04 00:00:44 +02001432 for (i = 0; i < max; i++, syms++) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001433
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001434 if (event_glob != NULL &&
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001435 !(strglobmatch(syms->symbol, event_glob) ||
1436 (syms->alias && strglobmatch(syms->alias, event_glob))))
1437 continue;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001438
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001439 if (!is_event_supported(type, i))
1440 continue;
1441
Yunlong Songab0e4802015-02-27 18:21:25 +08001442 if (!evt_num_known) {
1443 evt_num++;
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001444 continue;
1445 }
1446
Yunlong Songab0e4802015-02-27 18:21:25 +08001447 if (!name_only && strlen(syms->alias))
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001448 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301449 else
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001450 strncpy(name, syms->symbol, MAX_NAME_LEN);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001451
Yunlong Songab0e4802015-02-27 18:21:25 +08001452 evt_list[evt_i] = strdup(name);
1453 if (evt_list[evt_i] == NULL)
1454 goto out_enomem;
1455 evt_i++;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001456 }
1457
Yunlong Songab0e4802015-02-27 18:21:25 +08001458 if (!evt_num_known) {
1459 evt_num_known = true;
1460 goto restart;
1461 }
1462 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1463 evt_i = 0;
1464 while (evt_i < evt_num) {
1465 if (name_only) {
1466 printf("%s ", evt_list[evt_i++]);
1467 continue;
1468 }
1469 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
1470 }
1471 if (evt_num)
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001472 printf("\n");
Yunlong Songab0e4802015-02-27 18:21:25 +08001473
1474out_free:
1475 evt_num = evt_i;
1476 for (evt_i = 0; evt_i < evt_num; evt_i++)
1477 zfree(&evt_list[evt_i]);
1478 zfree(&evt_list);
1479 return;
1480
1481out_enomem:
1482 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
1483 if (evt_list)
1484 goto out_free;
Jiri Olsa1dc127602012-07-04 00:00:44 +02001485}
1486
1487/*
1488 * Print the help text for the event symbols:
1489 */
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001490void print_events(const char *event_glob, bool name_only)
Jiri Olsa1dc127602012-07-04 00:00:44 +02001491{
Jiri Olsa1dc127602012-07-04 00:00:44 +02001492 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001493 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
Jiri Olsa1dc127602012-07-04 00:00:44 +02001494
1495 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001496 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
Jiri Olsa1dc127602012-07-04 00:00:44 +02001497
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001498 print_hwcache_events(event_glob, name_only);
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001499
Andi Kleendc098b32013-04-20 11:02:29 -07001500 print_pmu_events(event_glob, name_only);
1501
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001502 if (event_glob != NULL)
1503 return;
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +05301504
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001505 if (!name_only) {
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001506 printf(" %-50s [%s]\n",
1507 "rNNN",
1508 event_type_descriptors[PERF_TYPE_RAW]);
1509 printf(" %-50s [%s]\n",
1510 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
1511 event_type_descriptors[PERF_TYPE_RAW]);
Borislav Petkovaf3df2c2012-09-28 19:47:07 +02001512 printf(" (see 'man perf-list' on how to encode it)\n");
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001513 printf("\n");
Thomas Gleixner86847b62009-06-06 12:24:17 +02001514
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001515 printf(" %-50s [%s]\n",
Jacob Shin3741eb92014-05-29 17:26:51 +02001516 "mem:<addr>[/len][:access]",
Liming Wang41bdcb22009-12-29 16:37:07 +08001517 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001518 printf("\n");
1519 }
Frederic Weisbecker1b290d62009-11-23 15:42:35 +01001520
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001521 print_tracepoint_events(NULL, NULL, name_only);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001522}
Jiri Olsa8f707d82012-03-15 20:09:16 +01001523
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001524int parse_events__is_hardcoded_term(struct parse_events_term *term)
Jiri Olsa8f707d82012-03-15 20:09:16 +01001525{
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001526 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001527}
1528
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001529static int new_term(struct parse_events_term **_term, int type_val,
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001530 int type_term, char *config,
Robert Richterb527bab2012-08-07 19:43:13 +02001531 char *str, u64 num)
Jiri Olsa8f707d82012-03-15 20:09:16 +01001532{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001533 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001534
1535 term = zalloc(sizeof(*term));
1536 if (!term)
1537 return -ENOMEM;
1538
1539 INIT_LIST_HEAD(&term->list);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001540 term->type_val = type_val;
1541 term->type_term = type_term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001542 term->config = config;
1543
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001544 switch (type_val) {
Jiri Olsa8f707d82012-03-15 20:09:16 +01001545 case PARSE_EVENTS__TERM_TYPE_NUM:
1546 term->val.num = num;
1547 break;
1548 case PARSE_EVENTS__TERM_TYPE_STR:
1549 term->val.str = str;
1550 break;
1551 default:
Adrian Hunter4be8be62013-07-04 16:20:24 +03001552 free(term);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001553 return -EINVAL;
1554 }
1555
1556 *_term = term;
1557 return 0;
1558}
1559
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001560int parse_events_term__num(struct parse_events_term **term,
Robert Richterb527bab2012-08-07 19:43:13 +02001561 int type_term, char *config, u64 num)
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001562{
1563 return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
1564 config, NULL, num);
1565}
1566
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001567int parse_events_term__str(struct parse_events_term **term,
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001568 int type_term, char *config, char *str)
1569{
1570 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
1571 config, str, 0);
1572}
1573
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001574int parse_events_term__sym_hw(struct parse_events_term **term,
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02001575 char *config, unsigned idx)
1576{
1577 struct event_symbol *sym;
1578
1579 BUG_ON(idx >= PERF_COUNT_HW_MAX);
1580 sym = &event_symbols_hw[idx];
1581
1582 if (config)
1583 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1584 PARSE_EVENTS__TERM_TYPE_USER, config,
1585 (char *) sym->symbol, 0);
1586 else
1587 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1588 PARSE_EVENTS__TERM_TYPE_USER,
1589 (char *) "event", (char *) sym->symbol, 0);
1590}
1591
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001592int parse_events_term__clone(struct parse_events_term **new,
1593 struct parse_events_term *term)
Zheng Yana6146d52012-06-15 14:31:41 +08001594{
1595 return new_term(new, term->type_val, term->type_term, term->config,
1596 term->val.str, term->val.num);
1597}
1598
Jiri Olsa8f707d82012-03-15 20:09:16 +01001599void parse_events__free_terms(struct list_head *terms)
1600{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001601 struct parse_events_term *term, *h;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001602
1603 list_for_each_entry_safe(term, h, terms, list)
1604 free(term);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001605}