From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 185CEC4167B for ; Fri, 1 Dec 2023 23:50:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231442AbjLAXul (ORCPT ); Fri, 1 Dec 2023 18:50:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50602 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231311AbjLAXug (ORCPT ); Fri, 1 Dec 2023 18:50:36 -0500 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9272ED7E for ; Fri, 1 Dec 2023 15:50:42 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5d67fc68a82so15487207b3.2 for ; Fri, 01 Dec 2023 15:50:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701474642; x=1702079442; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=GLMuugMS+2QSw9nEPVn5F8ycyfLnIiKmkZECZ6NVJPs=; b=F8JEMtLNcOAqtpVZJPdnhpNL2IHrqAvx5lYzB+J9OKhVni/fbrYCdvC6bLSkOlJ7uH qlCGRQHt8nY1BQQYLqhtR7pQfHyr/SPpgSyPiigafqtUBiMACTBcd12SqzRqHGElFkqx qQ49oZYyovGNyPqoTy/yexl6JcTOCdE/3CqebFtLE+9YYnu1ZEx/y0XN2iYqAqCuL8j6 E+B+7GCYbDO1gI5Z2NU4wkD1I3GQoktgXpAvbgXjFJwIgYCcsYVz6qMFuYw/2KNSFyV0 ReTQ9AShSpHnqE/ESHqc1qh0Mv0jD8c481qrLyIgoue61pEFJZ8uxIf7sTEVsH9sEk6A w5nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701474642; x=1702079442; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=GLMuugMS+2QSw9nEPVn5F8ycyfLnIiKmkZECZ6NVJPs=; b=KSFaxUeeMSBba6/XuFvP3C2EgPeKrQAPzxPx1b++pOFLWIJ67yB/D6IA8IYV1LJXWw Ufz3EN2mkbSv7gZG74mWWU+wr2u/Z2lTPkGTS/5uzs5UbHxrWu9Tw4yHmra8x4maRh0E KYvlBjYyk3vrB4HBS6VjGGStumVKdHN7vhew2H7Yo4OWLLN5yfLyycjx/7oqqGE6I9na lJ3BA6bhbqohqRe8E5Sz1+Z0WQj84efGtGambFDPRaMit7JS4eb3E74jwAV1mn7Xg0u7 d9GPmfAPjCkxWnzSqqJGAp3OcDNeVurMfdRTqU0uCaNrS5Oe5udM7UejfmkhVfO334DK zZjw== X-Gm-Message-State: AOJu0Yz/HCbJ/s1Tu6EoavyecQ6e7AC/CQIRVmBzy33vH1sBsEkk1jdU b1zEUJo2MlPbIp5aY21REP9+wm8lCfrx X-Google-Smtp-Source: AGHT+IHSKV/9LPaj9BTvOKMmF9QTWwBDBPJWjhyYWUeVCRViwcQF6/0CnJQ7mudoRnAwz3MkrWv9XN3DHyzE X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:df3:224b:7b7f:efc5]) (user=irogers job=sendgmr) by 2002:a05:690c:2d06:b0:5d4:1e95:1e8a with SMTP id eq6-20020a05690c2d0600b005d41e951e8amr114629ywb.4.1701474641878; Fri, 01 Dec 2023 15:50:41 -0800 (PST) Date: Fri, 1 Dec 2023 15:50:24 -0800 In-Reply-To: <20231201235031.475293-1-irogers@google.com> Message-Id: <20231201235031.475293-2-irogers@google.com> Mime-Version: 1.0 References: <20231201235031.475293-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog Subject: [PATCH v1 2/9] perf list: Add scandirat compatibility function From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nathan Chancellor , Nick Desaulniers , Tom Rix , Ravi Bangoria , James Clark , Kan Liang , John Garry , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, llvm@lists.linux.dev Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- tools/perf/util/print-events.c | 12 +++--------- tools/perf/util/util.c | 19 +++++++++++++++++++ tools/perf/util/util.h | 8 ++++++++ 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/tools/perf/util/print-events.c b/tools/perf/util/print-events.c index b0fc48be623f..15ec55b07bfd 100644 --- a/tools/perf/util/print-events.c +++ b/tools/perf/util/print-events.c @@ -63,6 +63,8 @@ void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unus { char *events_path = get_tracing_file("events"); int events_fd = open(events_path, O_PATH); + struct dirent **sys_namelist = NULL; + int sys_items; put_tracing_file(events_path); if (events_fd < 0) { @@ -70,10 +72,7 @@ void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unus return; } -#ifdef HAVE_SCANDIRAT_SUPPORT -{ - struct dirent **sys_namelist = NULL; - int sys_items = tracing_events__scandir_alphasort(&sys_namelist); + sys_items = tracing_events__scandir_alphasort(&sys_namelist); for (int i = 0; i < sys_items; i++) { struct dirent *sys_dirent = sys_namelist[i]; @@ -130,11 +129,6 @@ void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unus } 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); } diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c index c1fd9ba6d697..4f561e5e4162 100644 --- a/tools/perf/util/util.c +++ b/tools/perf/util/util.c @@ -552,3 +552,22 @@ int sched_getcpu(void) return -1; } #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 diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h index 7c8915d92dca..9966c21aaf04 100644 --- a/tools/perf/util/util.h +++ b/tools/perf/util/util.h @@ -6,6 +6,7 @@ /* glibc 2.20 deprecates _BSD_SOURCE in favour of _DEFAULT_SOURCE */ #define _DEFAULT_SOURCE 1 +#include #include #include #include @@ -56,6 +57,13 @@ int perf_tip(char **strp, const char *dirpath); int sched_getcpu(void); #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; void perf_set_singlethreaded(void); -- 2.43.0.rc2.451.g8631bc7472-goog