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;