From patchwork Fri Feb 2 22:04:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 196052 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp727036dyc; Fri, 2 Feb 2024 14:14:37 -0800 (PST) X-Google-Smtp-Source: AGHT+IHl7Xzk6wTS++riRjNkOvEAzMqyneyipGl2cosB+5OfKryjIJG6hQptaeQjL3LE6TvTJM1B X-Received: by 2002:a05:6358:d592:b0:176:302b:addf with SMTP id ms18-20020a056358d59200b00176302baddfmr8581914rwb.11.1706912076892; Fri, 02 Feb 2024 14:14:36 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706912076; cv=pass; d=google.com; s=arc-20160816; b=GX3i4xbzkkzhHHZdjo0H8FkPn7CPy5J0npO+GbeTHdEkyAd6MqNDVKcGvXWSAthbR1 sSkfX88oNMC4Df5ZeCrZ3SPyh+gv5CU3uijT3RSYBKWVLrkQ4gRCQakhEu3A8ZeAltcr HyS6sFg/wEnNGX30q+aqNmoYt3uVrvPpkE4mu3f9f7J0eLKKOs6R9oE//8tfGhO0vNQE vsxIerlubwyp2em8jp1WOIotoSLhqYS5V1KsCZ5vjQvX9JxvYINNOcKMzwYAJqLC6P8X X57/DK5DGfl7RQoh41peKBeMfKJzRICz5pO4hm4aK4GdKV//uzhBIIQLJzcRTiVT9FE9 4t3g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=ILQdtldHKkxzGYq0ZsixeGUGgbwjYxVElg1IX836Zlg=; fh=WoH5f+E0xFb0jPnrdN55+fOBvhm9wQJGD3bxLPhbymc=; b=KVlvaZqN1Xwmun2rD5un26kFHSKLvqKYGtOrXMwgam+b10yRLXpg8JAVtT5cQZLV6E f1FPI6Menlm+YJOtZuN6aUWzUtax7qbcZEvVbSddz6ZcO6J9s9yz2ofjtVQN7HEwbOcC wWtA8i3wL6oQeKCdy+1ywFQd5aLFTPPD1tBOr8jtOndu1mzbgVt5bDWvUq+Px7pKF9Ds fSROp82JnteG4koX7mttDE0XB2prmTS0NDJ56CfgqNBo3sZ7sqBzK0XAM4DQ8f9D2apm Pvfn6YY01GFTrL1CS34Kv6xEi3KG8mQBpViWmn1nwhcrE4GT8eDFZyA3oi4isZU3YyVG 1VGg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=H72KEJ6e; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50634-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50634-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCWmeIpoR6UX+tPVSwB0/UPqcE28IhW7ImLB3KKqwUUnUMA5AutwkORm4wx8Kuj6LxkT/NXTOJrF/seT4Sp/GhEU0UHUgA== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id d4-20020a631d44000000b005d55aec96b2si2261902pgm.41.2024.02.02.14.14.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 14:14:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50634-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=H72KEJ6e; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50634-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50634-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 07D34B26345 for ; Fri, 2 Feb 2024 22:06:14 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 14BF812C804; Fri, 2 Feb 2024 22:05:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="H72KEJ6e" 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 B88F38529C; Fri, 2 Feb 2024 22:05:02 +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=1706911502; cv=none; b=YL4+80gWlf2kwDVMiI2zDBfUcVQRY6nXPtZtAtBVeDq+m/PD2oN7afXx4vwNAMTJo0k5P3uML6Of5li1/UyDirTfk0Lx85U803R/Ep91m5wRovaKeJ0ydZj0MMaThPrMWESkKRL4GlarzcQQVXRI7RyPF2itNzdwN9RTxa5WN5Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706911502; c=relaxed/simple; bh=aCy4fZif4NL+l3ntdf5UR0MbQ0NnUASsa/mDDkArAHw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ArzTYkB3JSTrVibx4cMjIb7NytD3N9lHpHWBoJrimBJk1YmtrtKGN+90HDbFVliqxPIvMdH4zfTv63f3V8Yq/KaaBuhCEBpY5g4Hj6N9pMrbM3zP4lJrD80N/lDXY43m8WArq5t01dvg6LFSgh8V/CGKDy0+hGn8Rs6YK7VurRk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=H72KEJ6e; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id CBAB4C433B1; Fri, 2 Feb 2024 22:05:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706911502; bh=aCy4fZif4NL+l3ntdf5UR0MbQ0NnUASsa/mDDkArAHw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=H72KEJ6eDvvnP8OCPT3PAVAh1TyHCir1mq44pogmZg7Wn1Ihk6tAKfuwL6wKfe3tD Ediu9JFW8JZ9kc2us+fdXnpBPkUQ4zUY3gpY43F2nQ6RLy3jwRzUTl5xtiY5pUTa+3 iPMDb2rjwqQBSENbrQITBpnzr0tdWe414U7RAy9C5Pqvby6J52PYuKhslW3yl4668f 1CVdKDXb7w3WKh//L0REaNEQHRm7lP5tTQfzoUmYzvsN+NuD0fiPe6BZVuMPH9K8ji mPiwwactW9CnctdW+iZaDVCrMtFeFqabilpv0v7j9ZUa7llhwXZNBhoKW7KFkD19Q5 AJoX1+PSH4eog== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , 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 01/14] perf dwarf-aux: Add die_collect_vars() Date: Fri, 2 Feb 2024 14:04:46 -0800 Message-ID: <20240202220459.527138-2-namhyung@kernel.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240202220459.527138-1-namhyung@kernel.org> References: <20240202220459.527138-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789827038073236781 X-GMAIL-MSGID: 1789827038073236781 The die_collect_vars() is to find all variable information in the scope including function parameters. The struct die_var_type is to save the type of the variable with the location (reg and offset) as well as where it's defined in the code (addr). Acked-by: Masami Hiramatsu (Google) Signed-off-by: Namhyung Kim --- tools/perf/util/dwarf-aux.c | 118 +++++++++++++++++++++++++++--------- tools/perf/util/dwarf-aux.h | 17 ++++++ 2 files changed, 107 insertions(+), 28 deletions(-) diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c index 2791126069b4..f878014c9e27 100644 --- a/tools/perf/util/dwarf-aux.c +++ b/tools/perf/util/dwarf-aux.c @@ -1136,6 +1136,40 @@ int die_get_varname(Dwarf_Die *vr_die, struct strbuf *buf) return ret < 0 ? ret : strbuf_addf(buf, "\t%s", dwarf_diename(vr_die)); } +#if defined(HAVE_DWARF_GETLOCATIONS_SUPPORT) || defined(HAVE_DWARF_CFI_SUPPORT) +static int reg_from_dwarf_op(Dwarf_Op *op) +{ + switch (op->atom) { + case DW_OP_reg0 ... DW_OP_reg31: + return op->atom - DW_OP_reg0; + case DW_OP_breg0 ... DW_OP_breg31: + return op->atom - DW_OP_breg0; + case DW_OP_regx: + case DW_OP_bregx: + return op->number; + default: + break; + } + return -1; +} + +static int offset_from_dwarf_op(Dwarf_Op *op) +{ + switch (op->atom) { + case DW_OP_reg0 ... DW_OP_reg31: + case DW_OP_regx: + return 0; + case DW_OP_breg0 ... DW_OP_breg31: + return op->number; + case DW_OP_bregx: + return op->number2; + default: + break; + } + return -1; +} +#endif /* HAVE_DWARF_GETLOCATIONS_SUPPORT || HAVE_DWARF_CFI_SUPPORT */ + #ifdef HAVE_DWARF_GETLOCATIONS_SUPPORT /** * die_get_var_innermost_scope - Get innermost scope range of given variable DIE @@ -1479,41 +1513,69 @@ Dwarf_Die *die_find_variable_by_addr(Dwarf_Die *sc_die, Dwarf_Addr pc, *offset = data.offset; return result; } -#endif /* HAVE_DWARF_GETLOCATIONS_SUPPORT */ -#ifdef HAVE_DWARF_CFI_SUPPORT -static int reg_from_dwarf_op(Dwarf_Op *op) +static int __die_collect_vars_cb(Dwarf_Die *die_mem, void *arg) { - switch (op->atom) { - case DW_OP_reg0 ... DW_OP_reg31: - return op->atom - DW_OP_reg0; - case DW_OP_breg0 ... DW_OP_breg31: - return op->atom - DW_OP_breg0; - case DW_OP_regx: - case DW_OP_bregx: - return op->number; - default: - break; - } - return -1; + struct die_var_type **var_types = arg; + Dwarf_Die type_die; + int tag = 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 != 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; + + /* + * Only collect the first location as it can reconstruct the + * remaining state by following the instructions. + * start = 0 means it covers the whole range. + */ + if (dwarf_getlocations(&attr, 0, &base, &start, &end, &ops, &nops) <= 0) + return DIE_FIND_CB_SIBLING; + + if (die_get_real_type(die_mem, &type_die) == NULL) + return DIE_FIND_CB_SIBLING; + + vt = malloc(sizeof(*vt)); + if (vt == NULL) + return DIE_FIND_CB_END; + + vt->die_off = dwarf_dieoffset(&type_die); + vt->addr = start; + vt->reg = reg_from_dwarf_op(ops); + vt->offset = offset_from_dwarf_op(ops); + vt->next = *var_types; + *var_types = vt; + + return DIE_FIND_CB_SIBLING; } -static int offset_from_dwarf_op(Dwarf_Op *op) +/** + * die_collect_vars - Save all variables and parameters + * @sc_die: a scope DIE + * @var_types: a pointer to save the resulting list + * + * Save all variables and parameters in the @sc_die and save them to @var_types. + * The @var_types is a singly-linked list containing type and location info. + * Actual type can be retrieved using dwarf_offdie() with 'die_off' later. + * + * Callers should free @var_types. + */ +void die_collect_vars(Dwarf_Die *sc_die, struct die_var_type **var_types) { - switch (op->atom) { - case DW_OP_reg0 ... DW_OP_reg31: - case DW_OP_regx: - return 0; - case DW_OP_breg0 ... DW_OP_breg31: - return op->number; - case DW_OP_bregx: - return op->number2; - default: - break; - } - return -1; + Dwarf_Die die_mem; + + die_find_child(sc_die, __die_collect_vars_cb, (void *)var_types, &die_mem); } +#endif /* HAVE_DWARF_GETLOCATIONS_SUPPORT */ +#ifdef HAVE_DWARF_CFI_SUPPORT /** * die_get_cfa - Get frame base information * @dwarf: a Dwarf info diff --git a/tools/perf/util/dwarf-aux.h b/tools/perf/util/dwarf-aux.h index 85dd527ae1f7..efafd3a1f5b6 100644 --- a/tools/perf/util/dwarf-aux.h +++ b/tools/perf/util/dwarf-aux.h @@ -135,6 +135,15 @@ void die_skip_prologue(Dwarf_Die *sp_die, Dwarf_Die *cu_die, /* Get the list of including scopes */ int die_get_scopes(Dwarf_Die *cu_die, Dwarf_Addr pc, Dwarf_Die **scopes); +/* Variable type information */ +struct die_var_type { + struct die_var_type *next; + u64 die_off; + u64 addr; + int reg; + int offset; +}; + #ifdef HAVE_DWARF_GETLOCATIONS_SUPPORT /* Get byte offset range of given variable DIE */ @@ -150,6 +159,9 @@ Dwarf_Die *die_find_variable_by_addr(Dwarf_Die *sc_die, Dwarf_Addr pc, Dwarf_Addr addr, Dwarf_Die *die_mem, int *offset); +/* Save all variables and parameters in this scope */ +void die_collect_vars(Dwarf_Die *sc_die, struct die_var_type **var_types); + #else /* HAVE_DWARF_GETLOCATIONS_SUPPORT */ static inline int die_get_var_range(Dwarf_Die *sp_die __maybe_unused, @@ -178,6 +190,11 @@ static inline Dwarf_Die *die_find_variable_by_addr(Dwarf_Die *sc_die __maybe_unu return NULL; } +static inline void die_collect_vars(Dwarf_Die *sc_die __maybe_unused, + struct die_var_type **var_types __maybe_unused) +{ +} + #endif /* HAVE_DWARF_GETLOCATIONS_SUPPORT */ #ifdef HAVE_DWARF_CFI_SUPPORT From patchwork Fri Feb 2 22:04:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 196063 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp732961dyc; Fri, 2 Feb 2024 14:31:16 -0800 (PST) X-Google-Smtp-Source: AGHT+IHxJP6K4DvmJkwSlT4dhewLP57THw812JCAmQlXXfyYaHIo3pPpgNtemSw54Dwbxvfuxv6n X-Received: by 2002:a17:906:a888:b0:a37:21cb:f3fc with SMTP id ha8-20020a170906a88800b00a3721cbf3fcmr8544ejb.9.1706913075750; Fri, 02 Feb 2024 14:31:15 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706913075; cv=pass; d=google.com; s=arc-20160816; b=K70LpqHUZwW7zuFFllcVav1tXWCWqeGWAtJZESqcp6COjvMw3KcKivrzmGLIUpK4bK TLT7YqLAJb4I4umVu7HmKagmOYY7L+G71YNjn7DEgoPqIFQPYPsFOHDQ6qoUaDCfYb6c Juo4fT4H1WbN6T1uAY4su6yBsKuqfsr4VUHMrcqSkxRd+5nCRC4LoCvDJSjLR5WvG6wW Qd70auTD5ibDU0rQTp9HYlpaVrkZKbujZlI/xfYNSJKZo5Pu11hQ83KIJRKN9Xt1payp E5wA1VHOgG1yiOqLc9UBsyhpl4R8ysDnGIGNMaTcoMwYVZG8jsAtNLPOuuSClTwy0SVv pQPg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=x2DL2UveeLpLuMsEbdteBN2ifTTjqB6/QPkHXz4u9aA=; fh=1JxWv9fMrOG8nRCB3voAngGvC37qtsRktANVPyWADfI=; b=v1SxyjmFSgyPJEJdeNql9DoERqBsALtMi83CElVTzlutdyC8kKu+QXOZXj9RsK1ilB t7vXMyQHfXFAQIUjvuPwtnt7g8n6QDcPuBI+eE1ufAVIPl5B6kHUmIqM2CCj+hPaMECT 8b2QROOC9r/oIx5rler7vSWpgDQquYYVH40fyNq+74bMP9J97v9JP3SDQBYUVln6f+ja O4rUYZJHYo2+tC75EGVc4fQWiHj3EUUHYxzMfLKYzlG3PQ5bNx7RThNZY+76r0bUOEEL 7Edd0286JfMRNXyPTWncOCaT0Bfb34x3iYsKty85CDyQIY7xy4m9GQ7uscg3zCJ2OZp3 4IFA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="P/QlBZOV"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50635-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50635-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCXBOeJrZ8TrDvq7qxoLeNo/ykBKJiS0+onQfcTsm49y3zGzI7nD5oLfqnyQjScVvVXjiyffaZiW0NyTryMz6e0ZufGQBQ== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id f15-20020a17090624cf00b00a35c1f2ea3esi1245577ejb.992.2024.02.02.14.31.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 14:31:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50635-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="P/QlBZOV"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50635-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50635-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 823E81F2A170 for ; Fri, 2 Feb 2024 22:06:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A6E2C12D742; Fri, 2 Feb 2024 22:05:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="P/QlBZOV" 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 5A4CC85935; Fri, 2 Feb 2024 22:05:03 +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=1706911503; cv=none; b=p38cHaLGICu61442RyIBsVwb425sigdTe4Rqw6/eJcZFYp+YS/XS68Gmzh9DRwKFxChtC3aK9aWq4VeQLVrmPyWoLtE+mrrt3YIgkBh4U9gPep3gI69DPbhFCqiPPKOY+aw+lOE2p4dGPkbmQwtgwBgNIqpE68PrZw+qbvxk9v8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706911503; c=relaxed/simple; bh=jmdfikaTg6ynLFAg/Mk8pHeUzgNbVHrf3IL/djBhCOo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aULANspnAOHviM3ZwXldvbCxw6RMnyBrBcjfNOlhqDsfIPs+wJoRvUnXmuxDXtlx7kIGP+uH4qzFyvFedYparS3H1HtannLf7wyBESzw2VizL4KmSrDQEshjrpbOPqr7413mHTplF/h4Qg2zXuJ3t866Z9icw7t9PDBkVTmcSDk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=P/QlBZOV; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 72704C43390; Fri, 2 Feb 2024 22:05:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706911502; bh=jmdfikaTg6ynLFAg/Mk8pHeUzgNbVHrf3IL/djBhCOo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=P/QlBZOVURwJt2W/TTEwL1Dvui8C5poLRMPdhqanzuhMTyxGyNpoIidnOWhm774cC D8EhZH5bODyzLHfCDSHvgzbdqQQnk2Oz0qE5uEhp3jhx8/21TyVAH9foX5SuWTOXY4 XwlY/acsoBI7vPjTSOEZk8SW1sUm2p8a31KAY8QX8dsOBPLLgQ8T+s8wZv+mIlMFlP DldTqmONWIWvdSzH6j5TCbx+1d3lvY78BQcWcMlMA4liRCJ36Lxn1tMb0dqgY4+yyV LXyHM+gYg2uf2or0IJJ6MZmsJpqSZ0l1uCH0q+tWyxXfNrmGULnlBjyf1KW9mkqh1R pqiUhm12vR1dg== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , 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 02/14] perf dwarf-aux: Handle type transfer for memory access Date: Fri, 2 Feb 2024 14:04:47 -0800 Message-ID: <20240202220459.527138-3-namhyung@kernel.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240202220459.527138-1-namhyung@kernel.org> References: <20240202220459.527138-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789828085523141313 X-GMAIL-MSGID: 1789828085523141313 We want to track type states as instructions are executed. Each instruction can access compound types like struct or union and load/ store its members to a different location. The die_deref_ptr_type() is to find a type of memory access with a pointer variable. If it points to a compound type like struct, the target memory is a member in the struct. The access will happen with an offset indicating which member it refers. Let's follow the DWARF info to figure out the type of the pointer target. For example, say we have the following code. struct foo { int a; int b; }; struct foo *p = malloc(sizeof(*p)); p->b = 0; The last pointer access should produce x86 asm like below: mov 0x0, 4(%rbx) And we know %rbx register has a pointer to struct foo. Then offset 4 should return the debug info of member 'b'. Also variables of compound types can be accessed directly without a pointer. The die_get_member_type() is to handle a such case. Acked-by: Masami Hiramatsu (Google) Signed-off-by: Namhyung Kim --- tools/perf/util/dwarf-aux.c | 110 ++++++++++++++++++++++++++++++++++++ tools/perf/util/dwarf-aux.h | 6 ++ 2 files changed, 116 insertions(+) diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c index f878014c9e27..39851ff1d5c4 100644 --- a/tools/perf/util/dwarf-aux.c +++ b/tools/perf/util/dwarf-aux.c @@ -1841,3 +1841,113 @@ int die_get_scopes(Dwarf_Die *cu_die, Dwarf_Addr pc, Dwarf_Die **scopes) *scopes = data.scopes; return data.nr; } + +static int __die_find_member_offset_cb(Dwarf_Die *die_mem, void *arg) +{ + Dwarf_Die type_die; + Dwarf_Word size, loc; + Dwarf_Word offset = (long)arg; + int tag = dwarf_tag(die_mem); + + if (tag != DW_TAG_member) + return DIE_FIND_CB_SIBLING; + + /* Unions might not have location */ + if (die_get_data_member_location(die_mem, &loc) < 0) + loc = 0; + + if (offset == loc) + return DIE_FIND_CB_END; + + die_get_real_type(die_mem, &type_die); + + if (dwarf_aggregate_size(&type_die, &size) < 0) + size = 0; + + if (loc < offset && offset < (loc + size)) + return DIE_FIND_CB_END; + + return DIE_FIND_CB_SIBLING; +} + +/** + * die_get_member_type - Return type info of struct member + * @type_die: a type DIE + * @offset: offset in the type + * @die_mem: a buffer to save the resulting DIE + * + * This function returns a type of a member in @type_die where it's located at + * @offset if it's a struct. For now, it just returns the first matching + * member in a union. For other types, it'd return the given type directly + * if it's within the size of the type or NULL otherwise. + */ +Dwarf_Die *die_get_member_type(Dwarf_Die *type_die, int offset, + Dwarf_Die *die_mem) +{ + Dwarf_Die *member; + Dwarf_Die mb_type; + int tag; + + tag = dwarf_tag(type_die); + /* If it's not a compound type, return the type directly */ + if (tag != DW_TAG_structure_type && tag != DW_TAG_union_type) { + Dwarf_Word size; + + if (dwarf_aggregate_size(type_die, &size) < 0) + size = 0; + + if ((unsigned)offset >= size) + return NULL; + + *die_mem = *type_die; + return die_mem; + } + + mb_type = *type_die; + /* TODO: Handle union types better? */ + while (tag == DW_TAG_structure_type || tag == DW_TAG_union_type) { + member = die_find_child(&mb_type, __die_find_member_offset_cb, + (void *)(long)offset, die_mem); + if (member == NULL) + return NULL; + + if (die_get_real_type(member, &mb_type) == NULL) + return NULL; + + tag = dwarf_tag(&mb_type); + + if (tag == DW_TAG_structure_type || tag == DW_TAG_union_type) { + Dwarf_Word loc; + + /* Update offset for the start of the member struct */ + if (die_get_data_member_location(member, &loc) == 0) + offset -= loc; + } + } + *die_mem = mb_type; + return die_mem; +} + +/** + * die_deref_ptr_type - Return type info for pointer access + * @ptr_die: a pointer type DIE + * @offset: access offset for the pointer + * @die_mem: a buffer to save the resulting DIE + * + * This function follows the pointer in @ptr_die with given @offset + * and saves the resulting type in @die_mem. If the pointer points + * a struct type, actual member at the offset would be returned. + */ +Dwarf_Die *die_deref_ptr_type(Dwarf_Die *ptr_die, int offset, + Dwarf_Die *die_mem) +{ + Dwarf_Die type_die; + + if (dwarf_tag(ptr_die) != DW_TAG_pointer_type) + return NULL; + + if (die_get_real_type(ptr_die, &type_die) == NULL) + return NULL; + + return die_get_member_type(&type_die, offset, die_mem); +} diff --git a/tools/perf/util/dwarf-aux.h b/tools/perf/util/dwarf-aux.h index efafd3a1f5b6..ad4d7322fcbf 100644 --- a/tools/perf/util/dwarf-aux.h +++ b/tools/perf/util/dwarf-aux.h @@ -144,6 +144,12 @@ struct die_var_type { int offset; }; +/* Return type info of a member at offset */ +Dwarf_Die *die_get_member_type(Dwarf_Die *type_die, int offset, Dwarf_Die *die_mem); + +/* Return type info where the pointer and offset point to */ +Dwarf_Die *die_deref_ptr_type(Dwarf_Die *ptr_die, int offset, Dwarf_Die *die_mem); + #ifdef HAVE_DWARF_GETLOCATIONS_SUPPORT /* Get byte offset range of given variable DIE */ From patchwork Fri Feb 2 22:04:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 196045 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp724212dyc; Fri, 2 Feb 2024 14:07:22 -0800 (PST) X-Google-Smtp-Source: AGHT+IHdPov6oQxxbaSuNkwHNJHvVcr8avR5ddMHI2amq53A/lOUfJrYa3sCyaZwqwg6RtJI0Q6d X-Received: by 2002:a05:6830:1347:b0:6e0:cd09:47a4 with SMTP id r7-20020a056830134700b006e0cd0947a4mr7333591otq.35.1706911642603; Fri, 02 Feb 2024 14:07:22 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706911642; cv=pass; d=google.com; s=arc-20160816; b=g0C04QLCLV+1v2Wq4sdcUbh6ivQnuuYXCPggRFlZE2r/Bb3+I6ffpIock0ql4RNR6c VgqbbA/bg5rUjYJ9+naSmx3y1UqF9I9sDcY+muwWsYE/9vIBaZ35rVx0xGjjAfSio6t5 kWVQGi6PomGsOonCaVSt2b3OFPm/DleVd+uZH/NAuwhv8ybosbR1YvFkjiyzNzj22U4B moogT9pFWqhvVlp763OLpOn+3JSPVcmfKDQ0c9YtFwRQDoVHrDrg6AIcXJtzwzkCGt1X FJa741PAXR9Ac09EcZeHYTvf1saMv6iR+uZisqOD046yc6CSr1JpAVn3sjQzezDITFHA hAwA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=pqbc4m6U6rECWlFwtyrtECVz3YpeMrNaNxD2CSMmi80=; fh=OuexnhhCmA7l+YUOm06NcIZf5iZbSqDio/8KBSkJ1Tw=; b=y1234HT4nF3rcy++0U1cEzmy/q9PXD0GZ3ek5qi5u05uJ6D2HkbtBYIJqvtlC4jRNQ A5LSYYU7wihsX+rFchR2C7C1hh7jLoeAtvB1PZ5/+S3ImeuCLn6NctQghkF2qNqjUKDW zKV807vVDdnQA+dOYgJrOk4Z4PhPdgLVEN54CxZgMmgJ294IEBOGNltD/EDQB+5rV0tC xR+hpHFAINQaMgA5kX+WD+e0KpzA8daHRjeCwo+kjbi2bnxk5fEG6HjZclwz/BJCZ0nm 7Qj23aRx/KS8r/6ULc5ERcj5XxZvJ8VwaLOf0Tlj/okOyKgtdUIvaibwxKnh8fbfoIhY p5ig==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=N1UkVyRZ; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50636-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50636-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCU2nM4ZG/5sTiI2L8PSwlJ8cR4hqpHRQSJr2zDrgl0VlAaY3W2uW3tXTrkKFEnljY41zawSH33Jz/cbB8EZ6nAzTpRBUg== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id i23-20020ac860d7000000b0042c00d88ac2si2600017qtm.5.2024.02.02.14.07.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 14:07:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50636-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=N1UkVyRZ; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50636-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50636-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 201A71C223D1 for ; Fri, 2 Feb 2024 22:07:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CDC73130AC4; Fri, 2 Feb 2024 22:05:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="N1UkVyRZ" 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 0C9E385954; Fri, 2 Feb 2024 22:05:03 +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=1706911504; cv=none; b=ZQiAKe76BZe5Ho0dtM7MGyZiw7PYnEq1r27rLdjy/rq5TAwNgWzvGItvq64XdrRptKFLMHkSNJk8oOFrYbc/7Aep54OU0ztPMMfwRIVwfdl8X3i4gy6vyssp5nkbPtbxl/B4lk5jZ7HqsH5+DWsbS5BsFXs4VPu3XHNtgnntAyg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706911504; c=relaxed/simple; bh=uJ/e1019xzEMihf4k8j5WuA+3QD3U7RV6gdd3li3qcM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uTe5vP8JCzxbxcUhyc1EU8g6rF5160/D9BYLXbLc5tFjnMD1OU9fdvAmmAW78+S6+GshTd6ds+wip8IriIfWuE4JAZaD8Uzwft0KL8+XmgB4WN0JdDuAk6V5IDxTV207BctlvRGFR6RyzZC9rEw+NQuwWzAbGRJGb1yMJu5avSk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=N1UkVyRZ; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1D205C43141; Fri, 2 Feb 2024 22:05:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706911503; bh=uJ/e1019xzEMihf4k8j5WuA+3QD3U7RV6gdd3li3qcM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=N1UkVyRZVT+xs1znMXAH8igdQjaH4dM3DMOQzFvvNSh43UxWzXZ8EjhJj7sCsBOqs 5uvqxmlGbNEsJ1JfnpqNkkyrSPs/SOu45kO5N58geptJ6+C6kbnmunFAru9QyljVjr y1WNgDP/g6IQsReh5ErMb8RaGOfQ+kczeBkSfbKwN5JP+9MsHEIBUljw2Z3+3jbL4F dJuH+J5EcbcoGbsw0Nh3uFNGmcPGKdUTbTy7vorEdSRe2rq8E5iwFsClKz3130kXEW +L+6L/WqaXt4Fhcj9JWGV7VN7rpjm3yyzR3FmDcKn/DIUehMEGE2kZhKmCtzdncV/g HMqZtSdfnqgMA== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , 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 03/14] perf annotate-data: Introduce struct data_loc_info Date: Fri, 2 Feb 2024 14:04:48 -0800 Message-ID: <20240202220459.527138-4-namhyung@kernel.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240202220459.527138-1-namhyung@kernel.org> References: <20240202220459.527138-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789826582407353848 X-GMAIL-MSGID: 1789826582407353848 The find_data_type() needs many information to describe the location of the data. Add the new struct data_loc_info to pass those information at once. No functional changes intended. Signed-off-by: Namhyung Kim --- tools/perf/util/annotate-data.c | 83 +++++++++++++++++---------------- tools/perf/util/annotate-data.h | 38 ++++++++++++--- tools/perf/util/annotate.c | 30 ++++++------ 3 files changed, 91 insertions(+), 60 deletions(-) diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-data.c index 30c4d19fcf11..b8e60c42af8c 100644 --- a/tools/perf/util/annotate-data.c +++ b/tools/perf/util/annotate-data.c @@ -239,21 +239,28 @@ static int check_variable(Dwarf_Die *var_die, Dwarf_Die *type_die, int offset, } /* The result will be saved in @type_die */ -static int find_data_type_die(struct debuginfo *di, u64 pc, u64 addr, - const char *var_name, struct annotated_op_loc *loc, - Dwarf_Die *type_die) +static int find_data_type_die(struct data_loc_info *dloc, Dwarf_Die *type_die) { + struct annotated_op_loc *loc = dloc->op; Dwarf_Die cu_die, var_die; Dwarf_Die *scopes = NULL; int reg, offset; int ret = -1; int i, nr_scopes; int fbreg = -1; - bool is_fbreg = false; int fb_offset = 0; + bool is_fbreg = false; + u64 pc; + + /* + * IP is a relative instruction address from the start of the map, as + * it can be randomized/relocated, it needs to translate to PC which is + * a file address for DWARF processing. + */ + pc = map__rip_2objdump(dloc->ms->map, dloc->ip); /* Get a compile_unit for this address */ - if (!find_cu_die(di, pc, &cu_die)) { + if (!find_cu_die(dloc->di, pc, &cu_die)) { pr_debug("cannot find CU for address %" PRIx64 "\n", pc); ann_data_stat.no_cuinfo++; return -1; @@ -263,18 +270,19 @@ static int find_data_type_die(struct debuginfo *di, u64 pc, u64 addr, offset = loc->offset; if (reg == DWARF_REG_PC) { - if (die_find_variable_by_addr(&cu_die, pc, addr, &var_die, &offset)) { + if (die_find_variable_by_addr(&cu_die, pc, dloc->var_addr, + &var_die, &offset)) { ret = check_variable(&var_die, type_die, offset, /*is_pointer=*/false); - loc->offset = offset; + dloc->type_offset = offset; goto out; } - if (var_name && die_find_variable_at(&cu_die, var_name, pc, - &var_die)) { - ret = check_variable(&var_die, type_die, 0, + if (dloc->var_name && + die_find_variable_at(&cu_die, dloc->var_name, pc, &var_die)) { + ret = check_variable(&var_die, type_die, dloc->type_offset, /*is_pointer=*/false); - /* loc->offset will be updated by the caller */ + /* dloc->type_offset was updated by the caller */ goto out; } } @@ -291,10 +299,11 @@ static int find_data_type_die(struct debuginfo *di, u64 pc, u64 addr, dwarf_formblock(&attr, &block) == 0 && block.length == 1) { switch (*block.data) { case DW_OP_reg0 ... DW_OP_reg31: - fbreg = *block.data - DW_OP_reg0; + fbreg = dloc->fbreg = *block.data - DW_OP_reg0; break; case DW_OP_call_frame_cfa: - if (die_get_cfa(di->dbg, pc, &fbreg, + dloc->fb_cfa = true; + if (die_get_cfa(dloc->di->dbg, pc, &fbreg, &fb_offset) < 0) fbreg = -1; break; @@ -312,7 +321,7 @@ static int find_data_type_die(struct debuginfo *di, u64 pc, u64 addr, /* Search from the inner-most scope to the outer */ for (i = nr_scopes - 1; i >= 0; i--) { if (reg == DWARF_REG_PC) { - if (!die_find_variable_by_addr(&scopes[i], pc, addr, + if (!die_find_variable_by_addr(&scopes[i], pc, dloc->var_addr, &var_die, &offset)) continue; } else { @@ -325,7 +334,7 @@ static int find_data_type_die(struct debuginfo *di, u64 pc, u64 addr, /* Found a variable, see if it's correct */ ret = check_variable(&var_die, type_die, offset, reg != DWARF_REG_PC && !is_fbreg); - loc->offset = offset; + dloc->type_offset = offset; goto out; } @@ -344,50 +353,46 @@ static int find_data_type_die(struct debuginfo *di, u64 pc, u64 addr, /** * find_data_type - Return a data type at the location - * @ms: map and symbol at the location - * @ip: instruction address of the memory access - * @loc: instruction operand location - * @addr: data address of the memory access - * @var_name: global variable name + * @dloc: data location * * This functions searches the debug information of the binary to get the data - * type it accesses. The exact location is expressed by (@ip, reg, offset) - * for pointer variables or (@ip, @addr) for global variables. Note that global - * variables might update the @loc->offset after finding the start of the variable. - * If it cannot find a global variable by address, it tried to fine a declaration - * of the variable using @var_name. In that case, @loc->offset won't be updated. + * type it accesses. The exact location is expressed by (ip, reg, offset) + * for pointer variables or (ip, addr) for global variables. Note that global + * variables might update the @dloc->type_offset after finding the start of the + * variable. If it cannot find a global variable by address, it tried to find + * a declaration of the variable using var_name. In that case, @dloc->offset + * won't be updated. * * It return %NULL if not found. */ -struct annotated_data_type *find_data_type(struct map_symbol *ms, u64 ip, - struct annotated_op_loc *loc, u64 addr, - const char *var_name) +struct annotated_data_type *find_data_type(struct data_loc_info *dloc) { struct annotated_data_type *result = NULL; - struct dso *dso = map__dso(ms->map); - struct debuginfo *di; + struct dso *dso = map__dso(dloc->ms->map); Dwarf_Die type_die; - u64 pc; - di = debuginfo__new(dso->long_name); - if (di == NULL) { + dloc->di = debuginfo__new(dso->long_name); + if (dloc->di == NULL) { pr_debug("cannot get the debug info\n"); return NULL; } /* - * IP is a relative instruction address from the start of the map, as - * it can be randomized/relocated, it needs to translate to PC which is - * a file address for DWARF processing. + * The type offset is the same as instruction offset by default. + * But when finding a global variable, the offset won't be valid. */ - pc = map__rip_2objdump(ms->map, ip); - if (find_data_type_die(di, pc, addr, var_name, loc, &type_die) < 0) + if (dloc->var_name == NULL) + dloc->type_offset = dloc->op->offset; + + dloc->fbreg = -1; + + if (find_data_type_die(dloc, &type_die) < 0) goto out; result = dso__findnew_data_type(dso, &type_die); out: - debuginfo__delete(di); + debuginfo__delete(dloc->di); return result; } diff --git a/tools/perf/util/annotate-data.h b/tools/perf/util/annotate-data.h index 1b0db8e8c40e..ad6493ea2c8e 100644 --- a/tools/perf/util/annotate-data.h +++ b/tools/perf/util/annotate-data.h @@ -8,6 +8,7 @@ #include struct annotated_op_loc; +struct debuginfo; struct evsel; struct map_symbol; @@ -72,6 +73,35 @@ struct annotated_data_type { extern struct annotated_data_type unknown_type; extern struct annotated_data_type stackop_type; +/** + * struct data_loc_info - Data location information + * @ms: Map and Symbol info + * @ip: Instruction address + * @var_addr: Data address (for global variables) + * @var_name: Variable name (for global variables) + * @op: Instruction operand location (regs and offset) + * @di: Debug info + * @fbreg: Frame base register + * @fb_cfa: Whether the frame needs to check CFA + * @type_offset: Final offset in the type + */ +struct data_loc_info { + /* These are input field, should be filled by caller */ + struct map_symbol *ms; + u64 ip; + u64 var_addr; + const char *var_name; + struct annotated_op_loc *op; + + /* These are used internally */ + struct debuginfo *di; + int fbreg; + bool fb_cfa; + + /* This is for the result */ + int type_offset; +}; + /** * struct annotated_data_stat - Debug statistics * @total: Total number of entry @@ -106,9 +136,7 @@ extern struct annotated_data_stat ann_data_stat; #ifdef HAVE_DWARF_SUPPORT /* Returns data type at the location (ip, reg, offset) */ -struct annotated_data_type *find_data_type(struct map_symbol *ms, u64 ip, - struct annotated_op_loc *loc, u64 addr, - const char *var_name); +struct annotated_data_type *find_data_type(struct data_loc_info *dloc); /* Update type access histogram at the given offset */ int annotated_data_type__update_samples(struct annotated_data_type *adt, @@ -121,9 +149,7 @@ void annotated_data_type__tree_delete(struct rb_root *root); #else /* HAVE_DWARF_SUPPORT */ static inline struct annotated_data_type * -find_data_type(struct map_symbol *ms __maybe_unused, u64 ip __maybe_unused, - struct annotated_op_loc *loc __maybe_unused, - u64 addr __maybe_unused, const char *var_name __maybe_unused) +find_data_type(struct data_loc_info *dloc __maybe_unused) { return NULL; } diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index 107b264fa41e..cb5d4c517a4d 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -3788,9 +3788,7 @@ struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he) struct annotated_op_loc *op_loc; struct annotated_data_type *mem_type; struct annotated_item_stat *istat; - u64 ip = he->ip, addr = 0; - const char *var_name = NULL; - int var_offset; + u64 ip = he->ip; int i; ann_data_stat.total++; @@ -3843,51 +3841,53 @@ struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he) } for_each_insn_op_loc(&loc, i, op_loc) { + struct data_loc_info dloc = { + .ms = ms, + /* Recalculate IP for LOCK prefix or insn fusion */ + .ip = ms->sym->start + dl->al.offset, + .op = op_loc, + }; + if (!op_loc->mem_ref) continue; /* Recalculate IP because of LOCK prefix or insn fusion */ ip = ms->sym->start + dl->al.offset; - var_offset = op_loc->offset; - /* PC-relative addressing */ if (op_loc->reg1 == DWARF_REG_PC) { struct addr_location al; struct symbol *var; u64 map_addr; - addr = annotate_calc_pcrel(ms, ip, op_loc->offset, dl); + dloc.var_addr = annotate_calc_pcrel(ms, ip, op_loc->offset, dl); /* Kernel symbols might be relocated */ - map_addr = addr + map__reloc(ms->map); + map_addr = dloc.var_addr + map__reloc(ms->map); addr_location__init(&al); var = thread__find_symbol_fb(he->thread, he->cpumode, map_addr, &al); if (var) { - var_name = var->name; + dloc.var_name = var->name; /* Calculate type offset from the start of variable */ - var_offset = map_addr - map__unmap_ip(al.map, var->start); + dloc.type_offset = map_addr - map__unmap_ip(al.map, var->start); } addr_location__exit(&al); } - mem_type = find_data_type(ms, ip, op_loc, addr, var_name); + mem_type = find_data_type(&dloc); if (mem_type) istat->good++; else istat->bad++; - if (mem_type && var_name) - op_loc->offset = var_offset; - if (symbol_conf.annotate_data_sample) { annotated_data_type__update_samples(mem_type, evsel, - op_loc->offset, + dloc.type_offset, he->stat.nr_events, he->stat.period); } - he->mem_type_off = op_loc->offset; + he->mem_type_off = dloc.type_offset; return mem_type; } From patchwork Fri Feb 2 22:04:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 196044 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp723904dyc; Fri, 2 Feb 2024 14:06:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IG8So8M8id+zdltAraZkJvZgj4qNgFQTIbs77OZrxcqD4BTi/wt0QSBnInG0jtwPIwKzb3q X-Received: by 2002:a17:90a:c684:b0:28e:20d5:a047 with SMTP id n4-20020a17090ac68400b0028e20d5a047mr6689470pjt.27.1706911603117; Fri, 02 Feb 2024 14:06:43 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706911603; cv=pass; d=google.com; s=arc-20160816; b=ypdJy50MkAiX3ZNHjQHeg3DBqLscy8CswGzsjsQ2LA+xmz7VaURrfVPSidrtY3QTvM TolAXyc3O2pE9uvqbn0HlTCs86BbpiqSimZG9FkJlcNoc+Tg4kDN80X/KW+2PeFGlRzs 2f+JA7Iq1zdWIL0Y4z+qcSOgldDrJd2s7/LpdcZTFfDUe+ezCeROIJzOy6JbrTFJDOl3 k4LMdE30F5rTXRIon/OHhV1dEeIBmA+UcYXeXfsHlEv78oP5RLRVUieGAyOr8q40Twv+ Xp48nmoTWzSXsvoFO/rASx2IJ0J7ITKS68mS1aomvoRrPWkyT34LMJ7gXefqZmREHDi7 2nuQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=vOc2Z8JyMWK1Cos1JZ+vw5yBKHBtj+bfr6QqbvgGh24=; fh=hciVlrIpZng8ZH56LuNKBzZCwMRhTxchalCyg/wl3HA=; b=T7xYqTzCdg6VEv4fMFwZWuGwAbkZ+a9TwEVHydFhfNDW8qs8zn4HrUZAp//gQ+dy7+ PDYm2r6XQYQhx7Qhsr4AIIzU9Gf+YrqoO4CQUbffHaQ5ccjuPIe5knxkAEQiQ3fm1qF3 +q/YO3yr/Bmp44bb/2zULx0IoxxqKRfH5cgK5iUkhj5FhrQDdXqHi5uiyzk1S5YPZf9Z klHSHvw1oIE4jruiD+H84yaZI1V54vOEvMSLUYElKtTv3DJMFu9NG09FZLpSOMsX8gcN CAys0XCnpk/BgiZr+VbtQ+Snb7LrwWsAGqRo+iDnz3D0JJXTXBDlUmi2LtEP9NARVWAP 93Iw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=m77Rje8K; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50637-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50637-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCUwLp48F2Z/9X/b7D317TEJEpmxw1OXmaCyy97p2A7axlzcDda55og94NffrbJvOLUADnvqfvgIEMgxvVxKfGypPyygtQ== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id mh4-20020a17090b4ac400b00296563fbb75si547016pjb.95.2024.02.02.14.06.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 14:06:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50637-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=m77Rje8K; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50637-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50637-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id DB2EA28AFDF for ; Fri, 2 Feb 2024 22:06:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 51A9612D76C; Fri, 2 Feb 2024 22:05:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="m77Rje8K" 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 5EE19126F05; Fri, 2 Feb 2024 22:05:04 +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=1706911504; cv=none; b=AEf1MDc50sIOPZOTXzmW/z3RoCEw+3QUJ8XkRoc9RSe8od5lyjc2rBYHFjbWJyjeW57GiFa/Qm8w40VmS0nPYmRicyH/KXVRAJbWWLAG6LNyGPCHtKJ8IkS48RjGXlO8PgpLyXDAiSaGnTehnslUAzyi3AHiy0SA5+wFUqESUww= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706911504; c=relaxed/simple; bh=ksRwB6y7Bf3D1VViK194iwXHp5tebaWmPZdpxqK0sY0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TnA+i7v3d3liwQtiQbnX2FuNxv3XX+hkxS+AorjE3vfrYxZfFHW614gjn+rw2D7ntfVaHMEOP5QcfjbGVuz2jB9MTV+WsDXUEjwzNc5nUOHadzcA1/rSOS8KWE0HyHb29DsCQsL6RXYibpSXv9yFTLJGLTE5ORCORC41xrwTEzA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=m77Rje8K; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id B86C7C43142; Fri, 2 Feb 2024 22:05:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706911504; bh=ksRwB6y7Bf3D1VViK194iwXHp5tebaWmPZdpxqK0sY0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=m77Rje8KR9qi8D7/IwR6G3a4rHlf/J2LNxrkGo9chaQfeUpCtzwJCtvn2yEtaELOR bPS5LmLOZlDx3zlMgBjlypofa0Av7AgggOIMMEYWCI9fEsu7vSpl2r8xY3zcohzotp Jwir98s5VsJsH9Y5CbUem2Ye+RenIHOZDeK+HevXhlTta9PchwxswwTv/w1RAXO4eX QPb0eL+G+aoJ6wQ5xEADdI3PXuLlIn77WFMMCqqgoB1txsrQ3/i+qa4LwzVnV9qzPw jxLWHtmv0kU2eQnID/E4vs9JnoJ1QnmzjGIgA3giByztmpgvXLS46P1QrZGYS3JgND Y9jq+KcezyAmQ== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , 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 04/14] perf map: Add map__objdump_2rip() Date: Fri, 2 Feb 2024 14:04:49 -0800 Message-ID: <20240202220459.527138-5-namhyung@kernel.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240202220459.527138-1-namhyung@kernel.org> References: <20240202220459.527138-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789826540855746179 X-GMAIL-MSGID: 1789826540855746179 Sometimes we want to convert an address in objdump output to map-relative address to match with a sample data. Let's add map__objdump_2rip() for that. Cc: Adrian Hunter Signed-off-by: Namhyung Kim --- tools/perf/util/map.c | 20 ++++++++++++++++++++ tools/perf/util/map.h | 3 +++ 2 files changed, 23 insertions(+) diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c index 54c67cb7ecef..66542864b7b5 100644 --- a/tools/perf/util/map.c +++ b/tools/perf/util/map.c @@ -594,6 +594,26 @@ u64 map__objdump_2mem(struct map *map, u64 ip) return ip + map__reloc(map); } +u64 map__objdump_2rip(struct map *map, u64 ip) +{ + const struct dso *dso = map__dso(map); + + if (!dso->adjust_symbols) + return ip; + + if (dso->rel) + return ip + map__pgoff(map); + + /* + * kernel modules also have DSO_TYPE_USER in dso->kernel, + * but all kernel modules are ET_REL, so won't get here. + */ + if (dso->kernel == DSO_SPACE__USER) + return ip - dso->text_offset; + + return map__map_ip(map, ip + map__reloc(map)); +} + bool map__contains_symbol(const struct map *map, const struct symbol *sym) { u64 ip = map__unmap_ip(map, sym->start); diff --git a/tools/perf/util/map.h b/tools/perf/util/map.h index 49756716cb13..65e2609fa1b1 100644 --- a/tools/perf/util/map.h +++ b/tools/perf/util/map.h @@ -132,6 +132,9 @@ u64 map__rip_2objdump(struct map *map, u64 rip); /* objdump address -> memory address */ u64 map__objdump_2mem(struct map *map, u64 ip); +/* objdump address -> rip */ +u64 map__objdump_2rip(struct map *map, u64 ip); + struct symbol; struct thread; From patchwork Fri Feb 2 22:04:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 196046 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp724435dyc; Fri, 2 Feb 2024 14:08:00 -0800 (PST) X-Google-Smtp-Source: AGHT+IGilHY0bCKUvvXe+A5hnunwxvdfec13KXihWH5pN65DUJ/yrFZURTr/Mm0OnwLjXM3nWVuF X-Received: by 2002:a1f:dd83:0:b0:4c0:13af:a5fd with SMTP id u125-20020a1fdd83000000b004c013afa5fdmr1602115vkg.4.1706911680132; Fri, 02 Feb 2024 14:08:00 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706911680; cv=pass; d=google.com; s=arc-20160816; b=wllkgISxyJ4ULoc2soDAK36KnPBcShTa/A/uGpKMdyua/DyG2oJjDlgCN2IXUDxI/D nKgIQGQ2twd7k2gpnvJKdrQN1xYXvVF3WMSFWyQvOgtFAHH+FxH6DC5OVBNQE9yRBynR VBDj4GdcTaZl0v8vGnySgQ/gsK/7VfkfVMsiRjLLM1bxxY/vO5dsbxkQAzf++L6QjR+U 3jAiu4oo/l6yxGs+LDXYuf7S0Tg2IkXDgWbsngqoZksPkbj3RY7VT7Trpgvzn/dNJesc lewtsGHSUcU1YCGm4MYU1jzgw8mWILFKkKXGJ/xooKlgy9CqXiUQLZj0kXx8AF/sWsvm iq9g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=7WmXeHMtkFUBOAaml+dhW6S8bfMmDhJhZmX1p/82Lx4=; fh=L6agEPPlxwhbKchelkOlfu+/HYy48H6fJdhbf13YfG8=; b=qPsCKxYeFZXEP3Riv14lEOWhE/kSg2GkIg0NLAX34u0fh1MLme1qqF/o5wbfOBV7ci ANe7CcUmkHPipM0IdiBL7JavAGKYF/ArT6NRjMaZDOacbitDYeYsURMLfCVsnykbOjAT CoPxAEwAKNAL4G6ndt3cqF3xTChqvjt4K/zaZBfu9JrofVjGm3vEGHgibOmD8C9Os77V W04RSHnQVkWFw4rauqRse6KZRJYDmIr1oktknWyzQdnopWrBMWEdqI5uyIz0xlmgZvHE VXd/rfWJKra1XcqanvMGuSIgqKD6qLKqUllrrGWN3ztt7Vi0SM3J2mqgneJw2KAXbGLz nbgA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="tPpbn/sg"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50638-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50638-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCWTlSEjVpO1bqPmCKwJn5MCv26ZZBgxW8Jgy1AB7w84K1HV0PQpwZIPtM+WmOp9p8s7jkcYMBa3JgbpKXsJ+WFSuK16jg== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id t4-20020a0cea24000000b0068c672f29cesi3070785qvp.160.2024.02.02.14.08.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 14:08:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50638-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="tPpbn/sg"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50638-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50638-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id D95AF1C23BF7 for ; Fri, 2 Feb 2024 22:07:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4A14313246B; Fri, 2 Feb 2024 22:05:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="tPpbn/sg" 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 3FFE2126F33; Fri, 2 Feb 2024 22:05:04 +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=1706911505; cv=none; b=aUzLjfjMuek7Cn/Skku0RFKix0pAkup+0TQ518oY+q1u2WQt1hso20yxfF3FY5ut96Wax9OIlM4zyTjoC7hyqAGPO6Clip0OGqdrEyGxophVRquUuRj+4FM5B7TjEy7jvyzPWQai7O4Q6EQuyZYtz1SosQcvsaLsO8ZervsQxIs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706911505; c=relaxed/simple; bh=CoZB4kUOW8Td3LHXDq6pCIm5c+YrDXmi5S6745qI3lc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=prtNCzf5b8QxsRozTyzyfXy0PZCN454sa0M6pMdbWKhM7/sh5FfsAYp97EI2TJQWJSAWDYz3SoMWLO3jzPrZPVBlOiDcYCu7pPNLhIkyfsG80ql0EwP3TKDEqRtlwDfLtr9tya6Gk5O1fIwUur8l62SvTGUWCTnL44WX45mu89A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=tPpbn/sg; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5A16BC433F1; Fri, 2 Feb 2024 22:05:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706911504; bh=CoZB4kUOW8Td3LHXDq6pCIm5c+YrDXmi5S6745qI3lc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tPpbn/sgOXiN2+mdhj2RcuMSO0sf0lugQe37V31j63X89xEy2kbssVkvby/gpnVyb nfxvVk6LqIM4Hm3Tmd4OtqKHew5OuuLulHCrtc5X0h5LRMXVV/g8Ib8aIpBlHAvDJb B3bkkh+41ztaZ2E6EKxmxNEUIl10oER5n9dWM7JZm0JIKAGObYLuhP9WieifMTpOTm 2CgrBka5OqnZ9xLOWM9T19K8FhbuxZWyq7N3l1pPRapbw8niU9sOoSBrmQTZtOC1sT U+wmKa83mf/s8V9FeRqOr1aHAGb3kqbJYjmjRSXtdpaY1w7bvnxfmEcmbhEaOTRiqT 3+bztm7yuvSmw== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , 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 05/14] perf annotate: Add annotate_get_basic_blocks() Date: Fri, 2 Feb 2024 14:04:50 -0800 Message-ID: <20240202220459.527138-6-namhyung@kernel.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240202220459.527138-1-namhyung@kernel.org> References: <20240202220459.527138-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789826621974513353 X-GMAIL-MSGID: 1789826621974513353 The annotate_get_basic_blocks() is to find a list of basic blocks from the source instruction to the destination instruction in a function. It'll be used to find variables in a scope. Use BFS (Breadth First Search) to find a shortest path to carry the variable/register state minimally. Also change find_disasm_line() to be used in annotate_get_basic_blocks() and add 'allow_update' argument to control if it can update the IP. Signed-off-by: Namhyung Kim --- tools/perf/util/annotate.c | 222 ++++++++++++++++++++++++++++++++++++- tools/perf/util/annotate.h | 16 +++ 2 files changed, 235 insertions(+), 3 deletions(-) diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index cb5d4c517a4d..4ef14b3f49e4 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -3686,7 +3686,8 @@ static void symbol__ensure_annotate(struct map_symbol *ms, struct evsel *evsel) } } -static struct disasm_line *find_disasm_line(struct symbol *sym, u64 ip) +static struct disasm_line *find_disasm_line(struct symbol *sym, u64 ip, + bool allow_update) { struct disasm_line *dl; struct annotation *notes; @@ -3699,7 +3700,8 @@ static struct disasm_line *find_disasm_line(struct symbol *sym, u64 ip) * llvm-objdump places "lock" in a separate line and * in that case, we want to get the next line. */ - if (!strcmp(dl->ins.name, "lock") && *dl->ops.raw == '\0') { + if (!strcmp(dl->ins.name, "lock") && + *dl->ops.raw == '\0' && allow_update) { ip++; continue; } @@ -3815,7 +3817,7 @@ struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he) * Get a disasm to extract the location from the insn. * This is too slow... */ - dl = find_disasm_line(ms->sym, ip); + dl = find_disasm_line(ms->sym, ip, /*allow_update=*/true); if (dl == NULL) { ann_data_stat.no_insn++; return NULL; @@ -3909,3 +3911,217 @@ struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he) istat->bad++; return NULL; } + +/* Basic block traversal (BFS) data structure */ +struct basic_block_data { + struct list_head queue; + struct list_head visited; +}; + +/* + * During the traversal, it needs to know the parent block where the current + * block block started from. Note that single basic block can be parent of + * two child basic blocks (in case of condition jump). + */ +struct basic_block_link { + struct list_head node; + struct basic_block_link *parent; + struct annotated_basic_block *bb; +}; + +/* Check any of basic block in the list already has the offset */ +static bool basic_block_has_offset(struct list_head *head, s64 offset) +{ + struct basic_block_link *link; + + list_for_each_entry(link, head, node) { + s64 begin_offset = link->bb->begin->al.offset; + s64 end_offset = link->bb->end->al.offset; + + if (begin_offset <= offset && offset <= end_offset) + return true; + } + return false; +} + +static bool is_new_basic_block(struct basic_block_data *bb_data, + struct disasm_line *dl) +{ + s64 offset = dl->al.offset; + + if (basic_block_has_offset(&bb_data->visited, offset)) + return false; + if (basic_block_has_offset(&bb_data->queue, offset)) + return false; + return true; +} + +/* Add a basic block starting from dl and link it to the parent */ +static int add_basic_block(struct basic_block_data *bb_data, + struct basic_block_link *parent, + struct disasm_line *dl) +{ + struct annotated_basic_block *bb; + struct basic_block_link *link; + + if (dl == NULL) + return -1; + + if (!is_new_basic_block(bb_data, dl)) + return 0; + + bb = zalloc(sizeof(*bb)); + if (bb == NULL) + return -1; + + bb->begin = dl; + bb->end = dl; + INIT_LIST_HEAD(&bb->list); + + link = malloc(sizeof(*link)); + if (link == NULL) { + free(bb); + return -1; + } + + link->bb = bb; + link->parent = parent; + list_add_tail(&link->node, &bb_data->queue); + return 0; +} + +/* Returns true when it finds the target in the current basic block */ +static bool process_basic_block(struct basic_block_data *bb_data, + struct basic_block_link *link, + struct symbol *sym, u64 target) +{ + struct disasm_line *dl, *next_dl, *last_dl; + struct annotation *notes = symbol__annotation(sym); + bool found = false; + + dl = link->bb->begin; + /* Check if it's already visited */ + if (basic_block_has_offset(&bb_data->visited, dl->al.offset)) + return false; + + last_dl = list_last_entry(¬es->src->source, + struct disasm_line, al.node); + + list_for_each_entry_from(dl, ¬es->src->source, al.node) { + /* Found the target instruction */ + if (sym->start + dl->al.offset == target) { + found = true; + break; + } + /* End of the function, finish the block */ + if (dl == last_dl) + break; + /* 'return' instruction finishes the block */ + if (dl->ins.ops == &ret_ops) + break; + /* normal instructions are part of the basic block */ + if (dl->ins.ops != &jump_ops) + continue; + /* jump to a different function, tail call or return */ + if (dl->ops.target.outside) + break; + /* jump instruction creates new basic block(s) */ + next_dl = find_disasm_line(sym, sym->start + dl->ops.target.offset, + /*allow_update=*/false); + add_basic_block(bb_data, link, next_dl); + + /* + * FIXME: determine conditional jumps properly. + * Conditional jumps create another basic block with the + * next disasm line. + */ + if (!strstr(dl->ins.name, "jmp")) { + next_dl = list_next_entry(dl, al.node); + add_basic_block(bb_data, link, next_dl); + } + break; + + } + link->bb->end = dl; + return found; +} + +/* + * It founds a target basic block, build a proper linked list of basic blocks + * by following the link recursively. + */ +static void link_found_basic_blocks(struct basic_block_link *link, + struct list_head *head) +{ + while (link) { + struct basic_block_link *parent = link->parent; + + list_move(&link->bb->list, head); + list_del(&link->node); + free(link); + + link = parent; + } +} + +static void delete_basic_blocks(struct basic_block_data *bb_data) +{ + struct basic_block_link *link, *tmp; + + list_for_each_entry_safe(link, tmp, &bb_data->queue, node) { + list_del(&link->node); + free(link->bb); + free(link); + } + + list_for_each_entry_safe(link, tmp, &bb_data->visited, node) { + list_del(&link->node); + free(link->bb); + free(link); + } +} + +/** + * annotate_get_basic_blocks - Get basic blocks for given address range + * @sym: symbol to annotate + * @src: source address + * @dst: destination address + * @head: list head to save basic blocks + * + * This function traverses disasm_lines from @src to @dst and save them in a + * list of annotated_basic_block to @head. It uses BFS to find the shortest + * path between two. The basic_block_link is to maintain parent links so + * that it can build a list of blocks from the start. + */ +int annotate_get_basic_blocks(struct symbol *sym, s64 src, s64 dst, + struct list_head *head) +{ + struct basic_block_data bb_data = { + .queue = LIST_HEAD_INIT(bb_data.queue), + .visited = LIST_HEAD_INIT(bb_data.visited), + }; + struct basic_block_link *link; + struct disasm_line *dl; + int ret = -1; + + dl = find_disasm_line(sym, src, /*allow_update=*/false); + if (dl == NULL) + return -1; + + if (add_basic_block(&bb_data, /*parent=*/NULL, dl) < 0) + return -1; + + /* Find shortest path from src to dst using BFS */ + while (!list_empty(&bb_data.queue)) { + link = list_first_entry(&bb_data.queue, struct basic_block_link, node); + + if (process_basic_block(&bb_data, link, sym, dst)) { + link_found_basic_blocks(link, head); + ret = 0; + break; + } + list_move(&link->node, &bb_data.visited); + } + delete_basic_blocks(&bb_data); + return ret; +} diff --git a/tools/perf/util/annotate.h b/tools/perf/util/annotate.h index 94435607c958..83afbe294ab7 100644 --- a/tools/perf/util/annotate.h +++ b/tools/perf/util/annotate.h @@ -495,4 +495,20 @@ extern struct list_head ann_insn_stat; u64 annotate_calc_pcrel(struct map_symbol *ms, u64 ip, int offset, struct disasm_line *dl); +/** + * struct annotated_basic_block - Basic block of instructions + * @list: List node + * @begin: start instruction in the block + * @end: end instruction in the block + */ +struct annotated_basic_block { + struct list_head list; + struct disasm_line *begin; + struct disasm_line *end; +}; + +/* Get a list of basic blocks from src to dst addresses */ +int annotate_get_basic_blocks(struct symbol *sym, s64 src, s64 dst, + struct list_head *head); + #endif /* __PERF_ANNOTATE_H */ From patchwork Fri Feb 2 22:04:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 196053 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp727598dyc; Fri, 2 Feb 2024 14:16:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IEo/xo14fIXEL/b2mcp0YJjoMCsvREgOiitTwSj6Gpz/mRFNGfYdjtZ5ErK+p0AwefiuAll X-Received: by 2002:a05:6a00:1242:b0:6dd:816a:94db with SMTP id u2-20020a056a00124200b006dd816a94dbmr2879654pfi.27.1706912167748; Fri, 02 Feb 2024 14:16:07 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706912167; cv=pass; d=google.com; s=arc-20160816; b=KJG6pltnjFE7mOMoiib1Wi0AJDXdiu1exj5X6Ca91s7ZFWR7SpGaWJ943w7n+kI2LR sK9297ZCjn1bBd9G1M+YItEYtCCnNg2aeDq4mLWG87PCyFZwTlttZxJLK9cX44xjzyx2 xA3Vej27nfcMl4wQrwqg3qamDMiqfyGrm7QCWwJEdWw3tTnzrcQpi60sX2IrcVMGmi1h mygSshYFQTyNIONb5O3tCgXPsYCuvOjvBpTnWPTZhHHqWgMS4vkutG3G7CY6GV65kHig NsGPVeXI1d6YfsXeMzDC/kdbRA8kDnZjvSRh0IvHc04AFsGmtuYr7AK/pG0c+sQvSD2M jSDw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=MfosSC37FZlf3wksf6X2qCpnjniDUxp28zgkBkHjCRE=; fh=bMWCaGYYNYrPTtOFYRrMgVz/cc5ojfJ4np/ZxJrWFHg=; b=t8geA3GQg1o9muBOVTT10OAxdhM35Npa5QPV674v9TcNrRUGpb8VzQKizwZgyw3VvR c9ByeTCgq9Fqz9rk08Ofuo5siwkCR2y6licfOsCV1NiyQrsiOOFXCLG66iQdnxiY+c0+ vpZ81+T+4XmyI6tKXcP06cw9BoUQbhng2XeG/uvOzAmp9jnZ3eDMfjQjNSJOxdYmBSKM jH0CEgCk0QPGt4fmoOOKmeOXLFetnl+ZSazP69GdmYjjHbzIDCowmB4ognVUC7hgzsMy x0lDa1dcfIrqUZpSqhy9DokX1gfHI4vCVB8bs9BbSALeIW5VpMKkMMfodYNJCHHW6dVY q1wA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZWXDaAq3; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50639-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50639-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCVDjxwUPSIXeuBDQ1WcMJ1sh1YLDXCusTmM6f6pCwyEig73aGdvdhwTOiwHJP9VG1xu/B8JCGSA2GgXmavJW89Au48tWQ== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id i64-20020a62c143000000b006d9a571a59fsi2148609pfg.145.2024.02.02.14.16.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 14:16:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50639-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZWXDaAq3; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50639-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50639-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 94F7FB21687 for ; Fri, 2 Feb 2024 22:07:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 12B4A132462; Fri, 2 Feb 2024 22:05:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ZWXDaAq3" 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 E898812C7FF; Fri, 2 Feb 2024 22:05:05 +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=1706911506; cv=none; b=E8iJ/pWTI6ta3LsYRFuI2RBIAG4NBDVpLA5Zq54qgQf15HS3qIVboqhoPnAa+n2u43v1TPhpnYxHQxUsCBM8uqwEUt6IzEIx+RXww8mvdtYVDg1h1v+hmsdCxYlsX7yZTTTOZuyVMAHxPGVQ6VIM5CHkxoCm9Ltf0kKPqgqER0M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706911506; c=relaxed/simple; bh=ce3rdgKItQ/zl8//pGAPQd7MlZR2haA80pcRv2ilW2Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gfErMxpGyV6ockScYDHtXwXz4ySKmHUkwz/xHf2cBRsnE1O5pH0FZ3ykfDIw3eK8X05Yr1DvBomlaaCPIBz5KYRuQ8zLRqo0fvtsc3T328YlGbdP6jblPMBK8BcIbjXn1ehab7Vxk+fUcOA3+JkaGsFf5k8elPLrRq8H7o3Pepk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZWXDaAq3; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id F3359C433B1; Fri, 2 Feb 2024 22:05:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706911505; bh=ce3rdgKItQ/zl8//pGAPQd7MlZR2haA80pcRv2ilW2Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZWXDaAq3lZqppAgyOh4Y3pStZ3mdYGfpPsEJJjGxql3nJoTBIbl/IDPxrJaS3+MiX spU1RNeDoN8cW/1sXhm8z5LC2IKDRLhoQsiEfcSfp4/WbbFZ5GB5gtlOlaPcDAY98c Vu/CpQfBVFwKLTIZB6UbKBuMTGRKcq82aNXgKjo7szSf88VCZPwngwTssaYwH/vqha MIDF3ck5Y/loZG1exTwtuw7vr7EcB6sMp/8PnY/x/HN0C2rfo3XKoDSE9to65wHs9w OtkOdm9gwyyHhL4evvNcCu+A8elPsKDd+7HsWNJM7I7KGVUtJBfVtviBIJIlfPrIay IPLnduKCT3R4g== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , 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 06/14] perf annotate-data: Maintain variable type info Date: Fri, 2 Feb 2024 14:04:51 -0800 Message-ID: <20240202220459.527138-7-namhyung@kernel.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240202220459.527138-1-namhyung@kernel.org> References: <20240202220459.527138-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789827133140718260 X-GMAIL-MSGID: 1789827133140718260 As it collected basic block and variable information in each scope, it now can build a state table to find matching variable at the location. The struct type_state is to keep the type info saved in each register and stack slot. The update_var_state() updates the table when it finds variables in the current address. It expects die_collect_vars() filled a list of variables with type info and starting address. Signed-off-by: Namhyung Kim --- tools/perf/util/annotate-data.c | 155 ++++++++++++++++++++++++++++++++ tools/perf/util/annotate-data.h | 29 ++++++ tools/perf/util/dwarf-aux.c | 4 + 3 files changed, 188 insertions(+) diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-data.c index b8e60c42af8c..f8768c224bcc 100644 --- a/tools/perf/util/annotate-data.c +++ b/tools/perf/util/annotate-data.c @@ -23,6 +23,57 @@ #include "symbol.h" #include "symbol_conf.h" +/* Type information in a register, valid when ok is true */ +struct type_state_reg { + Dwarf_Die type; + bool ok; + bool scratch; +}; + +/* Type information in a stack location, dynamically allocated */ +struct type_state_stack { + struct list_head list; + Dwarf_Die type; + int offset; + int size; + bool compound; +}; + +/* FIXME: This should be arch-dependent */ +#define TYPE_STATE_MAX_REGS 16 + +/* + * State table to maintain type info in each register and stack location. + * It'll be updated when new variable is allocated or type info is moved + * to a new location (register or stack). As it'd be used with the + * shortest path of basic blocks, it only maintains a single table. + */ +struct type_state { + struct type_state_reg regs[TYPE_STATE_MAX_REGS]; + struct list_head stack_vars; +}; + +static bool has_reg_type(struct type_state *state, int reg) +{ + return (unsigned)reg < ARRAY_SIZE(state->regs); +} + +void init_type_state(struct type_state *state, struct arch *arch __maybe_unused) +{ + memset(state, 0, sizeof(*state)); + INIT_LIST_HEAD(&state->stack_vars); +} + +void exit_type_state(struct type_state *state) +{ + struct type_state_stack *stack, *tmp; + + list_for_each_entry_safe(stack, tmp, &state->stack_vars, list) { + list_del(&stack->list); + free(stack); + } +} + /* * Compare type name and size to maintain them in a tree. * I'm not sure if DWARF would have information of a single type in many @@ -238,6 +289,110 @@ static int check_variable(Dwarf_Die *var_die, Dwarf_Die *type_die, int offset, return 0; } +static struct type_state_stack *find_stack_state(struct type_state *state, + int offset) +{ + struct type_state_stack *stack; + + list_for_each_entry(stack, &state->stack_vars, list) { + if (offset == stack->offset) + return stack; + + if (stack->compound && stack->offset < offset && + offset < stack->offset + stack->size) + return stack; + } + return NULL; +} + +static void set_stack_state(struct type_state_stack *stack, int offset, + Dwarf_Die *type_die) +{ + int tag; + Dwarf_Word size; + + if (dwarf_aggregate_size(type_die, &size) < 0) + size = 0; + + tag = dwarf_tag(type_die); + + stack->type = *type_die; + stack->size = size; + stack->offset = offset; + + switch (tag) { + case DW_TAG_structure_type: + case DW_TAG_union_type: + stack->compound = true; + break; + default: + stack->compound = false; + break; + } +} + +static struct type_state_stack *findnew_stack_state(struct type_state *state, + int offset, Dwarf_Die *type_die) +{ + struct type_state_stack *stack = find_stack_state(state, offset); + + if (stack) { + set_stack_state(stack, offset, type_die); + return stack; + } + + stack = malloc(sizeof(*stack)); + if (stack) { + set_stack_state(stack, offset, type_die); + list_add(&stack->list, &state->stack_vars); + } + return stack; +} + +/** + * update_var_state - Update type state using given variables + * @state: type state table + * @dloc: data location info + * @addr: instruction address to update + * @var_types: list of variables with type info + * + * This function fills the @state table using @var_types info. Each variable + * is used only at the given location and updates an entry in the table. + */ +void update_var_state(struct type_state *state, struct data_loc_info *dloc, + u64 addr, struct die_var_type *var_types) +{ + Dwarf_Die mem_die; + struct die_var_type *var; + int fbreg = dloc->fbreg; + int fb_offset = 0; + + if (dloc->fb_cfa) { + if (die_get_cfa(dloc->di->dbg, addr, &fbreg, &fb_offset) < 0) + fbreg = -1; + } + + for (var = var_types; var != NULL; var = var->next) { + if (var->addr != addr) + continue; + /* Get the type DIE using the offset */ + if (!dwarf_offdie(dloc->di->dbg, var->die_off, &mem_die)) + continue; + + if (var->reg == DWARF_REG_FB) { + findnew_stack_state(state, var->offset, &mem_die); + } else if (var->reg == fbreg) { + findnew_stack_state(state, var->offset - fb_offset, &mem_die); + } else if (has_reg_type(state, var->reg)) { + struct type_state_reg *reg; + + reg = &state->regs[var->reg]; + reg->type = mem_die; + reg->ok = true; + } + } +} + /* The result will be saved in @type_die */ static int find_data_type_die(struct data_loc_info *dloc, Dwarf_Die *type_die) { diff --git a/tools/perf/util/annotate-data.h b/tools/perf/util/annotate-data.h index ad6493ea2c8e..7fbb9eb2e96f 100644 --- a/tools/perf/util/annotate-data.h +++ b/tools/perf/util/annotate-data.h @@ -8,9 +8,12 @@ #include struct annotated_op_loc; +struct arch; struct debuginfo; +struct die_var_type; struct evsel; struct map_symbol; +struct type_state; /** * struct annotated_member - Type of member field @@ -146,6 +149,16 @@ int annotated_data_type__update_samples(struct annotated_data_type *adt, /* Release all data type information in the tree */ void annotated_data_type__tree_delete(struct rb_root *root); +/* Initialize type state table */ +void init_type_state(struct type_state *state, struct arch *arch); + +/* Destroy type state table */ +void exit_type_state(struct type_state *state); + +/* Update type state table using variables */ +void update_var_state(struct type_state *state, struct data_loc_info *dloc, + u64 addr, struct die_var_type *var_types); + #else /* HAVE_DWARF_SUPPORT */ static inline struct annotated_data_type * @@ -168,6 +181,22 @@ static inline void annotated_data_type__tree_delete(struct rb_root *root __maybe { } +static inline void init_type_state(struct type_state *state __maybe_unused, + struct arch *arch __maybe_unused) +{ +} + +static inline void exit_type_state(struct type_state *state __maybe_unused) +{ +} + +static inline void update_var_state(struct type_state *state __maybe_unused, + struct data_loc_info *dloc __maybe_unused, + u64 addr __maybe_unused, + struct die_var_type *var_types __maybe_unused) +{ +} + #endif /* HAVE_DWARF_SUPPORT */ #endif /* _PERF_ANNOTATE_DATA_H */ diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c index 39851ff1d5c4..f88a8fb4a350 100644 --- a/tools/perf/util/dwarf-aux.c +++ b/tools/perf/util/dwarf-aux.c @@ -9,6 +9,7 @@ #include #include "debug.h" #include "dwarf-aux.h" +#include "dwarf-regs.h" #include "strbuf.h" #include "string2.h" @@ -1147,6 +1148,8 @@ static int reg_from_dwarf_op(Dwarf_Op *op) case DW_OP_regx: case DW_OP_bregx: return op->number; + case DW_OP_fbreg: + return DWARF_REG_FB; default: break; } @@ -1160,6 +1163,7 @@ static int offset_from_dwarf_op(Dwarf_Op *op) case DW_OP_regx: return 0; case DW_OP_breg0 ... DW_OP_breg31: + case DW_OP_fbreg: return op->number; case DW_OP_bregx: return op->number2; From patchwork Fri Feb 2 22:04:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 196058 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp731219dyc; Fri, 2 Feb 2024 14:26:45 -0800 (PST) X-Google-Smtp-Source: AGHT+IGrCejXxl+qo3+XYvOoqXo3099STDT+0TwvICBBkINiFMNi317Q2iy5p6QvmcMLRSSqNrkR X-Received: by 2002:a05:651c:552:b0:2cf:425b:59ee with SMTP id q18-20020a05651c055200b002cf425b59eemr4821436ljp.24.1706912805296; Fri, 02 Feb 2024 14:26:45 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706912805; cv=pass; d=google.com; s=arc-20160816; b=0c2SwI3wk1gd8ZRKWlYZGe8cpi7y/a+6IH9THMVVkABewllnCxkRMlnKDLv3lun8e7 pZfXGsEb4OwXLOj7AOasOQOkQr0NKeefL7d5wTWtKPYBW/fgYvm5BiuuvhYq0kfciFqo dU0AH2ShFmq2OkmFqePtVK479Qb8/uR7y3kyw8Hur+0smOQsgFN7tovtZbNxl2R0y7e3 +WKfR6awwrYiZDbkSVsql9m0kPXqO8IR8b0x8TKwVYtieV6jqysA0X6U3QzS9Sd/u4su oPGv7xYYJgVMj7OdYbn4AmKpMmOEiN4hsjlcB2Cq2lhB4G3D184wJcIxkvDhVTfTguuo haRg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=fQn6Y62TcPjOwYmB0wiMuvXJU5KdWJjtSrY86HtkCtI=; fh=lh/H0A9Qhqb8aWM0P7JOLqlnQ5YfZMsORpUCHf/YfCI=; b=KZFtGRuak0HJ8KLMTKrkXzP+Zfl5QwlBrlkrcoGdmNUvnyakVQS5fTkg21uWlGdD0P SQXsr7mcAT6Rk0JM5tGSgSYnbjLAYdSl+jsBUQg6hJn+3CNVSpj8eXnA3IZ1YdLUdMBF hqR+GOgb36zCundS4Ie8mOJhW0l34cS63niKsZo2EWUo9VVU8VRbRxts8j90+mAPnPgJ efloXKtXcF55BeTUV88XpsRlhLuyzADHVsbBhaG8hdBydAZWZJG+QkNMlQGpRP//zPux sm5VYqBkpe3ldKqKLNXoOMnuhT66TyROktLmtQcUpaguu3dGsekq/LGITtBWlINWHu8E M0tA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=RGPChVcn; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50640-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50640-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCWrnbygLY+Ip1sjSbim7XiKGdf9Zus0e0eK5E5qA60q1IWzdV1FjDEECYvBtFxyH5/sujNMmoCFgxQPvvuAYXv7l6bJKQ== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id u9-20020a50c2c9000000b0055d095ffc36si1223756edf.156.2024.02.02.14.26.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 14:26:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50640-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=RGPChVcn; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50640-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50640-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 9C4071F2CBD9 for ; Fri, 2 Feb 2024 22:08:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A7012132490; Fri, 2 Feb 2024 22:05:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="RGPChVcn" 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 8F0D612C81D; Fri, 2 Feb 2024 22:05:06 +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=1706911506; cv=none; b=QkDJt9eIe0F5tVl07Qsru7O4XFSv0va84lhZuIZpAv5+Ov3eJyPjbb5TKs/AzGxI8AnLHeWKsqzsFUTDehcejmtyBTYDLcc0pHPLvCgq4HnAFko8TM9TktKZ2h4kwZ5eP5j5Csx0BKU9W1U0ILCREMB3UlwE8gSQU57ojcP0iz0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706911506; c=relaxed/simple; bh=qaVZKbMGcsmUmdHezxQM/TrUxZNd4m0wzUk80zpPxq4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=N7zB/BjbK1KaCcrNeFrccueFiioIWOgmHCooijsOKon6JNZ6TXZYUspYkFc8sd0ZryEFkUz+aSOL0BCYEx7kBPuHQuMv/dLpgf0kLkhYI7pBxV3tsEHP5dWbwo0kizOsoBfUB7OUwGYiUsZ7LSUlhD61Ph/NRUwn+PcUkmfiakQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=RGPChVcn; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 98CFAC43390; Fri, 2 Feb 2024 22:05:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706911506; bh=qaVZKbMGcsmUmdHezxQM/TrUxZNd4m0wzUk80zpPxq4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RGPChVcnHKceZKxHgO+de7hlxSsLN0XuERsLKJBxhVSEU9W0MgV+5vyel3Y45fvyD 6P15TAkhrap1j6islLnXZLw7r+SNLhZ9v61Gt3Pib0zgZHcOkd7xE9iLbJJRUPda9K ldHkNUbdyfHaOWkPfNdrG+rfCJ+SsFlsDmH6OHp8ziYS84PLDGrw2185s8cwyXfuWB QExsjGGCrbxXXg9/mGw7ZXY3kF6g6PEYr6R8h1F6mrFWkmSdHJ8MnxeUwsbCHNdiLX 6Y3x9DLweCWPDETLvJ93FDFFxhznU3RiMWOlThJxTB55c9tQ114QnSxfPdQ4zjrwmp kHvV2402zZLgQ== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , 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 07/14] perf annotate-data: Add update_insn_state() Date: Fri, 2 Feb 2024 14:04:52 -0800 Message-ID: <20240202220459.527138-8-namhyung@kernel.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240202220459.527138-1-namhyung@kernel.org> References: <20240202220459.527138-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789827801454916540 X-GMAIL-MSGID: 1789827801454916540 The update_insn_state() function is to update the type state table after processing each instruction. For now, it handles MOV (on x86) insn to transfer type info from the source location to the target. The location can be a register or a stack slot. Check carefully when memory reference happens and fetch the type correctly. It basically ignores write to a memory since it doesn't change the type info. One exception is writes to (new) stack slots for register spilling. Signed-off-by: Namhyung Kim --- tools/perf/util/annotate-data.c | 128 +++++++++++++++++++++++++++++++- tools/perf/util/annotate-data.h | 13 ++++ tools/perf/util/annotate.c | 1 + 3 files changed, 140 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-data.c index f8768c224bcc..b1e921663452 100644 --- a/tools/perf/util/annotate-data.c +++ b/tools/perf/util/annotate-data.c @@ -27,7 +27,6 @@ struct type_state_reg { Dwarf_Die type; bool ok; - bool scratch; }; /* Type information in a stack location, dynamically allocated */ @@ -383,7 +382,7 @@ void update_var_state(struct type_state *state, struct data_loc_info *dloc, findnew_stack_state(state, var->offset, &mem_die); } else if (var->reg == fbreg) { findnew_stack_state(state, var->offset - fb_offset, &mem_die); - } else if (has_reg_type(state, var->reg)) { + } else if (has_reg_type(state, var->reg) && var->offset == 0) { struct type_state_reg *reg; reg = &state->regs[var->reg]; @@ -393,6 +392,131 @@ void update_var_state(struct type_state *state, struct data_loc_info *dloc, } } +/** + * update_insn_state - Update type state for an instruction + * @state: type state table + * @dloc: data location info + * @dl: disasm line for the instruction + * + * This function updates the @state table for the target operand of the + * instruction at @dl if it transfers the type like MOV on x86. Since it + * tracks the type, it won't care about the values like in arithmetic + * instructions like ADD/SUB/MUL/DIV and INC/DEC. + * + * Note that ops->reg2 is only available when both mem_ref and multi_regs + * are true. + */ +void update_insn_state(struct type_state *state, struct data_loc_info *dloc, + struct disasm_line *dl) +{ + struct annotated_insn_loc loc; + struct annotated_op_loc *src = &loc.ops[INSN_OP_SOURCE]; + struct annotated_op_loc *dst = &loc.ops[INSN_OP_TARGET]; + Dwarf_Die type_die; + int fbreg = dloc->fbreg; + int fboff = 0; + + /* FIXME: remove x86 specific code and handle more instructions like LEA */ + if (!strstr(dl->ins.name, "mov")) + return; + + if (annotate_get_insn_location(dloc->arch, dl, &loc) < 0) + return; + + if (dloc->fb_cfa) { + u64 ip = dloc->ms->sym->start + dl->al.offset; + u64 pc = map__rip_2objdump(dloc->ms->map, ip); + + if (die_get_cfa(dloc->di->dbg, pc, &fbreg, &fboff) < 0) + fbreg = -1; + } + + /* Case 1. register to register transfers */ + if (!src->mem_ref && !dst->mem_ref) { + if (!has_reg_type(state, dst->reg1)) + return; + + if (has_reg_type(state, src->reg1)) + state->regs[dst->reg1] = state->regs[src->reg1]; + else + state->regs[dst->reg1].ok = false; + } + /* Case 2. memory to register transers */ + if (src->mem_ref && !dst->mem_ref) { + int sreg = src->reg1; + + if (!has_reg_type(state, dst->reg1)) + return; + +retry: + /* Check stack variables with offset */ + if (sreg == fbreg) { + struct type_state_stack *stack; + int offset = src->offset - fboff; + + stack = find_stack_state(state, offset); + if (stack && die_get_member_type(&stack->type, + offset - stack->offset, + &type_die)) { + state->regs[dst->reg1].type = type_die; + state->regs[dst->reg1].ok = true; + } else + state->regs[dst->reg1].ok = false; + } + /* And then dereference the pointer if it has one */ + else if (has_reg_type(state, sreg) && state->regs[sreg].ok && + die_deref_ptr_type(&state->regs[sreg].type, + src->offset, &type_die)) { + state->regs[dst->reg1].type = type_die; + state->regs[dst->reg1].ok = true; + } + /* Or try another register if any */ + else if (src->multi_regs && sreg == src->reg1 && + src->reg1 != src->reg2) { + sreg = src->reg2; + goto retry; + } + /* It failed to get a type info, mark it as invalid */ + else { + state->regs[dst->reg1].ok = false; + } + } + /* Case 3. register to memory transfers */ + if (!src->mem_ref && dst->mem_ref) { + if (!has_reg_type(state, src->reg1) || + !state->regs[src->reg1].ok) + return; + + /* Check stack variables with offset */ + if (dst->reg1 == fbreg) { + struct type_state_stack *stack; + int offset = dst->offset - fboff; + + stack = find_stack_state(state, offset); + if (stack) { + /* + * The source register is likely to hold a type + * of member if it's a compound type. Do not + * update the stack variable type since we can + * get the member type later by using the + * die_get_member_type(). + */ + if (!stack->compound) + set_stack_state(stack, offset, + &state->regs[src->reg1].type); + } else { + findnew_stack_state(state, offset, + &state->regs[src->reg1].type); + } + } + /* + * Ignore other transfers since it'd set a value in a struct + * and won't change the type. + */ + } + /* Case 4. memory to memory transfers (not handled for now) */ +} + /* The result will be saved in @type_die */ static int find_data_type_die(struct data_loc_info *dloc, Dwarf_Die *type_die) { diff --git a/tools/perf/util/annotate-data.h b/tools/perf/util/annotate-data.h index 7fbb9eb2e96f..ff9acf6ea808 100644 --- a/tools/perf/util/annotate-data.h +++ b/tools/perf/util/annotate-data.h @@ -11,6 +11,7 @@ struct annotated_op_loc; struct arch; struct debuginfo; struct die_var_type; +struct disasm_line; struct evsel; struct map_symbol; struct type_state; @@ -78,6 +79,7 @@ extern struct annotated_data_type stackop_type; /** * struct data_loc_info - Data location information + * @arch: architecture info * @ms: Map and Symbol info * @ip: Instruction address * @var_addr: Data address (for global variables) @@ -90,6 +92,7 @@ extern struct annotated_data_type stackop_type; */ struct data_loc_info { /* These are input field, should be filled by caller */ + struct arch *arch; struct map_symbol *ms; u64 ip; u64 var_addr; @@ -159,6 +162,10 @@ void exit_type_state(struct type_state *state); void update_var_state(struct type_state *state, struct data_loc_info *dloc, u64 addr, struct die_var_type *var_types); +/* Update type state table for an instruction */ +void update_insn_state(struct type_state *state, struct data_loc_info *dloc, + struct disasm_line *dl); + #else /* HAVE_DWARF_SUPPORT */ static inline struct annotated_data_type * @@ -197,6 +204,12 @@ static inline void update_var_state(struct type_state *state __maybe_unused, { } +static inline void update_insn_state(struct type_state *state __maybe_unused, + struct data_loc_info *dloc __maybe_unused, + struct disasm_line *dl __maybe_unused) +{ +} + #endif /* HAVE_DWARF_SUPPORT */ #endif /* _PERF_ANNOTATE_DATA_H */ diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index 4ef14b3f49e4..44574056d4bd 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -3844,6 +3844,7 @@ struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he) for_each_insn_op_loc(&loc, i, op_loc) { struct data_loc_info dloc = { + .arch = arch, .ms = ms, /* Recalculate IP for LOCK prefix or insn fusion */ .ip = ms->sym->start + dl->al.offset, From patchwork Fri Feb 2 22:04:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 196055 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp727887dyc; Fri, 2 Feb 2024 14:16:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IHFhgcOo9GVXve/VILSrGYyaKDWT79PvSOalpmVBHRNP4ZRm85lLQ8NtWMre0RmgWzwUOLJ X-Received: by 2002:a05:6a21:3997:b0:19c:95cc:1d47 with SMTP id ad23-20020a056a21399700b0019c95cc1d47mr5035638pzc.6.1706912214390; Fri, 02 Feb 2024 14:16:54 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706912214; cv=pass; d=google.com; s=arc-20160816; b=0WFlnU6KKzRokk4XJp1RxFEQ389zD/Ntw0PmXY3Nvbf73wPLub2pDP+RSPJqh1jl6b zfslLIZqySbPr7bn9MIJCkZC3TcAT4df/RIr8QVRXNFjImJRhnC+LsgX4xDDttdZjeqO 4fK6mPVcI+KmgrSBKOL4R9Kx+pVv95nikxNa0wzXHsdcc++ShFVvmCaYjWWqnR0Yiz25 4QsOuayAPRTo+MTTvyGiZOmT+c9EnuMoBW9QjDVBQhYkj51I/AQJkETo1OCJ3bTYqmaN b9QPXYVS4zxDPmf9Ow6yKPX5ANvLdYXhEAsiuZVi4serOPe/Sp7gLmP/cy1QIjygrA3l 5UTw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=basjLO89QdKM74sccuYjhfHh6qn+PoaujJBcfTdOc5A=; fh=uMearFNeefUYc4rSyhauxnhoN8kBRumBb3iK4mH5a+4=; b=RkvATQ/DWYrSwExexoQxir2pch+AzCtELnqvMLMQTy8THV9TeHf1n8lqiXJrcvcIcI fLE1xGq/bCCENXoJdFR906o0Ir/jdJA/vcVCJJSM2vngPIDbTJqRAm/gY63TMZQDu3bZ dTPYqRJDrUbr4gWL8vzsuv8dG1Yhe0BnJlQlvLOonsr7WpXNHU4eglxlGAUIhMKFqdyE RzqeUMqL2SbLRXGYYOPqV/dXY787XJCw5N+OSN/xUDuwnHqXHNB9tMD6eODzmAuOKig5 DEEbIR5uLMVuBYWnb7wSZ5F2FYV4TJXZpRLTx81pxF+MchpErIn/Ltmn8FlMogAwbE/C cDGw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=r1RiXsPu; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50641-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50641-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCW2KyDnWRv4Oc7Dy6SXpNLQLbgNXQFqi64y6YlHIi1BGXDnxpQ3NJYXXCxAmxBwcnwA0bFGaMPD3+H5l/L7SepHWntssQ== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id d4-20020a631d44000000b005d55aec96b2si2261902pgm.41.2024.02.02.14.16.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 14:16:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50641-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=r1RiXsPu; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50641-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50641-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 66A9CB24D05 for ; Fri, 2 Feb 2024 22:08:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 861AB1332A2; Fri, 2 Feb 2024 22:05:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="r1RiXsPu" 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 31E6812D762; Fri, 2 Feb 2024 22:05:06 +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=1706911507; cv=none; b=fedeBVj76bAiK1B0osXiK2Qd0uoqyreaYXxqn9z3sugCmMARXqtV/tCbC7Vx71aC7++RQ4TQMwYx5WutCGncdaP8Dv/6hM4r/Cdh7kSx/uqHKkTZwfvu7MzdZ5/g2DZnsnD14OBu0cTR+FvspC5JZ9KPuwEy33i6jJ+7UGdk5nw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706911507; c=relaxed/simple; bh=Lf0brn5nL1UgsdGJBLme5o8pREvbDQOaQ2MZkihr1mQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=e+tqmWHyZKhr3mw9Y8b83eSBTkyhpvXLP2N9oIa3Uejtzi2PggBxDXtaXTFcIaqWcBAhAeYDRsOpJdxiA31ark2Cq0mo37dA5m+vU0ycVwOQtwa1siLcgu/knqdFvPaqdYj9Q5ytjITuscuRseLlmSBb2CCfI09GFZk7Bo22DXk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=r1RiXsPu; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3EE7FC433C7; Fri, 2 Feb 2024 22:05:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706911506; bh=Lf0brn5nL1UgsdGJBLme5o8pREvbDQOaQ2MZkihr1mQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=r1RiXsPuuGOoR3yz+7ZpliRqSyNDQtW+64gTe3HFYJaQR1gZgbPl8pxql78E5PPH/ Nc7lFEC9Qo0FVChl2GlUuMFnw7HVOHAeQwikgL5CwkNeVDuh2l5nx7B4pji7iilBV9 OXXWF2Xxhw/YCkyst9qTgaFGmSWznwhg4fQlsK+me/C+CuJzw9jM+LAwIynaZGFyRF EHw43ObyJHdtnp/3owV56s1hHymc48hra5Phl+vXV4Bvn5qliJQ0DNNwdwwsqffNVQ Ww8kJm/BJenZaaAoWSSiIZTL8IQvkwjMLLwqeCgqGSXkQW4gI2JQ2EEw6ifReCl38M /VWgzctnw3PEQ== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , 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 08/14] perf annotate-data: Handle global variable access Date: Fri, 2 Feb 2024 14:04:53 -0800 Message-ID: <20240202220459.527138-9-namhyung@kernel.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240202220459.527138-1-namhyung@kernel.org> References: <20240202220459.527138-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789827182167571221 X-GMAIL-MSGID: 1789827182167571221 When updating the instruction states, it also needs to handle global variable accesses. Same as it does for PC-relative addressing, it can look up the type by address (if it's defined in the same file), or by name after finding the symbol by address (for declarations). Signed-off-by: Namhyung Kim --- tools/perf/util/annotate-data.c | 45 ++++++++++++++++++++++++++++++--- tools/perf/util/annotate-data.h | 10 ++++++-- tools/perf/util/annotate.c | 45 ++++++++++++++++++++------------- tools/perf/util/annotate.h | 5 ++++ 4 files changed, 83 insertions(+), 22 deletions(-) diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-data.c index b1e921663452..e46e162c783f 100644 --- a/tools/perf/util/annotate-data.c +++ b/tools/perf/util/annotate-data.c @@ -396,6 +396,7 @@ void update_var_state(struct type_state *state, struct data_loc_info *dloc, * update_insn_state - Update type state for an instruction * @state: type state table * @dloc: data location info + * @cu_die: compile unit debug entry * @dl: disasm line for the instruction * * This function updates the @state table for the target operand of the @@ -407,7 +408,7 @@ void update_var_state(struct type_state *state, struct data_loc_info *dloc, * are true. */ void update_insn_state(struct type_state *state, struct data_loc_info *dloc, - struct disasm_line *dl) + void *cu_die, struct disasm_line *dl) { struct annotated_insn_loc loc; struct annotated_op_loc *src = &loc.ops[INSN_OP_SOURCE]; @@ -449,8 +450,46 @@ void update_insn_state(struct type_state *state, struct data_loc_info *dloc, return; retry: - /* Check stack variables with offset */ - if (sreg == fbreg) { + /* Check if it's a global variable */ + if (sreg == DWARF_REG_PC) { + Dwarf_Die var_die; + struct map_symbol *ms = dloc->ms; + int offset = src->offset; + u64 ip = ms->sym->start + dl->al.offset; + u64 pc, addr; + const char *var_name = NULL; + + addr = annotate_calc_pcrel(ms, ip, offset, dl); + pc = map__rip_2objdump(ms->map, ip); + + if (die_find_variable_by_addr(cu_die, pc, addr, + &var_die, &offset) && + check_variable(&var_die, &type_die, offset, + /*is_pointer=*/false) == 0 && + die_get_member_type(&type_die, offset, &type_die)) { + state->regs[dst->reg1].type = type_die; + state->regs[dst->reg1].ok = true; + return; + } + + /* Try to get the name of global variable */ + offset = src->offset; + get_global_var_info(dloc->thread, ms, ip, dl, + dloc->cpumode, &addr, + &var_name, &offset); + + if (var_name && die_find_variable_at(cu_die, var_name, + pc, &var_die) && + check_variable(&var_die, &type_die, offset, + /*is_pointer=*/false) == 0 && + die_get_member_type(&type_die, offset, &type_die)) { + state->regs[dst->reg1].type = type_die; + state->regs[dst->reg1].ok = true; + } else + state->regs[dst->reg1].ok = false; + } + /* And check stack variables with offset */ + else if (sreg == fbreg) { struct type_state_stack *stack; int offset = src->offset - fboff; diff --git a/tools/perf/util/annotate-data.h b/tools/perf/util/annotate-data.h index ff9acf6ea808..0bfef29fa52c 100644 --- a/tools/perf/util/annotate-data.h +++ b/tools/perf/util/annotate-data.h @@ -14,6 +14,7 @@ struct die_var_type; struct disasm_line; struct evsel; struct map_symbol; +struct thread; struct type_state; /** @@ -79,11 +80,13 @@ extern struct annotated_data_type stackop_type; /** * struct data_loc_info - Data location information - * @arch: architecture info + * @arch: CPU architecture info + * @thread: Thread info * @ms: Map and Symbol info * @ip: Instruction address * @var_addr: Data address (for global variables) * @var_name: Variable name (for global variables) + * @cpumode: CPU execution mode * @op: Instruction operand location (regs and offset) * @di: Debug info * @fbreg: Frame base register @@ -94,8 +97,10 @@ struct data_loc_info { /* These are input field, should be filled by caller */ struct arch *arch; struct map_symbol *ms; + struct thread *thread; u64 ip; u64 var_addr; + u8 cpumode; const char *var_name; struct annotated_op_loc *op; @@ -164,7 +169,7 @@ void update_var_state(struct type_state *state, struct data_loc_info *dloc, /* Update type state table for an instruction */ void update_insn_state(struct type_state *state, struct data_loc_info *dloc, - struct disasm_line *dl); + void *cu_die, struct disasm_line *dl); #else /* HAVE_DWARF_SUPPORT */ @@ -206,6 +211,7 @@ static inline void update_var_state(struct type_state *state __maybe_unused, static inline void update_insn_state(struct type_state *state __maybe_unused, struct data_loc_info *dloc __maybe_unused, + void *cu_die __maybe_unused, struct disasm_line *dl __maybe_unused) { } diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index 44574056d4bd..89a8d57b1bf7 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -3771,6 +3771,28 @@ u64 annotate_calc_pcrel(struct map_symbol *ms, u64 ip, int offset, return map__rip_2objdump(ms->map, addr); } +void get_global_var_info(struct thread *thread, struct map_symbol *ms, u64 ip, + struct disasm_line *dl, u8 cpumode, u64 *var_addr, + const char **var_name, int *poffset) +{ + struct addr_location al; + struct symbol *var; + u64 map_addr; + + *var_addr = annotate_calc_pcrel(ms, ip, *poffset, dl); + /* Kernel symbols might be relocated */ + map_addr = *var_addr + map__reloc(ms->map); + + addr_location__init(&al); + var = thread__find_symbol_fb(thread, cpumode, map_addr, &al); + if (var) { + *var_name = var->name; + /* Calculate type offset from the start of variable */ + *poffset = map_addr - map__unmap_ip(al.map, var->start); + } + addr_location__exit(&al); +} + /** * hist_entry__get_data_type - find data type for given hist entry * @he: hist entry @@ -3845,6 +3867,8 @@ struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he) for_each_insn_op_loc(&loc, i, op_loc) { struct data_loc_info dloc = { .arch = arch, + .thread = he->thread, + .cpumode = he->cpumode, .ms = ms, /* Recalculate IP for LOCK prefix or insn fusion */ .ip = ms->sym->start + dl->al.offset, @@ -3859,23 +3883,10 @@ struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he) /* PC-relative addressing */ if (op_loc->reg1 == DWARF_REG_PC) { - struct addr_location al; - struct symbol *var; - u64 map_addr; - - dloc.var_addr = annotate_calc_pcrel(ms, ip, op_loc->offset, dl); - /* Kernel symbols might be relocated */ - map_addr = dloc.var_addr + map__reloc(ms->map); - - addr_location__init(&al); - var = thread__find_symbol_fb(he->thread, he->cpumode, - map_addr, &al); - if (var) { - dloc.var_name = var->name; - /* Calculate type offset from the start of variable */ - dloc.type_offset = map_addr - map__unmap_ip(al.map, var->start); - } - addr_location__exit(&al); + dloc.type_offset = op_loc->offset; + get_global_var_info(he->thread, ms, ip, dl, he->cpumode, + &dloc.var_addr, &dloc.var_name, + &dloc.type_offset); } mem_type = find_data_type(&dloc); diff --git a/tools/perf/util/annotate.h b/tools/perf/util/annotate.h index 83afbe294ab7..b460785111a1 100644 --- a/tools/perf/util/annotate.h +++ b/tools/perf/util/annotate.h @@ -23,6 +23,7 @@ struct option; struct perf_sample; struct evsel; struct symbol; +struct thread; struct annotated_data_type; struct ins { @@ -495,6 +496,10 @@ extern struct list_head ann_insn_stat; u64 annotate_calc_pcrel(struct map_symbol *ms, u64 ip, int offset, struct disasm_line *dl); +void get_global_var_info(struct thread *thread, struct map_symbol *ms, u64 ip, + struct disasm_line *dl, u8 cpumode, u64 *var_addr, + const char **var_name, int *poffset); + /** * struct annotated_basic_block - Basic block of instructions * @list: List node From patchwork Fri Feb 2 22:04:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 196054 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp727654dyc; Fri, 2 Feb 2024 14:16:16 -0800 (PST) X-Google-Smtp-Source: AGHT+IH6K2AZAjBRyHANwAoPcpo/JjdYS45hHM6af3ncPq//WdLg/0NQ9iOtA9gFx924egR2v0Qu X-Received: by 2002:a17:906:e0b:b0:a36:73c0:d204 with SMTP id l11-20020a1709060e0b00b00a3673c0d204mr4888850eji.50.1706912176258; Fri, 02 Feb 2024 14:16:16 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706912176; cv=pass; d=google.com; s=arc-20160816; b=fwV+gvf0KsxGQwywSjcqS7PjSH3I+P7Hdn8si6Te4EROadh1thBekaFw1XIpOH6xsB lIYYEUDS/AMrSme7lIu41ZvbNiz9E4R3bENrcq3XYbmpMcEgeZcD7qXLHfMK8JIttWtg Ax4bvau6/6O8E6v7qNQafVdKbm0eLhWqyQeO70jsuHe2eg9Op4z6yi/GwRSaItAG5tU6 zn1NqteRE8ZCOE/AGa5CiKaaOmiLLKwjypQSkWrjSH3vKlIRhGiMDlhvtTUnwHT3jhjS cccctJaScZch9INBRtFy/nupW+037d1NI+qRM8aHdvslRwvfyWDGRey2O8UMILZ9ZgSN CIjA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=11jcm6oQL5Z0juBpgR3UXc9VrjA67UrtsA6JSfTA0Kk=; fh=tf24LPXpotcSk3Ai7h1H/GSae0KpiLOmFJNrS+ouSCk=; b=rDvnGDEKWLs6euEHJpx2YgpMl7mwBNFc32H4QRwdCdoWrb5e4VhwefRpbhVisVtkE6 emecA440YCVPR0a+fIFwmHa/C0EjXoCAVfAF9gM5xeoMaoBWGzTLvbdWtaDx5K6KIb/8 8B0sUCL9mKkIQUWHGl0x0IOQR3mQSXon+qlKyC6DWCOOhAwvo2XKpwXqGsSKDMewK7YQ ZneTXPRNzhv5Zpxd9+eqiJRchKaNU8m5gjN627xYYQ3oUOO32ufIKPD9Dd/FbXFqEyUp GA84041Vi8oOB7LVnBnfv87DHR0kAlfqQARHB2iEetvHsg2E0udi3pxJSYhrjdOVSX3/ 3C/A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ctB46xXC; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50642-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50642-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCXTbGjjY0VM4wJe7NRdWexzuHdEMVjgO8OUFQWMJalsvzRw9k54OzLY7XUTBEyYTFsVBAcwEfSluIanDhEpdM/yeKn1hQ== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id g24-20020a170906395800b00a371f4bccc0si637813eje.987.2024.02.02.14.16.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 14:16:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50642-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ctB46xXC; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50642-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50642-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id AEA3A1F2C7E1 for ; Fri, 2 Feb 2024 22:08:11 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 901C9132488; Fri, 2 Feb 2024 22:05:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ctB46xXC" 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 7BDE612D77A; Fri, 2 Feb 2024 22:05:07 +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=1706911507; cv=none; b=NMarRYHyffVKE303AwPO1GUZpuMJvQKUTLQVOd2S5OsGaBycp5MiWJqzUIlI4KAa4pBV788pT49+UOnd12W2//PJ8qryh7Q0IlBLjk6hgJIzt2q/3pxiCbycTSuMnf34Xa1p6osnA04j2f3Ox3MQrtZby5AH+i1Xa3BlLh0EvDA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706911507; c=relaxed/simple; bh=NAmRPC2/kvPkVI8QeH6SxAYcD47o8R8XglhwKCAOVSk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mfJad02ctC6FCe3wNnoziZiSvOEgzEzkmtlkObQsYY/EZsZBc7TEK14O/KY/floFqhsGODwjRaXDSRPubL9qFEjTKcaOwFJxyC7OmMICM22uFv/3GTvcqcYL7F72aRGjAwF1VkzhrduzZoCDoT85qE1SUpmXBFx+dhNe02mU/wY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ctB46xXC; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id D57F6C43601; Fri, 2 Feb 2024 22:05:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706911507; bh=NAmRPC2/kvPkVI8QeH6SxAYcD47o8R8XglhwKCAOVSk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ctB46xXCILUNSO1/4P2k6tYcFVQYzgq6eKHGIQ/gdMQO1ffdqS8qnSbanL/MIjlhz OappRGsP+fC2leT/uHq4aZh7OskRgkBYVw9Ikdr6hDWNKS1RBfzEbkhqQUu3LDkO9J GmwgfVcZaI2PVWCVfNX3p7kloJdlVRNoE1UqOVrgmpj+Ea7mRV31f9afmKTpC04zMj S+tGu2ym9m6hRAePgpf/++sd5bryC4mj/BODG1QbNJvjfSFkqTKyFu/0YmK2GJxkro G7bWh1y9/dCCvcela1/ZCkpthlIA5lIls7nQ7t+ajZoOk2uIEvxbG36nno2u5vF6If A19NkJPynavoA== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , 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 09/14] perf annotate-data: Handle call instructions Date: Fri, 2 Feb 2024 14:04:54 -0800 Message-ID: <20240202220459.527138-10-namhyung@kernel.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240202220459.527138-1-namhyung@kernel.org> References: <20240202220459.527138-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789827142038595732 X-GMAIL-MSGID: 1789827142038595732 When updating instruction states, the call instruction should play a role since it can change the register states. For simplicity, mark some registers as scratch registers (should be arch-dependent), and invalidate them all after a function call. If the function returns something, the designated register (ret_reg) will have the type info. Signed-off-by: Namhyung Kim --- tools/perf/util/annotate-data.c | 45 +++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-data.c index e46e162c783f..185cb896b9d6 100644 --- a/tools/perf/util/annotate-data.c +++ b/tools/perf/util/annotate-data.c @@ -23,10 +23,14 @@ #include "symbol.h" #include "symbol_conf.h" -/* Type information in a register, valid when ok is true */ +/* + * Type information in a register, valid when @ok is true. + * The @scratch registers are invalidated after a function call. + */ struct type_state_reg { Dwarf_Die type; bool ok; + bool scratch; }; /* Type information in a stack location, dynamically allocated */ @@ -50,6 +54,7 @@ struct type_state_stack { struct type_state { struct type_state_reg regs[TYPE_STATE_MAX_REGS]; struct list_head stack_vars; + int ret_reg; }; static bool has_reg_type(struct type_state *state, int reg) @@ -57,10 +62,23 @@ static bool has_reg_type(struct type_state *state, int reg) return (unsigned)reg < ARRAY_SIZE(state->regs); } -void init_type_state(struct type_state *state, struct arch *arch __maybe_unused) +void init_type_state(struct type_state *state, struct arch *arch) { memset(state, 0, sizeof(*state)); INIT_LIST_HEAD(&state->stack_vars); + + if (arch__is(arch, "x86")) { + state->regs[0].scratch = true; + state->regs[1].scratch = true; + state->regs[2].scratch = true; + state->regs[4].scratch = true; + state->regs[5].scratch = true; + state->regs[8].scratch = true; + state->regs[9].scratch = true; + state->regs[10].scratch = true; + state->regs[11].scratch = true; + state->ret_reg = 0; + } } void exit_type_state(struct type_state *state) @@ -417,6 +435,29 @@ void update_insn_state(struct type_state *state, struct data_loc_info *dloc, int fbreg = dloc->fbreg; int fboff = 0; + if (ins__is_call(&dl->ins)) { + Dwarf_Die func_die; + + /* __fentry__ will preserve all registers */ + if (dl->ops.target.sym && + !strcmp(dl->ops.target.sym->name, "__fentry__")) + return; + + /* Otherwise invalidate scratch registers after call */ + for (unsigned i = 0; i < ARRAY_SIZE(state->regs); i++) { + if (state->regs[i].scratch) + state->regs[i].ok = false; + } + + /* Update register with the return type (if any) */ + if (die_find_realfunc(cu_die, dl->ops.target.addr, &func_die) && + die_get_real_type(&func_die, &type_die)) { + state->regs[state->ret_reg].type = type_die; + state->regs[state->ret_reg].ok = true; + } + return; + } + /* FIXME: remove x86 specific code and handle more instructions like LEA */ if (!strstr(dl->ins.name, "mov")) return; From patchwork Fri Feb 2 22:04:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 196059 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp731253dyc; Fri, 2 Feb 2024 14:26:50 -0800 (PST) X-Google-Smtp-Source: AGHT+IFDYEvsFWoKWiogjg3zcG5oOirkNZgalqnFJpkWSfVow/8yK+sWPRumcv4CIk1vS3maWaPn X-Received: by 2002:a17:906:328a:b0:a29:b31d:1dc6 with SMTP id 10-20020a170906328a00b00a29b31d1dc6mr1324ejw.6.1706912810090; Fri, 02 Feb 2024 14:26:50 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706912810; cv=pass; d=google.com; s=arc-20160816; b=U3dDXEuUb/NnjieAppVpbBmkGUN1NHtcbQkNYs+PnlfZQz+LxuCpD1IBUR20MzNg+7 FPX0yZw224hOAttp46Uh7yppR38exZSq9v4FYdjymZZJzvGdLGy6bJe+3j2hFBhWCKIJ 8QlId/CFWZIEWPa26qqN640yCduPxJZCsDAci9nxnreZtPb2EIqlXoNx0HABiSHDMyWo sI5LUwMsgsEvmj81S9/86mLWYvHArKTmC7c4qyHQWf2URct+f1k4mwOqL6WqF98WWWDN n3ZAYpGp4vZN8xRg7QcFXguWtDVJV9LYjaD/EFa3y9WIG/8ASokeXy6UI60pT+Thb+Jb vRMw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=77MEaMJCRtj59fZq9V9L5WeRNZAUrcSaLqAisKoC/uk=; fh=b/X0mLg2++pIwX0bXa6MDqm7uLPhTeIeML4bH58SNU0=; b=IEi0ZOJGKoTbbDZhcR013nDzIivX6GchlsZLyMgiVFWcDn0bONvWbUhqQ2ys+ycoA8 r5YXlbTSW7rAgLzXoeCj6GlnN5CB0IilbtDO5GSv0JKuO5+jILK4BGVVEwL8DhfcMJ/5 5oQhAz3obm43JHvy/Fedi0ruNM34uoR74r4a5ZmpVQwtrMNEICTuFqOCcZ/RQLVQJ3ye FglxNLaABt+x2ov5+6Za/u75QQOcMCctUqk5RKAbT3R2x5VAOTDsfB8hwWPlmsszz1+m qe1sJuLGsG+HiCknRASsVkGHwtmwtojmtKhTRa+dgNNVzbu6c2dkcKoQESkBoM0NiGEk c2gg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=bOHxhshC; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50643-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50643-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCXtSLyeolUIHS11urXTL44aMyNJhgDuKcBANj7EB1sdKYEdmtZkTQ18Qd8z+69TcLUVoGS0AHc33J4rf3Gtkhs4ZIEckA== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id t3-20020a1709060c4300b00a338574d65fsi1205777ejf.130.2024.02.02.14.26.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 14:26:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50643-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=bOHxhshC; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50643-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50643-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id EB7071F2D559 for ; Fri, 2 Feb 2024 22:09:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EF28D134CDB; Fri, 2 Feb 2024 22:05:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="bOHxhshC" 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 7159212F38D; Fri, 2 Feb 2024 22:05:08 +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=1706911508; cv=none; b=LGZGTDDpIZE+osCLW/6QefqCljzfMSo8tlzJshI26tw7QC/yNOq1PjjEj308Txqx09DszR520T3eYiUAg+Av28N7VjLyKPsLc8slZ6uKwVuVO3vp23CW+u6da+EsBJkSE4J447AyJGt2IYWMRgQIZi4Pu15iMBOrBXjMjPz6Psk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706911508; c=relaxed/simple; bh=67xqRUBRXBFJtWE4Lz1TSu+uUUm+uE7WF2VRpnTVXxw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RvGMUa+LWnXQELYQTWbt6hvqTUFD2I+cXOzhZNDNZLsb78Qqe6Uzkt8xRRmDV+0PXjQ7+zxNiG/shIqpUFJCGdSYQa+7lHVs2oI4M26ybEYz60ldATP2sfbYWq9hs8YKoo1vEKM17E9M5i0JVqxFjHOVaSko0lRz1ax7WWHHoKI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bOHxhshC; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7D208C4166D; Fri, 2 Feb 2024 22:05:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706911508; bh=67xqRUBRXBFJtWE4Lz1TSu+uUUm+uE7WF2VRpnTVXxw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bOHxhshCqNR72Ha45u7z1v66+ImszsRbJsmu8VAFk02RRcg4NJl3QTRqfuUHjrZQ3 PzHij26wPAOY3zxJlFh+n5v6crUPKkk6HjnEAkLHk4EM/q/UWYnslmmMtKezSfgt/C pEy0+WsuOZ4K9sPfVPbYDpkMqIQCH5FEYb1rgrjeWfzaSVqYF5Kw+HI3+2i4xiVC2s ENwmFWcQulRmsW+X7/jNyabCSuwhdLsHySXqfxL9eiUMQARKJiMcmnSI5zuYjV0wnX yaff8kk7MtkjIOs4L7INzwA5H3/VtPZXgdnPv89RPzxx+hneAwzxEtkodd3e6UhvX6 GDJp9ZjTM7F6g== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , 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 10/14] perf annotate-data: Implement instruction tracking Date: Fri, 2 Feb 2024 14:04:55 -0800 Message-ID: <20240202220459.527138-11-namhyung@kernel.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240202220459.527138-1-namhyung@kernel.org> References: <20240202220459.527138-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789827806921175292 X-GMAIL-MSGID: 1789827806921175292 If it failed to find a variable for the location directly, it might be due to a missing variable in the source code. For example, accessing pointer variables in a chain can result in the case like below: struct foo *foo = ...; int i = foo->bar->baz; The DWARF debug information is created for each variable so it'd have one for 'foo'. But there's no variable for 'foo->bar' and then it cannot know the type of 'bar' and 'baz'. The above source code can be compiled to the follow x86 instructions: mov 0x8(%rax), %rcx mov 0x4(%rcx), %rdx <=== PMU sample mov %rdx, -4(%rbp) Let's say 'foo' is located in the %rax and it has a pointer to struct foo. But perf sample is captured in the second instruction and there is no variable or type info for the %rcx. It'd be great if compiler could generate debug info for %rcx, but we should handle it on our side. So this patch implements the logic to iterate instructions and update the type table for each location. As it already collected a list of scopes including the target instruction, we can use it to construct the type table smartly. +---------------- scope[0] subprogram | | +-------------- scope[1] lexical_block | | | | +------------ scope[2] inlined_subroutine | | | | | | +---------- scope[3] inlined_subroutine | | | | | | | | +-------- scope[4] lexical_block | | | | | | | | | | *** target instruction ... Image the target instruction has 5 scopes, each scope will have its own variables and parameters. Then it can start with the innermost scope (4). So it'd search the shortest path from the start of scope[4] to the target address and build a list of basic blocks. Then it iterates the basic blocks with the variables in the scope and update the table. If it finds a type at the target instruction, then returns it. Otherwise, it moves to the upper scope[3]. Now it'd search the shortest path from the start of scope[3] to the start of scope[4]. Then connect it to the existing basic block list. Then it'd iterate the blocks with variables for both scopes. It can repeat this until it finds a type at the target instruction or reaches to the top scope[0]. As the basic blocks contain the shortest path, it won't worry about branches and can update the table simply. With this change, the stat now looks like below: Annotate data type stats: total 294, ok 185 (62.9%), bad 109 (37.1%) ----------------------------------------------------------- 30 : no_sym 32 : no_mem_ops 27 : no_var 13 : no_typeinfo 7 : bad_offset Signed-off-by: Namhyung Kim --- tools/perf/util/annotate-data.c | 232 ++++++++++++++++++++++++++++++++ 1 file changed, 232 insertions(+) diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-data.c index 185cb896b9d6..cebbe17de64a 100644 --- a/tools/perf/util/annotate-data.c +++ b/tools/perf/util/annotate-data.c @@ -597,6 +597,231 @@ void update_insn_state(struct type_state *state, struct data_loc_info *dloc, /* Case 4. memory to memory transfers (not handled for now) */ } +/* Prepend this_list to full_list, removing duplicate disasm line */ +static void prepend_basic_blocks(struct list_head *this_blocks, + struct list_head *full_blocks) +{ + struct annotated_basic_block *first_bb, *last_bb; + + last_bb = list_last_entry(this_blocks, typeof(*last_bb), list); + first_bb = list_first_entry(full_blocks, typeof(*first_bb), list); + + if (list_empty(full_blocks)) + goto out; + + if (last_bb->end != first_bb->begin) { + pr_debug("prepend basic blocks: mismatched disasm line %lx -> %lx\n", + last_bb->end->al.offset, first_bb->begin->al.offset); + goto out; + } + + /* Is the basic block have only one disasm_line? */ + if (last_bb->begin == last_bb->end) { + list_del(&last_bb->list); + free(last_bb); + goto out; + } + + last_bb->end = list_prev_entry(last_bb->end, al.node); + +out: + list_splice(this_blocks, full_blocks); +} + +static void delete_basic_blocks(struct list_head *basic_blocks) +{ + struct annotated_basic_block *bb, *tmp; + + list_for_each_entry_safe(bb, tmp, basic_blocks, list) { + list_del(&bb->list); + free(bb); + } +} + +/* Make sure all variables have a valid start address */ +static void fixup_var_address(struct die_var_type *var_types, u64 addr) +{ + while (var_types) { + /* + * Some variables have no address range meaning it's always + * available in the whole scope. Let's adjust the start + * address to the start of the scope. + */ + if (var_types->addr == 0) + var_types->addr = addr; + + var_types = var_types->next; + } +} + +static void delete_var_types(struct die_var_type *var_types) +{ + while (var_types) { + struct die_var_type *next = var_types->next; + + free(var_types); + var_types = next; + } +} + +/* It's at the target address, check if it has a matching type */ +static bool find_matching_type(struct type_state *state, + struct data_loc_info *dloc, int reg, + Dwarf_Die *type_die) +{ + Dwarf_Word size; + + if (state->regs[reg].ok) { + int tag = dwarf_tag(&state->regs[reg].type); + + /* + * Normal registers should hold a pointer (or array) to + * dereference a memory location. + */ + if (tag != DW_TAG_pointer_type && tag != DW_TAG_array_type) + return false; + + if (die_get_real_type(&state->regs[reg].type, type_die) == NULL) + return false; + + dloc->type_offset = dloc->op->offset; + + /* Get the size of the actual type */ + if (dwarf_aggregate_size(type_die, &size) < 0 || + (unsigned)dloc->type_offset >= size) + return false; + + return true; + } + + if (reg == dloc->fbreg) { + struct type_state_stack *stack; + + stack = find_stack_state(state, dloc->type_offset); + if (stack == NULL) + return false; + + *type_die = stack->type; + /* Update the type offset from the start of slot */ + dloc->type_offset -= stack->offset; + return true; + } + + if (dloc->fb_cfa) { + struct type_state_stack *stack; + u64 pc = map__rip_2objdump(dloc->ms->map, dloc->ip); + int fbreg, fboff; + + if (die_get_cfa(dloc->di->dbg, pc, &fbreg, &fboff) < 0) + fbreg = -1; + + if (reg != fbreg) + return false; + + stack = find_stack_state(state, dloc->type_offset - fboff); + if (stack == NULL) + return false; + + *type_die = stack->type; + /* Update the type offset from the start of slot */ + dloc->type_offset -= fboff + stack->offset; + return true; + } + + return false; +} + +/* Iterate instructions in basic blocks and update type table */ +static bool find_data_type_insn(struct data_loc_info *dloc, int reg, + struct list_head *basic_blocks, + struct die_var_type *var_types, + Dwarf_Die *cu_die, Dwarf_Die *type_die) +{ + struct type_state state; + struct symbol *sym = dloc->ms->sym; + struct annotation *notes = symbol__annotation(sym); + struct annotated_basic_block *bb; + bool found = false; + + init_type_state(&state, dloc->arch); + + list_for_each_entry(bb, basic_blocks, list) { + struct disasm_line *dl = bb->begin; + + list_for_each_entry_from(dl, ¬es->src->source, al.node) { + u64 this_ip = sym->start + dl->al.offset; + u64 addr = map__rip_2objdump(dloc->ms->map, this_ip); + + /* Update variable type at this address */ + update_var_state(&state, dloc, addr, var_types); + + if (this_ip == dloc->ip) { + found = find_matching_type(&state, dloc, reg, + type_die); + goto out; + } + + /* Update type table after processing the instruction */ + update_insn_state(&state, dloc, cu_die, dl); + if (dl == bb->end) + break; + } + } + +out: + exit_type_state(&state); + return found; +} + +/* + * Construct a list of basic blocks for each scope with variables and try to find + * the data type by updating a type state table through instructions. + */ +static int find_data_type_block(struct data_loc_info *dloc, int reg, + Dwarf_Die *cu_die, Dwarf_Die *scopes, + int nr_scopes, Dwarf_Die *type_die) +{ + LIST_HEAD(basic_blocks); + struct die_var_type *var_types = NULL; + u64 src_ip, dst_ip; + int ret = -1; + + dst_ip = dloc->ip; + for (int i = nr_scopes - 1; i >= 0; i--) { + Dwarf_Addr base, start, end; + LIST_HEAD(this_blocks); + + if (dwarf_ranges(&scopes[i], 0, &base, &start, &end) < 0) + break; + + src_ip = map__objdump_2rip(dloc->ms->map, start); + + /* Get basic blocks for this scope */ + if (annotate_get_basic_blocks(dloc->ms->sym, src_ip, dst_ip, + &this_blocks) < 0) + continue; + prepend_basic_blocks(&this_blocks, &basic_blocks); + + /* Get variable info for this scope and add to var_types list */ + die_collect_vars(&scopes[i], &var_types); + fixup_var_address(var_types, start); + + /* Find from start of this scope to the target instruction */ + if (find_data_type_insn(dloc, reg, &basic_blocks, var_types, + cu_die, type_die)) { + ret = 0; + break; + } + + /* Go up to the next scope and find blocks to the start */ + dst_ip = src_ip; + } + + delete_basic_blocks(&basic_blocks); + delete_var_types(var_types); + return ret; +} + /* The result will be saved in @type_die */ static int find_data_type_die(struct data_loc_info *dloc, Dwarf_Die *type_die) { @@ -697,6 +922,13 @@ static int find_data_type_die(struct data_loc_info *dloc, Dwarf_Die *type_die) goto out; } + if (reg != DWARF_REG_PC) { + ret = find_data_type_block(dloc, reg, &cu_die, scopes, + nr_scopes, type_die); + if (ret == 0) + goto out; + } + if (loc->multi_regs && reg == loc->reg1 && loc->reg1 != loc->reg2) { reg = loc->reg2; goto retry; From patchwork Fri Feb 2 22:04:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 196047 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp724803dyc; Fri, 2 Feb 2024 14:08:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IHM020Oznm0lj9MLmv1HY7wKUXuNWIYPqda4IBp47xhROJAgM6EzyM5A3G8Y884bOM1AG3H X-Received: by 2002:a0c:df8c:0:b0:68c:4f1c:d33b with SMTP id w12-20020a0cdf8c000000b0068c4f1cd33bmr2846158qvl.37.1706911734473; Fri, 02 Feb 2024 14:08:54 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706911734; cv=pass; d=google.com; s=arc-20160816; b=pr2I1hCmOOzDkUqR6xMHxtrdAAUctOW1Uz6XT75O9rGmJcE+g0IceeSV+FDay6wZdI qVwHtH2+lGsZBiDa5cygdKIUo5JD+mda2k5yjaYyrqnaEgSLIUuBxkPGKWINTzO8Kqly rbDfrSQaKVfuDYa1Qw071lGpRt2OV+6omF95sweSAqbVZpPWbPW/6l9X8NuIDYqmEeIL 6ka9iLzXnaV9Crx2g14rXIWfi5gOZH4xSxErGIxYbMN1Clsry/Kknjeb/X2tk9HcMOFv i7Q/G6wlqcaemAuf4SbrRA21M8KzOuzEi4AGpKXo/8YDXy0YQTo6d5XjjSEFueOFFb3i oWBA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=IYw3E92La1xMAFvt8eVinq1pIoVaM7lHHMiLjO9q/r4=; fh=GoaB6PqE6dEX2iZl9UKl6Fhyy16a6wKdKY22Z+YzwbM=; b=UDAOLEDWc1pYZaNWoPAPAY/qQDhbuIbfP+sgEU+ceLLRm4996VLX4PH0QN091H8aHw 30NkW9TajxSJUTtWc2OnuvO+gWod9TYUAjPTEgJEz+ay48aAGKoi0nrsbI1F/9OYrmhl GsIcUaDOygsFH64wTEg4yM6JoT0K3JXA4liCMTNNueR41tIBlVZ6+IEY/X0ONVMcETdD +w8VR0w3OOo2n+mFXOaZ2diEAuGxY+dbmt/5HH7J3056qCT+6R2ZPZyL2PKt7fl+pBIe qRpv9iupRKProJsXdyjdD+vpEce3mQOWy1Owt3Kc2o206aHXiZK3RQen0KtmRDPPSWjy T1Dw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=BqLKpGXh; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50644-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50644-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCUj0t7YvylDnEfO6ClfOwHbP4sb7MLFIj5b+Ben8/B4rpRq8iqsmu6tKK8DUtOVvGvp0hCqIEqTtvZFiDYn+eLHzmJN9w== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id cp14-20020ad44aee000000b0068c475d53e0si2887455qvb.578.2024.02.02.14.08.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 14:08:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50644-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=BqLKpGXh; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50644-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50644-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 36EBC1C244C1 for ; Fri, 2 Feb 2024 22:08:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 33A031332BF; Fri, 2 Feb 2024 22:05:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="BqLKpGXh" 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 228F1130AD0; Fri, 2 Feb 2024 22:05:08 +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=1706911509; cv=none; b=ZP0+nZx7n+l+rpjNugtQ9reEmb/EhGbF7l+NGtRTEytOdkLxnBxo+3qEaUr/E1P0nxwpgPeOBPxsUTzKfJ8Qtt1kiznDB+O2SGJpkt37+CqeE863fm7b8D4ukEk8Btrpd+Pw44nUa3urYWWpomQi9GfLkLNZJgo0CJqEzb164qM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706911509; c=relaxed/simple; bh=i+cKdwWRkc3VtQ3wGRnNjPZEDzZov+iNSpO+Ws+UzjA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DMxGw2oR+YWeHJWW+IjbzId5mibAhEXvwhDV2J7P57BPVzl9hqnMEIDTEXncnQtBE51ATJvAgF8Yx0lJ25KtSDcTuS7cK/qHnBE++dZkdshiLTFoBJ22BgLXmShENfEB9gL7iU5xvEA6cfqFv7iehYU440aLNYTkQUqDiYrxuIw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BqLKpGXh; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1FE65C41679; Fri, 2 Feb 2024 22:05:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706911508; bh=i+cKdwWRkc3VtQ3wGRnNjPZEDzZov+iNSpO+Ws+UzjA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BqLKpGXhq8rFfojJ1pgADczuOdLkKNvIeoR/LVYkVr8+ZN6+aN/xXJ2oBkhTUI5/C 5b48DsMgzZMkkdDfclDizZwt2OkZxK+92L33dI05CrPBJFCnBEMvBhZbFkueOTyNy0 +NoVr0UGLWfofIN720AIZbD8lASOKBefTCvTbWI1yz7f2tlGPX7zWP0n2ferWkxlJG ivJPKCYVXxEa7sh2k5Y6OEwDT4CYCJK+55gMtsOw1NRVb2Tg3FClAB5IQBG/MpJBG2 76//JXP+KV4KGc6oLhTSVqaE60dgdpiyJFsiotpAUeOIoEEKfMjjLEPMRomnoo5ZGN crA5j+EbrPbHw== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , 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 11/14] perf annotate: Parse x86 segment register location Date: Fri, 2 Feb 2024 14:04:56 -0800 Message-ID: <20240202220459.527138-12-namhyung@kernel.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240202220459.527138-1-namhyung@kernel.org> References: <20240202220459.527138-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789826678945924718 X-GMAIL-MSGID: 1789826678945924718 Add a segment field in the struct annotated_insn_loc and save it for the segment based addressing like %gs:0x28. For simplicity it now handles %gs register only. Signed-off-by: Namhyung Kim --- tools/perf/util/annotate.c | 21 +++++++++++++++++++-- tools/perf/util/annotate.h | 13 +++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index 89a8d57b1bf7..86ac44c476bf 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -3557,6 +3557,12 @@ static int extract_reg_offset(struct arch *arch, const char *str, * %gs:0x18(%rbx). In that case it should skip the part. */ if (*str == arch->objdump.register_char) { + if (arch__is(arch, "x86")) { + /* FIXME: Handle other segment registers */ + if (!strncmp(str, "%gs:", 4)) + op_loc->segment = INSN_SEG_X86_GS; + } + while (*str && !isdigit(*str) && *str != arch->objdump.memory_ref_char) str++; @@ -3653,8 +3659,19 @@ int annotate_get_insn_location(struct arch *arch, struct disasm_line *dl, op_loc->multi_regs = multi_regs; extract_reg_offset(arch, insn_str, op_loc); } else { - char *s = strdup(insn_str); + char *s; + + if (arch__is(arch, "x86")) { + /* FIXME: Handle other segment registers */ + if (!strncmp(insn_str, "%gs:", 4)) { + op_loc->segment = INSN_SEG_X86_GS; + op_loc->offset = strtol(insn_str + 4, + NULL, 0); + continue; + } + } + s = strdup(insn_str); if (s) { op_loc->reg1 = get_dwarf_regnum(s, 0); free(s); @@ -3875,7 +3892,7 @@ struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he) .op = op_loc, }; - if (!op_loc->mem_ref) + if (!op_loc->mem_ref && op_loc->segment == INSN_SEG_NONE) continue; /* Recalculate IP because of LOCK prefix or insn fusion */ diff --git a/tools/perf/util/annotate.h b/tools/perf/util/annotate.h index b460785111a1..2bd654620de3 100644 --- a/tools/perf/util/annotate.h +++ b/tools/perf/util/annotate.h @@ -446,6 +446,7 @@ int annotate_check_args(void); * @reg1: First register in the operand * @reg2: Second register in the operand * @offset: Memory access offset in the operand + * @segment: Segment selector register * @mem_ref: Whether the operand accesses memory * @multi_regs: Whether the second register is used */ @@ -453,6 +454,7 @@ struct annotated_op_loc { int reg1; int reg2; int offset; + u8 segment; bool mem_ref; bool multi_regs; }; @@ -464,6 +466,17 @@ enum annotated_insn_ops { INSN_OP_MAX, }; +enum annotated_x86_segment { + INSN_SEG_NONE = 0, + + INSN_SEG_X86_CS, + INSN_SEG_X86_DS, + INSN_SEG_X86_ES, + INSN_SEG_X86_FS, + INSN_SEG_X86_GS, + INSN_SEG_X86_SS, +}; + /** * struct annotated_insn_loc - Location info of instruction * @ops: Array of location info for source and target operands From patchwork Fri Feb 2 22:04:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 196060 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp731452dyc; Fri, 2 Feb 2024 14:27:26 -0800 (PST) X-Google-Smtp-Source: AGHT+IHV/fxQGX7bIobgKn/lavdy/UmvUP9KSXwPZTtf4tw10PNDGm1sqmVk0mB8hb8Kq7RLguI7 X-Received: by 2002:aa7:df82:0:b0:55f:c4a7:fd6a with SMTP id b2-20020aa7df82000000b0055fc4a7fd6amr530449edy.29.1706912846218; Fri, 02 Feb 2024 14:27:26 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706912846; cv=pass; d=google.com; s=arc-20160816; b=r4xXuY0l2CwmJzrp67FEgd69L/fI3s78PLZd+MWPkNEEf6Vgfa1VNh9+lHVDkQKkO6 NZ1ODpqsBmiJL+ipxiZP5ma6W4RCOX4SxghA9ghMjfOwHtAVYJwMgEsEavAgUqOdeRu/ w9wiPtLdvivuik2eTZPXUVpsgfFgWIKo7i9o9xIQxTlntITF0UPLSH9p3/lJiwlB4YVM UNZ3uP8UiYD1d67hvpx+z/XhXPSWWctbjMIN/tZ/cZH1gwGQw/+qRQNUC4l2tc+k6Ln7 h3gtar7QvBBg+lPEHwkJH2Y+cyluEx2+AO2rjg4s9qUWahL/P1TXfWrkAccfa7e/S2Wj KoUQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=ukqL46glfuzW/mfbFJ9MrCpdUQKKGStKWBqqoZ7KWFs=; fh=YFmUj4UfFbvWaf+gmqpmh74gwcZTJ+SX8RuPlUsGaVs=; b=meKCIGh9H9OmN+sx1jOE5LsjAYU5ewiQTe/Vj6fcuDQSOElkIM8+Qt/MMfWlVAzfXy daJVZs9wGf0MrVF6V991a5HYCB1mukCoCR3qaHp8Ewlgwll3dU7l1zY4lWaaWwPMHKzQ q0DSq+7K4ObhBc27iIaeCdZ/tXhM5XI6Md+Me6m1WIEAQToUcNf/LSi+y8AqK/pKx9Wc 3lBH4D53BDk9Lp2ILbkx68Q6+YaTEEW30F56SPA+1e+pi7lPUdqi35r9xvWOd63jvDWH UhhGhR03o2wg+yOiMdgb8nMTl0sc+d+nuThKhtFLuPON6IPlVFqQ71Jcrwh7y8hAn4IK uHlg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ejNmlryN; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50646-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50646-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCUybvvSy9K418iWgISO3S7Xf/QNR70n0udksv6Za/GuUZYeet8JKNenDBgDOjmTxMIIPpaJciXty3D4K3JW9Np7R0FUug== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id cx17-20020a05640222b100b0055efe9dcb3dsi1138146edb.256.2024.02.02.14.27.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 14:27:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50646-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ejNmlryN; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50646-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50646-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 1474F1F2D6BF for ; Fri, 2 Feb 2024 22:09:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4BB0D134CE3; Fri, 2 Feb 2024 22:05:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ejNmlryN" 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 3978F132468; Fri, 2 Feb 2024 22:05:09 +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=1706911510; cv=none; b=YcGUObPfeV4wFt26jI4TEDxkMkmeKDbvh9D4GeUZhwpu18muy7t8ewWyAGXOeyUcTyXurao639hB/3mujtKAru5eXRCwIUIBS9RE8Zcv+Jb9RHUQFs0PzViRku+9LncuLZL9Ut0WFjGpGsso5R8Hw/LbGNqj9ZPerQKhtY0QzNk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706911510; c=relaxed/simple; bh=ozKwyj+cawqB9+hDRoeS33gIgZE7kn/6L5uzLJ/tqHg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=h/FDJD6DonAnJ7PvYY8WHriLrdDxCGUhedy2chKIMiFKHMvAwyxg1bUE1qGG8satgBu2JyYO6Jo1DyGujUryzqAdupnm8cU8PjCj17wdPJNXokP7nlNmeYB23l1yLvtlby38CNwzVJOnksS+u7MH1x7IdbE4+6eIa4SPEe9Gspo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ejNmlryN; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id B67DAC433F1; Fri, 2 Feb 2024 22:05:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706911509; bh=ozKwyj+cawqB9+hDRoeS33gIgZE7kn/6L5uzLJ/tqHg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ejNmlryNPrqsqXEL0aqgztoVPSL1Y6D1cX/Y6sPy6aF8Yp3dfWyet1aTtRxwgBgyi gxv5vFbxXylNw6m1yi7nbtLbGTVMjMsib4ryT/3DnjUSf0xz/NNejb+4rgWW/49kSt NxCMdSH16x2zom61JG7hoZSvCGOe3ii9+QTgWe3TDMdckRQPxZDET71F8xcbY4RcrV ll6ORU5wjSSns4UCRG1ioFgZwl318nlSB7NaQFL1kzdw9xLGHRk+tSZ4W3ZohGgHev z7v5GZtVSSf6cWjXHFnGhNSgrhRRip7BArVFw+0uqfB87e317N3OmlvIb3+aGAITXw ml9RVjSKPpvOg== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , 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 12/14] perf annotate-data: Handle this-cpu variables in kernel Date: Fri, 2 Feb 2024 14:04:57 -0800 Message-ID: <20240202220459.527138-13-namhyung@kernel.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240202220459.527138-1-namhyung@kernel.org> References: <20240202220459.527138-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789827844421392849 X-GMAIL-MSGID: 1789827844421392849 On x86, the kernel gets the current task using the current macro like below: #define current get_current() static __always_inline struct task_struct *get_current(void) { return this_cpu_read_stable(pcpu_hot.current_task); } So it returns the current_task field of struct pcpu_hot which is the first member. On my build, it's located at 0x32940. $ nm vmlinux | grep pcpu_hot 0000000000032940 D pcpu_hot And the current macro generates the instructions like below: mov %gs:0x32940, %rcx So the %gs segment register points to the beginning of the per-cpu region of this cpu and it points the variable with a constant. Let's update the instruction location info to have a segment register and handle %gs in kernel to look up a global variable. The new get_percpu_var_info() helper is to get information about the variable. Pretend it as a global variable by changing the register number to DWARF_REG_PC. Signed-off-by: Namhyung Kim --- tools/perf/util/annotate.c | 31 +++++++++++++++++++++++++++++++ tools/perf/util/annotate.h | 4 ++++ 2 files changed, 35 insertions(+) diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index 86ac44c476bf..5f3136f57c62 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -3810,6 +3810,27 @@ void get_global_var_info(struct thread *thread, struct map_symbol *ms, u64 ip, addr_location__exit(&al); } +void get_percpu_var_info(struct thread *thread, struct map_symbol *ms, + u8 cpumode, u64 var_addr, const char **var_name, + int *poffset) +{ + struct addr_location al; + struct symbol *var; + u64 map_addr; + + /* Kernel symbols might be relocated */ + map_addr = var_addr + map__reloc(ms->map); + + addr_location__init(&al); + var = thread__find_symbol_fb(thread, cpumode, map_addr, &al); + if (var) { + *var_name = var->name; + /* Calculate type offset from the start of variable */ + *poffset = map_addr - map__unmap_ip(al.map, var->start); + } + addr_location__exit(&al); +} + /** * hist_entry__get_data_type - find data type for given hist entry * @he: hist entry @@ -3906,6 +3927,16 @@ struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he) &dloc.type_offset); } + /* This CPU access in kernel - pretend PC-relative addressing */ + if (op_loc->reg1 < 0 && map__dso(ms->map)->kernel && + arch__is(arch, "x86") && op_loc->segment == INSN_SEG_X86_GS) { + dloc.var_addr = op_loc->offset; + get_percpu_var_info(he->thread, ms, he->cpumode, + dloc.var_addr, &dloc.var_name, + &dloc.type_offset); + op_loc->reg1 = DWARF_REG_PC; + } + mem_type = find_data_type(&dloc); if (mem_type) istat->good++; diff --git a/tools/perf/util/annotate.h b/tools/perf/util/annotate.h index 2bd654620de3..490134d19c9d 100644 --- a/tools/perf/util/annotate.h +++ b/tools/perf/util/annotate.h @@ -513,6 +513,10 @@ void get_global_var_info(struct thread *thread, struct map_symbol *ms, u64 ip, struct disasm_line *dl, u8 cpumode, u64 *var_addr, const char **var_name, int *poffset); +void get_percpu_var_info(struct thread *thread, struct map_symbol *ms, + u8 cpumode, u64 var_addr, const char **var_name, + int *poffset); + /** * struct annotated_basic_block - Basic block of instructions * @list: List node From patchwork Fri Feb 2 22:04:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 196048 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp725033dyc; Fri, 2 Feb 2024 14:09:34 -0800 (PST) X-Google-Smtp-Source: AGHT+IEtxd9NTkbf986BsC3ao/P/LneUiPI2qoHBvvAiMfe9DHajnEPMvw8h/4wGH1WxhxYRMyOe X-Received: by 2002:a05:6a00:1d16:b0:6de:12ae:68d6 with SMTP id a22-20020a056a001d1600b006de12ae68d6mr9812307pfx.17.1706911774611; Fri, 02 Feb 2024 14:09:34 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706911774; cv=pass; d=google.com; s=arc-20160816; b=H7ZNyUoC6uyErER4OvBa5EsfCRYJ4MvoKkzLNKzQsjp4jQzDCpKFzWMIQJWVqjbfYD QDmDsM7bbewv1k5EaPhtrgR/f+Ssg6F7RbD5KVDQrJtqFoL+cb1tJ28DxbSaCPIDpiXb SXzke1FEtTpdigmODqWFkuHfsbOO4FxK6G9flfd74Doh8Xy6rIU/32/cTP6wbizlRCP5 bQ4CcqWbyIGkUAKIxDJQOwtC47ujzpLnswmDDraVYOZ3e2d7KMM2Mu7hCD7KJhbaff8A Dh9hLwlSVrVTumcQMW3YjmFO4/S76r07zzacVJcg9LGHgeAcElerAdzczJL6JaEk6vpd /q3A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=g0XRdLleLKn1Zj/A9tKtIFP/m2yOWhx1p1ivAxZ0QkY=; fh=BBXdMkrNZsErYjFGjgOV139FUPnSUAu6OEhn0PbdUqg=; b=WUcI3iSLQsa8jfFiNFaeywKlEAyGbvOp+sKMGkAHkRxt6eWMAz1fQm322NM2VK977f 0l3JbeoF7ahDAd+EhOYif8zmLuMLLyMOkw64qIZfHY/b67Nt6u89PDL+W8vAtNIzyYC6 jtdrKWBgwkdtzhSzjcin1ZTKrW1Ad6X9xuNvubHiHu+oq2S1eATRnRDBziToHt0UAPal +M99X5epD5nsBNZNEwpQW8CnQTiccjKY5Ei6JlfFRYiuf4aJI5PBpGVtFrt4gp8a3mdI NkWbL2dBJ4vTzq1CGauFMbGeiPnS8ZB2R4vPpP2xY+X9pgWuSND6PjD16srSzVNSryMW 418g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=TxHn2IiH; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50645-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50645-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCUfQNMWLbOA4eYOog4Wc43oTSjMIdLmRDtDmgSWSfVrJ8m2knNpZuZRvpMai7r11fs63VlWO2xVWOCHAJjvthAoHSkasw== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id b24-20020a63eb58000000b005c66a3f3f5bsi2159588pgk.745.2024.02.02.14.09.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 14:09:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50645-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=TxHn2IiH; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50645-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50645-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 153C7284674 for ; Fri, 2 Feb 2024 22:09:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8BC92134CEB; Fri, 2 Feb 2024 22:05:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="TxHn2IiH" 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 39757132467; Fri, 2 Feb 2024 22:05:09 +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=1706911510; cv=none; b=UbI1WJhzJOKzZa1QhY0d78D3p6dQubkeROlL9daBI4Oq8J9xo3cJCUe1iuJpjsJRYpuEaWSRzbxfWUUVUGxiopf3cBetNQUvL4eAsk0aWQMvCnQK3K90C7fV9tCyOXZo2VX8xrN74bZQTGZTVG8SP6iooEWr47N3QiePFKb9hLE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706911510; c=relaxed/simple; bh=8cuanY9nIKPUghP/dPMClAHP/3p1hi26K3nztrI2MFI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=agG8Z709yo0/mB1R4qPOdvJr5Hq2A6IX/b74DJz1+fpOFUJ3CvDhLhi45txagN54WgKYS3coX0k5xbCYC4xiXA6X/DzPWDLxXWw8AHuLGCMIvnYmneTfXkxbA8jSjGcEu8FXv1M2GOy7GQjm+zsmrrZdEuLMvylii0r8gpiOXtg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=TxHn2IiH; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5866CC43390; Fri, 2 Feb 2024 22:05:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706911509; bh=8cuanY9nIKPUghP/dPMClAHP/3p1hi26K3nztrI2MFI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TxHn2IiHBRlypqccvr7R1y33hds9O14sWGIbCsicdHXkx2sdrIDnShKjbGjiY6x3O DOxLl3ppa9OEWw8+YoorLHC39SbSIedAHJiF/4IQwBXgQKe/FAdHEWRAqkcoUFVKaB 9s5hrCQQiaLpasPfJCiaGOlbgpb4k6o6ZYrnLJLDP4PARu/upkAU/5CMY1kNLzBhSh 2btZ6QO6LOCiW0JsDeZMgifISkpp3z4iraiN8Ka+qjhMFYfSPkhIzo1065+MjW0Z07 RZJDcmrt/D0cA6Um6u1oToUlo4XkY4/RBKYTkSiwvWSqqeaLvdjRVmw4ATVcdhxtO5 vLTKS3Z+Dtykg== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , 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 13/14] perf annotate-data: Track instructions with a this-cpu variable Date: Fri, 2 Feb 2024 14:04:58 -0800 Message-ID: <20240202220459.527138-14-namhyung@kernel.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240202220459.527138-1-namhyung@kernel.org> References: <20240202220459.527138-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789826720809100715 X-GMAIL-MSGID: 1789826720809100715 Like global variables, this per-cpu variables should be tracked correctly. Factor our get_global_var_type() to handle both global and per-cpu (for this cpu) variables in the same manner. Signed-off-by: Namhyung Kim --- tools/perf/util/annotate-data.c | 84 +++++++++++++++++++++++---------- 1 file changed, 60 insertions(+), 24 deletions(-) diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-data.c index cebbe17de64a..9d6cc6ac431c 100644 --- a/tools/perf/util/annotate-data.c +++ b/tools/perf/util/annotate-data.c @@ -410,6 +410,37 @@ void update_var_state(struct type_state *state, struct data_loc_info *dloc, } } +static bool get_global_var_type(Dwarf_Die *cu_die, struct map_symbol *ms, u64 ip, + u64 var_addr, const char *var_name, int var_offset, + Dwarf_Die *type_die) +{ + u64 pc; + int offset = var_offset; + bool is_pointer = false; + Dwarf_Die var_die; + + pc = map__rip_2objdump(ms->map, ip); + + /* Try to get the variable by address first */ + if (die_find_variable_by_addr(cu_die, pc, var_addr, &var_die, &offset) && + check_variable(&var_die, type_die, offset, is_pointer) == 0 && + die_get_member_type(type_die, offset, type_die)) + return true; + + if (var_name == NULL) + return false; + + offset = var_offset; + + /* Try to get the name of global variable */ + if (die_find_variable_at(cu_die, var_name, pc, &var_die) && + check_variable(&var_die, type_die, offset, is_pointer) == 0 && + die_get_member_type(type_die, offset, type_die)) + return true; + + return false; +} + /** * update_insn_state - Update type state for an instruction * @state: type state table @@ -473,14 +504,36 @@ void update_insn_state(struct type_state *state, struct data_loc_info *dloc, fbreg = -1; } - /* Case 1. register to register transfers */ + /* Case 1. register to register or segment:offset to register transfers */ if (!src->mem_ref && !dst->mem_ref) { if (!has_reg_type(state, dst->reg1)) return; if (has_reg_type(state, src->reg1)) state->regs[dst->reg1] = state->regs[src->reg1]; - else + else if (map__dso(dloc->ms->map)->kernel && + src->segment == INSN_SEG_X86_GS) { + struct map_symbol *ms = dloc->ms; + int offset = src->offset; + u64 ip = ms->sym->start + dl->al.offset; + const char *var_name = NULL; + u64 var_addr; + + /* + * In kernel, %gs points to a per-cpu region for the + * current CPU. Access with a constant offset should + * be treated as a global variable access. + */ + var_addr = src->offset; + get_percpu_var_info(dloc->thread, ms, dloc->cpumode, + var_addr, &var_name, &offset); + + if (get_global_var_type(cu_die, ms, ip, var_addr, + var_name, offset, &type_die)) { + state->regs[dst->reg1].type = type_die; + state->regs[dst->reg1].ok = true; + } + } else state->regs[dst->reg1].ok = false; } /* Case 2. memory to register transers */ @@ -493,37 +546,20 @@ void update_insn_state(struct type_state *state, struct data_loc_info *dloc, retry: /* Check if it's a global variable */ if (sreg == DWARF_REG_PC) { - Dwarf_Die var_die; struct map_symbol *ms = dloc->ms; int offset = src->offset; u64 ip = ms->sym->start + dl->al.offset; - u64 pc, addr; const char *var_name = NULL; + u64 var_addr; - addr = annotate_calc_pcrel(ms, ip, offset, dl); - pc = map__rip_2objdump(ms->map, ip); - - if (die_find_variable_by_addr(cu_die, pc, addr, - &var_die, &offset) && - check_variable(&var_die, &type_die, offset, - /*is_pointer=*/false) == 0 && - die_get_member_type(&type_die, offset, &type_die)) { - state->regs[dst->reg1].type = type_die; - state->regs[dst->reg1].ok = true; - return; - } + var_addr = annotate_calc_pcrel(ms, ip, offset, dl); - /* Try to get the name of global variable */ - offset = src->offset; get_global_var_info(dloc->thread, ms, ip, dl, - dloc->cpumode, &addr, + dloc->cpumode, &var_addr, &var_name, &offset); - if (var_name && die_find_variable_at(cu_die, var_name, - pc, &var_die) && - check_variable(&var_die, &type_die, offset, - /*is_pointer=*/false) == 0 && - die_get_member_type(&type_die, offset, &type_die)) { + if (get_global_var_type(cu_die, ms, ip, var_addr, + var_name, offset, &type_die)) { state->regs[dst->reg1].type = type_die; state->regs[dst->reg1].ok = true; } else From patchwork Fri Feb 2 22:04:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 196049 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp725040dyc; Fri, 2 Feb 2024 14:09:35 -0800 (PST) X-Google-Smtp-Source: AGHT+IF0ktLKm9gpuL4+rk18mdgofIlCLGSsRjDepVaxeMK3q7KArKxSkosHmCmxPHghdJKl5ukw X-Received: by 2002:a05:6359:a381:b0:178:72b5:cf41 with SMTP id ky1-20020a056359a38100b0017872b5cf41mr9703301rwc.7.1706911775316; Fri, 02 Feb 2024 14:09:35 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706911775; cv=pass; d=google.com; s=arc-20160816; b=jCRbl/yQItTSJqJ1SZiXv9ZfASKdEsm0pXsch+gsflTYIyySAULzcw24knmfae7u0n mxc8vsvQXuSuTNY/Eg0Ga9oeirljdDkJO3RolNlwZTiww1K4JJRFV48RNsYYpX8pyGZh QZZE49+qU61bRqRS7jn5TxpYGTaDMxfZjOEPEcZAPltO+yDY2p2KOKbFdONVyKZxPfAV GVV5kD7Dxc5XZ8Zw+HMAFOI8BL2nqyH8ExJVHaPhILCszJr8GFuYNtGukd3901g1TlFW r/XQx4WFzYR4pv+79DePNWOZfFlgmIlurg4fEP3S1LYQNTRfBgq6nOJ6VVoY1QSLZFOJ +/hw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=cyHEIG9bO6Abl7AHq3yfEYap5kTaynXteQfdMiMve6c=; fh=NerT2IPcodvohhlBftBIAHK6hfURN/QTmp1h76CaZ/M=; b=psilKqpFTc6b+FFjxZosSx2m3O0XO/m16fL49xpkn1wkhuU3YpwiNzKMgB51T1dzSh rXHzDJOfh9+8Zu+1smiaZt0FjCCS4eKC4KP0w1U0Tw7Tltv0pH9LKrOfTJo7/IfEt/Pa f7Hxg9QoioLIyfoyEYE0+oA6L4omscDVmZ8hjRrw3cj6QNfKesoJrElg6/58hXBxNkLU l9S089t0mUGv4ZNpESw9R3IdaHcLKA8XjqcmScEPQC6KhW/FdHB4W896pW+0A2Y/7bS2 /3528ZauvdJBYNClgnteCb3OcGezZ9zWY4AXDjJrawxtW4E96XBjIqXXLwMgcRN4Jdrp DmDQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Rj1bKvcP; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50647-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50647-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCUpw6lotQHXi0EApMa4qDV97rRLkD8tQBpHxRsBvtIESI+gnFRqjCdl9zBEsnFTRC54A85dFHqNGK2wG3ApLwKM9JsSBA== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id q36-20020a635c24000000b005d8e379746fsi2246151pgb.469.2024.02.02.14.09.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 14:09:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50647-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Rj1bKvcP; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50647-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50647-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 0F8902837EA for ; Fri, 2 Feb 2024 22:09:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A9905134CEF; Fri, 2 Feb 2024 22:05:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Rj1bKvcP" 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 C9F74132498; Fri, 2 Feb 2024 22:05:10 +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=1706911510; cv=none; b=B9XujTkVVcfeRZe5ZHM7RsuuemsGBPfw+KIBb3CQIO8rZKNPs3cwAUmEWjr0Azb4/0s1UpJj1tb6lMGCQtDcpWOIhYm92pEkWSbGGQ/dUdSPjz0zPEZXdbVRP5VpGrytQd3gMqtdFIANhsTgiiBEVk6ovxxzJzgBti2K+9lZ0m4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706911510; c=relaxed/simple; bh=u+3b408czl7+mH8GeTAKvju9aWQhRxzD0hyW0Dr5iWE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oBJLpojbZbIoZ0Yc0ozK85KovEb4V7Y2NodPbDy0GB/dg19qkvKotcaEyrDBVhdaVhLs22L+l2eGE5/fsmFYryndUzVnioigkno4bVKnQqug5tNFsHKM+xJStAjgn8THs6XlOdrOVA49LXXWGnaO4YWP0mZxZA+DAI6qAcpMW08= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Rj1bKvcP; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id EFFC0C433C7; Fri, 2 Feb 2024 22:05:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706911510; bh=u+3b408czl7+mH8GeTAKvju9aWQhRxzD0hyW0Dr5iWE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Rj1bKvcPX27x+4M1i+jYh7SuVlGttIBX9A5CC5LblU9hEvwGe2Od18VdDnFcbjFRk no3J1Xy5wkzm48aOCRzIo3KckxAdIxPqejNlRdZhTiAnV05sFv513YXo8N4sRXvNkc yc+SzlRdzjfBfA34VHkqBEWe0P6toRYi1gqtprXwEoJKSgtxfYXYe9gyyVK/u8gkcp zHn4niC9G0eo5JBaPs1Ib35wfDrhZjU769cjkQL+edF94vJBNMNwaBOdaKde9+v+ia L9d4L64ku4JADxIXdeXwwFnhW5HsYdcM9eaVLLWJxQwBESMMTkM1ZcHhL0gYCx2a1d fzw9QfMK+Ysdw== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , 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 14/14] perf annotate-data: Add stack canary type Date: Fri, 2 Feb 2024 14:04:59 -0800 Message-ID: <20240202220459.527138-15-namhyung@kernel.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240202220459.527138-1-namhyung@kernel.org> References: <20240202220459.527138-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789826721881884851 X-GMAIL-MSGID: 1789826721881884851 When the stack protector is enabled, compiler would generate code to check stack overflow with a special value called 'stack carary' at runtime. On x86_64, GCC hard-codes the stack canary as %gs:40. While there's a definition of fixed_percpu_data in asm/processor.h, it seems that the header is not included everywhere and many places it cannot find the type info. As it's in the well-known location (at %gs:40), let's add a pseudo stack canary type to handle it specially. Signed-off-by: Namhyung Kim --- tools/perf/util/annotate-data.h | 1 + tools/perf/util/annotate.c | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/tools/perf/util/annotate-data.h b/tools/perf/util/annotate-data.h index 0bfef29fa52c..e293980eb11b 100644 --- a/tools/perf/util/annotate-data.h +++ b/tools/perf/util/annotate-data.h @@ -77,6 +77,7 @@ struct annotated_data_type { extern struct annotated_data_type unknown_type; extern struct annotated_data_type stackop_type; +extern struct annotated_data_type canary_type; /** * struct data_loc_info - Data location information diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index 5f3136f57c62..f2683dadf3cf 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -116,6 +116,13 @@ struct annotated_data_type stackop_type = { }, }; +struct annotated_data_type canary_type = { + .self = { + .type_name = (char *)"(stack canary)", + .children = LIST_HEAD_INIT(canary_type.self.children), + }, +}; + static int arch__grow_instructions(struct arch *arch) { struct ins *new_instructions; @@ -3764,6 +3771,17 @@ static bool is_stack_operation(struct arch *arch, struct disasm_line *dl) return false; } +static bool is_stack_canary(struct arch *arch, struct annotated_op_loc *loc) +{ + /* On x86_64, %gs:40 is used for stack canary */ + if (arch__is(arch, "x86")) { + if (loc->segment == INSN_SEG_X86_GS && loc->offset == 40) + return true; + } + + return false; +} + u64 annotate_calc_pcrel(struct map_symbol *ms, u64 ip, int offset, struct disasm_line *dl) { @@ -3938,6 +3956,12 @@ struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he) } mem_type = find_data_type(&dloc); + + if (mem_type == NULL && is_stack_canary(arch, op_loc)) { + mem_type = &canary_type; + dloc.type_offset = 0; + } + if (mem_type) istat->good++; else