Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp2279346ioo; Mon, 23 May 2022 14:41:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzBbJAbK5xHoz07z5hQe29OTDblbgSX06u8AMufj5+gmbiwYXDTvXfIRA3mP/GFaU2CryU3 X-Received: by 2002:a17:902:74c5:b0:161:5061:9d93 with SMTP id f5-20020a17090274c500b0016150619d93mr24162564plt.171.1653342111999; Mon, 23 May 2022 14:41:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653342111; cv=none; d=google.com; s=arc-20160816; b=Da/V7BURPcJSXwlUjmUyzVOn+xVgnJW3Oc9Gd9k14OrH3hOvKN522WWniRMne04nnM 8+9ZWw+vqxbI5EH6xYkYgv8N1PgyruhwyRvlW0RlNpoCGCMF6aHS0Lzi1kujx7fNUjfM zXoOf1cWEGHwyA0YGfUpWyihnE4U5tf5p7v0PgjcaBw0SEllS4aPl6UNI93WGi18fIa3 4j7zzltGKxStIKc9keiAnpAJ4h3yn+thcykDwwwkTFbEDv8ycJ7hH7JCnTlCS3lkf9x+ jNHCbHw4Y4wCN1DmcqL7YwQmqruHrQJzCqS/CeI0VEjw+Rn+yLmz9DTi1/EfdaPxSBre k/Fw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:mime-version:message-id:date :dkim-signature; bh=P0dc/hsbLfI6NQ/lZ14Xz2MO6AZmT1SEz1e3p5QB+CU=; b=CsKWR/gYFF3lLucHpngXkKXkfP+qDdung5pPuqWiSMZGaF63qIYCP/rSKcPdXfgFGL UNrwR9850IFrovoPt8Gi29RyMiuJJQ8+hagrMsgAbFUygaMVX+NsorQNMIwbZfpLDz25 TjxtI5Kmy5SZx8ywDz5SwPnJAxJ3kd8GtlqBlgGcd60DEBNH7JuMWhI9IR1NyKjccB5s Q2DpJcYigfYpKd6v0C8Y4F1A4v06l1YXgf+fIAPJFTxEtaZO5+toa41iwgfnGKd793oP 9pPZ3FU/t207QcdnFyVPvH+z5XWd98wutbCw43KKftchiiCQMBG8tx/DL+TCnoY6+UxY E6pA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=KVrjJMgW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t19-20020a63eb13000000b003f60495f184si10149452pgh.236.2022.05.23.14.41.40; Mon, 23 May 2022 14:41:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=KVrjJMgW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231940AbiEWV2c (ORCPT + 99 others); Mon, 23 May 2022 17:28:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53618 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231784AbiEWV2S (ORCPT ); Mon, 23 May 2022 17:28:18 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A8E61A30B0 for ; Mon, 23 May 2022 14:28:17 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id p16-20020a170902e75000b00161d96620c4so7289750plf.14 for ; Mon, 23 May 2022 14:28:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:message-id:mime-version:subject:from:to:cc; bh=P0dc/hsbLfI6NQ/lZ14Xz2MO6AZmT1SEz1e3p5QB+CU=; b=KVrjJMgWtZ9fFnNlRcHeO9QfwmEQXGUYiM0Ui1qQ4KnHJmna9s7rviKOq0Z8+DZFab WoTShCn3U+p969h7FMSarMxfW03rqebUa9FnCSrcgdW5FAMmQEy/3xslLTP+lVrtfbgh FpTeL6PuyYBVU3AWdfm8cEvFE+jEyAdqJC3w6L29HQZgvlGaSpZWHwpmTbj9ZOYlyZ6T 3l2q52e67phuNwBvOtTsUP2MlvAzBOqe0Z5kmRkSj/ohrgU4t5KFApNEFcIeMwMHxR+J Mik+DeJP40n01Fb3l9lUEze3hJ5QELzbKZAlITxvhPCXr0ajJTO+LpBkRh+EfmVJsPpk 78Pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=P0dc/hsbLfI6NQ/lZ14Xz2MO6AZmT1SEz1e3p5QB+CU=; b=oW8CmVYmKUpuCIvNHpteShhV3OH0DGAgVuLDRvI9MU8d1hLV+jWkRmmr1y3jAtsrpv yWxr1VjXGsuRNSIaKMLK2yBiC14L/UFKt6hCDIapSmkV1E6lA3WweU/B84590cRGqlWK ro/ptbgRCXLPfW4vk4Jr+kjgCnBN/POIX1Y/jwBPDr6cpurm733ZCPOlT3TpMP+z2C2U XiTAtxRkTFXLHQ0pMlBVibn16tcthUeSt5ATB8s6G/tsjnf23+N4oQV3XJunjfqpZPGD GWJVNuT7+52U+R3MCvDQTH4965coEe3fDs/oawsGcn8FzEW6GycCjF8KDfPXWq6Cy8X/ N7Sg== X-Gm-Message-State: AOAM533aa4+cTzQG3fEfgumaW2HP4o56zlPlUM4vTa6ZM7OUGYGhbyOp iKUOFmxebVNlLZPn8J3dchVgi1wja4P8 X-Received: from connoro.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:a99]) (user=connoro job=sendgmr) by 2002:aa7:9019:0:b0:4fa:7532:9551 with SMTP id m25-20020aa79019000000b004fa75329551mr25987515pfo.26.1653341297002; Mon, 23 May 2022 14:28:17 -0700 (PDT) Date: Mon, 23 May 2022 21:28:08 +0000 Message-Id: <20220523212808.603526-1-connoro@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.36.1.124.g0e6072fb45-goog Subject: [PATCH bpf-next v2] libbpf: also check /sys/kernel/tracing for tracefs files From: "Connor O'Brien" To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, "Connor O'Brien" Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org libbpf looks for tracefs files only under debugfs, but tracefs may be mounted even if debugfs is not. When /sys/kernel/debug/tracing is absent, try looking under /sys/kernel/tracing instead. Signed-off-by: Connor O'Brien --- v1->v2: cache result of debugfs check. src/libbpf.c | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/libbpf.c b/src/libbpf.c index 2262bcd..cc47c52 100644 --- a/src/libbpf.c +++ b/src/libbpf.c @@ -9945,10 +9945,22 @@ static void gen_kprobe_legacy_event_name(char *buf, size_t buf_sz, __sync_fetch_and_add(&index, 1)); } +static bool debugfs_available(void) +{ + static bool initialized = false, available; + + if (!initialized) { + available = !access("/sys/kernel/debug/tracing", F_OK); + initialized = true; + } + return available; +} + static int add_kprobe_event_legacy(const char *probe_name, bool retprobe, const char *kfunc_name, size_t offset) { - const char *file = "/sys/kernel/debug/tracing/kprobe_events"; + const char *file = debugfs_available() ? "/sys/kernel/debug/tracing/kprobe_events" : + "/sys/kernel/tracing/kprobe_events"; return append_to_file(file, "%c:%s/%s %s+0x%zx", retprobe ? 'r' : 'p', @@ -9958,7 +9970,8 @@ static int add_kprobe_event_legacy(const char *probe_name, bool retprobe, static int remove_kprobe_event_legacy(const char *probe_name, bool retprobe) { - const char *file = "/sys/kernel/debug/tracing/kprobe_events"; + const char *file = debugfs_available() ? "/sys/kernel/debug/tracing/kprobe_events" : + "/sys/kernel/tracing/kprobe_events"; return append_to_file(file, "-:%s/%s", retprobe ? "kretprobes" : "kprobes", probe_name); } @@ -9968,7 +9981,8 @@ static int determine_kprobe_perf_type_legacy(const char *probe_name, bool retpro char file[256]; snprintf(file, sizeof(file), - "/sys/kernel/debug/tracing/events/%s/%s/id", + debugfs_available() ? "/sys/kernel/debug/tracing/events/%s/%s/id" : + "/sys/kernel/tracing/events/%s/%s/id", retprobe ? "kretprobes" : "kprobes", probe_name); return parse_uint_from_file(file, "%d\n"); @@ -10144,7 +10158,8 @@ static void gen_uprobe_legacy_event_name(char *buf, size_t buf_sz, static inline int add_uprobe_event_legacy(const char *probe_name, bool retprobe, const char *binary_path, size_t offset) { - const char *file = "/sys/kernel/debug/tracing/uprobe_events"; + const char *file = debugfs_available() ? "/sys/kernel/debug/tracing/uprobe_events" : + "/sys/kernel/tracing/uprobe_events"; return append_to_file(file, "%c:%s/%s %s:0x%zx", retprobe ? 'r' : 'p', @@ -10154,7 +10169,8 @@ static inline int add_uprobe_event_legacy(const char *probe_name, bool retprobe, static inline int remove_uprobe_event_legacy(const char *probe_name, bool retprobe) { - const char *file = "/sys/kernel/debug/tracing/uprobe_events"; + const char *file = debugfs_available() ? "/sys/kernel/debug/tracing/uprobe_events" : + "/sys/kernel/tracing/uprobe_events"; return append_to_file(file, "-:%s/%s", retprobe ? "uretprobes" : "uprobes", probe_name); } @@ -10164,7 +10180,8 @@ static int determine_uprobe_perf_type_legacy(const char *probe_name, bool retpro char file[512]; snprintf(file, sizeof(file), - "/sys/kernel/debug/tracing/events/%s/%s/id", + debugfs_available() ? "/sys/kernel/debug/tracing/events/%s/%s/id" : + "/sys/kernel/tracing/events/%s/%s/id", retprobe ? "uretprobes" : "uprobes", probe_name); return parse_uint_from_file(file, "%d\n"); @@ -10295,7 +10312,8 @@ static int determine_tracepoint_id(const char *tp_category, int ret; ret = snprintf(file, sizeof(file), - "/sys/kernel/debug/tracing/events/%s/%s/id", + debugfs_available() ? "/sys/kernel/debug/tracing/events/%s/%s/id" : + "/sys/kernel/tracing/events/%s/%s/id", tp_category, tp_name); if (ret < 0) return -errno; -- 2.36.1.124.g0e6072fb45-goog