Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp1497599rdf; Sun, 5 Nov 2023 01:48:53 -0800 (PST) X-Google-Smtp-Source: AGHT+IEvHz/zGar8f8laVstjZlkbWaF8Xsw/4Sl30wV+VbvvL2M68fqDlw33dSLpUFNSr8JIcZ3b X-Received: by 2002:ac8:5f88:0:b0:419:6cde:86c3 with SMTP id j8-20020ac85f88000000b004196cde86c3mr31892611qta.3.1699177733514; Sun, 05 Nov 2023 01:48:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699177733; cv=none; d=google.com; s=arc-20160816; b=FtC4uXRoU59ICh+8sOvVp8PwV8uBgxQ+bcY2onlGVoYQoooUS0Xy4pLqcvXbLFN4n0 N496jIMoIbC4Bf7cMCwCtzKrMi+6+zExefLd+a12bjaMnM/a3ThaWdglRbi2sS1/ne1M GOFt0lUQOLRRV48BM1DF9IbZN/9rctNuLYkwbCms+Jn7D0YLaIJY9VUTHjxqYlHtrqVE QgeyW1S8XQdNzKIEK+iHWrwShIMk1zZ8hz/zCIF4vKAL3R0cpPKAfb4t+mvnCsldgIoC 4n4awHq/tCio+Km/ShqEAwIqHDeTVcZSAYPAjecS+6LH73BNdFFMaOGMvdnJUnxVq7YO w9Xw== 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 :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=b7c2fkK+bkrb4qBlBUYhzup+DqjjBK4AaHxpaq0UQcU=; fh=isJd0JZVDkfXL73EXh8XKFxOIWM94S6/sYnFcQuWgSQ=; b=ipwl15o0fostuzlKXN7Btd/ZE6ln8ipaw/LME8kNKxeN/7t0KMHODRK7oOmLUpUhWO Wih1IsEOw8DY3wFMGiA44FTIPAr2XNRHT9KeZdhC84nDDpRr6S3mjQfYDGDzgwxPLfeU pocZlCJr/Js98eGPL9+y+dM4Fh/4dG1TC0PgUsK8VUwYv/f80ficllchxC+9lhqWFeJe Mf2haOn1o8rh7ofbkzzxU1cMKV+qbvPv05wEE0brsBpnwA0EBswiOhHxGJUIV11nTbzP 6pBZ/R6OIzzbfUCyBbzI2lUDfQjyoViM9NbmgTgqPgp+hpW9DUG1E31/hGK/GS/D4MS6 84+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=iVKWreV7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id e10-20020a056a001a8a00b006b440b5ace9si5679614pfv.44.2023.11.05.01.48.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Nov 2023 01:48:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=iVKWreV7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 420B3809D3D3; Sun, 5 Nov 2023 01:48:52 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229621AbjKEJsu (ORCPT + 99 others); Sun, 5 Nov 2023 04:48:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43348 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229447AbjKEJst (ORCPT ); Sun, 5 Nov 2023 04:48:49 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 07222CC; Sun, 5 Nov 2023 01:48:46 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5002BC433C8; Sun, 5 Nov 2023 09:48:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1699177726; bh=TWv1smHYN6QLsyC/JK7cl0Xj5FUDrDU2v7Za9ia3Nxg=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=iVKWreV7jAdLTp75MOaTg5Sm5lTCcXIxP+TmWTbRn+vVMDk+QuzsYm72ktq8+Dz/p /PM7XPqTsK1pFeBt3LDFaRInJ+eqit07aLkD6JW0B/BpmRJtVCrhn/RwoCOsGyOWyO icqEIbxujTcqjIYZlZbFlqQo4SELPA8AB42HjX/dOwowA+YF9GqN6Gr71mn2wd+FZ2 zrQpxuVE97liFn6rv3gsHrBwdWXvMig6Hsf2L5amXY6ToEs6Ybj3yiRjjet42lL5L/ sSb+lO8/aYq3XExX/aTS1G935/bi6wZIkXnzs4Kcs6bHTjtjtRBUS6bd2RSmvqtVCP MpSXYOMu7AGfw== Date: Sun, 5 Nov 2023 18:48:40 +0900 From: Masami Hiramatsu (Google) To: Namhyung Kim Cc: Arnaldo Carvalho de Melo , Jiri Olsa , Peter Zijlstra , Ian Rogers , Adrian Hunter , 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: Re: [PATCH 07/48] perf dwarf-aux: Add die_find_variable_by_reg() helper Message-Id: <20231105184840.947ae57233fbaeb3e66ae5d9@kernel.org> In-Reply-To: <20231012035111.676789-8-namhyung@kernel.org> References: <20231012035111.676789-1-namhyung@kernel.org> <20231012035111.676789-8-namhyung@kernel.org> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-8.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Sun, 05 Nov 2023 01:48:52 -0800 (PST) On Wed, 11 Oct 2023 20:50:30 -0700 Namhyung Kim wrote: > The die_find_variable_by_reg() will search for a variable or a parameter > sub-DIE in the given scope DIE where the location matches to the given > register. > > For the simpliest and most common case, memory access usually happens > with a base register and an offset to the field so the register would > hold a pointer in a variable or function parameter. Then we can find > one if it has a location expression at the (instruction) address. So > this function only handles such a simple case for now. > > In this case, the expression would have a DW_OP_regN operation where > N < 32. If the register index (N) is greater than or equal to 32, > DW_OP_regx operation with an operand which saves the value for the N > would be used. It would reject expressions with more operations. > Nice! This looks good to me. Acked-by: Masami Hiramatsu (Google) Thank you, > Cc: Masami Hiramatsu > Signed-off-by: Namhyung Kim > --- > tools/perf/util/dwarf-aux.c | 67 +++++++++++++++++++++++++++++++++++++ > tools/perf/util/dwarf-aux.h | 12 +++++++ > 2 files changed, 79 insertions(+) > > diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c > index 10aa32334d6f..652e6e7368a2 100644 > --- a/tools/perf/util/dwarf-aux.c > +++ b/tools/perf/util/dwarf-aux.c > @@ -1245,6 +1245,73 @@ int die_get_var_range(Dwarf_Die *sp_die, Dwarf_Die *vr_die, struct strbuf *buf) > out: > return ret; > } > + > +/* Interval parameters for __die_find_var_reg_cb() */ > +struct find_var_data { > + /* Target instruction address */ > + Dwarf_Addr pc; > + /* Target register */ > + unsigned reg; > +}; > + > +/* Max number of registers DW_OP_regN supports */ > +#define DWARF_OP_DIRECT_REGS 32 > + > +/* Only checks direct child DIEs in the given scope. */ > +static int __die_find_var_reg_cb(Dwarf_Die *die_mem, void *arg) > +{ > + struct find_var_data *data = arg; > + int tag = dwarf_tag(die_mem); > + ptrdiff_t off = 0; > + Dwarf_Attribute attr; > + Dwarf_Addr base, start, end; > + Dwarf_Op *ops; > + size_t nops; > + > + 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; > + > + while ((off = dwarf_getlocations(&attr, off, &base, &start, &end, &ops, &nops)) > 0) { > + /* Assuming the location list is sorted by address */ > + if (end < data->pc) > + continue; > + if (start > data->pc) > + break; > + > + /* Only match with a simple case */ > + if (data->reg < DWARF_OP_DIRECT_REGS) { > + if (ops->atom == (DW_OP_reg0 + data->reg) && nops == 1) > + return DIE_FIND_CB_END; > + } else { > + if (ops->atom == DW_OP_regx && ops->number == data->reg && > + nops == 1) > + return DIE_FIND_CB_END; > + } > + } > + return DIE_FIND_CB_SIBLING; > +} > + > +/** > + * die_find_variable_by_reg - Find a variable saved in a register > + * @sc_die: a scope DIE > + * @pc: the program address to find > + * @reg: the register number to find > + * @die_mem: a buffer to save the resulting DIE > + * > + * Find the variable DIE accessed by the given register. > + */ > +Dwarf_Die *die_find_variable_by_reg(Dwarf_Die *sc_die, Dwarf_Addr pc, int reg, > + Dwarf_Die *die_mem) > +{ > + struct find_var_data data = { > + .pc = pc, > + .reg = reg, > + }; > + return die_find_child(sc_die, __die_find_var_reg_cb, &data, die_mem); > +} > #endif > > /* > diff --git a/tools/perf/util/dwarf-aux.h b/tools/perf/util/dwarf-aux.h > index f9d765f80fb0..b6f430730bd1 100644 > --- a/tools/perf/util/dwarf-aux.h > +++ b/tools/perf/util/dwarf-aux.h > @@ -137,6 +137,10 @@ int die_get_scopes(Dwarf_Die *cu_die, Dwarf_Addr pc, Dwarf_Die **scopes); > /* Get byte offset range of given variable DIE */ > int die_get_var_range(Dwarf_Die *sp_die, Dwarf_Die *vr_die, struct strbuf *buf); > > +/* Find a variable saved in the 'reg' at given address */ > +Dwarf_Die *die_find_variable_by_reg(Dwarf_Die *sc_die, Dwarf_Addr pc, int reg, > + Dwarf_Die *die_mem); > + > #else /* HAVE_DWARF_GETLOCATIONS_SUPPORT */ > > static inline int die_get_var_range(Dwarf_Die *sp_die __maybe_unused, > @@ -146,6 +150,14 @@ static inline int die_get_var_range(Dwarf_Die *sp_die __maybe_unused, > return -ENOTSUP; > } > > +static inline Dwarf_Die *die_find_variable_by_reg(Dwarf_Die *sc_die __maybe_unused, > + Dwarf_Addr pc __maybe_unused, > + int reg __maybe_unused, > + Dwarf_Die *die_mem __maybe_unused) > +{ > + return NULL; > +} > + > #endif /* HAVE_DWARF_GETLOCATIONS_SUPPORT */ > > #endif /* _DWARF_AUX_H */ > -- > 2.42.0.655.g421f12c284-goog > -- Masami Hiramatsu (Google)