blob: 375af0e02831e603bd4b2f1d36aea0d7822be795 [file] [log] [blame]
David Howellsd2709c72012-11-19 22:21:03 +00001#include <linux/hw_breakpoint.h>
Jiri Olsa8dd2a132015-09-07 10:38:06 +02002#include <linux/err.h>
Ingo Molnar8ad8db32009-05-26 11:10:09 +02003#include "util.h"
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03004#include "../perf.h"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02005#include "evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02006#include "evsel.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06007#include <subcmd/parse-options.h>
Ingo Molnar8ad8db32009-05-26 11:10:09 +02008#include "parse-events.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06009#include <subcmd/exec-cmd.h>
Kan Liang42f60c22014-10-07 11:08:49 -040010#include "string.h"
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -030011#include "symbol.h"
Jason Baron5beeded2009-07-21 14:16:29 -040012#include "cache.h"
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020013#include "header.h"
Wang Nan84c86ca2015-10-14 12:41:14 +000014#include "bpf-loader.h"
Masami Hiramatsu6e81c742014-08-14 02:22:36 +000015#include "debug.h"
Jiri Olsa592d5a62015-09-02 09:56:34 +020016#include <api/fs/tracing_path.h>
Zheng Yanac20de6f2012-06-15 14:31:39 +080017#include "parse-events-bison.h"
Jiri Olsa90e2b222012-06-15 14:31:40 +080018#define YY_EXTRA_TYPE int
Jiri Olsa89812fc2012-03-15 20:09:15 +010019#include "parse-events-flex.h"
Jiri Olsa5f537a22012-03-15 20:09:18 +010020#include "pmu.h"
Namhyung Kimb41f1ce2013-08-27 11:41:53 +090021#include "thread_map.h"
Jiri Olsaf30a79b2015-06-23 00:36:04 +020022#include "cpumap.h"
Masami Hiramatsu40218da2016-06-24 18:06:46 +090023#include "probe-file.h"
Jiri Olsab39b8392015-04-22 21:10:16 +020024#include "asm/bug.h"
Jiri Olsa89812fc2012-03-15 20:09:15 +010025
26#define MAX_NAME_LEN 100
Ingo Molnar8ad8db32009-05-26 11:10:09 +020027
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);
He Kuange637d172015-09-28 03:52:16 +000032static int get_config_terms(struct list_head *head_config,
33 struct list_head *head_terms __maybe_unused);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +020034
Kan Liangdcb4e102014-10-07 11:08:50 -040035static struct perf_pmu_event_symbol *perf_pmu_events_list;
36/*
37 * The variable indicates the number of supported pmu event symbols.
38 * 0 means not initialized and ready to init
39 * -1 means failed to init, don't try anymore
40 * >0 is the number of supported pmu event symbols
41 */
42static int perf_pmu_events_list_num;
43
Yunlong Song705750f2015-02-27 18:21:27 +080044struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
Jiri Olsa1dc127602012-07-04 00:00:44 +020045 [PERF_COUNT_HW_CPU_CYCLES] = {
46 .symbol = "cpu-cycles",
47 .alias = "cycles",
48 },
49 [PERF_COUNT_HW_INSTRUCTIONS] = {
50 .symbol = "instructions",
51 .alias = "",
52 },
53 [PERF_COUNT_HW_CACHE_REFERENCES] = {
54 .symbol = "cache-references",
55 .alias = "",
56 },
57 [PERF_COUNT_HW_CACHE_MISSES] = {
58 .symbol = "cache-misses",
59 .alias = "",
60 },
61 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
62 .symbol = "branch-instructions",
63 .alias = "branches",
64 },
65 [PERF_COUNT_HW_BRANCH_MISSES] = {
66 .symbol = "branch-misses",
67 .alias = "",
68 },
69 [PERF_COUNT_HW_BUS_CYCLES] = {
70 .symbol = "bus-cycles",
71 .alias = "",
72 },
73 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
74 .symbol = "stalled-cycles-frontend",
75 .alias = "idle-cycles-frontend",
76 },
77 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
78 .symbol = "stalled-cycles-backend",
79 .alias = "idle-cycles-backend",
80 },
81 [PERF_COUNT_HW_REF_CPU_CYCLES] = {
82 .symbol = "ref-cycles",
83 .alias = "",
84 },
85};
Ingo Molnar8ad8db32009-05-26 11:10:09 +020086
Yunlong Song705750f2015-02-27 18:21:27 +080087struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
Jiri Olsa1dc127602012-07-04 00:00:44 +020088 [PERF_COUNT_SW_CPU_CLOCK] = {
89 .symbol = "cpu-clock",
90 .alias = "",
91 },
92 [PERF_COUNT_SW_TASK_CLOCK] = {
93 .symbol = "task-clock",
94 .alias = "",
95 },
96 [PERF_COUNT_SW_PAGE_FAULTS] = {
97 .symbol = "page-faults",
98 .alias = "faults",
99 },
100 [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
101 .symbol = "context-switches",
102 .alias = "cs",
103 },
104 [PERF_COUNT_SW_CPU_MIGRATIONS] = {
105 .symbol = "cpu-migrations",
106 .alias = "migrations",
107 },
108 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
109 .symbol = "minor-faults",
110 .alias = "",
111 },
112 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
113 .symbol = "major-faults",
114 .alias = "",
115 },
116 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
117 .symbol = "alignment-faults",
118 .alias = "",
119 },
120 [PERF_COUNT_SW_EMULATION_FAULTS] = {
121 .symbol = "emulation-faults",
122 .alias = "",
123 },
Adrian Hunterd22d1a22013-08-31 21:50:52 +0300124 [PERF_COUNT_SW_DUMMY] = {
125 .symbol = "dummy",
126 .alias = "",
127 },
Arnaldo Carvalho de Melobae9cc42015-11-27 15:54:33 -0300128 [PERF_COUNT_SW_BPF_OUTPUT] = {
129 .symbol = "bpf-output",
130 .alias = "",
131 },
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200132};
133
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200134#define __PERF_EVENT_FIELD(config, name) \
135 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
Ingo Molnar52425192009-05-26 09:17:18 +0200136
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200137#define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200138#define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200139#define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200140#define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
Ingo Molnar52425192009-05-26 09:17:18 +0200141
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300142#define for_each_subsystem(sys_dir, sys_dirent) \
143 while ((sys_dirent = readdir(sys_dir)) != NULL) \
144 if (sys_dirent->d_type == DT_DIR && \
145 (strcmp(sys_dirent->d_name, ".")) && \
146 (strcmp(sys_dirent->d_name, "..")))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400147
Peter Zijlstraae07b632009-08-06 16:48:54 +0200148static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
149{
150 char evt_path[MAXPATHLEN];
151 int fd;
152
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200153 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
Peter Zijlstraae07b632009-08-06 16:48:54 +0200154 sys_dir->d_name, evt_dir->d_name);
155 fd = open(evt_path, O_RDONLY);
156 if (fd < 0)
157 return -EINVAL;
158 close(fd);
159
160 return 0;
161}
162
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300163#define for_each_event(sys_dirent, evt_dir, evt_dirent) \
164 while ((evt_dirent = readdir(evt_dir)) != NULL) \
165 if (evt_dirent->d_type == DT_DIR && \
166 (strcmp(evt_dirent->d_name, ".")) && \
167 (strcmp(evt_dirent->d_name, "..")) && \
168 (!tp_event_has_id(sys_dirent, evt_dirent)))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400169
Li Zefan270bbbe2009-09-17 16:34:51 +0800170#define MAX_EVENT_LENGTH 512
Jason Baronf6bdafe2009-07-21 12:20:22 -0400171
Jason Baronf6bdafe2009-07-21 12:20:22 -0400172
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200173struct tracepoint_path *tracepoint_id_to_path(u64 config)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400174{
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200175 struct tracepoint_path *path = NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400176 DIR *sys_dir, *evt_dir;
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300177 struct dirent *sys_dirent, *evt_dirent;
Stephane Eranian8aa8a7c2012-03-13 16:51:02 +0100178 char id_buf[24];
Eric Dumazet725b1362009-09-24 15:39:09 +0200179 int fd;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400180 u64 id;
181 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +0200182 char dir_path[MAXPATHLEN];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400183
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200184 sys_dir = opendir(tracing_events_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400185 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200186 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400187
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300188 for_each_subsystem(sys_dir, sys_dirent) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200189
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200190 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300191 sys_dirent->d_name);
Eric Dumazet725b1362009-09-24 15:39:09 +0200192 evt_dir = opendir(dir_path);
193 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300194 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200195
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300196 for_each_event(sys_dirent, evt_dir, evt_dirent) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200197
198 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300199 evt_dirent->d_name);
Eric Dumazet725b1362009-09-24 15:39:09 +0200200 fd = open(evt_path, O_RDONLY);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400201 if (fd < 0)
202 continue;
203 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
204 close(fd);
205 continue;
206 }
207 close(fd);
208 id = atoll(id_buf);
209 if (id == config) {
210 closedir(evt_dir);
211 closedir(sys_dir);
Julia Lawall59b4cae2009-12-06 10:16:30 +0100212 path = zalloc(sizeof(*path));
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200213 path->system = malloc(MAX_EVENT_LENGTH);
214 if (!path->system) {
215 free(path);
216 return NULL;
217 }
218 path->name = malloc(MAX_EVENT_LENGTH);
219 if (!path->name) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300220 zfree(&path->system);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200221 free(path);
222 return NULL;
223 }
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300224 strncpy(path->system, sys_dirent->d_name,
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200225 MAX_EVENT_LENGTH);
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300226 strncpy(path->name, evt_dirent->d_name,
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200227 MAX_EVENT_LENGTH);
228 return path;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400229 }
230 }
231 closedir(evt_dir);
232 }
233
Jason Baronf6bdafe2009-07-21 12:20:22 -0400234 closedir(sys_dir);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200235 return NULL;
236}
237
Namhyung Kime7c93f02013-06-26 16:14:05 +0900238struct tracepoint_path *tracepoint_name_to_path(const char *name)
239{
240 struct tracepoint_path *path = zalloc(sizeof(*path));
241 char *str = strchr(name, ':');
242
243 if (path == NULL || str == NULL) {
244 free(path);
245 return NULL;
246 }
247
248 path->system = strndup(name, str - name);
249 path->name = strdup(str+1);
250
251 if (path->system == NULL || path->name == NULL) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300252 zfree(&path->system);
253 zfree(&path->name);
Namhyung Kime7c93f02013-06-26 16:14:05 +0900254 free(path);
255 path = NULL;
256 }
257
258 return path;
259}
260
David Ahern1424dc92011-03-09 22:23:28 -0700261const char *event_type(int type)
262{
263 switch (type) {
264 case PERF_TYPE_HARDWARE:
265 return "hardware";
266
267 case PERF_TYPE_SOFTWARE:
268 return "software";
269
270 case PERF_TYPE_TRACEPOINT:
271 return "tracepoint";
272
273 case PERF_TYPE_HW_CACHE:
274 return "hardware-cache";
275
276 default:
277 break;
278 }
279
280 return "unknown";
281}
282
Wang Nane814fdd2016-02-19 11:43:59 +0000283static int parse_events__is_name_term(struct parse_events_term *term)
284{
285 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
286}
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800287
Wang Nane814fdd2016-02-19 11:43:59 +0000288static char *get_config_name(struct list_head *head_terms)
289{
290 struct parse_events_term *term;
291
292 if (!head_terms)
293 return NULL;
294
295 list_for_each_entry(term, head_terms, list)
296 if (parse_events__is_name_term(term))
297 return term->val.str;
298
299 return NULL;
300}
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800301
Stephane Eranian410136f2013-11-12 17:58:49 +0100302static struct perf_evsel *
303__add_event(struct list_head *list, int *idx,
304 struct perf_event_attr *attr,
Jiri Olsa930a2e22015-07-29 05:42:10 -0400305 char *name, struct cpu_map *cpus,
306 struct list_head *config_terms)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100307{
308 struct perf_evsel *evsel;
309
310 event_attr_init(attr);
311
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300312 evsel = perf_evsel__new_idx(attr, (*idx)++);
David Ahernc5cd8ac2013-07-02 13:27:25 -0600313 if (!evsel)
Stephane Eranian410136f2013-11-12 17:58:49 +0100314 return NULL;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100315
Adrian Hunterfce4d292015-09-08 10:58:55 +0300316 evsel->cpus = cpu_map__get(cpus);
317 evsel->own_cpus = cpu_map__get(cpus);
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200318
Arnaldo Carvalho de Melo9db17632012-06-12 13:45:00 -0300319 if (name)
320 evsel->name = strdup(name);
Jiri Olsa930a2e22015-07-29 05:42:10 -0400321
322 if (config_terms)
323 list_splice(config_terms, &evsel->config_terms);
324
Jiri Olsab847cbd2012-05-21 09:12:51 +0200325 list_add_tail(&evsel->node, list);
Stephane Eranian410136f2013-11-12 17:58:49 +0100326 return evsel;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100327}
328
David Ahernc5cd8ac2013-07-02 13:27:25 -0600329static int add_event(struct list_head *list, int *idx,
Jiri Olsa930a2e22015-07-29 05:42:10 -0400330 struct perf_event_attr *attr, char *name,
331 struct list_head *config_terms)
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800332{
Jiri Olsa930a2e22015-07-29 05:42:10 -0400333 return __add_event(list, idx, attr, name, NULL, config_terms) ? 0 : -ENOMEM;
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800334}
335
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300336static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
Ingo Molnar8326f442009-06-05 20:22:46 +0200337{
338 int i, j;
Paul Mackerras61c45982009-07-01 13:04:34 +1000339 int n, longest = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200340
341 for (i = 0; i < size; i++) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300342 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
Paul Mackerras61c45982009-07-01 13:04:34 +1000343 n = strlen(names[i][j]);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100344 if (n > longest && !strncasecmp(str, names[i][j], n))
Paul Mackerras61c45982009-07-01 13:04:34 +1000345 longest = n;
346 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100347 if (longest > 0)
Paul Mackerras61c45982009-07-01 13:04:34 +1000348 return i;
Ingo Molnar8326f442009-06-05 20:22:46 +0200349 }
350
Ingo Molnar89536452009-06-06 21:04:17 +0200351 return -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200352}
353
Wang Nan43d0b972016-02-19 11:44:01 +0000354typedef int config_term_func_t(struct perf_event_attr *attr,
355 struct parse_events_term *term,
356 struct parse_events_error *err);
357static int config_term_common(struct perf_event_attr *attr,
358 struct parse_events_term *term,
359 struct parse_events_error *err);
360static int config_attr(struct perf_event_attr *attr,
361 struct list_head *head,
362 struct parse_events_error *err,
363 config_term_func_t config_term);
364
David Ahernc5cd8ac2013-07-02 13:27:25 -0600365int parse_events_add_cache(struct list_head *list, int *idx,
Wang Nan43d0b972016-02-19 11:44:01 +0000366 char *type, char *op_result1, char *op_result2,
Arnaldo Carvalho de Melo2c97b0d2016-02-19 19:47:04 -0300367 struct parse_events_error *err,
Wang Nan43d0b972016-02-19 11:44:01 +0000368 struct list_head *head_config)
Ingo Molnar8326f442009-06-05 20:22:46 +0200369{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100370 struct perf_event_attr attr;
Wang Nan43d0b972016-02-19 11:44:01 +0000371 LIST_HEAD(config_terms);
372 char name[MAX_NAME_LEN], *config_name;
Paul Mackerras61c45982009-07-01 13:04:34 +1000373 int cache_type = -1, cache_op = -1, cache_result = -1;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100374 char *op_result[2] = { op_result1, op_result2 };
375 int i, n;
Ingo Molnar8326f442009-06-05 20:22:46 +0200376
Ingo Molnar8326f442009-06-05 20:22:46 +0200377 /*
378 * No fallback - if we cannot get a clear cache type
379 * then bail out:
380 */
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300381 cache_type = parse_aliases(type, perf_evsel__hw_cache,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100382 PERF_COUNT_HW_CACHE_MAX);
Ingo Molnar8326f442009-06-05 20:22:46 +0200383 if (cache_type == -1)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100384 return -EINVAL;
Ingo Molnar8326f442009-06-05 20:22:46 +0200385
Wang Nan43d0b972016-02-19 11:44:01 +0000386 config_name = get_config_name(head_config);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100387 n = snprintf(name, MAX_NAME_LEN, "%s", type);
388
389 for (i = 0; (i < 2) && (op_result[i]); i++) {
390 char *str = op_result[i];
391
Jiri Olsa275ef382012-09-05 19:51:33 +0200392 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
Paul Mackerras61c45982009-07-01 13:04:34 +1000393
394 if (cache_op == -1) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300395 cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100396 PERF_COUNT_HW_CACHE_OP_MAX);
Paul Mackerras61c45982009-07-01 13:04:34 +1000397 if (cache_op >= 0) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300398 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
Jiri Olsa89812fc2012-03-15 20:09:15 +0100399 return -EINVAL;
Paul Mackerras61c45982009-07-01 13:04:34 +1000400 continue;
401 }
402 }
403
404 if (cache_result == -1) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300405 cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
406 PERF_COUNT_HW_CACHE_RESULT_MAX);
Paul Mackerras61c45982009-07-01 13:04:34 +1000407 if (cache_result >= 0)
408 continue;
409 }
Paul Mackerras61c45982009-07-01 13:04:34 +1000410 }
411
Ingo Molnar8326f442009-06-05 20:22:46 +0200412 /*
413 * Fall back to reads:
414 */
Ingo Molnar89536452009-06-06 21:04:17 +0200415 if (cache_op == -1)
416 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
Ingo Molnar8326f442009-06-05 20:22:46 +0200417
Ingo Molnar8326f442009-06-05 20:22:46 +0200418 /*
419 * Fall back to accesses:
420 */
421 if (cache_result == -1)
422 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
423
Jiri Olsa89812fc2012-03-15 20:09:15 +0100424 memset(&attr, 0, sizeof(attr));
425 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
426 attr.type = PERF_TYPE_HW_CACHE;
Wang Nan43d0b972016-02-19 11:44:01 +0000427
428 if (head_config) {
Arnaldo Carvalho de Melo2c97b0d2016-02-19 19:47:04 -0300429 if (config_attr(&attr, head_config, err,
Wang Nan43d0b972016-02-19 11:44:01 +0000430 config_term_common))
431 return -EINVAL;
432
433 if (get_config_terms(head_config, &config_terms))
434 return -ENOMEM;
435 }
436 return add_event(list, idx, &attr, config_name ? : name, &config_terms);
Ingo Molnar8326f442009-06-05 20:22:46 +0200437}
438
Jiri Olsa272ed292015-09-29 17:05:31 +0200439static void tracepoint_error(struct parse_events_error *e, int err,
Wang Nan8c619d62016-07-13 10:44:03 +0000440 const char *sys, const char *name)
Jiri Olsa19658172015-09-07 10:38:07 +0200441{
442 char help[BUFSIZ];
443
Adrian Hunterec183d22016-01-26 14:05:20 +0200444 if (!e)
445 return;
446
Jiri Olsa19658172015-09-07 10:38:07 +0200447 /*
448 * We get error directly from syscall errno ( > 0),
449 * or from encoded pointer's error ( < 0).
450 */
451 err = abs(err);
452
453 switch (err) {
454 case EACCES:
Jiri Olsa272ed292015-09-29 17:05:31 +0200455 e->str = strdup("can't access trace events");
Jiri Olsa19658172015-09-07 10:38:07 +0200456 break;
457 case ENOENT:
Jiri Olsa272ed292015-09-29 17:05:31 +0200458 e->str = strdup("unknown tracepoint");
Jiri Olsa19658172015-09-07 10:38:07 +0200459 break;
460 default:
Jiri Olsa272ed292015-09-29 17:05:31 +0200461 e->str = strdup("failed to add tracepoint");
Jiri Olsa19658172015-09-07 10:38:07 +0200462 break;
463 }
464
465 tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
Jiri Olsa272ed292015-09-29 17:05:31 +0200466 e->help = strdup(help);
Jiri Olsa19658172015-09-07 10:38:07 +0200467}
468
David Ahernc5cd8ac2013-07-02 13:27:25 -0600469static int add_tracepoint(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +0000470 const char *sys_name, const char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200471 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000472 struct list_head *head_config)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200473{
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300474 struct perf_evsel *evsel;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200475
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300476 evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
Jiri Olsa19658172015-09-07 10:38:07 +0200477 if (IS_ERR(evsel)) {
Jiri Olsa272ed292015-09-29 17:05:31 +0200478 tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200479 return PTR_ERR(evsel);
Jiri Olsa19658172015-09-07 10:38:07 +0200480 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200481
He Kuange637d172015-09-28 03:52:16 +0000482 if (head_config) {
483 LIST_HEAD(config_terms);
484
485 if (get_config_terms(head_config, &config_terms))
486 return -ENOMEM;
487 list_splice(&config_terms, &evsel->config_terms);
488 }
489
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300490 list_add_tail(&evsel->node, list);
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300491 return 0;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200492}
493
David Ahernc5cd8ac2013-07-02 13:27:25 -0600494static int add_tracepoint_multi_event(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +0000495 const char *sys_name, const char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200496 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000497 struct list_head *head_config)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200498{
499 char evt_path[MAXPATHLEN];
500 struct dirent *evt_ent;
501 DIR *evt_dir;
Jiri Olsa27bf90b2015-10-05 21:31:17 +0200502 int ret = 0, found = 0;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200503
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200504 snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200505 evt_dir = opendir(evt_path);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200506 if (!evt_dir) {
Jiri Olsa272ed292015-09-29 17:05:31 +0200507 tracepoint_error(err, errno, sys_name, evt_name);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100508 return -1;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200509 }
510
Jiri Olsa89812fc2012-03-15 20:09:15 +0100511 while (!ret && (evt_ent = readdir(evt_dir))) {
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200512 if (!strcmp(evt_ent->d_name, ".")
513 || !strcmp(evt_ent->d_name, "..")
514 || !strcmp(evt_ent->d_name, "enable")
515 || !strcmp(evt_ent->d_name, "filter"))
516 continue;
517
Jiri Olsa89812fc2012-03-15 20:09:15 +0100518 if (!strglobmatch(evt_ent->d_name, evt_name))
Masami Hiramatsufb1d2ed2010-01-05 17:47:17 -0500519 continue;
520
Jiri Olsa27bf90b2015-10-05 21:31:17 +0200521 found++;
522
He Kuange637d172015-09-28 03:52:16 +0000523 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200524 err, head_config);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200525 }
526
Jiri Olsa27bf90b2015-10-05 21:31:17 +0200527 if (!found) {
528 tracepoint_error(err, ENOENT, sys_name, evt_name);
529 ret = -1;
530 }
531
Jiri Olsa0bd3f082012-12-17 14:08:36 +0100532 closedir(evt_dir);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100533 return ret;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200534}
535
David Ahernc5cd8ac2013-07-02 13:27:25 -0600536static int add_tracepoint_event(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +0000537 const char *sys_name, const char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200538 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000539 struct list_head *head_config)
Jiri Olsaf35488f2012-12-17 14:08:37 +0100540{
541 return strpbrk(evt_name, "*?") ?
He Kuange637d172015-09-28 03:52:16 +0000542 add_tracepoint_multi_event(list, idx, sys_name, evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200543 err, head_config) :
He Kuange637d172015-09-28 03:52:16 +0000544 add_tracepoint(list, idx, sys_name, evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200545 err, head_config);
Jiri Olsaf35488f2012-12-17 14:08:37 +0100546}
547
David Ahernc5cd8ac2013-07-02 13:27:25 -0600548static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +0000549 const char *sys_name, const char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200550 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000551 struct list_head *head_config)
Jiri Olsaf35488f2012-12-17 14:08:37 +0100552{
553 struct dirent *events_ent;
554 DIR *events_dir;
555 int ret = 0;
556
557 events_dir = opendir(tracing_events_path);
558 if (!events_dir) {
Jiri Olsa272ed292015-09-29 17:05:31 +0200559 tracepoint_error(err, errno, sys_name, evt_name);
Jiri Olsaf35488f2012-12-17 14:08:37 +0100560 return -1;
561 }
562
563 while (!ret && (events_ent = readdir(events_dir))) {
564 if (!strcmp(events_ent->d_name, ".")
565 || !strcmp(events_ent->d_name, "..")
566 || !strcmp(events_ent->d_name, "enable")
567 || !strcmp(events_ent->d_name, "header_event")
568 || !strcmp(events_ent->d_name, "header_page"))
569 continue;
570
571 if (!strglobmatch(events_ent->d_name, sys_name))
572 continue;
573
574 ret = add_tracepoint_event(list, idx, events_ent->d_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200575 evt_name, err, head_config);
Jiri Olsaf35488f2012-12-17 14:08:37 +0100576 }
577
578 closedir(events_dir);
579 return ret;
580}
581
Wang Nan4edf30e2015-10-14 12:41:17 +0000582struct __add_bpf_event_param {
583 struct parse_events_evlist *data;
584 struct list_head *list;
Wang Nan95088a52016-02-22 09:10:36 +0000585 struct list_head *head_config;
Wang Nan4edf30e2015-10-14 12:41:17 +0000586};
587
Wang Nancd102d72016-07-13 10:44:04 +0000588static int add_bpf_event(const char *group, const char *event, int fd,
Wang Nan4edf30e2015-10-14 12:41:17 +0000589 void *_param)
590{
591 LIST_HEAD(new_evsels);
592 struct __add_bpf_event_param *param = _param;
593 struct parse_events_evlist *evlist = param->data;
594 struct list_head *list = param->list;
Wang Nan1f45b1d2015-10-14 12:41:18 +0000595 struct perf_evsel *pos;
Wang Nan4edf30e2015-10-14 12:41:17 +0000596 int err;
597
598 pr_debug("add bpf event %s:%s and attach bpf program %d\n",
Wang Nancd102d72016-07-13 10:44:04 +0000599 group, event, fd);
Wang Nan4edf30e2015-10-14 12:41:17 +0000600
Wang Nancd102d72016-07-13 10:44:04 +0000601 err = parse_events_add_tracepoint(&new_evsels, &evlist->idx, group,
602 event, evlist->error,
Wang Nan95088a52016-02-22 09:10:36 +0000603 param->head_config);
Wang Nan4edf30e2015-10-14 12:41:17 +0000604 if (err) {
605 struct perf_evsel *evsel, *tmp;
606
607 pr_debug("Failed to add BPF event %s:%s\n",
Wang Nancd102d72016-07-13 10:44:04 +0000608 group, event);
Wang Nan4edf30e2015-10-14 12:41:17 +0000609 list_for_each_entry_safe(evsel, tmp, &new_evsels, node) {
610 list_del(&evsel->node);
611 perf_evsel__delete(evsel);
612 }
613 return err;
614 }
Wang Nancd102d72016-07-13 10:44:04 +0000615 pr_debug("adding %s:%s\n", group, event);
Wang Nan4edf30e2015-10-14 12:41:17 +0000616
Wang Nan1f45b1d2015-10-14 12:41:18 +0000617 list_for_each_entry(pos, &new_evsels, node) {
618 pr_debug("adding %s:%s to %p\n",
Wang Nancd102d72016-07-13 10:44:04 +0000619 group, event, pos);
Wang Nan1f45b1d2015-10-14 12:41:18 +0000620 pos->bpf_fd = fd;
621 }
Wang Nan4edf30e2015-10-14 12:41:17 +0000622 list_splice(&new_evsels, list);
623 return 0;
624}
625
Wang Nan84c86ca2015-10-14 12:41:14 +0000626int parse_events_load_bpf_obj(struct parse_events_evlist *data,
627 struct list_head *list,
Wang Nan95088a52016-02-22 09:10:36 +0000628 struct bpf_object *obj,
629 struct list_head *head_config)
Wang Nan84c86ca2015-10-14 12:41:14 +0000630{
631 int err;
632 char errbuf[BUFSIZ];
Wang Nan95088a52016-02-22 09:10:36 +0000633 struct __add_bpf_event_param param = {data, list, head_config};
Wang Nanaa3abf32015-10-14 12:41:15 +0000634 static bool registered_unprobe_atexit = false;
Wang Nan84c86ca2015-10-14 12:41:14 +0000635
636 if (IS_ERR(obj) || !obj) {
637 snprintf(errbuf, sizeof(errbuf),
638 "Internal error: load bpf obj with NULL");
639 err = -EINVAL;
640 goto errout;
641 }
642
643 /*
Wang Nanaa3abf32015-10-14 12:41:15 +0000644 * Register atexit handler before calling bpf__probe() so
645 * bpf__probe() don't need to unprobe probe points its already
646 * created when failure.
647 */
648 if (!registered_unprobe_atexit) {
649 atexit(bpf__clear);
650 registered_unprobe_atexit = true;
651 }
652
653 err = bpf__probe(obj);
654 if (err) {
655 bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
656 goto errout;
657 }
658
Wang Nan1e5e3ee2015-10-14 12:41:16 +0000659 err = bpf__load(obj);
660 if (err) {
661 bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
662 goto errout;
663 }
664
Wang Nancd102d72016-07-13 10:44:04 +0000665 err = bpf__foreach_event(obj, add_bpf_event, &param);
Wang Nan4edf30e2015-10-14 12:41:17 +0000666 if (err) {
667 snprintf(errbuf, sizeof(errbuf),
668 "Attach events in BPF object failed");
669 goto errout;
670 }
671
672 return 0;
Wang Nan84c86ca2015-10-14 12:41:14 +0000673errout:
674 data->error->help = strdup("(add -v to see detail)");
675 data->error->str = strdup(errbuf);
676 return err;
677}
678
Wang Nana34f3be2016-02-22 09:10:31 +0000679static int
680parse_events_config_bpf(struct parse_events_evlist *data,
681 struct bpf_object *obj,
682 struct list_head *head_config)
683{
684 struct parse_events_term *term;
685 int error_pos;
686
687 if (!head_config || list_empty(head_config))
688 return 0;
689
690 list_for_each_entry(term, head_config, list) {
691 char errbuf[BUFSIZ];
692 int err;
693
694 if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) {
695 snprintf(errbuf, sizeof(errbuf),
696 "Invalid config term for BPF object");
697 errbuf[BUFSIZ - 1] = '\0';
698
699 data->error->idx = term->err_term;
700 data->error->str = strdup(errbuf);
701 return -EINVAL;
702 }
703
Wang Nan7630b3e2016-02-22 09:10:33 +0000704 err = bpf__config_obj(obj, term, data->evlist, &error_pos);
Wang Nana34f3be2016-02-22 09:10:31 +0000705 if (err) {
Wang Nan7630b3e2016-02-22 09:10:33 +0000706 bpf__strerror_config_obj(obj, term, data->evlist,
Wang Nana34f3be2016-02-22 09:10:31 +0000707 &error_pos, err, errbuf,
708 sizeof(errbuf));
709 data->error->help = strdup(
Wang Nan7630b3e2016-02-22 09:10:33 +0000710"Hint:\tValid config terms:\n"
Wang Nane571e022016-02-22 09:10:35 +0000711" \tmap:[<arraymap>].value<indices>=[value]\n"
712" \tmap:[<eventmap>].event<indices>=[event]\n"
Wang Nan7630b3e2016-02-22 09:10:33 +0000713"\n"
Wang Nane571e022016-02-22 09:10:35 +0000714" \twhere <indices> is something like [0,3...5] or [all]\n"
Wang Nana34f3be2016-02-22 09:10:31 +0000715" \t(add -v to see detail)");
716 data->error->str = strdup(errbuf);
717 if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE)
718 data->error->idx = term->err_val;
719 else
720 data->error->idx = term->err_term + error_pos;
721 return err;
722 }
723 }
724 return 0;
725}
726
Wang Nan95088a52016-02-22 09:10:36 +0000727/*
728 * Split config terms:
729 * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
730 * 'call-graph=fp' is 'evt config', should be applied to each
731 * events in bpf.c.
732 * 'map:array.value[0]=1' is 'obj config', should be processed
733 * with parse_events_config_bpf.
734 *
735 * Move object config terms from the first list to obj_head_config.
736 */
737static void
738split_bpf_config_terms(struct list_head *evt_head_config,
739 struct list_head *obj_head_config)
740{
741 struct parse_events_term *term, *temp;
742
743 /*
744 * Currectly, all possible user config term
745 * belong to bpf object. parse_events__is_hardcoded_term()
746 * happends to be a good flag.
747 *
748 * See parse_events_config_bpf() and
749 * config_term_tracepoint().
750 */
751 list_for_each_entry_safe(term, temp, evt_head_config, list)
752 if (!parse_events__is_hardcoded_term(term))
753 list_move_tail(&term->list, obj_head_config);
754}
755
Wang Nan84c86ca2015-10-14 12:41:14 +0000756int parse_events_load_bpf(struct parse_events_evlist *data,
757 struct list_head *list,
Wang Nand509db02015-10-14 12:41:20 +0000758 char *bpf_file_name,
Wang Nana34f3be2016-02-22 09:10:31 +0000759 bool source,
760 struct list_head *head_config)
Wang Nan84c86ca2015-10-14 12:41:14 +0000761{
Wang Nana34f3be2016-02-22 09:10:31 +0000762 int err;
Wang Nan95088a52016-02-22 09:10:36 +0000763 struct bpf_object *obj;
764 LIST_HEAD(obj_head_config);
765
766 if (head_config)
767 split_bpf_config_terms(head_config, &obj_head_config);
Wang Nan84c86ca2015-10-14 12:41:14 +0000768
Wang Nand509db02015-10-14 12:41:20 +0000769 obj = bpf__prepare_load(bpf_file_name, source);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000770 if (IS_ERR(obj)) {
Wang Nan84c86ca2015-10-14 12:41:14 +0000771 char errbuf[BUFSIZ];
Wang Nan84c86ca2015-10-14 12:41:14 +0000772
Wang Nan6371ca3b2015-11-06 13:49:37 +0000773 err = PTR_ERR(obj);
Wang Nan84c86ca2015-10-14 12:41:14 +0000774
775 if (err == -ENOTSUP)
776 snprintf(errbuf, sizeof(errbuf),
777 "BPF support is not compiled");
778 else
Wang Nand3e0ce32015-11-06 13:58:09 +0000779 bpf__strerror_prepare_load(bpf_file_name,
780 source,
781 -err, errbuf,
782 sizeof(errbuf));
Wang Nan84c86ca2015-10-14 12:41:14 +0000783
784 data->error->help = strdup("(add -v to see detail)");
785 data->error->str = strdup(errbuf);
786 return err;
787 }
788
Wang Nan95088a52016-02-22 09:10:36 +0000789 err = parse_events_load_bpf_obj(data, list, obj, head_config);
Wang Nana34f3be2016-02-22 09:10:31 +0000790 if (err)
791 return err;
Wang Nan95088a52016-02-22 09:10:36 +0000792 err = parse_events_config_bpf(data, obj, &obj_head_config);
793
794 /*
795 * Caller doesn't know anything about obj_head_config,
796 * so combine them together again before returnning.
797 */
798 if (head_config)
799 list_splice_tail(&obj_head_config, head_config);
800 return err;
Wang Nan84c86ca2015-10-14 12:41:14 +0000801}
802
Jiri Olsa89812fc2012-03-15 20:09:15 +0100803static int
804parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100805{
806 int i;
807
808 for (i = 0; i < 3; i++) {
Jiri Olsa89812fc2012-03-15 20:09:15 +0100809 if (!type || !type[i])
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100810 break;
811
Jiri Olsa75827322012-06-29 09:22:54 +0200812#define CHECK_SET_TYPE(bit) \
813do { \
814 if (attr->bp_type & bit) \
815 return -EINVAL; \
816 else \
817 attr->bp_type |= bit; \
818} while (0)
819
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100820 switch (type[i]) {
821 case 'r':
Jiri Olsa75827322012-06-29 09:22:54 +0200822 CHECK_SET_TYPE(HW_BREAKPOINT_R);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100823 break;
824 case 'w':
Jiri Olsa75827322012-06-29 09:22:54 +0200825 CHECK_SET_TYPE(HW_BREAKPOINT_W);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100826 break;
827 case 'x':
Jiri Olsa75827322012-06-29 09:22:54 +0200828 CHECK_SET_TYPE(HW_BREAKPOINT_X);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100829 break;
830 default:
Jiri Olsa89812fc2012-03-15 20:09:15 +0100831 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100832 }
833 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100834
Jiri Olsa75827322012-06-29 09:22:54 +0200835#undef CHECK_SET_TYPE
836
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100837 if (!attr->bp_type) /* Default */
838 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
839
Jiri Olsa89812fc2012-03-15 20:09:15 +0100840 return 0;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100841}
842
David Ahernc5cd8ac2013-07-02 13:27:25 -0600843int parse_events_add_breakpoint(struct list_head *list, int *idx,
Jacob Shin3741eb92014-05-29 17:26:51 +0200844 void *ptr, char *type, u64 len)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100845{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100846 struct perf_event_attr attr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100847
Jiri Olsa89812fc2012-03-15 20:09:15 +0100848 memset(&attr, 0, sizeof(attr));
Jiri Olsa9fafd982012-03-20 19:15:39 +0100849 attr.bp_addr = (unsigned long) ptr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100850
Jiri Olsa89812fc2012-03-15 20:09:15 +0100851 if (parse_breakpoint_type(type, &attr))
852 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100853
Jacob Shin3741eb92014-05-29 17:26:51 +0200854 /* Provide some defaults if len is not specified */
855 if (!len) {
856 if (attr.bp_type == HW_BREAKPOINT_X)
857 len = sizeof(long);
858 else
859 len = HW_BREAKPOINT_LEN_4;
860 }
861
862 attr.bp_len = len;
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200863
Jiri Olsa89812fc2012-03-15 20:09:15 +0100864 attr.type = PERF_TYPE_BREAKPOINT;
Jovi Zhang4a841d62012-07-15 03:03:10 +0800865 attr.sample_period = 1;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100866
Jiri Olsa930a2e22015-07-29 05:42:10 -0400867 return add_event(list, idx, &attr, NULL, NULL);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100868}
869
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200870static int check_type_val(struct parse_events_term *term,
871 struct parse_events_error *err,
872 int type)
Jiri Olsa8f707d82012-03-15 20:09:16 +0100873{
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200874 if (type == term->type_val)
875 return 0;
876
877 if (err) {
878 err->idx = term->err_val;
879 if (type == PARSE_EVENTS__TERM_TYPE_NUM)
880 err->str = strdup("expected numeric value");
881 else
882 err->str = strdup("expected string value");
883 }
884 return -EINVAL;
885}
886
Wang Nan17cb5f82016-02-19 11:43:57 +0000887/*
888 * Update according to parse-events.l
889 */
890static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
891 [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>",
892 [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config",
893 [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1",
894 [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2",
895 [PARSE_EVENTS__TERM_TYPE_NAME] = "name",
896 [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period",
897 [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq",
898 [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type",
899 [PARSE_EVENTS__TERM_TYPE_TIME] = "time",
900 [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph",
901 [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size",
902 [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit",
903 [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit",
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300904 [PARSE_EVENTS__TERM_TYPE_MAX_STACK] = "max-stack",
Wang Nan17cb5f82016-02-19 11:43:57 +0000905};
906
Wang Nan1669e502016-02-19 11:43:58 +0000907static bool config_term_shrinked;
908
909static bool
910config_term_avail(int term_type, struct parse_events_error *err)
911{
912 if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
913 err->str = strdup("Invalid term_type");
914 return false;
915 }
916 if (!config_term_shrinked)
917 return true;
918
919 switch (term_type) {
920 case PARSE_EVENTS__TERM_TYPE_CONFIG:
921 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
922 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
923 case PARSE_EVENTS__TERM_TYPE_NAME:
924 return true;
925 default:
926 if (!err)
927 return false;
928
929 /* term_type is validated so indexing is safe */
930 if (asprintf(&err->str, "'%s' is not usable in 'perf stat'",
931 config_term_names[term_type]) < 0)
932 err->str = NULL;
933 return false;
934 }
935}
936
937void parse_events__shrink_config_terms(void)
938{
939 config_term_shrinked = true;
940}
941
He Kuang0b8891a2015-09-28 03:52:13 +0000942static int config_term_common(struct perf_event_attr *attr,
943 struct parse_events_term *term,
944 struct parse_events_error *err)
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200945{
946#define CHECK_TYPE_VAL(type) \
947do { \
948 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
949 return -EINVAL; \
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200950} while (0)
951
952 switch (term->type_term) {
Jiri Olsa8f707d82012-03-15 20:09:16 +0100953 case PARSE_EVENTS__TERM_TYPE_CONFIG:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200954 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100955 attr->config = term->val.num;
956 break;
957 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200958 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100959 attr->config1 = term->val.num;
960 break;
961 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200962 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100963 attr->config2 = term->val.num;
964 break;
965 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200966 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100967 break;
Namhyung Kim09af2a52015-08-09 15:45:23 +0900968 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
969 CHECK_TYPE_VAL(NUM);
970 break;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100971 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
972 /*
973 * TODO uncomment when the field is available
974 * attr->branch_sample_type = term->val.num;
975 */
976 break;
Kan Liang32067712015-08-04 04:30:19 -0400977 case PARSE_EVENTS__TERM_TYPE_TIME:
978 CHECK_TYPE_VAL(NUM);
979 if (term->val.num > 1) {
980 err->str = strdup("expected 0 or 1");
981 err->idx = term->err_val;
982 return -EINVAL;
983 }
984 break;
Kan Liangd457c962015-08-11 06:30:47 -0400985 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
986 CHECK_TYPE_VAL(STR);
987 break;
988 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
989 CHECK_TYPE_VAL(NUM);
990 break;
Wang Nan374ce932015-10-28 10:55:02 +0000991 case PARSE_EVENTS__TERM_TYPE_INHERIT:
992 CHECK_TYPE_VAL(NUM);
993 break;
994 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
995 CHECK_TYPE_VAL(NUM);
996 break;
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200997 case PARSE_EVENTS__TERM_TYPE_NAME:
998 CHECK_TYPE_VAL(STR);
999 break;
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -03001000 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1001 CHECK_TYPE_VAL(NUM);
1002 break;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001003 default:
He Kuangffeb8832015-09-28 03:52:14 +00001004 err->str = strdup("unknown term");
1005 err->idx = term->err_term;
1006 err->help = parse_events_formats_error_string(NULL);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001007 return -EINVAL;
1008 }
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001009
Wang Nan1669e502016-02-19 11:43:58 +00001010 /*
1011 * Check term availbility after basic checking so
1012 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
1013 *
1014 * If check availbility at the entry of this function,
1015 * user will see "'<sysfs term>' is not usable in 'perf stat'"
1016 * if an invalid config term is provided for legacy events
1017 * (for example, instructions/badterm/...), which is confusing.
1018 */
1019 if (!config_term_avail(term->type_term, err))
1020 return -EINVAL;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001021 return 0;
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001022#undef CHECK_TYPE_VAL
Jiri Olsa8f707d82012-03-15 20:09:16 +01001023}
1024
He Kuang0b8891a2015-09-28 03:52:13 +00001025static int config_term_pmu(struct perf_event_attr *attr,
1026 struct parse_events_term *term,
1027 struct parse_events_error *err)
1028{
1029 if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER)
1030 /*
1031 * Always succeed for sysfs terms, as we dont know
1032 * at this point what type they need to have.
1033 */
1034 return 0;
1035 else
1036 return config_term_common(attr, term, err);
1037}
1038
He Kuange637d172015-09-28 03:52:16 +00001039static int config_term_tracepoint(struct perf_event_attr *attr,
1040 struct parse_events_term *term,
1041 struct parse_events_error *err)
1042{
1043 switch (term->type_term) {
1044 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1045 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
Wang Nan374ce932015-10-28 10:55:02 +00001046 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1047 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -03001048 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
He Kuange637d172015-09-28 03:52:16 +00001049 return config_term_common(attr, term, err);
1050 default:
1051 if (err) {
1052 err->idx = term->err_term;
1053 err->str = strdup("unknown term");
1054 err->help = strdup("valid terms: call-graph,stack-size\n");
1055 }
1056 return -EINVAL;
1057 }
1058
1059 return 0;
1060}
1061
Jiri Olsa8f707d82012-03-15 20:09:16 +01001062static int config_attr(struct perf_event_attr *attr,
Jiri Olsa3b0e3712015-04-22 21:10:22 +02001063 struct list_head *head,
He Kuang0b8891a2015-09-28 03:52:13 +00001064 struct parse_events_error *err,
1065 config_term_func_t config_term)
Jiri Olsa8f707d82012-03-15 20:09:16 +01001066{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001067 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001068
1069 list_for_each_entry(term, head, list)
Jiri Olsa3b0e3712015-04-22 21:10:22 +02001070 if (config_term(attr, term, err))
Jiri Olsa8f707d82012-03-15 20:09:16 +01001071 return -EINVAL;
1072
1073 return 0;
1074}
1075
Jiri Olsa930a2e22015-07-29 05:42:10 -04001076static int get_config_terms(struct list_head *head_config,
1077 struct list_head *head_terms __maybe_unused)
1078{
1079#define ADD_CONFIG_TERM(__type, __name, __val) \
1080do { \
1081 struct perf_evsel_config_term *__t; \
1082 \
1083 __t = zalloc(sizeof(*__t)); \
1084 if (!__t) \
1085 return -ENOMEM; \
1086 \
1087 INIT_LIST_HEAD(&__t->list); \
1088 __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \
1089 __t->val.__name = __val; \
1090 list_add_tail(&__t->list, head_terms); \
1091} while (0)
1092
1093 struct parse_events_term *term;
1094
1095 list_for_each_entry(term, head_config, list) {
1096 switch (term->type_term) {
Jiri Olsaee4c7582015-07-29 05:42:11 -04001097 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1098 ADD_CONFIG_TERM(PERIOD, period, term->val.num);
Kan Liang32067712015-08-04 04:30:19 -04001099 break;
Namhyung Kim09af2a52015-08-09 15:45:23 +09001100 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1101 ADD_CONFIG_TERM(FREQ, freq, term->val.num);
1102 break;
Kan Liang32067712015-08-04 04:30:19 -04001103 case PARSE_EVENTS__TERM_TYPE_TIME:
1104 ADD_CONFIG_TERM(TIME, time, term->val.num);
1105 break;
Kan Liangd457c962015-08-11 06:30:47 -04001106 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1107 ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
1108 break;
1109 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1110 ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
1111 break;
Wang Nan374ce932015-10-28 10:55:02 +00001112 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1113 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0);
1114 break;
1115 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1116 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1);
1117 break;
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -03001118 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1119 ADD_CONFIG_TERM(MAX_STACK, max_stack, term->val.num);
1120 break;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001121 default:
1122 break;
1123 }
1124 }
1125#undef ADD_EVSEL_CONFIG
1126 return 0;
1127}
1128
He Kuange637d172015-09-28 03:52:16 +00001129int parse_events_add_tracepoint(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +00001130 const char *sys, const char *event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001131 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +00001132 struct list_head *head_config)
1133{
1134 if (head_config) {
1135 struct perf_event_attr attr;
1136
Jiri Olsa272ed292015-09-29 17:05:31 +02001137 if (config_attr(&attr, head_config, err,
He Kuange637d172015-09-28 03:52:16 +00001138 config_term_tracepoint))
1139 return -EINVAL;
1140 }
1141
1142 if (strpbrk(sys, "*?"))
1143 return add_tracepoint_multi_sys(list, idx, sys, event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001144 err, head_config);
He Kuange637d172015-09-28 03:52:16 +00001145 else
1146 return add_tracepoint_event(list, idx, sys, event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001147 err, head_config);
He Kuange637d172015-09-28 03:52:16 +00001148}
1149
Jiri Olsa87d650b2015-04-22 21:10:24 +02001150int parse_events_add_numeric(struct parse_events_evlist *data,
1151 struct list_head *list,
Robert Richterb527bab2012-08-07 19:43:13 +02001152 u32 type, u64 config,
Jiri Olsa8f707d82012-03-15 20:09:16 +01001153 struct list_head *head_config)
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301154{
Jiri Olsa89812fc2012-03-15 20:09:15 +01001155 struct perf_event_attr attr;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001156 LIST_HEAD(config_terms);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301157
Jiri Olsa89812fc2012-03-15 20:09:15 +01001158 memset(&attr, 0, sizeof(attr));
1159 attr.type = type;
1160 attr.config = config;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001161
Jiri Olsa930a2e22015-07-29 05:42:10 -04001162 if (head_config) {
He Kuang0b8891a2015-09-28 03:52:13 +00001163 if (config_attr(&attr, head_config, data->error,
1164 config_term_common))
Jiri Olsa930a2e22015-07-29 05:42:10 -04001165 return -EINVAL;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001166
Jiri Olsa930a2e22015-07-29 05:42:10 -04001167 if (get_config_terms(head_config, &config_terms))
1168 return -ENOMEM;
1169 }
1170
Wang Nan10bf3582016-02-19 11:44:00 +00001171 return add_event(list, &data->idx, &attr,
1172 get_config_name(head_config), &config_terms);
Paul Mackerras61c45982009-07-01 13:04:34 +10001173}
1174
Jiri Olsa36adec82015-04-22 21:10:19 +02001175int parse_events_add_pmu(struct parse_events_evlist *data,
1176 struct list_head *list, char *name,
1177 struct list_head *head_config)
Jiri Olsa5f537a22012-03-15 20:09:18 +01001178{
1179 struct perf_event_attr attr;
Matt Fleming46441bd2014-09-24 15:04:06 +01001180 struct perf_pmu_info info;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001181 struct perf_pmu *pmu;
Stephane Eranian410136f2013-11-12 17:58:49 +01001182 struct perf_evsel *evsel;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001183 LIST_HEAD(config_terms);
Jiri Olsa5f537a22012-03-15 20:09:18 +01001184
1185 pmu = perf_pmu__find(name);
1186 if (!pmu)
1187 return -EINVAL;
1188
Adrian Hunterdc0a6202014-07-31 09:00:49 +03001189 if (pmu->default_config) {
1190 memcpy(&attr, pmu->default_config,
1191 sizeof(struct perf_event_attr));
1192 } else {
1193 memset(&attr, 0, sizeof(attr));
1194 }
Jiri Olsa5f537a22012-03-15 20:09:18 +01001195
Adrian Hunterad962272014-08-15 22:08:40 +03001196 if (!head_config) {
1197 attr.type = pmu->type;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001198 evsel = __add_event(list, &data->idx, &attr, NULL, pmu->cpus, NULL);
Adrian Hunterad962272014-08-15 22:08:40 +03001199 return evsel ? 0 : -ENOMEM;
1200 }
1201
Matt Fleming46441bd2014-09-24 15:04:06 +01001202 if (perf_pmu__check_alias(pmu, head_config, &info))
Zheng Yana6146d52012-06-15 14:31:41 +08001203 return -EINVAL;
1204
Jiri Olsa5f537a22012-03-15 20:09:18 +01001205 /*
1206 * Configure hardcoded terms first, no need to check
1207 * return value when called with fail == 0 ;)
1208 */
He Kuang0b8891a2015-09-28 03:52:13 +00001209 if (config_attr(&attr, head_config, data->error, config_term_pmu))
Jiri Olsac056ba6a2015-04-22 21:10:18 +02001210 return -EINVAL;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001211
Jiri Olsa930a2e22015-07-29 05:42:10 -04001212 if (get_config_terms(head_config, &config_terms))
1213 return -ENOMEM;
1214
Jiri Olsae64b0202015-04-22 21:10:21 +02001215 if (perf_pmu__config(pmu, &attr, head_config, data->error))
Jiri Olsa5f537a22012-03-15 20:09:18 +01001216 return -EINVAL;
1217
Jiri Olsa36adec82015-04-22 21:10:19 +02001218 evsel = __add_event(list, &data->idx, &attr,
Wang Nane814fdd2016-02-19 11:43:59 +00001219 get_config_name(head_config), pmu->cpus,
Jiri Olsa930a2e22015-07-29 05:42:10 -04001220 &config_terms);
Stephane Eranian410136f2013-11-12 17:58:49 +01001221 if (evsel) {
Matt Fleming46441bd2014-09-24 15:04:06 +01001222 evsel->unit = info.unit;
1223 evsel->scale = info.scale;
Matt Fleming044330c2014-11-21 10:31:12 +01001224 evsel->per_pkg = info.per_pkg;
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001225 evsel->snapshot = info.snapshot;
Stephane Eranian410136f2013-11-12 17:58:49 +01001226 }
1227
1228 return evsel ? 0 : -ENOMEM;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001229}
1230
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001231int parse_events__modifier_group(struct list_head *list,
1232 char *event_mod)
Jiri Olsa89efb022012-08-08 12:14:14 +02001233{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001234 return parse_events__modifier_event(list, event_mod, true);
1235}
1236
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -03001237void parse_events__set_leader(char *name, struct list_head *list)
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001238{
1239 struct perf_evsel *leader;
1240
Wang Nan854f7362015-09-06 07:13:17 +00001241 if (list_empty(list)) {
1242 WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1243 return;
1244 }
1245
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -03001246 __perf_evlist__set_leader(list);
1247 leader = list_entry(list->next, struct perf_evsel, node);
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001248 leader->group_name = name ? strdup(name) : NULL;
Jiri Olsa89efb022012-08-08 12:14:14 +02001249}
1250
David Ahernc5cd8ac2013-07-02 13:27:25 -06001251/* list_event is assumed to point to malloc'ed memory */
Jiri Olsa5d7be902012-03-20 19:15:40 +01001252void parse_events_update_lists(struct list_head *list_event,
1253 struct list_head *list_all)
1254{
1255 /*
1256 * Called for single event definition. Update the
Jiri Olsa89efb022012-08-08 12:14:14 +02001257 * 'all event' list, and reinit the 'single event'
Jiri Olsa5d7be902012-03-20 19:15:40 +01001258 * list, for next event definition.
1259 */
1260 list_splice_tail(list_event, list_all);
Jiri Olsab847cbd2012-05-21 09:12:51 +02001261 free(list_event);
Jiri Olsa5d7be902012-03-20 19:15:40 +01001262}
1263
Jiri Olsaf5b11352012-08-08 12:21:54 +02001264struct event_modifier {
1265 int eu;
1266 int ek;
1267 int eh;
1268 int eH;
1269 int eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001270 int eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001271 int precise;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001272 int precise_max;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001273 int exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02001274 int sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001275 int pinned;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001276};
Paul Mackerras61c45982009-07-01 13:04:34 +10001277
Jiri Olsaf5b11352012-08-08 12:21:54 +02001278static int get_event_modifier(struct event_modifier *mod, char *str,
1279 struct perf_evsel *evsel)
1280{
1281 int eu = evsel ? evsel->attr.exclude_user : 0;
1282 int ek = evsel ? evsel->attr.exclude_kernel : 0;
1283 int eh = evsel ? evsel->attr.exclude_hv : 0;
1284 int eH = evsel ? evsel->attr.exclude_host : 0;
1285 int eG = evsel ? evsel->attr.exclude_guest : 0;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001286 int eI = evsel ? evsel->attr.exclude_idle : 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001287 int precise = evsel ? evsel->attr.precise_ip : 0;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001288 int precise_max = 0;
Jiri Olsa3c176312012-10-10 17:39:03 +02001289 int sample_read = 0;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001290 int pinned = evsel ? evsel->attr.pinned : 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001291
1292 int exclude = eu | ek | eh;
1293 int exclude_GH = evsel ? evsel->exclude_GH : 0;
1294
Jiri Olsaf5b11352012-08-08 12:21:54 +02001295 memset(mod, 0, sizeof(*mod));
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001296
Paul Mackerras61c45982009-07-01 13:04:34 +10001297 while (*str) {
Peter Zijlstraab608342010-04-08 23:03:20 +02001298 if (*str == 'u') {
1299 if (!exclude)
1300 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001301 eu = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +02001302 } else if (*str == 'k') {
1303 if (!exclude)
1304 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001305 ek = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +02001306 } else if (*str == 'h') {
1307 if (!exclude)
1308 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001309 eh = 0;
Joerg Roedel99320cc2012-01-04 17:54:19 +01001310 } else if (*str == 'G') {
1311 if (!exclude_GH)
1312 exclude_GH = eG = eH = 1;
1313 eG = 0;
1314 } else if (*str == 'H') {
1315 if (!exclude_GH)
1316 exclude_GH = eG = eH = 1;
1317 eH = 0;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001318 } else if (*str == 'I') {
1319 eI = 1;
Peter Zijlstraab608342010-04-08 23:03:20 +02001320 } else if (*str == 'p') {
1321 precise++;
David Ahern13427982012-09-13 14:59:13 -06001322 /* use of precise requires exclude_guest */
1323 if (!exclude_GH)
1324 eG = 1;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001325 } else if (*str == 'P') {
1326 precise_max = 1;
Jiri Olsa3c176312012-10-10 17:39:03 +02001327 } else if (*str == 'S') {
1328 sample_read = 1;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001329 } else if (*str == 'D') {
1330 pinned = 1;
Peter Zijlstraab608342010-04-08 23:03:20 +02001331 } else
Paul Mackerras61c45982009-07-01 13:04:34 +10001332 break;
Peter Zijlstraab608342010-04-08 23:03:20 +02001333
Paul Mackerras61c45982009-07-01 13:04:34 +10001334 ++str;
1335 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001336
Jiri Olsa89812fc2012-03-15 20:09:15 +01001337 /*
1338 * precise ip:
1339 *
1340 * 0 - SAMPLE_IP can have arbitrary skid
1341 * 1 - SAMPLE_IP must have constant skid
1342 * 2 - SAMPLE_IP requested to have 0 skid
1343 * 3 - SAMPLE_IP must have 0 skid
1344 *
1345 * See also PERF_RECORD_MISC_EXACT_IP
1346 */
1347 if (precise > 3)
1348 return -EINVAL;
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001349
Jiri Olsaf5b11352012-08-08 12:21:54 +02001350 mod->eu = eu;
1351 mod->ek = ek;
1352 mod->eh = eh;
1353 mod->eH = eH;
1354 mod->eG = eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001355 mod->eI = eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001356 mod->precise = precise;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001357 mod->precise_max = precise_max;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001358 mod->exclude_GH = exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02001359 mod->sample_read = sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001360 mod->pinned = pinned;
1361
Jiri Olsaf5b11352012-08-08 12:21:54 +02001362 return 0;
1363}
1364
Jiri Olsa534123f2012-11-13 15:32:58 +01001365/*
1366 * Basic modifier sanity check to validate it contains only one
1367 * instance of any modifier (apart from 'p') present.
1368 */
1369static int check_modifier(char *str)
1370{
1371 char *p = str;
1372
1373 /* The sizeof includes 0 byte as well. */
Jiri Olsa7f94af72015-10-05 20:06:05 +02001374 if (strlen(str) > (sizeof("ukhGHpppPSDI") - 1))
Jiri Olsa534123f2012-11-13 15:32:58 +01001375 return -1;
1376
1377 while (*p) {
1378 if (*p != 'p' && strchr(p + 1, *p))
1379 return -1;
1380 p++;
1381 }
1382
1383 return 0;
1384}
1385
Jiri Olsaf5b11352012-08-08 12:21:54 +02001386int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1387{
1388 struct perf_evsel *evsel;
1389 struct event_modifier mod;
1390
1391 if (str == NULL)
1392 return 0;
1393
Jiri Olsa534123f2012-11-13 15:32:58 +01001394 if (check_modifier(str))
1395 return -EINVAL;
1396
Jiri Olsaf5b11352012-08-08 12:21:54 +02001397 if (!add && get_event_modifier(&mod, str, NULL))
1398 return -EINVAL;
1399
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001400 __evlist__for_each_entry(list, evsel) {
Jiri Olsaf5b11352012-08-08 12:21:54 +02001401 if (add && get_event_modifier(&mod, str, evsel))
1402 return -EINVAL;
1403
1404 evsel->attr.exclude_user = mod.eu;
1405 evsel->attr.exclude_kernel = mod.ek;
1406 evsel->attr.exclude_hv = mod.eh;
1407 evsel->attr.precise_ip = mod.precise;
1408 evsel->attr.exclude_host = mod.eH;
1409 evsel->attr.exclude_guest = mod.eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001410 evsel->attr.exclude_idle = mod.eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001411 evsel->exclude_GH = mod.exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02001412 evsel->sample_read = mod.sample_read;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001413 evsel->precise_max = mod.precise_max;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001414
1415 if (perf_evsel__is_group_leader(evsel))
1416 evsel->attr.pinned = mod.pinned;
Jiri Olsa89812fc2012-03-15 20:09:15 +01001417 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001418
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301419 return 0;
1420}
1421
Robert Richterac2ba9f2012-08-16 21:10:21 +02001422int parse_events_name(struct list_head *list, char *name)
1423{
1424 struct perf_evsel *evsel;
1425
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001426 __evlist__for_each_entry(list, evsel) {
Robert Richterac2ba9f2012-08-16 21:10:21 +02001427 if (!evsel->name)
1428 evsel->name = strdup(name);
1429 }
1430
1431 return 0;
1432}
1433
Kan Liangdcb4e102014-10-07 11:08:50 -04001434static int
1435comp_pmu(const void *p1, const void *p2)
1436{
1437 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
1438 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
1439
1440 return strcmp(pmu1->symbol, pmu2->symbol);
1441}
1442
1443static void perf_pmu__parse_cleanup(void)
1444{
1445 if (perf_pmu_events_list_num > 0) {
1446 struct perf_pmu_event_symbol *p;
1447 int i;
1448
1449 for (i = 0; i < perf_pmu_events_list_num; i++) {
1450 p = perf_pmu_events_list + i;
1451 free(p->symbol);
1452 }
1453 free(perf_pmu_events_list);
1454 perf_pmu_events_list = NULL;
1455 perf_pmu_events_list_num = 0;
1456 }
1457}
1458
1459#define SET_SYMBOL(str, stype) \
1460do { \
1461 p->symbol = str; \
1462 if (!p->symbol) \
1463 goto err; \
1464 p->type = stype; \
1465} while (0)
1466
1467/*
1468 * Read the pmu events list from sysfs
1469 * Save it into perf_pmu_events_list
1470 */
1471static void perf_pmu__parse_init(void)
1472{
1473
1474 struct perf_pmu *pmu = NULL;
1475 struct perf_pmu_alias *alias;
1476 int len = 0;
1477
1478 pmu = perf_pmu__find("cpu");
1479 if ((pmu == NULL) || list_empty(&pmu->aliases)) {
1480 perf_pmu_events_list_num = -1;
1481 return;
1482 }
1483 list_for_each_entry(alias, &pmu->aliases, list) {
1484 if (strchr(alias->name, '-'))
1485 len++;
1486 len++;
1487 }
1488 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1489 if (!perf_pmu_events_list)
1490 return;
1491 perf_pmu_events_list_num = len;
1492
1493 len = 0;
1494 list_for_each_entry(alias, &pmu->aliases, list) {
1495 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1496 char *tmp = strchr(alias->name, '-');
1497
1498 if (tmp != NULL) {
1499 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1500 PMU_EVENT_SYMBOL_PREFIX);
1501 p++;
1502 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1503 len += 2;
1504 } else {
1505 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1506 len++;
1507 }
1508 }
1509 qsort(perf_pmu_events_list, len,
1510 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1511
1512 return;
1513err:
1514 perf_pmu__parse_cleanup();
1515}
1516
1517enum perf_pmu_event_symbol_type
1518perf_pmu__parse_check(const char *name)
1519{
1520 struct perf_pmu_event_symbol p, *r;
1521
1522 /* scan kernel pmu events from sysfs if needed */
1523 if (perf_pmu_events_list_num == 0)
1524 perf_pmu__parse_init();
1525 /*
1526 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1527 * cpu-cycles has been handled by hardcode.
1528 * So it must be cpu// events, not kernel pmu event.
1529 */
1530 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1531 return PMU_EVENT_SYMBOL_ERR;
1532
1533 p.symbol = strdup(name);
1534 r = bsearch(&p, perf_pmu_events_list,
1535 (size_t) perf_pmu_events_list_num,
1536 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1537 free(p.symbol);
1538 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1539}
1540
Jiri Olsa90e2b222012-06-15 14:31:40 +08001541static int parse_events__scanner(const char *str, void *data, int start_token)
Zheng Yanac20de6f2012-06-15 14:31:39 +08001542{
1543 YY_BUFFER_STATE buffer;
1544 void *scanner;
1545 int ret;
1546
Jiri Olsa90e2b222012-06-15 14:31:40 +08001547 ret = parse_events_lex_init_extra(start_token, &scanner);
Zheng Yanac20de6f2012-06-15 14:31:39 +08001548 if (ret)
1549 return ret;
1550
1551 buffer = parse_events__scan_string(str, scanner);
1552
1553#ifdef PARSER_DEBUG
1554 parse_events_debug = 1;
1555#endif
1556 ret = parse_events_parse(data, scanner);
1557
1558 parse_events__flush_buffer(buffer, scanner);
1559 parse_events__delete_buffer(buffer, scanner);
1560 parse_events_lex_destroy(scanner);
1561 return ret;
1562}
1563
Jiri Olsa90e2b222012-06-15 14:31:40 +08001564/*
1565 * parse event config string, return a list of event terms.
1566 */
1567int parse_events_terms(struct list_head *terms, const char *str)
1568{
Arnaldo Carvalho de Melo23b63392013-01-18 16:56:57 -03001569 struct parse_events_terms data = {
Jiri Olsa90e2b222012-06-15 14:31:40 +08001570 .terms = NULL,
1571 };
1572 int ret;
1573
1574 ret = parse_events__scanner(str, &data, PE_START_TERMS);
1575 if (!ret) {
1576 list_splice(data.terms, terms);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001577 zfree(&data.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001578 return 0;
1579 }
1580
Arnaldo Carvalho de Melo2146afc2016-02-12 17:09:17 -03001581 parse_events_terms__delete(data.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001582 return ret;
1583}
1584
Jiri Olsab39b8392015-04-22 21:10:16 +02001585int parse_events(struct perf_evlist *evlist, const char *str,
1586 struct parse_events_error *err)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001587{
Arnaldo Carvalho de Melo23b63392013-01-18 16:56:57 -03001588 struct parse_events_evlist data = {
Wang Nan7630b3e2016-02-22 09:10:33 +00001589 .list = LIST_HEAD_INIT(data.list),
1590 .idx = evlist->nr_entries,
1591 .error = err,
1592 .evlist = evlist,
Jiri Olsa46010ab2012-06-15 14:31:38 +08001593 };
Jiri Olsa46010ab2012-06-15 14:31:38 +08001594 int ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001595
Jiri Olsa90e2b222012-06-15 14:31:40 +08001596 ret = parse_events__scanner(str, &data, PE_START_EVENTS);
Kan Liangdcb4e102014-10-07 11:08:50 -04001597 perf_pmu__parse_cleanup();
Jiri Olsa89812fc2012-03-15 20:09:15 +01001598 if (!ret) {
Wang Nan15bfd2c2015-07-10 07:36:09 +00001599 struct perf_evsel *last;
1600
Wang Nan854f7362015-09-06 07:13:17 +00001601 if (list_empty(&data.list)) {
1602 WARN_ONCE(true, "WARNING: event parser found nothing");
1603 return -1;
1604 }
1605
Adrian Hunterf114d6e2015-09-08 10:58:53 +03001606 perf_evlist__splice_list_tail(evlist, &data.list);
Namhyung Kim97f63e42013-01-22 18:09:29 +09001607 evlist->nr_groups += data.nr_groups;
Wang Nan15bfd2c2015-07-10 07:36:09 +00001608 last = perf_evlist__last(evlist);
1609 last->cmdline_group_boundary = true;
1610
Jiri Olsa89812fc2012-03-15 20:09:15 +01001611 return 0;
1612 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02001613
Jiri Olsa5d7be902012-03-20 19:15:40 +01001614 /*
1615 * There are 2 users - builtin-record and builtin-test objects.
1616 * Both call perf_evlist__delete in case of error, so we dont
1617 * need to bother.
1618 */
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02001619 return ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001620}
1621
Jiri Olsab39b8392015-04-22 21:10:16 +02001622#define MAX_WIDTH 1000
1623static int get_term_width(void)
1624{
1625 struct winsize ws;
1626
1627 get_term_dimensions(&ws);
1628 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1629}
1630
1631static void parse_events_print_error(struct parse_events_error *err,
1632 const char *event)
1633{
1634 const char *str = "invalid or unsupported event: ";
1635 char _buf[MAX_WIDTH];
1636 char *buf = (char *) event;
1637 int idx = 0;
1638
1639 if (err->str) {
1640 /* -2 for extra '' in the final fprintf */
1641 int width = get_term_width() - 2;
1642 int len_event = strlen(event);
1643 int len_str, max_len, cut = 0;
1644
1645 /*
1646 * Maximum error index indent, we will cut
1647 * the event string if it's bigger.
1648 */
Adrian Hunter141b2d32015-07-17 19:33:51 +03001649 int max_err_idx = 13;
Jiri Olsab39b8392015-04-22 21:10:16 +02001650
1651 /*
1652 * Let's be specific with the message when
1653 * we have the precise error.
1654 */
1655 str = "event syntax error: ";
1656 len_str = strlen(str);
1657 max_len = width - len_str;
1658
1659 buf = _buf;
1660
Adam Buchbinderbd1a0be52016-02-24 10:02:25 -08001661 /* We're cutting from the beginning. */
Jiri Olsab39b8392015-04-22 21:10:16 +02001662 if (err->idx > max_err_idx)
1663 cut = err->idx - max_err_idx;
1664
1665 strncpy(buf, event + cut, max_len);
1666
1667 /* Mark cut parts with '..' on both sides. */
1668 if (cut)
1669 buf[0] = buf[1] = '.';
1670
1671 if ((len_event - cut) > max_len) {
1672 buf[max_len - 1] = buf[max_len - 2] = '.';
1673 buf[max_len] = 0;
1674 }
1675
1676 idx = len_str + err->idx - cut;
1677 }
1678
1679 fprintf(stderr, "%s'%s'\n", str, buf);
1680 if (idx) {
1681 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
1682 if (err->help)
1683 fprintf(stderr, "\n%s\n", err->help);
1684 free(err->str);
1685 free(err->help);
1686 }
1687
1688 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
1689}
1690
1691#undef MAX_WIDTH
1692
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001693int parse_events_option(const struct option *opt, const char *str,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001694 int unset __maybe_unused)
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001695{
1696 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
Jiri Olsab39b8392015-04-22 21:10:16 +02001697 struct parse_events_error err = { .idx = 0, };
1698 int ret = parse_events(evlist, str, &err);
Andi Kleen9175ce12012-10-26 13:30:06 -07001699
Jiri Olsab39b8392015-04-22 21:10:16 +02001700 if (ret)
1701 parse_events_print_error(&err, str);
1702
Andi Kleen9175ce12012-10-26 13:30:06 -07001703 return ret;
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001704}
1705
Wang Nan4ba1faa2015-07-10 07:36:10 +00001706static int
1707foreach_evsel_in_last_glob(struct perf_evlist *evlist,
1708 int (*func)(struct perf_evsel *evsel,
1709 const void *arg),
1710 const void *arg)
Li Zefanc171b552009-10-15 11:22:07 +08001711{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001712 struct perf_evsel *last = NULL;
Wang Nan4ba1faa2015-07-10 07:36:10 +00001713 int err;
Li Zefanc171b552009-10-15 11:22:07 +08001714
Wang Nan854f7362015-09-06 07:13:17 +00001715 /*
1716 * Don't return when list_empty, give func a chance to report
1717 * error when it found last == NULL.
1718 *
1719 * So no need to WARN here, let *func do this.
1720 */
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001721 if (evlist->nr_entries > 0)
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001722 last = perf_evlist__last(evlist);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001723
Wang Nan15bfd2c2015-07-10 07:36:09 +00001724 do {
Wang Nan4ba1faa2015-07-10 07:36:10 +00001725 err = (*func)(last, arg);
1726 if (err)
Wang Nan15bfd2c2015-07-10 07:36:09 +00001727 return -1;
Wang Nan4ba1faa2015-07-10 07:36:10 +00001728 if (!last)
1729 return 0;
Wang Nan15bfd2c2015-07-10 07:36:09 +00001730
1731 if (last->node.prev == &evlist->entries)
1732 return 0;
1733 last = list_entry(last->node.prev, struct perf_evsel, node);
1734 } while (!last->cmdline_group_boundary);
Li Zefanc171b552009-10-15 11:22:07 +08001735
1736 return 0;
1737}
1738
Wang Nan4ba1faa2015-07-10 07:36:10 +00001739static int set_filter(struct perf_evsel *evsel, const void *arg)
1740{
1741 const char *str = arg;
1742
1743 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
1744 fprintf(stderr,
1745 "--filter option should follow a -e tracepoint option\n");
1746 return -1;
1747 }
1748
1749 if (perf_evsel__append_filter(evsel, "&&", str) < 0) {
1750 fprintf(stderr,
1751 "not enough memory to hold filter string\n");
1752 return -1;
1753 }
1754
1755 return 0;
1756}
1757
1758int parse_filter(const struct option *opt, const char *str,
1759 int unset __maybe_unused)
1760{
1761 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1762
1763 return foreach_evsel_in_last_glob(evlist, set_filter,
1764 (const void *)str);
1765}
1766
1767static int add_exclude_perf_filter(struct perf_evsel *evsel,
1768 const void *arg __maybe_unused)
1769{
1770 char new_filter[64];
1771
1772 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
1773 fprintf(stderr,
1774 "--exclude-perf option should follow a -e tracepoint option\n");
1775 return -1;
1776 }
1777
1778 snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
1779
1780 if (perf_evsel__append_filter(evsel, "&&", new_filter) < 0) {
1781 fprintf(stderr,
1782 "not enough memory to hold filter string\n");
1783 return -1;
1784 }
1785
1786 return 0;
1787}
1788
1789int exclude_perf(const struct option *opt,
1790 const char *arg __maybe_unused,
1791 int unset __maybe_unused)
1792{
1793 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1794
1795 return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
1796 NULL);
1797}
1798
Thomas Gleixner86847b62009-06-06 12:24:17 +02001799static const char * const event_type_descriptors[] = {
Thomas Gleixner86847b62009-06-06 12:24:17 +02001800 "Hardware event",
1801 "Software event",
1802 "Tracepoint event",
1803 "Hardware cache event",
Liming Wang41bdcb22009-12-29 16:37:07 +08001804 "Raw hardware event descriptor",
1805 "Hardware breakpoint",
Thomas Gleixner86847b62009-06-06 12:24:17 +02001806};
1807
Yunlong Songab0e4802015-02-27 18:21:25 +08001808static int cmp_string(const void *a, const void *b)
1809{
1810 const char * const *as = a;
1811 const char * const *bs = b;
1812
1813 return strcmp(*as, *bs);
1814}
1815
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001816/*
Jason Baronf6bdafe2009-07-21 12:20:22 -04001817 * Print the events from <debugfs_mount_point>/tracing/events
1818 */
1819
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001820void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
1821 bool name_only)
Jason Baronf6bdafe2009-07-21 12:20:22 -04001822{
1823 DIR *sys_dir, *evt_dir;
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03001824 struct dirent *sys_dirent, *evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001825 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +02001826 char dir_path[MAXPATHLEN];
Yunlong Songab0e4802015-02-27 18:21:25 +08001827 char **evt_list = NULL;
1828 unsigned int evt_i = 0, evt_num = 0;
1829 bool evt_num_known = false;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001830
Yunlong Songab0e4802015-02-27 18:21:25 +08001831restart:
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001832 sys_dir = opendir(tracing_events_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -04001833 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +02001834 return;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001835
Yunlong Songab0e4802015-02-27 18:21:25 +08001836 if (evt_num_known) {
1837 evt_list = zalloc(sizeof(char *) * evt_num);
1838 if (!evt_list)
1839 goto out_close_sys_dir;
1840 }
1841
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03001842 for_each_subsystem(sys_dir, sys_dirent) {
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001843 if (subsys_glob != NULL &&
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03001844 !strglobmatch(sys_dirent->d_name, subsys_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001845 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02001846
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001847 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03001848 sys_dirent->d_name);
Eric Dumazet725b1362009-09-24 15:39:09 +02001849 evt_dir = opendir(dir_path);
1850 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03001851 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02001852
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03001853 for_each_event(sys_dirent, evt_dir, evt_dirent) {
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001854 if (event_glob != NULL &&
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03001855 !strglobmatch(evt_dirent->d_name, event_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001856 continue;
1857
Yunlong Songab0e4802015-02-27 18:21:25 +08001858 if (!evt_num_known) {
1859 evt_num++;
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02001860 continue;
1861 }
1862
Jason Baronf6bdafe2009-07-21 12:20:22 -04001863 snprintf(evt_path, MAXPATHLEN, "%s:%s",
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03001864 sys_dirent->d_name, evt_dirent->d_name);
Yunlong Songab0e4802015-02-27 18:21:25 +08001865
1866 evt_list[evt_i] = strdup(evt_path);
1867 if (evt_list[evt_i] == NULL)
1868 goto out_close_evt_dir;
1869 evt_i++;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001870 }
1871 closedir(evt_dir);
1872 }
Jason Baronf6bdafe2009-07-21 12:20:22 -04001873 closedir(sys_dir);
Yunlong Songab0e4802015-02-27 18:21:25 +08001874
1875 if (!evt_num_known) {
1876 evt_num_known = true;
1877 goto restart;
1878 }
1879 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1880 evt_i = 0;
1881 while (evt_i < evt_num) {
1882 if (name_only) {
1883 printf("%s ", evt_list[evt_i++]);
1884 continue;
1885 }
1886 printf(" %-50s [%s]\n", evt_list[evt_i++],
1887 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1888 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03001889 if (evt_num && pager_in_use())
Yunlong Songab0e4802015-02-27 18:21:25 +08001890 printf("\n");
1891
1892out_free:
1893 evt_num = evt_i;
1894 for (evt_i = 0; evt_i < evt_num; evt_i++)
1895 zfree(&evt_list[evt_i]);
1896 zfree(&evt_list);
1897 return;
1898
1899out_close_evt_dir:
1900 closedir(evt_dir);
1901out_close_sys_dir:
1902 closedir(sys_dir);
1903
1904 printf("FATAL: not enough memory to print %s\n",
1905 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1906 if (evt_list)
1907 goto out_free;
Jason Baronf6bdafe2009-07-21 12:20:22 -04001908}
1909
1910/*
Thomas Renninger20c457b2011-01-03 17:50:45 +01001911 * Check whether event is in <debugfs_mount_point>/tracing/events
1912 */
1913
1914int is_valid_tracepoint(const char *event_string)
1915{
1916 DIR *sys_dir, *evt_dir;
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03001917 struct dirent *sys_dirent, *evt_dirent;
Thomas Renninger20c457b2011-01-03 17:50:45 +01001918 char evt_path[MAXPATHLEN];
1919 char dir_path[MAXPATHLEN];
1920
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001921 sys_dir = opendir(tracing_events_path);
Thomas Renninger20c457b2011-01-03 17:50:45 +01001922 if (!sys_dir)
1923 return 0;
1924
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03001925 for_each_subsystem(sys_dir, sys_dirent) {
Thomas Renninger20c457b2011-01-03 17:50:45 +01001926
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02001927 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03001928 sys_dirent->d_name);
Thomas Renninger20c457b2011-01-03 17:50:45 +01001929 evt_dir = opendir(dir_path);
1930 if (!evt_dir)
1931 continue;
1932
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03001933 for_each_event(sys_dirent, evt_dir, evt_dirent) {
Thomas Renninger20c457b2011-01-03 17:50:45 +01001934 snprintf(evt_path, MAXPATHLEN, "%s:%s",
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03001935 sys_dirent->d_name, evt_dirent->d_name);
Thomas Renninger20c457b2011-01-03 17:50:45 +01001936 if (!strcmp(evt_path, event_string)) {
1937 closedir(evt_dir);
1938 closedir(sys_dir);
1939 return 1;
1940 }
1941 }
1942 closedir(evt_dir);
1943 }
1944 closedir(sys_dir);
1945 return 0;
1946}
1947
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001948static bool is_event_supported(u8 type, unsigned config)
1949{
1950 bool ret = true;
Vince Weaver88fee522013-12-30 15:39:45 -05001951 int open_return;
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001952 struct perf_evsel *evsel;
1953 struct perf_event_attr attr = {
1954 .type = type,
1955 .config = config,
1956 .disabled = 1,
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001957 };
1958 struct {
1959 struct thread_map map;
1960 int threads[1];
1961 } tmap = {
1962 .map.nr = 1,
1963 .threads = { 0 },
1964 };
1965
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03001966 evsel = perf_evsel__new(&attr);
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001967 if (evsel) {
Vince Weaver88fee522013-12-30 15:39:45 -05001968 open_return = perf_evsel__open(evsel, NULL, &tmap.map);
1969 ret = open_return >= 0;
1970
1971 if (open_return == -EACCES) {
1972 /*
1973 * This happens if the paranoid value
1974 * /proc/sys/kernel/perf_event_paranoid is set to 2
1975 * Re-run with exclude_kernel set; we don't do that
1976 * by default as some ARM machines do not support it.
1977 *
1978 */
1979 evsel->attr.exclude_kernel = 1;
1980 ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0;
1981 }
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09001982 perf_evsel__delete(evsel);
1983 }
1984
1985 return ret;
1986}
1987
Masami Hiramatsu40218da2016-06-24 18:06:46 +09001988void print_sdt_events(const char *subsys_glob, const char *event_glob,
1989 bool name_only)
1990{
1991 struct probe_cache *pcache;
1992 struct probe_cache_entry *ent;
1993 struct strlist *bidlist, *sdtlist;
1994 struct strlist_config cfg = {.dont_dupstr = true};
1995 struct str_node *nd, *nd2;
1996 char *buf, *path, *ptr = NULL;
1997 bool show_detail = false;
1998 int ret;
1999
2000 sdtlist = strlist__new(NULL, &cfg);
2001 if (!sdtlist) {
2002 pr_debug("Failed to allocate new strlist for SDT\n");
2003 return;
2004 }
2005 bidlist = build_id_cache__list_all(true);
2006 if (!bidlist) {
2007 pr_debug("Failed to get buildids: %d\n", errno);
2008 return;
2009 }
2010 strlist__for_each_entry(nd, bidlist) {
2011 pcache = probe_cache__new(nd->s);
2012 if (!pcache)
2013 continue;
2014 list_for_each_entry(ent, &pcache->entries, node) {
2015 if (!ent->sdt)
2016 continue;
2017 if (subsys_glob &&
2018 !strglobmatch(ent->pev.group, subsys_glob))
2019 continue;
2020 if (event_glob &&
2021 !strglobmatch(ent->pev.event, event_glob))
2022 continue;
2023 ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
2024 ent->pev.event, nd->s);
2025 if (ret > 0)
2026 strlist__add(sdtlist, buf);
2027 }
2028 probe_cache__delete(pcache);
2029 }
2030 strlist__delete(bidlist);
2031
2032 strlist__for_each_entry(nd, sdtlist) {
2033 buf = strchr(nd->s, '@');
2034 if (buf)
2035 *(buf++) = '\0';
2036 if (name_only) {
2037 printf("%s ", nd->s);
2038 continue;
2039 }
2040 nd2 = strlist__next(nd);
2041 if (nd2) {
2042 ptr = strchr(nd2->s, '@');
2043 if (ptr)
2044 *ptr = '\0';
2045 if (strcmp(nd->s, nd2->s) == 0)
2046 show_detail = true;
2047 }
2048 if (show_detail) {
2049 path = build_id_cache__origname(buf);
2050 ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf);
2051 if (ret > 0) {
2052 printf(" %-50s [%s]\n", buf, "SDT event");
2053 free(buf);
2054 }
2055 } else
2056 printf(" %-50s [%s]\n", nd->s, "SDT event");
2057 if (nd2) {
2058 if (strcmp(nd->s, nd2->s) != 0)
2059 show_detail = false;
2060 if (ptr)
2061 *ptr = '@';
2062 }
2063 }
2064 strlist__delete(sdtlist);
2065}
2066
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002067int print_hwcache_events(const char *event_glob, bool name_only)
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002068{
Yunlong Songab0e4802015-02-27 18:21:25 +08002069 unsigned int type, op, i, evt_i = 0, evt_num = 0;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03002070 char name[64];
Yunlong Songab0e4802015-02-27 18:21:25 +08002071 char **evt_list = NULL;
2072 bool evt_num_known = false;
2073
2074restart:
2075 if (evt_num_known) {
2076 evt_list = zalloc(sizeof(char *) * evt_num);
2077 if (!evt_list)
2078 goto out_enomem;
2079 }
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002080
2081 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
2082 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
2083 /* skip invalid cache type */
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03002084 if (!perf_evsel__is_cache_op_valid(type, op))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002085 continue;
2086
2087 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03002088 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
2089 name, sizeof(name));
Ingo Molnar947b4ad2011-04-29 22:52:42 +02002090 if (event_glob != NULL && !strglobmatch(name, event_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002091 continue;
2092
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002093 if (!is_event_supported(PERF_TYPE_HW_CACHE,
2094 type | (op << 8) | (i << 16)))
2095 continue;
2096
Yunlong Songab0e4802015-02-27 18:21:25 +08002097 if (!evt_num_known) {
2098 evt_num++;
2099 continue;
2100 }
2101
2102 evt_list[evt_i] = strdup(name);
2103 if (evt_list[evt_i] == NULL)
2104 goto out_enomem;
2105 evt_i++;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002106 }
2107 }
2108 }
2109
Yunlong Songab0e4802015-02-27 18:21:25 +08002110 if (!evt_num_known) {
2111 evt_num_known = true;
2112 goto restart;
2113 }
2114 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2115 evt_i = 0;
2116 while (evt_i < evt_num) {
2117 if (name_only) {
2118 printf("%s ", evt_list[evt_i++]);
2119 continue;
2120 }
2121 printf(" %-50s [%s]\n", evt_list[evt_i++],
2122 event_type_descriptors[PERF_TYPE_HW_CACHE]);
2123 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002124 if (evt_num && pager_in_use())
Andi Kleendc098b32013-04-20 11:02:29 -07002125 printf("\n");
Yunlong Songab0e4802015-02-27 18:21:25 +08002126
2127out_free:
2128 evt_num = evt_i;
2129 for (evt_i = 0; evt_i < evt_num; evt_i++)
2130 zfree(&evt_list[evt_i]);
2131 zfree(&evt_list);
2132 return evt_num;
2133
2134out_enomem:
2135 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
2136 if (evt_list)
2137 goto out_free;
2138 return evt_num;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002139}
2140
Yunlong Song705750f2015-02-27 18:21:27 +08002141void print_symbol_events(const char *event_glob, unsigned type,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002142 struct event_symbol *syms, unsigned max,
2143 bool name_only)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002144{
Yunlong Songab0e4802015-02-27 18:21:25 +08002145 unsigned int i, evt_i = 0, evt_num = 0;
Ingo Molnar947b4ad2011-04-29 22:52:42 +02002146 char name[MAX_NAME_LEN];
Yunlong Songab0e4802015-02-27 18:21:25 +08002147 char **evt_list = NULL;
2148 bool evt_num_known = false;
2149
2150restart:
2151 if (evt_num_known) {
2152 evt_list = zalloc(sizeof(char *) * evt_num);
2153 if (!evt_list)
2154 goto out_enomem;
2155 syms -= max;
2156 }
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002157
Jiri Olsa1dc127602012-07-04 00:00:44 +02002158 for (i = 0; i < max; i++, syms++) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002159
Arnaldo Carvalho de Meloe37df6c2015-11-27 16:04:58 -03002160 if (event_glob != NULL && syms->symbol != NULL &&
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002161 !(strglobmatch(syms->symbol, event_glob) ||
2162 (syms->alias && strglobmatch(syms->alias, event_glob))))
2163 continue;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002164
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002165 if (!is_event_supported(type, i))
2166 continue;
2167
Yunlong Songab0e4802015-02-27 18:21:25 +08002168 if (!evt_num_known) {
2169 evt_num++;
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002170 continue;
2171 }
2172
Yunlong Songab0e4802015-02-27 18:21:25 +08002173 if (!name_only && strlen(syms->alias))
Ingo Molnar947b4ad2011-04-29 22:52:42 +02002174 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05302175 else
Ingo Molnar947b4ad2011-04-29 22:52:42 +02002176 strncpy(name, syms->symbol, MAX_NAME_LEN);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002177
Yunlong Songab0e4802015-02-27 18:21:25 +08002178 evt_list[evt_i] = strdup(name);
2179 if (evt_list[evt_i] == NULL)
2180 goto out_enomem;
2181 evt_i++;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002182 }
2183
Yunlong Songab0e4802015-02-27 18:21:25 +08002184 if (!evt_num_known) {
2185 evt_num_known = true;
2186 goto restart;
2187 }
2188 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2189 evt_i = 0;
2190 while (evt_i < evt_num) {
2191 if (name_only) {
2192 printf("%s ", evt_list[evt_i++]);
2193 continue;
2194 }
2195 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
2196 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002197 if (evt_num && pager_in_use())
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002198 printf("\n");
Yunlong Songab0e4802015-02-27 18:21:25 +08002199
2200out_free:
2201 evt_num = evt_i;
2202 for (evt_i = 0; evt_i < evt_num; evt_i++)
2203 zfree(&evt_list[evt_i]);
2204 zfree(&evt_list);
2205 return;
2206
2207out_enomem:
2208 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
2209 if (evt_list)
2210 goto out_free;
Jiri Olsa1dc127602012-07-04 00:00:44 +02002211}
2212
2213/*
2214 * Print the help text for the event symbols:
2215 */
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002216void print_events(const char *event_glob, bool name_only)
Jiri Olsa1dc127602012-07-04 00:00:44 +02002217{
Jiri Olsa1dc127602012-07-04 00:00:44 +02002218 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002219 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
Jiri Olsa1dc127602012-07-04 00:00:44 +02002220
2221 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002222 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
Jiri Olsa1dc127602012-07-04 00:00:44 +02002223
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002224 print_hwcache_events(event_glob, name_only);
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002225
Andi Kleendc098b32013-04-20 11:02:29 -07002226 print_pmu_events(event_glob, name_only);
2227
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002228 if (event_glob != NULL)
2229 return;
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +05302230
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002231 if (!name_only) {
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002232 printf(" %-50s [%s]\n",
2233 "rNNN",
2234 event_type_descriptors[PERF_TYPE_RAW]);
2235 printf(" %-50s [%s]\n",
2236 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
2237 event_type_descriptors[PERF_TYPE_RAW]);
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002238 if (pager_in_use())
2239 printf(" (see 'man perf-list' on how to encode it)\n\n");
Thomas Gleixner86847b62009-06-06 12:24:17 +02002240
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002241 printf(" %-50s [%s]\n",
Jacob Shin3741eb92014-05-29 17:26:51 +02002242 "mem:<addr>[/len][:access]",
Liming Wang41bdcb22009-12-29 16:37:07 +08002243 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002244 if (pager_in_use())
2245 printf("\n");
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002246 }
Frederic Weisbecker1b290d62009-11-23 15:42:35 +01002247
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002248 print_tracepoint_events(NULL, NULL, name_only);
Masami Hiramatsu40218da2016-06-24 18:06:46 +09002249
2250 print_sdt_events(NULL, NULL, name_only);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002251}
Jiri Olsa8f707d82012-03-15 20:09:16 +01002252
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002253int parse_events__is_hardcoded_term(struct parse_events_term *term)
Jiri Olsa8f707d82012-03-15 20:09:16 +01002254{
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002255 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002256}
2257
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002258static int new_term(struct parse_events_term **_term, int type_val,
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002259 int type_term, char *config,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002260 char *str, u64 num, int err_term, int err_val)
Jiri Olsa8f707d82012-03-15 20:09:16 +01002261{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002262 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002263
2264 term = zalloc(sizeof(*term));
2265 if (!term)
2266 return -ENOMEM;
2267
2268 INIT_LIST_HEAD(&term->list);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002269 term->type_val = type_val;
2270 term->type_term = type_term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002271 term->config = config;
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002272 term->err_term = err_term;
2273 term->err_val = err_val;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002274
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002275 switch (type_val) {
Jiri Olsa8f707d82012-03-15 20:09:16 +01002276 case PARSE_EVENTS__TERM_TYPE_NUM:
2277 term->val.num = num;
2278 break;
2279 case PARSE_EVENTS__TERM_TYPE_STR:
2280 term->val.str = str;
2281 break;
2282 default:
Adrian Hunter4be8be62013-07-04 16:20:24 +03002283 free(term);
Jiri Olsa8f707d82012-03-15 20:09:16 +01002284 return -EINVAL;
2285 }
2286
2287 *_term = term;
2288 return 0;
2289}
2290
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002291int parse_events_term__num(struct parse_events_term **term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002292 int type_term, char *config, u64 num,
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002293 void *loc_term_, void *loc_val_)
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002294{
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002295 YYLTYPE *loc_term = loc_term_;
2296 YYLTYPE *loc_val = loc_val_;
2297
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002298 return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002299 config, NULL, num,
2300 loc_term ? loc_term->first_column : 0,
2301 loc_val ? loc_val->first_column : 0);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002302}
2303
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002304int parse_events_term__str(struct parse_events_term **term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002305 int type_term, char *config, char *str,
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002306 void *loc_term_, void *loc_val_)
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002307{
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002308 YYLTYPE *loc_term = loc_term_;
2309 YYLTYPE *loc_val = loc_val_;
2310
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002311 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002312 config, str, 0,
2313 loc_term ? loc_term->first_column : 0,
2314 loc_val ? loc_val->first_column : 0);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002315}
2316
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002317int parse_events_term__sym_hw(struct parse_events_term **term,
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02002318 char *config, unsigned idx)
2319{
2320 struct event_symbol *sym;
2321
2322 BUG_ON(idx >= PERF_COUNT_HW_MAX);
2323 sym = &event_symbols_hw[idx];
2324
2325 if (config)
2326 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
2327 PARSE_EVENTS__TERM_TYPE_USER, config,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002328 (char *) sym->symbol, 0, 0, 0);
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02002329 else
2330 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
2331 PARSE_EVENTS__TERM_TYPE_USER,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002332 (char *) "event", (char *) sym->symbol,
2333 0, 0, 0);
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02002334}
2335
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002336int parse_events_term__clone(struct parse_events_term **new,
2337 struct parse_events_term *term)
Zheng Yana6146d52012-06-15 14:31:41 +08002338{
2339 return new_term(new, term->type_val, term->type_term, term->config,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002340 term->val.str, term->val.num,
2341 term->err_term, term->err_val);
Zheng Yana6146d52012-06-15 14:31:41 +08002342}
2343
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002344void parse_events_terms__purge(struct list_head *terms)
Jiri Olsa8f707d82012-03-15 20:09:16 +01002345{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002346 struct parse_events_term *term, *h;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002347
Wang Nana8adfce2016-02-12 16:31:23 -03002348 list_for_each_entry_safe(term, h, terms, list) {
Wang Nan2d055bf2016-02-22 09:10:34 +00002349 if (term->array.nr_ranges)
2350 free(term->array.ranges);
Wang Nana8adfce2016-02-12 16:31:23 -03002351 list_del_init(&term->list);
Jiri Olsa8f707d82012-03-15 20:09:16 +01002352 free(term);
Wang Nana8adfce2016-02-12 16:31:23 -03002353 }
Jiri Olsa8f707d82012-03-15 20:09:16 +01002354}
Jiri Olsab39b8392015-04-22 21:10:16 +02002355
Arnaldo Carvalho de Melo2146afc2016-02-12 17:09:17 -03002356void parse_events_terms__delete(struct list_head *terms)
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002357{
Arnaldo Carvalho de Melo2146afc2016-02-12 17:09:17 -03002358 if (!terms)
2359 return;
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002360 parse_events_terms__purge(terms);
Wang Nand20a5f22016-02-12 17:01:17 -03002361 free(terms);
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002362}
2363
Wang Nan2d055bf2016-02-22 09:10:34 +00002364void parse_events__clear_array(struct parse_events_array *a)
2365{
2366 free(a->ranges);
2367}
2368
Jiri Olsab39b8392015-04-22 21:10:16 +02002369void parse_events_evlist_error(struct parse_events_evlist *data,
2370 int idx, const char *str)
2371{
2372 struct parse_events_error *err = data->error;
2373
Adrian Huntera6ced2b2015-05-19 16:05:44 +03002374 if (!err)
2375 return;
Jiri Olsab39b8392015-04-22 21:10:16 +02002376 err->idx = idx;
2377 err->str = strdup(str);
2378 WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
2379}
He Kuangffeb8832015-09-28 03:52:14 +00002380
Wang Nan17cb5f82016-02-19 11:43:57 +00002381static void config_terms_list(char *buf, size_t buf_sz)
2382{
2383 int i;
2384 bool first = true;
2385
2386 buf[0] = '\0';
2387 for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
2388 const char *name = config_term_names[i];
2389
Wang Nan1669e502016-02-19 11:43:58 +00002390 if (!config_term_avail(i, NULL))
2391 continue;
Wang Nan17cb5f82016-02-19 11:43:57 +00002392 if (!name)
2393 continue;
2394 if (name[0] == '<')
2395 continue;
2396
2397 if (strlen(buf) + strlen(name) + 2 >= buf_sz)
2398 return;
2399
2400 if (!first)
2401 strcat(buf, ",");
2402 else
2403 first = false;
2404 strcat(buf, name);
2405 }
2406}
2407
He Kuangffeb8832015-09-28 03:52:14 +00002408/*
2409 * Return string contains valid config terms of an event.
2410 * @additional_terms: For terms such as PMU sysfs terms.
2411 */
2412char *parse_events_formats_error_string(char *additional_terms)
2413{
2414 char *str;
Wang Nan17cb5f82016-02-19 11:43:57 +00002415 /* "branch_type" is the longest name */
2416 char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
2417 (sizeof("branch_type") - 1)];
He Kuangffeb8832015-09-28 03:52:14 +00002418
Wang Nan17cb5f82016-02-19 11:43:57 +00002419 config_terms_list(static_terms, sizeof(static_terms));
He Kuangffeb8832015-09-28 03:52:14 +00002420 /* valid terms */
2421 if (additional_terms) {
Wang Nan26dee022016-02-19 11:43:52 +00002422 if (asprintf(&str, "valid terms: %s,%s",
2423 additional_terms, static_terms) < 0)
He Kuangffeb8832015-09-28 03:52:14 +00002424 goto fail;
2425 } else {
Wang Nan26dee022016-02-19 11:43:52 +00002426 if (asprintf(&str, "valid terms: %s", static_terms) < 0)
He Kuangffeb8832015-09-28 03:52:14 +00002427 goto fail;
2428 }
2429 return str;
2430
2431fail:
2432 return NULL;
2433}