perf list: Add scandirat compatibility function
[linux-2.6-microblaze.git] / tools / perf / util / print-events.c
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 "pmus.h"
24 #include "print-events.h"
25 #include "probe-file.h"
26 #include "string2.h"
27 #include "strlist.h"
28 #include "tracepoint.h"
29 #include "pfm.h"
30 #include "thread_map.h"
31 #include "util.h"
32
33 #define MAX_NAME_LEN 100
34
35 /** Strings corresponding to enum perf_type_id. */
36 static const char * const event_type_descriptors[] = {
37         "Hardware event",
38         "Software event",
39         "Tracepoint event",
40         "Hardware cache event",
41         "Raw hardware event descriptor",
42         "Hardware breakpoint",
43 };
44
45 static const struct event_symbol event_symbols_tool[PERF_TOOL_MAX] = {
46         [PERF_TOOL_DURATION_TIME] = {
47                 .symbol = "duration_time",
48                 .alias  = "",
49         },
50         [PERF_TOOL_USER_TIME] = {
51                 .symbol = "user_time",
52                 .alias  = "",
53         },
54         [PERF_TOOL_SYSTEM_TIME] = {
55                 .symbol = "system_time",
56                 .alias  = "",
57         },
58 };
59
60 /*
61  * Print the events from <debugfs_mount_point>/tracing/events
62  */
63 void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unused, void *print_state __maybe_unused)
64 {
65         char *events_path = get_tracing_file("events");
66         int events_fd = open(events_path, O_PATH);
67         struct dirent **sys_namelist = NULL;
68         int sys_items;
69
70         put_tracing_file(events_path);
71         if (events_fd < 0) {
72                 pr_err("Error: failed to open tracing events directory\n");
73                 return;
74         }
75
76         sys_items = tracing_events__scandir_alphasort(&sys_namelist);
77
78         for (int i = 0; i < sys_items; i++) {
79                 struct dirent *sys_dirent = sys_namelist[i];
80                 struct dirent **evt_namelist = NULL;
81                 int dir_fd;
82                 int evt_items;
83
84                 if (sys_dirent->d_type != DT_DIR ||
85                     !strcmp(sys_dirent->d_name, ".") ||
86                     !strcmp(sys_dirent->d_name, ".."))
87                         goto next_sys;
88
89                 dir_fd = openat(events_fd, sys_dirent->d_name, O_PATH);
90                 if (dir_fd < 0)
91                         goto next_sys;
92
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];
97                         int evt_fd;
98
99                         if (evt_dirent->d_type != DT_DIR ||
100                             !strcmp(evt_dirent->d_name, ".") ||
101                             !strcmp(evt_dirent->d_name, ".."))
102                                 goto next_evt;
103
104                         snprintf(evt_path, sizeof(evt_path), "%s/id", evt_dirent->d_name);
105                         evt_fd = openat(dir_fd, evt_path, O_RDONLY);
106                         if (evt_fd < 0)
107                                 goto next_evt;
108                         close(evt_fd);
109
110                         snprintf(evt_path, MAXPATHLEN, "%s:%s",
111                                  sys_dirent->d_name, evt_dirent->d_name);
112                         print_cb->print_event(print_state,
113                                         /*topic=*/NULL,
114                                         /*pmu_name=*/NULL,
115                                         evt_path,
116                                         /*event_alias=*/NULL,
117                                         /*scale_unit=*/NULL,
118                                         /*deprecated=*/false,
119                                         "Tracepoint event",
120                                         /*desc=*/NULL,
121                                         /*long_desc=*/NULL,
122                                         /*encoding_desc=*/NULL);
123 next_evt:
124                         free(evt_namelist[j]);
125                 }
126                 close(dir_fd);
127                 free(evt_namelist);
128 next_sys:
129                 free(sys_namelist[i]);
130         }
131
132         free(sys_namelist);
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 bool is_event_supported(u8 type, u64 config)
228 {
229         bool ret = true;
230         int open_return;
231         struct evsel *evsel;
232         struct perf_event_attr attr = {
233                 .type = type,
234                 .config = config,
235                 .disabled = 1,
236         };
237         struct perf_thread_map *tmap = thread_map__new_by_tid(0);
238
239         if (tmap == NULL)
240                 return false;
241
242         evsel = evsel__new(&attr);
243         if (evsel) {
244                 open_return = evsel__open(evsel, NULL, tmap);
245                 ret = open_return >= 0;
246
247                 if (open_return == -EACCES) {
248                         /*
249                          * This happens if the paranoid value
250                          * /proc/sys/kernel/perf_event_paranoid is set to 2
251                          * Re-run with exclude_kernel set; we don't do that
252                          * by default as some ARM machines do not support it.
253                          *
254                          */
255                         evsel->core.attr.exclude_kernel = 1;
256                         ret = evsel__open(evsel, NULL, tmap) >= 0;
257                 }
258                 evsel__delete(evsel);
259         }
260
261         perf_thread_map__put(tmap);
262         return ret;
263 }
264
265 int print_hwcache_events(const struct print_callbacks *print_cb, void *print_state)
266 {
267         struct perf_pmu *pmu = NULL;
268         const char *event_type_descriptor = event_type_descriptors[PERF_TYPE_HW_CACHE];
269
270         /*
271          * Only print core PMUs, skipping uncore for performance and
272          * PERF_TYPE_SOFTWARE that can succeed in opening legacy cache evenst.
273          */
274         while ((pmu = perf_pmus__scan_core(pmu)) != NULL) {
275                 if (pmu->is_uncore || pmu->type == PERF_TYPE_SOFTWARE)
276                         continue;
277
278                 for (int type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
279                         for (int op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
280                                 /* skip invalid cache type */
281                                 if (!evsel__is_cache_op_valid(type, op))
282                                         continue;
283
284                                 for (int res = 0; res < PERF_COUNT_HW_CACHE_RESULT_MAX; res++) {
285                                         char name[64];
286                                         char alias_name[128];
287                                         __u64 config;
288                                         int ret;
289
290                                         __evsel__hw_cache_type_op_res_name(type, op, res,
291                                                                         name, sizeof(name));
292
293                                         ret = parse_events__decode_legacy_cache(name, pmu->type,
294                                                                                 &config);
295                                         if (ret || !is_event_supported(PERF_TYPE_HW_CACHE, config))
296                                                 continue;
297                                         snprintf(alias_name, sizeof(alias_name), "%s/%s/",
298                                                  pmu->name, name);
299                                         print_cb->print_event(print_state,
300                                                         "cache",
301                                                         pmu->name,
302                                                         name,
303                                                         alias_name,
304                                                         /*scale_unit=*/NULL,
305                                                         /*deprecated=*/false,
306                                                         event_type_descriptor,
307                                                         /*desc=*/NULL,
308                                                         /*long_desc=*/NULL,
309                                                         /*encoding_desc=*/NULL);
310                                 }
311                         }
312                 }
313         }
314         return 0;
315 }
316
317 void print_tool_events(const struct print_callbacks *print_cb, void *print_state)
318 {
319         // Start at 1 because the first enum entry means no tool event.
320         for (int i = 1; i < PERF_TOOL_MAX; ++i) {
321                 print_cb->print_event(print_state,
322                                 "tool",
323                                 /*pmu_name=*/NULL,
324                                 event_symbols_tool[i].symbol,
325                                 event_symbols_tool[i].alias,
326                                 /*scale_unit=*/NULL,
327                                 /*deprecated=*/false,
328                                 "Tool event",
329                                 /*desc=*/NULL,
330                                 /*long_desc=*/NULL,
331                                 /*encoding_desc=*/NULL);
332         }
333 }
334
335 void print_symbol_events(const struct print_callbacks *print_cb, void *print_state,
336                          unsigned int type, const struct event_symbol *syms,
337                          unsigned int max)
338 {
339         struct strlist *evt_name_list = strlist__new(NULL, NULL);
340         struct str_node *nd;
341
342         if (!evt_name_list) {
343                 pr_debug("Failed to allocate new strlist for symbol events\n");
344                 return;
345         }
346         for (unsigned int i = 0; i < max; i++) {
347                 /*
348                  * New attr.config still not supported here, the latest
349                  * example was PERF_COUNT_SW_CGROUP_SWITCHES
350                  */
351                 if (syms[i].symbol == NULL)
352                         continue;
353
354                 if (!is_event_supported(type, i))
355                         continue;
356
357                 if (strlen(syms[i].alias)) {
358                         char name[MAX_NAME_LEN];
359
360                         snprintf(name, MAX_NAME_LEN, "%s OR %s", syms[i].symbol, syms[i].alias);
361                         strlist__add(evt_name_list, name);
362                 } else
363                         strlist__add(evt_name_list, syms[i].symbol);
364         }
365
366         strlist__for_each_entry(nd, evt_name_list) {
367                 char *alias = strstr(nd->s, " OR ");
368
369                 if (alias) {
370                         *alias = '\0';
371                         alias += 4;
372                 }
373                 print_cb->print_event(print_state,
374                                 /*topic=*/NULL,
375                                 /*pmu_name=*/NULL,
376                                 nd->s,
377                                 alias,
378                                 /*scale_unit=*/NULL,
379                                 /*deprecated=*/false,
380                                 event_type_descriptors[type],
381                                 /*desc=*/NULL,
382                                 /*long_desc=*/NULL,
383                                 /*encoding_desc=*/NULL);
384         }
385         strlist__delete(evt_name_list);
386 }
387
388 /*
389  * Print the help text for the event symbols:
390  */
391 void print_events(const struct print_callbacks *print_cb, void *print_state)
392 {
393         char *tmp;
394
395         print_symbol_events(print_cb, print_state, PERF_TYPE_HARDWARE,
396                         event_symbols_hw, PERF_COUNT_HW_MAX);
397         print_symbol_events(print_cb, print_state, PERF_TYPE_SOFTWARE,
398                         event_symbols_sw, PERF_COUNT_SW_MAX);
399
400         print_tool_events(print_cb, print_state);
401
402         print_hwcache_events(print_cb, print_state);
403
404         perf_pmus__print_pmu_events(print_cb, print_state);
405
406         print_cb->print_event(print_state,
407                         /*topic=*/NULL,
408                         /*pmu_name=*/NULL,
409                         "rNNN",
410                         /*event_alias=*/NULL,
411                         /*scale_unit=*/NULL,
412                         /*deprecated=*/false,
413                         event_type_descriptors[PERF_TYPE_RAW],
414                         /*desc=*/NULL,
415                         /*long_desc=*/NULL,
416                         /*encoding_desc=*/NULL);
417
418         if (asprintf(&tmp, "%s/t1=v1[,t2=v2,t3 ...]/modifier",
419                      perf_pmus__scan_core(/*pmu=*/NULL)->name) > 0) {
420                 print_cb->print_event(print_state,
421                                 /*topic=*/NULL,
422                                 /*pmu_name=*/NULL,
423                                 tmp,
424                                 /*event_alias=*/NULL,
425                                 /*scale_unit=*/NULL,
426                                 /*deprecated=*/false,
427                                 event_type_descriptors[PERF_TYPE_RAW],
428                                 "(see 'man perf-list' on how to encode it)",
429                                 /*long_desc=*/NULL,
430                                 /*encoding_desc=*/NULL);
431                 free(tmp);
432         }
433
434         print_cb->print_event(print_state,
435                         /*topic=*/NULL,
436                         /*pmu_name=*/NULL,
437                         "mem:<addr>[/len][:access]",
438                         /*scale_unit=*/NULL,
439                         /*event_alias=*/NULL,
440                         /*deprecated=*/false,
441                         event_type_descriptors[PERF_TYPE_BREAKPOINT],
442                         /*desc=*/NULL,
443                         /*long_desc=*/NULL,
444                         /*encoding_desc=*/NULL);
445
446         print_tracepoint_events(print_cb, print_state);
447
448         print_sdt_events(print_cb, print_state);
449
450         metricgroup__print(print_cb, print_state);
451
452         print_libpfm_events(print_cb, print_state);
453 }