From nobody Thu Oct 2 10:50:28 2025 Received: from mail-qk1-f201.google.com (mail-qk1-f201.google.com [209.85.222.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1B368261B97 for ; Wed, 17 Sep 2025 19:58:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758139094; cv=none; b=p6JIfKjBvbWCyxFc6At5Uj2TPYa7UJkvbqMvn7xWVyPeoD2ghnxrRCQHB3u3qowhjzqWt0sMHr6ZU7ESWmY4i9FhA3nN/bjuZWNYADyxVYjYRO325yzRlFVjRuGC2Oz56KFPiXgTZb4oxJiD9tE2iz/8u1ADEfCRl7WIW07WL6g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758139094; c=relaxed/simple; bh=685j0taKtKunwkJ8HlGLSk+D+ryDsJA6qFtnq5hZIBA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=qskgu6QBD9Mh/nOiPmt5NY4ucamnK/v3+sXEEC9rXmid2e1+LRjy5kHWvdNUKjmZnaL2MvR6AQwl7xnNJ5kHvyVeB/v31qS7HCdDqTCr+nGwAZitL7O1igij+RDrZvyAJ0jWMHDONp1svOc2m9czhWzExJzAs8VGBtkWXUZ7FaY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=hcC3l5rl; arc=none smtp.client-ip=209.85.222.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="hcC3l5rl" Received: by mail-qk1-f201.google.com with SMTP id af79cd13be357-8271bdaccf9so27082585a.0 for ; Wed, 17 Sep 2025 12:58:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1758139092; x=1758743892; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=bkIQMBhakJ32peLTS+evkX+NHcKo052jbZJFoM1YbHs=; b=hcC3l5rlu2nVKwZ27+kdWVbspa0Pyt8PAaIj8Js3Esa0eWMCipYDesXm4gZMeGSyvn n3q8uL9rJfQQQEvRmQGTg5WOrmlZCamc8z05wSeK6hzVZthXNrnHbMHL8GkyMFqIfidw QQv4yUFDNT7pAIXjyzofv4Ol9PaDoplyoqC1Bpnoa4oiu93iu5ygbg9UznlvBjNoE7Ps 7xI5265BI5H7aCMhKv7qw9rlRaA+KrtlbNQ/v0VQvWZT3PO0WyhszVVykA/uhJIuEkvw GAjgTGtXWdNDA8GkkRN1k0GNGaVsr55now8FqmODmy3MAYDmcPiXx69T8YHe4+FtrqFz pkJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758139092; x=1758743892; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=bkIQMBhakJ32peLTS+evkX+NHcKo052jbZJFoM1YbHs=; b=ai5DrgU0tc79h4wJpdtWzb0YtQw5m9aWk59yw7bwHcbkoq6TggBO6tcbIYuR0B4hd6 goEPBchi8K+RXjvHovkReBNeJVzaX6uZbsUroMqZafONebbTc8OFjuiUfCle5zfl9rj2 g3/Db1SDHRwXuFqkotK3gHNAmb7LvYQExTcNaEg1eUheEmvdNikkmsAPzkHMjRGqieLf CmRsdOjxDaIzPpQrOsG/+QhhDqtCg3YXgSakd4goZN4I7wy5/UP/8MDOhFy++nrUVreR MNHOgnk9ZPCh1c21KyoWF9z+iwfsN+RBuUHTOrka85j96x4ajclNjHUxFuwreXTX3IS9 nTTw== X-Forwarded-Encrypted: i=1; AJvYcCV9MU9DLKkK9lf8i9Y0D7MPjCwNwLle7IGTG+ohrCOnM8O6VdpXq3WkBT4p1Wiid3cc0lQbZRzF0GS0szc=@vger.kernel.org X-Gm-Message-State: AOJu0Yz1k6XbAq1ZNHIExSxDgGxa3ku1myZraS8+vcRh9Lcp5gkoYSAV isg8wcpwZbc5Mq7Av+UsJhHmF8H7Fg+AtWZIntjq1pScYSCoLN/tPbDi/smkskEmWIr7nXotRZF dwnnVaA1fsw== X-Google-Smtp-Source: AGHT+IFM2EWO+kmcC4eFMQ0HsyVb/F6wpZ2gs7sPIfCCXWLTCD5JSXyqjFL3cGUFyfbblycNjAs7PV7U89aH X-Received: from qknqj16.prod.google.com ([2002:a05:620a:8810:b0:813:8240:dae]) (user=zecheng job=prod-delivery.src-stubby-dispatcher) by 2002:a05:620a:390b:b0:829:6dbd:64ce with SMTP id af79cd13be357-8310765c285mr394346685a.17.1758139092000; Wed, 17 Sep 2025 12:58:12 -0700 (PDT) Date: Wed, 17 Sep 2025 19:57:59 +0000 In-Reply-To: <20250917195808.2514277-1-zecheng@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250917195808.2514277-1-zecheng@google.com> X-Mailer: git-send-email 2.51.0.384.g4c02a37b29-goog Message-ID: <20250917195808.2514277-2-zecheng@google.com> Subject: [PATCH v3 01/10] perf annotate: Skip annotating data types to lea instructions From: Zecheng Li To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , Masami Hiramatsu Cc: Xu Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Zecheng Li Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce a helper function is_address_gen_insn() to check arch-dependent address generation instructions like lea in x86. Remove type annotation on these instructions since they are not accessing memory. It should be counted as `no_mem_ops`. Signed-off-by: Zecheng Li --- tools/perf/util/annotate.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index c9b220d9f924..e2370b7fd599 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -2699,6 +2699,19 @@ static bool is_stack_canary(struct arch *arch, struc= t annotated_op_loc *loc) return false; } =20 +/** + * Returns true if the instruction has a memory operand without + * performing a load/store + */ +static bool is_address_gen_insn(struct arch *arch, struct disasm_line *dl) +{ + if (arch__is(arch, "x86")) + if (!strncmp(dl->ins.name, "lea", 3)) + return true; + + return false; +} + static struct disasm_line * annotation__prev_asm_line(struct annotation *notes, struct disasm_line *cu= rr) { @@ -2807,6 +2820,12 @@ __hist_entry__get_data_type(struct hist_entry *he, s= truct arch *arch, return &stackop_type; } =20 + if (is_address_gen_insn(arch, dl)) { + istat->bad++; + ann_data_stat.no_mem_ops++; + return NO_TYPE; + } + for_each_insn_op_loc(&loc, i, op_loc) { struct data_loc_info dloc =3D { .arch =3D arch, --=20 2.51.0.384.g4c02a37b29-goog From nobody Thu Oct 2 10:50:28 2025 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1CF3E27CB35 for ; Wed, 17 Sep 2025 19:58:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758139095; cv=none; b=hgWhFwpkfpIfNdmsOm+SDbxoTxVmDzkbGeYmqspEaZoL3fKaJRCRERqA9FgxGLDnFkozh6Tk+Ekt1q9ox2TukQB+10vSJpj/WkyvWrb8GvzYfKMZmgETCo8P1hrV5j63qxCSQ5Wc2ymwb0LvgudsdB0izrH4HaE475kk87jIvfU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758139095; c=relaxed/simple; bh=dpu8i8I6A6nGqt1QrLv4wp8M4TqiJP/BduY3wzc1ymE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=FOe34bK7JGIYaNsvxRwOSjTO6yVZKd95XU37KxfX47LcDzcGzPX4/Vj3GX11CWcrQ8ja8TikdY5/9MYlYd5hu+WMDJvFtMX5ZPyQMXfc57nNEo0KqDCnY1Dg6uMXJYK+OuFWZpXXurm9FF/9s6uKX5XWifVmwdJBxFBiKvFYkFg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=L7vy1f0/; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="L7vy1f0/" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-730b15a72deso1695067b3.0 for ; Wed, 17 Sep 2025 12:58:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1758139093; x=1758743893; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=7i+x5H1L416WqVhcp9K/dXS5Kf1wboTabZYAs3Wqz1g=; b=L7vy1f0/khA9bUo0o/PIdHKe4PYdZM3op9JdntiaTDoWrC7ruA2JUQBLdO69YZBhXT ow5PkGczmW99eg/2oybbeB1zagiBduexy1HT+BsWRzO6TVOuOmM+AODAZvPohA96rbM3 usC4dncHDeSEKJ1Qqz7HvgjmKzEPd0ar+szrCMN9fOZbhANSPU9tCZjvYwZRycc4FZ8t nHMQXT+XAWb6p4cjh+T4qhCyzqUsqr09bPLDN71o3BymYVMQkPtYDXbn0BItE3XDIDli G+vhkKX3zwHYcMb3k0ZVzgCFsTxYsx9Ubs2b+MI6kn8OFdRYmqmK5DNHD4V8NeQ5VqSr eKzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758139093; x=1758743893; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7i+x5H1L416WqVhcp9K/dXS5Kf1wboTabZYAs3Wqz1g=; b=VegS8MgfwhoXQviQthNhZRruiDDAW5J47e8ViCHpxj7kgLQbs4yQ1bS2SDlUyV4CEw JXN/DbYTmi/nUkXkHqZWEdW/wz96ngCDhvvq1AXyyTWxCsDn4uKRLye6pwYcXojYluMf u5JdM00UtWSO4A3ogWNA4XAZ0KCULUUiAELdBb4UlzbIoPEZjkO/SKRNYZkK0ers+X5f TqbWbq8i9tocYpbVzzQf3sA6l6mIjAjjTjgKvArVgRFxU2ztMEJ08rs4/VuHk12G5U/u Wlif1Q/4XHU0Nb76x9PvrRQT3dsFAr6PLOKLSOeE0r3I5eW4lfWiG9XL+dS6orNGDhHr dzbw== X-Forwarded-Encrypted: i=1; AJvYcCVm11ukIQOGzmfNh2HfdPqandAMMHn0bF/asLUdRgefG9dP2pvBKEVQ4IWXQKxHwXWmEGpNzX+lVogtfN4=@vger.kernel.org X-Gm-Message-State: AOJu0YxWtrdLV0Nzy3khzXzjst+x3B+EZ5nvHWClSBgJUEDgj6dBSOnJ DdeVPHi+hrSn1c23gNgvixcDzWqB7Y9aTgHUxVbJOjjlBh9qpk/l58P3hvje6n9I/yIlNvN1RST JLYQsE6MIgA== X-Google-Smtp-Source: AGHT+IG4W723sYY6hqIWWYmytVPBFPN4vrquo6D23T7Y9myc1cur3ijMv4Yp/xyL3yYQwQevsQ5UOobmscoq X-Received: from ywbfr3.prod.google.com ([2002:a05:690c:3583:b0:71c:4305:1a38]) (user=zecheng job=prod-delivery.src-stubby-dispatcher) by 2002:a05:690c:b89:b0:724:fc2c:769 with SMTP id 00721157ae682-738909bcc85mr28868297b3.24.1758139093213; Wed, 17 Sep 2025 12:58:13 -0700 (PDT) Date: Wed, 17 Sep 2025 19:58:00 +0000 In-Reply-To: <20250917195808.2514277-1-zecheng@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250917195808.2514277-1-zecheng@google.com> X-Mailer: git-send-email 2.51.0.384.g4c02a37b29-goog Message-ID: <20250917195808.2514277-3-zecheng@google.com> Subject: [PATCH v3 02/10] perf annotate: Rename TSR_KIND_POINTER to TSR_KIND_PERCPU_POINTER From: Zecheng Li To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , Masami Hiramatsu Cc: Xu Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Zecheng Li Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" TSR_KIND_POINTER only represents percpu pointers currently. Rename it to TSR_KIND_PERCPU_POINTER so we can use the TSR_KIND_POINTER to represent pointer to a type. Signed-off-by: Zecheng Li --- tools/perf/arch/x86/annotate/instructions.c | 4 ++-- tools/perf/util/annotate-data.c | 6 +++--- tools/perf/util/annotate-data.h | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tools/perf/arch/x86/annotate/instructions.c b/tools/perf/arch/= x86/annotate/instructions.c index c6d403eae744..da98a4e3c52c 100644 --- a/tools/perf/arch/x86/annotate/instructions.c +++ b/tools/perf/arch/x86/annotate/instructions.c @@ -301,7 +301,7 @@ static void update_insn_state_x86(struct type_state *st= ate, * as a pointer. */ tsr->type =3D type_die; - tsr->kind =3D TSR_KIND_POINTER; + tsr->kind =3D TSR_KIND_PERCPU_POINTER; tsr->ok =3D true; =20 pr_debug_dtp("add [%x] percpu %#"PRIx64" -> reg%d", @@ -521,7 +521,7 @@ static void update_insn_state_x86(struct type_state *st= ate, } /* And then dereference the calculated pointer if it has one */ else if (has_reg_type(state, sreg) && state->regs[sreg].ok && - state->regs[sreg].kind =3D=3D TSR_KIND_POINTER && + state->regs[sreg].kind =3D=3D TSR_KIND_PERCPU_POINTER && die_get_member_type(&state->regs[sreg].type, src->offset, &type_die)) { tsr->type =3D type_die; diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-dat= a.c index 258157cc43c2..903027a6fb7d 100644 --- a/tools/perf/util/annotate-data.c +++ b/tools/perf/util/annotate-data.c @@ -58,7 +58,7 @@ void pr_debug_type_name(Dwarf_Die *die, enum type_state_k= ind kind) case TSR_KIND_CONST: pr_info(" constant\n"); return; - case TSR_KIND_POINTER: + case TSR_KIND_PERCPU_POINTER: pr_info(" pointer"); /* it also prints the type info */ break; @@ -591,7 +591,7 @@ void set_stack_state(struct type_state_stack *stack, in= t offset, u8 kind, switch (tag) { case DW_TAG_structure_type: case DW_TAG_union_type: - stack->compound =3D (kind !=3D TSR_KIND_POINTER); + stack->compound =3D (kind !=3D TSR_KIND_PERCPU_POINTER); break; default: stack->compound =3D false; @@ -1116,7 +1116,7 @@ static enum type_match_result check_matching_type(str= uct type_state *state, return PERF_TMR_OK; } =20 - if (state->regs[reg].kind =3D=3D TSR_KIND_POINTER) { + if (state->regs[reg].kind =3D=3D TSR_KIND_PERCPU_POINTER) { pr_debug_dtp("percpu ptr"); =20 /* diff --git a/tools/perf/util/annotate-data.h b/tools/perf/util/annotate-dat= a.h index 541fee1a5f0a..dd3807b55208 100644 --- a/tools/perf/util/annotate-data.h +++ b/tools/perf/util/annotate-data.h @@ -34,7 +34,7 @@ enum type_state_kind { TSR_KIND_TYPE, TSR_KIND_PERCPU_BASE, TSR_KIND_CONST, - TSR_KIND_POINTER, + TSR_KIND_PERCPU_POINTER, TSR_KIND_CANARY, }; =20 --=20 2.51.0.384.g4c02a37b29-goog From nobody Thu Oct 2 10:50:28 2025 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 687C529D292 for ; Wed, 17 Sep 2025 19:58:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758139097; cv=none; b=CyEiDyoy6ECsezq+/+DI0UQAmYqSZPGbvsFg8EybZsf8FzRFjJznHSAdFoD6x7SBHCecFT/Irvv9H7mBY7O8D0qUy1ECmMucgULZweHRNEf1uSmyec0+bcaeus0n+8aLJ8+P23MN9WMP0lK5rWaKIugViSpD+rfGvIziMiFh1is= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758139097; c=relaxed/simple; bh=MX7sOJp4Xx/fUFqamHkOj4G4hWS0EnfUo1xRNws8ENU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=lBZbMBPgE8Ig9b807MVHUHKOEjfgt+T9aRhdErFcHbP7J+TvrjREUatnO3ggNivzLsGpMS4zdsqnQhjlOL6YNF6WmklJLrdykPhFaHAS93CvfdeSCXVBkCaZRdA1z5F28stiGUUmPihaOu5xMGPiOBgzKPj7ZHmHkM3G2BOX2os= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=wnlX8i41; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="wnlX8i41" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e9d7958520bso302151276.3 for ; Wed, 17 Sep 2025 12:58:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1758139094; x=1758743894; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=My5bzXx//Isb0dBX4SeFW0j9qjRvOgbzlew9SX0sll4=; b=wnlX8i41AbqieTHUxNuxfQGP76IldAkd+xJ7pxxFGeTRocLVf98ZwvvQa0wxNQ4A+j cKDBe8PG/mwwqIlREk44N1kvPuUFPlqAHKuuiIgJJFcqJq4WJ3KjpabER90spT3Z4U7q 7MjSfWIEXIt73nL5hDvw8bK5Mfirjo/e0YrBn8uW/tEG5yVdgkGnNW4Z+dkcbpazCgP2 86HyCKzm1H2BtIbjPiVMBn2jRIm9pJ/jSyMIjbwGekPgJgRqR6Nj1jU89ApUghiBvUvc 40Wj1xG3ufx36Eq/EmuMbZZvd1nCwxsKJHGh+VuORzh+/avPefWOHFXefubTx35fILcX +L6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758139094; x=1758743894; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=My5bzXx//Isb0dBX4SeFW0j9qjRvOgbzlew9SX0sll4=; b=s0chqk6sk9Lkfs0WQqE4WF9IwVLsHSEFNrU3S+7rvyV5Z9QYhV3/0x3SrFtlDdrMtg XtfcS6JF8S5eKu+9Y8tjJyH9roFgzrkjyD7MMyVdjMYDIVMjVryUhBhaaIGRidGRogd9 ymliwovJzOW5IIZXdO9YC/XVO9z+gEbIycfKynkk3COaaXAwogACpRW/EuApfssXuPqk vz2E3ryhKujKVlCkPdhli+VnrRXgeCHxMlKfqITve+7yqmByZTGwGkiHARAdPV9EHFPS IiFdGsCtuVM8+1VHknRejXrAhet+9c+A9qy7u2z+0yBRbhD0w4V+A6G5VX5R2d4nR3Rj KSdg== X-Forwarded-Encrypted: i=1; AJvYcCW0i2aS2fF0fQ6WiQO50K/358MjTr/St3vo9YEgy2HoAI6lSuBGltU7+vqPdjktKaKz8f4AW7xMBqvLFfE=@vger.kernel.org X-Gm-Message-State: AOJu0YxBFgD2zs5U98Q6pMoFJsIxHMW195ND6Gk1+tmqrht6fu+TGK36 RIZ+gHemtXUdILsUAq4sJdsa2nAObK+SFyk/jK/ekmlYOnC0CJRwJ7iJ7snPc/7KxuKoQVg4D8K jtiqZVIXD5A== X-Google-Smtp-Source: AGHT+IEu8OOTMqVmRfcrDOmDApehT5zQz5PdkdfjeUVjAf7w8nw79NsZXs59+Egbs0u0fc51ew2ksOUpSzy1 X-Received: from ybbgo8.prod.google.com ([2002:a05:6902:4208:b0:e96:fc8a:7763]) (user=zecheng job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6902:1884:b0:ea4:258:9ee3 with SMTP id 3f1490d57ef6-ea5c03ebbc1mr3361333276.17.1758139094314; Wed, 17 Sep 2025 12:58:14 -0700 (PDT) Date: Wed, 17 Sep 2025 19:58:01 +0000 In-Reply-To: <20250917195808.2514277-1-zecheng@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250917195808.2514277-1-zecheng@google.com> X-Mailer: git-send-email 2.51.0.384.g4c02a37b29-goog Message-ID: <20250917195808.2514277-4-zecheng@google.com> Subject: [PATCH v3 03/10] perf annotate: Track address registers via TSR_KIND_POINTER From: Zecheng Li To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , Masami Hiramatsu Cc: Xu Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Zecheng Li Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce TSR_KIND_POINTER to improve the data type profiler's ability to track pointer-based memory accesses and address register variables. TSR_KIND_POINTER represents a register that holds the address of the type in the `type_state_reg`. The semantics match the `breg` registers that describe a memory location. This change implements handling for this new kind in mov instructions and in the check_matching_type() function. When a TSR_KIND_POINTER is moved to the stack, the stack state size is set to the architecture's pointer size. Signed-off-by: Zecheng Li --- tools/perf/arch/x86/annotate/instructions.c | 19 +++++++- tools/perf/util/annotate-data.c | 52 +++++++++++++++++++-- tools/perf/util/annotate-data.h | 1 + 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/tools/perf/arch/x86/annotate/instructions.c b/tools/perf/arch/= x86/annotate/instructions.c index da98a4e3c52c..698cbb299c6d 100644 --- a/tools/perf/arch/x86/annotate/instructions.c +++ b/tools/perf/arch/x86/annotate/instructions.c @@ -391,7 +391,7 @@ static void update_insn_state_x86(struct type_state *st= ate, tsr->ok =3D true; =20 /* To copy back the variable type later (hopefully) */ - if (tsr->kind =3D=3D TSR_KIND_TYPE) + if (tsr->kind =3D=3D TSR_KIND_TYPE || tsr->kind =3D=3D TSR_KIND_POINTER) tsr->copied_from =3D src->reg1; =20 pr_debug_dtp("mov [%x] reg%d -> reg%d", @@ -418,6 +418,10 @@ static void update_insn_state_x86(struct type_state *s= tate, if (stack =3D=3D NULL) { tsr->ok =3D false; return; + } else if (stack->kind =3D=3D TSR_KIND_POINTER) { + tsr->type =3D stack->type; + tsr->kind =3D stack->kind; + tsr->ok =3D true; } else if (!stack->compound) { tsr->type =3D stack->type; tsr->kind =3D stack->kind; @@ -455,6 +459,19 @@ static void update_insn_state_x86(struct type_state *s= tate, insn_offset, src->offset, sreg, dst->reg1); pr_debug_type_name(&tsr->type, tsr->kind); } + /* Handle dereference of TSR_KIND_POINTER registers */ + else if (has_reg_type(state, sreg) && state->regs[sreg].ok && + state->regs[sreg].kind =3D=3D TSR_KIND_POINTER && + die_get_member_type(&state->regs[sreg].type, + src->offset, &type_die)) { + tsr->type =3D state->regs[sreg].type; + tsr->kind =3D TSR_KIND_TYPE; + tsr->ok =3D true; + + pr_debug_dtp("mov [%x] addr %#x(reg%d) -> reg%d", + insn_offset, src->offset, sreg, dst->reg1); + pr_debug_type_name(&tsr->type, tsr->kind); + } /* Or check if it's a global variable */ else if (sreg =3D=3D DWARF_REG_PC) { struct map_symbol *ms =3D dloc->ms; diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-dat= a.c index 903027a6fb7d..31b5896276f1 100644 --- a/tools/perf/util/annotate-data.c +++ b/tools/perf/util/annotate-data.c @@ -59,6 +59,10 @@ void pr_debug_type_name(Dwarf_Die *die, enum type_state_= kind kind) pr_info(" constant\n"); return; case TSR_KIND_PERCPU_POINTER: + pr_info(" percpu pointer"); + /* it also prints the type info */ + break; + case TSR_KIND_POINTER: pr_info(" pointer"); /* it also prints the type info */ break; @@ -578,7 +582,9 @@ void set_stack_state(struct type_state_stack *stack, in= t offset, u8 kind, int tag; Dwarf_Word size; =20 - if (dwarf_aggregate_size(type_die, &size) < 0) + if (kind =3D=3D TSR_KIND_POINTER) + size =3D 8; + else if (dwarf_aggregate_size(type_die, &size) < 0) size =3D 0; =20 tag =3D dwarf_tag(type_die); @@ -898,13 +904,25 @@ static void update_var_state(struct type_state *state= , struct data_loc_info *dlo =20 reg =3D &state->regs[var->reg]; =20 - /* For gp registers, skip the address registers for now */ - if (var->is_reg_var_addr) + if (reg->ok && reg->kind =3D=3D TSR_KIND_TYPE && + (!is_better_type(®->type, &mem_die) || var->is_reg_var_addr)) continue; =20 - if (reg->ok && reg->kind =3D=3D TSR_KIND_TYPE && - !is_better_type(®->type, &mem_die)) + /* Handle address registers with TSR_KIND_POINTER */ + if (var->is_reg_var_addr) { + if (reg->ok && reg->kind =3D=3D TSR_KIND_POINTER && + !is_better_type(®->type, &mem_die)) + continue; + + reg->type =3D mem_die; + reg->kind =3D TSR_KIND_POINTER; + reg->ok =3D true; + + pr_debug_dtp("var [%"PRIx64"] reg%d addr offset %x", + insn_offset, var->reg, var->offset); + pr_debug_type_name(&mem_die, TSR_KIND_POINTER); continue; + } =20 orig_type =3D reg->type; =20 @@ -1116,6 +1134,30 @@ static enum type_match_result check_matching_type(st= ruct type_state *state, return PERF_TMR_OK; } =20 + if (state->regs[reg].kind =3D=3D TSR_KIND_POINTER) { + struct strbuf sb; + + strbuf_init(&sb, 32); + die_get_typename_from_type(&state->regs[reg].type, &sb); + pr_debug_dtp("(ptr->%s)", sb.buf); + strbuf_release(&sb); + + /* + * Register holds a pointer (address) to the target variable. + * The type is the type of the variable it points to. + */ + *type_die =3D state->regs[reg].type; + + dloc->type_offset =3D dloc->op->offset; + + /* Get the size of the actual type */ + if (dwarf_aggregate_size(type_die, &size) < 0 || + (unsigned)dloc->type_offset >=3D size) + return PERF_TMR_BAD_OFFSET; + + return PERF_TMR_OK; + } + if (state->regs[reg].kind =3D=3D TSR_KIND_PERCPU_POINTER) { pr_debug_dtp("percpu ptr"); =20 diff --git a/tools/perf/util/annotate-data.h b/tools/perf/util/annotate-dat= a.h index dd3807b55208..fd0d1084bc4e 100644 --- a/tools/perf/util/annotate-data.h +++ b/tools/perf/util/annotate-data.h @@ -35,6 +35,7 @@ enum type_state_kind { TSR_KIND_PERCPU_BASE, TSR_KIND_CONST, TSR_KIND_PERCPU_POINTER, + TSR_KIND_POINTER, TSR_KIND_CANARY, }; =20 --=20 2.51.0.384.g4c02a37b29-goog From nobody Thu Oct 2 10:50:28 2025 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8D3942DCF69 for ; Wed, 17 Sep 2025 19:58:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758139098; cv=none; b=K35wRvkt6wWYMcMEsCQwd38Fkb7sRnvs+c/u5Nppdc5BBpbqgXcn/o93Wu66nwOEqCJZAUtvsUOYrlH5fPjAEPTEP3p/RVJClOsFJeGUHXwR0jw6iOjcdpNr+bAKIGcs0NFKB8VfJBcEZV3PuCxWlPYjnROQz+M9gLQ+PdkohN8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758139098; c=relaxed/simple; bh=XCrY+1rGpgecUi0nVjyjs9MUlD/2xdG6C/rkH+vuJYs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=q/n7WNOFhv8jVYYNsAOT4PzasNQKiSyX0WPasX31g4EHk1qHQBZWGEtz09Hkq80v3sJxuhERMrcoQh5gusklrXJv+CDWptlKYEtKSox9i7ImpZmiclKGi0hfSI85NaxNzy68Of9TSMAMsU5oZ9+4U02STXCzjWJdGRz+eMYQnBc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=QlCWkuCJ; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="QlCWkuCJ" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-71e7181cddeso2770207b3.2 for ; Wed, 17 Sep 2025 12:58:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1758139095; x=1758743895; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=8hHW+XAp3lKhCTedfjsNdbKqzalF8NTl2yuxT14sQQ0=; b=QlCWkuCJL6S7eIO8e0JbelLubowypQLh8n3ZhYnSGksqnNRb7TeMGZ22gC8fzeCk2Q b7eIMWSD69qVqCh0AT/OAnAI69fpvIvm3OJ1zdRS00lMLmUGWQx7IHqKkpKj4GRoPdCx 9Y8KouNEV3ikzlhe8lZn156+OmtvzK9sFgT1uTYW82R8JN0LEZsi4bpbFx7W10cE9Ygk g3vUl8CtCMvrVERi23Qd8HLwbPhn3EY1oP9Snfqq0iJO8wf43P7/pXGSI0SIk1VTh/nt 4euh+uu1THIgZ5CL9h7/IzxoCCcSNhmaU0eYYA0CRq0q27YDckMhZ6tOojtkiV69+ueD g7qQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758139095; x=1758743895; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=8hHW+XAp3lKhCTedfjsNdbKqzalF8NTl2yuxT14sQQ0=; b=OBlNPPf90jgYu1c0ZpOalJf3vkMXETXI4TW2tsL9LzIjieEw+e4g56an4qpu7PF6Bw 6NruWMqcduF+3yIMZF7cc6o2HHkmsFviiFHGohAC2D2Ev01rwzZyAtuhnI2SSIm6DrvL OiXfmaTJoZg9WlG7W3UlwVDCIUXbmwmT3eoGUtPUlG7cFFX2amAnYgLWM2+sNGNXhNQm n/jSdCE8B7KGd4Cc8PThmCuWjIiDJSJ3JeZBq2Y/1t691jaD/f0FIMWDLD/XoEOvx8JP dyw8AJPCte1U5bSLd1M47QVKfDcFkdja2K8wDKBzgLKms3hlD7UQhSMztNivmhXANVt8 2+CA== X-Forwarded-Encrypted: i=1; AJvYcCXo+ETzD82Xxg4AgVy8xSklBn8o5DjPk5Qld9Aa29zQjgkSTgyBfJZZVDmL0pIZJy7Bds54yFxJXHdAXg4=@vger.kernel.org X-Gm-Message-State: AOJu0Yw7cfPFpbHnDsP9+EffQgz/0np1mDUIPnqSW1teHNZkyhfha/Be OcbKqErcLfoF4ravIKKYauJZNlcOLAQh1SiJ8St8n2akOl/CPwduYo1gygj6hbmsxDt3VYIwx9P wRJcVTOLjJQ== X-Google-Smtp-Source: AGHT+IE0ago1RspXdbNKJZnBtTNQP8H+m7DjLmXV6pLIDw2hhD6kL8x8NC9PjMJKjLN03yClaD9vqa9Hsskn X-Received: from ywbgq5.prod.google.com ([2002:a05:690c:4445:b0:71f:d0b0:127a]) (user=zecheng job=prod-delivery.src-stubby-dispatcher) by 2002:a05:690c:88:b0:720:631:e778 with SMTP id 00721157ae682-73892a4925emr29091867b3.30.1758139095516; Wed, 17 Sep 2025 12:58:15 -0700 (PDT) Date: Wed, 17 Sep 2025 19:58:02 +0000 In-Reply-To: <20250917195808.2514277-1-zecheng@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250917195808.2514277-1-zecheng@google.com> X-Mailer: git-send-email 2.51.0.384.g4c02a37b29-goog Message-ID: <20250917195808.2514277-5-zecheng@google.com> Subject: [PATCH v3 04/10] perf annotate: Track arithmetic instructions on pointers From: Zecheng Li To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , Masami Hiramatsu Cc: Xu Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Zecheng Li Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Track the arithmetic operations on registers with pointer types. We handle only add, sub and lea instructions. The original pointer information needs to be preserved for getting outermost struct types. For example, reg0 points to a struct cfs_rq, when we add 0x10 to reg0, it should preserve the information of struct cfs_rq + 0x10 in the register instead of a pointer type to the child field at 0x10. Details: 1. struct type_state_reg now includes an offset, indicating if the register points to the start or an internal part of its associated type. This offset is used in mem to reg and reg to stack mem transfers, and also applied to the final type offset. 2. lea offset(%sp/%fp), reg is now treated as taking the address of a stack variable. It worked fine in most cases, but an issue with this approach is the pointer type may not exist. 3. lea offset(%base), reg is handled by moving the type from %base and adding an offset, similar to an add operation followed by a mov reg to reg. 4. Non-stack variables from DWARF with non-zero offsets in their location expressions are now accepted with register offset tracking. Multi-register addressing modes in LEA are not supported. Signed-off-by: Zecheng Li --- tools/perf/arch/x86/annotate/instructions.c | 121 +++++++++++++++++++- tools/perf/util/annotate-data.c | 17 ++- tools/perf/util/annotate-data.h | 6 + 3 files changed, 136 insertions(+), 8 deletions(-) diff --git a/tools/perf/arch/x86/annotate/instructions.c b/tools/perf/arch/= x86/annotate/instructions.c index 698cbb299c6d..cfb07cff8fc8 100644 --- a/tools/perf/arch/x86/annotate/instructions.c +++ b/tools/perf/arch/x86/annotate/instructions.c @@ -248,6 +248,7 @@ static void update_insn_state_x86(struct type_state *st= ate, tsr =3D &state->regs[state->ret_reg]; tsr->type =3D type_die; tsr->kind =3D TSR_KIND_TYPE; + tsr->offset =3D 0; tsr->ok =3D true; =20 pr_debug_dtp("call [%x] return -> reg%d", @@ -284,6 +285,7 @@ static void update_insn_state_x86(struct type_state *st= ate, !strcmp(var_name, "this_cpu_off") && tsr->kind =3D=3D TSR_KIND_CONST) { tsr->kind =3D TSR_KIND_PERCPU_BASE; + tsr->offset =3D 0; tsr->ok =3D true; imm_value =3D tsr->imm_value; } @@ -291,6 +293,16 @@ static void update_insn_state_x86(struct type_state *s= tate, else return; =20 + /* Ignore add to non-pointer types like int */ + if (tsr->kind =3D=3D TSR_KIND_POINTER || + (dwarf_tag(&tsr->type) =3D=3D DW_TAG_pointer_type && + src->reg1 !=3D DWARF_REG_PC && tsr->kind =3D=3D TSR_KIND_TYPE && !d= st->mem_ref)) { + tsr->offset +=3D imm_value; + pr_debug_dtp("add [%x] offset %#"PRIx64" to reg%d", + insn_offset, imm_value, dst->reg1); + pr_debug_type_name(&tsr->type, tsr->kind); + } + if (tsr->kind !=3D TSR_KIND_PERCPU_BASE) return; =20 @@ -302,6 +314,7 @@ static void update_insn_state_x86(struct type_state *st= ate, */ tsr->type =3D type_die; tsr->kind =3D TSR_KIND_PERCPU_POINTER; + tsr->offset =3D 0; tsr->ok =3D true; =20 pr_debug_dtp("add [%x] percpu %#"PRIx64" -> reg%d", @@ -311,6 +324,93 @@ static void update_insn_state_x86(struct type_state *s= tate, return; } =20 + if (!strncmp(dl->ins.name, "sub", 3)) { + u64 imm_value =3D -1ULL; + + if (!has_reg_type(state, dst->reg1)) + return; + + tsr =3D &state->regs[dst->reg1]; + tsr->copied_from =3D -1; + + if (src->imm) + imm_value =3D src->offset; + else if (has_reg_type(state, src->reg1) && + state->regs[src->reg1].kind =3D=3D TSR_KIND_CONST) + imm_value =3D state->regs[src->reg1].imm_value; + + if (tsr->kind =3D=3D TSR_KIND_POINTER || + (dwarf_tag(&tsr->type) =3D=3D DW_TAG_pointer_type && + src->reg1 !=3D DWARF_REG_PC && tsr->kind =3D=3D TSR_KIND_TYPE && !d= st->mem_ref)) { + tsr->offset -=3D imm_value; + pr_debug_dtp("sub [%x] offset %#"PRIx64" to reg%d", + insn_offset, imm_value, dst->reg1); + pr_debug_type_name(&tsr->type, tsr->kind); + } + } + + if (!strncmp(dl->ins.name, "lea", 3)) { + int sreg =3D src->reg1; + struct type_state_reg src_tsr; + + if (!has_reg_type(state, sreg) || + !has_reg_type(state, dst->reg1) || + !src->mem_ref) + return; + + src_tsr =3D state->regs[sreg]; + tsr =3D &state->regs[dst->reg1]; + + tsr->copied_from =3D -1; + tsr->ok =3D false; + + /* Case 1: Based on stack pointer or frame pointer */ + if (sreg =3D=3D fbreg || sreg =3D=3D state->stack_reg) { + struct type_state_stack *stack; + int offset =3D src->offset - fboff; + + stack =3D find_stack_state(state, offset); + if (!stack) + return; + + tsr->type =3D stack->type; + tsr->kind =3D TSR_KIND_POINTER; + tsr->offset =3D offset - stack->offset; + tsr->ok =3D true; + + if (sreg =3D=3D fbreg) { + pr_debug_dtp("lea [%x] address of -%#x(stack) -> reg%d", + insn_offset, -src->offset, dst->reg1); + } else { + pr_debug_dtp("lea [%x] address of %#x(reg%d) -> reg%d", + insn_offset, src->offset, sreg, dst->reg1); + } + + pr_debug_type_name(&tsr->type, tsr->kind); + } + /* Case 2: Based on a register holding a typed pointer */ + else if (src_tsr.ok && src_tsr.kind =3D=3D TSR_KIND_TYPE) { + + /* Check if the target type has a member at the new offset */ + if (__die_get_real_type(&state->regs[sreg].type, &type_die) =3D=3D NULL= || + die_get_member_type(&type_die, + src->offset + src_tsr.offset, &type_die) =3D=3D NULL) + return; + + tsr->type =3D src_tsr.type; + tsr->kind =3D TSR_KIND_TYPE; + tsr->offset =3D src->offset + src_tsr.offset; + tsr->ok =3D true; + + pr_debug_dtp("lea [%x] address of %s%#x(reg%d) -> reg%d", + insn_offset, src->offset < 0 ? "-" : "", + abs(src->offset), sreg, dst->reg1); + + pr_debug_type_name(&tsr->type, tsr->kind); + } + return; + } + if (strncmp(dl->ins.name, "mov", 3)) return; =20 @@ -345,6 +445,7 @@ static void update_insn_state_x86(struct type_state *st= ate, =20 if (var_addr =3D=3D 40) { tsr->kind =3D TSR_KIND_CANARY; + tsr->offset =3D 0; tsr->ok =3D true; =20 pr_debug_dtp("mov [%x] stack canary -> reg%d\n", @@ -361,6 +462,7 @@ static void update_insn_state_x86(struct type_state *st= ate, =20 tsr->type =3D type_die; tsr->kind =3D TSR_KIND_TYPE; + tsr->offset =3D 0; tsr->ok =3D true; =20 pr_debug_dtp("mov [%x] this-cpu addr=3D%#"PRIx64" -> reg%d", @@ -372,6 +474,7 @@ static void update_insn_state_x86(struct type_state *st= ate, if (src->imm) { tsr->kind =3D TSR_KIND_CONST; tsr->imm_value =3D src->offset; + tsr->offset =3D 0; tsr->ok =3D true; =20 pr_debug_dtp("mov [%x] imm=3D%#x -> reg%d\n", @@ -388,6 +491,7 @@ static void update_insn_state_x86(struct type_state *st= ate, tsr->type =3D state->regs[src->reg1].type; tsr->kind =3D state->regs[src->reg1].kind; tsr->imm_value =3D state->regs[src->reg1].imm_value; + tsr->offset =3D state->regs[src->reg1].offset; tsr->ok =3D true; =20 /* To copy back the variable type later (hopefully) */ @@ -421,16 +525,19 @@ static void update_insn_state_x86(struct type_state *= state, } else if (stack->kind =3D=3D TSR_KIND_POINTER) { tsr->type =3D stack->type; tsr->kind =3D stack->kind; + tsr->offset =3D 0; tsr->ok =3D true; } else if (!stack->compound) { tsr->type =3D stack->type; tsr->kind =3D stack->kind; + tsr->offset =3D 0; tsr->ok =3D true; } else if (die_get_member_type(&stack->type, offset - stack->offset, &type_die)) { tsr->type =3D type_die; tsr->kind =3D TSR_KIND_TYPE; + tsr->offset =3D 0; tsr->ok =3D true; } else { tsr->ok =3D false; @@ -450,9 +557,10 @@ static void update_insn_state_x86(struct type_state *s= tate, else if (has_reg_type(state, sreg) && state->regs[sreg].ok && state->regs[sreg].kind =3D=3D TSR_KIND_TYPE && die_deref_ptr_type(&state->regs[sreg].type, - src->offset, &type_die)) { + src->offset + state->regs[sreg].offset, &type_die)) { tsr->type =3D type_die; tsr->kind =3D TSR_KIND_TYPE; + tsr->offset =3D 0; tsr->ok =3D true; =20 pr_debug_dtp("mov [%x] %#x(reg%d) -> reg%d", @@ -463,9 +571,10 @@ static void update_insn_state_x86(struct type_state *s= tate, else if (has_reg_type(state, sreg) && state->regs[sreg].ok && state->regs[sreg].kind =3D=3D TSR_KIND_POINTER && die_get_member_type(&state->regs[sreg].type, - src->offset, &type_die)) { + src->offset + state->regs[sreg].offset, &type_die)) { tsr->type =3D state->regs[sreg].type; tsr->kind =3D TSR_KIND_TYPE; + tsr->offset =3D src->offset + state->regs[sreg].offset; tsr->ok =3D true; =20 pr_debug_dtp("mov [%x] addr %#x(reg%d) -> reg%d", @@ -490,6 +599,7 @@ static void update_insn_state_x86(struct type_state *st= ate, =20 tsr->type =3D type_die; tsr->kind =3D TSR_KIND_TYPE; + tsr->offset =3D 0; tsr->ok =3D true; =20 pr_debug_dtp("mov [%x] global addr=3D%"PRIx64" -> reg%d", @@ -521,6 +631,7 @@ static void update_insn_state_x86(struct type_state *st= ate, die_get_member_type(&type_die, offset, &type_die)) { tsr->type =3D type_die; tsr->kind =3D TSR_KIND_TYPE; + tsr->offset =3D 0; tsr->ok =3D true; =20 if (src->multi_regs) { @@ -543,6 +654,7 @@ static void update_insn_state_x86(struct type_state *st= ate, src->offset, &type_die)) { tsr->type =3D type_die; tsr->kind =3D TSR_KIND_TYPE; + tsr->offset =3D 0; tsr->ok =3D true; =20 pr_debug_dtp("mov [%x] pointer %#x(reg%d) -> reg%d", @@ -565,6 +677,7 @@ static void update_insn_state_x86(struct type_state *st= ate, &var_name, &offset) && !strcmp(var_name, "__per_cpu_offset")) { tsr->kind =3D TSR_KIND_PERCPU_BASE; + tsr->offset =3D 0; tsr->ok =3D true; =20 pr_debug_dtp("mov [%x] percpu base reg%d\n", @@ -613,6 +726,10 @@ static void update_insn_state_x86(struct type_state *s= tate, pr_debug_dtp("mov [%x] reg%d -> %#x(reg%d)", insn_offset, src->reg1, offset, dst->reg1); } + if (tsr->offset !=3D 0) + pr_debug_dtp(" reg%d offset %#x ->", + src->reg1, tsr->offset); + pr_debug_type_name(&tsr->type, tsr->kind); } /* diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-dat= a.c index 31b5896276f1..6ca5489f3c4c 100644 --- a/tools/perf/util/annotate-data.c +++ b/tools/perf/util/annotate-data.c @@ -898,7 +898,7 @@ static void update_var_state(struct type_state *state, = struct data_loc_info *dlo insn_offset, -offset); } pr_debug_type_name(&mem_die, TSR_KIND_TYPE); - } else if (has_reg_type(state, var->reg) && var->offset =3D=3D 0) { + } else if (has_reg_type(state, var->reg)) { struct type_state_reg *reg; Dwarf_Die orig_type; =20 @@ -914,6 +914,7 @@ static void update_var_state(struct type_state *state, = struct data_loc_info *dlo !is_better_type(®->type, &mem_die)) continue; =20 + reg->offset =3D -var->offset; reg->type =3D mem_die; reg->kind =3D TSR_KIND_POINTER; reg->ok =3D true; @@ -925,13 +926,17 @@ static void update_var_state(struct type_state *state= , struct data_loc_info *dlo } =20 orig_type =3D reg->type; - + /* + * var->offset + reg value is the beginning of the struct + * reg->offset is the offset the reg points + */ + reg->offset =3D -var->offset; reg->type =3D mem_die; reg->kind =3D TSR_KIND_TYPE; reg->ok =3D true; =20 - pr_debug_dtp("var [%"PRIx64"] reg%d", - insn_offset, var->reg); + pr_debug_dtp("var [%"PRIx64"] reg%d offset %x", + insn_offset, var->reg, var->offset); pr_debug_type_name(&mem_die, TSR_KIND_TYPE); =20 /* @@ -1119,7 +1124,7 @@ static enum type_match_result check_matching_type(str= uct type_state *state, if (__die_get_real_type(&state->regs[reg].type, type_die) =3D=3D NULL) return PERF_TMR_NO_POINTER; =20 - dloc->type_offset =3D dloc->op->offset; + dloc->type_offset =3D dloc->op->offset + state->regs[reg].offset; =20 if (dwarf_tag(type_die) =3D=3D DW_TAG_typedef) die_get_real_type(type_die, &sized_type); @@ -1148,7 +1153,7 @@ static enum type_match_result check_matching_type(str= uct type_state *state, */ *type_die =3D state->regs[reg].type; =20 - dloc->type_offset =3D dloc->op->offset; + dloc->type_offset =3D dloc->op->offset + state->regs[reg].offset; =20 /* Get the size of the actual type */ if (dwarf_aggregate_size(type_die, &size) < 0 || diff --git a/tools/perf/util/annotate-data.h b/tools/perf/util/annotate-dat= a.h index fd0d1084bc4e..20237e7e4e2f 100644 --- a/tools/perf/util/annotate-data.h +++ b/tools/perf/util/annotate-data.h @@ -174,6 +174,12 @@ extern struct annotated_data_stat ann_data_stat; struct type_state_reg { Dwarf_Die type; u32 imm_value; + /* + * The offset within the struct that the register points to. + * A value of 0 means the register points to the beginning. + * type_offset =3D op->offset + reg->offset + */ + s32 offset; bool ok; bool caller_saved; u8 kind; --=20 2.51.0.384.g4c02a37b29-goog From nobody Thu Oct 2 10:50:28 2025 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E9A402F5A3A for ; Wed, 17 Sep 2025 19:58:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758139099; cv=none; b=SEIOePWRoZSUqgq4+/PpvlLXOei7qOeZ57dr2BFnk5MeFtlSrcf5TUOc1U23CgfG8h1TsKLWopdjyu24SlSTqe8Hw6GMgTghmV9i4vvgkMY+Ijh+23KXJ2l2TVgbNGBsQv+ZTDMFkJcHpvKPY/r1Y2I2Ag6lJ2eOfra2zOs059E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758139099; c=relaxed/simple; bh=2MXbAv0kCGgjdw/se833dTtc0f43qdPxo31NoZAjt3g=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=SSwT8DTy07YuCXXzN2FZtFi8jNSVM/DjUhaJOfg1zBzYIkktc+fLJPnlkXHlA40xT3nHM07kwPRpNXZ4uxcVQfyt10B5D4fvibW+NTIBG7KnFQdw2C7s0uO1kDVodxDaKjzyjBk5zQZom+6JkgsojZeDrbUDZJYj5PSebQRGsvA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=JhZ6qRlZ; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="JhZ6qRlZ" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-733ff1a73dcso4154117b3.1 for ; Wed, 17 Sep 2025 12:58:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1758139097; x=1758743897; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=HnN3JvUSgCj73ym6XHuqdmgq/i5u2M7eJ3+csln+azY=; b=JhZ6qRlZ4fgb5+RrvrgdZFkTOEK/xrVxUS+vqfxgiCn9Qf26G+obog79leSewLwjyD aNwvUffG81GO9W+6KtQ4CWHLxQ2TLPY9uwmcMlkjjCKROAJ3bKcqL49jqnbrQTDnG+cp J6qyEiH5vfu0ieJEyKHuGoWjK8BQ+SHqcZBdMiHb0AyQ4zAuPYKQuRIozS27Tfq0uc4U KXmBcYk3ct+gpy3Cawz+EeJRyq0CZbVsk2UjsRPsMVfGce9gvYHnBB0GvwXIadZvs87H i++MeGozKqmEHPdv07AN201/3wpixwJlohcAZzF+VQcCxDQSCYcx9h5ar7Qb6HRJw2jv /HEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758139097; x=1758743897; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=HnN3JvUSgCj73ym6XHuqdmgq/i5u2M7eJ3+csln+azY=; b=vX8cfJ2/7FPCri3yZAQ62pTch09gX+Asc/VigDySGlQ4qvZsfeZPy8Rc1MHb/0702k KeseP/MEW2sVvz3ZYzDmgEKBllvrcHlpGXT/HH5pqDwX+lqd0asg1surKQYCBCTK8wv9 OylgtXk/1zgDDLJp94Oy+NfUrltvqgxUgDCNH3/u3LWn+nYtaYGp4TL3/RXwIXnPQz8/ o/AvmnECYpaFHlkz7H7PK753V216tnyUcYs2OqZsNYctcSlGDai//AP8tBxNCCiio8Mv CYc/uXbn90DYIgwnjcDKzoq6wWuJ34q6Gc1eIGk96vKhamIJELaKlvk1r7HhRbIqku3J Ez6w== X-Forwarded-Encrypted: i=1; AJvYcCUBtb/hfDlfRDYIXYgCkoevHmBA/Qrky5nNQLnnx5iHVj+0+mO500uklDJ+KQMld7jxId3aA4sN1RnJuJI=@vger.kernel.org X-Gm-Message-State: AOJu0Yy+3fy/UppVUQJjTuQ3TNOCPcQ56JRg5BMUrK1I3tjl27tmSHtC 5Og+zY/K3mtrVzMdf165Z0jfRzn6aqOifSuw1qSJdTECINL/0AEDcmATPsh2aOpyRORowefbbpF 1o7+xx6BvxQ== X-Google-Smtp-Source: AGHT+IHj7+GPPv+UYNJ9mWbDPAecxGy50S64SheNETQVT5qX2NhQt11owM2BprcVkbQpGKTl/HJRyyXkVOjo X-Received: from ywbii6.prod.google.com ([2002:a05:690c:6a86:b0:723:af81:5294]) (user=zecheng job=prod-delivery.src-stubby-dispatcher) by 2002:a05:690c:7486:b0:733:2cbb:7afc with SMTP id 00721157ae682-73890babf9amr25703647b3.6.1758139096815; Wed, 17 Sep 2025 12:58:16 -0700 (PDT) Date: Wed, 17 Sep 2025 19:58:03 +0000 In-Reply-To: <20250917195808.2514277-1-zecheng@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250917195808.2514277-1-zecheng@google.com> X-Mailer: git-send-email 2.51.0.384.g4c02a37b29-goog Message-ID: <20250917195808.2514277-6-zecheng@google.com> Subject: [PATCH v3 05/10] perf annotate: Save pointer offset in stack state From: Zecheng Li To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , Masami Hiramatsu Cc: Xu Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Zecheng Li Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The tracked pointer offset was not being preserved in the stack state, which could lead to incorrect type analysis. This change adds a ptr_offset field to the type_state_stack struct and passes it to set_stack_state and findnew_stack_state to ensure the offset is preserved after the pointer is loaded from a stack location. It improves the type annotation coverage and quality. Signed-off-by: Zecheng Li --- tools/perf/arch/x86/annotate/instructions.c | 8 ++++---- tools/perf/util/annotate-data.c | 12 +++++++----- tools/perf/util/annotate-data.h | 7 +++++-- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/tools/perf/arch/x86/annotate/instructions.c b/tools/perf/arch/= x86/annotate/instructions.c index cfb07cff8fc8..709c6f7efe82 100644 --- a/tools/perf/arch/x86/annotate/instructions.c +++ b/tools/perf/arch/x86/annotate/instructions.c @@ -525,12 +525,12 @@ static void update_insn_state_x86(struct type_state *= state, } else if (stack->kind =3D=3D TSR_KIND_POINTER) { tsr->type =3D stack->type; tsr->kind =3D stack->kind; - tsr->offset =3D 0; + tsr->offset =3D stack->ptr_offset; tsr->ok =3D true; } else if (!stack->compound) { tsr->type =3D stack->type; tsr->kind =3D stack->kind; - tsr->offset =3D 0; + tsr->offset =3D stack->ptr_offset; tsr->ok =3D true; } else if (die_get_member_type(&stack->type, offset - stack->offset, @@ -713,10 +713,10 @@ static void update_insn_state_x86(struct type_state *= state, */ if (!stack->compound) set_stack_state(stack, offset, tsr->kind, - &tsr->type); + &tsr->type, tsr->offset); } else { findnew_stack_state(state, offset, tsr->kind, - &tsr->type); + &tsr->type, tsr->offset); } =20 if (dst->reg1 =3D=3D fbreg) { diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-dat= a.c index 6ca5489f3c4c..68c69d343bff 100644 --- a/tools/perf/util/annotate-data.c +++ b/tools/perf/util/annotate-data.c @@ -577,7 +577,7 @@ struct type_state_stack *find_stack_state(struct type_s= tate *state, } =20 void set_stack_state(struct type_state_stack *stack, int offset, u8 kind, - Dwarf_Die *type_die) + Dwarf_Die *type_die, int ptr_offset) { int tag; Dwarf_Word size; @@ -592,6 +592,7 @@ void set_stack_state(struct type_state_stack *stack, in= t offset, u8 kind, stack->type =3D *type_die; stack->size =3D size; stack->offset =3D offset; + stack->ptr_offset =3D ptr_offset; stack->kind =3D kind; =20 switch (tag) { @@ -607,18 +608,19 @@ void set_stack_state(struct type_state_stack *stack, = int offset, u8 kind, =20 struct type_state_stack *findnew_stack_state(struct type_state *state, int offset, u8 kind, - Dwarf_Die *type_die) + Dwarf_Die *type_die, + int ptr_offset) { struct type_state_stack *stack =3D find_stack_state(state, offset); =20 if (stack) { - set_stack_state(stack, offset, kind, type_die); + set_stack_state(stack, offset, kind, type_die, ptr_offset); return stack; } =20 stack =3D malloc(sizeof(*stack)); if (stack) { - set_stack_state(stack, offset, kind, type_die); + set_stack_state(stack, offset, kind, type_die, ptr_offset); list_add(&stack->list, &state->stack_vars); } return stack; @@ -888,7 +890,7 @@ static void update_var_state(struct type_state *state, = struct data_loc_info *dlo continue; =20 findnew_stack_state(state, offset, TSR_KIND_TYPE, - &mem_die); + &mem_die, /*ptr_offset=3D*/0); =20 if (var->reg =3D=3D state->stack_reg) { pr_debug_dtp("var [%"PRIx64"] %#x(reg%d)", diff --git a/tools/perf/util/annotate-data.h b/tools/perf/util/annotate-dat= a.h index 20237e7e4e2f..e1e9c5f6915a 100644 --- a/tools/perf/util/annotate-data.h +++ b/tools/perf/util/annotate-data.h @@ -191,6 +191,8 @@ struct type_state_stack { struct list_head list; Dwarf_Die type; int offset; + /* pointer offset, saves tsr->offset on the stack state */ + int ptr_offset; int size; bool compound; u8 kind; @@ -244,9 +246,10 @@ int annotated_data_type__get_member_name(struct annota= ted_data_type *adt, bool has_reg_type(struct type_state *state, int reg); struct type_state_stack *findnew_stack_state(struct type_state *state, int offset, u8 kind, - Dwarf_Die *type_die); + Dwarf_Die *type_die, + int ptr_offset); void set_stack_state(struct type_state_stack *stack, int offset, u8 kind, - Dwarf_Die *type_die); + Dwarf_Die *type_die, int ptr_offset); struct type_state_stack *find_stack_state(struct type_state *state, int offset); bool get_global_var_type(Dwarf_Die *cu_die, struct data_loc_info *dloc, --=20 2.51.0.384.g4c02a37b29-goog From nobody Thu Oct 2 10:50:28 2025 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C77332F7465 for ; Wed, 17 Sep 2025 19:58:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758139100; cv=none; b=VCYDHVXPV+W2R4GlJy/sucLiGJGwlGhmzjTAZsehvP5j/aWCmdFixUDiYlrj+881iU2ZfSzKgAs4rVidejlliuIdoIA8HWuNG61iNGysWumSj2LpI6+umijIfuMusA3MAPwEZN2jvGyIbxNCjeTwLmtD59cWQ3IX/r8JhnAv/NM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758139100; c=relaxed/simple; bh=aIrH08/y/Z/lAxV9n78mPynZXsloFUItlzemt8D+ugs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fQ4WcRl4SCp5jbeLpB6H/FqD+8jX3hwRvdfPXaSk/rKwjAM7sjpKKhe4jgnLw0cByKxe3F398P2Qd1PCofXCV0IxOz3yzXXvB3enYxjb+mwesv7J5RCXCheb4ZENdSMTWWk4cFNkanFxQfS8U+ncMPUZ2B8AbdfOuI2UsWMGYvo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=DKSSt5QW; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="DKSSt5QW" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-73934dd7a22so3603027b3.1 for ; Wed, 17 Sep 2025 12:58:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1758139098; x=1758743898; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=t3P6BaHAlTLMS5IFeUPZbOJc6tY1Tw9ZwhFGASaPvcU=; b=DKSSt5QWroHxZXXs7JbkfPbwvFHZa5uMziZkWeS74l7jJAMVXcTp9QQttE5ZFhhFSo ieujNqaMWMbuSP7SxXDiagWbTUHg0R8Tt3Xttb9MLp6x3HL0OLfOHwkfiQ8uBdvySgXZ pDpRojTmWGd4/djCEG11jKU8eCRinXHl0wYL2lxvTpSbiwSR9/sHOS4kc13vqVzkJS3K FSbZJIgEjWBOadJFJU6cR0CVQFGdyLisJVaVso0d9nMSzUR2cB4RB8qMXR7HUJ6d18OD /CMjX29/n57PXKXaf9h3ue3qCzjg14uJP/49t0dVipdjAMH1fAk2xo5zCA8hvLhYPSfi JsJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758139098; x=1758743898; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=t3P6BaHAlTLMS5IFeUPZbOJc6tY1Tw9ZwhFGASaPvcU=; b=nVOSJh99v1b70Y27wBh2V+2K4vU2ft6ukbwlNHxIrQ9RMMEaSY7RGhmAQ2b96pRG0o GE+qAPpPSX9kFG9KHkphpkTb9d54q/saIiSMZXBx79fqc0m5io2LGwYUU/Uz1LXIDxuK +9dgA+FMn/LzDrlaELNRthKal9acKBFVHGZGtYQlp7bK+JVJ1ARA1sm8ftJvaJ/nuruI 5HlL/eka8QwY9OudoQbbcT2MOp0u7AtLMscifqQMAP9ReSLBf9s+02zhap93BW3hD7k4 N3rLkRvuVNtxjtf3ARYPSt44bMBl6ETapo+bUBa32a7CUNDid9FLStQyvKbRpXP6IFgb tUkw== X-Forwarded-Encrypted: i=1; AJvYcCV4zBPma/A82N/Shga9mTwBvtBci85mUFt+rjSvgtYcalBqTIKzJsq5CXn0R0N8W1ACXbpldUhiLze6bCs=@vger.kernel.org X-Gm-Message-State: AOJu0YwuOkG6iK+Wct8ScNT5AtrSpagmOjphr8avzY9BTaGPjVPs1ygS IooV1Cu+SFR23AE6+gNWFrsnsSCO+lhfYwVCLCkY9WEzQmwTt7btsrUazk0SFjkjH9lkCMvlSsn ij/JraizuYQ== X-Google-Smtp-Source: AGHT+IGDVHCwR/ebkkI0VupWyBime1xijFWaHj0znQ9HxK/8v7g94HpHXf9Wicz8r0Su7cYSlnTECY0TYxAG X-Received: from ywv5.prod.google.com ([2002:a05:690c:9b05:b0:71b:ff19:8cf5]) (user=zecheng job=prod-delivery.src-stubby-dispatcher) by 2002:a05:690c:6483:b0:729:ad49:dafb with SMTP id 00721157ae682-738920668a5mr33365047b3.33.1758139097843; Wed, 17 Sep 2025 12:58:17 -0700 (PDT) Date: Wed, 17 Sep 2025 19:58:04 +0000 In-Reply-To: <20250917195808.2514277-1-zecheng@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250917195808.2514277-1-zecheng@google.com> X-Mailer: git-send-email 2.51.0.384.g4c02a37b29-goog Message-ID: <20250917195808.2514277-7-zecheng@google.com> Subject: [PATCH v3 06/10] perf annotate: Invalidate register states for untracked instructions From: Zecheng Li To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , Masami Hiramatsu Cc: Xu Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Zecheng Li Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When tracking variable types, instructions that modify a pointer value in an untracked way can lead to incorrect type propagation. To prevent this, invalidate the register state when encountering such instructions. This change invalidates pointer types for various arithmetic and bitwise operations that current pointer offset tracking doesn't support, like imul, shl, and, inc, etc. A special case is added for 'xor reg, reg', which is a common idiom for zeroing a register. For this, the register state is updated to be a constant with a value of 0. This could introduce slight regressions if a variable is zeroed and then reused. This can be addressed in the future by using all DWARF locations for instruction tracking instead of only the first one. Signed-off-by: Zecheng Li --- tools/perf/arch/x86/annotate/instructions.c | 29 +++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tools/perf/arch/x86/annotate/instructions.c b/tools/perf/arch/= x86/annotate/instructions.c index 709c6f7efe82..3c98f72c423f 100644 --- a/tools/perf/arch/x86/annotate/instructions.c +++ b/tools/perf/arch/x86/annotate/instructions.c @@ -411,6 +411,35 @@ static void update_insn_state_x86(struct type_state *s= tate, return; } =20 + /* Invalidate register states for other ops which may change pointers */ + if (has_reg_type(state, dst->reg1) && !dst->mem_ref && + dwarf_tag(&state->regs[dst->reg1].type) =3D=3D DW_TAG_pointer_type) { + if (!strncmp(dl->ins.name, "imul", 4) || !strncmp(dl->ins.name, "mul", 3= ) || + !strncmp(dl->ins.name, "idiv", 4) || !strncmp(dl->ins.name, "div", 3= ) || + !strncmp(dl->ins.name, "shl", 3) || !strncmp(dl->ins.name, "shr", 3= ) || + !strncmp(dl->ins.name, "sar", 3) || !strncmp(dl->ins.name, "and", 3= ) || + !strncmp(dl->ins.name, "or", 2) || !strncmp(dl->ins.name, "neg", 3= ) || + !strncmp(dl->ins.name, "inc", 3) || !strncmp(dl->ins.name, "dec", 3= )) { + pr_debug_dtp("%s [%x] invalidate reg%d\n", + dl->ins.name, insn_offset, dst->reg1); + state->regs[dst->reg1].ok =3D false; + state->regs[dst->reg1].copied_from =3D -1; + return; + } + + if (!strncmp(dl->ins.name, "xor", 3) && dst->reg1 =3D=3D src->reg1) { + /* xor reg, reg clears the register */ + pr_debug_dtp("xor [%x] clear reg%d\n", + insn_offset, dst->reg1); + + state->regs[dst->reg1].kind =3D TSR_KIND_CONST; + state->regs[dst->reg1].imm_value =3D 0; + state->regs[dst->reg1].ok =3D false; + state->regs[dst->reg1].copied_from =3D -1; + return; + } + } + if (strncmp(dl->ins.name, "mov", 3)) return; =20 --=20 2.51.0.384.g4c02a37b29-goog From nobody Thu Oct 2 10:50:28 2025 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E39842F5A24 for ; Wed, 17 Sep 2025 19:58:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758139101; cv=none; b=nBHZx6NFcrYVLiuDxKxAzrbgKDdnxveendi7o/33C14nRme5iQDsH3dNcuOvr3lF6FHjM01IQ39BTj8gXOLUGp99lApkyX9t8A8m7IuJWWEH6/gGy5TcByEfq/I1POBh2kbEzw8/J2MdsHXllSdMutNP/HS40SPuZ44rVAuDRkk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758139101; c=relaxed/simple; bh=fB/6i1qanI6SJs8QAzxHrArcw2uiQzagwE/LnBHWUVc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=r6t353qcg5Pj4YxQE4yai67NjWJbCvdKsDDSyv0F1DhRPc+ROruvwwHILIe2adfok4uyuYYRUnmJ1QIkY0j40l19Of0zigoOKokMcXuRLwIxddyuOqhj7nnRZCjVEkZdUHGguFBP8ttUg6ivQosLuJDoZnkWFhkAvFNJqeBRJYs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=foHAGZAb; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="foHAGZAb" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e94dfb23622so311313276.3 for ; Wed, 17 Sep 2025 12:58:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1758139099; x=1758743899; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=T4aZ86fc6szpTU0Yd+pCMzTGqFuiPjIauERb4YlvtW0=; b=foHAGZAbvdXHuRjqYhY+kDfrJdj9PvmEaopotQ8J7innL0lBwwNIt/BIOwmNV6OJqj G3uLkwjheHc/pNeXk29NEjCubXv8XtXc93E+5gEFNvgYT5VqSZL3ezMptOXRtUwFWM3n 7I1SNemcoaxHZhxkNAnvpTua2uBCi8wFRGURxctrAvIinkccP15aP2N5Sp2nDbgX/BHP K56CHewbvyvRfGwPl9C2ZgaVB42e2yFnWE4ftqMpnAN2RW2MTu3SrnS9d0wGFlwM+8Z8 UloFFUZ7+x1s1jllP+h3iJKLw8axbpg9cgdnSE8+bAKwia7zqdTnO1XziCDKBc95WaOO 8vvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758139099; x=1758743899; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=T4aZ86fc6szpTU0Yd+pCMzTGqFuiPjIauERb4YlvtW0=; b=lmbBzk5itDAvQ6fDG4Mj1dZe9XA75HEqWytbsA5iCAYs7VF9t6n896W8y9ct1OCdBZ 6CgNUaByJQ5lYyVCLCDG3icncnXtO4TSXkgntWljRY8eI2WZaTM7q0qwOMZYKjUlPkvr oRKNkWAaYwefNbaJWkLmDDh227VxrAW6FWo7QJ3jHY20g5Oq6X66hwy7VsNWlevTMb5D v5bFF1ElgGyf/I2H7RF5nx1eXdd6laRhMSLpPlcJlUOS++FXJrKuFHbYu/IMblosAtT/ t9Sv7NMAjqYxayo4Z7waO6lJd+BWpe0WgIJY77bHJJNNodU7l/RWJ8a+1mdG9ZiHQz0d ufNw== X-Forwarded-Encrypted: i=1; AJvYcCXNoCHSXUWSYpGXtLimNVgk275texpHIGYYakRs/JeoyNDvbKpYXUYnWfRmr6l8PVnHWuwXSjl6iQqXOD4=@vger.kernel.org X-Gm-Message-State: AOJu0Yy4xdXfByEwA4XknEToBobLgeis6WzdN9gJ4oHQ4qF69rpy93F/ Jv4SYCglgwm+duTyrOSQPd5ihxEV2QmAg/Di/zQed1lbpEkCJZR7cu/S9WWqZAjea5Fg2l5reC4 MDp4nfwvnFQ== X-Google-Smtp-Source: AGHT+IFbRYoLbefNIf9LIPamUpqMmx0NkDKL+ovOKnmXT2DhvKlRz6nOoq9G+z1EqrxiE2xu8RBFvItx5bky X-Received: from ybey13.prod.google.com ([2002:a25:dc0d:0:b0:ea5:cbb5:9bb9]) (user=zecheng job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6902:1884:b0:ea5:ce42:68c0 with SMTP id 3f1490d57ef6-ea5ce427a12mr812601276.42.1758139098955; Wed, 17 Sep 2025 12:58:18 -0700 (PDT) Date: Wed, 17 Sep 2025 19:58:05 +0000 In-Reply-To: <20250917195808.2514277-1-zecheng@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250917195808.2514277-1-zecheng@google.com> X-Mailer: git-send-email 2.51.0.384.g4c02a37b29-goog Message-ID: <20250917195808.2514277-8-zecheng@google.com> Subject: [PATCH v3 07/10] perf dwarf-aux: Skip check_variable for die_find_variable_by_reg From: Zecheng Li To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , Masami Hiramatsu Cc: Xu Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Zecheng Li Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In die_find_variable_by_reg, match_var_offset already performs sufficient checking and type matching. The additional check_variable call is redundant, and its need_pointer logic is only a heuristic. Since DWARF encodes accurate type information, which match_var_offset verifies, skipping check_variable improves both coverage and accuracy. Return type from die_find_variable_by_reg via a new `type` field in find_var_data. Signed-off-by: Zecheng Li --- tools/perf/util/annotate-data.c | 8 +++++--- tools/perf/util/dwarf-aux.c | 18 +++++++++++------- tools/perf/util/dwarf-aux.h | 2 +- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-dat= a.c index 68c69d343bff..7e4c045d0f4d 100644 --- a/tools/perf/util/annotate-data.c +++ b/tools/perf/util/annotate-data.c @@ -1596,19 +1596,21 @@ static int find_data_type_die(struct data_loc_info = *dloc, Dwarf_Die *type_die) if (!die_find_variable_by_addr(&scopes[i], dloc->var_addr, &var_die, &type_offset)) continue; + /* Found a variable, see if it's correct */ + result =3D check_variable(dloc, &var_die, &mem_die, reg, + type_offset, is_fbreg); } else { /* Look up variables/parameters in this scope */ if (!die_find_variable_by_reg(&scopes[i], pc, reg, - &type_offset, is_fbreg, &var_die)) + &mem_die, &type_offset, is_fbreg, &var_die)) continue; + result =3D PERF_TMR_OK; } =20 pr_debug_dtp("found \"%s\" (die: %#lx) in scope=3D%d/%d (die: %#lx) ", dwarf_diename(&var_die), (long)dwarf_dieoffset(&var_die), i+1, nr_scopes, (long)dwarf_dieoffset(&scopes[i])); =20 - /* Found a variable, see if it's correct */ - result =3D check_variable(dloc, &var_die, &mem_die, reg, type_offset, is= _fbreg); if (result =3D=3D PERF_TMR_OK) { if (reg =3D=3D DWARF_REG_PC) { pr_debug_dtp("addr=3D%#"PRIx64" type_offset=3D%#x\n", diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c index 9267af204c7d..b57cdc8860f0 100644 --- a/tools/perf/util/dwarf-aux.c +++ b/tools/perf/util/dwarf-aux.c @@ -1378,6 +1378,8 @@ struct find_var_data { Dwarf_Addr addr; /* Target register */ unsigned reg; + /* Access data type */ + Dwarf_Die type; /* Access offset, set for global data */ int offset; /* True if the current register is the frame base */ @@ -1390,7 +1392,6 @@ struct find_var_data { static bool match_var_offset(Dwarf_Die *die_mem, struct find_var_data *dat= a, s64 addr_offset, s64 addr_type, bool is_pointer) { - Dwarf_Die type_die; Dwarf_Word size; s64 offset =3D addr_offset - addr_type; =20 @@ -1403,16 +1404,16 @@ static bool match_var_offset(Dwarf_Die *die_mem, st= ruct find_var_data *data, if (offset < 0) return false; =20 - if (die_get_real_type(die_mem, &type_die) =3D=3D NULL) + if (die_get_real_type(die_mem, &data->type) =3D=3D NULL) return false; =20 - if (is_pointer && dwarf_tag(&type_die) =3D=3D DW_TAG_pointer_type) { + if (is_pointer && dwarf_tag(&data->type) =3D=3D DW_TAG_pointer_type) { /* Get the target type of the pointer */ - if (die_get_real_type(&type_die, &type_die) =3D=3D NULL) + if (die_get_real_type(&data->type, &data->type) =3D=3D NULL) return false; } =20 - if (dwarf_aggregate_size(&type_die, &size) < 0) + if (dwarf_aggregate_size(&data->type, &size) < 0) return false; =20 if ((u64)offset >=3D size) @@ -1529,7 +1530,7 @@ static int __die_find_var_reg_cb(Dwarf_Die *die_mem, = void *arg) * when the variable is in the stack. */ Dwarf_Die *die_find_variable_by_reg(Dwarf_Die *sc_die, Dwarf_Addr pc, int = reg, - int *poffset, bool is_fbreg, + Dwarf_Die *type_die, int *poffset, bool is_fbreg, Dwarf_Die *die_mem) { struct find_var_data data =3D { @@ -1541,8 +1542,11 @@ Dwarf_Die *die_find_variable_by_reg(Dwarf_Die *sc_di= e, Dwarf_Addr pc, int reg, Dwarf_Die *result; =20 result =3D die_find_child(sc_die, __die_find_var_reg_cb, &data, die_mem); - if (result) + if (result) { *poffset =3D data.offset; + *type_die =3D data.type; + } + return result; } =20 diff --git a/tools/perf/util/dwarf-aux.h b/tools/perf/util/dwarf-aux.h index cd481ec9c5a1..b3ee5df0b6be 100644 --- a/tools/perf/util/dwarf-aux.h +++ b/tools/perf/util/dwarf-aux.h @@ -163,7 +163,7 @@ int die_get_var_range(Dwarf_Die *sp_die, Dwarf_Die *vr_= die, struct strbuf *buf); =20 /* Find a variable saved in the 'reg' at given address */ Dwarf_Die *die_find_variable_by_reg(Dwarf_Die *sc_die, Dwarf_Addr pc, int = reg, - int *poffset, bool is_fbreg, + Dwarf_Die *type_die, int *poffset, bool is_fbreg, Dwarf_Die *die_mem); =20 /* Find a (global) variable located in the 'addr' */ --=20 2.51.0.384.g4c02a37b29-goog From nobody Thu Oct 2 10:50:28 2025 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 293D82FFF9D for ; Wed, 17 Sep 2025 19:58:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758139102; cv=none; b=OGj/n6Y0k6BmAN3tLt4JcsbnkB9WNvB9eJntRfvuz03fgbXZyrCX39G+4V/rqjz/G5827bRYjhrm2lMfn/R7k58L2bUvw1caw5QRpftbKrzM5aU+gWRPivO+CqO7qlzgPtgKtqq9piMEDJK1laKzcrYG8/yDze9JN07bBuDdl3Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758139102; c=relaxed/simple; bh=YaCTe7tvAvbh0b1M/IwTP5q+eEGK9U/Dhs+HYoFfQOc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ILkNrSHY0JgK2UGQf952gLiT1jaVFQ4dPptd6Iyr1Cqa+c9hJD0Nj7Sel3b6ctur0sAzAUQPgk7js9MJByAZemLbRq6WKFXmJYsTgEtekfqDc1USc+W3NquCUmJXmlNCYuwLq+3YuPZLeeVomY+temqMDT0JMeB6sY5zZDixjC8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=w6kZ2psm; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="w6kZ2psm" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-729540b6278so2965997b3.1 for ; Wed, 17 Sep 2025 12:58:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1758139100; x=1758743900; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ff8iQk0FPzgWQVhFkOxEf6wop7fJ/8iqGcNpfT69dcA=; b=w6kZ2psmpYUAw9m6s+W3xXe4v95RQ0+PvdoB2XN+h+1B1uKgeDTuB8jVWZJBIiTP6l qStORZImJ+UAuBQDOCBN3n9Jb0BjjPAC1GfRtQ5dvQSig8nA0Ywp89QZp7izX/YzqGLH IPV2zKfe3UKgiVQ4buySR4sXBHUF5I8+OGj51NwhXN4gDloX5dDxQ/Vm5LHUlo6KFajV 45pJQnWFB0KCIeIVjLZq+wMU4AtVYkU74uPeHn6TutkSpv8Z9nh316ZGlUkSo+kCnZYo 0QB97TUCJwr3Wb6Sz2/giQiG1/5LQZ7WoqF2QWeJjeT2K1tZceWSebmkalSpHVggo68G RP9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758139100; x=1758743900; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ff8iQk0FPzgWQVhFkOxEf6wop7fJ/8iqGcNpfT69dcA=; b=RKU1068kT4ugdy7PmzjrhbNaru1wawAH+CmNsOzop6VfM31lXz9je8WsAp4jT10WVm EroMva6efOxd8TLfghWtaB9ZVAaRd8O3VzJeeGagKCNTfcd4Yt4b7k63bgtSBnIzJjHb VI5+xScUHXX6LA8TEyI0LmkwhInDe5tSiY1tJdlQB+z1Y3/Ed3vQuL9kvT56BTn3FG1S YERJuq5xJaaN2keoenKHdZ1tHJAw1j2erNrC3tCODk5LyHfDwleXiKfOxVs1BZauvKk6 2f68306/+G2EZ+nXgJOLqXvrgQbba0iEtFp6vk61qUXM4lcDAJZAeprByUwjvJnnGCER JhuA== X-Forwarded-Encrypted: i=1; AJvYcCVb/hYKp3zm5XCXKo9nu9S1rswtAbuKEdQm5uj2X328WqiOq5ZiJZPgVFlaqXmD2iP8hBjCtsuuelf9+3I=@vger.kernel.org X-Gm-Message-State: AOJu0YwfNrl5dI1k+MYSVX4C2Y7xxFVL1RcgWk6aMDFcxE6B54YwOqWb QJ6ltxxBle/1wyqo5iafPPnrM2mihaRwggofujFo9CGnBhJZItA2/svbjO9ufbe2PPDEQgnsOSg gQ7vhmTIPLA== X-Google-Smtp-Source: AGHT+IFCBl5FOhbjQY+swMvC9WLhg4anbTbvjABfmmdmioCSc+TNQepIhR3e/bBE3JHsnu8q/HRpmofN+Z6u X-Received: from ybbby7.prod.google.com ([2002:a05:6902:1707:b0:ea4:791:2410]) (user=zecheng job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6902:5408:b0:e97:b52:a814 with SMTP id 3f1490d57ef6-ea5c044dd64mr3348542276.34.1758139100119; Wed, 17 Sep 2025 12:58:20 -0700 (PDT) Date: Wed, 17 Sep 2025 19:58:06 +0000 In-Reply-To: <20250917195808.2514277-1-zecheng@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250917195808.2514277-1-zecheng@google.com> X-Mailer: git-send-email 2.51.0.384.g4c02a37b29-goog Message-ID: <20250917195808.2514277-9-zecheng@google.com> Subject: [PATCH v3 08/10] perf dwarf-aux: Preserve typedefs in match_var_offset From: Zecheng Li To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , Masami Hiramatsu Cc: Xu Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Zecheng Li Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Since we are skipping the check_variable, we need to preserve typedefs in match_var_offset to match the results by __die_get_real_type. Also move the (offset =3D=3D 0) branch after the is_pointer check to ensure the correct type is used, fixing cases where an incorrect pointer type was chosen when the access offset was 0. Signed-off-by: Zecheng Li --- tools/perf/util/dwarf-aux.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c index b57cdc8860f0..b2189de07daf 100644 --- a/tools/perf/util/dwarf-aux.c +++ b/tools/perf/util/dwarf-aux.c @@ -1395,24 +1395,24 @@ static bool match_var_offset(Dwarf_Die *die_mem, st= ruct find_var_data *data, Dwarf_Word size; s64 offset =3D addr_offset - addr_type; =20 - if (offset =3D=3D 0) { - /* Update offset relative to the start of the variable */ - data->offset =3D 0; - return true; - } - if (offset < 0) return false; =20 - if (die_get_real_type(die_mem, &data->type) =3D=3D NULL) + if (__die_get_real_type(die_mem, &data->type) =3D=3D NULL) return false; =20 if (is_pointer && dwarf_tag(&data->type) =3D=3D DW_TAG_pointer_type) { /* Get the target type of the pointer */ - if (die_get_real_type(&data->type, &data->type) =3D=3D NULL) + if (__die_get_real_type(&data->type, &data->type) =3D=3D NULL) return false; } =20 + if (offset =3D=3D 0) { + /* Update offset relative to the start of the variable */ + data->offset =3D 0; + return true; + } + if (dwarf_aggregate_size(&data->type, &size) < 0) return false; =20 --=20 2.51.0.384.g4c02a37b29-goog From nobody Thu Oct 2 10:50:28 2025 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1C8AF280CE5 for ; Wed, 17 Sep 2025 19:58:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758139103; cv=none; b=El/T5fhiL7oy3FX4MvgJi7trvAWjb84dZkcQyTWy9hhy0E+MJyzB0BFarFqjLc73CqsyoRAx70DEWeNnR41wEQydCkIA9i9JA18NBS9YiJuJ2KbAHrRFDawOXqEH2aX4W+rf4WDHzaynSFhC5eSX3qIBsNQGGDBJuTPjxviAmk0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758139103; c=relaxed/simple; bh=0lWzt9RO+8jsRK3poX0e6qyYY2f7EOwhvwqUc9lYhdU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=avDgspfT9sg767QOpph+1KC6VzwjLXhHC9p4LmgT5deSazS10z3O3cpEbney3Kme+bt4dyk4+iQFVLjvn9YMNfJMJu+tPhwCAgYTZb4ysbQfdK8SOgtct10epclRWh565F0pRECwheJ2Op4ee8SLywMXrP85fWAytaFTAUceLFY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=EgUMSkOW; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="EgUMSkOW" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-72396ac948eso3004337b3.1 for ; Wed, 17 Sep 2025 12:58:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1758139101; x=1758743901; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=h9DHlI9Ihi3mnPuz7nsW2r0i9Vkc+j3chUUZTsmP0Lc=; b=EgUMSkOW4m0qQ4FP9uhV7svX09AA2YpwpyxdptZ3cGPie6aQAyryGp/8VXZzLQ7SNO yEVABSmKLl0Lm4306zpUAcNB/PM4qE77BG0LBK4DawJuYfLDu77qWB2iXCdtskN02+c6 26nk7G3jzjSWRNBmvnstBE19rnnrNViB7s9xmCXPtESKpVLqVTkr3ncv+hzEjxWn88Ny b5Tq8jdCRHAi3TQpfbiB3AzMYsKLA/hkAAlyaBTu2lT6ExOuDdLHR0rJxJAAMXSWjY7X C1lD9Hi2cnHWUYT3kt0ZtCR0jM3AJO4rmt4OJYZJjKVuFryf8KJLOKsCMTQmzeM1Phxi hCEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758139101; x=1758743901; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=h9DHlI9Ihi3mnPuz7nsW2r0i9Vkc+j3chUUZTsmP0Lc=; b=KmFanX1b0yXUBsYW54Z6lXbX1n71BP4shNgeftSURqjqOX0NhVeNXA/jmC6ZnKCrOD S12rE4ZVry6Toui0KE5B7EQ5XS6rbDtjMhzaxNZZMYK057az66+HBLpsPB1kvJoohvpY /VGkQzy79z89fu3rYfviIwmUr7Y5GqPK6ita7P3qenxK0lNAYtjZ/sxeQW01Tl1Uy40X AhdstEGnRRUaA41xEPyyrBiCcQ8P7ecydOsX3gSw8xrsShMwLkc3M2k4s7vjx32t3vJD YFHDGtnpjwffTCb3mm4onAcciTbVSRWMVZtpZllI1uol6oURsylyYPNt0Lw8e/4CHu30 vmsQ== X-Forwarded-Encrypted: i=1; AJvYcCWHO2jZiACuHoyvMQjqI/sCFkiBDscsg9y0LgU2gmahnzb6W7vc2je0cyupuvwtPesWuY3DzinI0Ylaor0=@vger.kernel.org X-Gm-Message-State: AOJu0Yz1LnMKJ2KNWfEPwYKt8g/Ee3bkGFbqaOii8ikwC0AdpIQqRsWf 0Ej4on4/mKhFGLkS6ipIuxPon1fyPF2IoPDEAdWVrtgiMzoQSPYjemkCim8sFR/4L5YQ4jqyYQd nNrfTJp81Rg== X-Google-Smtp-Source: AGHT+IEwbYCWP2bNi/CnkWrXxccCC22yUDESZA7PFDkENPsFEF4nWCL7Y9MLRk9ACZV2PmfW8TTOgQwRgw6k X-Received: from ywbbg13.prod.google.com ([2002:a05:690c:30d:b0:722:83d1:81db]) (user=zecheng job=prod-delivery.src-stubby-dispatcher) by 2002:a05:690c:d0b:b0:734:81fb:8ba0 with SMTP id 00721157ae682-7389129df2fmr29782737b3.19.1758139101185; Wed, 17 Sep 2025 12:58:21 -0700 (PDT) Date: Wed, 17 Sep 2025 19:58:07 +0000 In-Reply-To: <20250917195808.2514277-1-zecheng@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250917195808.2514277-1-zecheng@google.com> X-Mailer: git-send-email 2.51.0.384.g4c02a37b29-goog Message-ID: <20250917195808.2514277-10-zecheng@google.com> Subject: [PATCH v3 09/10] perf annotate: Improve type comparison from different scopes From: Zecheng Li To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , Masami Hiramatsu Cc: Xu Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Zecheng Li Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When comparing types from different scopes, first compare their type offsets. A larger offset means the field belongs to an outer (enclosing) struct. This helps resolve cases where a pointer is found in an inner scope, but a struct containing that pointer exists in an outer scope. Previously, is_better_type would prefer the pointer type, but the struct type is actually more complete and should be chosen. Prefer types from outer scopes when is_better_type cannot determine a better type. This sometimes helps pick a more complete type. Signed-off-by: Zecheng Li --- tools/perf/util/annotate-data.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-dat= a.c index 7e4c045d0f4d..51765bd36c47 100644 --- a/tools/perf/util/annotate-data.c +++ b/tools/perf/util/annotate-data.c @@ -1622,7 +1622,9 @@ static int find_data_type_die(struct data_loc_info *d= loc, Dwarf_Die *type_die) pr_debug_dtp("type_offset=3D%#x\n", type_offset); } =20 - if (!found || is_better_type(type_die, &mem_die)) { + if (!found || dloc->type_offset < type_offset || + (dloc->type_offset =3D=3D type_offset && + !is_better_type(&mem_die, type_die))) { *type_die =3D mem_die; dloc->type_offset =3D type_offset; found =3D true; --=20 2.51.0.384.g4c02a37b29-goog From nobody Thu Oct 2 10:50:28 2025 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5C2F73019B7 for ; Wed, 17 Sep 2025 19:58:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758139105; cv=none; b=W067YlOklDpVEfgUJvd5qppAvo91V4FqrW9u795//tk9Yx+HcYJyh5vBmhGDVlBx3x8TzbvmWhvQ6P+WwYRzgoPUmdGjHct6Vxo5HBh/SnZhkB9+W/tzfyo3PQ0+iPRWV6S5br4z1OGx1lk4uEMNxJMiDn9T6EsKNQhT/DDaRFw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758139105; c=relaxed/simple; bh=/F58P/YxjYrKoka+7vu+bHrQcwHs2hOyARDQ5uPmEic=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GDPmE+9io3VMN1isnneI/+j4hqT5Y0LUQCdKaj2X0n0n3Reivf8YiWKMK231j96E4hM3p/H2vndi+m9ZJjgbq/jHf3VvzUKhJaNFyGpH77njdPVM42zG5TJhHk/zMg1JWKlMlbJ59Giup6HC17MYMW+rpI7JvAZ9WM9XjlWyhrE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=2JbsR2oR; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="2JbsR2oR" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-ea40041ab9eso192040276.1 for ; Wed, 17 Sep 2025 12:58:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1758139102; x=1758743902; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=JWTEtgPxSrBsYU6g+cDk31HXjxfCjijkteP7xD/xTbc=; b=2JbsR2oRp2w/JP0bucN5N+OM5jwZr6ctySyksWldznRdxDkds+yDBn1xxlHh0fHjl+ uFpetk/a4EYDslynmtpsTTliDDYqznZ5nzlI0CRIJOVd+Oes8452cprxmDab8WnEYA07 zoSiDcSfZLCoG+ShwwoX/ELy6jrzIpYKpZWqmuiFvRM3ymq6NF2fwlL6ACQ0kb2Vd3Ao Gv5LSBu8TA76ARL0qkbLPgcwZIq36/5hxVpE2QLTFX0VE8Ni/Cv2p77USamXGR6PGt+2 CeX2L6OITfgrTGQF0yoqn/kbcn5ErPCK1ZVSEcti+YBuOZTaw/dk3MVDO+Tmf7aONTeL xNwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758139102; x=1758743902; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=JWTEtgPxSrBsYU6g+cDk31HXjxfCjijkteP7xD/xTbc=; b=cvop9p0kIyIF8l/a4/vdIz2NODBo3x/tlnT5CggsBnMv7mnjkkIIQQvFic5qD+lBwd CVzE9uktYZXPiVvZGUDW4sr8tzwhYN60uWcVfo9tOyYWrEH/tEvrh4c4hYomzyNeazzo 6yP91mCd0F7afIKvZWlbvWoZF+MMNkSbOwBb27HNPzfFXupr5A/Dz7h+1hnNimjGn+17 M9w6ikW7qmVL6EY4u3NCBSXly/tsIMnlxzUaC8BBKlDHFye8L9+6qFoo+r8cDpLFx5cu 7plFSDq+j/Nh1/s4gTn3/iGYTLMjtsT1c/j0fvoo5+KIgcR8EW09yEUs4zwQYIJhgqUe bdqA== X-Forwarded-Encrypted: i=1; AJvYcCUSd6A3U114TVH1GVya7/Bgf1apyprPnfQX3Ec0x8KIJ1yncMtsyjuB7OAdQyiB3YLHzdV2CRZdqlpSuRI=@vger.kernel.org X-Gm-Message-State: AOJu0Yyehc0C45ApLmEFFhiztjeYDEH6d1OA+CLBna5Ug0p8bvAsHvu/ h+SY0mPkrvKhqX2cRWj1qdJppsYcB63mzLHRNfFtqdDlI0nQnzK76oRc/mrpVG9ZMetqRJBfXLh LR0vnkuvEwQ== X-Google-Smtp-Source: AGHT+IG08BhZhawfTdGuzjbiqhrvEhyIzX6FKnBqwOT1ntDkoB5dGR4kb9fm9mCJPrVKQHGeFQAz3moO6Y+J X-Received: from ybbgo2.prod.google.com ([2002:a05:6902:4202:b0:ea4:16f0:d3f6]) (user=zecheng job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6902:120f:b0:e96:f587:cb21 with SMTP id 3f1490d57ef6-ea5c044ce30mr2422450276.27.1758139102335; Wed, 17 Sep 2025 12:58:22 -0700 (PDT) Date: Wed, 17 Sep 2025 19:58:08 +0000 In-Reply-To: <20250917195808.2514277-1-zecheng@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250917195808.2514277-1-zecheng@google.com> X-Mailer: git-send-email 2.51.0.384.g4c02a37b29-goog Message-ID: <20250917195808.2514277-11-zecheng@google.com> Subject: [PATCH v3 10/10] perf dwarf-aux: Support DW_OP_piece expressions From: Zecheng Li To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , Masami Hiramatsu Cc: Xu Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Zecheng Li Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Support variables split across multiple registers or stack locations by handling DW_OP_piece in DWARF expressions. This enables correct matching of such variables by iterating over all pieces in the expression. There are two cases for matching memory access on the target register: 1. Accessing a struct member: - The type is the original variable's type. - The offset is the sum of the piece's offset and the operand's offset. 2. Dereferencing a member: - The type is the member of the original variable (the member must be a pointer). - The size must match the piece size. - The access offset is the operand's offset. This change improves support for piece-wise variable locations in DWARF expressions. Signed-off-by: Zecheng Li --- tools/perf/util/dwarf-aux.c | 244 +++++++++++++++++++++++++++--------- 1 file changed, 187 insertions(+), 57 deletions(-) diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c index b2189de07daf..aae15a2cb7d0 100644 --- a/tools/perf/util/dwarf-aux.c +++ b/tools/perf/util/dwarf-aux.c @@ -1390,21 +1390,44 @@ struct find_var_data { #define DWARF_OP_DIRECT_REGS 32 =20 static bool match_var_offset(Dwarf_Die *die_mem, struct find_var_data *dat= a, - s64 addr_offset, s64 addr_type, bool is_pointer) + s64 addr_offset, s64 addr_type, s64 piece_offset, + s64 piece_size, bool is_pointer) { - Dwarf_Word size; + Dwarf_Word size =3D 0; s64 offset =3D addr_offset - addr_type; =20 - if (offset < 0) + if (piece_size =3D=3D 0 || offset < 0) return false; =20 + if (piece_size > 0 && !is_pointer) { + offset +=3D piece_offset; + size =3D piece_offset + piece_size; + } + if (__die_get_real_type(die_mem, &data->type) =3D=3D NULL) return false; =20 - if (is_pointer && dwarf_tag(&data->type) =3D=3D DW_TAG_pointer_type) { - /* Get the target type of the pointer */ - if (__die_get_real_type(&data->type, &data->type) =3D=3D NULL) - return false; + if (is_pointer) { + if (piece_size < 0 && dwarf_tag(&data->type) =3D=3D DW_TAG_pointer_type)= { + /* Get the target type of the pointer */ + if (__die_get_real_type(&data->type, &data->type) =3D=3D NULL) + return false; + } + + if (piece_size > 0) { + Dwarf_Die member_die; + + if (die_get_member_type(&data->type, piece_offset, &member_die) =3D=3D = NULL) + return false; + + if (dwarf_aggregate_size(&member_die, &size) < 0) + return false; + + if (size =3D=3D (u64)piece_size && + dwarf_tag(&data->type) =3D=3D DW_TAG_pointer_type) + if (__die_get_real_type(&member_die, &data->type) =3D=3D NULL) + return false; + } } =20 if (offset =3D=3D 0) { @@ -1413,7 +1436,7 @@ static bool match_var_offset(Dwarf_Die *die_mem, stru= ct find_var_data *data, return true; } =20 - if (dwarf_aggregate_size(&data->type, &size) < 0) + if (size =3D=3D 0 && dwarf_aggregate_size(&data->type, &size) < 0) return false; =20 if ((u64)offset >=3D size) @@ -1452,6 +1475,67 @@ static bool is_breg_access_indirect(Dwarf_Op *ops, s= ize_t nops) return false; } =20 +struct op_piece_iter { + /* Pointer to the beginning of the op array */ + Dwarf_Op *ops; + size_t nops; + /* The index where the next search will begin */ + size_t current_idx; + size_t next_offset; + + /* Pointer to the start of the current piece's ops */ + Dwarf_Op *piece_ops; + /* The number of ops in the current piece */ + size_t num_piece_ops; + size_t piece_offset; +}; + +static void op_piece_iter_init(struct op_piece_iter *it, Dwarf_Op *ops, si= ze_t nops) +{ + it->ops =3D ops; + it->nops =3D nops; + it->current_idx =3D 0; + it->next_offset =3D 0; + it->piece_ops =3D NULL; + it->num_piece_ops =3D 0; + it->piece_offset =3D 0; +} + +/* Finds the next non-empty piece segment. */ +static bool op_piece_iter_next(struct op_piece_iter *it) +{ + /* Loop until a non-empty piece is found */ + while (it->current_idx < it->nops) { + size_t start; + size_t end; + + start =3D it->current_idx; + end =3D start; + + while (end < it->nops && it->ops[end].atom !=3D DW_OP_piece) + end++; + + /* The number of ops in this segment, including DW_OP_piece */ + it->num_piece_ops =3D min(end - start + 1, it->nops - start); + it->piece_ops =3D &it->ops[start]; + it->piece_offset =3D it->next_offset; + + it->current_idx =3D end; + if (it->current_idx < it->nops) { + const Dwarf_Op *piece_op =3D &it->ops[it->current_idx]; + size_t piece_size =3D (size_t)piece_op->number; + + it->next_offset +=3D piece_size; + it->current_idx++; + } + + if (end > start) + return true; + } + + return false; +} + /* Only checks direct child DIEs in the given scope. */ static int __die_find_var_reg_cb(Dwarf_Die *die_mem, void *arg) { @@ -1470,48 +1554,65 @@ static int __die_find_var_reg_cb(Dwarf_Die *die_mem= , void *arg) return DIE_FIND_CB_SIBLING; =20 while ((off =3D dwarf_getlocations(&attr, off, &base, &start, &end, &ops,= &nops)) > 0) { + struct op_piece_iter piece_iter; /* Assuming the location list is sorted by address */ if (end <=3D data->pc) continue; if (start > data->pc) break; =20 - /* Local variables accessed using frame base register */ - if (data->is_fbreg && ops->atom =3D=3D DW_OP_fbreg && - check_allowed_ops(ops, nops) && - match_var_offset(die_mem, data, data->offset, ops->number, - is_breg_access_indirect(ops, nops))) - return DIE_FIND_CB_END; + op_piece_iter_init(&piece_iter, ops, nops); + while (op_piece_iter_next(&piece_iter)) { + Dwarf_Op *pops =3D piece_iter.piece_ops; + size_t pnops =3D piece_iter.num_piece_ops; + size_t piece_offset =3D piece_iter.piece_offset; + int piece_size =3D -1; + bool is_pointer =3D true; + int access_offset =3D data->offset; =20 - /* Only match with a simple case */ - if (data->reg < DWARF_OP_DIRECT_REGS) { - /* pointer variables saved in a register 0 to 31 */ - if (ops->atom =3D=3D (DW_OP_reg0 + data->reg) && - check_allowed_ops(ops, nops) && - match_var_offset(die_mem, data, data->offset, 0, - /*is_pointer=3D*/true)) - return DIE_FIND_CB_END; + if (pops[pnops - 1].atom =3D=3D DW_OP_piece) + piece_size =3D (int)pops[pnops - 1].number; =20 - /* variables accessed by a register + offset */ - if (ops->atom =3D=3D (DW_OP_breg0 + data->reg) && - check_allowed_ops(ops, nops) && - match_var_offset(die_mem, data, data->offset, ops->number, - is_breg_access_indirect(ops, nops))) - return DIE_FIND_CB_END; - } else { - /* pointer variables saved in a register 32 or above */ - if (ops->atom =3D=3D DW_OP_regx && ops->number =3D=3D data->reg && - check_allowed_ops(ops, nops) && - match_var_offset(die_mem, data, data->offset, 0, - /*is_pointer=3D*/true)) - return DIE_FIND_CB_END; + if (!check_allowed_ops(pops, pnops)) + continue; =20 - /* variables accessed by a register + offset */ - if (ops->atom =3D=3D DW_OP_bregx && data->reg =3D=3D ops->number && - check_allowed_ops(ops, nops) && - match_var_offset(die_mem, data, data->offset, ops->number2, - is_breg_access_indirect(ops, nops))) + if ((data->is_fbreg && pops->atom =3D=3D DW_OP_fbreg) || + (pops->atom =3D=3D DW_OP_breg0 + data->reg) || + (pops->atom =3D=3D DW_OP_bregx && data->reg =3D=3D pops->number)) + is_pointer =3D is_breg_access_indirect(pops, pnops); + + /* Local variables accessed using frame base register */ + if (data->is_fbreg && pops->atom =3D=3D DW_OP_fbreg && + match_var_offset(die_mem, data, access_offset, + pops->number, piece_offset, piece_size, is_pointer)) return DIE_FIND_CB_END; + + /* Only match with a simple case */ + if (data->reg < DWARF_OP_DIRECT_REGS) { + /* pointer variables saved in a register 0 to 31 */ + if (pops->atom =3D=3D (DW_OP_reg0 + data->reg) && + match_var_offset(die_mem, data, access_offset, + 0, piece_offset, piece_size, is_pointer)) + return DIE_FIND_CB_END; + + /* variables accessed by a register + offset */ + if (pops->atom =3D=3D (DW_OP_breg0 + data->reg) && + match_var_offset(die_mem, data, access_offset, + pops->number, piece_offset, piece_size, is_pointer)) + return DIE_FIND_CB_END; + } else { + /* pointer variables saved in a register 32 or above */ + if (pops->atom =3D=3D DW_OP_regx && pops->number =3D=3D data->reg && + match_var_offset(die_mem, data, access_offset, + 0, piece_offset, piece_size, is_pointer)) + return DIE_FIND_CB_END; + + /* variables accessed by a register + offset */ + if (pops->atom =3D=3D DW_OP_bregx && data->reg =3D=3D pops->number && + match_var_offset(die_mem, data, access_offset, + pops->number2, piece_offset, piece_size, is_pointer)) + return DIE_FIND_CB_END; + } } } return DIE_FIND_CB_SIBLING; @@ -1572,7 +1673,7 @@ static int __die_find_var_addr_cb(Dwarf_Die *die_mem,= void *arg) continue; =20 if (check_allowed_ops(ops, nops) && - match_var_offset(die_mem, data, data->addr, ops->number, + match_var_offset(die_mem, data, data->addr, ops->number, 0, -1, /*is_pointer=3D*/false)) return DIE_FIND_CB_END; } @@ -1613,6 +1714,7 @@ static int __die_collect_vars_cb(Dwarf_Die *die_mem, = void *arg) Dwarf_Op *ops; size_t nops; struct die_var_type *vt; + struct op_piece_iter piece_iter; =20 if (tag !=3D DW_TAG_variable && tag !=3D DW_TAG_formal_parameter) return DIE_FIND_CB_SIBLING; @@ -1634,25 +1736,53 @@ static int __die_collect_vars_cb(Dwarf_Die *die_mem= , void *arg) if (__die_get_real_type(die_mem, &type_die) =3D=3D NULL) return DIE_FIND_CB_SIBLING; =20 - vt =3D malloc(sizeof(*vt)); - if (vt =3D=3D NULL) - return DIE_FIND_CB_END; + op_piece_iter_init(&piece_iter, ops, nops); + while (op_piece_iter_next(&piece_iter)) { + Dwarf_Op *pops =3D piece_iter.ops; + size_t pnops =3D piece_iter.num_piece_ops; + size_t piece_offset =3D piece_iter.piece_offset; + size_t offset =3D offset_from_dwarf_op(pops); + s64 piece_size =3D -1; + /* Usually a register holds the value of the variable */ + bool is_reg_var_addr =3D false; + + if (((pops->atom >=3D DW_OP_breg0 && pops->atom <=3D DW_OP_breg31) || + pops->atom =3D=3D DW_OP_bregx || pops->atom =3D=3D DW_OP_fbreg) && + !is_breg_access_indirect(pops, pnops)) + /* The register holds the address of the variable. */ + is_reg_var_addr =3D true; + + if (pops[pnops - 1].atom =3D=3D DW_OP_piece) + piece_size =3D (s64)pops[pnops - 1].number; + + if (piece_size > 0) { + if (!is_reg_var_addr) { + size_t size; + + if (die_get_member_type(&type_die, piece_offset, &type_die) =3D=3D NUL= L) + continue; =20 - /* Usually a register holds the value of a variable */ - vt->is_reg_var_addr =3D false; + if (dwarf_aggregate_size(&type_die, &size) < 0) + continue; =20 - if (((ops->atom >=3D DW_OP_breg0 && ops->atom <=3D DW_OP_breg31) || - ops->atom =3D=3D DW_OP_bregx || ops->atom =3D=3D DW_OP_fbreg) && - !is_breg_access_indirect(ops, nops)) - /* The register contains an address of the variable. */ - vt->is_reg_var_addr =3D true; + if (size !=3D (u64)piece_size) + continue; + } else + offset +=3D piece_offset; + } =20 - vt->die_off =3D dwarf_dieoffset(&type_die); - vt->addr =3D start; - vt->reg =3D reg_from_dwarf_op(ops); - vt->offset =3D offset_from_dwarf_op(ops); - vt->next =3D *var_types; - *var_types =3D vt; + vt =3D malloc(sizeof(*vt)); + if (vt =3D=3D NULL) + return DIE_FIND_CB_END; + + vt->is_reg_var_addr =3D is_reg_var_addr; + vt->die_off =3D dwarf_dieoffset(&type_die); + vt->addr =3D start; + vt->reg =3D reg_from_dwarf_op(pops); + vt->offset =3D offset; + vt->next =3D *var_types; + *var_types =3D vt; + } =20 return DIE_FIND_CB_SIBLING; } --=20 2.51.0.384.g4c02a37b29-goog