1 // SPDX-License-Identifier: GPL-2.0
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>
20 #include "metricgroup.h"
21 #include "parse-events.h"
24 #include "print-events.h"
25 #include "probe-file.h"
28 #include "tracepoint.h"
30 #include "thread_map.h"
32 #define MAX_NAME_LEN 100
34 /** Strings corresponding to enum perf_type_id. */
35 static const char * const event_type_descriptors[] = {
39 "Hardware cache event",
40 "Raw hardware event descriptor",
41 "Hardware breakpoint",
44 static const struct event_symbol event_symbols_tool[PERF_TOOL_MAX] = {
45 [PERF_TOOL_DURATION_TIME] = {
46 .symbol = "duration_time",
49 [PERF_TOOL_USER_TIME] = {
50 .symbol = "user_time",
53 [PERF_TOOL_SYSTEM_TIME] = {
54 .symbol = "system_time",
60 * Print the events from <debugfs_mount_point>/tracing/events
62 void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unused, void *print_state __maybe_unused)
64 char *events_path = get_tracing_file("events");
65 int events_fd = open(events_path, O_PATH);
67 put_tracing_file(events_path);
69 pr_err("Error: failed to open tracing events directory\n");
73 #ifdef HAVE_SCANDIRAT_SUPPORT
75 struct dirent **sys_namelist = NULL;
76 int sys_items = tracing_events__scandir_alphasort(&sys_namelist);
78 for (int i = 0; i < sys_items; i++) {
79 struct dirent *sys_dirent = sys_namelist[i];
80 struct dirent **evt_namelist = NULL;
84 if (sys_dirent->d_type != DT_DIR ||
85 !strcmp(sys_dirent->d_name, ".") ||
86 !strcmp(sys_dirent->d_name, ".."))
89 dir_fd = openat(events_fd, sys_dirent->d_name, O_PATH);
93 evt_items = scandirat(events_fd, sys_dirent->d_name, &evt_namelist, NULL, alphasort);
94 for (int j = 0; j < evt_items; j++) {
95 struct dirent *evt_dirent = evt_namelist[j];
96 char evt_path[MAXPATHLEN];
99 if (evt_dirent->d_type != DT_DIR ||
100 !strcmp(evt_dirent->d_name, ".") ||
101 !strcmp(evt_dirent->d_name, ".."))
104 snprintf(evt_path, sizeof(evt_path), "%s/id", evt_dirent->d_name);
105 evt_fd = openat(dir_fd, evt_path, O_RDONLY);
110 snprintf(evt_path, MAXPATHLEN, "%s:%s",
111 sys_dirent->d_name, evt_dirent->d_name);
112 print_cb->print_event(print_state,
116 /*event_alias=*/NULL,
118 /*deprecated=*/false,
122 /*encoding_desc=*/NULL);
124 free(evt_namelist[j]);
129 free(sys_namelist[i]);
135 printf("\nWARNING: Your libc doesn't have the scandirat function, please ask its maintainers to implement it.\n"
136 " As a rough fallback, please do 'ls %s' to see the available tracepoint events.\n", events_path);
141 void print_sdt_events(const struct print_callbacks *print_cb, void *print_state)
143 struct strlist *bidlist, *sdtlist;
144 struct str_node *bid_nd, *sdt_name, *next_sdt_name;
145 const char *last_sdt_name = NULL;
148 * The implicitly sorted sdtlist will hold the tracepoint name followed
149 * by @<buildid>. If the tracepoint name is unique (determined by
150 * looking at the adjacent nodes) the @<buildid> is dropped otherwise
151 * the executable path and buildid are added to the name.
153 sdtlist = strlist__new(NULL, NULL);
155 pr_debug("Failed to allocate new strlist for SDT\n");
158 bidlist = build_id_cache__list_all(true);
160 pr_debug("Failed to get buildids: %d\n", errno);
163 strlist__for_each_entry(bid_nd, bidlist) {
164 struct probe_cache *pcache;
165 struct probe_cache_entry *ent;
167 pcache = probe_cache__new(bid_nd->s, NULL);
170 list_for_each_entry(ent, &pcache->entries, node) {
173 snprintf(buf, sizeof(buf), "%s:%s@%s",
174 ent->pev.group, ent->pev.event, bid_nd->s);
175 strlist__add(sdtlist, buf);
177 probe_cache__delete(pcache);
179 strlist__delete(bidlist);
181 strlist__for_each_entry(sdt_name, sdtlist) {
182 bool show_detail = false;
183 char *bid = strchr(sdt_name->s, '@');
184 char *evt_name = NULL;
189 if (last_sdt_name && !strcmp(last_sdt_name, sdt_name->s)) {
192 next_sdt_name = strlist__next(sdt_name);
194 char *bid2 = strchr(next_sdt_name->s, '@');
198 if (strcmp(sdt_name->s, next_sdt_name->s) == 0)
204 last_sdt_name = sdt_name->s;
207 char *path = build_id_cache__origname(bid);
210 if (asprintf(&evt_name, "%s@%s(%.12s)", sdt_name->s, path, bid) < 0)
215 print_cb->print_event(print_state,
218 evt_name ?: sdt_name->s,
219 /*event_alias=*/NULL,
220 /*deprecated=*/false,
225 /*encoding_desc=*/NULL);
229 strlist__delete(sdtlist);
232 bool is_event_supported(u8 type, u64 config)
237 struct perf_event_attr attr = {
242 struct perf_thread_map *tmap = thread_map__new_by_tid(0);
247 evsel = evsel__new(&attr);
249 open_return = evsel__open(evsel, NULL, tmap);
250 ret = open_return >= 0;
252 if (open_return == -EACCES) {
254 * This happens if the paranoid value
255 * /proc/sys/kernel/perf_event_paranoid is set to 2
256 * Re-run with exclude_kernel set; we don't do that
257 * by default as some ARM machines do not support it.
260 evsel->core.attr.exclude_kernel = 1;
261 ret = evsel__open(evsel, NULL, tmap) >= 0;
263 evsel__delete(evsel);
266 perf_thread_map__put(tmap);
270 int print_hwcache_events(const struct print_callbacks *print_cb, void *print_state)
272 struct perf_pmu *pmu = NULL;
273 const char *event_type_descriptor = event_type_descriptors[PERF_TYPE_HW_CACHE];
276 * Only print core PMUs, skipping uncore for performance and
277 * PERF_TYPE_SOFTWARE that can succeed in opening legacy cache evenst.
279 while ((pmu = perf_pmus__scan_core(pmu)) != NULL) {
280 if (pmu->is_uncore || pmu->type == PERF_TYPE_SOFTWARE)
283 for (int type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
284 for (int op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
285 /* skip invalid cache type */
286 if (!evsel__is_cache_op_valid(type, op))
289 for (int res = 0; res < PERF_COUNT_HW_CACHE_RESULT_MAX; res++) {
291 char alias_name[128];
295 __evsel__hw_cache_type_op_res_name(type, op, res,
298 ret = parse_events__decode_legacy_cache(name, pmu->type,
300 if (ret || !is_event_supported(PERF_TYPE_HW_CACHE, config))
302 snprintf(alias_name, sizeof(alias_name), "%s/%s/",
304 print_cb->print_event(print_state,
310 /*deprecated=*/false,
311 event_type_descriptor,
314 /*encoding_desc=*/NULL);
322 void print_tool_events(const struct print_callbacks *print_cb, void *print_state)
324 // Start at 1 because the first enum entry means no tool event.
325 for (int i = 1; i < PERF_TOOL_MAX; ++i) {
326 print_cb->print_event(print_state,
329 event_symbols_tool[i].symbol,
330 event_symbols_tool[i].alias,
332 /*deprecated=*/false,
336 /*encoding_desc=*/NULL);
340 void print_symbol_events(const struct print_callbacks *print_cb, void *print_state,
341 unsigned int type, const struct event_symbol *syms,
344 struct strlist *evt_name_list = strlist__new(NULL, NULL);
347 if (!evt_name_list) {
348 pr_debug("Failed to allocate new strlist for symbol events\n");
351 for (unsigned int i = 0; i < max; i++) {
353 * New attr.config still not supported here, the latest
354 * example was PERF_COUNT_SW_CGROUP_SWITCHES
356 if (syms[i].symbol == NULL)
359 if (!is_event_supported(type, i))
362 if (strlen(syms[i].alias)) {
363 char name[MAX_NAME_LEN];
365 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms[i].symbol, syms[i].alias);
366 strlist__add(evt_name_list, name);
368 strlist__add(evt_name_list, syms[i].symbol);
371 strlist__for_each_entry(nd, evt_name_list) {
372 char *alias = strstr(nd->s, " OR ");
378 print_cb->print_event(print_state,
384 /*deprecated=*/false,
385 event_type_descriptors[type],
388 /*encoding_desc=*/NULL);
390 strlist__delete(evt_name_list);
394 * Print the help text for the event symbols:
396 void print_events(const struct print_callbacks *print_cb, void *print_state)
400 print_symbol_events(print_cb, print_state, PERF_TYPE_HARDWARE,
401 event_symbols_hw, PERF_COUNT_HW_MAX);
402 print_symbol_events(print_cb, print_state, PERF_TYPE_SOFTWARE,
403 event_symbols_sw, PERF_COUNT_SW_MAX);
405 print_tool_events(print_cb, print_state);
407 print_hwcache_events(print_cb, print_state);
409 perf_pmus__print_pmu_events(print_cb, print_state);
411 print_cb->print_event(print_state,
415 /*event_alias=*/NULL,
417 /*deprecated=*/false,
418 event_type_descriptors[PERF_TYPE_RAW],
421 /*encoding_desc=*/NULL);
423 if (asprintf(&tmp, "%s/t1=v1[,t2=v2,t3 ...]/modifier",
424 perf_pmus__scan_core(/*pmu=*/NULL)->name) > 0) {
425 print_cb->print_event(print_state,
429 /*event_alias=*/NULL,
431 /*deprecated=*/false,
432 event_type_descriptors[PERF_TYPE_RAW],
433 "(see 'man perf-list' on how to encode it)",
435 /*encoding_desc=*/NULL);
439 print_cb->print_event(print_state,
442 "mem:<addr>[/len][:access]",
444 /*event_alias=*/NULL,
445 /*deprecated=*/false,
446 event_type_descriptors[PERF_TYPE_BREAKPOINT],
449 /*encoding_desc=*/NULL);
451 print_tracepoint_events(print_cb, print_state);
453 print_sdt_events(print_cb, print_state);
455 metricgroup__print(print_cb, print_state);
457 print_libpfm_events(print_cb, print_state);