From patchwork Thu Nov 9 23:59:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 163647 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b129:0:b0:403:3b70:6f57 with SMTP id q9csp786551vqs; Thu, 9 Nov 2023 16:02:58 -0800 (PST) X-Google-Smtp-Source: AGHT+IGAV043KB4BbKNbUgeZsUJtKQJzfXcLRQQDP9WPO2Duk0WHop71C9OkImb2ZUo8F2u5s5DU X-Received: by 2002:a05:6a20:938a:b0:181:90eb:6b24 with SMTP id x10-20020a056a20938a00b0018190eb6b24mr7537334pzh.22.1699574578121; Thu, 09 Nov 2023 16:02:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699574578; cv=none; d=google.com; s=arc-20160816; b=cGeIGB/dtZ+7Pdh8/aQ7jmrHdJQPZS3mSj3mf9JlvUpZwOhL2yNkr0jiIhdUe2UFDL 5WbXTpS8GirUoMxh8sAIRaWYOdU8pfYA2pTIM0ZSEco0GxF/f1VXq0Kev6IBP5Ddo/yR K+eS+I86AEOkulG9q3NdKINNbRWO0xyqOP2jNvpmnBU7/cbNOGX9fAXVDK3iOvnPXOfL ChiEBTUt46kqoNl6p+MusgNugznF0MCO+qd7izuea8ChWvKUuQlTzgbCgmLJuJZ4wG3n HxnxQeWDDnMDG5Ha2/1QEzX+XpvFk8ToGd3VgmGzuMtYirBl2vh3RPKTw+OLhwmb3XTZ R8lQ== 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=MVwEgWJomgfw297GtG9OF7+0iUeurVYuT1rqsW67QX0=; fh=K//si1Hi/RYC7s39rVvmvh0YbZGmBrdWWHQg3cPF7Xc=; b=kgXA4hJXq0CtvaXmW0pFnBE9aReXbO1vDjnWDuy2FFb18f6olS7dQRLWj6oZrsAZx4 CCrmVgFYB48EGmD8xRkFU0a280TTh1HIWeUVUYiIVN8MzgjMo09+LzM/4LKqlfnVASRQ tAvgJc7ab01PVxipG3S03CNokTg0wYa+Z6cfKmMdoptCjm6QVKqZ6mspHO73/T36WYEK nj16L8maCOJLmDR47Zd4klyDdpFPIIqDRRB8Nm91NayB22up5LWEGCIYjLUDqrrJx+9h X1JSFqRARerplCOf5540+KHiDsV7+/HqB7SLozhIXPz0QWwPMiFFfisisKMdCvvRyKVC 4RUw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=jT9icGVX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id r73-20020a632b4c000000b005b99bfe3301si8752922pgr.462.2023.11.09.16.02.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 16:02:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=jT9icGVX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 lipwig.vger.email (Postfix) with ESMTP id CCC5A8083E78; Thu, 9 Nov 2023 16:01:16 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345507AbjKJAAm (ORCPT + 30 others); Thu, 9 Nov 2023 19:00:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46590 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345457AbjKJAAh (ORCPT ); Thu, 9 Nov 2023 19:00:37 -0500 Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2B3454693; Thu, 9 Nov 2023 16:00:26 -0800 (PST) Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-1cc316ccc38so12752135ad.1; Thu, 09 Nov 2023 16:00:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699574425; x=1700179225; 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=MVwEgWJomgfw297GtG9OF7+0iUeurVYuT1rqsW67QX0=; b=jT9icGVXK2F8It0gtLH//nJbw5IUDqeNUg48nF4zmk4TUiezseWg0YqvDoUiU3kzH5 DS9mBRtr5UjY060aO2vV67E5Q2uK9EgsDsNOZXJrL7t3PMgumLAIkrbuPrsyfzfp2BS6 MNuCDfJumf/kQYfckP38Hjcs+ULxCu/7W7PMxnDkQjsZY/4/nPhH/finCj8OYcYB8acn kMC/wlkGiLXitMrNio18JutvMgKi3H70WWnYwuwnCebs9DfVTdN8pwQKTtnIWXqhOSv0 yzVDr8CtqJ9fPFttiSzOYs4eqSCrXLpVuSmXY000nU2JaKfMchHZB0/5AqoORJXx8ulD VhFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699574425; x=1700179225; 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=MVwEgWJomgfw297GtG9OF7+0iUeurVYuT1rqsW67QX0=; b=sls7f1vIkojZzMreQLOtlThQITd2cJQqZLlhxTMVpsYRs+6c6Az8JmvxMWrX8CB4DE HneLMgyA87emyYXjb3cjXHf+7hbFQZ12llV2aDUSWrT9dFJprtl8nd9EjclwuxgnEjKs jlHs0YXX9wG0dpHUXxsph9ZAa+jd46dLoHGJIAeIB4QIy5CjOElWY5RR4Jhzh/7AmDi9 bgb0Tui/rRq0Z3KyYuVcB2TkRNvHvXvXnxfF9vlRf+le4o8nZQbondkVmUDZbikLn/+i adXvaYNIyVYtvYS+q8lfepJgl6MGR1Ml5EAN/u7Ej1kFgxlAlF/gJlxWlflWMHTwpFs4 k2wA== X-Gm-Message-State: AOJu0YzAXopfdOhcdii+LCtV7FHG7pZqDFCRODwjmGOIT5IVikvGEdoL PEy2cdrif9FeZyG4D8euawRsh9CgsUk= X-Received: by 2002:a17:90b:4c47:b0:27d:4f1f:47f5 with SMTP id np7-20020a17090b4c4700b0027d4f1f47f5mr3352933pjb.23.1699574425414; Thu, 09 Nov 2023 16:00:25 -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.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 16:00:24 -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 06/52] perf dwarf-aux: Add die_get_scopes() helper Date: Thu, 9 Nov 2023 15:59:25 -0800 Message-ID: <20231110000012.3538610-7-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 (lipwig.vger.email [0.0.0.0]); Thu, 09 Nov 2023 16:01:16 -0800 (PST) X-Spam-Status: No, score=-1.0 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782133112390266531 X-GMAIL-MSGID: 1782133112390266531 The die_get_scopes() would return the number of enclosing DIEs for the given address and it fills an array of DIEs like dwarf_getscopes(). But it doesn't follow the abstract origin of inlined functions as we want information of the concrete instance. This is needed to check the location of parameters and local variables properly. Users can check the origin separately if needed. Acked-by: Masami Hiramatsu (Google) Signed-off-by: Namhyung Kim --- tools/perf/util/dwarf-aux.c | 53 +++++++++++++++++++++++++++++++++++++ tools/perf/util/dwarf-aux.h | 3 +++ 2 files changed, 56 insertions(+) diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c index adef2635587d..10aa32334d6f 100644 --- a/tools/perf/util/dwarf-aux.c +++ b/tools/perf/util/dwarf-aux.c @@ -1425,3 +1425,56 @@ void die_skip_prologue(Dwarf_Die *sp_die, Dwarf_Die *cu_die, *entrypc = postprologue_addr; } + +/* Internal parameters for __die_find_scope_cb() */ +struct find_scope_data { + /* Target instruction address */ + Dwarf_Addr pc; + /* Number of scopes found [output] */ + int nr; + /* Array of scopes found, 0 for the outermost one. [output] */ + Dwarf_Die *scopes; +}; + +static int __die_find_scope_cb(Dwarf_Die *die_mem, void *arg) +{ + struct find_scope_data *data = arg; + + if (dwarf_haspc(die_mem, data->pc)) { + Dwarf_Die *tmp; + + tmp = realloc(data->scopes, (data->nr + 1) * sizeof(*tmp)); + if (tmp == NULL) + return DIE_FIND_CB_END; + + memcpy(tmp + data->nr, die_mem, sizeof(*die_mem)); + data->scopes = tmp; + data->nr++; + return DIE_FIND_CB_CHILD; + } + return DIE_FIND_CB_SIBLING; +} + +/** + * die_get_scopes - Return a list of scopes including the address + * @cu_die: a compile unit DIE + * @pc: the address to find + * @scopes: the array of DIEs for scopes (result) + * + * This function does the same as the dwarf_getscopes() but doesn't follow + * the origins of inlined functions. It returns the number of scopes saved + * in the @scopes argument. The outer scope will be saved first (index 0) and + * the last one is the innermost scope at the @pc. + */ +int die_get_scopes(Dwarf_Die *cu_die, Dwarf_Addr pc, Dwarf_Die **scopes) +{ + struct find_scope_data data = { + .pc = pc, + }; + Dwarf_Die die_mem; + + die_find_child(cu_die, __die_find_scope_cb, &data, &die_mem); + + *scopes = data.scopes; + return data.nr; +} diff --git a/tools/perf/util/dwarf-aux.h b/tools/perf/util/dwarf-aux.h index 4f5d0211ee4f..f9d765f80fb0 100644 --- a/tools/perf/util/dwarf-aux.h +++ b/tools/perf/util/dwarf-aux.h @@ -129,6 +129,9 @@ bool die_is_optimized_target(Dwarf_Die *cu_die); void die_skip_prologue(Dwarf_Die *sp_die, Dwarf_Die *cu_die, Dwarf_Addr *entrypc); +/* Get the list of including scopes */ +int die_get_scopes(Dwarf_Die *cu_die, Dwarf_Addr pc, Dwarf_Die **scopes); + #ifdef HAVE_DWARF_GETLOCATIONS_SUPPORT /* Get byte offset range of given variable DIE */