From nobody Thu Dec 18 07:33:52 2025 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 75DAC200C1; Thu, 2 May 2024 06:00:13 +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=1714629613; cv=none; b=KUu/4MxVDMi40qtfEROUgXwFW0HgJNBfHzyCqjboK9Pmppmg7AWYLhTl8EI7SHcXfkaATE+4Pb9LVNiRa/+D9kTCYDve5iRemgz6+ZbQ3XfaRuyx326mQNZ/hjp3fU219VQ4Uuq1oDo/M5rdo1lgxREF0pcKZfeTwfEbYAEB/18= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714629613; c=relaxed/simple; bh=zamPJCSjSG+pCfLqayGyCmEr6GJWt/RSUPZolnFgaGU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=e3Xt3uj05gyVcrdM3EEMgkLB82Z0Guoj7aZIxN1oUq/BmuuZlYBOTW377ihW2r4yCK+xatQctfbXf+TiPRCZ0G+6+mUdrvJoM55Wuz/NE4TU3Wx/QCBS1/TGsOobLxxWwSoj7hjzNhna96rCBK9ZLa5JKbk0d1wr2qrScw4tF6U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Mf2zs8bG; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Mf2zs8bG" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EE07CC4AF1B; Thu, 2 May 2024 06:00:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714629613; bh=zamPJCSjSG+pCfLqayGyCmEr6GJWt/RSUPZolnFgaGU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Mf2zs8bGiCmtcXgOJkeMSCdHVIVT415uotH3XYA+24ZYUoz+tYMvw71BANcQ1/Vae BMNGfbb2y00lYg2yot8ZWh9eYODHM4h72jktERY4ypljt00HdQbFHaSie3bYeZc2Vs xjnB5GupsH9RMPLkqKUPDktbQMzBx0PS/koYUf1QJdHIbu/FO5fW+cQ07XHdC2kZGp w3HwNfY5h3lboobASJTP8hezJHY8kDAjWAhlB6wV7/u4aUANypM6GjkDKmkjky5jbW PA6xK2DFUnaKPCgSCBDrjNe/+uQccWGfdpVpnnaJgWNesVPoi+F1WEygWVxpQOthLa +KFa7F4dRYr4g== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers , Kan Liang Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Masami Hiramatsu Subject: [PATCH 1/6] perf dwarf-aux: Add die_collect_global_vars() Date: Wed, 1 May 2024 23:00:06 -0700 Message-ID: <20240502060011.1838090-2-namhyung@kernel.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240502060011.1838090-1-namhyung@kernel.org> References: <20240502060011.1838090-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: quoted-printable Content-Type: text/plain; charset="utf-8" This function is to search all global variables in the CU. We want to have the list of global variables at once and match them later. Cc: Masami Hiramatsu Signed-off-by: Namhyung Kim --- tools/perf/util/dwarf-aux.c | 62 +++++++++++++++++++++++++++++++++++++ tools/perf/util/dwarf-aux.h | 8 +++++ 2 files changed, 70 insertions(+) diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c index 40cfbdfe2d75..c0a492e65388 100644 --- a/tools/perf/util/dwarf-aux.c +++ b/tools/perf/util/dwarf-aux.c @@ -1630,6 +1630,68 @@ void die_collect_vars(Dwarf_Die *sc_die, struct die_= var_type **var_types) =20 die_find_child(sc_die, __die_collect_vars_cb, (void *)var_types, &die_mem= ); } + +static int __die_collect_global_vars_cb(Dwarf_Die *die_mem, void *arg) +{ + struct die_var_type **var_types =3D arg; + Dwarf_Die type_die; + int tag =3D 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 !=3D DW_TAG_variable) + return DIE_FIND_CB_SIBLING; + + if (dwarf_attr(die_mem, DW_AT_location, &attr) =3D=3D NULL) + return DIE_FIND_CB_SIBLING; + + /* Only collect the location with an absolute address. */ + if (dwarf_getlocations(&attr, 0, &base, &start, &end, &ops, &nops) <=3D 0) + return DIE_FIND_CB_SIBLING; + + if (ops->atom !=3D DW_OP_addr) + return DIE_FIND_CB_SIBLING; + + if (!check_allowed_ops(ops, nops)) + return DIE_FIND_CB_SIBLING; + + if (die_get_real_type(die_mem, &type_die) =3D=3D NULL) + return DIE_FIND_CB_SIBLING; + + vt =3D malloc(sizeof(*vt)); + if (vt =3D=3D NULL) + return DIE_FIND_CB_END; + + vt->die_off =3D dwarf_dieoffset(&type_die); + vt->addr =3D ops->number; + vt->reg =3D -1; + vt->offset =3D 0; + vt->next =3D *var_types; + *var_types =3D vt; + + return DIE_FIND_CB_SIBLING; +} + +/** + * die_collect_global_vars - Save all global variables + * @cu_die: a CU DIE + * @var_types: a pointer to save the resulting list + * + * Save all global variables in the @cu_die and save them to @var_types. + * The @var_types is a singly-linked list containing type and location inf= o. + * Actual type can be retrieved using dwarf_offdie() with 'die_off' later. + * + * Callers should free @var_types. + */ +void die_collect_global_vars(Dwarf_Die *cu_die, struct die_var_type **var_= types) +{ + Dwarf_Die die_mem; + + die_find_child(cu_die, __die_collect_global_vars_cb, (void *)var_types, &= die_mem); +} #endif /* HAVE_DWARF_GETLOCATIONS_SUPPORT */ =20 #ifdef HAVE_DWARF_CFI_SUPPORT diff --git a/tools/perf/util/dwarf-aux.h b/tools/perf/util/dwarf-aux.h index b0f25fbf9668..24446412b869 100644 --- a/tools/perf/util/dwarf-aux.h +++ b/tools/perf/util/dwarf-aux.h @@ -171,6 +171,9 @@ Dwarf_Die *die_find_variable_by_addr(Dwarf_Die *sc_die,= Dwarf_Addr addr, /* Save all variables and parameters in this scope */ void die_collect_vars(Dwarf_Die *sc_die, struct die_var_type **var_types); =20 +/* Save all global variables in this CU */ +void die_collect_global_vars(Dwarf_Die *cu_die, struct die_var_type **var_= types); + #else /* HAVE_DWARF_GETLOCATIONS_SUPPORT */ =20 static inline int die_get_var_range(Dwarf_Die *sp_die __maybe_unused, @@ -203,6 +206,11 @@ static inline void die_collect_vars(Dwarf_Die *sc_die = __maybe_unused, { } =20 +static inline void die_collect_global_vars(Dwarf_Die *cu_die __maybe_unuse= d, + struct die_var_type **var_types __maybe_unused) +{ +} + #endif /* HAVE_DWARF_GETLOCATIONS_SUPPORT */ =20 #ifdef HAVE_DWARF_CFI_SUPPORT --=20 2.45.0.rc1.225.g2a3ae87e7f-goog