Received: by 2002:ab2:620c:0:b0:1ef:ffd0:ce49 with SMTP id o12csp1033802lqt; Tue, 19 Mar 2024 10:44:20 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXZPlA9GZDyZIajLjPp7X97Rxfhb5pVfchtkSE5/bxAjX9HkDbZjklNwaG1N83VNRJx9YERZ660f44NaJM0hRSqo3s3ejLNqRgmftMVWQ== X-Google-Smtp-Source: AGHT+IE6NKaTlGCgbhU9sxT45IKKjfQFnxjkX2KR8WjuLfN0MoZKzyvn2zxsnsL/KxyTa4DyjjUf X-Received: by 2002:a05:6402:4497:b0:567:1947:d53f with SMTP id er23-20020a056402449700b005671947d53fmr151905edb.9.1710870259949; Tue, 19 Mar 2024 10:44:19 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710870259; cv=pass; d=google.com; s=arc-20160816; b=LLI6v5GrAIwURag/g50jvQVUvcwb7Tu5mJG8wvj/pDDQn9y3y2ERYZsz2dlEyH3qWf 1SqP+k9oa/VI8LjGXGuMIk3vBLKVU+64eB+/HpuFzu2wVuMpkZrG19NSC2HrGiMwO/ty iKly16zaAHy/I6GJN/Ag7IK3jHNpziYdgEBLmbuFL9s+yaQMN08954nQmJJhk33b/Y8i qTe0Vnxn/U7sjxJbauECruRx+Qd6FVa6K9J809W4lvSzDDl+BOwJK4TCEfpc6GRsE0JB M4u4DiP9M18K3omq0/+tlVxFFNzbbWVxGhu49NWmF550GPnPmw65ym1BK8msotd95IVz VRCg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence; bh=1CSPbX1Xd5Cwgf7z/rJxAM7//MMvTw7xggH/ih20s0w=; fh=7XOvrvlfiqit3DkTBy7Ob010yFB9L6vUFY5yAaJVz5k=; b=v8COcA++i/XJD3wOlBkOUzNQ43TR86U4UqmbqjBWl1Aq4udOamyRlPgDPpLQETCSN5 lC73QwSvZDqwO6X7mKq0SjPIeGAPnxWKOA6DxpgJO/pM0nHi/FKQNDabOJiSaCKx7rdc Au3JdoDofv1nYZdHZ0wR9YCGcTu/k7QKWhKv1RM0rOB07LlWuS1lrw4QW11LaRoFhT2s D0aGR4LAA9CU+ESwdoh1/yZcSBk5p9dclwgXDRqBZiYaE/yvCY46u4ElQreI2mb1uFS6 6aYcg4+HFjAECINDnFqUreImgQL0h1MBeP/u8E+ajcZypHbFzC/hpLse5Ec5YjEvyto4 5kTA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-107936-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-107936-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id h19-20020a056402281300b0056ba97ecfd4si23256ede.489.2024.03.19.10.44.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Mar 2024 10:44:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-107936-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-107936-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-107936-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 820A21F22016 for ; Tue, 19 Mar 2024 17:44:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DD0252C690; Tue, 19 Mar 2024 17:41:21 +0000 (UTC) Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) (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 7B93A1CD24; Tue, 19 Mar 2024 17:41:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710870081; cv=none; b=a4mSFD3Hgsu25rhp+z7E8gcnermnHowcyTTkgkhARJXzicVvEDsVlRlnwp+xtyUFD6yFbi4g6XUg4qgSjW4aAbtQJoVF+2JIW/9TTQKZE20HeNqNfYTD3E3qZE2fljmUdvsyINk3QNh1vMZPkrNVOObChWAhKHJMG05N7Lnx47g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710870081; c=relaxed/simple; bh=TvNDMnssTfhV6421IEswYDmR42C5dMtYiJ1mEVtIdFs=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=Zblm+ptiiClJHuIw5+mHpgVV5tJlF686eZSAcyE7VQ2jqZBZEYbKwYMY8AgLOjWbtAuC5ymaPTeahfjbZyWTbKXq6CD9SFSSp2kkysH8PaxC+BWUEDEDmgo/TXGWhzCU1WCEg5tI/3Vufct0BsJYwpZDrYeu41LQhbHYTybDnP8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.client-ip=209.85.215.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pg1-f182.google.com with SMTP id 41be03b00d2f7-5e4613f2b56so4449612a12.1; Tue, 19 Mar 2024 10:41:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710870079; x=1711474879; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1CSPbX1Xd5Cwgf7z/rJxAM7//MMvTw7xggH/ih20s0w=; b=GE0PBODechFNjpKGyi8XgIsPMXnobEgCQHESwgey+zh9MjkoD5Jet6cdCgFGNXFH/0 BDXXudBFiZUSdvkRn1EiTqvGkyuNo3ooVj/1xzypCn2xxMrrt93ECgnNEjjpfZ4rxjWk LFw2VL6Fr7PHoSs7ZUsbYczWACH7Or8m4sMTxgtUhonWZy49eupYdqbTpCnpqqeIRUxU fWD+fwupNbRLcbhEDDlUUwTdaNjFk1osKsvr+rbZH7eycu4mtX9BKnHDjGPwXgNUpJmv SrqOy/m/X0oK2kM+Pt8meBMq74F1vP4XWoDKXo2/oPajxpFNjklGKE3gK5N1Hhbipq8f 0Cwg== X-Forwarded-Encrypted: i=1; AJvYcCXQSTGsDcsXTS0fF1U9L9uJYTf8I/eauzgy6DEMy/Z8sYpMUYZhYiLLxXSjVG9KEUJzWPZHM5J1tMl9HapPz+rErBio/88rfkGRxSLKU5bJM8EIWmqZWgnr75wEoeyHbYYDkKc3aYAgNmBRLMP+MgyytA+yNbgdU8VxSzHXxBee2WhTP3WGNqCp5KMP2uepzFPTbcyvdRkD2Kk+SHV+dGIp9oYQb/9lpwWiNXt7tJPPHwEPsg== X-Gm-Message-State: AOJu0YwQ8+tJ293zkUWtw8p63wUOojoLJdP62ewz/eeGxMC+LC51Tt26 7CmRR1WUBcYz9ZvF/B3iz8XNUs8zIfM3B7/ya1qz3eBvhnOdDJLksc7wvy+XJ8rX3GgqFgTsALy a47KJNPdIPxPHyLE3DRlkmSS2rPY= X-Received: by 2002:a17:90a:e50d:b0:29b:ccb4:a250 with SMTP id t13-20020a17090ae50d00b0029bccb4a250mr11108973pjy.16.1710870078736; Tue, 19 Mar 2024 10:41:18 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240319055115.4063940-1-namhyung@kernel.org> <20240319055115.4063940-4-namhyung@kernel.org> In-Reply-To: From: Namhyung Kim Date: Tue, 19 Mar 2024 10:41:06 -0700 Message-ID: Subject: Re: [PATCH 03/23] perf dwarf-aux: Handle type transfer for memory access To: Arnaldo Carvalho de Melo Cc: Ian Rogers , Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Linus Torvalds , Stephane Eranian , Masami Hiramatsu , linux-toolchains@vger.kernel.org, linux-trace-devel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Mar 19, 2024 at 6:55=E2=80=AFAM Arnaldo Carvalho de Melo wrote: > > On Mon, Mar 18, 2024 at 10:50:55PM -0700, Namhyung Kim wrote: > > We want to track type states as instructions are executed. Each > > instruction can access compound types like struct or union and load/ > > store its members to a different location. > > > > The die_deref_ptr_type() is to find a type of memory access with a > > pointer variable. If it points to a compound type like struct, the > > target memory is a member in the struct. The access will happen > > with an offset indicating which member it refers. Let's follow the > > DWARF info to figure out the type of the pointer target. > > > > For example, say we have the following code. > > > > struct foo { > > int a; > > int b; > > }; > > > > struct foo *p =3D malloc(sizeof(*p)); > > p->b =3D 0; > > > > The last pointer access should produce x86 asm like below: > > > > mov 0x0, 4(%rbx) > > > > And we know %rbx register has a pointer to struct foo. Then offset 4 > > should return the debug info of member 'b'. > > > > Also variables of compound types can be accessed directly without a > > pointer. The die_get_member_type() is to handle a such case. > > > > Acked-by: Masami Hiramatsu (Google) > > Signed-off-by: Namhyung Kim > > --- > > tools/perf/util/dwarf-aux.c | 110 ++++++++++++++++++++++++++++++++++++ > > tools/perf/util/dwarf-aux.h | 6 ++ > > 2 files changed, 116 insertions(+) > > > > diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c > > index 785aa7a3d725..cd9364d296b6 100644 > > --- a/tools/perf/util/dwarf-aux.c > > +++ b/tools/perf/util/dwarf-aux.c > > @@ -1838,3 +1838,113 @@ int die_get_scopes(Dwarf_Die *cu_die, Dwarf_Add= r pc, Dwarf_Die **scopes) > > *scopes =3D data.scopes; > > return data.nr; > > } > > + > > +static int __die_find_member_offset_cb(Dwarf_Die *die_mem, void *arg) > > +{ > > + Dwarf_Die type_die; > > + Dwarf_Word size, loc; > > + Dwarf_Word offset =3D (long)arg; > > + int tag =3D dwarf_tag(die_mem); > > + > > + if (tag !=3D DW_TAG_member) > > + return DIE_FIND_CB_SIBLING; > > + > > + /* Unions might not have location */ > > + if (die_get_data_member_location(die_mem, &loc) < 0) > > + loc =3D 0; > > + > > + if (offset =3D=3D loc) > > + return DIE_FIND_CB_END; > > + > > + die_get_real_type(die_mem, &type_die); > > Don't we have to check that the type_die was initialized? From what I > can see there is the possibility it isn't: Right, it might return NULL if the entry doesn't have a valid DW_AT_type. I'll check the return value and skip. Thanks, Namhyung > > Dwarf_Die type_die; > die_get_real_type(die_mem, &type_die); > do { > vr_die =3D __die_get_real_type(vr_die, die_mem); > } while (vr_die && dwarf_tag(vr_die) =3D=3D DW_TAG_typedef); > > return vr_die; > > static Dwarf_Die *__die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *d= ie_mem) > > do { > vr_die =3D die_get_type(vr_die, die_mem); > if (!vr_die) > break; > > Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem) > > Dwarf_Attribute attr; > > if (dwarf_attr_integrate(vr_die, DW_AT_type, &attr) && > dwarf_formref_die(&attr, die_mem)) > return die_mem; > else > return NULL; > } > > > + if (dwarf_aggregate_size(&type_die, &size) < 0) > > + size =3D 0; > > + > > + if (loc < offset && offset < (loc + size)) > > + return DIE_FIND_CB_END; > > + > > + return DIE_FIND_CB_SIBLING; > > +} > > + > > +/** > > + * die_get_member_type - Return type info of struct member > > + * @type_die: a type DIE > > + * @offset: offset in the type > > + * @die_mem: a buffer to save the resulting DIE > > + * > > + * This function returns a type of a member in @type_die where it's lo= cated at > > + * @offset if it's a struct. For now, it just returns the first match= ing > > + * member in a union. For other types, it'd return the given type dir= ectly > > + * if it's within the size of the type or NULL otherwise. > > + */ > > +Dwarf_Die *die_get_member_type(Dwarf_Die *type_die, int offset, > > + Dwarf_Die *die_mem) > > +{ > > + Dwarf_Die *member; > > + Dwarf_Die mb_type; > > + int tag; > > + > > + tag =3D dwarf_tag(type_die); > > + /* If it's not a compound type, return the type directly */ > > + if (tag !=3D DW_TAG_structure_type && tag !=3D DW_TAG_union_type)= { > > + Dwarf_Word size; > > + > > + if (dwarf_aggregate_size(type_die, &size) < 0) > > + size =3D 0; > > + > > + if ((unsigned)offset >=3D size) > > + return NULL; > > + > > + *die_mem =3D *type_die; > > + return die_mem; > > + } > > + > > + mb_type =3D *type_die; > > + /* TODO: Handle union types better? */ > > + while (tag =3D=3D DW_TAG_structure_type || tag =3D=3D DW_TAG_unio= n_type) { > > + member =3D die_find_child(&mb_type, __die_find_member_off= set_cb, > > + (void *)(long)offset, die_mem); > > + if (member =3D=3D NULL) > > + return NULL; > > + > > + if (die_get_real_type(member, &mb_type) =3D=3D NULL) > > + return NULL; > > + > > + tag =3D dwarf_tag(&mb_type); > > + > > + if (tag =3D=3D DW_TAG_structure_type || tag =3D=3D DW_TAG= _union_type) { > > + Dwarf_Word loc; > > + > > + /* Update offset for the start of the member stru= ct */ > > + if (die_get_data_member_location(member, &loc) = =3D=3D 0) > > + offset -=3D loc; > > + } > > + } > > + *die_mem =3D mb_type; > > + return die_mem; > > +} > > + > > +/** > > + * die_deref_ptr_type - Return type info for pointer access > > + * @ptr_die: a pointer type DIE > > + * @offset: access offset for the pointer > > + * @die_mem: a buffer to save the resulting DIE > > + * > > + * This function follows the pointer in @ptr_die with given @offset > > + * and saves the resulting type in @die_mem. If the pointer points > > + * a struct type, actual member at the offset would be returned. > > + */ > > +Dwarf_Die *die_deref_ptr_type(Dwarf_Die *ptr_die, int offset, > > + Dwarf_Die *die_mem) > > +{ > > + Dwarf_Die type_die; > > + > > + if (dwarf_tag(ptr_die) !=3D DW_TAG_pointer_type) > > + return NULL; > > + > > + if (die_get_real_type(ptr_die, &type_die) =3D=3D NULL) > > + return NULL; > > + > > + return die_get_member_type(&type_die, offset, die_mem); > > +} > > diff --git a/tools/perf/util/dwarf-aux.h b/tools/perf/util/dwarf-aux.h > > index cd171b06fd4c..16c916311bc0 100644 > > --- a/tools/perf/util/dwarf-aux.h > > +++ b/tools/perf/util/dwarf-aux.h > > @@ -144,6 +144,12 @@ struct die_var_type { > > int offset; > > }; > > > > +/* Return type info of a member at offset */ > > +Dwarf_Die *die_get_member_type(Dwarf_Die *type_die, int offset, Dwarf_= Die *die_mem); > > + > > +/* Return type info where the pointer and offset point to */ > > +Dwarf_Die *die_deref_ptr_type(Dwarf_Die *ptr_die, int offset, Dwarf_Di= e *die_mem); > > + > > #ifdef HAVE_DWARF_GETLOCATIONS_SUPPORT > > > > /* Get byte offset range of given variable DIE */ > > -- > > 2.44.0.291.gc1ea87d7ee-goog