Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp1108383rdb; Fri, 2 Feb 2024 14:06:16 -0800 (PST) X-Google-Smtp-Source: AGHT+IGV+kwqFlCvAC4fFylRhqgnfgaO2Zi5VxKH2qTJFm5mwCSuGqFWcAiq63OgYAqc7m6tQlGe X-Received: by 2002:a05:6358:6e83:b0:178:67dc:aba8 with SMTP id q3-20020a0563586e8300b0017867dcaba8mr8575954rwm.17.1706911576457; Fri, 02 Feb 2024 14:06:16 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706911576; cv=pass; d=google.com; s=arc-20160816; b=S74wfdYmRtDYFy293Ir9NNzXYeTV3p8fsoyDcE1Uz5k9hwHxg5hjA9hAa6a/7tjOC0 tmTSFhJYvToZQfkhmGszu1LsMgpAr0WcaYmxqNFrbrVJ2ZI7c2eev53VdRyeLsNIzDSC YQlHU+scP5nRii61Zdfzf9w21/mwGVAYhlgwpeimwd5OuF3ASIng02v2Haea9GgUTZRE ODC8aptBm/+zF/hRZVSAFrpUonR2buxR4qGgKhXSkKu/waP2LtJzh81HlMRL+Dia27do xQeJI0PwwJTRZrLH3MErfqSdBtTjVJJ0dMbxcmgWTBvnwvzyfJQ1Cy5ivNpPimVOU4ih I5Dw== 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=ILQdtldHKkxzGYq0ZsixeGUGgbwjYxVElg1IX836Zlg=; fh=iupRN+aeAJrOwWJLQHIHj2796rrhltAGsOXfJ4rooMU=; b=i94omwOTrOlvtYlqMVkmS3AxwFAYX94tGYa872JdEvpI3Dxjh7cXP3nhKYYRzh5zhW tMV5Rs/UVkKXGqlcY+2e+Zaht111WLbS9+tcjx4Kfi+RUZ2BVYZVmOI1T80NBJMNerIf C/a7DO6cxL6FQGe7lnb3sCyyEOxWopy76y4W5Z+XGEujdHkQUlwO4gNvarv4l9G/acPf UKiILgVEhzh8MiOtfp/AAXHRvMXp+unobKWLbI/VyZk8ssBXd5RknqlAKzijcqqphAkR dQi9tHSCP06jikJc0HhpOzTf+lQv0djeqkN2kZhLnDAVjiO+xKnawJrTWuLG7gvX0aIa RROQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=H72KEJ6e; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50634-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50634-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCVpf5NU2KbtfhZQRkIVyNVyPYYajuu7/ksYRj997D+TykziiDbg1JgLCzf87vYvEK7nB7UpidF9yju7d5/fLMpw6Sf/Mon6Ia2sT909lQ== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id f8-20020a0cc308000000b0068c4790c9afsi2949271qvi.268.2024.02.02.14.06.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 14:06:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50634-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=H72KEJ6e; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50634-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50634-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 73DCF1C259EA for ; Fri, 2 Feb 2024 22:05:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BA03685943; Fri, 2 Feb 2024 22:05:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="H72KEJ6e" 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 B88F38529C; Fri, 2 Feb 2024 22:05:02 +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=1706911502; cv=none; b=YL4+80gWlf2kwDVMiI2zDBfUcVQRY6nXPtZtAtBVeDq+m/PD2oN7afXx4vwNAMTJo0k5P3uML6Of5li1/UyDirTfk0Lx85U803R/Ep91m5wRovaKeJ0ydZj0MMaThPrMWESkKRL4GlarzcQQVXRI7RyPF2itNzdwN9RTxa5WN5Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706911502; c=relaxed/simple; bh=aCy4fZif4NL+l3ntdf5UR0MbQ0NnUASsa/mDDkArAHw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ArzTYkB3JSTrVibx4cMjIb7NytD3N9lHpHWBoJrimBJk1YmtrtKGN+90HDbFVliqxPIvMdH4zfTv63f3V8Yq/KaaBuhCEBpY5g4Hj6N9pMrbM3zP4lJrD80N/lDXY43m8WArq5t01dvg6LFSgh8V/CGKDy0+hGn8Rs6YK7VurRk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=H72KEJ6e; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id CBAB4C433B1; Fri, 2 Feb 2024 22:05:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706911502; bh=aCy4fZif4NL+l3ntdf5UR0MbQ0NnUASsa/mDDkArAHw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=H72KEJ6eDvvnP8OCPT3PAVAh1TyHCir1mq44pogmZg7Wn1Ihk6tAKfuwL6wKfe3tD Ediu9JFW8JZ9kc2us+fdXnpBPkUQ4zUY3gpY43F2nQ6RLy3jwRzUTl5xtiY5pUTa+3 iPMDb2rjwqQBSENbrQITBpnzr0tdWe414U7RAy9C5Pqvby6J52PYuKhslW3yl4668f 1CVdKDXb7w3WKh//L0REaNEQHRm7lP5tTQfzoUmYzvsN+NuD0fiPe6BZVuMPH9K8ji mPiwwactW9CnctdW+iZaDVCrMtFeFqabilpv0v7j9ZUa7llhwXZNBhoKW7KFkD19Q5 AJoX1+PSH4eog== 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 01/14] perf dwarf-aux: Add die_collect_vars() Date: Fri, 2 Feb 2024 14:04:46 -0800 Message-ID: <20240202220459.527138-2-namhyung@kernel.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240202220459.527138-1-namhyung@kernel.org> References: <20240202220459.527138-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 2791126069b4..f878014c9e27 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 @@ -1479,41 +1513,69 @@ Dwarf_Die *die_find_variable_by_addr(Dwarf_Die *sc_die, Dwarf_Addr pc, *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 85dd527ae1f7..efafd3a1f5b6 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 */ @@ -150,6 +159,9 @@ Dwarf_Die *die_find_variable_by_addr(Dwarf_Die *sc_die, Dwarf_Addr pc, 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, @@ -178,6 +190,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.43.0.594.gd9cf4e227d-goog