blob: 7e8ae21906e2a87a02d7043b80917ccc6f8fbe66 [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 Olsaf30a79b2015-06-23 00:36:04 +020020#include "cpumap.h"
Jiri Olsab39b8392015-04-22 21:10:16 +020021#include "asm/bug.h"
Jiri Olsa89812fc2012-03-15 20:09:15 +010022
23#define MAX_NAME_LEN 100
Ingo Molnar8ad8db32009-05-26 11:10:09 +020024
Jiri Olsa82ba1f22012-05-21 09:12:50 +020025#ifdef PARSER_DEBUG
26extern int parse_events_debug;
27#endif
Zheng Yanac20de6f2012-06-15 14:31:39 +080028int parse_events_parse(void *data, void *scanner);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +020029
Kan Liangdcb4e102014-10-07 11:08:50 -040030static struct perf_pmu_event_symbol *perf_pmu_events_list;
31/*
32 * The variable indicates the number of supported pmu event symbols.
33 * 0 means not initialized and ready to init
34 * -1 means failed to init, don't try anymore
35 * >0 is the number of supported pmu event symbols
36 */
37static int perf_pmu_events_list_num;
38
Yunlong Song705750f2015-02-27 18:21:27 +080039struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
Jiri Olsa1dc127602012-07-04 00:00:44 +020040 [PERF_COUNT_HW_CPU_CYCLES] = {
41 .symbol = "cpu-cycles",
42 .alias = "cycles",
43 },
44 [PERF_COUNT_HW_INSTRUCTIONS] = {
45 .symbol = "instructions",
46 .alias = "",
47 },
48 [PERF_COUNT_HW_CACHE_REFERENCES] = {
49 .symbol = "cache-references",
50 .alias = "",
51 },
52 [PERF_COUNT_HW_CACHE_MISSES] = {
53 .symbol = "cache-misses",
54 .alias = "",
55 },
56 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
57 .symbol = "branch-instructions",
58 .alias = "branches",
59 },
60 [PERF_COUNT_HW_BRANCH_MISSES] = {
61 .symbol = "branch-misses",
62 .alias = "",
63 },
64 [PERF_COUNT_HW_BUS_CYCLES] = {
65 .symbol = "bus-cycles",
66 .alias = "",
67 },
68 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
69 .symbol = "stalled-cycles-frontend",
70 .alias = "idle-cycles-frontend",
71 },
72 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
73 .symbol = "stalled-cycles-backend",
74 .alias = "idle-cycles-backend",
75 },
76 [PERF_COUNT_HW_REF_CPU_CYCLES] = {
77 .symbol = "ref-cycles",
78 .alias = "",
79 },
80};
Ingo Molnar8ad8db32009-05-26 11:10:09 +020081
Yunlong Song705750f2015-02-27 18:21:27 +080082struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
Jiri Olsa1dc127602012-07-04 00:00:44 +020083 [PERF_COUNT_SW_CPU_CLOCK] = {
84 .symbol = "cpu-clock",
85 .alias = "",
86 },
87 [PERF_COUNT_SW_TASK_CLOCK] = {
88 .symbol = "task-clock",
89 .alias = "",
90 },
91 [PERF_COUNT_SW_PAGE_FAULTS] = {
92 .symbol = "page-faults",
93 .alias = "faults",
94 },
95 [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
96 .symbol = "context-switches",
97 .alias = "cs",
98 },
99 [PERF_COUNT_SW_CPU_MIGRATIONS] = {
100 .symbol = "cpu-migrations",
101 .alias = "migrations",
102 },
103 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
104 .symbol = "minor-faults",
105 .alias = "",
106 },
107 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
108 .symbol = "major-faults",
109 .alias = "",
110 },
111 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
112 .symbol = "alignment-faults",
113 .alias = "",
114 },
115 [PERF_COUNT_SW_EMULATION_FAULTS] = {
116 .symbol = "emulation-faults",
117 .alias = "",
118 },
Adrian Hunterd22d1a22013-08-31 21:50:52 +0300119 [PERF_COUNT_SW_DUMMY] = {
120 .symbol = "dummy",
121 .alias = "",
122 },
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200123};
124
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200125#define __PERF_EVENT_FIELD(config, name) \
126 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
Ingo Molnar52425192009-05-26 09:17:18 +0200127
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200128#define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200129#define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200130#define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200131#define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
Ingo Molnar52425192009-05-26 09:17:18 +0200132
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300133#define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400134 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300135 if (sys_dirent.d_type == DT_DIR && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400136 (strcmp(sys_dirent.d_name, ".")) && \
137 (strcmp(sys_dirent.d_name, "..")))
138
Peter Zijlstraae07b632009-08-06 16:48:54 +0200139static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
140{
141 char evt_path[MAXPATHLEN];
142 int fd;
143
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200144 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
Peter Zijlstraae07b632009-08-06 16:48:54 +0200145 sys_dir->d_name, evt_dir->d_name);
146 fd = open(evt_path, O_RDONLY);
147 if (fd < 0)
148 return -EINVAL;
149 close(fd);
150
151 return 0;
152}
153
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300154#define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400155 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300156 if (evt_dirent.d_type == DT_DIR && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400157 (strcmp(evt_dirent.d_name, ".")) && \
Peter Zijlstraae07b632009-08-06 16:48:54 +0200158 (strcmp(evt_dirent.d_name, "..")) && \
159 (!tp_event_has_id(&sys_dirent, &evt_dirent)))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400160
Li Zefan270bbbe2009-09-17 16:34:51 +0800161#define MAX_EVENT_LENGTH 512
Jason Baronf6bdafe2009-07-21 12:20:22 -0400162
Jason Baronf6bdafe2009-07-21 12:20:22 -0400163
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200164struct tracepoint_path *tracepoint_id_to_path(u64 config)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400165{
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200166 struct tracepoint_path *path = NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400167 DIR *sys_dir, *evt_dir;
168 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Stephane Eranian8aa8a7c2012-03-13 16:51:02 +0100169 char id_buf[24];
Eric Dumazet725b1362009-09-24 15:39:09 +0200170 int fd;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400171 u64 id;
172 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +0200173 char dir_path[MAXPATHLEN];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400174
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200175 sys_dir = opendir(tracing_events_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400176 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200177 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400178
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300179 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200180
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200181 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Eric Dumazet725b1362009-09-24 15:39:09 +0200182 sys_dirent.d_name);
183 evt_dir = opendir(dir_path);
184 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300185 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200186
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300187 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200188
189 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
Jason Baronf6bdafe2009-07-21 12:20:22 -0400190 evt_dirent.d_name);
Eric Dumazet725b1362009-09-24 15:39:09 +0200191 fd = open(evt_path, O_RDONLY);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400192 if (fd < 0)
193 continue;
194 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
195 close(fd);
196 continue;
197 }
198 close(fd);
199 id = atoll(id_buf);
200 if (id == config) {
201 closedir(evt_dir);
202 closedir(sys_dir);
Julia Lawall59b4cae2009-12-06 10:16:30 +0100203 path = zalloc(sizeof(*path));
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200204 path->system = malloc(MAX_EVENT_LENGTH);
205 if (!path->system) {
206 free(path);
207 return NULL;
208 }
209 path->name = malloc(MAX_EVENT_LENGTH);
210 if (!path->name) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300211 zfree(&path->system);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200212 free(path);
213 return NULL;
214 }
215 strncpy(path->system, sys_dirent.d_name,
216 MAX_EVENT_LENGTH);
217 strncpy(path->name, evt_dirent.d_name,
218 MAX_EVENT_LENGTH);
219 return path;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400220 }
221 }
222 closedir(evt_dir);
223 }
224
Jason Baronf6bdafe2009-07-21 12:20:22 -0400225 closedir(sys_dir);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200226 return NULL;
227}
228
Namhyung Kime7c93f02013-06-26 16:14:05 +0900229struct tracepoint_path *tracepoint_name_to_path(const char *name)
230{
231 struct tracepoint_path *path = zalloc(sizeof(*path));
232 char *str = strchr(name, ':');
233
234 if (path == NULL || str == NULL) {
235 free(path);
236 return NULL;
237 }
238
239 path->system = strndup(name, str - name);
240 path->name = strdup(str+1);
241
242 if (path->system == NULL || path->name == NULL) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300243 zfree(&path->system);
244 zfree(&path->name);
Namhyung Kime7c93f02013-06-26 16:14:05 +0900245 free(path);
246 path = NULL;
247 }
248
249 return path;
250}
251
David Ahern1424dc92011-03-09 22:23:28 -0700252const char *event_type(int type)
253{
254 switch (type) {
255 case PERF_TYPE_HARDWARE:
256 return "hardware";
257
258 case PERF_TYPE_SOFTWARE:
259 return "software";
260
261 case PERF_TYPE_TRACEPOINT:
262 return "tracepoint";
263
264 case PERF_TYPE_HW_CACHE:
265 return "hardware-cache";
266
267 default:
268 break;
269 }
270
271 return "unknown";
272}
273
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800274
275
Stephane Eranian410136f2013-11-12 17:58:49 +0100276static struct perf_evsel *
277__add_event(struct list_head *list, int *idx,
278 struct perf_event_attr *attr,
Jiri Olsa930a2e22015-07-29 05:42:10 -0400279 char *name, struct cpu_map *cpus,
280 struct list_head *config_terms)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100281{
282 struct perf_evsel *evsel;
283
284 event_attr_init(attr);
285
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300286 evsel = perf_evsel__new_idx(attr, (*idx)++);
David Ahernc5cd8ac2013-07-02 13:27:25 -0600287 if (!evsel)
Stephane Eranian410136f2013-11-12 17:58:49 +0100288 return NULL;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100289
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200290 if (cpus)
291 evsel->cpus = cpu_map__get(cpus);
292
Arnaldo Carvalho de Melo9db17632012-06-12 13:45:00 -0300293 if (name)
294 evsel->name = strdup(name);
Jiri Olsa930a2e22015-07-29 05:42:10 -0400295
296 if (config_terms)
297 list_splice(config_terms, &evsel->config_terms);
298
Jiri Olsab847cbd2012-05-21 09:12:51 +0200299 list_add_tail(&evsel->node, list);
Stephane Eranian410136f2013-11-12 17:58:49 +0100300 return evsel;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100301}
302
David Ahernc5cd8ac2013-07-02 13:27:25 -0600303static int add_event(struct list_head *list, int *idx,
Jiri Olsa930a2e22015-07-29 05:42:10 -0400304 struct perf_event_attr *attr, char *name,
305 struct list_head *config_terms)
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800306{
Jiri Olsa930a2e22015-07-29 05:42:10 -0400307 return __add_event(list, idx, attr, name, NULL, config_terms) ? 0 : -ENOMEM;
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800308}
309
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300310static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
Ingo Molnar8326f442009-06-05 20:22:46 +0200311{
312 int i, j;
Paul Mackerras61c45982009-07-01 13:04:34 +1000313 int n, longest = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200314
315 for (i = 0; i < size; i++) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300316 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
Paul Mackerras61c45982009-07-01 13:04:34 +1000317 n = strlen(names[i][j]);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100318 if (n > longest && !strncasecmp(str, names[i][j], n))
Paul Mackerras61c45982009-07-01 13:04:34 +1000319 longest = n;
320 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100321 if (longest > 0)
Paul Mackerras61c45982009-07-01 13:04:34 +1000322 return i;
Ingo Molnar8326f442009-06-05 20:22:46 +0200323 }
324
Ingo Molnar89536452009-06-06 21:04:17 +0200325 return -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200326}
327
David Ahernc5cd8ac2013-07-02 13:27:25 -0600328int parse_events_add_cache(struct list_head *list, int *idx,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100329 char *type, char *op_result1, char *op_result2)
Ingo Molnar8326f442009-06-05 20:22:46 +0200330{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100331 struct perf_event_attr attr;
332 char name[MAX_NAME_LEN];
Paul Mackerras61c45982009-07-01 13:04:34 +1000333 int cache_type = -1, cache_op = -1, cache_result = -1;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100334 char *op_result[2] = { op_result1, op_result2 };
335 int i, n;
Ingo Molnar8326f442009-06-05 20:22:46 +0200336
Ingo Molnar8326f442009-06-05 20:22:46 +0200337 /*
338 * No fallback - if we cannot get a clear cache type
339 * then bail out:
340 */
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300341 cache_type = parse_aliases(type, perf_evsel__hw_cache,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100342 PERF_COUNT_HW_CACHE_MAX);
Ingo Molnar8326f442009-06-05 20:22:46 +0200343 if (cache_type == -1)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100344 return -EINVAL;
Ingo Molnar8326f442009-06-05 20:22:46 +0200345
Jiri Olsa89812fc2012-03-15 20:09:15 +0100346 n = snprintf(name, MAX_NAME_LEN, "%s", type);
347
348 for (i = 0; (i < 2) && (op_result[i]); i++) {
349 char *str = op_result[i];
350
Jiri Olsa275ef382012-09-05 19:51:33 +0200351 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
Paul Mackerras61c45982009-07-01 13:04:34 +1000352
353 if (cache_op == -1) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300354 cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100355 PERF_COUNT_HW_CACHE_OP_MAX);
Paul Mackerras61c45982009-07-01 13:04:34 +1000356 if (cache_op >= 0) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300357 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
Jiri Olsa89812fc2012-03-15 20:09:15 +0100358 return -EINVAL;
Paul Mackerras61c45982009-07-01 13:04:34 +1000359 continue;
360 }
361 }
362
363 if (cache_result == -1) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300364 cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
365 PERF_COUNT_HW_CACHE_RESULT_MAX);
Paul Mackerras61c45982009-07-01 13:04:34 +1000366 if (cache_result >= 0)
367 continue;
368 }
Paul Mackerras61c45982009-07-01 13:04:34 +1000369 }
370
Ingo Molnar8326f442009-06-05 20:22:46 +0200371 /*
372 * Fall back to reads:
373 */
Ingo Molnar89536452009-06-06 21:04:17 +0200374 if (cache_op == -1)
375 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
Ingo Molnar8326f442009-06-05 20:22:46 +0200376
Ingo Molnar8326f442009-06-05 20:22:46 +0200377 /*
378 * Fall back to accesses:
379 */
380 if (cache_result == -1)
381 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
382
Jiri Olsa89812fc2012-03-15 20:09:15 +0100383 memset(&attr, 0, sizeof(attr));
384 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
385 attr.type = PERF_TYPE_HW_CACHE;
Jiri Olsa930a2e22015-07-29 05:42:10 -0400386 return add_event(list, idx, &attr, name, NULL);
Ingo Molnar8326f442009-06-05 20:22:46 +0200387}
388
David Ahernc5cd8ac2013-07-02 13:27:25 -0600389static int add_tracepoint(struct list_head *list, int *idx,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100390 char *sys_name, char *evt_name)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200391{
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300392 struct perf_evsel *evsel;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200393
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300394 evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
David Ahernc5cd8ac2013-07-02 13:27:25 -0600395 if (!evsel)
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300396 return -ENOMEM;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200397
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300398 list_add_tail(&evsel->node, list);
David Ahernc5cd8ac2013-07-02 13:27:25 -0600399
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300400 return 0;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200401}
402
David Ahernc5cd8ac2013-07-02 13:27:25 -0600403static int add_tracepoint_multi_event(struct list_head *list, int *idx,
Jiri Olsaf35488f2012-12-17 14:08:37 +0100404 char *sys_name, char *evt_name)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200405{
406 char evt_path[MAXPATHLEN];
407 struct dirent *evt_ent;
408 DIR *evt_dir;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100409 int ret = 0;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200410
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200411 snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200412 evt_dir = opendir(evt_path);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200413 if (!evt_dir) {
414 perror("Can't open event dir");
Jiri Olsa89812fc2012-03-15 20:09:15 +0100415 return -1;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200416 }
417
Jiri Olsa89812fc2012-03-15 20:09:15 +0100418 while (!ret && (evt_ent = readdir(evt_dir))) {
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200419 if (!strcmp(evt_ent->d_name, ".")
420 || !strcmp(evt_ent->d_name, "..")
421 || !strcmp(evt_ent->d_name, "enable")
422 || !strcmp(evt_ent->d_name, "filter"))
423 continue;
424
Jiri Olsa89812fc2012-03-15 20:09:15 +0100425 if (!strglobmatch(evt_ent->d_name, evt_name))
Masami Hiramatsufb1d2ed2010-01-05 17:47:17 -0500426 continue;
427
Jiri Olsa89812fc2012-03-15 20:09:15 +0100428 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200429 }
430
Jiri Olsa0bd3f082012-12-17 14:08:36 +0100431 closedir(evt_dir);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100432 return ret;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200433}
434
David Ahernc5cd8ac2013-07-02 13:27:25 -0600435static int add_tracepoint_event(struct list_head *list, int *idx,
Jiri Olsaf35488f2012-12-17 14:08:37 +0100436 char *sys_name, char *evt_name)
437{
438 return strpbrk(evt_name, "*?") ?
439 add_tracepoint_multi_event(list, idx, sys_name, evt_name) :
440 add_tracepoint(list, idx, sys_name, evt_name);
441}
442
David Ahernc5cd8ac2013-07-02 13:27:25 -0600443static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
Jiri Olsaf35488f2012-12-17 14:08:37 +0100444 char *sys_name, char *evt_name)
445{
446 struct dirent *events_ent;
447 DIR *events_dir;
448 int ret = 0;
449
450 events_dir = opendir(tracing_events_path);
451 if (!events_dir) {
452 perror("Can't open event dir");
453 return -1;
454 }
455
456 while (!ret && (events_ent = readdir(events_dir))) {
457 if (!strcmp(events_ent->d_name, ".")
458 || !strcmp(events_ent->d_name, "..")
459 || !strcmp(events_ent->d_name, "enable")
460 || !strcmp(events_ent->d_name, "header_event")
461 || !strcmp(events_ent->d_name, "header_page"))
462 continue;
463
464 if (!strglobmatch(events_ent->d_name, sys_name))
465 continue;
466
467 ret = add_tracepoint_event(list, idx, events_ent->d_name,
468 evt_name);
469 }
470
471 closedir(events_dir);
472 return ret;
473}
474
David Ahernc5cd8ac2013-07-02 13:27:25 -0600475int parse_events_add_tracepoint(struct list_head *list, int *idx,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100476 char *sys, char *event)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400477{
Jiri Olsaf35488f2012-12-17 14:08:37 +0100478 if (strpbrk(sys, "*?"))
479 return add_tracepoint_multi_sys(list, idx, sys, event);
480 else
481 return add_tracepoint_event(list, idx, sys, event);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400482}
483
Jiri Olsa89812fc2012-03-15 20:09:15 +0100484static int
485parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100486{
487 int i;
488
489 for (i = 0; i < 3; i++) {
Jiri Olsa89812fc2012-03-15 20:09:15 +0100490 if (!type || !type[i])
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100491 break;
492
Jiri Olsa75827322012-06-29 09:22:54 +0200493#define CHECK_SET_TYPE(bit) \
494do { \
495 if (attr->bp_type & bit) \
496 return -EINVAL; \
497 else \
498 attr->bp_type |= bit; \
499} while (0)
500
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100501 switch (type[i]) {
502 case 'r':
Jiri Olsa75827322012-06-29 09:22:54 +0200503 CHECK_SET_TYPE(HW_BREAKPOINT_R);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100504 break;
505 case 'w':
Jiri Olsa75827322012-06-29 09:22:54 +0200506 CHECK_SET_TYPE(HW_BREAKPOINT_W);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100507 break;
508 case 'x':
Jiri Olsa75827322012-06-29 09:22:54 +0200509 CHECK_SET_TYPE(HW_BREAKPOINT_X);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100510 break;
511 default:
Jiri Olsa89812fc2012-03-15 20:09:15 +0100512 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100513 }
514 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100515
Jiri Olsa75827322012-06-29 09:22:54 +0200516#undef CHECK_SET_TYPE
517
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100518 if (!attr->bp_type) /* Default */
519 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
520
Jiri Olsa89812fc2012-03-15 20:09:15 +0100521 return 0;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100522}
523
David Ahernc5cd8ac2013-07-02 13:27:25 -0600524int parse_events_add_breakpoint(struct list_head *list, int *idx,
Jacob Shin3741eb92014-05-29 17:26:51 +0200525 void *ptr, char *type, u64 len)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100526{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100527 struct perf_event_attr attr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100528
Jiri Olsa89812fc2012-03-15 20:09:15 +0100529 memset(&attr, 0, sizeof(attr));
Jiri Olsa9fafd982012-03-20 19:15:39 +0100530 attr.bp_addr = (unsigned long) ptr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100531
Jiri Olsa89812fc2012-03-15 20:09:15 +0100532 if (parse_breakpoint_type(type, &attr))
533 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100534
Jacob Shin3741eb92014-05-29 17:26:51 +0200535 /* Provide some defaults if len is not specified */
536 if (!len) {
537 if (attr.bp_type == HW_BREAKPOINT_X)
538 len = sizeof(long);
539 else
540 len = HW_BREAKPOINT_LEN_4;
541 }
542
543 attr.bp_len = len;
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200544
Jiri Olsa89812fc2012-03-15 20:09:15 +0100545 attr.type = PERF_TYPE_BREAKPOINT;
Jovi Zhang4a841d62012-07-15 03:03:10 +0800546 attr.sample_period = 1;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100547
Jiri Olsa930a2e22015-07-29 05:42:10 -0400548 return add_event(list, idx, &attr, NULL, NULL);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100549}
550
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200551static int check_type_val(struct parse_events_term *term,
552 struct parse_events_error *err,
553 int type)
Jiri Olsa8f707d82012-03-15 20:09:16 +0100554{
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200555 if (type == term->type_val)
556 return 0;
557
558 if (err) {
559 err->idx = term->err_val;
560 if (type == PARSE_EVENTS__TERM_TYPE_NUM)
561 err->str = strdup("expected numeric value");
562 else
563 err->str = strdup("expected string value");
564 }
565 return -EINVAL;
566}
567
568static int config_term(struct perf_event_attr *attr,
569 struct parse_events_term *term,
570 struct parse_events_error *err)
571{
572#define CHECK_TYPE_VAL(type) \
573do { \
574 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
575 return -EINVAL; \
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200576} while (0)
577
578 switch (term->type_term) {
Jiri Olsac056ba6a2015-04-22 21:10:18 +0200579 case PARSE_EVENTS__TERM_TYPE_USER:
580 /*
581 * Always succeed for sysfs terms, as we dont know
582 * at this point what type they need to have.
583 */
584 return 0;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100585 case PARSE_EVENTS__TERM_TYPE_CONFIG:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200586 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100587 attr->config = term->val.num;
588 break;
589 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200590 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100591 attr->config1 = term->val.num;
592 break;
593 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200594 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100595 attr->config2 = term->val.num;
596 break;
597 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200598 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100599 break;
Namhyung Kim09af2a52015-08-09 15:45:23 +0900600 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
601 CHECK_TYPE_VAL(NUM);
602 break;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100603 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
604 /*
605 * TODO uncomment when the field is available
606 * attr->branch_sample_type = term->val.num;
607 */
608 break;
Kan Liang32067712015-08-04 04:30:19 -0400609 case PARSE_EVENTS__TERM_TYPE_TIME:
610 CHECK_TYPE_VAL(NUM);
611 if (term->val.num > 1) {
612 err->str = strdup("expected 0 or 1");
613 err->idx = term->err_val;
614 return -EINVAL;
615 }
616 break;
Kan Liangd457c962015-08-11 06:30:47 -0400617 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
618 CHECK_TYPE_VAL(STR);
619 break;
620 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
621 CHECK_TYPE_VAL(NUM);
622 break;
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200623 case PARSE_EVENTS__TERM_TYPE_NAME:
624 CHECK_TYPE_VAL(STR);
625 break;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100626 default:
627 return -EINVAL;
628 }
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200629
Jiri Olsa8f707d82012-03-15 20:09:16 +0100630 return 0;
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200631#undef CHECK_TYPE_VAL
Jiri Olsa8f707d82012-03-15 20:09:16 +0100632}
633
634static int config_attr(struct perf_event_attr *attr,
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200635 struct list_head *head,
636 struct parse_events_error *err)
Jiri Olsa8f707d82012-03-15 20:09:16 +0100637{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300638 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100639
640 list_for_each_entry(term, head, list)
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200641 if (config_term(attr, term, err))
Jiri Olsa8f707d82012-03-15 20:09:16 +0100642 return -EINVAL;
643
644 return 0;
645}
646
Jiri Olsa930a2e22015-07-29 05:42:10 -0400647static int get_config_terms(struct list_head *head_config,
648 struct list_head *head_terms __maybe_unused)
649{
650#define ADD_CONFIG_TERM(__type, __name, __val) \
651do { \
652 struct perf_evsel_config_term *__t; \
653 \
654 __t = zalloc(sizeof(*__t)); \
655 if (!__t) \
656 return -ENOMEM; \
657 \
658 INIT_LIST_HEAD(&__t->list); \
659 __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \
660 __t->val.__name = __val; \
661 list_add_tail(&__t->list, head_terms); \
662} while (0)
663
664 struct parse_events_term *term;
665
666 list_for_each_entry(term, head_config, list) {
667 switch (term->type_term) {
Jiri Olsaee4c7582015-07-29 05:42:11 -0400668 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
669 ADD_CONFIG_TERM(PERIOD, period, term->val.num);
Kan Liang32067712015-08-04 04:30:19 -0400670 break;
Namhyung Kim09af2a52015-08-09 15:45:23 +0900671 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
672 ADD_CONFIG_TERM(FREQ, freq, term->val.num);
673 break;
Kan Liang32067712015-08-04 04:30:19 -0400674 case PARSE_EVENTS__TERM_TYPE_TIME:
675 ADD_CONFIG_TERM(TIME, time, term->val.num);
676 break;
Kan Liangd457c962015-08-11 06:30:47 -0400677 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
678 ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
679 break;
680 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
681 ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
682 break;
Jiri Olsa930a2e22015-07-29 05:42:10 -0400683 default:
684 break;
685 }
686 }
687#undef ADD_EVSEL_CONFIG
688 return 0;
689}
690
Jiri Olsa87d650b2015-04-22 21:10:24 +0200691int parse_events_add_numeric(struct parse_events_evlist *data,
692 struct list_head *list,
Robert Richterb527bab2012-08-07 19:43:13 +0200693 u32 type, u64 config,
Jiri Olsa8f707d82012-03-15 20:09:16 +0100694 struct list_head *head_config)
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530695{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100696 struct perf_event_attr attr;
Jiri Olsa930a2e22015-07-29 05:42:10 -0400697 LIST_HEAD(config_terms);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530698
Jiri Olsa89812fc2012-03-15 20:09:15 +0100699 memset(&attr, 0, sizeof(attr));
700 attr.type = type;
701 attr.config = config;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100702
Jiri Olsa930a2e22015-07-29 05:42:10 -0400703 if (head_config) {
704 if (config_attr(&attr, head_config, data->error))
705 return -EINVAL;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100706
Jiri Olsa930a2e22015-07-29 05:42:10 -0400707 if (get_config_terms(head_config, &config_terms))
708 return -ENOMEM;
709 }
710
711 return add_event(list, &data->idx, &attr, NULL, &config_terms);
Paul Mackerras61c45982009-07-01 13:04:34 +1000712}
713
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300714static int parse_events__is_name_term(struct parse_events_term *term)
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200715{
716 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
717}
718
Arnaldo Carvalho de Melo9db17632012-06-12 13:45:00 -0300719static char *pmu_event_name(struct list_head *head_terms)
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200720{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300721 struct parse_events_term *term;
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200722
723 list_for_each_entry(term, head_terms, list)
724 if (parse_events__is_name_term(term))
725 return term->val.str;
726
Arnaldo Carvalho de Melo9db17632012-06-12 13:45:00 -0300727 return NULL;
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200728}
729
Jiri Olsa36adec82015-04-22 21:10:19 +0200730int parse_events_add_pmu(struct parse_events_evlist *data,
731 struct list_head *list, char *name,
732 struct list_head *head_config)
Jiri Olsa5f537a22012-03-15 20:09:18 +0100733{
734 struct perf_event_attr attr;
Matt Fleming46441bd2014-09-24 15:04:06 +0100735 struct perf_pmu_info info;
Jiri Olsa5f537a22012-03-15 20:09:18 +0100736 struct perf_pmu *pmu;
Stephane Eranian410136f2013-11-12 17:58:49 +0100737 struct perf_evsel *evsel;
Jiri Olsa930a2e22015-07-29 05:42:10 -0400738 LIST_HEAD(config_terms);
Jiri Olsa5f537a22012-03-15 20:09:18 +0100739
740 pmu = perf_pmu__find(name);
741 if (!pmu)
742 return -EINVAL;
743
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300744 if (pmu->default_config) {
745 memcpy(&attr, pmu->default_config,
746 sizeof(struct perf_event_attr));
747 } else {
748 memset(&attr, 0, sizeof(attr));
749 }
Jiri Olsa5f537a22012-03-15 20:09:18 +0100750
Adrian Hunterad962272014-08-15 22:08:40 +0300751 if (!head_config) {
752 attr.type = pmu->type;
Jiri Olsa930a2e22015-07-29 05:42:10 -0400753 evsel = __add_event(list, &data->idx, &attr, NULL, pmu->cpus, NULL);
Adrian Hunterad962272014-08-15 22:08:40 +0300754 return evsel ? 0 : -ENOMEM;
755 }
756
Matt Fleming46441bd2014-09-24 15:04:06 +0100757 if (perf_pmu__check_alias(pmu, head_config, &info))
Zheng Yana6146d52012-06-15 14:31:41 +0800758 return -EINVAL;
759
Jiri Olsa5f537a22012-03-15 20:09:18 +0100760 /*
761 * Configure hardcoded terms first, no need to check
762 * return value when called with fail == 0 ;)
763 */
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200764 if (config_attr(&attr, head_config, data->error))
Jiri Olsac056ba6a2015-04-22 21:10:18 +0200765 return -EINVAL;
Jiri Olsa5f537a22012-03-15 20:09:18 +0100766
Jiri Olsa930a2e22015-07-29 05:42:10 -0400767 if (get_config_terms(head_config, &config_terms))
768 return -ENOMEM;
769
Jiri Olsae64b0202015-04-22 21:10:21 +0200770 if (perf_pmu__config(pmu, &attr, head_config, data->error))
Jiri Olsa5f537a22012-03-15 20:09:18 +0100771 return -EINVAL;
772
Jiri Olsa36adec82015-04-22 21:10:19 +0200773 evsel = __add_event(list, &data->idx, &attr,
Jiri Olsa930a2e22015-07-29 05:42:10 -0400774 pmu_event_name(head_config), pmu->cpus,
775 &config_terms);
Stephane Eranian410136f2013-11-12 17:58:49 +0100776 if (evsel) {
Matt Fleming46441bd2014-09-24 15:04:06 +0100777 evsel->unit = info.unit;
778 evsel->scale = info.scale;
Matt Fleming044330c2014-11-21 10:31:12 +0100779 evsel->per_pkg = info.per_pkg;
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100780 evsel->snapshot = info.snapshot;
Stephane Eranian410136f2013-11-12 17:58:49 +0100781 }
782
783 return evsel ? 0 : -ENOMEM;
Jiri Olsa5f537a22012-03-15 20:09:18 +0100784}
785
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200786int parse_events__modifier_group(struct list_head *list,
787 char *event_mod)
Jiri Olsa89efb022012-08-08 12:14:14 +0200788{
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200789 return parse_events__modifier_event(list, event_mod, true);
790}
791
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300792void parse_events__set_leader(char *name, struct list_head *list)
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200793{
794 struct perf_evsel *leader;
795
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300796 __perf_evlist__set_leader(list);
797 leader = list_entry(list->next, struct perf_evsel, node);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200798 leader->group_name = name ? strdup(name) : NULL;
Jiri Olsa89efb022012-08-08 12:14:14 +0200799}
800
David Ahernc5cd8ac2013-07-02 13:27:25 -0600801/* list_event is assumed to point to malloc'ed memory */
Jiri Olsa5d7be902012-03-20 19:15:40 +0100802void parse_events_update_lists(struct list_head *list_event,
803 struct list_head *list_all)
804{
805 /*
806 * Called for single event definition. Update the
Jiri Olsa89efb022012-08-08 12:14:14 +0200807 * 'all event' list, and reinit the 'single event'
Jiri Olsa5d7be902012-03-20 19:15:40 +0100808 * list, for next event definition.
809 */
810 list_splice_tail(list_event, list_all);
Jiri Olsab847cbd2012-05-21 09:12:51 +0200811 free(list_event);
Jiri Olsa5d7be902012-03-20 19:15:40 +0100812}
813
Jiri Olsaf5b11352012-08-08 12:21:54 +0200814struct event_modifier {
815 int eu;
816 int ek;
817 int eh;
818 int eH;
819 int eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +0200820 int eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +0200821 int precise;
822 int exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +0200823 int sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +1000824 int pinned;
Jiri Olsaf5b11352012-08-08 12:21:54 +0200825};
Paul Mackerras61c45982009-07-01 13:04:34 +1000826
Jiri Olsaf5b11352012-08-08 12:21:54 +0200827static int get_event_modifier(struct event_modifier *mod, char *str,
828 struct perf_evsel *evsel)
829{
830 int eu = evsel ? evsel->attr.exclude_user : 0;
831 int ek = evsel ? evsel->attr.exclude_kernel : 0;
832 int eh = evsel ? evsel->attr.exclude_hv : 0;
833 int eH = evsel ? evsel->attr.exclude_host : 0;
834 int eG = evsel ? evsel->attr.exclude_guest : 0;
Jiri Olsaa1e12da2015-04-07 23:25:14 +0200835 int eI = evsel ? evsel->attr.exclude_idle : 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +0200836 int precise = evsel ? evsel->attr.precise_ip : 0;
Jiri Olsa3c176312012-10-10 17:39:03 +0200837 int sample_read = 0;
Michael Ellermane9a7c412013-08-06 23:28:05 +1000838 int pinned = evsel ? evsel->attr.pinned : 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +0200839
840 int exclude = eu | ek | eh;
841 int exclude_GH = evsel ? evsel->exclude_GH : 0;
842
Jiri Olsaf5b11352012-08-08 12:21:54 +0200843 memset(mod, 0, sizeof(*mod));
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200844
Paul Mackerras61c45982009-07-01 13:04:34 +1000845 while (*str) {
Peter Zijlstraab608342010-04-08 23:03:20 +0200846 if (*str == 'u') {
847 if (!exclude)
848 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000849 eu = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200850 } else if (*str == 'k') {
851 if (!exclude)
852 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000853 ek = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200854 } else if (*str == 'h') {
855 if (!exclude)
856 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000857 eh = 0;
Joerg Roedel99320cc2012-01-04 17:54:19 +0100858 } else if (*str == 'G') {
859 if (!exclude_GH)
860 exclude_GH = eG = eH = 1;
861 eG = 0;
862 } else if (*str == 'H') {
863 if (!exclude_GH)
864 exclude_GH = eG = eH = 1;
865 eH = 0;
Jiri Olsaa1e12da2015-04-07 23:25:14 +0200866 } else if (*str == 'I') {
867 eI = 1;
Peter Zijlstraab608342010-04-08 23:03:20 +0200868 } else if (*str == 'p') {
869 precise++;
David Ahern13427982012-09-13 14:59:13 -0600870 /* use of precise requires exclude_guest */
871 if (!exclude_GH)
872 eG = 1;
Jiri Olsa3c176312012-10-10 17:39:03 +0200873 } else if (*str == 'S') {
874 sample_read = 1;
Michael Ellermane9a7c412013-08-06 23:28:05 +1000875 } else if (*str == 'D') {
876 pinned = 1;
Peter Zijlstraab608342010-04-08 23:03:20 +0200877 } else
Paul Mackerras61c45982009-07-01 13:04:34 +1000878 break;
Peter Zijlstraab608342010-04-08 23:03:20 +0200879
Paul Mackerras61c45982009-07-01 13:04:34 +1000880 ++str;
881 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200882
Jiri Olsa89812fc2012-03-15 20:09:15 +0100883 /*
884 * precise ip:
885 *
886 * 0 - SAMPLE_IP can have arbitrary skid
887 * 1 - SAMPLE_IP must have constant skid
888 * 2 - SAMPLE_IP requested to have 0 skid
889 * 3 - SAMPLE_IP must have 0 skid
890 *
891 * See also PERF_RECORD_MISC_EXACT_IP
892 */
893 if (precise > 3)
894 return -EINVAL;
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200895
Jiri Olsaf5b11352012-08-08 12:21:54 +0200896 mod->eu = eu;
897 mod->ek = ek;
898 mod->eh = eh;
899 mod->eH = eH;
900 mod->eG = eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +0200901 mod->eI = eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +0200902 mod->precise = precise;
903 mod->exclude_GH = exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +0200904 mod->sample_read = sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +1000905 mod->pinned = pinned;
906
Jiri Olsaf5b11352012-08-08 12:21:54 +0200907 return 0;
908}
909
Jiri Olsa534123f2012-11-13 15:32:58 +0100910/*
911 * Basic modifier sanity check to validate it contains only one
912 * instance of any modifier (apart from 'p') present.
913 */
914static int check_modifier(char *str)
915{
916 char *p = str;
917
918 /* The sizeof includes 0 byte as well. */
Jiri Olsaa1e12da2015-04-07 23:25:14 +0200919 if (strlen(str) > (sizeof("ukhGHpppSDI") - 1))
Jiri Olsa534123f2012-11-13 15:32:58 +0100920 return -1;
921
922 while (*p) {
923 if (*p != 'p' && strchr(p + 1, *p))
924 return -1;
925 p++;
926 }
927
928 return 0;
929}
930
Jiri Olsaf5b11352012-08-08 12:21:54 +0200931int parse_events__modifier_event(struct list_head *list, char *str, bool add)
932{
933 struct perf_evsel *evsel;
934 struct event_modifier mod;
935
936 if (str == NULL)
937 return 0;
938
Jiri Olsa534123f2012-11-13 15:32:58 +0100939 if (check_modifier(str))
940 return -EINVAL;
941
Jiri Olsaf5b11352012-08-08 12:21:54 +0200942 if (!add && get_event_modifier(&mod, str, NULL))
943 return -EINVAL;
944
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300945 __evlist__for_each(list, evsel) {
Jiri Olsaf5b11352012-08-08 12:21:54 +0200946 if (add && get_event_modifier(&mod, str, evsel))
947 return -EINVAL;
948
949 evsel->attr.exclude_user = mod.eu;
950 evsel->attr.exclude_kernel = mod.ek;
951 evsel->attr.exclude_hv = mod.eh;
952 evsel->attr.precise_ip = mod.precise;
953 evsel->attr.exclude_host = mod.eH;
954 evsel->attr.exclude_guest = mod.eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +0200955 evsel->attr.exclude_idle = mod.eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +0200956 evsel->exclude_GH = mod.exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +0200957 evsel->sample_read = mod.sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +1000958
959 if (perf_evsel__is_group_leader(evsel))
960 evsel->attr.pinned = mod.pinned;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100961 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200962
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530963 return 0;
964}
965
Robert Richterac2ba9f2012-08-16 21:10:21 +0200966int parse_events_name(struct list_head *list, char *name)
967{
968 struct perf_evsel *evsel;
969
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300970 __evlist__for_each(list, evsel) {
Robert Richterac2ba9f2012-08-16 21:10:21 +0200971 if (!evsel->name)
972 evsel->name = strdup(name);
973 }
974
975 return 0;
976}
977
Kan Liangdcb4e102014-10-07 11:08:50 -0400978static int
979comp_pmu(const void *p1, const void *p2)
980{
981 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
982 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
983
984 return strcmp(pmu1->symbol, pmu2->symbol);
985}
986
987static void perf_pmu__parse_cleanup(void)
988{
989 if (perf_pmu_events_list_num > 0) {
990 struct perf_pmu_event_symbol *p;
991 int i;
992
993 for (i = 0; i < perf_pmu_events_list_num; i++) {
994 p = perf_pmu_events_list + i;
995 free(p->symbol);
996 }
997 free(perf_pmu_events_list);
998 perf_pmu_events_list = NULL;
999 perf_pmu_events_list_num = 0;
1000 }
1001}
1002
1003#define SET_SYMBOL(str, stype) \
1004do { \
1005 p->symbol = str; \
1006 if (!p->symbol) \
1007 goto err; \
1008 p->type = stype; \
1009} while (0)
1010
1011/*
1012 * Read the pmu events list from sysfs
1013 * Save it into perf_pmu_events_list
1014 */
1015static void perf_pmu__parse_init(void)
1016{
1017
1018 struct perf_pmu *pmu = NULL;
1019 struct perf_pmu_alias *alias;
1020 int len = 0;
1021
1022 pmu = perf_pmu__find("cpu");
1023 if ((pmu == NULL) || list_empty(&pmu->aliases)) {
1024 perf_pmu_events_list_num = -1;
1025 return;
1026 }
1027 list_for_each_entry(alias, &pmu->aliases, list) {
1028 if (strchr(alias->name, '-'))
1029 len++;
1030 len++;
1031 }
1032 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1033 if (!perf_pmu_events_list)
1034 return;
1035 perf_pmu_events_list_num = len;
1036
1037 len = 0;
1038 list_for_each_entry(alias, &pmu->aliases, list) {
1039 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1040 char *tmp = strchr(alias->name, '-');
1041
1042 if (tmp != NULL) {
1043 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1044 PMU_EVENT_SYMBOL_PREFIX);
1045 p++;
1046 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1047 len += 2;
1048 } else {
1049 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1050 len++;
1051 }
1052 }
1053 qsort(perf_pmu_events_list, len,
1054 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1055
1056 return;
1057err:
1058 perf_pmu__parse_cleanup();
1059}
1060
1061enum perf_pmu_event_symbol_type
1062perf_pmu__parse_check(const char *name)
1063{
1064 struct perf_pmu_event_symbol p, *r;
1065
1066 /* scan kernel pmu events from sysfs if needed */
1067 if (perf_pmu_events_list_num == 0)
1068 perf_pmu__parse_init();
1069 /*
1070 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1071 * cpu-cycles has been handled by hardcode.
1072 * So it must be cpu// events, not kernel pmu event.
1073 */
1074 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1075 return PMU_EVENT_SYMBOL_ERR;
1076
1077 p.symbol = strdup(name);
1078 r = bsearch(&p, perf_pmu_events_list,
1079 (size_t) perf_pmu_events_list_num,
1080 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1081 free(p.symbol);
1082 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1083}
1084
Jiri Olsa90e2b222012-06-15 14:31:40 +08001085static int parse_events__scanner(const char *str, void *data, int start_token)
Zheng Yanac20de6f2012-06-15 14:31:39 +08001086{
1087 YY_BUFFER_STATE buffer;
1088 void *scanner;
1089 int ret;
1090
Jiri Olsa90e2b222012-06-15 14:31:40 +08001091 ret = parse_events_lex_init_extra(start_token, &scanner);
Zheng Yanac20de6f2012-06-15 14:31:39 +08001092 if (ret)
1093 return ret;
1094
1095 buffer = parse_events__scan_string(str, scanner);
1096
1097#ifdef PARSER_DEBUG
1098 parse_events_debug = 1;
1099#endif
1100 ret = parse_events_parse(data, scanner);
1101
1102 parse_events__flush_buffer(buffer, scanner);
1103 parse_events__delete_buffer(buffer, scanner);
1104 parse_events_lex_destroy(scanner);
1105 return ret;
1106}
1107
Jiri Olsa90e2b222012-06-15 14:31:40 +08001108/*
1109 * parse event config string, return a list of event terms.
1110 */
1111int parse_events_terms(struct list_head *terms, const char *str)
1112{
Arnaldo Carvalho de Melo23b63392013-01-18 16:56:57 -03001113 struct parse_events_terms data = {
Jiri Olsa90e2b222012-06-15 14:31:40 +08001114 .terms = NULL,
1115 };
1116 int ret;
1117
1118 ret = parse_events__scanner(str, &data, PE_START_TERMS);
1119 if (!ret) {
1120 list_splice(data.terms, terms);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001121 zfree(&data.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001122 return 0;
1123 }
1124
Adrian Hunterb2c34fd2013-07-04 16:20:23 +03001125 if (data.terms)
1126 parse_events__free_terms(data.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001127 return ret;
1128}
1129
Jiri Olsab39b8392015-04-22 21:10:16 +02001130int parse_events(struct perf_evlist *evlist, const char *str,
1131 struct parse_events_error *err)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001132{
Arnaldo Carvalho de Melo23b63392013-01-18 16:56:57 -03001133 struct parse_events_evlist data = {
Jiri Olsab39b8392015-04-22 21:10:16 +02001134 .list = LIST_HEAD_INIT(data.list),
1135 .idx = evlist->nr_entries,
1136 .error = err,
Jiri Olsa46010ab2012-06-15 14:31:38 +08001137 };
Jiri Olsa46010ab2012-06-15 14:31:38 +08001138 int ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001139
Jiri Olsa90e2b222012-06-15 14:31:40 +08001140 ret = parse_events__scanner(str, &data, PE_START_EVENTS);
Kan Liangdcb4e102014-10-07 11:08:50 -04001141 perf_pmu__parse_cleanup();
Jiri Olsa89812fc2012-03-15 20:09:15 +01001142 if (!ret) {
Wang Nan15bfd2c2015-07-10 07:36:09 +00001143 struct perf_evsel *last;
1144
Adrian Hunterf114d6e2015-09-08 10:58:53 +03001145 perf_evlist__splice_list_tail(evlist, &data.list);
Namhyung Kim97f63e42013-01-22 18:09:29 +09001146 evlist->nr_groups += data.nr_groups;
Wang Nan15bfd2c2015-07-10 07:36:09 +00001147 last = perf_evlist__last(evlist);
1148 last->cmdline_group_boundary = true;
1149
Jiri Olsa89812fc2012-03-15 20:09:15 +01001150 return 0;
1151 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02001152
Jiri Olsa5d7be902012-03-20 19:15:40 +01001153 /*
1154 * There are 2 users - builtin-record and builtin-test objects.
1155 * Both call perf_evlist__delete in case of error, so we dont
1156 * need to bother.
1157 */
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02001158 return ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001159}
1160
Jiri Olsab39b8392015-04-22 21:10:16 +02001161#define MAX_WIDTH 1000
1162static int get_term_width(void)
1163{
1164 struct winsize ws;
1165
1166 get_term_dimensions(&ws);
1167 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1168}
1169
1170static void parse_events_print_error(struct parse_events_error *err,
1171 const char *event)
1172{
1173 const char *str = "invalid or unsupported event: ";
1174 char _buf[MAX_WIDTH];
1175 char *buf = (char *) event;
1176 int idx = 0;
1177
1178 if (err->str) {
1179 /* -2 for extra '' in the final fprintf */
1180 int width = get_term_width() - 2;
1181 int len_event = strlen(event);
1182 int len_str, max_len, cut = 0;
1183
1184 /*
1185 * Maximum error index indent, we will cut
1186 * the event string if it's bigger.
1187 */
Adrian Hunter141b2d32015-07-17 19:33:51 +03001188 int max_err_idx = 13;
Jiri Olsab39b8392015-04-22 21:10:16 +02001189
1190 /*
1191 * Let's be specific with the message when
1192 * we have the precise error.
1193 */
1194 str = "event syntax error: ";
1195 len_str = strlen(str);
1196 max_len = width - len_str;
1197
1198 buf = _buf;
1199
1200 /* We're cutting from the beggining. */
1201 if (err->idx > max_err_idx)
1202 cut = err->idx - max_err_idx;
1203
1204 strncpy(buf, event + cut, max_len);
1205
1206 /* Mark cut parts with '..' on both sides. */
1207 if (cut)
1208 buf[0] = buf[1] = '.';
1209
1210 if ((len_event - cut) > max_len) {
1211 buf[max_len - 1] = buf[max_len - 2] = '.';
1212 buf[max_len] = 0;
1213 }
1214
1215 idx = len_str + err->idx - cut;
1216 }
1217
1218 fprintf(stderr, "%s'%s'\n", str, buf);
1219 if (idx) {
1220 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
1221 if (err->help)
1222 fprintf(stderr, "\n%s\n", err->help);
1223 free(err->str);
1224 free(err->help);
1225 }
1226
1227 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
1228}
1229
1230#undef MAX_WIDTH
1231
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001232int parse_events_option(const struct option *opt, const char *str,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001233 int unset __maybe_unused)
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001234{
1235 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
Jiri Olsab39b8392015-04-22 21:10:16 +02001236 struct parse_events_error err = { .idx = 0, };
1237 int ret = parse_events(evlist, str, &err);
Andi Kleen9175ce12012-10-26 13:30:06 -07001238
Jiri Olsab39b8392015-04-22 21:10:16 +02001239 if (ret)
1240 parse_events_print_error(&err, str);
1241
Andi Kleen9175ce12012-10-26 13:30:06 -07001242 return ret;
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001243}
1244
Wang Nan4ba1faa2015-07-10 07:36:10 +00001245static int
1246foreach_evsel_in_last_glob(struct perf_evlist *evlist,
1247 int (*func)(struct perf_evsel *evsel,
1248 const void *arg),
1249 const void *arg)
Li Zefanc171b552009-10-15 11:22:07 +08001250{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001251 struct perf_evsel *last = NULL;
Wang Nan4ba1faa2015-07-10 07:36:10 +00001252 int err;
Li Zefanc171b552009-10-15 11:22:07 +08001253
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001254 if (evlist->nr_entries > 0)
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001255 last = perf_evlist__last(evlist);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001256
Wang Nan15bfd2c2015-07-10 07:36:09 +00001257 do {
Wang Nan4ba1faa2015-07-10 07:36:10 +00001258 err = (*func)(last, arg);
1259 if (err)
Wang Nan15bfd2c2015-07-10 07:36:09 +00001260 return -1;
Wang Nan4ba1faa2015-07-10 07:36:10 +00001261 if (!last)
1262 return 0;
Wang Nan15bfd2c2015-07-10 07:36:09 +00001263
1264 if (last->node.prev == &evlist->entries)
1265 return 0;
1266 last = list_entry(last->node.prev, struct perf_evsel, node);
1267 } while (!last->cmdline_group_boundary);
Li Zefanc171b552009-10-15 11:22:07 +08001268
1269 return 0;
1270}
1271
Wang Nan4ba1faa2015-07-10 07:36:10 +00001272static int set_filter(struct perf_evsel *evsel, const void *arg)
1273{
1274 const char *str = arg;
1275
1276 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
1277 fprintf(stderr,
1278 "--filter option should follow a -e tracepoint option\n");
1279 return -1;
1280 }
1281
1282 if (perf_evsel__append_filter(evsel, "&&", str) < 0) {
1283 fprintf(stderr,
1284 "not enough memory to hold filter string\n");
1285 return -1;
1286 }
1287
1288 return 0;
1289}
1290
1291int parse_filter(const struct option *opt, const char *str,
1292 int unset __maybe_unused)
1293{
1294 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1295
1296 return foreach_evsel_in_last_glob(evlist, set_filter,
1297 (const void *)str);
1298}
1299
1300static int add_exclude_perf_filter(struct perf_evsel *evsel,
1301 const void *arg __maybe_unused)
1302{
1303 char new_filter[64];
1304
1305 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
1306 fprintf(stderr,
1307 "--exclude-perf option should follow a -e tracepoint option\n");
1308 return -1;
1309 }
1310
1311 snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
1312
1313 if (perf_evsel__append_filter(evsel, "&&", new_filter) < 0) {
1314 fprintf(stderr,
1315 "not enough memory to hold filter string\n");
1316 return -1;
1317 }
1318
1319 return 0;
1320}
1321
1322int exclude_perf(const struct option *opt,
1323 const char *arg __maybe_unused,
1324 int unset __maybe_unused)
1325{
1326 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1327
1328 return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
1329 NULL);
1330}
1331
Thomas Gleixner86847b62009-06-06 12:24:17 +02001332static const char * const event_type_descriptors[] = {
Thomas Gleixner86847b62009-06-06 12:24:17 +02001333 "Hardware event",
1334 "Software event",
1335 "Tracepoint event",
1336 "Hardware cache event",
Liming Wang41bdcb22009-12-29 16:37:07 +08001337 "Raw hardware event descriptor",
1338 "Hardware breakpoint",
Thomas Gleixner86847b62009-06-06 12:24:17 +02001339};
1340
Yunlong Songab0e4802015-02-27 18:21:25 +08001341static int cmp_string(const void *a, const void *b)
1342{
1343 const char * const *as = a;
1344 const char * const *bs = b;
1345
1346 return strcmp(*as, *bs);
1347}
1348
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001349/*
Jason Baronf6bdafe2009-07-21 12:20:22 -04001350 * Print the events from <debugfs_mount_point>/tracing/events
1351 */
1352
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001353void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
1354 bool name_only)
Jason Baronf6bdafe2009-07-21 12:20:22 -04001355{
1356 DIR *sys_dir, *evt_dir;
1357 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001358 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +02001359 char dir_path[MAXPATHLEN];
Yunlong Songab0e4802015-02-27 18:21:25 +08001360 char **evt_list = NULL;
1361 unsigned int evt_i = 0, evt_num = 0;
1362 bool evt_num_known = false;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001363
Yunlong Songab0e4802015-02-27 18:21:25 +08001364restart:
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001365 sys_dir = opendir(tracing_events_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -04001366 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +02001367 return;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001368
Yunlong Songab0e4802015-02-27 18:21:25 +08001369 if (evt_num_known) {
1370 evt_list = zalloc(sizeof(char *) * evt_num);
1371 if (!evt_list)
1372 goto out_close_sys_dir;
1373 }
1374
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03001375 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001376 if (subsys_glob != NULL &&
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001377 !strglobmatch(sys_dirent.d_name, subsys_glob))
1378 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02001379
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001380 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Eric Dumazet725b1362009-09-24 15:39:09 +02001381 sys_dirent.d_name);
1382 evt_dir = opendir(dir_path);
1383 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03001384 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02001385
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03001386 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001387 if (event_glob != NULL &&
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001388 !strglobmatch(evt_dirent.d_name, event_glob))
1389 continue;
1390
Yunlong Songab0e4802015-02-27 18:21:25 +08001391 if (!evt_num_known) {
1392 evt_num++;
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001393 continue;
1394 }
1395
Jason Baronf6bdafe2009-07-21 12:20:22 -04001396 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1397 sys_dirent.d_name, evt_dirent.d_name);
Yunlong Songab0e4802015-02-27 18:21:25 +08001398
1399 evt_list[evt_i] = strdup(evt_path);
1400 if (evt_list[evt_i] == NULL)
1401 goto out_close_evt_dir;
1402 evt_i++;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001403 }
1404 closedir(evt_dir);
1405 }
Jason Baronf6bdafe2009-07-21 12:20:22 -04001406 closedir(sys_dir);
Yunlong Songab0e4802015-02-27 18:21:25 +08001407
1408 if (!evt_num_known) {
1409 evt_num_known = true;
1410 goto restart;
1411 }
1412 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1413 evt_i = 0;
1414 while (evt_i < evt_num) {
1415 if (name_only) {
1416 printf("%s ", evt_list[evt_i++]);
1417 continue;
1418 }
1419 printf(" %-50s [%s]\n", evt_list[evt_i++],
1420 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1421 }
1422 if (evt_num)
1423 printf("\n");
1424
1425out_free:
1426 evt_num = evt_i;
1427 for (evt_i = 0; evt_i < evt_num; evt_i++)
1428 zfree(&evt_list[evt_i]);
1429 zfree(&evt_list);
1430 return;
1431
1432out_close_evt_dir:
1433 closedir(evt_dir);
1434out_close_sys_dir:
1435 closedir(sys_dir);
1436
1437 printf("FATAL: not enough memory to print %s\n",
1438 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1439 if (evt_list)
1440 goto out_free;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001441}
1442
1443/*
Thomas Renninger20c457b2011-01-03 17:50:45 +01001444 * Check whether event is in <debugfs_mount_point>/tracing/events
1445 */
1446
1447int is_valid_tracepoint(const char *event_string)
1448{
1449 DIR *sys_dir, *evt_dir;
1450 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
1451 char evt_path[MAXPATHLEN];
1452 char dir_path[MAXPATHLEN];
1453
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001454 sys_dir = opendir(tracing_events_path);
Thomas Renninger20c457b2011-01-03 17:50:45 +01001455 if (!sys_dir)
1456 return 0;
1457
1458 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1459
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001460 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Thomas Renninger20c457b2011-01-03 17:50:45 +01001461 sys_dirent.d_name);
1462 evt_dir = opendir(dir_path);
1463 if (!evt_dir)
1464 continue;
1465
1466 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1467 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1468 sys_dirent.d_name, evt_dirent.d_name);
1469 if (!strcmp(evt_path, event_string)) {
1470 closedir(evt_dir);
1471 closedir(sys_dir);
1472 return 1;
1473 }
1474 }
1475 closedir(evt_dir);
1476 }
1477 closedir(sys_dir);
1478 return 0;
1479}
1480
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001481static bool is_event_supported(u8 type, unsigned config)
1482{
1483 bool ret = true;
Vince Weaver88fee522013-12-30 15:39:45 -05001484 int open_return;
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001485 struct perf_evsel *evsel;
1486 struct perf_event_attr attr = {
1487 .type = type,
1488 .config = config,
1489 .disabled = 1,
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001490 };
1491 struct {
1492 struct thread_map map;
1493 int threads[1];
1494 } tmap = {
1495 .map.nr = 1,
1496 .threads = { 0 },
1497 };
1498
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03001499 evsel = perf_evsel__new(&attr);
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001500 if (evsel) {
Vince Weaver88fee522013-12-30 15:39:45 -05001501 open_return = perf_evsel__open(evsel, NULL, &tmap.map);
1502 ret = open_return >= 0;
1503
1504 if (open_return == -EACCES) {
1505 /*
1506 * This happens if the paranoid value
1507 * /proc/sys/kernel/perf_event_paranoid is set to 2
1508 * Re-run with exclude_kernel set; we don't do that
1509 * by default as some ARM machines do not support it.
1510 *
1511 */
1512 evsel->attr.exclude_kernel = 1;
1513 ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0;
1514 }
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001515 perf_evsel__delete(evsel);
1516 }
1517
1518 return ret;
1519}
1520
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001521int print_hwcache_events(const char *event_glob, bool name_only)
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001522{
Yunlong Songab0e4802015-02-27 18:21:25 +08001523 unsigned int type, op, i, evt_i = 0, evt_num = 0;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03001524 char name[64];
Yunlong Songab0e4802015-02-27 18:21:25 +08001525 char **evt_list = NULL;
1526 bool evt_num_known = false;
1527
1528restart:
1529 if (evt_num_known) {
1530 evt_list = zalloc(sizeof(char *) * evt_num);
1531 if (!evt_list)
1532 goto out_enomem;
1533 }
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001534
1535 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1536 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1537 /* skip invalid cache type */
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03001538 if (!perf_evsel__is_cache_op_valid(type, op))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001539 continue;
1540
1541 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03001542 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
1543 name, sizeof(name));
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001544 if (event_glob != NULL && !strglobmatch(name, event_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001545 continue;
1546
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001547 if (!is_event_supported(PERF_TYPE_HW_CACHE,
1548 type | (op << 8) | (i << 16)))
1549 continue;
1550
Yunlong Songab0e4802015-02-27 18:21:25 +08001551 if (!evt_num_known) {
1552 evt_num++;
1553 continue;
1554 }
1555
1556 evt_list[evt_i] = strdup(name);
1557 if (evt_list[evt_i] == NULL)
1558 goto out_enomem;
1559 evt_i++;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001560 }
1561 }
1562 }
1563
Yunlong Songab0e4802015-02-27 18:21:25 +08001564 if (!evt_num_known) {
1565 evt_num_known = true;
1566 goto restart;
1567 }
1568 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1569 evt_i = 0;
1570 while (evt_i < evt_num) {
1571 if (name_only) {
1572 printf("%s ", evt_list[evt_i++]);
1573 continue;
1574 }
1575 printf(" %-50s [%s]\n", evt_list[evt_i++],
1576 event_type_descriptors[PERF_TYPE_HW_CACHE]);
1577 }
1578 if (evt_num)
Andi Kleendc098b32013-04-20 11:02:29 -07001579 printf("\n");
Yunlong Songab0e4802015-02-27 18:21:25 +08001580
1581out_free:
1582 evt_num = evt_i;
1583 for (evt_i = 0; evt_i < evt_num; evt_i++)
1584 zfree(&evt_list[evt_i]);
1585 zfree(&evt_list);
1586 return evt_num;
1587
1588out_enomem:
1589 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
1590 if (evt_list)
1591 goto out_free;
1592 return evt_num;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001593}
1594
Yunlong Song705750f2015-02-27 18:21:27 +08001595void print_symbol_events(const char *event_glob, unsigned type,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001596 struct event_symbol *syms, unsigned max,
1597 bool name_only)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001598{
Yunlong Songab0e4802015-02-27 18:21:25 +08001599 unsigned int i, evt_i = 0, evt_num = 0;
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001600 char name[MAX_NAME_LEN];
Yunlong Songab0e4802015-02-27 18:21:25 +08001601 char **evt_list = NULL;
1602 bool evt_num_known = false;
1603
1604restart:
1605 if (evt_num_known) {
1606 evt_list = zalloc(sizeof(char *) * evt_num);
1607 if (!evt_list)
1608 goto out_enomem;
1609 syms -= max;
1610 }
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001611
Jiri Olsa1dc127602012-07-04 00:00:44 +02001612 for (i = 0; i < max; i++, syms++) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001613
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001614 if (event_glob != NULL &&
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001615 !(strglobmatch(syms->symbol, event_glob) ||
1616 (syms->alias && strglobmatch(syms->alias, event_glob))))
1617 continue;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001618
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001619 if (!is_event_supported(type, i))
1620 continue;
1621
Yunlong Songab0e4802015-02-27 18:21:25 +08001622 if (!evt_num_known) {
1623 evt_num++;
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001624 continue;
1625 }
1626
Yunlong Songab0e4802015-02-27 18:21:25 +08001627 if (!name_only && strlen(syms->alias))
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001628 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301629 else
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001630 strncpy(name, syms->symbol, MAX_NAME_LEN);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001631
Yunlong Songab0e4802015-02-27 18:21:25 +08001632 evt_list[evt_i] = strdup(name);
1633 if (evt_list[evt_i] == NULL)
1634 goto out_enomem;
1635 evt_i++;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001636 }
1637
Yunlong Songab0e4802015-02-27 18:21:25 +08001638 if (!evt_num_known) {
1639 evt_num_known = true;
1640 goto restart;
1641 }
1642 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1643 evt_i = 0;
1644 while (evt_i < evt_num) {
1645 if (name_only) {
1646 printf("%s ", evt_list[evt_i++]);
1647 continue;
1648 }
1649 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
1650 }
1651 if (evt_num)
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001652 printf("\n");
Yunlong Songab0e4802015-02-27 18:21:25 +08001653
1654out_free:
1655 evt_num = evt_i;
1656 for (evt_i = 0; evt_i < evt_num; evt_i++)
1657 zfree(&evt_list[evt_i]);
1658 zfree(&evt_list);
1659 return;
1660
1661out_enomem:
1662 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
1663 if (evt_list)
1664 goto out_free;
Jiri Olsa1dc127602012-07-04 00:00:44 +02001665}
1666
1667/*
1668 * Print the help text for the event symbols:
1669 */
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001670void print_events(const char *event_glob, bool name_only)
Jiri Olsa1dc127602012-07-04 00:00:44 +02001671{
Jiri Olsa1dc127602012-07-04 00:00:44 +02001672 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001673 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
Jiri Olsa1dc127602012-07-04 00:00:44 +02001674
1675 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001676 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
Jiri Olsa1dc127602012-07-04 00:00:44 +02001677
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001678 print_hwcache_events(event_glob, name_only);
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001679
Andi Kleendc098b32013-04-20 11:02:29 -07001680 print_pmu_events(event_glob, name_only);
1681
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001682 if (event_glob != NULL)
1683 return;
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +05301684
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001685 if (!name_only) {
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001686 printf(" %-50s [%s]\n",
1687 "rNNN",
1688 event_type_descriptors[PERF_TYPE_RAW]);
1689 printf(" %-50s [%s]\n",
1690 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
1691 event_type_descriptors[PERF_TYPE_RAW]);
Borislav Petkovaf3df2c2012-09-28 19:47:07 +02001692 printf(" (see 'man perf-list' on how to encode it)\n");
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001693 printf("\n");
Thomas Gleixner86847b62009-06-06 12:24:17 +02001694
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001695 printf(" %-50s [%s]\n",
Jacob Shin3741eb92014-05-29 17:26:51 +02001696 "mem:<addr>[/len][:access]",
Liming Wang41bdcb22009-12-29 16:37:07 +08001697 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001698 printf("\n");
1699 }
Frederic Weisbecker1b290d62009-11-23 15:42:35 +01001700
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001701 print_tracepoint_events(NULL, NULL, name_only);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001702}
Jiri Olsa8f707d82012-03-15 20:09:16 +01001703
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001704int parse_events__is_hardcoded_term(struct parse_events_term *term)
Jiri Olsa8f707d82012-03-15 20:09:16 +01001705{
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001706 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001707}
1708
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001709static int new_term(struct parse_events_term **_term, int type_val,
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001710 int type_term, char *config,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02001711 char *str, u64 num, int err_term, int err_val)
Jiri Olsa8f707d82012-03-15 20:09:16 +01001712{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001713 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001714
1715 term = zalloc(sizeof(*term));
1716 if (!term)
1717 return -ENOMEM;
1718
1719 INIT_LIST_HEAD(&term->list);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001720 term->type_val = type_val;
1721 term->type_term = type_term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001722 term->config = config;
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02001723 term->err_term = err_term;
1724 term->err_val = err_val;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001725
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001726 switch (type_val) {
Jiri Olsa8f707d82012-03-15 20:09:16 +01001727 case PARSE_EVENTS__TERM_TYPE_NUM:
1728 term->val.num = num;
1729 break;
1730 case PARSE_EVENTS__TERM_TYPE_STR:
1731 term->val.str = str;
1732 break;
1733 default:
Adrian Hunter4be8be62013-07-04 16:20:24 +03001734 free(term);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001735 return -EINVAL;
1736 }
1737
1738 *_term = term;
1739 return 0;
1740}
1741
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001742int parse_events_term__num(struct parse_events_term **term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02001743 int type_term, char *config, u64 num,
Adrian Hunterbb78ce72015-05-19 16:05:42 +03001744 void *loc_term_, void *loc_val_)
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001745{
Adrian Hunterbb78ce72015-05-19 16:05:42 +03001746 YYLTYPE *loc_term = loc_term_;
1747 YYLTYPE *loc_val = loc_val_;
1748
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001749 return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02001750 config, NULL, num,
1751 loc_term ? loc_term->first_column : 0,
1752 loc_val ? loc_val->first_column : 0);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001753}
1754
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001755int parse_events_term__str(struct parse_events_term **term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02001756 int type_term, char *config, char *str,
Adrian Hunterbb78ce72015-05-19 16:05:42 +03001757 void *loc_term_, void *loc_val_)
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001758{
Adrian Hunterbb78ce72015-05-19 16:05:42 +03001759 YYLTYPE *loc_term = loc_term_;
1760 YYLTYPE *loc_val = loc_val_;
1761
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001762 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02001763 config, str, 0,
1764 loc_term ? loc_term->first_column : 0,
1765 loc_val ? loc_val->first_column : 0);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001766}
1767
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001768int parse_events_term__sym_hw(struct parse_events_term **term,
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02001769 char *config, unsigned idx)
1770{
1771 struct event_symbol *sym;
1772
1773 BUG_ON(idx >= PERF_COUNT_HW_MAX);
1774 sym = &event_symbols_hw[idx];
1775
1776 if (config)
1777 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1778 PARSE_EVENTS__TERM_TYPE_USER, config,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02001779 (char *) sym->symbol, 0, 0, 0);
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02001780 else
1781 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1782 PARSE_EVENTS__TERM_TYPE_USER,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02001783 (char *) "event", (char *) sym->symbol,
1784 0, 0, 0);
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02001785}
1786
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001787int parse_events_term__clone(struct parse_events_term **new,
1788 struct parse_events_term *term)
Zheng Yana6146d52012-06-15 14:31:41 +08001789{
1790 return new_term(new, term->type_val, term->type_term, term->config,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02001791 term->val.str, term->val.num,
1792 term->err_term, term->err_val);
Zheng Yana6146d52012-06-15 14:31:41 +08001793}
1794
Jiri Olsa8f707d82012-03-15 20:09:16 +01001795void parse_events__free_terms(struct list_head *terms)
1796{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001797 struct parse_events_term *term, *h;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001798
1799 list_for_each_entry_safe(term, h, terms, list)
1800 free(term);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001801}
Jiri Olsab39b8392015-04-22 21:10:16 +02001802
1803void parse_events_evlist_error(struct parse_events_evlist *data,
1804 int idx, const char *str)
1805{
1806 struct parse_events_error *err = data->error;
1807
Adrian Huntera6ced2b2015-05-19 16:05:44 +03001808 if (!err)
1809 return;
Jiri Olsab39b8392015-04-22 21:10:16 +02001810 err->idx = idx;
1811 err->str = strdup(str);
1812 WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
1813}