Received: by 2002:ab2:620c:0:b0:1ef:ffd0:ce49 with SMTP id o12csp666640lqt; Mon, 18 Mar 2024 22:51:49 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWXBaUuVSWIXUMb/gnKsmzq1nz8Ue1IDbQTXUZBc/11H0GRyH6HWA+m/EFHCmxSxMx3j6Ja6feD476PXsm7baxKOwsQGDXllkthy8Q90Q== X-Google-Smtp-Source: AGHT+IECxMaCoYq4VhKnNmw+MIuqAMm87bMOG/+T8I7soYbOj+4Gj9DZyjhHStTOe2fezOUKhkdQ X-Received: by 2002:a05:6a21:7898:b0:1a3:46fd:6e2d with SMTP id bf24-20020a056a21789800b001a346fd6e2dmr10854440pzc.7.1710827509392; Mon, 18 Mar 2024 22:51:49 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710827509; cv=pass; d=google.com; s=arc-20160816; b=CImusSbaZ4wLJhJZ28taNHjS7KDQKKAfWzZ5kITAFzAzzXiumVwsGhwZ/I0QeJy1XV HEH/6gTLW7Gw8A+2XVFK7PpocFZaczABxbVzFaNbC0tjQRX5BSYh1hNs6Wdr67kvDRUB 3BJZNfOLYmWaHsHS1rFPgSQCpPa2/Ki5tUbQVPbyAz5uUdvWvjopeNgehTjYDQC6bK05 2GIAUw27xkzgHuAt0+s2Aj/ArVEbQWjhsU/Tp0n+ErhIbv06pu6B2Q618WoHxuJw1KEv 1AKdDirxKItgZpen/jA/63+IIMiw0VE9mWP7uehIZ83II9f2D8DBXYi6ZhsAn22tU8h0 25Ig== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=8BcUO+5g/lacg14i1N9Bo7D/4zzx8JORzw80bw8ROtg=; fh=44lErbL5kvNUjSV6UDWgBEjJyOJpjjMpxi6K+HFi+Bc=; b=h5Zwa5bf5XYFojVxWQwg302SuTcICAQNmX7tNp5gZVRpG02sBCgSYj0jHgeftmGIk0 Hp6TEX+olYH5lAtn0GDf/JOKNNr4ijgofPjfputhk3xFtRmcEgCYqZmDq1bWgd5X0ODp fqEq9O44q0jU3ZU8EIO+cxOIm2+FQsMh8v38xbLGRbLs8nMiFrP36Zh22BGUXYCfyrE3 VQKhKk1smdabuh74dMTSKDfyfCYKZQJt1VDNwG6xTIYU/XfGPh5fBaWHbr0+wbLdpHQq 4ECZ3dKC940rDI0sd1RZdk0SEwBMKdSBhBAtF1tL5z8xeH7Y+DKQFFbRHjpYV6ATlXct IkBw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=omlgjw4G; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-107028-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-107028-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id t191-20020a6381c8000000b005dc89648272si9232093pgd.24.2024.03.18.22.51.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Mar 2024 22:51:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-107028-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=omlgjw4G; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-107028-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-107028-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 0CC782820DF for ; Tue, 19 Mar 2024 05:51:49 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1CFA17C0A6; Tue, 19 Mar 2024 05:51:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="omlgjw4G" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 03CB14CDEB; Tue, 19 Mar 2024 05:51:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710827479; cv=none; b=AKYINXO/ay8kKje61b0cHo52aLlc26IGq9O08vRgeDZEMW4YAopLQ0CQEE8r0LJ5uK1GQz6mV2ZeXI3NoGhpkUbvt973ToNd2+/4DOWh61wryNkC3spOuVdZl+hNr7OiIL51+NjLeEpyCKmL8aHn6gvUazcfYLnQJb+NHK8jo5w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710827479; c=relaxed/simple; bh=przeKXxbKOyzoWsi4ae34zOaUqPq/+Uecl8MReIGEjE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=odP2vJtoZ1ILT9f4hDfZY/28p0nGx0PS6PWeYBoAhiaNgmMputkbkGO+lw6T8quN7csz7/60KRantiypKTiPe3dEojWA3kSMM+g4x7LkCUSXf45gh3ecF+KmkcdxehM/TmfsRYhP/oJAGrRxfsUYpNxpcsW0+8CeqRiV+acAV6o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=omlgjw4G; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 18A0DC433A6; Tue, 19 Mar 2024 05:51:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1710827478; bh=przeKXxbKOyzoWsi4ae34zOaUqPq/+Uecl8MReIGEjE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=omlgjw4G3JwX2vrRO8EH9OMTmNcMmzl8xHe5g5AkaorCLIKLgFypQJYl9FC44GUgr d41X6nsl1TSNo7aRFVWqmnD97lEYp2zwl+57zjvnvj66ShBATXz+m0Zoj7yPn7fU6O ODeOKfQ9KT05vcjdR0B7BM3jfkZi7pZo3UKBmI9gQGOksxpZ475Qks69NpwZM//hnB PQNB8WFnSV+QyDoznN3kCqntMDXI5bvgbR4WjSs25u6P6n0WnXeKGOdVz7xEQLxdY3 246CWwDjT8mIfHvRi2xjMKccKBcfK36Mtyzez2IbGyuG4kV26ABN4gS2hqFad2hW76 T/a0aec9pXFlQ== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Linus Torvalds , Stephane Eranian , Masami Hiramatsu , linux-toolchains@vger.kernel.org, linux-trace-devel@vger.kernel.org Subject: [PATCH 02/23] perf dwarf-aux: Add die_collect_vars() Date: Mon, 18 Mar 2024 22:50:54 -0700 Message-ID: <20240319055115.4063940-3-namhyung@kernel.org> X-Mailer: git-send-email 2.44.0.291.gc1ea87d7ee-goog In-Reply-To: <20240319055115.4063940-1-namhyung@kernel.org> References: <20240319055115.4063940-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The die_collect_vars() is to find all variable information in the scope including function parameters. The struct die_var_type is to save the type of the variable with the location (reg and offset) as well as where it's defined in the code (addr). Acked-by: Masami Hiramatsu (Google) Signed-off-by: Namhyung Kim --- tools/perf/util/dwarf-aux.c | 118 +++++++++++++++++++++++++++--------- tools/perf/util/dwarf-aux.h | 17 ++++++ 2 files changed, 107 insertions(+), 28 deletions(-) diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c index e84d0d6a7750..785aa7a3d725 100644 --- a/tools/perf/util/dwarf-aux.c +++ b/tools/perf/util/dwarf-aux.c @@ -1136,6 +1136,40 @@ int die_get_varname(Dwarf_Die *vr_die, struct strbuf *buf) return ret < 0 ? ret : strbuf_addf(buf, "\t%s", dwarf_diename(vr_die)); } +#if defined(HAVE_DWARF_GETLOCATIONS_SUPPORT) || defined(HAVE_DWARF_CFI_SUPPORT) +static int reg_from_dwarf_op(Dwarf_Op *op) +{ + switch (op->atom) { + case DW_OP_reg0 ... DW_OP_reg31: + return op->atom - DW_OP_reg0; + case DW_OP_breg0 ... DW_OP_breg31: + return op->atom - DW_OP_breg0; + case DW_OP_regx: + case DW_OP_bregx: + return op->number; + default: + break; + } + return -1; +} + +static int offset_from_dwarf_op(Dwarf_Op *op) +{ + switch (op->atom) { + case DW_OP_reg0 ... DW_OP_reg31: + case DW_OP_regx: + return 0; + case DW_OP_breg0 ... DW_OP_breg31: + return op->number; + case DW_OP_bregx: + return op->number2; + default: + break; + } + return -1; +} +#endif /* HAVE_DWARF_GETLOCATIONS_SUPPORT || HAVE_DWARF_CFI_SUPPORT */ + #ifdef HAVE_DWARF_GETLOCATIONS_SUPPORT /** * die_get_var_innermost_scope - Get innermost scope range of given variable DIE @@ -1476,41 +1510,69 @@ Dwarf_Die *die_find_variable_by_addr(Dwarf_Die *sc_die, Dwarf_Addr addr, *offset = data.offset; return result; } -#endif /* HAVE_DWARF_GETLOCATIONS_SUPPORT */ -#ifdef HAVE_DWARF_CFI_SUPPORT -static int reg_from_dwarf_op(Dwarf_Op *op) +static int __die_collect_vars_cb(Dwarf_Die *die_mem, void *arg) { - switch (op->atom) { - case DW_OP_reg0 ... DW_OP_reg31: - return op->atom - DW_OP_reg0; - case DW_OP_breg0 ... DW_OP_breg31: - return op->atom - DW_OP_breg0; - case DW_OP_regx: - case DW_OP_bregx: - return op->number; - default: - break; - } - return -1; + struct die_var_type **var_types = arg; + Dwarf_Die type_die; + int tag = dwarf_tag(die_mem); + Dwarf_Attribute attr; + Dwarf_Addr base, start, end; + Dwarf_Op *ops; + size_t nops; + struct die_var_type *vt; + + if (tag != DW_TAG_variable && tag != DW_TAG_formal_parameter) + return DIE_FIND_CB_SIBLING; + + if (dwarf_attr(die_mem, DW_AT_location, &attr) == NULL) + return DIE_FIND_CB_SIBLING; + + /* + * Only collect the first location as it can reconstruct the + * remaining state by following the instructions. + * start = 0 means it covers the whole range. + */ + if (dwarf_getlocations(&attr, 0, &base, &start, &end, &ops, &nops) <= 0) + return DIE_FIND_CB_SIBLING; + + if (die_get_real_type(die_mem, &type_die) == NULL) + return DIE_FIND_CB_SIBLING; + + vt = malloc(sizeof(*vt)); + if (vt == NULL) + return DIE_FIND_CB_END; + + vt->die_off = dwarf_dieoffset(&type_die); + vt->addr = start; + vt->reg = reg_from_dwarf_op(ops); + vt->offset = offset_from_dwarf_op(ops); + vt->next = *var_types; + *var_types = vt; + + return DIE_FIND_CB_SIBLING; } -static int offset_from_dwarf_op(Dwarf_Op *op) +/** + * die_collect_vars - Save all variables and parameters + * @sc_die: a scope DIE + * @var_types: a pointer to save the resulting list + * + * Save all variables and parameters in the @sc_die and save them to @var_types. + * The @var_types is a singly-linked list containing type and location info. + * Actual type can be retrieved using dwarf_offdie() with 'die_off' later. + * + * Callers should free @var_types. + */ +void die_collect_vars(Dwarf_Die *sc_die, struct die_var_type **var_types) { - switch (op->atom) { - case DW_OP_reg0 ... DW_OP_reg31: - case DW_OP_regx: - return 0; - case DW_OP_breg0 ... DW_OP_breg31: - return op->number; - case DW_OP_bregx: - return op->number2; - default: - break; - } - return -1; + Dwarf_Die die_mem; + + die_find_child(sc_die, __die_collect_vars_cb, (void *)var_types, &die_mem); } +#endif /* HAVE_DWARF_GETLOCATIONS_SUPPORT */ +#ifdef HAVE_DWARF_CFI_SUPPORT /** * die_get_cfa - Get frame base information * @dwarf: a Dwarf info diff --git a/tools/perf/util/dwarf-aux.h b/tools/perf/util/dwarf-aux.h index 9973801a20c1..cd171b06fd4c 100644 --- a/tools/perf/util/dwarf-aux.h +++ b/tools/perf/util/dwarf-aux.h @@ -135,6 +135,15 @@ void die_skip_prologue(Dwarf_Die *sp_die, Dwarf_Die *cu_die, /* Get the list of including scopes */ int die_get_scopes(Dwarf_Die *cu_die, Dwarf_Addr pc, Dwarf_Die **scopes); +/* Variable type information */ +struct die_var_type { + struct die_var_type *next; + u64 die_off; + u64 addr; + int reg; + int offset; +}; + #ifdef HAVE_DWARF_GETLOCATIONS_SUPPORT /* Get byte offset range of given variable DIE */ @@ -149,6 +158,9 @@ Dwarf_Die *die_find_variable_by_reg(Dwarf_Die *sc_die, Dwarf_Addr pc, int reg, Dwarf_Die *die_find_variable_by_addr(Dwarf_Die *sc_die, Dwarf_Addr addr, Dwarf_Die *die_mem, int *offset); +/* Save all variables and parameters in this scope */ +void die_collect_vars(Dwarf_Die *sc_die, struct die_var_type **var_types); + #else /* HAVE_DWARF_GETLOCATIONS_SUPPORT */ static inline int die_get_var_range(Dwarf_Die *sp_die __maybe_unused, @@ -176,6 +188,11 @@ static inline Dwarf_Die *die_find_variable_by_addr(Dwarf_Die *sc_die __maybe_unu return NULL; } +static inline void die_collect_vars(Dwarf_Die *sc_die __maybe_unused, + struct die_var_type **var_types __maybe_unused) +{ +} + #endif /* HAVE_DWARF_GETLOCATIONS_SUPPORT */ #ifdef HAVE_DWARF_CFI_SUPPORT -- 2.44.0.291.gc1ea87d7ee-goog