From patchwork Thu Nov 9 23:59:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 163644 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b129:0:b0:403:3b70:6f57 with SMTP id q9csp786444vqs; Thu, 9 Nov 2023 16:02:48 -0800 (PST) X-Google-Smtp-Source: AGHT+IH8lPZg/uZWA70q4MVpF48tQDo9p9RhE1sxiYhbv2teiGu+4WUgW7fs1f1FXhqIXPeKrBpV X-Received: by 2002:a05:6808:2e86:b0:3a9:bb4f:9efd with SMTP id gt6-20020a0568082e8600b003a9bb4f9efdmr4103866oib.29.1699574567832; Thu, 09 Nov 2023 16:02:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699574567; cv=none; d=google.com; s=arc-20160816; b=dy3npMfD7bNN6tTxkXxRnFutqzQH3Mn3UrvjyaipupcQMw6aviTwGzCk1hWRgDxUf9 fYXXISSjhj7829TbZXdFy+EM9GE6yV8OhvGWqnOSJKBbK6h2MpQAPhExsDJcAqApMVW2 xv1kUrqelzlJPW4S6jazEm+J8kEMXaJ57rzpGVN0B3yUn1nLY8DoTNoRxja2XqmkQAL8 kkSX/0ECvrU0Q9bZOprMKE3WJKqkNgQApOW41C2ypobk7ImVyQDlWr12fqgYUTQxeAjE T2XAyydhOjxhZmPGt4YnOFeEeAsuIDJmCkIoaVVjTPuZWuk96oxMb0f0Fz5DjeSmR5KP oOTg== 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:date:subject:cc:to:from:sender :dkim-signature; bh=UzNWsgJvVG83qkttBUZAr2wiEePWD6gXUY5o4BwenZA=; fh=K//si1Hi/RYC7s39rVvmvh0YbZGmBrdWWHQg3cPF7Xc=; b=BTmF56NQcDvqj/tz72ZZZlW5c1IU9nyS6ZcjBVpBPOMA/D3GzHXw2Tzq8kTKvjUgVl Ki/M0P6cd3Cgvcxrytc6lSfmZG4vWUhjfmS8PJPF85mA/yq4nzu0fYZdg/VBfVr3820G jUgkZhf1q/TtA7P196vxFHr3j4Ajzl2YN00dIFPofzRgjNGpyrM2KNfHxIkWxx81bq4h oLU5rDRk3S946AYA/xgI54Ha4NJRvZUOcVzJaNcpGPskd09LO8/r55aKr30EXLeH/Isd q7ve+ucGkI5Y/NEcNmoFl6x18oVGUyPGmPTqElNXitwN8NYVIC3MMbiNZxyMh9dnk/Ez pCSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=mBStWFfd; 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 snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id t40-20020a05680815a800b003ab88711c51si7253933oiw.162.2023.11.09.16.02.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 16:02:47 -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=@gmail.com header.s=20230601 header.b=mBStWFfd; 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 AC4A1822C020; Thu, 9 Nov 2023 16:01:07 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345496AbjKJAAu (ORCPT + 30 others); Thu, 9 Nov 2023 19:00:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46548 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345470AbjKJAAi (ORCPT ); Thu, 9 Nov 2023 19:00:38 -0500 Received: from mail-pg1-x530.google.com (mail-pg1-x530.google.com [IPv6:2607:f8b0:4864:20::530]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 860FB46A0; Thu, 9 Nov 2023 16:00:27 -0800 (PST) Received: by mail-pg1-x530.google.com with SMTP id 41be03b00d2f7-5aa481d53e5so1188714a12.1; Thu, 09 Nov 2023 16:00:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699574427; x=1700179227; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=UzNWsgJvVG83qkttBUZAr2wiEePWD6gXUY5o4BwenZA=; b=mBStWFfdtzHeDoEpJSikIejM6kx1p3v/0EnIKnUojEekQns4UA7+ZZevb5bbIKZ3uq ypofpOtLlV12mlP7lYZNC+VRu5BglnHEOiAV4jj4j3tSYNyBieP6vJ0iVC4OcI/uMlhU zD07gTRAzApk6Iap+/yrEgj5vwv1GPXDQffXJuWs/QMgh8eGQbX+CCFUYRuNec4NBScp koCNjWvy1jRkIYTZAzLaMOIlbRFsSLPtrnRHOVjFlvsHzcMeeJFWZKe/vSQy2g4zKAAE ObS4Dg2dFwMHz84drQ6XcOadQ4k1pCDBU7dSUtZn1IQ3GSvz6wCQGFAQ4tuUzT13Tepw aOQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699574427; x=1700179227; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=UzNWsgJvVG83qkttBUZAr2wiEePWD6gXUY5o4BwenZA=; b=w6FA0TSfELayeMJSF0uajYujf6fXhSfyrheqYANVNkaVgMeA+9oxwwEjswAHl6hbez hsC4EeUKly0uO9DT1z6A3B843t56OGOpgUXuzwOSxrQ6OCzFSscKz/9QtF6BfYmN7lBz f92yUeYHtKmhV0TD1h+nwwat5RJiQtuYRrC2XOJoWdYN0RBnWf3wx7uZUZcrLtGJR8HI SN9AHKcGTSjJ49nEog08gnNKP5ZBRZ13ZvLA4rFm6rgX6ASe6mWVSHFxpNL5znWw2AJl hlnJgnoN1qeHPhG7EhRLFU/QSLtwEwML5KtMJkEqFddFTwo/GBoeNSjqLikGSdnkfMa/ lB6g== X-Gm-Message-State: AOJu0YzLcgIduxSZehmpVY8wpKzVAsW5+v8Up5OXLubOqtOY0oecmIYC HkQs94CHNS1PNS+fhAjaHaA= X-Received: by 2002:a17:90b:3b83:b0:280:52a3:711e with SMTP id pc3-20020a17090b3b8300b0028052a3711emr3317822pjb.47.1699574426927; Thu, 09 Nov 2023 16:00:26 -0800 (PST) Received: from bangji.corp.google.com ([2620:15c:2c0:5:d45c:ae16:d959:a5f1]) by smtp.gmail.com with ESMTPSA id k32-20020a17090a4ca300b002635db431a0sm371312pjh.45.2023.11.09.16.00.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 16:00:26 -0800 (PST) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa , Peter Zijlstra Cc: Ian Rogers , Adrian Hunter , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Linus Torvalds , Stephane Eranian , Masami Hiramatsu , Andi Kleen , linux-trace-devel@vger.kernel.org, linux-toolchains@vger.kernel.org Subject: [PATCH 07/52] perf dwarf-aux: Add die_find_variable_by_reg() helper Date: Thu, 9 Nov 2023 15:59:26 -0800 Message-ID: <20231110000012.3538610-8-namhyung@kernel.org> X-Mailer: git-send-email 2.42.0.869.gea05f2083d-goog In-Reply-To: <20231110000012.3538610-1-namhyung@kernel.org> References: <20231110000012.3538610-1-namhyung@kernel.org> MIME-Version: 1.0 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]); Thu, 09 Nov 2023 16:01:07 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782133101998516970 X-GMAIL-MSGID: 1782133101998516970 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. Acked-by: Masami Hiramatsu (Google) 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 */