From patchwork Fri Nov 10 00:00:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 163686 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b129:0:b0:403:3b70:6f57 with SMTP id q9csp789266vqs; Thu, 9 Nov 2023 16:07:45 -0800 (PST) X-Google-Smtp-Source: AGHT+IHjq51Rebue1Y9wKw7UKZGvdn71bI7n57Ha3Lf5ZoaYl3BawnJicnC5CaJmoO4a98doboDB X-Received: by 2002:a05:6830:1347:b0:6bf:12:5189 with SMTP id r7-20020a056830134700b006bf00125189mr6407745otq.26.1699574865633; Thu, 09 Nov 2023 16:07:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699574865; cv=none; d=google.com; s=arc-20160816; b=WfqHR6T4Bn8YZUM+TmXvRCalxZVPF54bVUHIErbZIGnAllUMhxU57RGlURlysFuUG8 kd6yUsMqg1CYKPVq2uMaDumC/xS8Tk9KIsbR2X87TxlaKcxnoBbkO96VUXvYGxN5sGt9 wKSj0TCeDm3toS3+YBDnSmpNnLJ6imOOc99D15Yvt3CWac9V2iyA4VEwcRUUKsIBhzdj ptrLlRKbJpk4KdVUYhOYVY526TgDvHfPSStmqBtQiobeiL8MEHg1yv1JMKCI/20iI/O1 SLKPB6UN7Db0x6qlyDWEN9KDjZH9BAYVJKQ45yi9xn1RmoSXKlBV9CwdXWGXO32Iirb7 hnFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=kvTfv482Q4PYRjTpfYUAW6Q8pGRZjJf+S0DM/gjie/M=; fh=K//si1Hi/RYC7s39rVvmvh0YbZGmBrdWWHQg3cPF7Xc=; b=a9TaG6CLiIh/KOvDhoL4bmGJ8WCNdRSBzQzYP4Nzh9yK2L67dn90IDcaQgzCSpvWmu BNzjqynjdWZUv5+8BRzdzNfngSRQCetvfsLYu7CeBKnVEQcRSo2saeHwJ+WxQ8yU11PM r7GAiC0pJckbtr9br778ndeJogK63MgVGg36wSMzEM7vl+NZgfaTY+/VL1BcZJidiVIW fzs8uanaAuB0xko09LjEjoT/OjgyDEEQb61AUpY2o00IwsT5tFksnNH+zOjHKlapFv7i vzpJcG5da/PRwGhibLiGslRWM6qIDld9qmzXUD64dHSxmjKUB7p8e2qlSsx9h/ef3dya RyvA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=goKoS3+h; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id c4-20020a056830314400b006ce33d361b6si7416056ots.256.2023.11.09.16.07.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 16:07:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=goKoS3+h; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 31A6C8026926; Thu, 9 Nov 2023 16:07:09 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345587AbjKJAEG (ORCPT + 30 others); Thu, 9 Nov 2023 19:04:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51804 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345688AbjKJAD0 (ORCPT ); Thu, 9 Nov 2023 19:03:26 -0500 Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8087555B6; Thu, 9 Nov 2023 16:01:23 -0800 (PST) Received: by mail-pg1-x52b.google.com with SMTP id 41be03b00d2f7-5bd6ac9833fso1121352a12.0; Thu, 09 Nov 2023 16:01:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699574483; x=1700179283; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=kvTfv482Q4PYRjTpfYUAW6Q8pGRZjJf+S0DM/gjie/M=; b=goKoS3+hEtjNEmwDoegPLNieykU8JjffpOH06hD2FsqPQGlUtZC5fLBwHWs0mGwlQq miISCxD20AaU0X69+TgDviwyRQD8nyXUb97ewKtGppxttK2gQRF+7LFraPb+MMsteD2a jzFxkpGnOH0xJICANwiU4rO37K9sb4h6E/Br2q/VQrNr+XkBhHOwcXPTcJXevKVeaWgG UkFT72H4usOwDfQJtVJ5Ox4gGv6YQjVk7BXXYonuru/UJA89q/9b9DrfJa1Vw59cApXQ d1P8XrHvr3UomIU0nzsUfaLNQo3h1hnwcYmGIy8BP2yEheEgtE3w/6v4Auo4J71KRhFK VOGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699574483; x=1700179283; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=kvTfv482Q4PYRjTpfYUAW6Q8pGRZjJf+S0DM/gjie/M=; b=hcum198OQalSh4LH3VDWw5mQv/k1HTdIBSRHrxSJkSLvpjsLRjpzOM7tgqBd/QqOP+ FEX/P91JRNiR9xpQb/SD8kGqA9ijUMHppMmfmRLW7V3mI3c8IEyj5P5lSZrjpEp863mI yHJZtFwSobR38+f4PLBCVI+Uz2/My1AgK6kTIkjeWkMrViOnJX7ResmGmXwpKyJn3Nq1 a4hxqK9TwOKsOmwVZEBiFz7YA8bTpOpRyDf+7TKGlMhN9epFeKekTEJ9ewsq7LqGA4Ev /C3RgYgdadlWirkvD2PRp2ucLd80rgEJDO/dx8Qfbsh20WSkxowaTeuJ0IytCduxE6HE CX1A== X-Gm-Message-State: AOJu0YxuiarsaI5Ams+NP8RrxU9tKakACP4OiUDpY+NQNv5xsNp/9t1S RO3eqACzSPgEyH2mQE5QW0o= X-Received: by 2002:a17:90b:1a91:b0:27d:114e:d4a3 with SMTP id ng17-20020a17090b1a9100b0027d114ed4a3mr3003610pjb.14.1699574482871; Thu, 09 Nov 2023 16:01:22 -0800 (PST) Received: from bangji.corp.google.com ([2620:15c:2c0:5:d45c:ae16:d959:a5f1]) by smtp.gmail.com with ESMTPSA id k32-20020a17090a4ca300b002635db431a0sm371312pjh.45.2023.11.09.16.01.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 16:01:22 -0800 (PST) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa , Peter Zijlstra Cc: Ian Rogers , Adrian Hunter , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Linus Torvalds , Stephane Eranian , Masami Hiramatsu , Andi Kleen , linux-trace-devel@vger.kernel.org, linux-toolchains@vger.kernel.org Subject: [PATCH 43/52] perf annotate-data: Maintain variable type info Date: Thu, 9 Nov 2023 16:00:02 -0800 Message-ID: <20231110000012.3538610-44-namhyung@kernel.org> X-Mailer: git-send-email 2.42.0.869.gea05f2083d-goog In-Reply-To: <20231110000012.3538610-1-namhyung@kernel.org> References: <20231110000012.3538610-1-namhyung@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Thu, 09 Nov 2023 16:07:10 -0800 (PST) X-Spam-Status: No, score=-1.0 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782133414202472968 X-GMAIL-MSGID: 1782133414202472968 As it collected basic block and variable information in each scope, it now can build a state table to find matching variable at the location. The struct type_state is to keep the type info saved in each register and stack slot. The update_var_state() updates the table when it finds variables in the current address. It expects die_collect_vars() filled a list of variables with type info and starting address. Signed-off-by: Namhyung Kim --- tools/perf/util/annotate-data.c | 155 ++++++++++++++++++++++++++++++++ tools/perf/util/annotate-data.h | 29 ++++++ tools/perf/util/dwarf-aux.c | 4 + 3 files changed, 188 insertions(+) diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-data.c index c61f5b5b6adc..438d6234020b 100644 --- a/tools/perf/util/annotate-data.c +++ b/tools/perf/util/annotate-data.c @@ -22,6 +22,57 @@ #include "symbol.h" #include "symbol_conf.h" +/* Type information in a register, valid when ok is true */ +struct type_state_reg { + Dwarf_Die type; + bool ok; + bool scratch; +}; + +/* Type information in a stack location, dynamically allocated */ +struct type_state_stack { + struct list_head list; + Dwarf_Die type; + int offset; + int size; + bool compound; +}; + +/* FIXME: This should be arch-dependent */ +#define TYPE_STATE_MAX_REGS 16 + +/* + * State table to maintain type info in each register and stack location. + * It'll be updated when new variable is allocated or type info is moved + * to a new location (register or stack). As it'd be used with the + * shortest path of basic blocks, it only maintains a single table. + */ +struct type_state { + struct type_state_reg regs[TYPE_STATE_MAX_REGS]; + struct list_head stack_vars; +}; + +static bool has_reg_type(struct type_state *state, int reg) +{ + return (unsigned)reg < ARRAY_SIZE(state->regs); +} + +void init_type_state(struct type_state *state, struct arch *arch __maybe_unused) +{ + memset(state, 0, sizeof(*state)); + INIT_LIST_HEAD(&state->stack_vars); +} + +void exit_type_state(struct type_state *state) +{ + struct type_state_stack *stack, *tmp; + + list_for_each_entry_safe(stack, tmp, &state->stack_vars, list) { + list_del(&stack->list); + free(stack); + } +} + /* * Compare type name and size to maintain them in a tree. * I'm not sure if DWARF would have information of a single type in many @@ -237,6 +288,110 @@ static int check_variable(Dwarf_Die *var_die, Dwarf_Die *type_die, int offset, return 0; } +static struct type_state_stack *find_stack_state(struct type_state *state, + int offset) +{ + struct type_state_stack *stack; + + list_for_each_entry(stack, &state->stack_vars, list) { + if (offset == stack->offset) + return stack; + + if (stack->compound && stack->offset < offset && + offset < stack->offset + stack->size) + return stack; + } + return NULL; +} + +static void set_stack_state(struct type_state_stack *stack, int offset, + Dwarf_Die *type_die) +{ + int tag; + Dwarf_Word size; + + if (dwarf_aggregate_size(type_die, &size) < 0) + size = 0; + + tag = dwarf_tag(type_die); + + stack->type = *type_die; + stack->size = size; + stack->offset = offset; + + switch (tag) { + case DW_TAG_structure_type: + case DW_TAG_union_type: + stack->compound = true; + break; + default: + stack->compound = false; + break; + } +} + +static struct type_state_stack *findnew_stack_state(struct type_state *state, + int offset, Dwarf_Die *type_die) +{ + struct type_state_stack *stack = find_stack_state(state, offset); + + if (stack) { + set_stack_state(stack, offset, type_die); + return stack; + } + + stack = malloc(sizeof(*stack)); + if (stack) { + set_stack_state(stack, offset, type_die); + list_add(&stack->list, &state->stack_vars); + } + return stack; +} + +/** + * update_var_state - Update type state using given variables + * @state: type state table + * @dloc: data location info + * @addr: instruction address to update + * @var_types: list of variables with type info + * + * This function fills the @state table using @var_types info. Each variable + * is used only at the given location and updates an entry in the table. + */ +void update_var_state(struct type_state *state, struct data_loc_info *dloc, + u64 addr, struct die_var_type *var_types) +{ + Dwarf_Die mem_die; + struct die_var_type *var; + int fbreg = dloc->fbreg; + int fb_offset = 0; + + if (dloc->fb_cfa) { + if (die_get_cfa(dloc->di->dbg, addr, &fbreg, &fb_offset) < 0) + fbreg = -1; + } + + for (var = var_types; var != NULL; var = var->next) { + if (var->addr != addr) + continue; + /* Get the type DIE using the offset */ + if (!dwarf_offdie(dloc->di->dbg, var->die_off, &mem_die)) + continue; + + if (var->reg == DWARF_REG_FB) { + findnew_stack_state(state, var->offset, &mem_die); + } else if (var->reg == fbreg) { + findnew_stack_state(state, var->offset - fb_offset, &mem_die); + } else if (has_reg_type(state, var->reg)) { + struct type_state_reg *reg; + + reg = &state->regs[var->reg]; + reg->type = mem_die; + reg->ok = true; + } + } +} + /* The result will be saved in @type_die */ static int find_data_type_die(struct data_loc_info *dloc, Dwarf_Die *type_die) { diff --git a/tools/perf/util/annotate-data.h b/tools/perf/util/annotate-data.h index ad6493ea2c8e..7fbb9eb2e96f 100644 --- a/tools/perf/util/annotate-data.h +++ b/tools/perf/util/annotate-data.h @@ -8,9 +8,12 @@ #include struct annotated_op_loc; +struct arch; struct debuginfo; +struct die_var_type; struct evsel; struct map_symbol; +struct type_state; /** * struct annotated_member - Type of member field @@ -146,6 +149,16 @@ int annotated_data_type__update_samples(struct annotated_data_type *adt, /* Release all data type information in the tree */ void annotated_data_type__tree_delete(struct rb_root *root); +/* Initialize type state table */ +void init_type_state(struct type_state *state, struct arch *arch); + +/* Destroy type state table */ +void exit_type_state(struct type_state *state); + +/* Update type state table using variables */ +void update_var_state(struct type_state *state, struct data_loc_info *dloc, + u64 addr, struct die_var_type *var_types); + #else /* HAVE_DWARF_SUPPORT */ static inline struct annotated_data_type * @@ -168,6 +181,22 @@ static inline void annotated_data_type__tree_delete(struct rb_root *root __maybe { } +static inline void init_type_state(struct type_state *state __maybe_unused, + struct arch *arch __maybe_unused) +{ +} + +static inline void exit_type_state(struct type_state *state __maybe_unused) +{ +} + +static inline void update_var_state(struct type_state *state __maybe_unused, + struct data_loc_info *dloc __maybe_unused, + u64 addr __maybe_unused, + struct die_var_type *var_types __maybe_unused) +{ +} + #endif /* HAVE_DWARF_SUPPORT */ #endif /* _PERF_ANNOTATE_DATA_H */ diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c index 39851ff1d5c4..f88a8fb4a350 100644 --- a/tools/perf/util/dwarf-aux.c +++ b/tools/perf/util/dwarf-aux.c @@ -9,6 +9,7 @@ #include #include "debug.h" #include "dwarf-aux.h" +#include "dwarf-regs.h" #include "strbuf.h" #include "string2.h" @@ -1147,6 +1148,8 @@ static int reg_from_dwarf_op(Dwarf_Op *op) case DW_OP_regx: case DW_OP_bregx: return op->number; + case DW_OP_fbreg: + return DWARF_REG_FB; default: break; } @@ -1160,6 +1163,7 @@ static int offset_from_dwarf_op(Dwarf_Op *op) case DW_OP_regx: return 0; case DW_OP_breg0 ... DW_OP_breg31: + case DW_OP_fbreg: return op->number; case DW_OP_bregx: return op->number2;