blob: d3086cecd2dde23114cbc791aad22666a51f2ef6 [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"
Ingo Molnar8ad8db32009-05-26 11:10:09 +020014
Ingo Molnar8ad8db32009-05-26 11:10:09 +020015struct event_symbol {
Ingo Molnar83a09442009-08-15 12:26:57 +020016 u8 type;
17 u64 config;
18 const char *symbol;
19 const char *alias;
Ingo Molnar8ad8db32009-05-26 11:10:09 +020020};
21
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +020022enum event_result {
23 EVT_FAILED,
24 EVT_HANDLED,
25 EVT_HANDLED_ALL
26};
27
Jason Baron5beeded2009-07-21 14:16:29 -040028char debugfs_path[MAXPATHLEN];
29
Jaswinder Singh Rajput51e26842009-06-22 16:43:14 +053030#define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
31#define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
Ingo Molnar8ad8db32009-05-26 11:10:09 +020032
Ingo Molnara21ca2c2009-06-06 09:58:57 +020033static struct event_symbol event_symbols[] = {
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +053034 { CHW(CPU_CYCLES), "cpu-cycles", "cycles" },
35 { CHW(INSTRUCTIONS), "instructions", "" },
36 { CHW(CACHE_REFERENCES), "cache-references", "" },
37 { CHW(CACHE_MISSES), "cache-misses", "" },
38 { CHW(BRANCH_INSTRUCTIONS), "branch-instructions", "branches" },
39 { CHW(BRANCH_MISSES), "branch-misses", "" },
40 { CHW(BUS_CYCLES), "bus-cycles", "" },
Ingo Molnara21ca2c2009-06-06 09:58:57 +020041
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +053042 { CSW(CPU_CLOCK), "cpu-clock", "" },
43 { CSW(TASK_CLOCK), "task-clock", "" },
Jaswinder Singh Rajputc0c22db2009-06-22 20:47:26 +053044 { CSW(PAGE_FAULTS), "page-faults", "faults" },
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +053045 { CSW(PAGE_FAULTS_MIN), "minor-faults", "" },
46 { CSW(PAGE_FAULTS_MAJ), "major-faults", "" },
47 { CSW(CONTEXT_SWITCHES), "context-switches", "cs" },
48 { CSW(CPU_MIGRATIONS), "cpu-migrations", "migrations" },
Anton Blanchardf7d79862009-10-18 01:09:29 +000049 { CSW(ALIGNMENT_FAULTS), "alignment-faults", "" },
50 { CSW(EMULATION_FAULTS), "emulation-faults", "" },
Ingo Molnar8ad8db32009-05-26 11:10:09 +020051};
52
Ingo Molnarcdd6c482009-09-21 12:02:48 +020053#define __PERF_EVENT_FIELD(config, name) \
54 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
Ingo Molnar52425192009-05-26 09:17:18 +020055
Ingo Molnarcdd6c482009-09-21 12:02:48 +020056#define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
57#define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
58#define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
59#define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
Ingo Molnar52425192009-05-26 09:17:18 +020060
Ingo Molnar83a09442009-08-15 12:26:57 +020061static const char *hw_event_names[] = {
Ingo Molnar8faf3b52009-06-06 13:58:12 +020062 "cycles",
Ingo Molnar52425192009-05-26 09:17:18 +020063 "instructions",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020064 "cache-references",
65 "cache-misses",
Ingo Molnar52425192009-05-26 09:17:18 +020066 "branches",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020067 "branch-misses",
68 "bus-cycles",
Ingo Molnar52425192009-05-26 09:17:18 +020069};
70
Ingo Molnar83a09442009-08-15 12:26:57 +020071static const char *sw_event_names[] = {
Ingo Molnar44175b62009-06-13 13:35:00 +020072 "cpu-clock-msecs",
73 "task-clock-msecs",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020074 "page-faults",
75 "context-switches",
76 "CPU-migrations",
77 "minor-faults",
78 "major-faults",
Anton Blanchardf7d79862009-10-18 01:09:29 +000079 "alignment-faults",
80 "emulation-faults",
Ingo Molnar52425192009-05-26 09:17:18 +020081};
82
Ingo Molnar8326f442009-06-05 20:22:46 +020083#define MAX_ALIASES 8
84
Ingo Molnar83a09442009-08-15 12:26:57 +020085static const char *hw_cache[][MAX_ALIASES] = {
Anton Blanchard9590b7b2009-07-06 22:01:31 +100086 { "L1-dcache", "l1-d", "l1d", "L1-data", },
87 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +053088 { "LLC", "L2" },
89 { "dTLB", "d-tlb", "Data-TLB", },
90 { "iTLB", "i-tlb", "Instruction-TLB", },
91 { "branch", "branches", "bpu", "btb", "bpc", },
Ingo Molnar8326f442009-06-05 20:22:46 +020092};
93
Ingo Molnar83a09442009-08-15 12:26:57 +020094static const char *hw_cache_op[][MAX_ALIASES] = {
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +053095 { "load", "loads", "read", },
96 { "store", "stores", "write", },
97 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
Ingo Molnar8326f442009-06-05 20:22:46 +020098};
99
Ingo Molnar83a09442009-08-15 12:26:57 +0200100static const char *hw_cache_result[][MAX_ALIASES] = {
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530101 { "refs", "Reference", "ops", "access", },
102 { "misses", "miss", },
Ingo Molnar8326f442009-06-05 20:22:46 +0200103};
104
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530105#define C(x) PERF_COUNT_HW_CACHE_##x
106#define CACHE_READ (1 << C(OP_READ))
107#define CACHE_WRITE (1 << C(OP_WRITE))
108#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
109#define COP(x) (1 << x)
110
111/*
112 * cache operartion stat
113 * L1I : Read and prefetch only
114 * ITLB and BPU : Read-only
115 */
116static unsigned long hw_cache_stat[C(MAX)] = {
117 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
118 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
119 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
120 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
121 [C(ITLB)] = (CACHE_READ),
122 [C(BPU)] = (CACHE_READ),
123};
124
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300125#define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400126 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300127 if (sys_dirent.d_type == DT_DIR && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400128 (strcmp(sys_dirent.d_name, ".")) && \
129 (strcmp(sys_dirent.d_name, "..")))
130
Peter Zijlstraae07b632009-08-06 16:48:54 +0200131static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
132{
133 char evt_path[MAXPATHLEN];
134 int fd;
135
136 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
137 sys_dir->d_name, evt_dir->d_name);
138 fd = open(evt_path, O_RDONLY);
139 if (fd < 0)
140 return -EINVAL;
141 close(fd);
142
143 return 0;
144}
145
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300146#define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400147 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300148 if (evt_dirent.d_type == DT_DIR && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400149 (strcmp(evt_dirent.d_name, ".")) && \
Peter Zijlstraae07b632009-08-06 16:48:54 +0200150 (strcmp(evt_dirent.d_name, "..")) && \
151 (!tp_event_has_id(&sys_dirent, &evt_dirent)))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400152
Li Zefan270bbbe2009-09-17 16:34:51 +0800153#define MAX_EVENT_LENGTH 512
Jason Baronf6bdafe2009-07-21 12:20:22 -0400154
Jason Baronf6bdafe2009-07-21 12:20:22 -0400155
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200156struct tracepoint_path *tracepoint_id_to_path(u64 config)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400157{
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200158 struct tracepoint_path *path = NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400159 DIR *sys_dir, *evt_dir;
160 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400161 char id_buf[4];
Eric Dumazet725b1362009-09-24 15:39:09 +0200162 int fd;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400163 u64 id;
164 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +0200165 char dir_path[MAXPATHLEN];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400166
Clark Williams549104f2009-11-08 09:03:07 -0600167 if (debugfs_valid_mountpoint(debugfs_path))
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200168 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400169
Jason Baron5beeded2009-07-21 14:16:29 -0400170 sys_dir = opendir(debugfs_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400171 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200172 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400173
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300174 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200175
176 snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
177 sys_dirent.d_name);
178 evt_dir = opendir(dir_path);
179 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300180 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200181
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300182 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200183
184 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
Jason Baronf6bdafe2009-07-21 12:20:22 -0400185 evt_dirent.d_name);
Eric Dumazet725b1362009-09-24 15:39:09 +0200186 fd = open(evt_path, O_RDONLY);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400187 if (fd < 0)
188 continue;
189 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
190 close(fd);
191 continue;
192 }
193 close(fd);
194 id = atoll(id_buf);
195 if (id == config) {
196 closedir(evt_dir);
197 closedir(sys_dir);
Julia Lawall59b4cae2009-12-06 10:16:30 +0100198 path = zalloc(sizeof(*path));
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200199 path->system = malloc(MAX_EVENT_LENGTH);
200 if (!path->system) {
201 free(path);
202 return NULL;
203 }
204 path->name = malloc(MAX_EVENT_LENGTH);
205 if (!path->name) {
206 free(path->system);
207 free(path);
208 return NULL;
209 }
210 strncpy(path->system, sys_dirent.d_name,
211 MAX_EVENT_LENGTH);
212 strncpy(path->name, evt_dirent.d_name,
213 MAX_EVENT_LENGTH);
214 return path;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400215 }
216 }
217 closedir(evt_dir);
218 }
219
Jason Baronf6bdafe2009-07-21 12:20:22 -0400220 closedir(sys_dir);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200221 return NULL;
222}
223
224#define TP_PATH_LEN (MAX_EVENT_LENGTH * 2 + 1)
225static const char *tracepoint_id_to_name(u64 config)
226{
227 static char buf[TP_PATH_LEN];
228 struct tracepoint_path *path;
229
230 path = tracepoint_id_to_path(config);
231 if (path) {
232 snprintf(buf, TP_PATH_LEN, "%s:%s", path->system, path->name);
233 free(path->name);
234 free(path->system);
235 free(path);
236 } else
237 snprintf(buf, TP_PATH_LEN, "%s:%s", "unknown", "unknown");
238
239 return buf;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400240}
241
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530242static int is_cache_op_valid(u8 cache_type, u8 cache_op)
243{
244 if (hw_cache_stat[cache_type] & COP(cache_op))
245 return 1; /* valid */
246 else
247 return 0; /* invalid */
248}
249
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530250static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result)
251{
252 static char name[50];
253
254 if (cache_result) {
255 sprintf(name, "%s-%s-%s", hw_cache[cache_type][0],
256 hw_cache_op[cache_op][0],
257 hw_cache_result[cache_result][0]);
258 } else {
259 sprintf(name, "%s-%s", hw_cache[cache_type][0],
260 hw_cache_op[cache_op][1]);
261 }
262
263 return name;
264}
265
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200266const char *event_name(struct perf_evsel *evsel)
Ingo Molnar52425192009-05-26 09:17:18 +0200267{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200268 u64 config = evsel->attr.config;
269 int type = evsel->attr.type;
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200270
271 return __event_name(type, config);
272}
273
Ingo Molnar83a09442009-08-15 12:26:57 +0200274const char *__event_name(int type, u64 config)
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200275{
Ingo Molnar52425192009-05-26 09:17:18 +0200276 static char buf[32];
277
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200278 if (type == PERF_TYPE_RAW) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200279 sprintf(buf, "raw 0x%" PRIx64, config);
Ingo Molnar52425192009-05-26 09:17:18 +0200280 return buf;
281 }
282
283 switch (type) {
284 case PERF_TYPE_HARDWARE:
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200285 if (config < PERF_COUNT_HW_MAX)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200286 return hw_event_names[config];
Ingo Molnar52425192009-05-26 09:17:18 +0200287 return "unknown-hardware";
288
Ingo Molnar8326f442009-06-05 20:22:46 +0200289 case PERF_TYPE_HW_CACHE: {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000290 u8 cache_type, cache_op, cache_result;
Ingo Molnar8326f442009-06-05 20:22:46 +0200291
292 cache_type = (config >> 0) & 0xff;
293 if (cache_type > PERF_COUNT_HW_CACHE_MAX)
294 return "unknown-ext-hardware-cache-type";
295
296 cache_op = (config >> 8) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200297 if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
298 return "unknown-ext-hardware-cache-op";
Ingo Molnar8326f442009-06-05 20:22:46 +0200299
300 cache_result = (config >> 16) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200301 if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
302 return "unknown-ext-hardware-cache-result";
Ingo Molnar8326f442009-06-05 20:22:46 +0200303
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530304 if (!is_cache_op_valid(cache_type, cache_op))
305 return "invalid-cache";
Ingo Molnar8326f442009-06-05 20:22:46 +0200306
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530307 return event_cache_name(cache_type, cache_op, cache_result);
Ingo Molnar8326f442009-06-05 20:22:46 +0200308 }
309
Ingo Molnar52425192009-05-26 09:17:18 +0200310 case PERF_TYPE_SOFTWARE:
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200311 if (config < PERF_COUNT_SW_MAX)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200312 return sw_event_names[config];
Ingo Molnar52425192009-05-26 09:17:18 +0200313 return "unknown-software";
314
Jason Baronf6bdafe2009-07-21 12:20:22 -0400315 case PERF_TYPE_TRACEPOINT:
316 return tracepoint_id_to_name(config);
317
Ingo Molnar52425192009-05-26 09:17:18 +0200318 default:
319 break;
320 }
321
322 return "unknown";
323}
324
Ingo Molnar83a09442009-08-15 12:26:57 +0200325static int parse_aliases(const char **str, const char *names[][MAX_ALIASES], int size)
Ingo Molnar8326f442009-06-05 20:22:46 +0200326{
327 int i, j;
Paul Mackerras61c45982009-07-01 13:04:34 +1000328 int n, longest = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200329
330 for (i = 0; i < size; i++) {
Paul Mackerras61c45982009-07-01 13:04:34 +1000331 for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
332 n = strlen(names[i][j]);
333 if (n > longest && !strncasecmp(*str, names[i][j], n))
334 longest = n;
335 }
336 if (longest > 0) {
337 *str += longest;
338 return i;
Ingo Molnar8326f442009-06-05 20:22:46 +0200339 }
340 }
341
Ingo Molnar89536452009-06-06 21:04:17 +0200342 return -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200343}
344
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200345static enum event_result
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200346parse_generic_hw_event(const char **str, struct perf_event_attr *attr)
Ingo Molnar8326f442009-06-05 20:22:46 +0200347{
Paul Mackerras61c45982009-07-01 13:04:34 +1000348 const char *s = *str;
349 int cache_type = -1, cache_op = -1, cache_result = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200350
Paul Mackerras61c45982009-07-01 13:04:34 +1000351 cache_type = parse_aliases(&s, hw_cache, PERF_COUNT_HW_CACHE_MAX);
Ingo Molnar8326f442009-06-05 20:22:46 +0200352 /*
353 * No fallback - if we cannot get a clear cache type
354 * then bail out:
355 */
356 if (cache_type == -1)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200357 return EVT_FAILED;
Ingo Molnar8326f442009-06-05 20:22:46 +0200358
Paul Mackerras61c45982009-07-01 13:04:34 +1000359 while ((cache_op == -1 || cache_result == -1) && *s == '-') {
360 ++s;
361
362 if (cache_op == -1) {
363 cache_op = parse_aliases(&s, hw_cache_op,
364 PERF_COUNT_HW_CACHE_OP_MAX);
365 if (cache_op >= 0) {
366 if (!is_cache_op_valid(cache_type, cache_op))
367 return 0;
368 continue;
369 }
370 }
371
372 if (cache_result == -1) {
373 cache_result = parse_aliases(&s, hw_cache_result,
374 PERF_COUNT_HW_CACHE_RESULT_MAX);
375 if (cache_result >= 0)
376 continue;
377 }
378
379 /*
380 * Can't parse this as a cache op or result, so back up
381 * to the '-'.
382 */
383 --s;
384 break;
385 }
386
Ingo Molnar8326f442009-06-05 20:22:46 +0200387 /*
388 * Fall back to reads:
389 */
Ingo Molnar89536452009-06-06 21:04:17 +0200390 if (cache_op == -1)
391 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
Ingo Molnar8326f442009-06-05 20:22:46 +0200392
Ingo Molnar8326f442009-06-05 20:22:46 +0200393 /*
394 * Fall back to accesses:
395 */
396 if (cache_result == -1)
397 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
398
399 attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
400 attr->type = PERF_TYPE_HW_CACHE;
401
Paul Mackerras61c45982009-07-01 13:04:34 +1000402 *str = s;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200403 return EVT_HANDLED;
Ingo Molnar8326f442009-06-05 20:22:46 +0200404}
405
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200406static enum event_result
407parse_single_tracepoint_event(char *sys_name,
408 const char *evt_name,
409 unsigned int evt_length,
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200410 struct perf_event_attr *attr,
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200411 const char **strp)
412{
413 char evt_path[MAXPATHLEN];
414 char id_buf[4];
415 u64 id;
416 int fd;
417
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200418 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
419 sys_name, evt_name);
420
421 fd = open(evt_path, O_RDONLY);
422 if (fd < 0)
423 return EVT_FAILED;
424
425 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
426 close(fd);
427 return EVT_FAILED;
428 }
429
430 close(fd);
431 id = atoll(id_buf);
432 attr->config = id;
433 attr->type = PERF_TYPE_TRACEPOINT;
Corey Ashford4c635a42010-11-30 14:27:01 -0800434 *strp += strlen(sys_name) + evt_length + 1; /* + 1 for the ':' */
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200435
Stephane Eranian5710fca2010-04-21 18:06:01 +0200436 attr->sample_type |= PERF_SAMPLE_RAW;
437 attr->sample_type |= PERF_SAMPLE_TIME;
438 attr->sample_type |= PERF_SAMPLE_CPU;
439
440 attr->sample_period = 1;
441
442
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200443 return EVT_HANDLED;
444}
445
446/* sys + ':' + event + ':' + flags*/
447#define MAX_EVOPT_LEN (MAX_EVENT_LENGTH * 2 + 2 + 128)
448static enum event_result
Masami Hiramatsufb1d2ed2010-01-05 17:47:17 -0500449parse_multiple_tracepoint_event(char *sys_name, const char *evt_exp,
450 char *flags)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200451{
452 char evt_path[MAXPATHLEN];
453 struct dirent *evt_ent;
454 DIR *evt_dir;
455
456 snprintf(evt_path, MAXPATHLEN, "%s/%s", debugfs_path, sys_name);
457 evt_dir = opendir(evt_path);
458
459 if (!evt_dir) {
460 perror("Can't open event dir");
461 return EVT_FAILED;
462 }
463
464 while ((evt_ent = readdir(evt_dir))) {
465 char event_opt[MAX_EVOPT_LEN + 1];
466 int len;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200467
468 if (!strcmp(evt_ent->d_name, ".")
469 || !strcmp(evt_ent->d_name, "..")
470 || !strcmp(evt_ent->d_name, "enable")
471 || !strcmp(evt_ent->d_name, "filter"))
472 continue;
473
Masami Hiramatsufb1d2ed2010-01-05 17:47:17 -0500474 if (!strglobmatch(evt_ent->d_name, evt_exp))
475 continue;
476
Ulrich Drepper180570f2009-12-06 13:25:30 -0500477 len = snprintf(event_opt, MAX_EVOPT_LEN, "%s:%s%s%s", sys_name,
478 evt_ent->d_name, flags ? ":" : "",
479 flags ?: "");
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200480 if (len < 0)
481 return EVT_FAILED;
482
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200483 if (parse_events(NULL, event_opt, 0))
484 return EVT_FAILED;
485 }
486
487 return EVT_HANDLED_ALL;
488}
489
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200490static enum event_result parse_tracepoint_event(const char **strp,
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200491 struct perf_event_attr *attr)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400492{
493 const char *evt_name;
Corey Ashford4c635a42010-11-30 14:27:01 -0800494 char *flags = NULL, *comma_loc;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400495 char sys_name[MAX_EVENT_LENGTH];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400496 unsigned int sys_length, evt_length;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400497
Clark Williams549104f2009-11-08 09:03:07 -0600498 if (debugfs_valid_mountpoint(debugfs_path))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400499 return 0;
500
501 evt_name = strchr(*strp, ':');
502 if (!evt_name)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200503 return EVT_FAILED;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400504
505 sys_length = evt_name - *strp;
506 if (sys_length >= MAX_EVENT_LENGTH)
507 return 0;
508
509 strncpy(sys_name, *strp, sys_length);
510 sys_name[sys_length] = '\0';
511 evt_name = evt_name + 1;
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200512
Corey Ashford4c635a42010-11-30 14:27:01 -0800513 comma_loc = strchr(evt_name, ',');
514 if (comma_loc) {
515 /* take the event name up to the comma */
516 evt_name = strndup(evt_name, comma_loc - evt_name);
517 }
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200518 flags = strchr(evt_name, ':');
519 if (flags) {
Ingo Molnar1fc35b22009-09-13 09:44:29 +0200520 /* split it out: */
521 evt_name = strndup(evt_name, flags - evt_name);
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200522 flags++;
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200523 }
524
Jason Baronf6bdafe2009-07-21 12:20:22 -0400525 evt_length = strlen(evt_name);
526 if (evt_length >= MAX_EVENT_LENGTH)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200527 return EVT_FAILED;
Masami Hiramatsufb1d2ed2010-01-05 17:47:17 -0500528 if (strpbrk(evt_name, "*?")) {
Arnaldo Carvalho de Melodd9a9ad2011-01-17 14:25:06 -0200529 *strp += strlen(sys_name) + evt_length + 1; /* 1 == the ':' */
Masami Hiramatsufb1d2ed2010-01-05 17:47:17 -0500530 return parse_multiple_tracepoint_event(sys_name, evt_name,
531 flags);
Han Pingtianf006d252011-01-06 17:39:22 +0800532 } else {
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200533 return parse_single_tracepoint_event(sys_name, evt_name,
Frederic Weisbeckerbdef3b02010-04-14 20:05:17 +0200534 evt_length, attr, strp);
Han Pingtianf006d252011-01-06 17:39:22 +0800535 }
Jason Baronf6bdafe2009-07-21 12:20:22 -0400536}
537
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100538static enum event_result
539parse_breakpoint_type(const char *type, const char **strp,
540 struct perf_event_attr *attr)
541{
542 int i;
543
544 for (i = 0; i < 3; i++) {
545 if (!type[i])
546 break;
547
548 switch (type[i]) {
549 case 'r':
550 attr->bp_type |= HW_BREAKPOINT_R;
551 break;
552 case 'w':
553 attr->bp_type |= HW_BREAKPOINT_W;
554 break;
555 case 'x':
556 attr->bp_type |= HW_BREAKPOINT_X;
557 break;
558 default:
559 return EVT_FAILED;
560 }
561 }
562 if (!attr->bp_type) /* Default */
563 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
564
565 *strp = type + i;
566
567 return EVT_HANDLED;
568}
569
570static enum event_result
571parse_breakpoint_event(const char **strp, struct perf_event_attr *attr)
572{
573 const char *target;
574 const char *type;
575 char *endaddr;
576 u64 addr;
577 enum event_result err;
578
579 target = strchr(*strp, ':');
580 if (!target)
581 return EVT_FAILED;
582
583 if (strncmp(*strp, "mem", target - *strp) != 0)
584 return EVT_FAILED;
585
586 target++;
587
588 addr = strtoull(target, &endaddr, 0);
589 if (target == endaddr)
590 return EVT_FAILED;
591
592 attr->bp_addr = addr;
593 *strp = endaddr;
594
595 type = strchr(target, ':');
596
597 /* If no type is defined, just rw as default */
598 if (!type) {
599 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
600 } else {
601 err = parse_breakpoint_type(++type, strp, attr);
602 if (err == EVT_FAILED)
603 return EVT_FAILED;
604 }
605
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200606 /*
607 * We should find a nice way to override the access length
608 * Provide some defaults for now
609 */
610 if (attr->bp_type == HW_BREAKPOINT_X)
611 attr->bp_len = sizeof(long);
612 else
613 attr->bp_len = HW_BREAKPOINT_LEN_4;
614
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100615 attr->type = PERF_TYPE_BREAKPOINT;
616
617 return EVT_HANDLED;
618}
619
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530620static int check_events(const char *str, unsigned int i)
621{
Paul Mackerras61c45982009-07-01 13:04:34 +1000622 int n;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530623
Paul Mackerras61c45982009-07-01 13:04:34 +1000624 n = strlen(event_symbols[i].symbol);
625 if (!strncmp(str, event_symbols[i].symbol, n))
626 return n;
627
628 n = strlen(event_symbols[i].alias);
629 if (n)
630 if (!strncmp(str, event_symbols[i].alias, n))
631 return n;
632 return 0;
633}
634
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200635static enum event_result
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200636parse_symbolic_event(const char **strp, struct perf_event_attr *attr)
Paul Mackerras61c45982009-07-01 13:04:34 +1000637{
638 const char *str = *strp;
639 unsigned int i;
640 int n;
641
642 for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
643 n = check_events(str, i);
644 if (n > 0) {
645 attr->type = event_symbols[i].type;
646 attr->config = event_symbols[i].config;
647 *strp = str + n;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200648 return EVT_HANDLED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000649 }
650 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200651 return EVT_FAILED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000652}
653
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200654static enum event_result
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200655parse_raw_event(const char **strp, struct perf_event_attr *attr)
Paul Mackerras61c45982009-07-01 13:04:34 +1000656{
657 const char *str = *strp;
658 u64 config;
659 int n;
660
661 if (*str != 'r')
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200662 return EVT_FAILED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000663 n = hex2u64(str + 1, &config);
664 if (n > 0) {
665 *strp = str + n + 1;
666 attr->type = PERF_TYPE_RAW;
667 attr->config = config;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200668 return EVT_HANDLED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000669 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200670 return EVT_FAILED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000671}
672
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200673static enum event_result
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200674parse_numeric_event(const char **strp, struct perf_event_attr *attr)
Paul Mackerras61c45982009-07-01 13:04:34 +1000675{
676 const char *str = *strp;
677 char *endp;
678 unsigned long type;
679 u64 config;
680
681 type = strtoul(str, &endp, 0);
682 if (endp > str && type < PERF_TYPE_MAX && *endp == ':') {
683 str = endp + 1;
684 config = strtoul(str, &endp, 0);
685 if (endp > str) {
686 attr->type = type;
687 attr->config = config;
688 *strp = endp;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200689 return EVT_HANDLED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000690 }
691 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200692 return EVT_FAILED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000693}
694
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200695static enum event_result
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200696parse_event_modifier(const char **strp, struct perf_event_attr *attr)
Paul Mackerras61c45982009-07-01 13:04:34 +1000697{
698 const char *str = *strp;
Peter Zijlstraab608342010-04-08 23:03:20 +0200699 int exclude = 0;
700 int eu = 0, ek = 0, eh = 0, precise = 0;
Paul Mackerras61c45982009-07-01 13:04:34 +1000701
702 if (*str++ != ':')
703 return 0;
704 while (*str) {
Peter Zijlstraab608342010-04-08 23:03:20 +0200705 if (*str == 'u') {
706 if (!exclude)
707 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000708 eu = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200709 } else if (*str == 'k') {
710 if (!exclude)
711 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000712 ek = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200713 } else if (*str == 'h') {
714 if (!exclude)
715 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000716 eh = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200717 } else if (*str == 'p') {
718 precise++;
719 } else
Paul Mackerras61c45982009-07-01 13:04:34 +1000720 break;
Peter Zijlstraab608342010-04-08 23:03:20 +0200721
Paul Mackerras61c45982009-07-01 13:04:34 +1000722 ++str;
723 }
724 if (str >= *strp + 2) {
725 *strp = str;
726 attr->exclude_user = eu;
727 attr->exclude_kernel = ek;
728 attr->exclude_hv = eh;
Peter Zijlstraab608342010-04-08 23:03:20 +0200729 attr->precise_ip = precise;
Paul Mackerras61c45982009-07-01 13:04:34 +1000730 return 1;
731 }
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530732 return 0;
733}
734
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200735/*
736 * Each event can have multiple symbolic names.
737 * Symbolic names are (almost) exactly matched.
738 */
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200739static enum event_result
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200740parse_event_symbols(const char **str, struct perf_event_attr *attr)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200741{
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200742 enum event_result ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200743
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200744 ret = parse_tracepoint_event(str, attr);
745 if (ret != EVT_FAILED)
746 goto modifier;
747
748 ret = parse_raw_event(str, attr);
749 if (ret != EVT_FAILED)
750 goto modifier;
751
752 ret = parse_numeric_event(str, attr);
753 if (ret != EVT_FAILED)
754 goto modifier;
755
756 ret = parse_symbolic_event(str, attr);
757 if (ret != EVT_FAILED)
758 goto modifier;
759
760 ret = parse_generic_hw_event(str, attr);
761 if (ret != EVT_FAILED)
762 goto modifier;
763
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100764 ret = parse_breakpoint_event(str, attr);
765 if (ret != EVT_FAILED)
766 goto modifier;
767
Marti Raudsepp85df6f62009-10-27 00:33:04 +0000768 fprintf(stderr, "invalid or unsupported event: '%s'\n", *str);
769 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200770 return EVT_FAILED;
771
772modifier:
Paul Mackerras61c45982009-07-01 13:04:34 +1000773 parse_event_modifier(str, attr);
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200774
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200775 return ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200776}
777
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200778int parse_events(const struct option *opt, const char *str, int unset __used)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200779{
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200780 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200781 struct perf_event_attr attr;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200782 enum event_result ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200783
Paul Mackerras61c45982009-07-01 13:04:34 +1000784 for (;;) {
Paul Mackerras61c45982009-07-01 13:04:34 +1000785 memset(&attr, 0, sizeof(attr));
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200786 ret = parse_event_symbols(&str, &attr);
787 if (ret == EVT_FAILED)
Paul Mackerras61c45982009-07-01 13:04:34 +1000788 return -1;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200789
Paul Mackerras61c45982009-07-01 13:04:34 +1000790 if (!(*str == 0 || *str == ',' || isspace(*str)))
791 return -1;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200792
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200793 if (ret != EVT_HANDLED_ALL) {
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200794 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200795 evsel = perf_evsel__new(&attr, evlist->nr_entries);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200796 if (evsel == NULL)
797 return -1;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200798 perf_evlist__add(evlist, evsel);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200799 }
Paul Mackerras61c45982009-07-01 13:04:34 +1000800
801 if (*str == 0)
802 break;
803 if (*str == ',')
804 ++str;
805 while (isspace(*str))
806 ++str;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200807 }
808
809 return 0;
810}
811
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200812int parse_filter(const struct option *opt, const char *str,
Li Zefanc171b552009-10-15 11:22:07 +0800813 int unset __used)
814{
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200815 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200816 struct perf_evsel *last = NULL;
Li Zefanc171b552009-10-15 11:22:07 +0800817
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200818 if (evlist->nr_entries > 0)
819 last = list_entry(evlist->entries.prev, struct perf_evsel, node);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200820
821 if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
Li Zefanc171b552009-10-15 11:22:07 +0800822 fprintf(stderr,
823 "-F option should follow a -e tracepoint option\n");
824 return -1;
825 }
826
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200827 last->filter = strdup(str);
828 if (last->filter == NULL) {
Li Zefanc171b552009-10-15 11:22:07 +0800829 fprintf(stderr, "not enough memory to hold filter string\n");
830 return -1;
831 }
Li Zefanc171b552009-10-15 11:22:07 +0800832
833 return 0;
834}
835
Thomas Gleixner86847b62009-06-06 12:24:17 +0200836static const char * const event_type_descriptors[] = {
Thomas Gleixner86847b62009-06-06 12:24:17 +0200837 "Hardware event",
838 "Software event",
839 "Tracepoint event",
840 "Hardware cache event",
Liming Wang41bdcb22009-12-29 16:37:07 +0800841 "Raw hardware event descriptor",
842 "Hardware breakpoint",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200843};
844
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200845/*
Jason Baronf6bdafe2009-07-21 12:20:22 -0400846 * Print the events from <debugfs_mount_point>/tracing/events
847 */
848
849static void print_tracepoint_events(void)
850{
851 DIR *sys_dir, *evt_dir;
852 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400853 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +0200854 char dir_path[MAXPATHLEN];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400855
Clark Williams549104f2009-11-08 09:03:07 -0600856 if (debugfs_valid_mountpoint(debugfs_path))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400857 return;
858
Jason Baron5beeded2009-07-21 14:16:29 -0400859 sys_dir = opendir(debugfs_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400860 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200861 return;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400862
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300863 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200864
865 snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
866 sys_dirent.d_name);
867 evt_dir = opendir(dir_path);
868 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300869 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200870
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300871 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Jason Baronf6bdafe2009-07-21 12:20:22 -0400872 snprintf(evt_path, MAXPATHLEN, "%s:%s",
873 sys_dirent.d_name, evt_dirent.d_name);
Marti Raudsepp689d3012009-10-27 00:33:05 +0000874 printf(" %-42s [%s]\n", evt_path,
Liming Wang41bdcb22009-12-29 16:37:07 +0800875 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400876 }
877 closedir(evt_dir);
878 }
Jason Baronf6bdafe2009-07-21 12:20:22 -0400879 closedir(sys_dir);
880}
881
882/*
Thomas Renninger20c457b2011-01-03 17:50:45 +0100883 * Check whether event is in <debugfs_mount_point>/tracing/events
884 */
885
886int is_valid_tracepoint(const char *event_string)
887{
888 DIR *sys_dir, *evt_dir;
889 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
890 char evt_path[MAXPATHLEN];
891 char dir_path[MAXPATHLEN];
892
893 if (debugfs_valid_mountpoint(debugfs_path))
894 return 0;
895
896 sys_dir = opendir(debugfs_path);
897 if (!sys_dir)
898 return 0;
899
900 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
901
902 snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
903 sys_dirent.d_name);
904 evt_dir = opendir(dir_path);
905 if (!evt_dir)
906 continue;
907
908 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
909 snprintf(evt_path, MAXPATHLEN, "%s:%s",
910 sys_dirent.d_name, evt_dirent.d_name);
911 if (!strcmp(evt_path, event_string)) {
912 closedir(evt_dir);
913 closedir(sys_dir);
914 return 1;
915 }
916 }
917 closedir(evt_dir);
918 }
919 closedir(sys_dir);
920 return 0;
921}
922
923/*
Thomas Gleixner86847b62009-06-06 12:24:17 +0200924 * Print the help text for the event symbols:
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200925 */
Thomas Gleixner86847b62009-06-06 12:24:17 +0200926void print_events(void)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200927{
Thomas Gleixner86847b62009-06-06 12:24:17 +0200928 struct event_symbol *syms = event_symbols;
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +0530929 unsigned int i, type, op, prev_type = -1;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530930 char name[40];
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200931
Marti Raudsepp689d3012009-10-27 00:33:05 +0000932 printf("\n");
933 printf("List of pre-defined events (to be used in -e):\n");
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200934
Thomas Gleixner86847b62009-06-06 12:24:17 +0200935 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
Liming Wang41bdcb22009-12-29 16:37:07 +0800936 type = syms->type;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200937
Thomas Gleixner86847b62009-06-06 12:24:17 +0200938 if (type != prev_type)
Marti Raudsepp689d3012009-10-27 00:33:05 +0000939 printf("\n");
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200940
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530941 if (strlen(syms->alias))
942 sprintf(name, "%s OR %s", syms->symbol, syms->alias);
943 else
944 strcpy(name, syms->symbol);
Marti Raudsepp689d3012009-10-27 00:33:05 +0000945 printf(" %-42s [%s]\n", name,
Thomas Gleixner86847b62009-06-06 12:24:17 +0200946 event_type_descriptors[type]);
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200947
Thomas Gleixner86847b62009-06-06 12:24:17 +0200948 prev_type = type;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200949 }
950
Marti Raudsepp689d3012009-10-27 00:33:05 +0000951 printf("\n");
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +0530952 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
953 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
954 /* skip invalid cache type */
955 if (!is_cache_op_valid(type, op))
956 continue;
957
958 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
Marti Raudsepp689d3012009-10-27 00:33:05 +0000959 printf(" %-42s [%s]\n",
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +0530960 event_cache_name(type, op, i),
Liming Wang41bdcb22009-12-29 16:37:07 +0800961 event_type_descriptors[PERF_TYPE_HW_CACHE]);
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +0530962 }
963 }
964 }
965
Marti Raudsepp689d3012009-10-27 00:33:05 +0000966 printf("\n");
Liming Wang41bdcb22009-12-29 16:37:07 +0800967 printf(" %-42s [%s]\n",
Arnaldo Carvalho de Melo1cf4a062010-05-07 14:07:05 -0300968 "rNNN (see 'perf list --help' on how to encode it)",
969 event_type_descriptors[PERF_TYPE_RAW]);
Marti Raudsepp689d3012009-10-27 00:33:05 +0000970 printf("\n");
Thomas Gleixner86847b62009-06-06 12:24:17 +0200971
Liming Wang41bdcb22009-12-29 16:37:07 +0800972 printf(" %-42s [%s]\n",
973 "mem:<addr>[:access]",
974 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100975 printf("\n");
976
Jason Baronf6bdafe2009-07-21 12:20:22 -0400977 print_tracepoint_events();
978
Thomas Gleixner86847b62009-06-06 12:24:17 +0200979 exit(129);
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200980}