From patchwork Thu Oct 12 03:50:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 151718 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp965737vqb; Wed, 11 Oct 2023 20:56:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHU2Cxh8TEiwCZXLXp8LpC4Jgg+sqYAGROtQiATL4lfW03b53ubX/WeLVYwT6Kj72Erey6i X-Received: by 2002:a05:6870:ab13:b0:1e9:9f9b:eb7a with SMTP id gu19-20020a056870ab1300b001e99f9beb7amr2741419oab.4.1697082988663; Wed, 11 Oct 2023 20:56:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697082988; cv=none; d=google.com; s=arc-20160816; b=XqDc8aVtrX+0ZPN2PadXZTOaKQzLd7suzpY3KEuoGx5Mzi5TpkT3mfe1fy8BXxQx3X yYTqVnY+B0XfzBOQFr1//6IMKA6RHq73zEdtZmApGjyu6Hw3dlr438tRbDFG1oVy8fX1 qRZSD/HtjnPBrpC6CcqM1yRATbojF8CC+DQyNPR5h7YrKUKAHLMGVXHzqWMAGQI271fS cdYbBQ8f3GKEU8xH0Sf5TZbQGRcfSbWCWXSs5aZVVI6eFKp5p7D47eOqm/EfEPRegFoj td/xAYfcAzonNpOV+nA8K37ZbUHpJdmR/V8urYe2P92QEWXAdStfu/nKTlHn21lRQ0X5 MLrw== 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=bdbuy60PoHQjVoDHAgnvJ5vo16j0MWSlUix9kXJv1e0=; fh=7Id7wL2M+oCJXD4imOzx4OKioxBXSOZqypDprm7aO4E=; b=Jd0GyrV7Pw3I4Lf3cVbqhabRJbER4YABGAvQ+FBXhZXcbVC43HM2Jx1OT/qE7vmnXE LOm8+mu/892tyQtrilzf4ivKyrlaFhYRYNjSGswNi68h9RRSYP49sIcqgjUJKYl3H9vZ HcTdLlX1YA1JwyN5RTjyXpZBTZ7Wwb800lYjC9wDfxvfei9tDGZbdcLDuSq/mRb8ryk/ Jk+gE3qqufFcsX0p9BYgy4DXdM45V1RR7faTcvuhgO0hM+dxirt8koN4J6UECVgEx+yb okxpwqF/4clEHXcgzIL+u8A/1UC7B0ju7tglo4qYr3VIXMwn/MDJ09vNzPfii32MuqXj zryg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=HSwlYPSD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id p24-20020a637418000000b005649cee422esi1255676pgc.464.2023.10.11.20.56.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 20:56:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=HSwlYPSD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (Postfix) with ESMTP id 1A73480F696E; Wed, 11 Oct 2023 20:55:05 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377411AbjJLDyk (ORCPT + 18 others); Wed, 11 Oct 2023 23:54:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39774 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377468AbjJLDxP (ORCPT ); Wed, 11 Oct 2023 23:53:15 -0400 Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5EB82D7F; Wed, 11 Oct 2023 20:51:52 -0700 (PDT) Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-1c9b95943beso4894025ad.1; Wed, 11 Oct 2023 20:51:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697082711; x=1697687511; 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=bdbuy60PoHQjVoDHAgnvJ5vo16j0MWSlUix9kXJv1e0=; b=HSwlYPSD6ohCFcYWRM4CoUg3Mdw5nNjSNWYRQwqCOUnqHC2Xo/dsJ2d2xwZC1VCo6X Z/jnnNxHP+xqAqLVh7FW7v8JIC3bYK13Hgco7P1XrS+0F07n7vLDnaYHHgEDFaBLtNES /PzwvVOiYewhUSB++f1fefXAhQT3R+2dhaUXnx1bmJNmNuD44bgGALRHxj2OYhixHOAA O1hFKLq7v1qfgDtmg7Zb0wqJhr7sUalKaUOt687ZuARTdmYIWOe6go3CxT/t8WH+rNDS OUSaal4buDL0B6Q3oIyVHTe2h5X606Jb+lq1PRmXE9NiuSr9ynuAXM7WyirwuPSJnRS2 lOpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697082711; x=1697687511; 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=bdbuy60PoHQjVoDHAgnvJ5vo16j0MWSlUix9kXJv1e0=; b=UDoBb3X9nKPa2kjdbYvb3mAGxSPd4l7Aibaf9EYX6nNGpkEodCoA9ux7i9XogdLyKq ZLLbi53hPqKjjsw7E0pJUW0GOO8A/PV+l7xqAer09OSS78caaIIoegCBwGiOyCN9x0Hf Ygn2xZa599z6ySRsmpVpNBThqniaSTYeyuHO/YgL09bhUJ6aCTF188JxcaOz9QdMfK9p 0QTLCJOv+ZPr0DLrFxv2TrKzMVwFl5WRN9oHncD59rA4BrjDEtK9uxHe7iZ+np4Cuyh/ LmgrbXSbnIS5gpWr9dMVpQXydZOnalsQ22NyBYwVh4EtfFrUXXV0y77foUE57Z7pcjWR D2ig== X-Gm-Message-State: AOJu0YxPVyQS4I9Z5c3XfIVd3zZHLVGkJem7bmK1mOkQE/fja9h6k+nR E8CiN+18mAdirOdQmWuuanQ= X-Received: by 2002:a17:903:48d:b0:1c9:dff1:6ddd with SMTP id jj13-20020a170903048d00b001c9dff16dddmr588990plb.35.1697082711592; Wed, 11 Oct 2023 20:51:51 -0700 (PDT) Received: from bangji.hsd1.ca.comcast.net ([2601:647:6780:42e0:b1b9:d490:2f5e:be06]) by smtp.gmail.com with ESMTPSA id w8-20020a170902d70800b001bc18e579aesm711374ply.101.2023.10.11.20.51.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 20:51:51 -0700 (PDT) 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 , linux-toolchains@vger.kernel.org, linux-trace-devel@vger.kernel.org Subject: [PATCH 28/48] perf dwarf-aux: Add die_find_variable_by_addr() Date: Wed, 11 Oct 2023 20:50:51 -0700 Message-ID: <20231012035111.676789-29-namhyung@kernel.org> X-Mailer: git-send-email 2.42.0.655.g421f12c284-goog In-Reply-To: <20231012035111.676789-1-namhyung@kernel.org> References: <20231012035111.676789-1-namhyung@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-1.0 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email 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 (agentk.vger.email [0.0.0.0]); Wed, 11 Oct 2023 20:55:05 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779520491971238059 X-GMAIL-MSGID: 1779520491971238059 The die_find_variable_by_addr() is to find a variables in the given DIE using given (PC-relative) address. Global variables will have a location expression with DW_OP_addr which has an address so can simply compare it with the address. <1><143a7>: Abbrev Number: 2 (DW_TAG_variable) <143a8> DW_AT_name : loops_per_jiffy <143ac> DW_AT_type : <0x1cca> <143b0> DW_AT_external : 1 <143b0> DW_AT_decl_file : 193 <143b1> DW_AT_decl_line : 213 <143b2> DW_AT_location : 9 byte block: 3 b0 46 41 82 ff ff ff ff (DW_OP_addr: ffffffff824146b0) Note that the type-offset should be calculated from the base address of the global variable. Signed-off-by: Namhyung Kim Acked-by: Masami Hiramatsu (Google) --- tools/perf/util/dwarf-aux.c | 80 +++++++++++++++++++++++++++++++++++++ tools/perf/util/dwarf-aux.h | 14 +++++++ 2 files changed, 94 insertions(+) diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c index 5bb05c84d249..97d9ae56350e 100644 --- a/tools/perf/util/dwarf-aux.c +++ b/tools/perf/util/dwarf-aux.c @@ -1266,8 +1266,12 @@ int die_get_var_range(Dwarf_Die *sp_die, Dwarf_Die *vr_die, struct strbuf *buf) struct find_var_data { /* Target instruction address */ Dwarf_Addr pc; + /* Target memory address (for global data) */ + Dwarf_Addr addr; /* Target register */ unsigned reg; + /* Access offset, set for global data */ + int offset; }; /* Max number of registers DW_OP_regN supports */ @@ -1328,6 +1332,82 @@ Dwarf_Die *die_find_variable_by_reg(Dwarf_Die *sc_die, Dwarf_Addr pc, int reg, }; return die_find_child(sc_die, __die_find_var_reg_cb, &data, die_mem); } + +/* Only checks direct child DIEs in the given scope */ +static int __die_find_var_addr_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_Word size; + Dwarf_Die type_die; + Dwarf_Op *ops; + size_t nops; + + if (tag != DW_TAG_variable) + 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) { + if (ops->atom != DW_OP_addr) + continue; + + if (data->addr < ops->number) + continue; + + if (data->addr == ops->number) { + /* Update offset relative to the start of the variable */ + data->offset = 0; + return DIE_FIND_CB_END; + } + + if (die_get_real_type(die_mem, &type_die) == NULL) + continue; + + if (dwarf_aggregate_size(&type_die, &size) < 0) + continue; + + if (data->addr >= ops->number + size) + continue; + + /* Update offset relative to the start of the variable */ + data->offset = data->addr - ops->number; + return DIE_FIND_CB_END; + } + return DIE_FIND_CB_SIBLING; +} + +/** + * die_find_variable_by_addr - Find variable located at given address + * @sc_die: a scope DIE + * @pc: the program address to find + * @addr: the data address to find + * @die_mem: a buffer to save the resulting DIE + * @offset: the offset in the resulting type + * + * Find the variable DIE located at the given address (in PC-relative mode). + * This is usually for global variables. + */ +Dwarf_Die *die_find_variable_by_addr(Dwarf_Die *sc_die, Dwarf_Addr pc, + Dwarf_Addr addr, Dwarf_Die *die_mem, + int *offset) +{ + struct find_var_data data = { + .pc = pc, + .addr = addr, + }; + Dwarf_Die *result; + + result = die_find_child(sc_die, __die_find_var_addr_cb, &data, die_mem); + if (result) + *offset = data.offset; + return result; +} + #endif /* diff --git a/tools/perf/util/dwarf-aux.h b/tools/perf/util/dwarf-aux.h index 574405c57d3b..742098e3ee7e 100644 --- a/tools/perf/util/dwarf-aux.h +++ b/tools/perf/util/dwarf-aux.h @@ -144,6 +144,11 @@ int die_get_var_range(Dwarf_Die *sp_die, Dwarf_Die *vr_die, struct strbuf *buf); Dwarf_Die *die_find_variable_by_reg(Dwarf_Die *sc_die, Dwarf_Addr pc, int reg, Dwarf_Die *die_mem); +/* Find a (global) variable located in the 'addr' */ +Dwarf_Die *die_find_variable_by_addr(Dwarf_Die *sc_die, Dwarf_Addr pc, + Dwarf_Addr addr, Dwarf_Die *die_mem, + int *offset); + #else /* HAVE_DWARF_GETLOCATIONS_SUPPORT */ static inline int die_get_var_range(Dwarf_Die *sp_die __maybe_unused, @@ -161,6 +166,15 @@ static inline Dwarf_Die *die_find_variable_by_reg(Dwarf_Die *sc_die __maybe_unus return NULL; } +static inline Dwarf_Die *die_find_variable_by_addr(Dwarf_Die *sc_die __maybe_unused, + Dwarf_Addr pc __maybe_unused, + Dwarf_Addr addr __maybe_unused, + Dwarf_Die *die_mem __maybe_unused, + int *offset __maybe_unused) +{ + return NULL; +} + #endif /* HAVE_DWARF_GETLOCATIONS_SUPPORT */ #endif /* _DWARF_AUX_H */