Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp1443028iob; Fri, 29 Apr 2022 05:33:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxe3YznP3PjxBRv5TUAUuw1pyGGSReaWeyEhBKwmFOwOUFV3hx2ANxDDJDYOpeCR/T4x++n X-Received: by 2002:a05:6a00:114b:b0:4f7:915:3ec3 with SMTP id b11-20020a056a00114b00b004f709153ec3mr40161867pfm.8.1651235593508; Fri, 29 Apr 2022 05:33:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651235593; cv=none; d=google.com; s=arc-20160816; b=dDaIfl94ZycQsngq/EPIjXTIiP3O5sHObNwIpg9hZI/bp70IyC+cNEgWmTbi5zr4xy ZwIF9A7NKUQk58PNiQRH1AnhH+YtvBa9w9H400vjMBjqUiUFm8cf4jVvdhxdk7S2Ru5+ kXZhAiHYk09Qvu4+fVasRVlLWobx2a0bijYQuNlnxAsE/HOrBx4BfWXmwzwkemVO13oB e5VJ7a05wR1Y3nBUdk8/NEBkZWDOJlUP9gL4C5hoeZQ8PHryV0N6HVSOHciX2pKl8dst OVd6A5nMHB/SuqEDGo+bR47Oy40rKO/tfYCcyRyfbMDyf0gb7+gl4N8YsliKVm34Bh4N 5eQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=NNbdnm6VF5w4R3+oVI1cQUGY2fGXX9HDrHfwIJGLLqc=; b=ntWqMC9igKP12YXIOKkMT+pA3+7O2BIF1spRBMwM4QCW5FjQkGT6uSAH4KHAJFW+Wz GDGFn9aI+jsnGH7QZ3PZ+0uJ1J1et2HIWwUZhqT9eZ3mvo2UrH6UgqDkLwFE3bX9zjKb pcjtTbrvVKyRjmwO4+uQaUUCsxnWxEq5TnI5PLmpp+P0IVFxagFtvoRMdHE56q9K3y0j tCep/XwL7R8WoO+K7z0YIei8nBeLVQ9PAiJvmOLrYLumGBcXsDh+M0nD7DLgMq9eoqRw e1/1Lc5fNfYZiOf6iaL3YZNZ6IpwLODkbOMk+XCOvorzmoOyonUttuZUEzEnUmPvfI+Z wUiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="Meg06O/K"; 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=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j7-20020a170902da8700b0015d1f4d22b6si7899721plx.133.2022.04.29.05.32.53; Fri, 29 Apr 2022 05:33:13 -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=@intel.com header.s=Intel header.b="Meg06O/K"; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235174AbiD1L04 (ORCPT + 99 others); Thu, 28 Apr 2022 07:26:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34326 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235063AbiD1L0y (ORCPT ); Thu, 28 Apr 2022 07:26:54 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C0A505E16C; Thu, 28 Apr 2022 04:23:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1651145020; x=1682681020; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=YSpDATXy/AEnQCqNpDDdMNwfxMpapZmzIYX8S6XiNuc=; b=Meg06O/KeVEOvbcvwHOkss7GumVYJbLxc7P6jPEMBnqpwHb/QLaA6cgV O5K1DarW7M96UkAn6pPR19Ajk+0uAjvYBE+PVtdj9vSTKsHgovLKg6yAM 7oVH33UaGpGYGUxDOtTES+uhH/5+t4OHzEGCkl+JSYYrbcmUujm3JMrs2 eqy6VWYmVRbak3YIZlcumeibMrbOyQk9bnwXIxIwjkvNwAss4JfeO9/JM S40mPfl6MM5wYCcZYRhGFaARym7sxnII1RC10yfGnNXDt+N6/XUCuR1/u A2/7x2saHMNYbo2IMD7qj7uTXSzzWxb0LZhiNnLIic3qhMOIUk8feNvzq w==; X-IronPort-AV: E=McAfee;i="6400,9594,10330"; a="352692120" X-IronPort-AV: E=Sophos;i="5.90,295,1643702400"; d="scan'208";a="352692120" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Apr 2022 04:23:40 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.90,295,1643702400"; d="scan'208";a="541152152" Received: from irvmail001.ir.intel.com ([10.43.11.63]) by orsmga002.jf.intel.com with ESMTP; 28 Apr 2022 04:23:37 -0700 Received: from lincoln.igk.intel.com (lincoln.igk.intel.com [10.102.21.235]) by irvmail001.ir.intel.com (8.14.3/8.13.6/MailSET/Hub) with ESMTP id 23SBNZi8003960; Thu, 28 Apr 2022 12:23:35 +0100 From: Larysa Zaremba To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , Maciej Fijalkowski , Larysa Zaremba , Alexander Lobakin Subject: [PATCH RESEND bpf-next] bpftool: Use sysfs vmlinux when dumping BTF by ID Date: Thu, 28 Apr 2022 13:14:42 +0200 Message-Id: <20220428111442.111805-1-larysa.zaremba@intel.com> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-5.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham 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 Currently, dumping almost all BTFs specified by id requires using the -B option to pass the base BTF. For most cases the vmlinux BTF sysfs path should work. This patch simplifies dumping by ID usage by attempting to use vmlinux BTF from sysfs, if the first try of loading BTF by ID fails with certain conditions. Signed-off-by: Larysa Zaremba Reviewed-by: Alexander Lobakin --- tools/bpf/bpftool/btf.c | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c index a2c665beda87..557f65e2de5c 100644 --- a/tools/bpf/bpftool/btf.c +++ b/tools/bpf/bpftool/btf.c @@ -459,6 +459,22 @@ static int dump_btf_c(const struct btf *btf, return err; } +static const char sysfs_vmlinux[] = "/sys/kernel/btf/vmlinux"; + +static struct btf *get_vmlinux_btf_from_sysfs(void) +{ + struct btf *base; + + base = btf__parse(sysfs_vmlinux, NULL); + if (libbpf_get_error(base)) { + p_err("failed to parse vmlinux BTF at '%s': %ld\n", + sysfs_vmlinux, libbpf_get_error(base)); + base = NULL; + } + + return base; +} + static int do_dump(int argc, char **argv) { struct btf *btf = NULL, *base = NULL; @@ -536,18 +552,11 @@ static int do_dump(int argc, char **argv) NEXT_ARG(); } else if (is_prefix(src, "file")) { const char sysfs_prefix[] = "/sys/kernel/btf/"; - const char sysfs_vmlinux[] = "/sys/kernel/btf/vmlinux"; if (!base_btf && strncmp(*argv, sysfs_prefix, sizeof(sysfs_prefix) - 1) == 0 && - strcmp(*argv, sysfs_vmlinux) != 0) { - base = btf__parse(sysfs_vmlinux, NULL); - if (libbpf_get_error(base)) { - p_err("failed to parse vmlinux BTF at '%s': %ld\n", - sysfs_vmlinux, libbpf_get_error(base)); - base = NULL; - } - } + strcmp(*argv, sysfs_vmlinux)) + base = get_vmlinux_btf_from_sysfs(); btf = btf__parse_split(*argv, base ?: base_btf); err = libbpf_get_error(btf); @@ -593,6 +602,14 @@ static int do_dump(int argc, char **argv) if (!btf) { btf = btf__load_from_kernel_by_id_split(btf_id, base_btf); err = libbpf_get_error(btf); + if (err == -EINVAL && !base_btf) { + btf__free(base); + base = get_vmlinux_btf_from_sysfs(); + p_info("Warning: valid base BTF was not specified with -B option, falling back on standard base BTF (sysfs vmlinux)"); + btf = btf__load_from_kernel_by_id_split(btf_id, base); + err = libbpf_get_error(btf); + } + if (err) { p_err("get btf by id (%u): %s", btf_id, strerror(err)); goto done; -- 2.35.1