Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp208500imm; Fri, 13 Jul 2018 21:58:39 -0700 (PDT) X-Google-Smtp-Source: AAOMgpehsQDozXjzX+fn6DuqcYase/ia2MCOwx7ySZkPr4A4VO/mjl7R/Mh9q20wbCDVoXXUXLP/ X-Received: by 2002:a65:5286:: with SMTP id y6-v6mr8422667pgp.65.1531544319315; Fri, 13 Jul 2018 21:58:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531544319; cv=none; d=google.com; s=arc-20160816; b=0CrzLN3P8ktK3zNL+8Kl+1zhm2FOEXu9p9zKMRbneM13iag3wPBivXTan8mAblzmBo ja0yzJKI717Rpe+2QLCRbSsweTJ85dItOjtFbklFKTDUhJGwf7LBePmaEaF7kT8K6dwa g/IUrAxAJBy1uhPsvBM03xzUXhdWtl/KoHV/7iUeU71HzazLPKjSPDWCAJ8uWFmahydA LDf8Q2iqupOEb8l+5OWSlXaPOELrx0q+04tUFO3peciC5lZkbJj/gV9IWsYEMBEcPzxq FIljzJisALsn2ef06ej8lytwpOKtXQscFrvZ0AoniQ7GmpTZR9Bb55Q22YnuVitjcUxu 47yA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:smtp-origin-cluster:cc:to :smtp-origin-hostname:from:smtp-origin-hostprefix:dkim-signature :arc-authentication-results; bh=CXnTOraIabDKL+Zy8Zj4bQVbeSa8xYxd+OawgjtG+Mw=; b=nZ5rOQbwM2JcmmlHQ+D3cgqWVq7qe7z2wRIWGxJ87ATYzH0wIM3WCwq0FVNuf29i5V rRDoVUtrx69vJ72AokLFqcuZuUq9OGHohmVZ0e7y7KqE2Zlu+wVzQdy7vm94jIBprGli S5QfJuFaLDmj94r1ANkFwGz9aWhyFlxskIAix9IRqWyAJ/BiQ7keyugwDfLQ2JTM1gwB UdXECbzVyVlr3HWjXEIvNh0ghDDUUKv1rDD9VpjzxzjOw2hUymPHLS7+4U0WRUgmO3tc pnenU5HHQzzfNk5H+f72IeiuraeJQ4Cg8F8+nopqpfS5zuuEh1Ar9LkIQpvSxxI+4ElB pilA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=EGJAiCAl; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=fb.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d32-v6si24671183pla.329.2018.07.13.21.58.10; Fri, 13 Jul 2018 21:58:39 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=EGJAiCAl; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=fb.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726412AbeGNFPD (ORCPT + 99 others); Sat, 14 Jul 2018 01:15:03 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:42694 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725851AbeGNFPC (ORCPT ); Sat, 14 Jul 2018 01:15:02 -0400 Received: from pps.filterd (m0109331.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w6E4qiud023281 for ; Fri, 13 Jul 2018 21:57:23 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=facebook; bh=CXnTOraIabDKL+Zy8Zj4bQVbeSa8xYxd+OawgjtG+Mw=; b=EGJAiCAlLdDpwo6tooYmckyeBsTpAQREgPotQR+OHAye1JJgnocqfpL2bqoD9A4NnoGM 2MmygCnXkjL/n2Ko9aZLqNt2kKrJNlVaNPM2vT1ktjOFDHEuHNjataSDGhus+nih5/zu S7LYod3b968LJwo3CU2n+T/BxrovuRFUpkU= Received: from mail.thefacebook.com ([199.201.64.23]) by mx0a-00082601.pphosted.com with ESMTP id 2k7a3ag16h-5 (version=TLSv1 cipher=ECDHE-RSA-AES256-SHA bits=256 verify=NOT) for ; Fri, 13 Jul 2018 21:57:23 -0700 Received: from mx-out.facebook.com (192.168.52.123) by mail.thefacebook.com (192.168.16.16) with Microsoft SMTP Server (TLS) id 14.3.361.1; Fri, 13 Jul 2018 21:57:22 -0700 Received: by dev026.lla1.facebook.com (Postfix, from userid 152872) id 34D9A8A443B; Fri, 13 Jul 2018 21:57:14 -0700 (PDT) Smtp-Origin-Hostprefix: dev From: Okash Khawaja Smtp-Origin-Hostname: dev026.lla1.facebook.com To: Daniel Borkmann , Martin KaFai Lau , Alexei Starovoitov , Yonghong Song , Quentin Monnet , Jakub Kicinski , "David S . Miller" CC: , , Smtp-Origin-Cluster: lla1c22 Subject: [PATCH bpf-next v5 1/3] bpf: btf: export btf types and name by offset from lib Date: Fri, 13 Jul 2018 21:57:02 -0700 Message-ID: <20180714045704.3779427-2-osk@fb.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180714045704.3779427-1-osk@fb.com> References: <20180714045704.3779427-1-osk@fb.com> X-FB-Internal: Safe MIME-Version: 1.0 Content-Type: text/plain X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2018-07-14_02:,, signatures=0 X-Proofpoint-Spam-Reason: safe X-FB-Internal: Safe Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch introduces btf__resolve_type() function and exports two existing functions from libbpf. btf__resolve_type follows modifier types like const and typedef until it hits a type which actually takes up memory, and then returns it. This function follows similar pattern to btf__resolve_size but instead of computing size, it just returns the type. These functions will be used in the followig patch which parses information inside array of `struct btf_type *`. btf_name_by_offset is used for printing variable names. Signed-off-by: Okash Khawaja Acked-by: Martin KaFai Lau Acked-by: Song Liu Reviewed-by: Jakub Kicinski --- tools/lib/bpf/btf.c | 65 ++++++++++++++++++++++++++++++++++++---------------- tools/lib/bpf/btf.h | 3 ++ 2 files changed, 48 insertions(+), 20 deletions(-) --- a/tools/lib/bpf/btf.c +++ b/tools/lib/bpf/btf.c @@ -17,6 +17,11 @@ #define BTF_MAX_NR_TYPES 65535 +#define IS_MODIFIER(k) (((k) == BTF_KIND_TYPEDEF) || \ + ((k) == BTF_KIND_VOLATILE) || \ + ((k) == BTF_KIND_CONST) || \ + ((k) == BTF_KIND_RESTRICT)) + static struct btf_type btf_void; struct btf { @@ -33,14 +38,6 @@ struct btf { int fd; }; -static const char *btf_name_by_offset(const struct btf *btf, uint32_t offset) -{ - if (offset < btf->hdr->str_len) - return &btf->strings[offset]; - else - return NULL; -} - static int btf_add_type(struct btf *btf, struct btf_type *t) { if (btf->types_size - btf->nr_types < 2) { @@ -190,15 +187,6 @@ static int btf_parse_type_sec(struct btf return 0; } -static const struct btf_type *btf_type_by_id(const struct btf *btf, - uint32_t type_id) -{ - if (type_id > btf->nr_types) - return NULL; - - return btf->types[type_id]; -} - static bool btf_type_is_void(const struct btf_type *t) { return t == &btf_void || BTF_INFO_KIND(t->info) == BTF_KIND_FWD; @@ -234,7 +222,7 @@ int64_t btf__resolve_size(const struct b int64_t size = -1; int i; - t = btf_type_by_id(btf, type_id); + t = btf__type_by_id(btf, type_id); for (i = 0; i < MAX_RESOLVE_DEPTH && !btf_type_is_void_or_null(t); i++) { size = btf_type_size(t); @@ -259,7 +247,7 @@ int64_t btf__resolve_size(const struct b return -EINVAL; } - t = btf_type_by_id(btf, type_id); + t = btf__type_by_id(btf, type_id); } if (size < 0) @@ -271,6 +259,26 @@ int64_t btf__resolve_size(const struct b return nelems * size; } +int btf__resolve_type(const struct btf *btf, __u32 type_id) +{ + const struct btf_type *t; + int depth = 0; + + t = btf__type_by_id(btf, type_id); + while (depth < MAX_RESOLVE_DEPTH && + !btf_type_is_void_or_null(t) && + IS_MODIFIER(BTF_INFO_KIND(t->info))) { + type_id = t->type; + t = btf__type_by_id(btf, type_id); + depth++; + } + + if (depth == MAX_RESOLVE_DEPTH || btf_type_is_void_or_null(t)) + return -EINVAL; + + return type_id; +} + int32_t btf__find_by_name(const struct btf *btf, const char *type_name) { uint32_t i; @@ -280,7 +288,7 @@ int32_t btf__find_by_name(const struct b for (i = 1; i <= btf->nr_types; i++) { const struct btf_type *t = btf->types[i]; - const char *name = btf_name_by_offset(btf, t->name_off); + const char *name = btf__name_by_offset(btf, t->name_off); if (name && !strcmp(type_name, name)) return i; @@ -371,3 +379,20 @@ int btf__fd(const struct btf *btf) { return btf->fd; } + +const char *btf__name_by_offset(const struct btf *btf, __u32 offset) +{ + if (offset < btf->hdr->str_len) + return &btf->strings[offset]; + else + return NULL; +} + +const struct btf_type *btf__type_by_id(const struct btf *btf, + __u32 type_id) +{ + if (type_id > btf->nr_types) + return NULL; + + return btf->types[type_id]; +} --- a/tools/lib/bpf/btf.h +++ b/tools/lib/bpf/btf.h @@ -17,6 +17,9 @@ void btf__free(struct btf *btf); struct btf *btf__new(uint8_t *data, uint32_t size, btf_print_fn_t err_log); int32_t btf__find_by_name(const struct btf *btf, const char *type_name); int64_t btf__resolve_size(const struct btf *btf, uint32_t type_id); +int btf__resolve_type(const struct btf *btf, __u32 type_id); int btf__fd(const struct btf *btf); +const char *btf__name_by_offset(const struct btf *btf, __u32 offset); +const struct btf_type *btf__type_by_id(const struct btf *btf, __u32 type_id); #endif