From patchwork Fri Feb 16 23:54:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 202435 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp59517dyc; Fri, 16 Feb 2024 15:55:50 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXw3mjcTa/5rSJLlI10l48oQ0HO9+qiHAzw9oqXXizrSOxGDWTKwi6w+A0hrBlVcw1Dl6ZnlqhuD99iV+nQtEvlcrYLSw== X-Google-Smtp-Source: AGHT+IHf8pMQxoQqE085gldY8zWpii09UVTZ27O4JmQzvK4NcizowInTzjtvBm5yiZRGvpBxUF6N X-Received: by 2002:a05:620a:179f:b0:787:3fb9:462e with SMTP id ay31-20020a05620a179f00b007873fb9462emr5175258qkb.0.1708127750005; Fri, 16 Feb 2024 15:55:50 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708127749; cv=pass; d=google.com; s=arc-20160816; b=x0XInezczo/PpN5sMFZOwTbYYf268xdq3d00DwobtL2a36pTKNWJSYs+/gUN9oE/k+ +hpVV0PLoAKgM5vTIMkwGc+PbF8GuREbJgbsXylczIyTmqknT0RUQnnSF5JLpwqJGpRY 80gFqS0kahqr22tj2htZPmTYOFrBNVNQx6JiGUY8GSEN4MBxlLs5yvMeuiXJXoNQxq6m 0w+EYSi5UlSdHkW5yC4oIwx6fnGsqFuPD/dCZhGjSED84IsvOUDGGf1Wi54SLkr+aHHB ClMPuUiK5OOajvQDgALY3uKfXjksNUYxCRpEUntHXVtY3AE7EUNn7IaGsVl1EgDP6xcL I/tQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=AMjn1MOHxQ47BFFvvgVpz+CYXZhZH/7Kzah8Z8XWlVI=; fh=44lErbL5kvNUjSV6UDWgBEjJyOJpjjMpxi6K+HFi+Bc=; b=kF4OcE1/ZuBGI/SkB/P2psUbeBpeIIfxEQXFA8Ey+b6JFsvSIJACf4gykuLelh7x92 GIZ4B2PYSQZ5TuYGLLzO2wNY+Rh75A8/kt4dzZY7NjRIHu0x/Ha4duCoOxR8OxrLIEuu x9OSUK4qtY6r5SKRmGhFxh6FBn9xB+wXafzwe3NCa6/+/ZExjX/SJNY/WEcD2+cu6Via dj8u9IlP24QtEA3LKAETisjsKKx87pWKWi/zzUY4l4mkOpodnI7SxYrjvtiUSaGSoK5i PCCuDbli+z7EdHG4OA/KSMM05HIQgUYvlZj7eQQZVs4aeATkYD6y9SMovNKIeksb4mFt NByA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=dj2OBKlc; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-69495-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69495-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id oq18-20020a05620a611200b00787167c97cfsi1022992qkn.324.2024.02.16.15.55.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 15:55:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-69495-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=dj2OBKlc; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-69495-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69495-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 3EC851C22267 for ; Fri, 16 Feb 2024 23:55:39 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BC02E14C5A6; Fri, 16 Feb 2024 23:54:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="dj2OBKlc" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9D33514A087; Fri, 16 Feb 2024 23:54:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708127666; cv=none; b=SlMN+7P0tQSoyssMlmYxOqnILdmZuSWBQhMr2SArT2ktWbI0bg3nrGOQYjoT4lpawg3DoQkhRtIa2xkwNGfu1gRl/8mLtq2fUP8+9LzaKJz5tn8fr4BqdYjlOeGL+37LrN8WN+XIVmz8ETEGa36K9FYv1IOV4XqQsyfj+sbNrSE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708127666; c=relaxed/simple; bh=yYo85dqmUvjj/MYrq9h8iGZJuhDhkkgQ/jmNNXT6JHI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QLLYHfcGHi/7+GJ+ktNHyGybeqRXUlelqH+MFBFYvwRyaC2rW1s9I767dsEoSPnYW2nb/up/PIzXfpNiK7Fpmz5ziR18q2jwR+P/dRn0PK+X85WVLObs6+J1f4puYrIw73dsJtv4g+jDZquR+vQWxfhchrryFKdEmhJUQiHKAIo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=dj2OBKlc; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id B49C7C43394; Fri, 16 Feb 2024 23:54:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708127666; bh=yYo85dqmUvjj/MYrq9h8iGZJuhDhkkgQ/jmNNXT6JHI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dj2OBKlcSEHwerRQs2FFVXSo6SZ6FtK8rSto1WrOtbZ+DRGo7gvzjEgf50D/MllDs 5no8ezrgD4kYDpItCLEUmRWCuup70QLmll33UzE5yvC88O2T3HJ5Qr0ltfjKqgx7IY Eloy+ilGt8jAkBiPaH1fj9biz6DrrdVD6LkRFKJjgCz0BWrBGeri51KAbkml/MH/xk IoXRitUwiy4h5ex4OWRSle21pi0eqxjhhJbfexoCQAKOaGE2UIJZOgymU7R0IVZui2 TmIokKkbNus2S1f3r/lF8bs1lXwGv2YNHg/y3KXJX5oS+5J9HXzoTR9zuIUHSCdpFi wZpVpp2LwYxRw== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Linus Torvalds , Stephane Eranian , Masami Hiramatsu , linux-toolchains@vger.kernel.org, linux-trace-devel@vger.kernel.org Subject: [PATCH 02/14] perf dwarf-aux: Handle type transfer for memory access Date: Fri, 16 Feb 2024 15:54:11 -0800 Message-ID: <20240216235423.2343167-3-namhyung@kernel.org> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog In-Reply-To: <20240216235423.2343167-1-namhyung@kernel.org> References: <20240216235423.2343167-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791101763486537557 X-GMAIL-MSGID: 1791101763486537557 We want to track type states as instructions are executed. Each instruction can access compound types like struct or union and load/ store its members to a different location. The die_deref_ptr_type() is to find a type of memory access with a pointer variable. If it points to a compound type like struct, the target memory is a member in the struct. The access will happen with an offset indicating which member it refers. Let's follow the DWARF info to figure out the type of the pointer target. For example, say we have the following code. struct foo { int a; int b; }; struct foo *p = malloc(sizeof(*p)); p->b = 0; The last pointer access should produce x86 asm like below: mov 0x0, 4(%rbx) And we know %rbx register has a pointer to struct foo. Then offset 4 should return the debug info of member 'b'. Also variables of compound types can be accessed directly without a pointer. The die_get_member_type() is to handle a such case. Acked-by: Masami Hiramatsu (Google) Signed-off-by: Namhyung Kim --- tools/perf/util/dwarf-aux.c | 110 ++++++++++++++++++++++++++++++++++++ tools/perf/util/dwarf-aux.h | 6 ++ 2 files changed, 116 insertions(+) diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c index f878014c9e27..39851ff1d5c4 100644 --- a/tools/perf/util/dwarf-aux.c +++ b/tools/perf/util/dwarf-aux.c @@ -1841,3 +1841,113 @@ int die_get_scopes(Dwarf_Die *cu_die, Dwarf_Addr pc, Dwarf_Die **scopes) *scopes = data.scopes; return data.nr; } + +static int __die_find_member_offset_cb(Dwarf_Die *die_mem, void *arg) +{ + Dwarf_Die type_die; + Dwarf_Word size, loc; + Dwarf_Word offset = (long)arg; + int tag = dwarf_tag(die_mem); + + if (tag != DW_TAG_member) + return DIE_FIND_CB_SIBLING; + + /* Unions might not have location */ + if (die_get_data_member_location(die_mem, &loc) < 0) + loc = 0; + + if (offset == loc) + return DIE_FIND_CB_END; + + die_get_real_type(die_mem, &type_die); + + if (dwarf_aggregate_size(&type_die, &size) < 0) + size = 0; + + if (loc < offset && offset < (loc + size)) + return DIE_FIND_CB_END; + + return DIE_FIND_CB_SIBLING; +} + +/** + * die_get_member_type - Return type info of struct member + * @type_die: a type DIE + * @offset: offset in the type + * @die_mem: a buffer to save the resulting DIE + * + * This function returns a type of a member in @type_die where it's located at + * @offset if it's a struct. For now, it just returns the first matching + * member in a union. For other types, it'd return the given type directly + * if it's within the size of the type or NULL otherwise. + */ +Dwarf_Die *die_get_member_type(Dwarf_Die *type_die, int offset, + Dwarf_Die *die_mem) +{ + Dwarf_Die *member; + Dwarf_Die mb_type; + int tag; + + tag = dwarf_tag(type_die); + /* If it's not a compound type, return the type directly */ + if (tag != DW_TAG_structure_type && tag != DW_TAG_union_type) { + Dwarf_Word size; + + if (dwarf_aggregate_size(type_die, &size) < 0) + size = 0; + + if ((unsigned)offset >= size) + return NULL; + + *die_mem = *type_die; + return die_mem; + } + + mb_type = *type_die; + /* TODO: Handle union types better? */ + while (tag == DW_TAG_structure_type || tag == DW_TAG_union_type) { + member = die_find_child(&mb_type, __die_find_member_offset_cb, + (void *)(long)offset, die_mem); + if (member == NULL) + return NULL; + + if (die_get_real_type(member, &mb_type) == NULL) + return NULL; + + tag = dwarf_tag(&mb_type); + + if (tag == DW_TAG_structure_type || tag == DW_TAG_union_type) { + Dwarf_Word loc; + + /* Update offset for the start of the member struct */ + if (die_get_data_member_location(member, &loc) == 0) + offset -= loc; + } + } + *die_mem = mb_type; + return die_mem; +} + +/** + * die_deref_ptr_type - Return type info for pointer access + * @ptr_die: a pointer type DIE + * @offset: access offset for the pointer + * @die_mem: a buffer to save the resulting DIE + * + * This function follows the pointer in @ptr_die with given @offset + * and saves the resulting type in @die_mem. If the pointer points + * a struct type, actual member at the offset would be returned. + */ +Dwarf_Die *die_deref_ptr_type(Dwarf_Die *ptr_die, int offset, + Dwarf_Die *die_mem) +{ + Dwarf_Die type_die; + + if (dwarf_tag(ptr_die) != DW_TAG_pointer_type) + return NULL; + + if (die_get_real_type(ptr_die, &type_die) == NULL) + return NULL; + + return die_get_member_type(&type_die, offset, die_mem); +} diff --git a/tools/perf/util/dwarf-aux.h b/tools/perf/util/dwarf-aux.h index efafd3a1f5b6..ad4d7322fcbf 100644 --- a/tools/perf/util/dwarf-aux.h +++ b/tools/perf/util/dwarf-aux.h @@ -144,6 +144,12 @@ struct die_var_type { int offset; }; +/* Return type info of a member at offset */ +Dwarf_Die *die_get_member_type(Dwarf_Die *type_die, int offset, Dwarf_Die *die_mem); + +/* Return type info where the pointer and offset point to */ +Dwarf_Die *die_deref_ptr_type(Dwarf_Die *ptr_die, int offset, Dwarf_Die *die_mem); + #ifdef HAVE_DWARF_GETLOCATIONS_SUPPORT /* Get byte offset range of given variable DIE */