1

perf list: Add scandirat compatibility function

scandirat is used during the printing of tracepoint events but may be
missing from certain libcs. Add a compatibility implementation that
uses the symlink of an fd in /proc as a path for the reliably present
scandir.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: James Clark <james.clark@arm.com>
Cc: Justin Stitt <justinstitt@google.com>
Cc: Bill Wendling <morbo@google.com>
Cc: Nick Desaulniers <ndesaulniers@google.com>
Cc: Yang Jihong <yangjihong1@huawei.com>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Athira Jajeev <atrajeev@linux.vnet.ibm.com>
Cc: llvm@lists.linux.dev
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/r/20240221034155.1500118-3-irogers@google.com
This commit is contained in:
Ian Rogers 2024-02-20 19:41:49 -08:00 committed by Namhyung Kim
parent 510e528786
commit 8ece26ad5a
3 changed files with 31 additions and 9 deletions

View File

@ -28,6 +28,7 @@
#include "tracepoint.h" #include "tracepoint.h"
#include "pfm.h" #include "pfm.h"
#include "thread_map.h" #include "thread_map.h"
#include "util.h"
#define MAX_NAME_LEN 100 #define MAX_NAME_LEN 100
@ -63,6 +64,8 @@ void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unus
{ {
char *events_path = get_tracing_file("events"); char *events_path = get_tracing_file("events");
int events_fd = open(events_path, O_PATH); int events_fd = open(events_path, O_PATH);
struct dirent **sys_namelist = NULL;
int sys_items;
put_tracing_file(events_path); put_tracing_file(events_path);
if (events_fd < 0) { if (events_fd < 0) {
@ -70,10 +73,7 @@ void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unus
return; return;
} }
#ifdef HAVE_SCANDIRAT_SUPPORT sys_items = tracing_events__scandir_alphasort(&sys_namelist);
{
struct dirent **sys_namelist = NULL;
int sys_items = tracing_events__scandir_alphasort(&sys_namelist);
for (int i = 0; i < sys_items; i++) { for (int i = 0; i < sys_items; i++) {
struct dirent *sys_dirent = sys_namelist[i]; struct dirent *sys_dirent = sys_namelist[i];
@ -130,11 +130,6 @@ next_sys:
} }
free(sys_namelist); free(sys_namelist);
}
#else
printf("\nWARNING: Your libc doesn't have the scandirat function, please ask its maintainers to implement it.\n"
" As a rough fallback, please do 'ls %s' to see the available tracepoint events.\n", events_path);
#endif
close(events_fd); close(events_fd);
} }

View File

@ -552,3 +552,22 @@ int sched_getcpu(void)
return -1; return -1;
} }
#endif #endif
#ifndef HAVE_SCANDIRAT_SUPPORT
int scandirat(int dirfd, const char *dirp,
struct dirent ***namelist,
int (*filter)(const struct dirent *),
int (*compar)(const struct dirent **, const struct dirent **))
{
char path[PATH_MAX];
int err, fd = openat(dirfd, dirp, O_PATH);
if (fd < 0)
return fd;
snprintf(path, sizeof(path), "/proc/%d/fd/%d", getpid(), fd);
err = scandir(path, namelist, filter, compar);
close(fd);
return err;
}
#endif

View File

@ -6,6 +6,7 @@
/* glibc 2.20 deprecates _BSD_SOURCE in favour of _DEFAULT_SOURCE */ /* glibc 2.20 deprecates _BSD_SOURCE in favour of _DEFAULT_SOURCE */
#define _DEFAULT_SOURCE 1 #define _DEFAULT_SOURCE 1
#include <dirent.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
@ -56,6 +57,13 @@ int perf_tip(char **strp, const char *dirpath);
int sched_getcpu(void); int sched_getcpu(void);
#endif #endif
#ifndef HAVE_SCANDIRAT_SUPPORT
int scandirat(int dirfd, const char *dirp,
struct dirent ***namelist,
int (*filter)(const struct dirent *),
int (*compar)(const struct dirent **, const struct dirent **));
#endif
extern bool perf_singlethreaded; extern bool perf_singlethreaded;
void perf_set_singlethreaded(void); void perf_set_singlethreaded(void);