Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp1032108pxb; Thu, 5 Nov 2020 21:56:39 -0800 (PST) X-Google-Smtp-Source: ABdhPJzgqDtFMHBuz73czpcfziaReMx6CeZDTkTszMw14Dw79Focgn/w8NXKQ6i0GLJq818lhbvd X-Received: by 2002:a17:906:32d7:: with SMTP id k23mr468178ejk.294.1604642199703; Thu, 05 Nov 2020 21:56:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604642199; cv=none; d=google.com; s=arc-20160816; b=w3hWErhfxsdJ5kcWiKrwDq4G21l1ZA23s8jIXXJzEqlIhguxK9o2mTH8fHrJ/fo9x0 qlwDdtMcT7dRwp7RydVfxLQzeo1vsn2GnqTj4l9F+26ZywPWJaaW3Xua+ZJ9Rtrvir2P tbKy/gBrUxJ0S/RAyYmu9234w1BNZNgQVtoRXdGuZ/FYVLK0JOPB5FeO7fhZ9d+NKj4L cVZ5l/fbSVEHB3B/xoSu7j2c6wwJwN8vjWo+lXRGs+EstGO0KBUxhNqmDXHPzc7xdhRi VxYhfxwKW5Ax0I7rOqH+hXUEE5/9akh9FqqHPrnMZOKUOpTKyylmicX3xciz620zlK9v AhuA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=64Qq8PSoVKqGhVqKgIGDjYSnJYcasLRY3aQ8jeK/blY=; b=zMXjmTzaum+05tyC2LH2fmyFpTwPzX14SO4y29abjzkxOadTMxkMiNvDRobnZSRQVK 3/XPCx9zX2d014bd8OlStVpJqpJgw5l5fCJHUFa0+AMvexvXxgas69cwx6/EHfeA61LW zCt9+SgZSKF7+hsIy56PId1Gzhcy6Z6ZknUJ+W7W76WyiIeV/WXdT3gN8GKjWYbOLayP un+F0vzA0ffttO7ngK4Lq6aH1VHaendUYe4iulwf/T3R6Re3TXa85Ci88w2MxBdiM2Dr merXnQOn4OzGQmP5VJfw8tDymYUzvGNlv7kI3jpnwFPR5D8H0eb+mfGmGiW/lWajAYgO Nmvw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h14si112261ejs.645.2020.11.05.21.56.17; Thu, 05 Nov 2020 21:56:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726287AbgKFFyi convert rfc822-to-8bit (ORCPT + 99 others); Fri, 6 Nov 2020 00:54:38 -0500 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:10488 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726126AbgKFFyi (ORCPT ); Fri, 6 Nov 2020 00:54:38 -0500 Received: from pps.filterd (m0109331.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 0A65e7sc005581 for ; Thu, 5 Nov 2020 21:54:37 -0800 Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com with ESMTP id 34mw5bgrwp-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Thu, 05 Nov 2020 21:54:37 -0800 Received: from intmgw001.03.ash8.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:83::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1979.3; Thu, 5 Nov 2020 21:54:36 -0800 Received: by devbig012.ftw2.facebook.com (Postfix, from userid 137359) id C2A1B2EC8EFE; Thu, 5 Nov 2020 21:51:40 -0800 (PST) From: Andrii Nakryiko To: , , , CC: , , , , Andrii Nakryiko , Arnaldo Carvalho de Melo , Greg Kroah-Hartman Subject: [PATCH bpf-next 5/5] tools/bpftool: add support for in-kernel and named BTF in `btf show` Date: Thu, 5 Nov 2020 21:51:10 -0800 Message-ID: <20201106055111.3972047-6-andrii@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20201106055111.3972047-1-andrii@kernel.org> References: <20201106055111.3972047-1-andrii@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8BIT X-FB-Internal: Safe Content-Type: text/plain X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.312,18.0.737 definitions=2020-11-06_01:2020-11-05,2020-11-06 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 malwarescore=0 bulkscore=0 spamscore=0 impostorscore=0 lowpriorityscore=0 phishscore=0 suspectscore=9 adultscore=0 mlxlogscore=999 clxscore=1015 mlxscore=0 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2011060039 X-FB-Internal: deliver Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Display vmlinux BTF name and kernel module names when listing available BTFs on the system. In human-readable output mode, module BTFs are reported with "name [module-name]", while vmlinux BTF will be reported as "name [vmlinux]". Square brackets are added by bpftool and follow kernel convention when displaying modules in human-readable text outputs. [vmuser@archvm bpf]$ sudo ../../../bpf/bpftool/bpftool btf s 1: name [vmlinux] size 4082281B 6: size 2365B prog_ids 8,6 map_ids 3 7: name [button] size 46895B 8: name [pcspkr] size 42328B 9: name [serio_raw] size 39375B 10: name [floppy] size 57185B 11: name [i2c_core] size 76186B 12: name [crc32c_intel] size 16036B 13: name [i2c_piix4] size 50497B 14: name [irqbypass] size 14124B 15: name [kvm] size 197985B 16: name [kvm_intel] size 123564B 17: name [cryptd] size 42466B 18: name [crypto_simd] size 17187B 19: name [glue_helper] size 39205B 20: name [aesni_intel] size 41034B 25: size 36150B pids bpftool(2519) In JSON mode, two fields (boolean "kernel" and string "name") are reported for each BTF object. vmlinux BTF is reported with name "vmlinux" (kernel itself returns and empty name for vmlinux BTF). [vmuser@archvm bpf]$ sudo ../../../bpf/bpftool/bpftool btf s -jp [{ "id": 1, "size": 4082281, "prog_ids": [], "map_ids": [], "kernel": true, "name": "vmlinux" },{ "id": 6, "size": 2365, "prog_ids": [8,6 ], "map_ids": [3 ], "kernel": false },{ "id": 7, "size": 46895, "prog_ids": [], "map_ids": [], "kernel": true, "name": "button" },{ ... Signed-off-by: Andrii Nakryiko --- tools/bpf/bpftool/btf.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c index c96b56e8e3a4..ed5e97157241 100644 --- a/tools/bpf/bpftool/btf.c +++ b/tools/bpf/bpftool/btf.c @@ -742,9 +742,14 @@ show_btf_plain(struct bpf_btf_info *info, int fd, struct btf_attach_table *btf_map_table) { struct btf_attach_point *obj; + const char *name = u64_to_ptr(info->name); int n; printf("%u: ", info->id); + if (info->kernel_btf) + printf("name [%s] ", name); + else if (name && name[0]) + printf("name %s ", name); printf("size %uB", info->btf_size); n = 0; @@ -771,6 +776,7 @@ show_btf_json(struct bpf_btf_info *info, int fd, struct btf_attach_table *btf_map_table) { struct btf_attach_point *obj; + const char *name = u64_to_ptr(info->name); jsonw_start_object(json_wtr); /* btf object */ jsonw_uint_field(json_wtr, "id", info->id); @@ -796,6 +802,11 @@ show_btf_json(struct bpf_btf_info *info, int fd, emit_obj_refs_json(&refs_table, info->id, json_wtr); /* pids */ + jsonw_bool_field(json_wtr, "kernel", info->kernel_btf); + + if (name && name[0]) + jsonw_string_field(json_wtr, "name", name); + jsonw_end_object(json_wtr); /* btf object */ } @@ -803,15 +814,30 @@ static int show_btf(int fd, struct btf_attach_table *btf_prog_table, struct btf_attach_table *btf_map_table) { - struct bpf_btf_info info = {}; + struct bpf_btf_info info; __u32 len = sizeof(info); + char name[64]; int err; + memset(&info, 0, sizeof(info)); err = bpf_obj_get_info_by_fd(fd, &info, &len); if (err) { p_err("can't get BTF object info: %s", strerror(errno)); return -1; } + /* if kernel support emitting BTF object name, pass name pointer */ + if (info.name_len) { + memset(&info, 0, sizeof(info)); + info.name_len = sizeof(name); + info.name = ptr_to_u64(name); + len = sizeof(info); + + err = bpf_obj_get_info_by_fd(fd, &info, &len); + if (err) { + p_err("can't get BTF object info: %s", strerror(errno)); + return -1; + } + } if (json_output) show_btf_json(&info, fd, btf_prog_table, btf_map_table); -- 2.24.1