blob: c71b29ad26ecf646fa49df7c81648420cafb11eb [file] [log] [blame]
Frederic Weisbecker1b290d62009-11-23 15:42:35 +01001#include "../../../include/linux/hw_breakpoint.h"
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002#include "util.h"
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03003#include "../perf.h"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02004#include "evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02005#include "evsel.h"
Ingo Molnar8ad8db32009-05-26 11:10:09 +02006#include "parse-options.h"
7#include "parse-events.h"
8#include "exec_cmd.h"
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -03009#include "string.h"
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -030010#include "symbol.h"
Jason Baron5beeded2009-07-21 14:16:29 -040011#include "cache.h"
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020012#include "header.h"
Clark Williams549104f2009-11-08 09:03:07 -060013#include "debugfs.h"
Jiri Olsa89812fc2012-03-15 20:09:15 +010014#include "parse-events-flex.h"
Jiri Olsa5f537a22012-03-15 20:09:18 +010015#include "pmu.h"
Jiri Olsa89812fc2012-03-15 20:09:15 +010016
17#define MAX_NAME_LEN 100
Ingo Molnar8ad8db32009-05-26 11:10:09 +020018
Ingo Molnar8ad8db32009-05-26 11:10:09 +020019struct event_symbol {
Ingo Molnar83a09442009-08-15 12:26:57 +020020 u8 type;
21 u64 config;
22 const char *symbol;
23 const char *alias;
Ingo Molnar8ad8db32009-05-26 11:10:09 +020024};
25
Jiri Olsa82ba1f22012-05-21 09:12:50 +020026#ifdef PARSER_DEBUG
27extern int parse_events_debug;
28#endif
Jiri Olsa46010ab2012-06-15 14:31:38 +080029int parse_events_parse(void *data);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +020030
Jaswinder Singh Rajput51e26842009-06-22 16:43:14 +053031#define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
32#define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
Ingo Molnar8ad8db32009-05-26 11:10:09 +020033
Ingo Molnara21ca2c2009-06-06 09:58:57 +020034static struct event_symbol event_symbols[] = {
Ingo Molnar129c04c2011-04-29 14:41:28 +020035 { CHW(CPU_CYCLES), "cpu-cycles", "cycles" },
36 { CHW(STALLED_CYCLES_FRONTEND), "stalled-cycles-frontend", "idle-cycles-frontend" },
37 { CHW(STALLED_CYCLES_BACKEND), "stalled-cycles-backend", "idle-cycles-backend" },
38 { CHW(INSTRUCTIONS), "instructions", "" },
39 { CHW(CACHE_REFERENCES), "cache-references", "" },
40 { CHW(CACHE_MISSES), "cache-misses", "" },
41 { CHW(BRANCH_INSTRUCTIONS), "branch-instructions", "branches" },
42 { CHW(BRANCH_MISSES), "branch-misses", "" },
43 { CHW(BUS_CYCLES), "bus-cycles", "" },
Stephane Eranianf1ac18a2011-12-11 00:28:54 +010044 { CHW(REF_CPU_CYCLES), "ref-cycles", "" },
Ingo Molnara21ca2c2009-06-06 09:58:57 +020045
Ingo Molnar129c04c2011-04-29 14:41:28 +020046 { CSW(CPU_CLOCK), "cpu-clock", "" },
47 { CSW(TASK_CLOCK), "task-clock", "" },
48 { CSW(PAGE_FAULTS), "page-faults", "faults" },
49 { CSW(PAGE_FAULTS_MIN), "minor-faults", "" },
50 { CSW(PAGE_FAULTS_MAJ), "major-faults", "" },
51 { CSW(CONTEXT_SWITCHES), "context-switches", "cs" },
52 { CSW(CPU_MIGRATIONS), "cpu-migrations", "migrations" },
53 { CSW(ALIGNMENT_FAULTS), "alignment-faults", "" },
54 { CSW(EMULATION_FAULTS), "emulation-faults", "" },
Ingo Molnar8ad8db32009-05-26 11:10:09 +020055};
56
Ingo Molnarcdd6c482009-09-21 12:02:48 +020057#define __PERF_EVENT_FIELD(config, name) \
58 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
Ingo Molnar52425192009-05-26 09:17:18 +020059
Ingo Molnar1fc570a2011-04-27 05:20:22 +020060#define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020061#define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
Ingo Molnar1fc570a2011-04-27 05:20:22 +020062#define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020063#define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
Ingo Molnar52425192009-05-26 09:17:18 +020064
Ingo Molnard3d1e862011-04-29 13:49:08 +020065static const char *sw_event_names[PERF_COUNT_SW_MAX] = {
Ingo Molnar749141d2011-04-27 04:24:57 +020066 "cpu-clock",
67 "task-clock",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020068 "page-faults",
69 "context-switches",
70 "CPU-migrations",
71 "minor-faults",
72 "major-faults",
Anton Blanchardf7d79862009-10-18 01:09:29 +000073 "alignment-faults",
74 "emulation-faults",
Ingo Molnar52425192009-05-26 09:17:18 +020075};
76
Ingo Molnar8326f442009-06-05 20:22:46 +020077#define MAX_ALIASES 8
78
Jiri Olsa01119192011-07-13 22:58:18 +020079static const char *hw_cache[PERF_COUNT_HW_CACHE_MAX][MAX_ALIASES] = {
Anton Blanchard9590b7b2009-07-06 22:01:31 +100080 { "L1-dcache", "l1-d", "l1d", "L1-data", },
81 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
Jiri Olsa01119192011-07-13 22:58:18 +020082 { "LLC", "L2", },
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +053083 { "dTLB", "d-tlb", "Data-TLB", },
84 { "iTLB", "i-tlb", "Instruction-TLB", },
85 { "branch", "branches", "bpu", "btb", "bpc", },
Jiri Olsa01119192011-07-13 22:58:18 +020086 { "node", },
Ingo Molnar8326f442009-06-05 20:22:46 +020087};
88
Jiri Olsa01119192011-07-13 22:58:18 +020089static const char *hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX][MAX_ALIASES] = {
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +053090 { "load", "loads", "read", },
91 { "store", "stores", "write", },
92 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
Ingo Molnar8326f442009-06-05 20:22:46 +020093};
94
Jiri Olsa01119192011-07-13 22:58:18 +020095static const char *hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
96 [MAX_ALIASES] = {
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +053097 { "refs", "Reference", "ops", "access", },
98 { "misses", "miss", },
Ingo Molnar8326f442009-06-05 20:22:46 +020099};
100
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530101#define C(x) PERF_COUNT_HW_CACHE_##x
102#define CACHE_READ (1 << C(OP_READ))
103#define CACHE_WRITE (1 << C(OP_WRITE))
104#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
105#define COP(x) (1 << x)
106
107/*
108 * cache operartion stat
109 * L1I : Read and prefetch only
110 * ITLB and BPU : Read-only
111 */
112static unsigned long hw_cache_stat[C(MAX)] = {
113 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
114 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
115 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
116 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
117 [C(ITLB)] = (CACHE_READ),
118 [C(BPU)] = (CACHE_READ),
Jiri Olsa01119192011-07-13 22:58:18 +0200119 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530120};
121
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300122#define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400123 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300124 if (sys_dirent.d_type == DT_DIR && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400125 (strcmp(sys_dirent.d_name, ".")) && \
126 (strcmp(sys_dirent.d_name, "..")))
127
Peter Zijlstraae07b632009-08-06 16:48:54 +0200128static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
129{
130 char evt_path[MAXPATHLEN];
131 int fd;
132
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200133 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
Peter Zijlstraae07b632009-08-06 16:48:54 +0200134 sys_dir->d_name, evt_dir->d_name);
135 fd = open(evt_path, O_RDONLY);
136 if (fd < 0)
137 return -EINVAL;
138 close(fd);
139
140 return 0;
141}
142
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300143#define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400144 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300145 if (evt_dirent.d_type == DT_DIR && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400146 (strcmp(evt_dirent.d_name, ".")) && \
Peter Zijlstraae07b632009-08-06 16:48:54 +0200147 (strcmp(evt_dirent.d_name, "..")) && \
148 (!tp_event_has_id(&sys_dirent, &evt_dirent)))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400149
Li Zefan270bbbe2009-09-17 16:34:51 +0800150#define MAX_EVENT_LENGTH 512
Jason Baronf6bdafe2009-07-21 12:20:22 -0400151
Jason Baronf6bdafe2009-07-21 12:20:22 -0400152
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200153struct tracepoint_path *tracepoint_id_to_path(u64 config)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400154{
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200155 struct tracepoint_path *path = NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400156 DIR *sys_dir, *evt_dir;
157 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Stephane Eranian8aa8a7c2012-03-13 16:51:02 +0100158 char id_buf[24];
Eric Dumazet725b1362009-09-24 15:39:09 +0200159 int fd;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400160 u64 id;
161 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +0200162 char dir_path[MAXPATHLEN];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400163
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200164 if (debugfs_valid_mountpoint(tracing_events_path))
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200165 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400166
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200167 sys_dir = opendir(tracing_events_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400168 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200169 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400170
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300171 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200172
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200173 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Eric Dumazet725b1362009-09-24 15:39:09 +0200174 sys_dirent.d_name);
175 evt_dir = opendir(dir_path);
176 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300177 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200178
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300179 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200180
181 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
Jason Baronf6bdafe2009-07-21 12:20:22 -0400182 evt_dirent.d_name);
Eric Dumazet725b1362009-09-24 15:39:09 +0200183 fd = open(evt_path, O_RDONLY);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400184 if (fd < 0)
185 continue;
186 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
187 close(fd);
188 continue;
189 }
190 close(fd);
191 id = atoll(id_buf);
192 if (id == config) {
193 closedir(evt_dir);
194 closedir(sys_dir);
Julia Lawall59b4cae2009-12-06 10:16:30 +0100195 path = zalloc(sizeof(*path));
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200196 path->system = malloc(MAX_EVENT_LENGTH);
197 if (!path->system) {
198 free(path);
199 return NULL;
200 }
201 path->name = malloc(MAX_EVENT_LENGTH);
202 if (!path->name) {
203 free(path->system);
204 free(path);
205 return NULL;
206 }
207 strncpy(path->system, sys_dirent.d_name,
208 MAX_EVENT_LENGTH);
209 strncpy(path->name, evt_dirent.d_name,
210 MAX_EVENT_LENGTH);
211 return path;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400212 }
213 }
214 closedir(evt_dir);
215 }
216
Jason Baronf6bdafe2009-07-21 12:20:22 -0400217 closedir(sys_dir);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200218 return NULL;
219}
220
221#define TP_PATH_LEN (MAX_EVENT_LENGTH * 2 + 1)
222static const char *tracepoint_id_to_name(u64 config)
223{
224 static char buf[TP_PATH_LEN];
225 struct tracepoint_path *path;
226
227 path = tracepoint_id_to_path(config);
228 if (path) {
229 snprintf(buf, TP_PATH_LEN, "%s:%s", path->system, path->name);
230 free(path->name);
231 free(path->system);
232 free(path);
233 } else
234 snprintf(buf, TP_PATH_LEN, "%s:%s", "unknown", "unknown");
235
236 return buf;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400237}
238
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530239static int is_cache_op_valid(u8 cache_type, u8 cache_op)
240{
241 if (hw_cache_stat[cache_type] & COP(cache_op))
242 return 1; /* valid */
243 else
244 return 0; /* invalid */
245}
246
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530247static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result)
248{
249 static char name[50];
250
251 if (cache_result) {
252 sprintf(name, "%s-%s-%s", hw_cache[cache_type][0],
253 hw_cache_op[cache_op][0],
254 hw_cache_result[cache_result][0]);
255 } else {
256 sprintf(name, "%s-%s", hw_cache[cache_type][0],
257 hw_cache_op[cache_op][1]);
258 }
259
260 return name;
261}
262
David Ahern1424dc92011-03-09 22:23:28 -0700263const char *event_type(int type)
264{
265 switch (type) {
266 case PERF_TYPE_HARDWARE:
267 return "hardware";
268
269 case PERF_TYPE_SOFTWARE:
270 return "software";
271
272 case PERF_TYPE_TRACEPOINT:
273 return "tracepoint";
274
275 case PERF_TYPE_HW_CACHE:
276 return "hardware-cache";
277
278 default:
279 break;
280 }
281
282 return "unknown";
283}
284
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200285const char *event_name(struct perf_evsel *evsel)
Ingo Molnar52425192009-05-26 09:17:18 +0200286{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200287 u64 config = evsel->attr.config;
288 int type = evsel->attr.type;
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200289
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300290 if (type == PERF_TYPE_RAW || type == PERF_TYPE_HARDWARE) {
291 /*
292 * XXX minimal fix, see comment on perf_evsen__name, this static buffer
293 * will go away together with event_name in the next devel cycle.
294 */
295 static char bf[128];
296 perf_evsel__name(evsel, bf, sizeof(bf));
297 return bf;
298 }
299
Stephane Eranianf0c55bc2011-02-16 15:10:01 +0200300 if (evsel->name)
301 return evsel->name;
302
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200303 return __event_name(type, config);
304}
305
Ingo Molnar83a09442009-08-15 12:26:57 +0200306const char *__event_name(int type, u64 config)
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200307{
Ingo Molnar52425192009-05-26 09:17:18 +0200308 static char buf[32];
309
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200310 if (type == PERF_TYPE_RAW) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200311 sprintf(buf, "raw 0x%" PRIx64, config);
Ingo Molnar52425192009-05-26 09:17:18 +0200312 return buf;
313 }
314
315 switch (type) {
316 case PERF_TYPE_HARDWARE:
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300317 return __perf_evsel__hw_name(config);
Ingo Molnar52425192009-05-26 09:17:18 +0200318
Ingo Molnar8326f442009-06-05 20:22:46 +0200319 case PERF_TYPE_HW_CACHE: {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000320 u8 cache_type, cache_op, cache_result;
Ingo Molnar8326f442009-06-05 20:22:46 +0200321
322 cache_type = (config >> 0) & 0xff;
323 if (cache_type > PERF_COUNT_HW_CACHE_MAX)
324 return "unknown-ext-hardware-cache-type";
325
326 cache_op = (config >> 8) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200327 if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
328 return "unknown-ext-hardware-cache-op";
Ingo Molnar8326f442009-06-05 20:22:46 +0200329
330 cache_result = (config >> 16) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200331 if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
332 return "unknown-ext-hardware-cache-result";
Ingo Molnar8326f442009-06-05 20:22:46 +0200333
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530334 if (!is_cache_op_valid(cache_type, cache_op))
335 return "invalid-cache";
Ingo Molnar8326f442009-06-05 20:22:46 +0200336
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530337 return event_cache_name(cache_type, cache_op, cache_result);
Ingo Molnar8326f442009-06-05 20:22:46 +0200338 }
339
Ingo Molnar52425192009-05-26 09:17:18 +0200340 case PERF_TYPE_SOFTWARE:
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200341 if (config < PERF_COUNT_SW_MAX && sw_event_names[config])
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200342 return sw_event_names[config];
Ingo Molnar52425192009-05-26 09:17:18 +0200343 return "unknown-software";
344
Jason Baronf6bdafe2009-07-21 12:20:22 -0400345 case PERF_TYPE_TRACEPOINT:
346 return tracepoint_id_to_name(config);
347
Ingo Molnar52425192009-05-26 09:17:18 +0200348 default:
349 break;
350 }
351
352 return "unknown";
353}
354
Jiri Olsab847cbd2012-05-21 09:12:51 +0200355static int add_event(struct list_head **_list, int *idx,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100356 struct perf_event_attr *attr, char *name)
357{
358 struct perf_evsel *evsel;
Jiri Olsab847cbd2012-05-21 09:12:51 +0200359 struct list_head *list = *_list;
360
361 if (!list) {
362 list = malloc(sizeof(*list));
363 if (!list)
364 return -ENOMEM;
365 INIT_LIST_HEAD(list);
366 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100367
368 event_attr_init(attr);
369
370 evsel = perf_evsel__new(attr, (*idx)++);
Jiri Olsab847cbd2012-05-21 09:12:51 +0200371 if (!evsel) {
372 free(list);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100373 return -ENOMEM;
Jiri Olsab847cbd2012-05-21 09:12:51 +0200374 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100375
376 evsel->name = strdup(name);
Jiri Olsab847cbd2012-05-21 09:12:51 +0200377 list_add_tail(&evsel->node, list);
378 *_list = list;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100379 return 0;
380}
381
382static int parse_aliases(char *str, const char *names[][MAX_ALIASES], int size)
Ingo Molnar8326f442009-06-05 20:22:46 +0200383{
384 int i, j;
Paul Mackerras61c45982009-07-01 13:04:34 +1000385 int n, longest = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200386
387 for (i = 0; i < size; i++) {
Paul Mackerras61c45982009-07-01 13:04:34 +1000388 for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
389 n = strlen(names[i][j]);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100390 if (n > longest && !strncasecmp(str, names[i][j], n))
Paul Mackerras61c45982009-07-01 13:04:34 +1000391 longest = n;
392 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100393 if (longest > 0)
Paul Mackerras61c45982009-07-01 13:04:34 +1000394 return i;
Ingo Molnar8326f442009-06-05 20:22:46 +0200395 }
396
Ingo Molnar89536452009-06-06 21:04:17 +0200397 return -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200398}
399
Jiri Olsab847cbd2012-05-21 09:12:51 +0200400int parse_events_add_cache(struct list_head **list, int *idx,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100401 char *type, char *op_result1, char *op_result2)
Ingo Molnar8326f442009-06-05 20:22:46 +0200402{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100403 struct perf_event_attr attr;
404 char name[MAX_NAME_LEN];
Paul Mackerras61c45982009-07-01 13:04:34 +1000405 int cache_type = -1, cache_op = -1, cache_result = -1;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100406 char *op_result[2] = { op_result1, op_result2 };
407 int i, n;
Ingo Molnar8326f442009-06-05 20:22:46 +0200408
Ingo Molnar8326f442009-06-05 20:22:46 +0200409 /*
410 * No fallback - if we cannot get a clear cache type
411 * then bail out:
412 */
Jiri Olsa89812fc2012-03-15 20:09:15 +0100413 cache_type = parse_aliases(type, hw_cache,
414 PERF_COUNT_HW_CACHE_MAX);
Ingo Molnar8326f442009-06-05 20:22:46 +0200415 if (cache_type == -1)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100416 return -EINVAL;
Ingo Molnar8326f442009-06-05 20:22:46 +0200417
Jiri Olsa89812fc2012-03-15 20:09:15 +0100418 n = snprintf(name, MAX_NAME_LEN, "%s", type);
419
420 for (i = 0; (i < 2) && (op_result[i]); i++) {
421 char *str = op_result[i];
422
423 snprintf(name + n, MAX_NAME_LEN - n, "-%s\n", str);
Paul Mackerras61c45982009-07-01 13:04:34 +1000424
425 if (cache_op == -1) {
Jiri Olsa89812fc2012-03-15 20:09:15 +0100426 cache_op = parse_aliases(str, hw_cache_op,
427 PERF_COUNT_HW_CACHE_OP_MAX);
Paul Mackerras61c45982009-07-01 13:04:34 +1000428 if (cache_op >= 0) {
429 if (!is_cache_op_valid(cache_type, cache_op))
Jiri Olsa89812fc2012-03-15 20:09:15 +0100430 return -EINVAL;
Paul Mackerras61c45982009-07-01 13:04:34 +1000431 continue;
432 }
433 }
434
435 if (cache_result == -1) {
Jiri Olsa89812fc2012-03-15 20:09:15 +0100436 cache_result = parse_aliases(str, hw_cache_result,
Paul Mackerras61c45982009-07-01 13:04:34 +1000437 PERF_COUNT_HW_CACHE_RESULT_MAX);
438 if (cache_result >= 0)
439 continue;
440 }
Paul Mackerras61c45982009-07-01 13:04:34 +1000441 }
442
Ingo Molnar8326f442009-06-05 20:22:46 +0200443 /*
444 * Fall back to reads:
445 */
Ingo Molnar89536452009-06-06 21:04:17 +0200446 if (cache_op == -1)
447 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
Ingo Molnar8326f442009-06-05 20:22:46 +0200448
Ingo Molnar8326f442009-06-05 20:22:46 +0200449 /*
450 * Fall back to accesses:
451 */
452 if (cache_result == -1)
453 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
454
Jiri Olsa89812fc2012-03-15 20:09:15 +0100455 memset(&attr, 0, sizeof(attr));
456 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
457 attr.type = PERF_TYPE_HW_CACHE;
458 return add_event(list, idx, &attr, name);
Ingo Molnar8326f442009-06-05 20:22:46 +0200459}
460
Jiri Olsab847cbd2012-05-21 09:12:51 +0200461static int add_tracepoint(struct list_head **list, int *idx,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100462 char *sys_name, char *evt_name)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200463{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100464 struct perf_event_attr attr;
465 char name[MAX_NAME_LEN];
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200466 char evt_path[MAXPATHLEN];
467 char id_buf[4];
468 u64 id;
469 int fd;
470
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200471 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200472 sys_name, evt_name);
473
474 fd = open(evt_path, O_RDONLY);
475 if (fd < 0)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100476 return -1;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200477
478 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
479 close(fd);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100480 return -1;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200481 }
482
483 close(fd);
484 id = atoll(id_buf);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200485
Jiri Olsa89812fc2012-03-15 20:09:15 +0100486 memset(&attr, 0, sizeof(attr));
487 attr.config = id;
488 attr.type = PERF_TYPE_TRACEPOINT;
489 attr.sample_type |= PERF_SAMPLE_RAW;
490 attr.sample_type |= PERF_SAMPLE_TIME;
491 attr.sample_type |= PERF_SAMPLE_CPU;
492 attr.sample_period = 1;
Stephane Eranian5710fca2010-04-21 18:06:01 +0200493
Jiri Olsa89812fc2012-03-15 20:09:15 +0100494 snprintf(name, MAX_NAME_LEN, "%s:%s", sys_name, evt_name);
495 return add_event(list, idx, &attr, name);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200496}
497
Jiri Olsab847cbd2012-05-21 09:12:51 +0200498static int add_tracepoint_multi(struct list_head **list, int *idx,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100499 char *sys_name, char *evt_name)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200500{
501 char evt_path[MAXPATHLEN];
502 struct dirent *evt_ent;
503 DIR *evt_dir;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100504 int ret = 0;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200505
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200506 snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200507 evt_dir = opendir(evt_path);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200508 if (!evt_dir) {
509 perror("Can't open event dir");
Jiri Olsa89812fc2012-03-15 20:09:15 +0100510 return -1;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200511 }
512
Jiri Olsa89812fc2012-03-15 20:09:15 +0100513 while (!ret && (evt_ent = readdir(evt_dir))) {
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200514 if (!strcmp(evt_ent->d_name, ".")
515 || !strcmp(evt_ent->d_name, "..")
516 || !strcmp(evt_ent->d_name, "enable")
517 || !strcmp(evt_ent->d_name, "filter"))
518 continue;
519
Jiri Olsa89812fc2012-03-15 20:09:15 +0100520 if (!strglobmatch(evt_ent->d_name, evt_name))
Masami Hiramatsufb1d2ed2010-01-05 17:47:17 -0500521 continue;
522
Jiri Olsa89812fc2012-03-15 20:09:15 +0100523 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200524 }
525
Jiri Olsa89812fc2012-03-15 20:09:15 +0100526 return ret;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200527}
528
Jiri Olsab847cbd2012-05-21 09:12:51 +0200529int parse_events_add_tracepoint(struct list_head **list, int *idx,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100530 char *sys, char *event)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400531{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100532 int ret;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400533
Jiri Olsa89812fc2012-03-15 20:09:15 +0100534 ret = debugfs_valid_mountpoint(tracing_events_path);
535 if (ret)
536 return ret;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400537
Jiri Olsa89812fc2012-03-15 20:09:15 +0100538 return strpbrk(event, "*?") ?
539 add_tracepoint_multi(list, idx, sys, event) :
540 add_tracepoint(list, idx, sys, event);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400541}
542
Jiri Olsa89812fc2012-03-15 20:09:15 +0100543static int
544parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100545{
546 int i;
547
548 for (i = 0; i < 3; i++) {
Jiri Olsa89812fc2012-03-15 20:09:15 +0100549 if (!type || !type[i])
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100550 break;
551
552 switch (type[i]) {
553 case 'r':
554 attr->bp_type |= HW_BREAKPOINT_R;
555 break;
556 case 'w':
557 attr->bp_type |= HW_BREAKPOINT_W;
558 break;
559 case 'x':
560 attr->bp_type |= HW_BREAKPOINT_X;
561 break;
562 default:
Jiri Olsa89812fc2012-03-15 20:09:15 +0100563 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100564 }
565 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100566
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100567 if (!attr->bp_type) /* Default */
568 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
569
Jiri Olsa89812fc2012-03-15 20:09:15 +0100570 return 0;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100571}
572
Jiri Olsab847cbd2012-05-21 09:12:51 +0200573int parse_events_add_breakpoint(struct list_head **list, int *idx,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100574 void *ptr, char *type)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100575{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100576 struct perf_event_attr attr;
577 char name[MAX_NAME_LEN];
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100578
Jiri Olsa89812fc2012-03-15 20:09:15 +0100579 memset(&attr, 0, sizeof(attr));
Jiri Olsa9fafd982012-03-20 19:15:39 +0100580 attr.bp_addr = (unsigned long) ptr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100581
Jiri Olsa89812fc2012-03-15 20:09:15 +0100582 if (parse_breakpoint_type(type, &attr))
583 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100584
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200585 /*
586 * We should find a nice way to override the access length
587 * Provide some defaults for now
588 */
Jiri Olsa89812fc2012-03-15 20:09:15 +0100589 if (attr.bp_type == HW_BREAKPOINT_X)
590 attr.bp_len = sizeof(long);
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200591 else
Jiri Olsa89812fc2012-03-15 20:09:15 +0100592 attr.bp_len = HW_BREAKPOINT_LEN_4;
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200593
Jiri Olsa89812fc2012-03-15 20:09:15 +0100594 attr.type = PERF_TYPE_BREAKPOINT;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100595
Jiri Olsa89812fc2012-03-15 20:09:15 +0100596 snprintf(name, MAX_NAME_LEN, "mem:%p:%s", ptr, type ? type : "rw");
597 return add_event(list, idx, &attr, name);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100598}
599
Jiri Olsa8f707d82012-03-15 20:09:16 +0100600static int config_term(struct perf_event_attr *attr,
601 struct parse_events__term *term)
602{
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200603#define CHECK_TYPE_VAL(type) \
604do { \
605 if (PARSE_EVENTS__TERM_TYPE_ ## type != term->type_val) \
606 return -EINVAL; \
607} while (0)
608
609 switch (term->type_term) {
Jiri Olsa8f707d82012-03-15 20:09:16 +0100610 case PARSE_EVENTS__TERM_TYPE_CONFIG:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200611 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100612 attr->config = term->val.num;
613 break;
614 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200615 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100616 attr->config1 = term->val.num;
617 break;
618 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200619 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100620 attr->config2 = term->val.num;
621 break;
622 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200623 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100624 attr->sample_period = term->val.num;
625 break;
626 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
627 /*
628 * TODO uncomment when the field is available
629 * attr->branch_sample_type = term->val.num;
630 */
631 break;
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200632 case PARSE_EVENTS__TERM_TYPE_NAME:
633 CHECK_TYPE_VAL(STR);
634 break;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100635 default:
636 return -EINVAL;
637 }
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200638
Jiri Olsa8f707d82012-03-15 20:09:16 +0100639 return 0;
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200640#undef CHECK_TYPE_VAL
Jiri Olsa8f707d82012-03-15 20:09:16 +0100641}
642
643static int config_attr(struct perf_event_attr *attr,
644 struct list_head *head, int fail)
645{
646 struct parse_events__term *term;
647
648 list_for_each_entry(term, head, list)
649 if (config_term(attr, term) && fail)
650 return -EINVAL;
651
652 return 0;
653}
654
Jiri Olsab847cbd2012-05-21 09:12:51 +0200655int parse_events_add_numeric(struct list_head **list, int *idx,
Jiri Olsa8f707d82012-03-15 20:09:16 +0100656 unsigned long type, unsigned long config,
657 struct list_head *head_config)
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530658{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100659 struct perf_event_attr attr;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530660
Jiri Olsa89812fc2012-03-15 20:09:15 +0100661 memset(&attr, 0, sizeof(attr));
662 attr.type = type;
663 attr.config = config;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100664
665 if (head_config &&
666 config_attr(&attr, head_config, 1))
667 return -EINVAL;
668
Jiri Olsa89812fc2012-03-15 20:09:15 +0100669 return add_event(list, idx, &attr,
670 (char *) __event_name(type, config));
Paul Mackerras61c45982009-07-01 13:04:34 +1000671}
672
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200673static int parse_events__is_name_term(struct parse_events__term *term)
674{
675 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
676}
677
678static char *pmu_event_name(struct perf_event_attr *attr,
679 struct list_head *head_terms)
680{
681 struct parse_events__term *term;
682
683 list_for_each_entry(term, head_terms, list)
684 if (parse_events__is_name_term(term))
685 return term->val.str;
686
687 return (char *) __event_name(PERF_TYPE_RAW, attr->config);
688}
689
Jiri Olsab847cbd2012-05-21 09:12:51 +0200690int parse_events_add_pmu(struct list_head **list, int *idx,
Jiri Olsa5f537a22012-03-15 20:09:18 +0100691 char *name, struct list_head *head_config)
692{
693 struct perf_event_attr attr;
694 struct perf_pmu *pmu;
695
696 pmu = perf_pmu__find(name);
697 if (!pmu)
698 return -EINVAL;
699
700 memset(&attr, 0, sizeof(attr));
701
702 /*
703 * Configure hardcoded terms first, no need to check
704 * return value when called with fail == 0 ;)
705 */
706 config_attr(&attr, head_config, 0);
707
708 if (perf_pmu__config(pmu, &attr, head_config))
709 return -EINVAL;
710
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200711 return add_event(list, idx, &attr,
712 pmu_event_name(&attr, head_config));
Jiri Olsa5f537a22012-03-15 20:09:18 +0100713}
714
Jiri Olsa5d7be902012-03-20 19:15:40 +0100715void parse_events_update_lists(struct list_head *list_event,
716 struct list_head *list_all)
717{
718 /*
719 * Called for single event definition. Update the
720 * 'all event' list, and reinit the 'signle event'
721 * list, for next event definition.
722 */
723 list_splice_tail(list_event, list_all);
Jiri Olsab847cbd2012-05-21 09:12:51 +0200724 free(list_event);
Jiri Olsa5d7be902012-03-20 19:15:40 +0100725}
726
Jiri Olsa89812fc2012-03-15 20:09:15 +0100727int parse_events_modifier(struct list_head *list, char *str)
Paul Mackerras61c45982009-07-01 13:04:34 +1000728{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100729 struct perf_evsel *evsel;
Joerg Roedel99320cc2012-01-04 17:54:19 +0100730 int exclude = 0, exclude_GH = 0;
731 int eu = 0, ek = 0, eh = 0, eH = 0, eG = 0, precise = 0;
Paul Mackerras61c45982009-07-01 13:04:34 +1000732
Jiri Olsa89812fc2012-03-15 20:09:15 +0100733 if (str == NULL)
Paul Mackerras61c45982009-07-01 13:04:34 +1000734 return 0;
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200735
Paul Mackerras61c45982009-07-01 13:04:34 +1000736 while (*str) {
Peter Zijlstraab608342010-04-08 23:03:20 +0200737 if (*str == 'u') {
738 if (!exclude)
739 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000740 eu = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200741 } else if (*str == 'k') {
742 if (!exclude)
743 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000744 ek = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200745 } else if (*str == 'h') {
746 if (!exclude)
747 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000748 eh = 0;
Joerg Roedel99320cc2012-01-04 17:54:19 +0100749 } else if (*str == 'G') {
750 if (!exclude_GH)
751 exclude_GH = eG = eH = 1;
752 eG = 0;
753 } else if (*str == 'H') {
754 if (!exclude_GH)
755 exclude_GH = eG = eH = 1;
756 eH = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200757 } else if (*str == 'p') {
758 precise++;
759 } else
Paul Mackerras61c45982009-07-01 13:04:34 +1000760 break;
Peter Zijlstraab608342010-04-08 23:03:20 +0200761
Paul Mackerras61c45982009-07-01 13:04:34 +1000762 ++str;
763 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200764
Jiri Olsa89812fc2012-03-15 20:09:15 +0100765 /*
766 * precise ip:
767 *
768 * 0 - SAMPLE_IP can have arbitrary skid
769 * 1 - SAMPLE_IP must have constant skid
770 * 2 - SAMPLE_IP requested to have 0 skid
771 * 3 - SAMPLE_IP must have 0 skid
772 *
773 * See also PERF_RECORD_MISC_EXACT_IP
774 */
775 if (precise > 3)
776 return -EINVAL;
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200777
Jiri Olsa89812fc2012-03-15 20:09:15 +0100778 list_for_each_entry(evsel, list, node) {
779 evsel->attr.exclude_user = eu;
780 evsel->attr.exclude_kernel = ek;
781 evsel->attr.exclude_hv = eh;
782 evsel->attr.precise_ip = precise;
783 evsel->attr.exclude_host = eH;
784 evsel->attr.exclude_guest = eG;
785 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200786
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530787 return 0;
788}
789
Jiri Olsa89812fc2012-03-15 20:09:15 +0100790int parse_events(struct perf_evlist *evlist, const char *str, int unset __used)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200791{
Jiri Olsa46010ab2012-06-15 14:31:38 +0800792 struct parse_events_data__events data = {
793 .list = LIST_HEAD_INIT(data.list),
794 .idx = evlist->nr_entries,
795 };
Jiri Olsa89812fc2012-03-15 20:09:15 +0100796 YY_BUFFER_STATE buffer;
Jiri Olsa46010ab2012-06-15 14:31:38 +0800797 int ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200798
Jiri Olsa89812fc2012-03-15 20:09:15 +0100799 buffer = parse_events__scan_string(str);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200800
Jiri Olsa82ba1f22012-05-21 09:12:50 +0200801#ifdef PARSER_DEBUG
802 parse_events_debug = 1;
803#endif
Jiri Olsa46010ab2012-06-15 14:31:38 +0800804 ret = parse_events_parse(&data);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200805
Jiri Olsa89812fc2012-03-15 20:09:15 +0100806 parse_events__flush_buffer(buffer);
807 parse_events__delete_buffer(buffer);
Jiri Olsa08d2f762012-05-21 09:12:52 +0200808 parse_events_lex_destroy();
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200809
Jiri Olsa89812fc2012-03-15 20:09:15 +0100810 if (!ret) {
Jiri Olsa46010ab2012-06-15 14:31:38 +0800811 int entries = data.idx - evlist->nr_entries;
812 perf_evlist__splice_list_tail(evlist, &data.list, entries);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100813 return 0;
814 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200815
Jiri Olsa5d7be902012-03-20 19:15:40 +0100816 /*
817 * There are 2 users - builtin-record and builtin-test objects.
818 * Both call perf_evlist__delete in case of error, so we dont
819 * need to bother.
820 */
Jiri Olsa89812fc2012-03-15 20:09:15 +0100821 fprintf(stderr, "invalid or unsupported event: '%s'\n", str);
Marti Raudsepp85df6f62009-10-27 00:33:04 +0000822 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200823 return ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200824}
825
Jiri Olsaf120f9d2011-07-14 11:25:32 +0200826int parse_events_option(const struct option *opt, const char *str,
827 int unset __used)
828{
829 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
830 return parse_events(evlist, str, unset);
831}
832
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200833int parse_filter(const struct option *opt, const char *str,
Li Zefanc171b552009-10-15 11:22:07 +0800834 int unset __used)
835{
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200836 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200837 struct perf_evsel *last = NULL;
Li Zefanc171b552009-10-15 11:22:07 +0800838
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200839 if (evlist->nr_entries > 0)
840 last = list_entry(evlist->entries.prev, struct perf_evsel, node);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200841
842 if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
Li Zefanc171b552009-10-15 11:22:07 +0800843 fprintf(stderr,
844 "-F option should follow a -e tracepoint option\n");
845 return -1;
846 }
847
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200848 last->filter = strdup(str);
849 if (last->filter == NULL) {
Li Zefanc171b552009-10-15 11:22:07 +0800850 fprintf(stderr, "not enough memory to hold filter string\n");
851 return -1;
852 }
Li Zefanc171b552009-10-15 11:22:07 +0800853
854 return 0;
855}
856
Thomas Gleixner86847b62009-06-06 12:24:17 +0200857static const char * const event_type_descriptors[] = {
Thomas Gleixner86847b62009-06-06 12:24:17 +0200858 "Hardware event",
859 "Software event",
860 "Tracepoint event",
861 "Hardware cache event",
Liming Wang41bdcb22009-12-29 16:37:07 +0800862 "Raw hardware event descriptor",
863 "Hardware breakpoint",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200864};
865
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200866/*
Jason Baronf6bdafe2009-07-21 12:20:22 -0400867 * Print the events from <debugfs_mount_point>/tracing/events
868 */
869
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200870void print_tracepoint_events(const char *subsys_glob, const char *event_glob)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400871{
872 DIR *sys_dir, *evt_dir;
873 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400874 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +0200875 char dir_path[MAXPATHLEN];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400876
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200877 if (debugfs_valid_mountpoint(tracing_events_path))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400878 return;
879
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200880 sys_dir = opendir(tracing_events_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400881 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200882 return;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400883
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300884 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200885 if (subsys_glob != NULL &&
886 !strglobmatch(sys_dirent.d_name, subsys_glob))
887 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200888
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200889 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Eric Dumazet725b1362009-09-24 15:39:09 +0200890 sys_dirent.d_name);
891 evt_dir = opendir(dir_path);
892 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300893 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200894
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300895 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200896 if (event_glob != NULL &&
897 !strglobmatch(evt_dirent.d_name, event_glob))
898 continue;
899
Jason Baronf6bdafe2009-07-21 12:20:22 -0400900 snprintf(evt_path, MAXPATHLEN, "%s:%s",
901 sys_dirent.d_name, evt_dirent.d_name);
Ingo Molnar947b4ad2011-04-29 22:52:42 +0200902 printf(" %-50s [%s]\n", evt_path,
Liming Wang41bdcb22009-12-29 16:37:07 +0800903 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400904 }
905 closedir(evt_dir);
906 }
Jason Baronf6bdafe2009-07-21 12:20:22 -0400907 closedir(sys_dir);
908}
909
910/*
Thomas Renninger20c457b2011-01-03 17:50:45 +0100911 * Check whether event is in <debugfs_mount_point>/tracing/events
912 */
913
914int is_valid_tracepoint(const char *event_string)
915{
916 DIR *sys_dir, *evt_dir;
917 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
918 char evt_path[MAXPATHLEN];
919 char dir_path[MAXPATHLEN];
920
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200921 if (debugfs_valid_mountpoint(tracing_events_path))
Thomas Renninger20c457b2011-01-03 17:50:45 +0100922 return 0;
923
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200924 sys_dir = opendir(tracing_events_path);
Thomas Renninger20c457b2011-01-03 17:50:45 +0100925 if (!sys_dir)
926 return 0;
927
928 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
929
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200930 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Thomas Renninger20c457b2011-01-03 17:50:45 +0100931 sys_dirent.d_name);
932 evt_dir = opendir(dir_path);
933 if (!evt_dir)
934 continue;
935
936 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
937 snprintf(evt_path, MAXPATHLEN, "%s:%s",
938 sys_dirent.d_name, evt_dirent.d_name);
939 if (!strcmp(evt_path, event_string)) {
940 closedir(evt_dir);
941 closedir(sys_dir);
942 return 1;
943 }
944 }
945 closedir(evt_dir);
946 }
947 closedir(sys_dir);
948 return 0;
949}
950
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200951void print_events_type(u8 type)
952{
953 struct event_symbol *syms = event_symbols;
954 unsigned int i;
955 char name[64];
956
957 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
958 if (type != syms->type)
959 continue;
960
961 if (strlen(syms->alias))
962 snprintf(name, sizeof(name), "%s OR %s",
963 syms->symbol, syms->alias);
964 else
965 snprintf(name, sizeof(name), "%s", syms->symbol);
966
Ingo Molnar947b4ad2011-04-29 22:52:42 +0200967 printf(" %-50s [%s]\n", name,
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200968 event_type_descriptors[type]);
969 }
970}
971
972int print_hwcache_events(const char *event_glob)
973{
974 unsigned int type, op, i, printed = 0;
975
976 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
977 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
978 /* skip invalid cache type */
979 if (!is_cache_op_valid(type, op))
980 continue;
981
982 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
983 char *name = event_cache_name(type, op, i);
984
Ingo Molnar947b4ad2011-04-29 22:52:42 +0200985 if (event_glob != NULL && !strglobmatch(name, event_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200986 continue;
987
Ingo Molnar947b4ad2011-04-29 22:52:42 +0200988 printf(" %-50s [%s]\n", name,
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200989 event_type_descriptors[PERF_TYPE_HW_CACHE]);
990 ++printed;
991 }
992 }
993 }
994
995 return printed;
996}
997
Thomas Renninger20c457b2011-01-03 17:50:45 +0100998/*
Thomas Gleixner86847b62009-06-06 12:24:17 +0200999 * Print the help text for the event symbols:
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001000 */
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001001void print_events(const char *event_glob)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001002{
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001003 unsigned int i, type, prev_type = -1, printed = 0, ntypes_printed = 0;
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001004 struct event_symbol *syms = event_symbols;
1005 char name[MAX_NAME_LEN];
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001006
Marti Raudsepp689d3012009-10-27 00:33:05 +00001007 printf("\n");
1008 printf("List of pre-defined events (to be used in -e):\n");
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001009
Thomas Gleixner86847b62009-06-06 12:24:17 +02001010 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
Liming Wang41bdcb22009-12-29 16:37:07 +08001011 type = syms->type;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001012
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001013 if (type != prev_type && printed) {
Marti Raudsepp689d3012009-10-27 00:33:05 +00001014 printf("\n");
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001015 printed = 0;
1016 ntypes_printed++;
1017 }
1018
1019 if (event_glob != NULL &&
1020 !(strglobmatch(syms->symbol, event_glob) ||
1021 (syms->alias && strglobmatch(syms->alias, event_glob))))
1022 continue;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001023
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301024 if (strlen(syms->alias))
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001025 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301026 else
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001027 strncpy(name, syms->symbol, MAX_NAME_LEN);
1028 printf(" %-50s [%s]\n", name,
Thomas Gleixner86847b62009-06-06 12:24:17 +02001029 event_type_descriptors[type]);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001030
Thomas Gleixner86847b62009-06-06 12:24:17 +02001031 prev_type = type;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001032 ++printed;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001033 }
1034
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001035 if (ntypes_printed) {
1036 printed = 0;
1037 printf("\n");
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +05301038 }
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001039 print_hwcache_events(event_glob);
1040
1041 if (event_glob != NULL)
1042 return;
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +05301043
Marti Raudsepp689d3012009-10-27 00:33:05 +00001044 printf("\n");
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001045 printf(" %-50s [%s]\n",
Jiri Olsa5f537a22012-03-15 20:09:18 +01001046 "rNNN",
Arnaldo Carvalho de Melo1cf4a062010-05-07 14:07:05 -03001047 event_type_descriptors[PERF_TYPE_RAW]);
Jiri Olsa5f537a22012-03-15 20:09:18 +01001048 printf(" %-50s [%s]\n",
1049 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
1050 event_type_descriptors[PERF_TYPE_RAW]);
1051 printf(" (see 'perf list --help' on how to encode it)\n");
Marti Raudsepp689d3012009-10-27 00:33:05 +00001052 printf("\n");
Thomas Gleixner86847b62009-06-06 12:24:17 +02001053
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001054 printf(" %-50s [%s]\n",
Liming Wang41bdcb22009-12-29 16:37:07 +08001055 "mem:<addr>[:access]",
1056 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +01001057 printf("\n");
1058
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001059 print_tracepoint_events(NULL, NULL);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001060}
Jiri Olsa8f707d82012-03-15 20:09:16 +01001061
1062int parse_events__is_hardcoded_term(struct parse_events__term *term)
1063{
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001064 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001065}
1066
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001067static int new_term(struct parse_events__term **_term, int type_val,
1068 int type_term, char *config,
1069 char *str, long num)
Jiri Olsa8f707d82012-03-15 20:09:16 +01001070{
1071 struct parse_events__term *term;
1072
1073 term = zalloc(sizeof(*term));
1074 if (!term)
1075 return -ENOMEM;
1076
1077 INIT_LIST_HEAD(&term->list);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001078 term->type_val = type_val;
1079 term->type_term = type_term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001080 term->config = config;
1081
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001082 switch (type_val) {
Jiri Olsa8f707d82012-03-15 20:09:16 +01001083 case PARSE_EVENTS__TERM_TYPE_NUM:
1084 term->val.num = num;
1085 break;
1086 case PARSE_EVENTS__TERM_TYPE_STR:
1087 term->val.str = str;
1088 break;
1089 default:
1090 return -EINVAL;
1091 }
1092
1093 *_term = term;
1094 return 0;
1095}
1096
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001097int parse_events__term_num(struct parse_events__term **term,
1098 int type_term, char *config, long num)
1099{
1100 return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
1101 config, NULL, num);
1102}
1103
1104int parse_events__term_str(struct parse_events__term **term,
1105 int type_term, char *config, char *str)
1106{
1107 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
1108 config, str, 0);
1109}
1110
Jiri Olsa8f707d82012-03-15 20:09:16 +01001111void parse_events__free_terms(struct list_head *terms)
1112{
1113 struct parse_events__term *term, *h;
1114
1115 list_for_each_entry_safe(term, h, terms, list)
1116 free(term);
1117
1118 free(terms);
1119}