xref: /linux/tools/perf/util/print-events.c (revision 63df0e4bc368adbd12ed70ed4789d8d52d65661d)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <dirent.h>
3 #include <errno.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <fcntl.h>
8 #include <sys/param.h>
9 #include <unistd.h>
10 
11 #include <api/fs/tracing_path.h>
12 #include <linux/stddef.h>
13 #include <linux/perf_event.h>
14 #include <linux/zalloc.h>
15 #include <subcmd/pager.h>
16 
17 #include "build-id.h"
18 #include "debug.h"
19 #include "evsel.h"
20 #include "metricgroup.h"
21 #include "parse-events.h"
22 #include "pmu.h"
23 #include "print-events.h"
24 #include "probe-file.h"
25 #include "string2.h"
26 #include "strlist.h"
27 #include "tracepoint.h"
28 #include "pfm.h"
29 #include "pmu-hybrid.h"
30 
31 #define MAX_NAME_LEN 100
32 
33 /** Strings corresponding to enum perf_type_id. */
34 static const char * const event_type_descriptors[] = {
35 	"Hardware event",
36 	"Software event",
37 	"Tracepoint event",
38 	"Hardware cache event",
39 	"Raw hardware event descriptor",
40 	"Hardware breakpoint",
41 };
42 
43 static const struct event_symbol event_symbols_tool[PERF_TOOL_MAX] = {
44 	[PERF_TOOL_DURATION_TIME] = {
45 		.symbol = "duration_time",
46 		.alias  = "",
47 	},
48 	[PERF_TOOL_USER_TIME] = {
49 		.symbol = "user_time",
50 		.alias  = "",
51 	},
52 	[PERF_TOOL_SYSTEM_TIME] = {
53 		.symbol = "system_time",
54 		.alias  = "",
55 	},
56 };
57 
58 /*
59  * Print the events from <debugfs_mount_point>/tracing/events
60  */
61 void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unused, void *print_state __maybe_unused)
62 {
63 	char *events_path = get_tracing_file("events");
64 	int events_fd = open(events_path, O_PATH);
65 
66 	put_tracing_file(events_path);
67 	if (events_fd < 0) {
68 		printf("Error: failed to open tracing events directory\n");
69 		return;
70 	}
71 
72 #ifdef HAVE_SCANDIRAT_SUPPORT
73 {
74 	struct dirent **sys_namelist = NULL;
75 	int sys_items = tracing_events__scandir_alphasort(&sys_namelist);
76 
77 	for (int i = 0; i < sys_items; i++) {
78 		struct dirent *sys_dirent = sys_namelist[i];
79 		struct dirent **evt_namelist = NULL;
80 		int dir_fd;
81 		int evt_items;
82 
83 		if (sys_dirent->d_type != DT_DIR ||
84 		    !strcmp(sys_dirent->d_name, ".") ||
85 		    !strcmp(sys_dirent->d_name, ".."))
86 			continue;
87 
88 		dir_fd = openat(events_fd, sys_dirent->d_name, O_PATH);
89 		if (dir_fd < 0)
90 			continue;
91 
92 		evt_items = scandirat(events_fd, sys_dirent->d_name, &evt_namelist, NULL, alphasort);
93 		for (int j = 0; j < evt_items; j++) {
94 			struct dirent *evt_dirent = evt_namelist[j];
95 			char evt_path[MAXPATHLEN];
96 			int evt_fd;
97 
98 			if (evt_dirent->d_type != DT_DIR ||
99 			    !strcmp(evt_dirent->d_name, ".") ||
100 			    !strcmp(evt_dirent->d_name, ".."))
101 				continue;
102 
103 			snprintf(evt_path, sizeof(evt_path), "%s/id", evt_dirent->d_name);
104 			evt_fd = openat(dir_fd, evt_path, O_RDONLY);
105 			if (evt_fd < 0)
106 				continue;
107 			close(evt_fd);
108 
109 			snprintf(evt_path, MAXPATHLEN, "%s:%s",
110 				 sys_dirent->d_name, evt_dirent->d_name);
111 			print_cb->print_event(print_state,
112 					/*topic=*/NULL,
113 					/*pmu_name=*/NULL,
114 					evt_path,
115 					/*event_alias=*/NULL,
116 					/*scale_unit=*/NULL,
117 					/*deprecated=*/false,
118 					"Tracepoint event",
119 					/*desc=*/NULL,
120 					/*long_desc=*/NULL,
121 					/*encoding_desc=*/NULL);
122 		}
123 		close(dir_fd);
124 		free(evt_namelist);
125 	}
126 
127 	free(sys_namelist);
128 }
129 #else
130 	printf("\nWARNING: Your libc doesn't have the scandir function, please ask its maintainers to implement it.\n"
131 	       "         As a rough fallback, please do 'ls %s' to see the available tracepoint events.\n", events_path);
132 #endif
133 	close(events_fd);
134 }
135 
136 void print_sdt_events(const struct print_callbacks *print_cb, void *print_state)
137 {
138 	struct strlist *bidlist, *sdtlist;
139 	struct str_node *bid_nd, *sdt_name, *next_sdt_name;
140 	const char *last_sdt_name = NULL;
141 
142 	/*
143 	 * The implicitly sorted sdtlist will hold the tracepoint name followed
144 	 * by @<buildid>. If the tracepoint name is unique (determined by
145 	 * looking at the adjacent nodes) the @<buildid> is dropped otherwise
146 	 * the executable path and buildid are added to the name.
147 	 */
148 	sdtlist = strlist__new(NULL, NULL);
149 	if (!sdtlist) {
150 		pr_debug("Failed to allocate new strlist for SDT\n");
151 		return;
152 	}
153 	bidlist = build_id_cache__list_all(true);
154 	if (!bidlist) {
155 		pr_debug("Failed to get buildids: %d\n", errno);
156 		return;
157 	}
158 	strlist__for_each_entry(bid_nd, bidlist) {
159 		struct probe_cache *pcache;
160 		struct probe_cache_entry *ent;
161 
162 		pcache = probe_cache__new(bid_nd->s, NULL);
163 		if (!pcache)
164 			continue;
165 		list_for_each_entry(ent, &pcache->entries, node) {
166 			char buf[1024];
167 
168 			snprintf(buf, sizeof(buf), "%s:%s@%s",
169 				 ent->pev.group, ent->pev.event, bid_nd->s);
170 			strlist__add(sdtlist, buf);
171 		}
172 		probe_cache__delete(pcache);
173 	}
174 	strlist__delete(bidlist);
175 
176 	strlist__for_each_entry(sdt_name, sdtlist) {
177 		bool show_detail = false;
178 		char *bid = strchr(sdt_name->s, '@');
179 		char *evt_name = NULL;
180 
181 		if (bid)
182 			*(bid++) = '\0';
183 
184 		if (last_sdt_name && !strcmp(last_sdt_name, sdt_name->s)) {
185 			show_detail = true;
186 		} else {
187 			next_sdt_name = strlist__next(sdt_name);
188 			if (next_sdt_name) {
189 				char *bid2 = strchr(next_sdt_name->s, '@');
190 
191 				if (bid2)
192 					*bid2 = '\0';
193 				if (strcmp(sdt_name->s, next_sdt_name->s) == 0)
194 					show_detail = true;
195 				if (bid2)
196 					*bid2 = '@';
197 			}
198 		}
199 		last_sdt_name = sdt_name->s;
200 
201 		if (show_detail) {
202 			char *path = build_id_cache__origname(bid);
203 
204 			if (path) {
205 				if (asprintf(&evt_name, "%s@%s(%.12s)", sdt_name->s, path, bid) < 0)
206 					evt_name = NULL;
207 				free(path);
208 			}
209 		}
210 		print_cb->print_event(print_state,
211 				/*topic=*/NULL,
212 				/*pmu_name=*/NULL,
213 				evt_name ?: sdt_name->s,
214 				/*event_alias=*/NULL,
215 				/*deprecated=*/false,
216 				/*scale_unit=*/NULL,
217 				"SDT event",
218 				/*desc=*/NULL,
219 				/*long_desc=*/NULL,
220 				/*encoding_desc=*/NULL);
221 
222 		free(evt_name);
223 	}
224 	strlist__delete(sdtlist);
225 }
226 
227 int print_hwcache_events(const struct print_callbacks *print_cb, void *print_state)
228 {
229 	struct strlist *evt_name_list = strlist__new(NULL, NULL);
230 	struct str_node *nd;
231 
232 	if (!evt_name_list) {
233 		pr_debug("Failed to allocate new strlist for hwcache events\n");
234 		return -ENOMEM;
235 	}
236 	for (int type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
237 		for (int op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
238 			/* skip invalid cache type */
239 			if (!evsel__is_cache_op_valid(type, op))
240 				continue;
241 
242 			for (int i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
243 				struct perf_pmu *pmu = NULL;
244 				char name[64];
245 
246 				__evsel__hw_cache_type_op_res_name(type, op, i, name, sizeof(name));
247 				if (!perf_pmu__has_hybrid()) {
248 					if (is_event_supported(PERF_TYPE_HW_CACHE,
249 							       type | (op << 8) | (i << 16)))
250 						strlist__add(evt_name_list, name);
251 					continue;
252 				}
253 				perf_pmu__for_each_hybrid_pmu(pmu) {
254 					if (is_event_supported(PERF_TYPE_HW_CACHE,
255 					    type | (op << 8) | (i << 16) |
256 					    ((__u64)pmu->type << PERF_PMU_TYPE_SHIFT))) {
257 						char new_name[128];
258 							snprintf(new_name, sizeof(new_name),
259 								 "%s/%s/", pmu->name, name);
260 							strlist__add(evt_name_list, new_name);
261 					}
262 				}
263 			}
264 		}
265 	}
266 
267 	strlist__for_each_entry(nd, evt_name_list) {
268 		print_cb->print_event(print_state,
269 				"cache",
270 				/*pmu_name=*/NULL,
271 				nd->s,
272 				/*event_alias=*/NULL,
273 				/*scale_unit=*/NULL,
274 				/*deprecated=*/false,
275 				event_type_descriptors[PERF_TYPE_HW_CACHE],
276 				/*desc=*/NULL,
277 				/*long_desc=*/NULL,
278 				/*encoding_desc=*/NULL);
279 	}
280 	strlist__delete(evt_name_list);
281 	return 0;
282 }
283 
284 void print_tool_events(const struct print_callbacks *print_cb, void *print_state)
285 {
286 	// Start at 1 because the first enum entry means no tool event.
287 	for (int i = 1; i < PERF_TOOL_MAX; ++i) {
288 		print_cb->print_event(print_state,
289 				"tool",
290 				/*pmu_name=*/NULL,
291 				event_symbols_tool[i].symbol,
292 				event_symbols_tool[i].alias,
293 				/*scale_unit=*/NULL,
294 				/*deprecated=*/false,
295 				"Tool event",
296 				/*desc=*/NULL,
297 				/*long_desc=*/NULL,
298 				/*encoding_desc=*/NULL);
299 	}
300 }
301 
302 void print_symbol_events(const struct print_callbacks *print_cb, void *print_state,
303 			 unsigned int type, const struct event_symbol *syms,
304 			 unsigned int max)
305 {
306 	struct strlist *evt_name_list = strlist__new(NULL, NULL);
307 	struct str_node *nd;
308 
309 	if (!evt_name_list) {
310 		pr_debug("Failed to allocate new strlist for symbol events\n");
311 		return;
312 	}
313 	for (unsigned int i = 0; i < max; i++) {
314 		/*
315 		 * New attr.config still not supported here, the latest
316 		 * example was PERF_COUNT_SW_CGROUP_SWITCHES
317 		 */
318 		if (syms[i].symbol == NULL)
319 			continue;
320 
321 		if (!is_event_supported(type, i))
322 			continue;
323 
324 		if (strlen(syms[i].alias)) {
325 			char name[MAX_NAME_LEN];
326 
327 			snprintf(name, MAX_NAME_LEN, "%s OR %s", syms[i].symbol, syms[i].alias);
328 			strlist__add(evt_name_list, name);
329 		} else
330 			strlist__add(evt_name_list, syms[i].symbol);
331 	}
332 
333 	strlist__for_each_entry(nd, evt_name_list) {
334 		char *alias = strstr(nd->s, " OR ");
335 
336 		if (alias) {
337 			*alias = '\0';
338 			alias += 4;
339 		}
340 		print_cb->print_event(print_state,
341 				/*topic=*/NULL,
342 				/*pmu_name=*/NULL,
343 				nd->s,
344 				alias,
345 				/*scale_unit=*/NULL,
346 				/*deprecated=*/false,
347 				event_type_descriptors[type],
348 				/*desc=*/NULL,
349 				/*long_desc=*/NULL,
350 				/*encoding_desc=*/NULL);
351 	}
352 	strlist__delete(evt_name_list);
353 }
354 
355 /*
356  * Print the help text for the event symbols:
357  */
358 void print_events(const struct print_callbacks *print_cb, void *print_state)
359 {
360 	print_symbol_events(print_cb, print_state, PERF_TYPE_HARDWARE,
361 			event_symbols_hw, PERF_COUNT_HW_MAX);
362 	print_symbol_events(print_cb, print_state, PERF_TYPE_SOFTWARE,
363 			event_symbols_sw, PERF_COUNT_SW_MAX);
364 
365 	print_tool_events(print_cb, print_state);
366 
367 	print_hwcache_events(print_cb, print_state);
368 
369 	print_pmu_events(print_cb, print_state);
370 
371 	print_cb->print_event(print_state,
372 			/*topic=*/NULL,
373 			/*pmu_name=*/NULL,
374 			"rNNN",
375 			/*event_alias=*/NULL,
376 			/*scale_unit=*/NULL,
377 			/*deprecated=*/false,
378 			event_type_descriptors[PERF_TYPE_RAW],
379 			/*desc=*/NULL,
380 			/*long_desc=*/NULL,
381 			/*encoding_desc=*/NULL);
382 
383 	print_cb->print_event(print_state,
384 			/*topic=*/NULL,
385 			/*pmu_name=*/NULL,
386 			"cpu/t1=v1[,t2=v2,t3 ...]/modifier",
387 			/*event_alias=*/NULL,
388 			/*scale_unit=*/NULL,
389 			/*deprecated=*/false,
390 			event_type_descriptors[PERF_TYPE_RAW],
391 			"(see 'man perf-list' on how to encode it)",
392 			/*long_desc=*/NULL,
393 			/*encoding_desc=*/NULL);
394 
395 	print_cb->print_event(print_state,
396 			/*topic=*/NULL,
397 			/*pmu_name=*/NULL,
398 			"mem:<addr>[/len][:access]",
399 			/*scale_unit=*/NULL,
400 			/*event_alias=*/NULL,
401 			/*deprecated=*/false,
402 			event_type_descriptors[PERF_TYPE_BREAKPOINT],
403 			/*desc=*/NULL,
404 			/*long_desc=*/NULL,
405 			/*encoding_desc=*/NULL);
406 
407 	print_tracepoint_events(print_cb, print_state);
408 
409 	print_sdt_events(print_cb, print_state);
410 
411 	metricgroup__print(print_cb, print_state);
412 
413 	print_libpfm_events(print_cb, print_state);
414 }
415