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"
23 #include "print-events.h"
24 #include "probe-file.h"
27 #include "tracepoint.h"
29 #include "pmu-hybrid.h"
31 #define MAX_NAME_LEN 100
33 /** Strings corresponding to enum perf_type_id. */
34 static const char * const event_type_descriptors[] = {
38 "Hardware cache event",
39 "Raw hardware event descriptor",
40 "Hardware breakpoint",
43 static const struct event_symbol event_symbols_tool[PERF_TOOL_MAX] = {
44 [PERF_TOOL_DURATION_TIME] = {
45 .symbol = "duration_time",
48 [PERF_TOOL_USER_TIME] = {
49 .symbol = "user_time",
52 [PERF_TOOL_SYSTEM_TIME] = {
53 .symbol = "system_time",
59 * Print the events from <debugfs_mount_point>/tracing/events
61 void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unused, void *print_state __maybe_unused)
63 char *events_path = get_tracing_file("events");
64 int events_fd = open(events_path, O_PATH);
66 put_tracing_file(events_path);
68 printf("Error: failed to open tracing events directory\n");
72 #ifdef HAVE_SCANDIRAT_SUPPORT
74 struct dirent **sys_namelist = NULL;
75 int sys_items = tracing_events__scandir_alphasort(&sys_namelist);
77 for (int i = 0; i < sys_items; i++) {
78 struct dirent *sys_dirent = sys_namelist[i];
79 struct dirent **evt_namelist = NULL;
83 if (sys_dirent->d_type != DT_DIR ||
84 !strcmp(sys_dirent->d_name, ".") ||
85 !strcmp(sys_dirent->d_name, ".."))
88 dir_fd = openat(events_fd, sys_dirent->d_name, O_PATH);
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];
98 if (evt_dirent->d_type != DT_DIR ||
99 !strcmp(evt_dirent->d_name, ".") ||
100 !strcmp(evt_dirent->d_name, ".."))
103 snprintf(evt_path, sizeof(evt_path), "%s/id", evt_dirent->d_name);
104 evt_fd = openat(dir_fd, evt_path, O_RDONLY);
109 snprintf(evt_path, MAXPATHLEN, "%s:%s",
110 sys_dirent->d_name, evt_dirent->d_name);
111 print_cb->print_event(print_state,
115 /*event_alias=*/NULL,
117 /*deprecated=*/false,
121 /*encoding_desc=*/NULL);
123 free(evt_namelist[j]);
128 free(sys_namelist[i]);
134 printf("\nWARNING: Your libc doesn't have the scandir function, please ask its maintainers to implement it.\n"
135 " As a rough fallback, please do 'ls %s' to see the available tracepoint events.\n", events_path);
140 void print_sdt_events(const struct print_callbacks *print_cb, void *print_state)
142 struct strlist *bidlist, *sdtlist;
143 struct str_node *bid_nd, *sdt_name, *next_sdt_name;
144 const char *last_sdt_name = NULL;
147 * The implicitly sorted sdtlist will hold the tracepoint name followed
148 * by @<buildid>. If the tracepoint name is unique (determined by
149 * looking at the adjacent nodes) the @<buildid> is dropped otherwise
150 * the executable path and buildid are added to the name.
152 sdtlist = strlist__new(NULL, NULL);
154 pr_debug("Failed to allocate new strlist for SDT\n");
157 bidlist = build_id_cache__list_all(true);
159 pr_debug("Failed to get buildids: %d\n", errno);
162 strlist__for_each_entry(bid_nd, bidlist) {
163 struct probe_cache *pcache;
164 struct probe_cache_entry *ent;
166 pcache = probe_cache__new(bid_nd->s, NULL);
169 list_for_each_entry(ent, &pcache->entries, node) {
172 snprintf(buf, sizeof(buf), "%s:%s@%s",
173 ent->pev.group, ent->pev.event, bid_nd->s);
174 strlist__add(sdtlist, buf);
176 probe_cache__delete(pcache);
178 strlist__delete(bidlist);
180 strlist__for_each_entry(sdt_name, sdtlist) {
181 bool show_detail = false;
182 char *bid = strchr(sdt_name->s, '@');
183 char *evt_name = NULL;
188 if (last_sdt_name && !strcmp(last_sdt_name, sdt_name->s)) {
191 next_sdt_name = strlist__next(sdt_name);
193 char *bid2 = strchr(next_sdt_name->s, '@');
197 if (strcmp(sdt_name->s, next_sdt_name->s) == 0)
203 last_sdt_name = sdt_name->s;
206 char *path = build_id_cache__origname(bid);
209 if (asprintf(&evt_name, "%s@%s(%.12s)", sdt_name->s, path, bid) < 0)
214 print_cb->print_event(print_state,
217 evt_name ?: sdt_name->s,
218 /*event_alias=*/NULL,
219 /*deprecated=*/false,
224 /*encoding_desc=*/NULL);
228 strlist__delete(sdtlist);
231 int print_hwcache_events(const struct print_callbacks *print_cb, void *print_state)
233 struct strlist *evt_name_list = strlist__new(NULL, NULL);
236 if (!evt_name_list) {
237 pr_debug("Failed to allocate new strlist for hwcache events\n");
240 for (int type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
241 for (int op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
242 /* skip invalid cache type */
243 if (!evsel__is_cache_op_valid(type, op))
246 for (int i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
247 struct perf_pmu *pmu = NULL;
250 __evsel__hw_cache_type_op_res_name(type, op, i, name, sizeof(name));
251 if (!perf_pmu__has_hybrid()) {
252 if (is_event_supported(PERF_TYPE_HW_CACHE,
253 type | (op << 8) | (i << 16)))
254 strlist__add(evt_name_list, name);
257 perf_pmu__for_each_hybrid_pmu(pmu) {
258 if (is_event_supported(PERF_TYPE_HW_CACHE,
259 type | (op << 8) | (i << 16) |
260 ((__u64)pmu->type << PERF_PMU_TYPE_SHIFT))) {
262 snprintf(new_name, sizeof(new_name),
263 "%s/%s/", pmu->name, name);
264 strlist__add(evt_name_list, new_name);
271 strlist__for_each_entry(nd, evt_name_list) {
272 print_cb->print_event(print_state,
276 /*event_alias=*/NULL,
278 /*deprecated=*/false,
279 event_type_descriptors[PERF_TYPE_HW_CACHE],
282 /*encoding_desc=*/NULL);
284 strlist__delete(evt_name_list);
288 void print_tool_events(const struct print_callbacks *print_cb, void *print_state)
290 // Start at 1 because the first enum entry means no tool event.
291 for (int i = 1; i < PERF_TOOL_MAX; ++i) {
292 print_cb->print_event(print_state,
295 event_symbols_tool[i].symbol,
296 event_symbols_tool[i].alias,
298 /*deprecated=*/false,
302 /*encoding_desc=*/NULL);
306 void print_symbol_events(const struct print_callbacks *print_cb, void *print_state,
307 unsigned int type, const struct event_symbol *syms,
310 struct strlist *evt_name_list = strlist__new(NULL, NULL);
313 if (!evt_name_list) {
314 pr_debug("Failed to allocate new strlist for symbol events\n");
317 for (unsigned int i = 0; i < max; i++) {
319 * New attr.config still not supported here, the latest
320 * example was PERF_COUNT_SW_CGROUP_SWITCHES
322 if (syms[i].symbol == NULL)
325 if (!is_event_supported(type, i))
328 if (strlen(syms[i].alias)) {
329 char name[MAX_NAME_LEN];
331 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms[i].symbol, syms[i].alias);
332 strlist__add(evt_name_list, name);
334 strlist__add(evt_name_list, syms[i].symbol);
337 strlist__for_each_entry(nd, evt_name_list) {
338 char *alias = strstr(nd->s, " OR ");
344 print_cb->print_event(print_state,
350 /*deprecated=*/false,
351 event_type_descriptors[type],
354 /*encoding_desc=*/NULL);
356 strlist__delete(evt_name_list);
360 * Print the help text for the event symbols:
362 void print_events(const struct print_callbacks *print_cb, void *print_state)
364 print_symbol_events(print_cb, print_state, PERF_TYPE_HARDWARE,
365 event_symbols_hw, PERF_COUNT_HW_MAX);
366 print_symbol_events(print_cb, print_state, PERF_TYPE_SOFTWARE,
367 event_symbols_sw, PERF_COUNT_SW_MAX);
369 print_tool_events(print_cb, print_state);
371 print_hwcache_events(print_cb, print_state);
373 print_pmu_events(print_cb, print_state);
375 print_cb->print_event(print_state,
379 /*event_alias=*/NULL,
381 /*deprecated=*/false,
382 event_type_descriptors[PERF_TYPE_RAW],
385 /*encoding_desc=*/NULL);
387 print_cb->print_event(print_state,
390 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
391 /*event_alias=*/NULL,
393 /*deprecated=*/false,
394 event_type_descriptors[PERF_TYPE_RAW],
395 "(see 'man perf-list' on how to encode it)",
397 /*encoding_desc=*/NULL);
399 print_cb->print_event(print_state,
402 "mem:<addr>[/len][:access]",
404 /*event_alias=*/NULL,
405 /*deprecated=*/false,
406 event_type_descriptors[PERF_TYPE_BREAKPOINT],
409 /*encoding_desc=*/NULL);
411 print_tracepoint_events(print_cb, print_state);
413 print_sdt_events(print_cb, print_state);
415 metricgroup__print(print_cb, print_state);
417 print_libpfm_events(print_cb, print_state);