From patchwork Mon Aug 7 02:54:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Masami Hiramatsu (Google)" X-Patchwork-Id: 131655 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp1212511vqr; Sun, 6 Aug 2023 21:00:42 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHAlPAZFIE8jelVYOwVDA0ZEuAgNMngi4mCubYAaBQnTf3LkDQVNU6To1M3yHMK8BDy09HG X-Received: by 2002:a17:906:156:b0:99b:5ba6:f2cf with SMTP id 22-20020a170906015600b0099b5ba6f2cfmr6140912ejh.26.1691380841716; Sun, 06 Aug 2023 21:00:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691380841; cv=none; d=google.com; s=arc-20160816; b=U/B8sKkLUO5gYOPewVpbph1O3LSfiqRbRC9VtyQaf0dbSmh+ArlK6rSofNc309xBDo FLAqmCKpjPXMN93hrlBRy35LxuRBPchZIgFUNa1EvrnGeBKOE9dbBYxYDCJTtTkq1bjH CKpkL5fORUv/kVe5erAmS3vgwUsYHftg2F8NStqEjUG3y9MLvjQyltpZ6P2ZX4OYP4zV usgQsoqYn2a8iVHeXfd2i+C7LMy/Y1XPjR0Frzqnm38PSHe8GDGGsZQyLCBRmc5u7hfc zsLwPChETD9BG5iiRYTpQ8yH2s1CLjR6AhvBg0ba0xqk2w4SYXAMVmfGqzD4yGbXyu+4 3ybA== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=z5bD/fowAtp6Bz1Dos+34LmHLP2+agBPEKaP1tHxuX8=; fh=raxTTJvC+dSJYbgfX5JG5gvrVjxTZXczN0HF8iDayLY=; b=ze0ySATh+HtE/9nDlX7C2ZfZiVwfudSFFEJ19HmGcBGMblY/1qxxeloKpMFxYwRgM4 2++1F2pYdaz/FX49AbY5wtHEGBKZEB6t9VA7lEqIQnprrcaZIJBgB6XkRvO8XfGG52Sn O/6lSNGTe7nDp2tdzsLlH984LiDu18pBdeTHSbsulOJJwbVwLqQb5OWOQ6IbJOZ4aPf7 RwoJVL0SNFSXyZnIupMGzGRk3pTQ+Jj7IPzdzYTY5u8SzEmqRlqA9N6Syr6JOt5WKXiB 8hZt7hzEaGmccWw+7X46Bd2cRrm1s1mWUwzzZYALCX0PwtKtB3uInEGowEbeBPPUvPY7 lmSQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="qCZG/V3f"; 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=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o20-20020a1709061d5400b0099bbc75eb36si4317731ejh.47.2023.08.06.21.00.17; Sun, 06 Aug 2023 21:00:41 -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=@kernel.org header.s=k20201202 header.b="qCZG/V3f"; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229720AbjHGCzd (ORCPT + 99 others); Sun, 6 Aug 2023 22:55:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48222 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229521AbjHGCzc (ORCPT ); Sun, 6 Aug 2023 22:55:32 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 88DE81BD2; Sun, 6 Aug 2023 19:55:04 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id CE6FD61337; Mon, 7 Aug 2023 02:55:03 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3EC5AC433C7; Mon, 7 Aug 2023 02:55:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1691376903; bh=rV1bOw0hiZEtTOaUVe7fSzFVckAJ245a4sD8LTyT1SE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qCZG/V3fcbZVNuMOaTj3EJJObTyrXo5Q4DaxzsTy7pvcVIHUFsu1FKiF5Hu5cc+MY LcKUM95FijXPv+8p5I+hYNeoi8YsfasDjOC/teENOMP50uosuuXwTq0XH/aMPN6nv2 YkocZgQOhBO706c6uE51VhTbz7YaOXjPiG2tbgYfYmrxrXBkrK9Md+3A3QQc1hRKof wOl1toGSyJ6TDnj44HCBykS5FII3CYI456r7PDovemhXAUzXrDmfvOHEo82M245tcO UVa0qEtZuw0B1Jpl+y2QW6uR/hDz6ZIwBTklAaeU63ciMMDH9+zCVR3ekCOVxjjTSD k3aLJxZAK5uOA== From: "Masami Hiramatsu (Google)" To: linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Steven Rostedt , mhiramat@kernel.org, Martin KaFai Lau , bpf@vger.kernel.org, Sven Schnelle , Alexei Starovoitov Subject: [PATCH v5 3/9] tracing/probes: Add a function to search a member of a struct/union Date: Mon, 7 Aug 2023 11:54:58 +0900 Message-Id: <169137689818.271367.4200174950023036516.stgit@devnote2> X-Mailer: git-send-email 2.34.1 In-Reply-To: <169137686814.271367.11218568219311636206.stgit@devnote2> References: <169137686814.271367.11218568219311636206.stgit@devnote2> User-Agent: StGit/0.19 MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773541357502702657 X-GMAIL-MSGID: 1773541357502702657 From: Masami Hiramatsu (Google) Add btf_find_struct_member() API to search a member of a given data structure or union from the member's name. Signed-off-by: Masami Hiramatsu (Google) Reviewed-by: Alan Maguire --- Changes in v3: - Remove simple input check. - Fix unneeded IS_ERR_OR_NULL() check for btf_type_by_id(). - Move the code next to btf_get_func_param(). - Use for_each_member() macro instead of for-loop. - Use btf_type_skip_modifiers() instead of btf_type_by_id(). Changes in v4: - Use a stack for searching in anonymous members instead of nested call. Changes in v5: - Move the generic functions into a new file, trace_btf.c, for avoiding tree-merge conflict. - Return anon_offset for calculating correct offset from root struct. --- kernel/trace/trace_btf.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++ kernel/trace/trace_btf.h | 4 +++ 2 files changed, 61 insertions(+) diff --git a/kernel/trace/trace_btf.c b/kernel/trace/trace_btf.c index 82cbbeeda071..109a238437cf 100644 --- a/kernel/trace/trace_btf.c +++ b/kernel/trace/trace_btf.c @@ -50,3 +50,60 @@ const struct btf_param *btf_get_func_param(const struct btf_type *func_proto, s3 return NULL; } +#define BTF_ANON_STACK_MAX 16 + +/* + * Find a member of data structure/union by name and return it. + * Return NULL if not found, or -EINVAL if parameter is invalid. + * If the member is an member of anonymous union/structure, the offset + * of that anonymous union/structure is stored into @anon_offset. Caller + * can calculate the correct offset from the root data structure by + * adding anon_offset to the member's offset. + */ +const struct btf_member *btf_find_struct_member(struct btf *btf, + const struct btf_type *type, + const char *member_name, + u32 *anon_offset) +{ + struct { + u32 tid; + u32 offset; + } anon_stack[BTF_ANON_STACK_MAX]; + const struct btf_member *member; + u32 tid, cur_offset = 0; + const char *name; + int i, top = 0; + +retry: + if (!btf_type_is_struct(type)) + return ERR_PTR(-EINVAL); + + for_each_member(i, type, member) { + if (!member->name_off) { + /* Anonymous union/struct: push it for later use */ + type = btf_type_skip_modifiers(btf, member->type, &tid); + if (type && top < BTF_ANON_STACK_MAX) { + anon_stack[top].tid = tid; + anon_stack[top++].offset = + cur_offset + member->offset; + } + } else { + name = btf_name_by_offset(btf, member->name_off); + if (name && !strcmp(member_name, name)) { + if (anon_offset) + *anon_offset = cur_offset; + return member; + } + } + } + if (top > 0) { + /* Pop from the anonymous stack and retry */ + tid = anon_stack[--top].tid; + cur_offset = anon_stack[top].offset; + type = btf_type_by_id(btf, tid); + goto retry; + } + + return NULL; +} + diff --git a/kernel/trace/trace_btf.h b/kernel/trace/trace_btf.h index 2ef1b2367e1e..fb2919a74225 100644 --- a/kernel/trace/trace_btf.h +++ b/kernel/trace/trace_btf.h @@ -5,3 +5,7 @@ const struct btf_type *btf_find_func_proto(const char *func_name, struct btf **btf_p); const struct btf_param *btf_get_func_param(const struct btf_type *func_proto, s32 *nr); +const struct btf_member *btf_find_struct_member(struct btf *btf, + const struct btf_type *type, + const char *member_name, + u32 *anon_offset);