xref: /linux/tools/perf/util/parse-events.c (revision dd9a9ad5e1e94894433110ccbf492ed60d75ffcb)
11b290d67SFrederic Weisbecker #include "../../../include/linux/hw_breakpoint.h"
286470930SIngo Molnar #include "util.h"
36b58e7f1SUlrich Drepper #include "../perf.h"
469aad6f1SArnaldo Carvalho de Melo #include "evsel.h"
586470930SIngo Molnar #include "parse-options.h"
686470930SIngo Molnar #include "parse-events.h"
786470930SIngo Molnar #include "exec_cmd.h"
886470930SIngo Molnar #include "string.h"
95aab621bSArnaldo Carvalho de Melo #include "symbol.h"
105beeded1SJason Baron #include "cache.h"
118755a8f2SArjan van de Ven #include "header.h"
12549104f2SClark Williams #include "debugfs.h"
1386470930SIngo Molnar 
1486470930SIngo Molnar int				nr_counters;
1586470930SIngo Molnar 
1669aad6f1SArnaldo Carvalho de Melo LIST_HEAD(evsel_list);
1786470930SIngo Molnar 
1886470930SIngo Molnar struct event_symbol {
199cffa8d5SPaul Mackerras 	u8		type;
209cffa8d5SPaul Mackerras 	u64		config;
2183a0944fSIngo Molnar 	const char	*symbol;
2283a0944fSIngo Molnar 	const char	*alias;
2386470930SIngo Molnar };
2486470930SIngo Molnar 
25bcd3279fSFrederic Weisbecker enum event_result {
26bcd3279fSFrederic Weisbecker 	EVT_FAILED,
27bcd3279fSFrederic Weisbecker 	EVT_HANDLED,
28bcd3279fSFrederic Weisbecker 	EVT_HANDLED_ALL
29bcd3279fSFrederic Weisbecker };
30bcd3279fSFrederic Weisbecker 
315beeded1SJason Baron char debugfs_path[MAXPATHLEN];
325beeded1SJason Baron 
3351e26842SJaswinder Singh Rajput #define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
3451e26842SJaswinder Singh Rajput #define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
3586470930SIngo Molnar 
3686470930SIngo Molnar static struct event_symbol event_symbols[] = {
3774d5b588SJaswinder Singh Rajput   { CHW(CPU_CYCLES),		"cpu-cycles",		"cycles"	},
3874d5b588SJaswinder Singh Rajput   { CHW(INSTRUCTIONS),		"instructions",		""		},
3974d5b588SJaswinder Singh Rajput   { CHW(CACHE_REFERENCES),	"cache-references",	""		},
4074d5b588SJaswinder Singh Rajput   { CHW(CACHE_MISSES),		"cache-misses",		""		},
4174d5b588SJaswinder Singh Rajput   { CHW(BRANCH_INSTRUCTIONS),	"branch-instructions",	"branches"	},
4274d5b588SJaswinder Singh Rajput   { CHW(BRANCH_MISSES),		"branch-misses",	""		},
4374d5b588SJaswinder Singh Rajput   { CHW(BUS_CYCLES),		"bus-cycles",		""		},
4486470930SIngo Molnar 
4574d5b588SJaswinder Singh Rajput   { CSW(CPU_CLOCK),		"cpu-clock",		""		},
4674d5b588SJaswinder Singh Rajput   { CSW(TASK_CLOCK),		"task-clock",		""		},
47c0c22dbfSJaswinder Singh Rajput   { CSW(PAGE_FAULTS),		"page-faults",		"faults"	},
4874d5b588SJaswinder Singh Rajput   { CSW(PAGE_FAULTS_MIN),	"minor-faults",		""		},
4974d5b588SJaswinder Singh Rajput   { CSW(PAGE_FAULTS_MAJ),	"major-faults",		""		},
5074d5b588SJaswinder Singh Rajput   { CSW(CONTEXT_SWITCHES),	"context-switches",	"cs"		},
5174d5b588SJaswinder Singh Rajput   { CSW(CPU_MIGRATIONS),	"cpu-migrations",	"migrations"	},
52f7d79860SAnton Blanchard   { CSW(ALIGNMENT_FAULTS),	"alignment-faults",	""		},
53f7d79860SAnton Blanchard   { CSW(EMULATION_FAULTS),	"emulation-faults",	""		},
5486470930SIngo Molnar };
5586470930SIngo Molnar 
56cdd6c482SIngo Molnar #define __PERF_EVENT_FIELD(config, name) \
57cdd6c482SIngo Molnar 	((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
5886470930SIngo Molnar 
59cdd6c482SIngo Molnar #define PERF_EVENT_RAW(config)	__PERF_EVENT_FIELD(config, RAW)
60cdd6c482SIngo Molnar #define PERF_EVENT_CONFIG(config)	__PERF_EVENT_FIELD(config, CONFIG)
61cdd6c482SIngo Molnar #define PERF_EVENT_TYPE(config)	__PERF_EVENT_FIELD(config, TYPE)
62cdd6c482SIngo Molnar #define PERF_EVENT_ID(config)		__PERF_EVENT_FIELD(config, EVENT)
6386470930SIngo Molnar 
6483a0944fSIngo Molnar static const char *hw_event_names[] = {
6586470930SIngo Molnar 	"cycles",
6686470930SIngo Molnar 	"instructions",
6786470930SIngo Molnar 	"cache-references",
6886470930SIngo Molnar 	"cache-misses",
6986470930SIngo Molnar 	"branches",
7086470930SIngo Molnar 	"branch-misses",
7186470930SIngo Molnar 	"bus-cycles",
7286470930SIngo Molnar };
7386470930SIngo Molnar 
7483a0944fSIngo Molnar static const char *sw_event_names[] = {
7544175b6fSIngo Molnar 	"cpu-clock-msecs",
7644175b6fSIngo Molnar 	"task-clock-msecs",
7786470930SIngo Molnar 	"page-faults",
7886470930SIngo Molnar 	"context-switches",
7986470930SIngo Molnar 	"CPU-migrations",
8086470930SIngo Molnar 	"minor-faults",
8186470930SIngo Molnar 	"major-faults",
82f7d79860SAnton Blanchard 	"alignment-faults",
83f7d79860SAnton Blanchard 	"emulation-faults",
8486470930SIngo Molnar };
8586470930SIngo Molnar 
8686470930SIngo Molnar #define MAX_ALIASES 8
8786470930SIngo Molnar 
8883a0944fSIngo Molnar static const char *hw_cache[][MAX_ALIASES] = {
899590b7baSAnton Blanchard  { "L1-dcache",	"l1-d",		"l1d",		"L1-data",		},
909590b7baSAnton Blanchard  { "L1-icache",	"l1-i",		"l1i",		"L1-instruction",	},
91e5c59547SJaswinder Singh Rajput  { "LLC",	"L2"							},
92e5c59547SJaswinder Singh Rajput  { "dTLB",	"d-tlb",	"Data-TLB",				},
93e5c59547SJaswinder Singh Rajput  { "iTLB",	"i-tlb",	"Instruction-TLB",			},
94e5c59547SJaswinder Singh Rajput  { "branch",	"branches",	"bpu",		"btb",		"bpc",	},
9586470930SIngo Molnar };
9686470930SIngo Molnar 
9783a0944fSIngo Molnar static const char *hw_cache_op[][MAX_ALIASES] = {
98e5c59547SJaswinder Singh Rajput  { "load",	"loads",	"read",					},
99e5c59547SJaswinder Singh Rajput  { "store",	"stores",	"write",				},
100e5c59547SJaswinder Singh Rajput  { "prefetch",	"prefetches",	"speculative-read", "speculative-load",	},
10186470930SIngo Molnar };
10286470930SIngo Molnar 
10383a0944fSIngo Molnar static const char *hw_cache_result[][MAX_ALIASES] = {
104e5c59547SJaswinder Singh Rajput  { "refs",	"Reference",	"ops",		"access",		},
105e5c59547SJaswinder Singh Rajput  { "misses",	"miss",							},
10686470930SIngo Molnar };
10786470930SIngo Molnar 
10806813f6cSJaswinder Singh Rajput #define C(x)		PERF_COUNT_HW_CACHE_##x
10906813f6cSJaswinder Singh Rajput #define CACHE_READ	(1 << C(OP_READ))
11006813f6cSJaswinder Singh Rajput #define CACHE_WRITE	(1 << C(OP_WRITE))
11106813f6cSJaswinder Singh Rajput #define CACHE_PREFETCH	(1 << C(OP_PREFETCH))
11206813f6cSJaswinder Singh Rajput #define COP(x)		(1 << x)
11306813f6cSJaswinder Singh Rajput 
11406813f6cSJaswinder Singh Rajput /*
11506813f6cSJaswinder Singh Rajput  * cache operartion stat
11606813f6cSJaswinder Singh Rajput  * L1I : Read and prefetch only
11706813f6cSJaswinder Singh Rajput  * ITLB and BPU : Read-only
11806813f6cSJaswinder Singh Rajput  */
11906813f6cSJaswinder Singh Rajput static unsigned long hw_cache_stat[C(MAX)] = {
12006813f6cSJaswinder Singh Rajput  [C(L1D)]	= (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
12106813f6cSJaswinder Singh Rajput  [C(L1I)]	= (CACHE_READ | CACHE_PREFETCH),
12206813f6cSJaswinder Singh Rajput  [C(LL)]	= (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
12306813f6cSJaswinder Singh Rajput  [C(DTLB)]	= (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
12406813f6cSJaswinder Singh Rajput  [C(ITLB)]	= (CACHE_READ),
12506813f6cSJaswinder Singh Rajput  [C(BPU)]	= (CACHE_READ),
12606813f6cSJaswinder Singh Rajput };
12706813f6cSJaswinder Singh Rajput 
1286b58e7f1SUlrich Drepper #define for_each_subsystem(sys_dir, sys_dirent, sys_next)	       \
129f6bdafefSJason Baron 	while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next)	       \
1306b58e7f1SUlrich Drepper 	if (sys_dirent.d_type == DT_DIR &&				       \
131f6bdafefSJason Baron 	   (strcmp(sys_dirent.d_name, ".")) &&				       \
132f6bdafefSJason Baron 	   (strcmp(sys_dirent.d_name, "..")))
133f6bdafefSJason Baron 
134ae07b63fSPeter Zijlstra static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
135ae07b63fSPeter Zijlstra {
136ae07b63fSPeter Zijlstra 	char evt_path[MAXPATHLEN];
137ae07b63fSPeter Zijlstra 	int fd;
138ae07b63fSPeter Zijlstra 
139ae07b63fSPeter Zijlstra 	snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
140ae07b63fSPeter Zijlstra 			sys_dir->d_name, evt_dir->d_name);
141ae07b63fSPeter Zijlstra 	fd = open(evt_path, O_RDONLY);
142ae07b63fSPeter Zijlstra 	if (fd < 0)
143ae07b63fSPeter Zijlstra 		return -EINVAL;
144ae07b63fSPeter Zijlstra 	close(fd);
145ae07b63fSPeter Zijlstra 
146ae07b63fSPeter Zijlstra 	return 0;
147ae07b63fSPeter Zijlstra }
148ae07b63fSPeter Zijlstra 
1496b58e7f1SUlrich Drepper #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next)	       \
150f6bdafefSJason Baron 	while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next)        \
1516b58e7f1SUlrich Drepper 	if (evt_dirent.d_type == DT_DIR &&				       \
152f6bdafefSJason Baron 	   (strcmp(evt_dirent.d_name, ".")) &&				       \
153ae07b63fSPeter Zijlstra 	   (strcmp(evt_dirent.d_name, "..")) &&				       \
154ae07b63fSPeter Zijlstra 	   (!tp_event_has_id(&sys_dirent, &evt_dirent)))
155f6bdafefSJason Baron 
156270bbbe8SLi Zefan #define MAX_EVENT_LENGTH 512
157f6bdafefSJason Baron 
158f6bdafefSJason Baron 
1591ef2ed10SFrederic Weisbecker struct tracepoint_path *tracepoint_id_to_path(u64 config)
160f6bdafefSJason Baron {
1611ef2ed10SFrederic Weisbecker 	struct tracepoint_path *path = NULL;
162f6bdafefSJason Baron 	DIR *sys_dir, *evt_dir;
163f6bdafefSJason Baron 	struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
164f6bdafefSJason Baron 	char id_buf[4];
165725b1368SEric Dumazet 	int fd;
166f6bdafefSJason Baron 	u64 id;
167f6bdafefSJason Baron 	char evt_path[MAXPATHLEN];
168725b1368SEric Dumazet 	char dir_path[MAXPATHLEN];
169f6bdafefSJason Baron 
170549104f2SClark Williams 	if (debugfs_valid_mountpoint(debugfs_path))
1711ef2ed10SFrederic Weisbecker 		return NULL;
172f6bdafefSJason Baron 
1735beeded1SJason Baron 	sys_dir = opendir(debugfs_path);
174f6bdafefSJason Baron 	if (!sys_dir)
175725b1368SEric Dumazet 		return NULL;
176f6bdafefSJason Baron 
1776b58e7f1SUlrich Drepper 	for_each_subsystem(sys_dir, sys_dirent, sys_next) {
178725b1368SEric Dumazet 
179725b1368SEric Dumazet 		snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
180725b1368SEric Dumazet 			 sys_dirent.d_name);
181725b1368SEric Dumazet 		evt_dir = opendir(dir_path);
182725b1368SEric Dumazet 		if (!evt_dir)
1836b58e7f1SUlrich Drepper 			continue;
184725b1368SEric Dumazet 
1856b58e7f1SUlrich Drepper 		for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
186725b1368SEric Dumazet 
187725b1368SEric Dumazet 			snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
188f6bdafefSJason Baron 				 evt_dirent.d_name);
189725b1368SEric Dumazet 			fd = open(evt_path, O_RDONLY);
190f6bdafefSJason Baron 			if (fd < 0)
191f6bdafefSJason Baron 				continue;
192f6bdafefSJason Baron 			if (read(fd, id_buf, sizeof(id_buf)) < 0) {
193f6bdafefSJason Baron 				close(fd);
194f6bdafefSJason Baron 				continue;
195f6bdafefSJason Baron 			}
196f6bdafefSJason Baron 			close(fd);
197f6bdafefSJason Baron 			id = atoll(id_buf);
198f6bdafefSJason Baron 			if (id == config) {
199f6bdafefSJason Baron 				closedir(evt_dir);
200f6bdafefSJason Baron 				closedir(sys_dir);
20159b4caebSJulia Lawall 				path = zalloc(sizeof(*path));
2021ef2ed10SFrederic Weisbecker 				path->system = malloc(MAX_EVENT_LENGTH);
2031ef2ed10SFrederic Weisbecker 				if (!path->system) {
2041ef2ed10SFrederic Weisbecker 					free(path);
2051ef2ed10SFrederic Weisbecker 					return NULL;
2061ef2ed10SFrederic Weisbecker 				}
2071ef2ed10SFrederic Weisbecker 				path->name = malloc(MAX_EVENT_LENGTH);
2081ef2ed10SFrederic Weisbecker 				if (!path->name) {
2091ef2ed10SFrederic Weisbecker 					free(path->system);
2101ef2ed10SFrederic Weisbecker 					free(path);
2111ef2ed10SFrederic Weisbecker 					return NULL;
2121ef2ed10SFrederic Weisbecker 				}
2131ef2ed10SFrederic Weisbecker 				strncpy(path->system, sys_dirent.d_name,
2141ef2ed10SFrederic Weisbecker 					MAX_EVENT_LENGTH);
2151ef2ed10SFrederic Weisbecker 				strncpy(path->name, evt_dirent.d_name,
2161ef2ed10SFrederic Weisbecker 					MAX_EVENT_LENGTH);
2171ef2ed10SFrederic Weisbecker 				return path;
218f6bdafefSJason Baron 			}
219f6bdafefSJason Baron 		}
220f6bdafefSJason Baron 		closedir(evt_dir);
221f6bdafefSJason Baron 	}
222f6bdafefSJason Baron 
223f6bdafefSJason Baron 	closedir(sys_dir);
2241ef2ed10SFrederic Weisbecker 	return NULL;
2251ef2ed10SFrederic Weisbecker }
2261ef2ed10SFrederic Weisbecker 
2271ef2ed10SFrederic Weisbecker #define TP_PATH_LEN (MAX_EVENT_LENGTH * 2 + 1)
2281ef2ed10SFrederic Weisbecker static const char *tracepoint_id_to_name(u64 config)
2291ef2ed10SFrederic Weisbecker {
2301ef2ed10SFrederic Weisbecker 	static char buf[TP_PATH_LEN];
2311ef2ed10SFrederic Weisbecker 	struct tracepoint_path *path;
2321ef2ed10SFrederic Weisbecker 
2331ef2ed10SFrederic Weisbecker 	path = tracepoint_id_to_path(config);
2341ef2ed10SFrederic Weisbecker 	if (path) {
2351ef2ed10SFrederic Weisbecker 		snprintf(buf, TP_PATH_LEN, "%s:%s", path->system, path->name);
2361ef2ed10SFrederic Weisbecker 		free(path->name);
2371ef2ed10SFrederic Weisbecker 		free(path->system);
2381ef2ed10SFrederic Weisbecker 		free(path);
2391ef2ed10SFrederic Weisbecker 	} else
2401ef2ed10SFrederic Weisbecker 		snprintf(buf, TP_PATH_LEN, "%s:%s", "unknown", "unknown");
2411ef2ed10SFrederic Weisbecker 
2421ef2ed10SFrederic Weisbecker 	return buf;
243f6bdafefSJason Baron }
244f6bdafefSJason Baron 
24506813f6cSJaswinder Singh Rajput static int is_cache_op_valid(u8 cache_type, u8 cache_op)
24606813f6cSJaswinder Singh Rajput {
24706813f6cSJaswinder Singh Rajput 	if (hw_cache_stat[cache_type] & COP(cache_op))
24806813f6cSJaswinder Singh Rajput 		return 1;	/* valid */
24906813f6cSJaswinder Singh Rajput 	else
25006813f6cSJaswinder Singh Rajput 		return 0;	/* invalid */
25106813f6cSJaswinder Singh Rajput }
25206813f6cSJaswinder Singh Rajput 
253e5c59547SJaswinder Singh Rajput static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result)
254e5c59547SJaswinder Singh Rajput {
255e5c59547SJaswinder Singh Rajput 	static char name[50];
256e5c59547SJaswinder Singh Rajput 
257e5c59547SJaswinder Singh Rajput 	if (cache_result) {
258e5c59547SJaswinder Singh Rajput 		sprintf(name, "%s-%s-%s", hw_cache[cache_type][0],
259e5c59547SJaswinder Singh Rajput 			hw_cache_op[cache_op][0],
260e5c59547SJaswinder Singh Rajput 			hw_cache_result[cache_result][0]);
261e5c59547SJaswinder Singh Rajput 	} else {
262e5c59547SJaswinder Singh Rajput 		sprintf(name, "%s-%s", hw_cache[cache_type][0],
263e5c59547SJaswinder Singh Rajput 			hw_cache_op[cache_op][1]);
264e5c59547SJaswinder Singh Rajput 	}
265e5c59547SJaswinder Singh Rajput 
266e5c59547SJaswinder Singh Rajput 	return name;
267e5c59547SJaswinder Singh Rajput }
268e5c59547SJaswinder Singh Rajput 
26969aad6f1SArnaldo Carvalho de Melo const char *event_name(struct perf_evsel *evsel)
27086470930SIngo Molnar {
27169aad6f1SArnaldo Carvalho de Melo 	u64 config = evsel->attr.config;
27269aad6f1SArnaldo Carvalho de Melo 	int type = evsel->attr.type;
2738f18aec5SPeter Zijlstra 
2748f18aec5SPeter Zijlstra 	return __event_name(type, config);
2758f18aec5SPeter Zijlstra }
2768f18aec5SPeter Zijlstra 
27783a0944fSIngo Molnar const char *__event_name(int type, u64 config)
2788f18aec5SPeter Zijlstra {
27986470930SIngo Molnar 	static char buf[32];
28086470930SIngo Molnar 
2818f18aec5SPeter Zijlstra 	if (type == PERF_TYPE_RAW) {
28286470930SIngo Molnar 		sprintf(buf, "raw 0x%llx", config);
28386470930SIngo Molnar 		return buf;
28486470930SIngo Molnar 	}
28586470930SIngo Molnar 
28686470930SIngo Molnar 	switch (type) {
28786470930SIngo Molnar 	case PERF_TYPE_HARDWARE:
288f4dbfa8fSPeter Zijlstra 		if (config < PERF_COUNT_HW_MAX)
28986470930SIngo Molnar 			return hw_event_names[config];
29086470930SIngo Molnar 		return "unknown-hardware";
29186470930SIngo Molnar 
29286470930SIngo Molnar 	case PERF_TYPE_HW_CACHE: {
2939cffa8d5SPaul Mackerras 		u8 cache_type, cache_op, cache_result;
29486470930SIngo Molnar 
29586470930SIngo Molnar 		cache_type   = (config >>  0) & 0xff;
29686470930SIngo Molnar 		if (cache_type > PERF_COUNT_HW_CACHE_MAX)
29786470930SIngo Molnar 			return "unknown-ext-hardware-cache-type";
29886470930SIngo Molnar 
29986470930SIngo Molnar 		cache_op     = (config >>  8) & 0xff;
30086470930SIngo Molnar 		if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
30186470930SIngo Molnar 			return "unknown-ext-hardware-cache-op";
30286470930SIngo Molnar 
30386470930SIngo Molnar 		cache_result = (config >> 16) & 0xff;
30486470930SIngo Molnar 		if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
30586470930SIngo Molnar 			return "unknown-ext-hardware-cache-result";
30686470930SIngo Molnar 
30706813f6cSJaswinder Singh Rajput 		if (!is_cache_op_valid(cache_type, cache_op))
30806813f6cSJaswinder Singh Rajput 			return "invalid-cache";
30986470930SIngo Molnar 
310e5c59547SJaswinder Singh Rajput 		return event_cache_name(cache_type, cache_op, cache_result);
31186470930SIngo Molnar 	}
31286470930SIngo Molnar 
31386470930SIngo Molnar 	case PERF_TYPE_SOFTWARE:
314f4dbfa8fSPeter Zijlstra 		if (config < PERF_COUNT_SW_MAX)
31586470930SIngo Molnar 			return sw_event_names[config];
31686470930SIngo Molnar 		return "unknown-software";
31786470930SIngo Molnar 
318f6bdafefSJason Baron 	case PERF_TYPE_TRACEPOINT:
319f6bdafefSJason Baron 		return tracepoint_id_to_name(config);
320f6bdafefSJason Baron 
32186470930SIngo Molnar 	default:
32286470930SIngo Molnar 		break;
32386470930SIngo Molnar 	}
32486470930SIngo Molnar 
32586470930SIngo Molnar 	return "unknown";
32686470930SIngo Molnar }
32786470930SIngo Molnar 
32883a0944fSIngo Molnar static int parse_aliases(const char **str, const char *names[][MAX_ALIASES], int size)
32986470930SIngo Molnar {
33086470930SIngo Molnar 	int i, j;
33161c45981SPaul Mackerras 	int n, longest = -1;
33286470930SIngo Molnar 
33386470930SIngo Molnar 	for (i = 0; i < size; i++) {
33461c45981SPaul Mackerras 		for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
33561c45981SPaul Mackerras 			n = strlen(names[i][j]);
33661c45981SPaul Mackerras 			if (n > longest && !strncasecmp(*str, names[i][j], n))
33761c45981SPaul Mackerras 				longest = n;
33861c45981SPaul Mackerras 		}
33961c45981SPaul Mackerras 		if (longest > 0) {
34061c45981SPaul Mackerras 			*str += longest;
34186470930SIngo Molnar 			return i;
34286470930SIngo Molnar 		}
34386470930SIngo Molnar 	}
34486470930SIngo Molnar 
3458953645fSIngo Molnar 	return -1;
34686470930SIngo Molnar }
34786470930SIngo Molnar 
348bcd3279fSFrederic Weisbecker static enum event_result
349cdd6c482SIngo Molnar parse_generic_hw_event(const char **str, struct perf_event_attr *attr)
35086470930SIngo Molnar {
35161c45981SPaul Mackerras 	const char *s = *str;
35261c45981SPaul Mackerras 	int cache_type = -1, cache_op = -1, cache_result = -1;
35386470930SIngo Molnar 
35461c45981SPaul Mackerras 	cache_type = parse_aliases(&s, hw_cache, PERF_COUNT_HW_CACHE_MAX);
35586470930SIngo Molnar 	/*
35686470930SIngo Molnar 	 * No fallback - if we cannot get a clear cache type
35786470930SIngo Molnar 	 * then bail out:
35886470930SIngo Molnar 	 */
35986470930SIngo Molnar 	if (cache_type == -1)
360bcd3279fSFrederic Weisbecker 		return EVT_FAILED;
36186470930SIngo Molnar 
36261c45981SPaul Mackerras 	while ((cache_op == -1 || cache_result == -1) && *s == '-') {
36361c45981SPaul Mackerras 		++s;
36461c45981SPaul Mackerras 
36561c45981SPaul Mackerras 		if (cache_op == -1) {
36661c45981SPaul Mackerras 			cache_op = parse_aliases(&s, hw_cache_op,
36761c45981SPaul Mackerras 						PERF_COUNT_HW_CACHE_OP_MAX);
36861c45981SPaul Mackerras 			if (cache_op >= 0) {
36961c45981SPaul Mackerras 				if (!is_cache_op_valid(cache_type, cache_op))
37061c45981SPaul Mackerras 					return 0;
37161c45981SPaul Mackerras 				continue;
37261c45981SPaul Mackerras 			}
37361c45981SPaul Mackerras 		}
37461c45981SPaul Mackerras 
37561c45981SPaul Mackerras 		if (cache_result == -1) {
37661c45981SPaul Mackerras 			cache_result = parse_aliases(&s, hw_cache_result,
37761c45981SPaul Mackerras 						PERF_COUNT_HW_CACHE_RESULT_MAX);
37861c45981SPaul Mackerras 			if (cache_result >= 0)
37961c45981SPaul Mackerras 				continue;
38061c45981SPaul Mackerras 		}
38161c45981SPaul Mackerras 
38261c45981SPaul Mackerras 		/*
38361c45981SPaul Mackerras 		 * Can't parse this as a cache op or result, so back up
38461c45981SPaul Mackerras 		 * to the '-'.
38561c45981SPaul Mackerras 		 */
38661c45981SPaul Mackerras 		--s;
38761c45981SPaul Mackerras 		break;
38861c45981SPaul Mackerras 	}
38961c45981SPaul Mackerras 
39086470930SIngo Molnar 	/*
39186470930SIngo Molnar 	 * Fall back to reads:
39286470930SIngo Molnar 	 */
3938953645fSIngo Molnar 	if (cache_op == -1)
3948953645fSIngo Molnar 		cache_op = PERF_COUNT_HW_CACHE_OP_READ;
39586470930SIngo Molnar 
39686470930SIngo Molnar 	/*
39786470930SIngo Molnar 	 * Fall back to accesses:
39886470930SIngo Molnar 	 */
39986470930SIngo Molnar 	if (cache_result == -1)
40086470930SIngo Molnar 		cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
40186470930SIngo Molnar 
40286470930SIngo Molnar 	attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
40386470930SIngo Molnar 	attr->type = PERF_TYPE_HW_CACHE;
40486470930SIngo Molnar 
40561c45981SPaul Mackerras 	*str = s;
406bcd3279fSFrederic Weisbecker 	return EVT_HANDLED;
40786470930SIngo Molnar }
40886470930SIngo Molnar 
409bcd3279fSFrederic Weisbecker static enum event_result
410bcd3279fSFrederic Weisbecker parse_single_tracepoint_event(char *sys_name,
411bcd3279fSFrederic Weisbecker 			      const char *evt_name,
412bcd3279fSFrederic Weisbecker 			      unsigned int evt_length,
413cdd6c482SIngo Molnar 			      struct perf_event_attr *attr,
414bcd3279fSFrederic Weisbecker 			      const char **strp)
415bcd3279fSFrederic Weisbecker {
416bcd3279fSFrederic Weisbecker 	char evt_path[MAXPATHLEN];
417bcd3279fSFrederic Weisbecker 	char id_buf[4];
418bcd3279fSFrederic Weisbecker 	u64 id;
419bcd3279fSFrederic Weisbecker 	int fd;
420bcd3279fSFrederic Weisbecker 
421bcd3279fSFrederic Weisbecker 	snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
422bcd3279fSFrederic Weisbecker 		 sys_name, evt_name);
423bcd3279fSFrederic Weisbecker 
424bcd3279fSFrederic Weisbecker 	fd = open(evt_path, O_RDONLY);
425bcd3279fSFrederic Weisbecker 	if (fd < 0)
426bcd3279fSFrederic Weisbecker 		return EVT_FAILED;
427bcd3279fSFrederic Weisbecker 
428bcd3279fSFrederic Weisbecker 	if (read(fd, id_buf, sizeof(id_buf)) < 0) {
429bcd3279fSFrederic Weisbecker 		close(fd);
430bcd3279fSFrederic Weisbecker 		return EVT_FAILED;
431bcd3279fSFrederic Weisbecker 	}
432bcd3279fSFrederic Weisbecker 
433bcd3279fSFrederic Weisbecker 	close(fd);
434bcd3279fSFrederic Weisbecker 	id = atoll(id_buf);
435bcd3279fSFrederic Weisbecker 	attr->config = id;
436bcd3279fSFrederic Weisbecker 	attr->type = PERF_TYPE_TRACEPOINT;
4374c635a4eSCorey Ashford 	*strp += strlen(sys_name) + evt_length + 1; /* + 1 for the ':' */
438bcd3279fSFrederic Weisbecker 
4395710fcadSStephane Eranian 	attr->sample_type |= PERF_SAMPLE_RAW;
4405710fcadSStephane Eranian 	attr->sample_type |= PERF_SAMPLE_TIME;
4415710fcadSStephane Eranian 	attr->sample_type |= PERF_SAMPLE_CPU;
4425710fcadSStephane Eranian 
4435710fcadSStephane Eranian 	attr->sample_period = 1;
4445710fcadSStephane Eranian 
4455710fcadSStephane Eranian 
446bcd3279fSFrederic Weisbecker 	return EVT_HANDLED;
447bcd3279fSFrederic Weisbecker }
448bcd3279fSFrederic Weisbecker 
449bcd3279fSFrederic Weisbecker /* sys + ':' + event + ':' + flags*/
450bcd3279fSFrederic Weisbecker #define MAX_EVOPT_LEN	(MAX_EVENT_LENGTH * 2 + 2 + 128)
451bcd3279fSFrederic Weisbecker static enum event_result
452fb1d2edfSMasami Hiramatsu parse_multiple_tracepoint_event(char *sys_name, const char *evt_exp,
453fb1d2edfSMasami Hiramatsu 				char *flags)
454bcd3279fSFrederic Weisbecker {
455bcd3279fSFrederic Weisbecker 	char evt_path[MAXPATHLEN];
456bcd3279fSFrederic Weisbecker 	struct dirent *evt_ent;
457bcd3279fSFrederic Weisbecker 	DIR *evt_dir;
458bcd3279fSFrederic Weisbecker 
459bcd3279fSFrederic Weisbecker 	snprintf(evt_path, MAXPATHLEN, "%s/%s", debugfs_path, sys_name);
460bcd3279fSFrederic Weisbecker 	evt_dir = opendir(evt_path);
461bcd3279fSFrederic Weisbecker 
462bcd3279fSFrederic Weisbecker 	if (!evt_dir) {
463bcd3279fSFrederic Weisbecker 		perror("Can't open event dir");
464bcd3279fSFrederic Weisbecker 		return EVT_FAILED;
465bcd3279fSFrederic Weisbecker 	}
466bcd3279fSFrederic Weisbecker 
467bcd3279fSFrederic Weisbecker 	while ((evt_ent = readdir(evt_dir))) {
468bcd3279fSFrederic Weisbecker 		char event_opt[MAX_EVOPT_LEN + 1];
469bcd3279fSFrederic Weisbecker 		int len;
470bcd3279fSFrederic Weisbecker 
471bcd3279fSFrederic Weisbecker 		if (!strcmp(evt_ent->d_name, ".")
472bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "..")
473bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "enable")
474bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "filter"))
475bcd3279fSFrederic Weisbecker 			continue;
476bcd3279fSFrederic Weisbecker 
477fb1d2edfSMasami Hiramatsu 		if (!strglobmatch(evt_ent->d_name, evt_exp))
478fb1d2edfSMasami Hiramatsu 			continue;
479fb1d2edfSMasami Hiramatsu 
480180570fdSUlrich Drepper 		len = snprintf(event_opt, MAX_EVOPT_LEN, "%s:%s%s%s", sys_name,
481180570fdSUlrich Drepper 			       evt_ent->d_name, flags ? ":" : "",
482180570fdSUlrich Drepper 			       flags ?: "");
483bcd3279fSFrederic Weisbecker 		if (len < 0)
484bcd3279fSFrederic Weisbecker 			return EVT_FAILED;
485bcd3279fSFrederic Weisbecker 
486bcd3279fSFrederic Weisbecker 		if (parse_events(NULL, event_opt, 0))
487bcd3279fSFrederic Weisbecker 			return EVT_FAILED;
488bcd3279fSFrederic Weisbecker 	}
489bcd3279fSFrederic Weisbecker 
490bcd3279fSFrederic Weisbecker 	return EVT_HANDLED_ALL;
491bcd3279fSFrederic Weisbecker }
492bcd3279fSFrederic Weisbecker 
493f006d25aSHan Pingtian static int store_event_type(const char *orgname)
494f006d25aSHan Pingtian {
495f006d25aSHan Pingtian 	char filename[PATH_MAX], *c;
496f006d25aSHan Pingtian 	FILE *file;
497f006d25aSHan Pingtian 	int id, n;
498f006d25aSHan Pingtian 
499f006d25aSHan Pingtian 	sprintf(filename, "%s/", debugfs_path);
500f006d25aSHan Pingtian 	strncat(filename, orgname, strlen(orgname));
501f006d25aSHan Pingtian 	strcat(filename, "/id");
502f006d25aSHan Pingtian 
503f006d25aSHan Pingtian 	c = strchr(filename, ':');
504f006d25aSHan Pingtian 	if (c)
505f006d25aSHan Pingtian 		*c = '/';
506f006d25aSHan Pingtian 
507f006d25aSHan Pingtian 	file = fopen(filename, "r");
508f006d25aSHan Pingtian 	if (!file)
509f006d25aSHan Pingtian 		return 0;
510f006d25aSHan Pingtian 	n = fscanf(file, "%i", &id);
511f006d25aSHan Pingtian 	fclose(file);
512f006d25aSHan Pingtian 	if (n < 1) {
513f006d25aSHan Pingtian 		pr_err("cannot store event ID\n");
514f006d25aSHan Pingtian 		return -EINVAL;
515f006d25aSHan Pingtian 	}
516f006d25aSHan Pingtian 	return perf_header__push_event(id, orgname);
517f006d25aSHan Pingtian }
518bcd3279fSFrederic Weisbecker 
519bcd3279fSFrederic Weisbecker static enum event_result parse_tracepoint_event(const char **strp,
520cdd6c482SIngo Molnar 				    struct perf_event_attr *attr)
521f6bdafefSJason Baron {
522f6bdafefSJason Baron 	const char *evt_name;
5234c635a4eSCorey Ashford 	char *flags = NULL, *comma_loc;
524f6bdafefSJason Baron 	char sys_name[MAX_EVENT_LENGTH];
525f6bdafefSJason Baron 	unsigned int sys_length, evt_length;
526f6bdafefSJason Baron 
527549104f2SClark Williams 	if (debugfs_valid_mountpoint(debugfs_path))
528f6bdafefSJason Baron 		return 0;
529f6bdafefSJason Baron 
530f6bdafefSJason Baron 	evt_name = strchr(*strp, ':');
531f6bdafefSJason Baron 	if (!evt_name)
532bcd3279fSFrederic Weisbecker 		return EVT_FAILED;
533f6bdafefSJason Baron 
534f6bdafefSJason Baron 	sys_length = evt_name - *strp;
535f6bdafefSJason Baron 	if (sys_length >= MAX_EVENT_LENGTH)
536f6bdafefSJason Baron 		return 0;
537f6bdafefSJason Baron 
538f6bdafefSJason Baron 	strncpy(sys_name, *strp, sys_length);
539f6bdafefSJason Baron 	sys_name[sys_length] = '\0';
540f6bdafefSJason Baron 	evt_name = evt_name + 1;
5413a9f131fSFrederic Weisbecker 
5424c635a4eSCorey Ashford 	comma_loc = strchr(evt_name, ',');
5434c635a4eSCorey Ashford 	if (comma_loc) {
5444c635a4eSCorey Ashford 		/* take the event name up to the comma */
5454c635a4eSCorey Ashford 		evt_name = strndup(evt_name, comma_loc - evt_name);
5464c635a4eSCorey Ashford 	}
5473a9f131fSFrederic Weisbecker 	flags = strchr(evt_name, ':');
5483a9f131fSFrederic Weisbecker 	if (flags) {
5491fc35b29SIngo Molnar 		/* split it out: */
5501fc35b29SIngo Molnar 		evt_name = strndup(evt_name, flags - evt_name);
5513a9f131fSFrederic Weisbecker 		flags++;
5523a9f131fSFrederic Weisbecker 	}
5533a9f131fSFrederic Weisbecker 
554f6bdafefSJason Baron 	evt_length = strlen(evt_name);
555f6bdafefSJason Baron 	if (evt_length >= MAX_EVENT_LENGTH)
556bcd3279fSFrederic Weisbecker 		return EVT_FAILED;
557fb1d2edfSMasami Hiramatsu 	if (strpbrk(evt_name, "*?")) {
558*dd9a9ad5SArnaldo Carvalho de Melo 		*strp += strlen(sys_name) + evt_length + 1; /* 1 == the ':' */
559fb1d2edfSMasami Hiramatsu 		return parse_multiple_tracepoint_event(sys_name, evt_name,
560fb1d2edfSMasami Hiramatsu 						       flags);
561f006d25aSHan Pingtian 	} else {
562f006d25aSHan Pingtian 		if (store_event_type(evt_name) < 0)
563f006d25aSHan Pingtian 			return EVT_FAILED;
564f006d25aSHan Pingtian 
565bcd3279fSFrederic Weisbecker 		return parse_single_tracepoint_event(sys_name, evt_name,
566bdef3b02SFrederic Weisbecker 						     evt_length, attr, strp);
567f6bdafefSJason Baron 	}
568f006d25aSHan Pingtian }
569f6bdafefSJason Baron 
5701b290d67SFrederic Weisbecker static enum event_result
5711b290d67SFrederic Weisbecker parse_breakpoint_type(const char *type, const char **strp,
5721b290d67SFrederic Weisbecker 		      struct perf_event_attr *attr)
5731b290d67SFrederic Weisbecker {
5741b290d67SFrederic Weisbecker 	int i;
5751b290d67SFrederic Weisbecker 
5761b290d67SFrederic Weisbecker 	for (i = 0; i < 3; i++) {
5771b290d67SFrederic Weisbecker 		if (!type[i])
5781b290d67SFrederic Weisbecker 			break;
5791b290d67SFrederic Weisbecker 
5801b290d67SFrederic Weisbecker 		switch (type[i]) {
5811b290d67SFrederic Weisbecker 		case 'r':
5821b290d67SFrederic Weisbecker 			attr->bp_type |= HW_BREAKPOINT_R;
5831b290d67SFrederic Weisbecker 			break;
5841b290d67SFrederic Weisbecker 		case 'w':
5851b290d67SFrederic Weisbecker 			attr->bp_type |= HW_BREAKPOINT_W;
5861b290d67SFrederic Weisbecker 			break;
5871b290d67SFrederic Weisbecker 		case 'x':
5881b290d67SFrederic Weisbecker 			attr->bp_type |= HW_BREAKPOINT_X;
5891b290d67SFrederic Weisbecker 			break;
5901b290d67SFrederic Weisbecker 		default:
5911b290d67SFrederic Weisbecker 			return EVT_FAILED;
5921b290d67SFrederic Weisbecker 		}
5931b290d67SFrederic Weisbecker 	}
5941b290d67SFrederic Weisbecker 	if (!attr->bp_type) /* Default */
5951b290d67SFrederic Weisbecker 		attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
5961b290d67SFrederic Weisbecker 
5971b290d67SFrederic Weisbecker 	*strp = type + i;
5981b290d67SFrederic Weisbecker 
5991b290d67SFrederic Weisbecker 	return EVT_HANDLED;
6001b290d67SFrederic Weisbecker }
6011b290d67SFrederic Weisbecker 
6021b290d67SFrederic Weisbecker static enum event_result
6031b290d67SFrederic Weisbecker parse_breakpoint_event(const char **strp, struct perf_event_attr *attr)
6041b290d67SFrederic Weisbecker {
6051b290d67SFrederic Weisbecker 	const char *target;
6061b290d67SFrederic Weisbecker 	const char *type;
6071b290d67SFrederic Weisbecker 	char *endaddr;
6081b290d67SFrederic Weisbecker 	u64 addr;
6091b290d67SFrederic Weisbecker 	enum event_result err;
6101b290d67SFrederic Weisbecker 
6111b290d67SFrederic Weisbecker 	target = strchr(*strp, ':');
6121b290d67SFrederic Weisbecker 	if (!target)
6131b290d67SFrederic Weisbecker 		return EVT_FAILED;
6141b290d67SFrederic Weisbecker 
6151b290d67SFrederic Weisbecker 	if (strncmp(*strp, "mem", target - *strp) != 0)
6161b290d67SFrederic Weisbecker 		return EVT_FAILED;
6171b290d67SFrederic Weisbecker 
6181b290d67SFrederic Weisbecker 	target++;
6191b290d67SFrederic Weisbecker 
6201b290d67SFrederic Weisbecker 	addr = strtoull(target, &endaddr, 0);
6211b290d67SFrederic Weisbecker 	if (target == endaddr)
6221b290d67SFrederic Weisbecker 		return EVT_FAILED;
6231b290d67SFrederic Weisbecker 
6241b290d67SFrederic Weisbecker 	attr->bp_addr = addr;
6251b290d67SFrederic Weisbecker 	*strp = endaddr;
6261b290d67SFrederic Weisbecker 
6271b290d67SFrederic Weisbecker 	type = strchr(target, ':');
6281b290d67SFrederic Weisbecker 
6291b290d67SFrederic Weisbecker 	/* If no type is defined, just rw as default */
6301b290d67SFrederic Weisbecker 	if (!type) {
6311b290d67SFrederic Weisbecker 		attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
6321b290d67SFrederic Weisbecker 	} else {
6331b290d67SFrederic Weisbecker 		err = parse_breakpoint_type(++type, strp, attr);
6341b290d67SFrederic Weisbecker 		if (err == EVT_FAILED)
6351b290d67SFrederic Weisbecker 			return EVT_FAILED;
6361b290d67SFrederic Weisbecker 	}
6371b290d67SFrederic Weisbecker 
638aa59a485SFrederic Weisbecker 	/*
639aa59a485SFrederic Weisbecker 	 * We should find a nice way to override the access length
640aa59a485SFrederic Weisbecker 	 * Provide some defaults for now
641aa59a485SFrederic Weisbecker 	 */
642aa59a485SFrederic Weisbecker 	if (attr->bp_type == HW_BREAKPOINT_X)
643aa59a485SFrederic Weisbecker 		attr->bp_len = sizeof(long);
644aa59a485SFrederic Weisbecker 	else
6451b290d67SFrederic Weisbecker 		attr->bp_len = HW_BREAKPOINT_LEN_4;
646aa59a485SFrederic Weisbecker 
6471b290d67SFrederic Weisbecker 	attr->type = PERF_TYPE_BREAKPOINT;
6481b290d67SFrederic Weisbecker 
6491b290d67SFrederic Weisbecker 	return EVT_HANDLED;
6501b290d67SFrederic Weisbecker }
6511b290d67SFrederic Weisbecker 
65274d5b588SJaswinder Singh Rajput static int check_events(const char *str, unsigned int i)
65374d5b588SJaswinder Singh Rajput {
65461c45981SPaul Mackerras 	int n;
65574d5b588SJaswinder Singh Rajput 
65661c45981SPaul Mackerras 	n = strlen(event_symbols[i].symbol);
65761c45981SPaul Mackerras 	if (!strncmp(str, event_symbols[i].symbol, n))
65861c45981SPaul Mackerras 		return n;
65961c45981SPaul Mackerras 
66061c45981SPaul Mackerras 	n = strlen(event_symbols[i].alias);
66161c45981SPaul Mackerras 	if (n)
66261c45981SPaul Mackerras 		if (!strncmp(str, event_symbols[i].alias, n))
66361c45981SPaul Mackerras 			return n;
66461c45981SPaul Mackerras 	return 0;
66561c45981SPaul Mackerras }
66661c45981SPaul Mackerras 
667bcd3279fSFrederic Weisbecker static enum event_result
668cdd6c482SIngo Molnar parse_symbolic_event(const char **strp, struct perf_event_attr *attr)
66961c45981SPaul Mackerras {
67061c45981SPaul Mackerras 	const char *str = *strp;
67161c45981SPaul Mackerras 	unsigned int i;
67261c45981SPaul Mackerras 	int n;
67361c45981SPaul Mackerras 
67461c45981SPaul Mackerras 	for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
67561c45981SPaul Mackerras 		n = check_events(str, i);
67661c45981SPaul Mackerras 		if (n > 0) {
67761c45981SPaul Mackerras 			attr->type = event_symbols[i].type;
67861c45981SPaul Mackerras 			attr->config = event_symbols[i].config;
67961c45981SPaul Mackerras 			*strp = str + n;
680bcd3279fSFrederic Weisbecker 			return EVT_HANDLED;
68161c45981SPaul Mackerras 		}
68261c45981SPaul Mackerras 	}
683bcd3279fSFrederic Weisbecker 	return EVT_FAILED;
68461c45981SPaul Mackerras }
68561c45981SPaul Mackerras 
686bcd3279fSFrederic Weisbecker static enum event_result
687cdd6c482SIngo Molnar parse_raw_event(const char **strp, struct perf_event_attr *attr)
68861c45981SPaul Mackerras {
68961c45981SPaul Mackerras 	const char *str = *strp;
69061c45981SPaul Mackerras 	u64 config;
69161c45981SPaul Mackerras 	int n;
69261c45981SPaul Mackerras 
69361c45981SPaul Mackerras 	if (*str != 'r')
694bcd3279fSFrederic Weisbecker 		return EVT_FAILED;
69561c45981SPaul Mackerras 	n = hex2u64(str + 1, &config);
69661c45981SPaul Mackerras 	if (n > 0) {
69761c45981SPaul Mackerras 		*strp = str + n + 1;
69861c45981SPaul Mackerras 		attr->type = PERF_TYPE_RAW;
69961c45981SPaul Mackerras 		attr->config = config;
700bcd3279fSFrederic Weisbecker 		return EVT_HANDLED;
70161c45981SPaul Mackerras 	}
702bcd3279fSFrederic Weisbecker 	return EVT_FAILED;
70361c45981SPaul Mackerras }
70461c45981SPaul Mackerras 
705bcd3279fSFrederic Weisbecker static enum event_result
706cdd6c482SIngo Molnar parse_numeric_event(const char **strp, struct perf_event_attr *attr)
70761c45981SPaul Mackerras {
70861c45981SPaul Mackerras 	const char *str = *strp;
70961c45981SPaul Mackerras 	char *endp;
71061c45981SPaul Mackerras 	unsigned long type;
71161c45981SPaul Mackerras 	u64 config;
71261c45981SPaul Mackerras 
71361c45981SPaul Mackerras 	type = strtoul(str, &endp, 0);
71461c45981SPaul Mackerras 	if (endp > str && type < PERF_TYPE_MAX && *endp == ':') {
71561c45981SPaul Mackerras 		str = endp + 1;
71661c45981SPaul Mackerras 		config = strtoul(str, &endp, 0);
71761c45981SPaul Mackerras 		if (endp > str) {
71861c45981SPaul Mackerras 			attr->type = type;
71961c45981SPaul Mackerras 			attr->config = config;
72061c45981SPaul Mackerras 			*strp = endp;
721bcd3279fSFrederic Weisbecker 			return EVT_HANDLED;
72261c45981SPaul Mackerras 		}
72361c45981SPaul Mackerras 	}
724bcd3279fSFrederic Weisbecker 	return EVT_FAILED;
72561c45981SPaul Mackerras }
72661c45981SPaul Mackerras 
727bcd3279fSFrederic Weisbecker static enum event_result
728cdd6c482SIngo Molnar parse_event_modifier(const char **strp, struct perf_event_attr *attr)
72961c45981SPaul Mackerras {
73061c45981SPaul Mackerras 	const char *str = *strp;
731ab608344SPeter Zijlstra 	int exclude = 0;
732ab608344SPeter Zijlstra 	int eu = 0, ek = 0, eh = 0, precise = 0;
73361c45981SPaul Mackerras 
73461c45981SPaul Mackerras 	if (*str++ != ':')
73561c45981SPaul Mackerras 		return 0;
73661c45981SPaul Mackerras 	while (*str) {
737ab608344SPeter Zijlstra 		if (*str == 'u') {
738ab608344SPeter Zijlstra 			if (!exclude)
739ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
74061c45981SPaul Mackerras 			eu = 0;
741ab608344SPeter Zijlstra 		} else if (*str == 'k') {
742ab608344SPeter Zijlstra 			if (!exclude)
743ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
74461c45981SPaul Mackerras 			ek = 0;
745ab608344SPeter Zijlstra 		} else if (*str == 'h') {
746ab608344SPeter Zijlstra 			if (!exclude)
747ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
74861c45981SPaul Mackerras 			eh = 0;
749ab608344SPeter Zijlstra 		} else if (*str == 'p') {
750ab608344SPeter Zijlstra 			precise++;
751ab608344SPeter Zijlstra 		} else
75261c45981SPaul Mackerras 			break;
753ab608344SPeter Zijlstra 
75461c45981SPaul Mackerras 		++str;
75561c45981SPaul Mackerras 	}
75661c45981SPaul Mackerras 	if (str >= *strp + 2) {
75761c45981SPaul Mackerras 		*strp = str;
75861c45981SPaul Mackerras 		attr->exclude_user   = eu;
75961c45981SPaul Mackerras 		attr->exclude_kernel = ek;
76061c45981SPaul Mackerras 		attr->exclude_hv     = eh;
761ab608344SPeter Zijlstra 		attr->precise_ip     = precise;
76261c45981SPaul Mackerras 		return 1;
76361c45981SPaul Mackerras 	}
76474d5b588SJaswinder Singh Rajput 	return 0;
76574d5b588SJaswinder Singh Rajput }
76674d5b588SJaswinder Singh Rajput 
76786470930SIngo Molnar /*
76886470930SIngo Molnar  * Each event can have multiple symbolic names.
76986470930SIngo Molnar  * Symbolic names are (almost) exactly matched.
77086470930SIngo Molnar  */
771bcd3279fSFrederic Weisbecker static enum event_result
772cdd6c482SIngo Molnar parse_event_symbols(const char **str, struct perf_event_attr *attr)
77386470930SIngo Molnar {
774bcd3279fSFrederic Weisbecker 	enum event_result ret;
77586470930SIngo Molnar 
776bcd3279fSFrederic Weisbecker 	ret = parse_tracepoint_event(str, attr);
777bcd3279fSFrederic Weisbecker 	if (ret != EVT_FAILED)
778bcd3279fSFrederic Weisbecker 		goto modifier;
779bcd3279fSFrederic Weisbecker 
780bcd3279fSFrederic Weisbecker 	ret = parse_raw_event(str, attr);
781bcd3279fSFrederic Weisbecker 	if (ret != EVT_FAILED)
782bcd3279fSFrederic Weisbecker 		goto modifier;
783bcd3279fSFrederic Weisbecker 
784bcd3279fSFrederic Weisbecker 	ret = parse_numeric_event(str, attr);
785bcd3279fSFrederic Weisbecker 	if (ret != EVT_FAILED)
786bcd3279fSFrederic Weisbecker 		goto modifier;
787bcd3279fSFrederic Weisbecker 
788bcd3279fSFrederic Weisbecker 	ret = parse_symbolic_event(str, attr);
789bcd3279fSFrederic Weisbecker 	if (ret != EVT_FAILED)
790bcd3279fSFrederic Weisbecker 		goto modifier;
791bcd3279fSFrederic Weisbecker 
792bcd3279fSFrederic Weisbecker 	ret = parse_generic_hw_event(str, attr);
793bcd3279fSFrederic Weisbecker 	if (ret != EVT_FAILED)
794bcd3279fSFrederic Weisbecker 		goto modifier;
795bcd3279fSFrederic Weisbecker 
7961b290d67SFrederic Weisbecker 	ret = parse_breakpoint_event(str, attr);
7971b290d67SFrederic Weisbecker 	if (ret != EVT_FAILED)
7981b290d67SFrederic Weisbecker 		goto modifier;
7991b290d67SFrederic Weisbecker 
80085df6f68SMarti Raudsepp 	fprintf(stderr, "invalid or unsupported event: '%s'\n", *str);
80185df6f68SMarti Raudsepp 	fprintf(stderr, "Run 'perf list' for a list of valid events\n");
802bcd3279fSFrederic Weisbecker 	return EVT_FAILED;
803bcd3279fSFrederic Weisbecker 
804bcd3279fSFrederic Weisbecker modifier:
80561c45981SPaul Mackerras 	parse_event_modifier(str, attr);
80686470930SIngo Molnar 
807bcd3279fSFrederic Weisbecker 	return ret;
80886470930SIngo Molnar }
80986470930SIngo Molnar 
810f37a291cSIngo Molnar int parse_events(const struct option *opt __used, const char *str, int unset __used)
81186470930SIngo Molnar {
812cdd6c482SIngo Molnar 	struct perf_event_attr attr;
813bcd3279fSFrederic Weisbecker 	enum event_result ret;
81486470930SIngo Molnar 
81561c45981SPaul Mackerras 	for (;;) {
81661c45981SPaul Mackerras 		memset(&attr, 0, sizeof(attr));
817bcd3279fSFrederic Weisbecker 		ret = parse_event_symbols(&str, &attr);
818bcd3279fSFrederic Weisbecker 		if (ret == EVT_FAILED)
81961c45981SPaul Mackerras 			return -1;
82061c45981SPaul Mackerras 
82161c45981SPaul Mackerras 		if (!(*str == 0 || *str == ',' || isspace(*str)))
82261c45981SPaul Mackerras 			return -1;
82386470930SIngo Molnar 
824bcd3279fSFrederic Weisbecker 		if (ret != EVT_HANDLED_ALL) {
82569aad6f1SArnaldo Carvalho de Melo 			struct perf_evsel *evsel;
82623a2f3abSLin Ming 			evsel = perf_evsel__new(&attr,
82769aad6f1SArnaldo Carvalho de Melo 						nr_counters);
82869aad6f1SArnaldo Carvalho de Melo 			if (evsel == NULL)
82969aad6f1SArnaldo Carvalho de Melo 				return -1;
83069aad6f1SArnaldo Carvalho de Melo 			list_add_tail(&evsel->node, &evsel_list);
83169aad6f1SArnaldo Carvalho de Melo 			++nr_counters;
832bcd3279fSFrederic Weisbecker 		}
83386470930SIngo Molnar 
83461c45981SPaul Mackerras 		if (*str == 0)
83561c45981SPaul Mackerras 			break;
83661c45981SPaul Mackerras 		if (*str == ',')
83761c45981SPaul Mackerras 			++str;
83861c45981SPaul Mackerras 		while (isspace(*str))
83961c45981SPaul Mackerras 			++str;
84086470930SIngo Molnar 	}
84186470930SIngo Molnar 
84286470930SIngo Molnar 	return 0;
84386470930SIngo Molnar }
84486470930SIngo Molnar 
845c171b552SLi Zefan int parse_filter(const struct option *opt __used, const char *str,
846c171b552SLi Zefan 		 int unset __used)
847c171b552SLi Zefan {
84869aad6f1SArnaldo Carvalho de Melo 	struct perf_evsel *last = NULL;
849c171b552SLi Zefan 
85069aad6f1SArnaldo Carvalho de Melo 	if (!list_empty(&evsel_list))
85169aad6f1SArnaldo Carvalho de Melo 		last = list_entry(evsel_list.prev, struct perf_evsel, node);
85269aad6f1SArnaldo Carvalho de Melo 
85369aad6f1SArnaldo Carvalho de Melo 	if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
854c171b552SLi Zefan 		fprintf(stderr,
855c171b552SLi Zefan 			"-F option should follow a -e tracepoint option\n");
856c171b552SLi Zefan 		return -1;
857c171b552SLi Zefan 	}
858c171b552SLi Zefan 
85969aad6f1SArnaldo Carvalho de Melo 	last->filter = strdup(str);
86069aad6f1SArnaldo Carvalho de Melo 	if (last->filter == NULL) {
861c171b552SLi Zefan 		fprintf(stderr, "not enough memory to hold filter string\n");
862c171b552SLi Zefan 		return -1;
863c171b552SLi Zefan 	}
864c171b552SLi Zefan 
865c171b552SLi Zefan 	return 0;
866c171b552SLi Zefan }
867c171b552SLi Zefan 
86886470930SIngo Molnar static const char * const event_type_descriptors[] = {
86986470930SIngo Molnar 	"Hardware event",
87086470930SIngo Molnar 	"Software event",
87186470930SIngo Molnar 	"Tracepoint event",
87286470930SIngo Molnar 	"Hardware cache event",
87341bdcb23SLiming Wang 	"Raw hardware event descriptor",
87441bdcb23SLiming Wang 	"Hardware breakpoint",
87586470930SIngo Molnar };
87686470930SIngo Molnar 
87786470930SIngo Molnar /*
878f6bdafefSJason Baron  * Print the events from <debugfs_mount_point>/tracing/events
879f6bdafefSJason Baron  */
880f6bdafefSJason Baron 
881f6bdafefSJason Baron static void print_tracepoint_events(void)
882f6bdafefSJason Baron {
883f6bdafefSJason Baron 	DIR *sys_dir, *evt_dir;
884f6bdafefSJason Baron 	struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
885f6bdafefSJason Baron 	char evt_path[MAXPATHLEN];
886725b1368SEric Dumazet 	char dir_path[MAXPATHLEN];
887f6bdafefSJason Baron 
888549104f2SClark Williams 	if (debugfs_valid_mountpoint(debugfs_path))
889f6bdafefSJason Baron 		return;
890f6bdafefSJason Baron 
8915beeded1SJason Baron 	sys_dir = opendir(debugfs_path);
892f6bdafefSJason Baron 	if (!sys_dir)
893725b1368SEric Dumazet 		return;
894f6bdafefSJason Baron 
8956b58e7f1SUlrich Drepper 	for_each_subsystem(sys_dir, sys_dirent, sys_next) {
896725b1368SEric Dumazet 
897725b1368SEric Dumazet 		snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
898725b1368SEric Dumazet 			 sys_dirent.d_name);
899725b1368SEric Dumazet 		evt_dir = opendir(dir_path);
900725b1368SEric Dumazet 		if (!evt_dir)
9016b58e7f1SUlrich Drepper 			continue;
902725b1368SEric Dumazet 
9036b58e7f1SUlrich Drepper 		for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
904f6bdafefSJason Baron 			snprintf(evt_path, MAXPATHLEN, "%s:%s",
905f6bdafefSJason Baron 				 sys_dirent.d_name, evt_dirent.d_name);
906689d3018SMarti Raudsepp 			printf("  %-42s [%s]\n", evt_path,
90741bdcb23SLiming Wang 				event_type_descriptors[PERF_TYPE_TRACEPOINT]);
908f6bdafefSJason Baron 		}
909f6bdafefSJason Baron 		closedir(evt_dir);
910f6bdafefSJason Baron 	}
911f6bdafefSJason Baron 	closedir(sys_dir);
912f6bdafefSJason Baron }
913f6bdafefSJason Baron 
914f6bdafefSJason Baron /*
91520c457b8SThomas Renninger  * Check whether event is in <debugfs_mount_point>/tracing/events
91620c457b8SThomas Renninger  */
91720c457b8SThomas Renninger 
91820c457b8SThomas Renninger int is_valid_tracepoint(const char *event_string)
91920c457b8SThomas Renninger {
92020c457b8SThomas Renninger 	DIR *sys_dir, *evt_dir;
92120c457b8SThomas Renninger 	struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
92220c457b8SThomas Renninger 	char evt_path[MAXPATHLEN];
92320c457b8SThomas Renninger 	char dir_path[MAXPATHLEN];
92420c457b8SThomas Renninger 
92520c457b8SThomas Renninger 	if (debugfs_valid_mountpoint(debugfs_path))
92620c457b8SThomas Renninger 		return 0;
92720c457b8SThomas Renninger 
92820c457b8SThomas Renninger 	sys_dir = opendir(debugfs_path);
92920c457b8SThomas Renninger 	if (!sys_dir)
93020c457b8SThomas Renninger 		return 0;
93120c457b8SThomas Renninger 
93220c457b8SThomas Renninger 	for_each_subsystem(sys_dir, sys_dirent, sys_next) {
93320c457b8SThomas Renninger 
93420c457b8SThomas Renninger 		snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
93520c457b8SThomas Renninger 			 sys_dirent.d_name);
93620c457b8SThomas Renninger 		evt_dir = opendir(dir_path);
93720c457b8SThomas Renninger 		if (!evt_dir)
93820c457b8SThomas Renninger 			continue;
93920c457b8SThomas Renninger 
94020c457b8SThomas Renninger 		for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
94120c457b8SThomas Renninger 			snprintf(evt_path, MAXPATHLEN, "%s:%s",
94220c457b8SThomas Renninger 				 sys_dirent.d_name, evt_dirent.d_name);
94320c457b8SThomas Renninger 			if (!strcmp(evt_path, event_string)) {
94420c457b8SThomas Renninger 				closedir(evt_dir);
94520c457b8SThomas Renninger 				closedir(sys_dir);
94620c457b8SThomas Renninger 				return 1;
94720c457b8SThomas Renninger 			}
94820c457b8SThomas Renninger 		}
94920c457b8SThomas Renninger 		closedir(evt_dir);
95020c457b8SThomas Renninger 	}
95120c457b8SThomas Renninger 	closedir(sys_dir);
95220c457b8SThomas Renninger 	return 0;
95320c457b8SThomas Renninger }
95420c457b8SThomas Renninger 
95520c457b8SThomas Renninger /*
95686470930SIngo Molnar  * Print the help text for the event symbols:
95786470930SIngo Molnar  */
95886470930SIngo Molnar void print_events(void)
95986470930SIngo Molnar {
96086470930SIngo Molnar 	struct event_symbol *syms = event_symbols;
96173c24cb8SJaswinder Singh Rajput 	unsigned int i, type, op, prev_type = -1;
96274d5b588SJaswinder Singh Rajput 	char name[40];
96386470930SIngo Molnar 
964689d3018SMarti Raudsepp 	printf("\n");
965689d3018SMarti Raudsepp 	printf("List of pre-defined events (to be used in -e):\n");
96686470930SIngo Molnar 
96786470930SIngo Molnar 	for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
96841bdcb23SLiming Wang 		type = syms->type;
96986470930SIngo Molnar 
97086470930SIngo Molnar 		if (type != prev_type)
971689d3018SMarti Raudsepp 			printf("\n");
97286470930SIngo Molnar 
97374d5b588SJaswinder Singh Rajput 		if (strlen(syms->alias))
97474d5b588SJaswinder Singh Rajput 			sprintf(name, "%s OR %s", syms->symbol, syms->alias);
97574d5b588SJaswinder Singh Rajput 		else
97674d5b588SJaswinder Singh Rajput 			strcpy(name, syms->symbol);
977689d3018SMarti Raudsepp 		printf("  %-42s [%s]\n", name,
97886470930SIngo Molnar 			event_type_descriptors[type]);
97986470930SIngo Molnar 
98086470930SIngo Molnar 		prev_type = type;
98186470930SIngo Molnar 	}
98286470930SIngo Molnar 
983689d3018SMarti Raudsepp 	printf("\n");
98473c24cb8SJaswinder Singh Rajput 	for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
98573c24cb8SJaswinder Singh Rajput 		for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
98673c24cb8SJaswinder Singh Rajput 			/* skip invalid cache type */
98773c24cb8SJaswinder Singh Rajput 			if (!is_cache_op_valid(type, op))
98873c24cb8SJaswinder Singh Rajput 				continue;
98973c24cb8SJaswinder Singh Rajput 
99073c24cb8SJaswinder Singh Rajput 			for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
991689d3018SMarti Raudsepp 				printf("  %-42s [%s]\n",
99273c24cb8SJaswinder Singh Rajput 					event_cache_name(type, op, i),
99341bdcb23SLiming Wang 					event_type_descriptors[PERF_TYPE_HW_CACHE]);
99473c24cb8SJaswinder Singh Rajput 			}
99573c24cb8SJaswinder Singh Rajput 		}
99673c24cb8SJaswinder Singh Rajput 	}
99773c24cb8SJaswinder Singh Rajput 
998689d3018SMarti Raudsepp 	printf("\n");
99941bdcb23SLiming Wang 	printf("  %-42s [%s]\n",
10001cf4a063SArnaldo Carvalho de Melo 		"rNNN (see 'perf list --help' on how to encode it)",
10011cf4a063SArnaldo Carvalho de Melo 	       event_type_descriptors[PERF_TYPE_RAW]);
1002689d3018SMarti Raudsepp 	printf("\n");
100386470930SIngo Molnar 
100441bdcb23SLiming Wang 	printf("  %-42s [%s]\n",
100541bdcb23SLiming Wang 			"mem:<addr>[:access]",
100641bdcb23SLiming Wang 			event_type_descriptors[PERF_TYPE_BREAKPOINT]);
10071b290d67SFrederic Weisbecker 	printf("\n");
10081b290d67SFrederic Weisbecker 
1009f6bdafefSJason Baron 	print_tracepoint_events();
1010f6bdafefSJason Baron 
101186470930SIngo Molnar 	exit(129);
101286470930SIngo Molnar }
101369aad6f1SArnaldo Carvalho de Melo 
101469aad6f1SArnaldo Carvalho de Melo int perf_evsel_list__create_default(void)
101569aad6f1SArnaldo Carvalho de Melo {
101623a2f3abSLin Ming 	struct perf_evsel *evsel;
101723a2f3abSLin Ming 	struct perf_event_attr attr;
101823a2f3abSLin Ming 
101923a2f3abSLin Ming 	memset(&attr, 0, sizeof(attr));
102023a2f3abSLin Ming 	attr.type = PERF_TYPE_HARDWARE;
102123a2f3abSLin Ming 	attr.config = PERF_COUNT_HW_CPU_CYCLES;
102223a2f3abSLin Ming 
102323a2f3abSLin Ming 	evsel = perf_evsel__new(&attr, 0);
102423a2f3abSLin Ming 
102569aad6f1SArnaldo Carvalho de Melo 	if (evsel == NULL)
102669aad6f1SArnaldo Carvalho de Melo 		return -ENOMEM;
102769aad6f1SArnaldo Carvalho de Melo 
102869aad6f1SArnaldo Carvalho de Melo 	list_add(&evsel->node, &evsel_list);
102969aad6f1SArnaldo Carvalho de Melo 	++nr_counters;
103069aad6f1SArnaldo Carvalho de Melo 	return 0;
103169aad6f1SArnaldo Carvalho de Melo }
103270d544d0SArnaldo Carvalho de Melo 
103370d544d0SArnaldo Carvalho de Melo void perf_evsel_list__delete(void)
103470d544d0SArnaldo Carvalho de Melo {
103570d544d0SArnaldo Carvalho de Melo 	struct perf_evsel *pos, *n;
103670d544d0SArnaldo Carvalho de Melo 
103770d544d0SArnaldo Carvalho de Melo 	list_for_each_entry_safe(pos, n, &evsel_list, node) {
103870d544d0SArnaldo Carvalho de Melo 		list_del_init(&pos->node);
103970d544d0SArnaldo Carvalho de Melo 		perf_evsel__delete(pos);
104070d544d0SArnaldo Carvalho de Melo 	}
104170d544d0SArnaldo Carvalho de Melo 	nr_counters = 0;
104270d544d0SArnaldo Carvalho de Melo }
1043