From nobody Fri Dec 19 17:54:43 2025 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 24AE814D2B8; Fri, 16 Aug 2024 23:58:43 +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=1723852724; cv=none; b=EyLp7JhrszwNAfz3/d+iEeA6zxCO4AeoyM+xr5pUayRG2HvNlUehgNA6cvoRwgDnAeFMBioX8iEhBdHszMONZ6dIcIpavcC2mJX2r5q7RNPN3mC+H3SET8u5NAQLjYnEebw6krhHydFYcLDnsyGRkjlnf4mkFHW0rqWZ5e4akD4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723852724; c=relaxed/simple; bh=WFqLrUsJ90ZZBi2rRNTqQUEvO6udoH82ef5MZBqnMCA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Wfol/UKVK/Fxod8v1qQGddbxCj5QTvS2RpREVQw+HTnM9/EUwOs5IWemLPAVSCDgO6vnNpzzQJBK2+YWp3+/n5QDT6p8CrSaFbEvVAX0IHJdoz0d9w3Cc4f9m9wZIdr9ibe5T3Pa0nlmPCAo0DKg2rZqG2hWHuoXSVtfY2jxAGs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=o7g6GFtR; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="o7g6GFtR" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 637E4C4AF09; Fri, 16 Aug 2024 23:58:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723852723; bh=WFqLrUsJ90ZZBi2rRNTqQUEvO6udoH82ef5MZBqnMCA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=o7g6GFtRBCqdz+ABbzaiBg5N/cti+AgbeoTBvQ8SOWx2OQfdnh7LuDemyM4Y9IZ30 vWnjKCbAMfmLm7M8IkeBGFbtwls6DIxkewV1qlzjD6sV6Yfp8v9qf7zrezwLTJriTq 47+qUsvbB+wSRK+Tybd4LAEqnMiRWzqBMnVUjjiWBSTRsujGsLNzIys5KeeKImUWLX l1E481GWFOl+bNgy9GnV4UHlVnA2t8Zviqa+z+7pPQyT/wgiiT20XB4yaUiD0TgDLI OaXEn9BA+mSCL6qwznGuQCHkkb/AFJ57aAlwQBkG1dM5v9z7MsF4re/CmZQwtEO+Jy EnsSQdV/HAU7A== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers , Kan Liang Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Athira Rajeev Subject: [PATCH 5/9] perf annotate-data: Change return type of find_data_type_block() Date: Fri, 16 Aug 2024 16:58:35 -0700 Message-ID: <20240816235840.2754937-6-namhyung@kernel.org> X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog In-Reply-To: <20240816235840.2754937-1-namhyung@kernel.org> References: <20240816235840.2754937-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" So that it can return enum variable_match_type to be propagated to the find_data_type_die(). Also update the debug message to show the result of the check_matching_type(). chk [dd] reg0 offset=3D0 ok=3D1 kind=3D1 : Good! or chk [177] reg4 offset=3D0x138 ok=3D0 kind=3D0 cfa : no type information Signed-off-by: Namhyung Kim --- tools/perf/util/annotate-data.c | 117 ++++++++++++++++---------------- 1 file changed, 58 insertions(+), 59 deletions(-) diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-dat= a.c index 332254da49be..5548cd8e84ba 100644 --- a/tools/perf/util/annotate-data.c +++ b/tools/perf/util/annotate-data.c @@ -352,6 +352,7 @@ enum type_match_result { PERF_TMR_NO_POINTER, PERF_TMR_NO_SIZE, PERF_TMR_BAD_OFFSET, + PERF_TMR_BAIL_OUT, }; =20 static const char *match_result_str(enum type_match_result tmr) @@ -368,6 +369,7 @@ static const char *match_result_str(enum type_match_res= ult tmr) case PERF_TMR_BAD_OFFSET: return "offset bigger than size"; case PERF_TMR_UNKNOWN: + case PERF_TMR_BAIL_OUT: default: return "invalid state"; } @@ -868,19 +870,19 @@ static void setup_stack_canary(struct data_loc_info *= dloc) =20 /* * It's at the target address, check if it has a matching type. - * It returns 1 if found, 0 if not or -1 if not found but no need to - * repeat the search. The last case is for per-cpu variables which + * It returns PERF_TMR_BAIL_OUT when it looks up per-cpu variables which * are similar to global variables and no additional info is needed. */ -static int check_matching_type(struct type_state *state, - struct data_loc_info *dloc, - Dwarf_Die *cu_die, Dwarf_Die *type_die) +static enum type_match_result check_matching_type(struct type_state *state, + struct data_loc_info *dloc, + Dwarf_Die *cu_die, + Dwarf_Die *type_die) { Dwarf_Word size; u32 insn_offset =3D dloc->ip - dloc->ms->sym->start; int reg =3D dloc->op->reg1; =20 - pr_debug_dtp("chk [%x] reg%d offset=3D%#x ok=3D%d kind=3D%d", + pr_debug_dtp("chk [%x] reg%d offset=3D%#x ok=3D%d kind=3D%d ", insn_offset, reg, dloc->op->offset, state->regs[reg].ok, state->regs[reg].kind); =20 @@ -896,15 +898,12 @@ static int check_matching_type(struct type_state *sta= te, if (dloc->op->offset < 0 && reg !=3D state->stack_reg) goto check_kernel; =20 - pr_debug_dtp("\n"); - return -1; + return PERF_TMR_NO_POINTER; } =20 - pr_debug_dtp("\n"); - /* Remove the pointer and get the target type */ if (__die_get_real_type(&state->regs[reg].type, type_die) =3D=3D NULL) - return -1; + return PERF_TMR_NO_POINTER; =20 dloc->type_offset =3D dloc->op->offset; =20 @@ -916,33 +915,33 @@ static int check_matching_type(struct type_state *sta= te, /* Get the size of the actual type */ if (dwarf_aggregate_size(&sized_type, &size) < 0 || (unsigned)dloc->type_offset >=3D size) - return -1; + return PERF_TMR_BAD_OFFSET; =20 - return 1; + return PERF_TMR_OK; } =20 if (reg =3D=3D dloc->fbreg) { struct type_state_stack *stack; =20 - pr_debug_dtp(" fbreg\n"); + pr_debug_dtp("fbreg"); =20 stack =3D find_stack_state(state, dloc->type_offset); if (stack =3D=3D NULL) - return 0; + return PERF_TMR_NO_TYPE; =20 if (stack->kind =3D=3D TSR_KIND_CANARY) { setup_stack_canary(dloc); - return -1; + return PERF_TMR_BAIL_OUT; } =20 if (stack->kind !=3D TSR_KIND_TYPE) - return 0; + return PERF_TMR_NO_TYPE; =20 *type_die =3D stack->type; /* Update the type offset from the start of slot */ dloc->type_offset -=3D stack->offset; =20 - return 1; + return PERF_TMR_OK; } =20 if (dloc->fb_cfa) { @@ -950,38 +949,38 @@ static int check_matching_type(struct type_state *sta= te, u64 pc =3D map__rip_2objdump(dloc->ms->map, dloc->ip); int fbreg, fboff; =20 - pr_debug_dtp(" cfa\n"); + pr_debug_dtp("cfa"); =20 if (die_get_cfa(dloc->di->dbg, pc, &fbreg, &fboff) < 0) fbreg =3D -1; =20 if (reg !=3D fbreg) - return 0; + return PERF_TMR_NO_TYPE; =20 stack =3D find_stack_state(state, dloc->type_offset - fboff); if (stack =3D=3D NULL) - return 0; + return PERF_TMR_NO_TYPE; =20 if (stack->kind =3D=3D TSR_KIND_CANARY) { setup_stack_canary(dloc); - return -1; + return PERF_TMR_BAIL_OUT; } =20 if (stack->kind !=3D TSR_KIND_TYPE) - return 0; + return PERF_TMR_NO_TYPE; =20 *type_die =3D stack->type; /* Update the type offset from the start of slot */ dloc->type_offset -=3D fboff + stack->offset; =20 - return 1; + return PERF_TMR_OK; } =20 if (state->regs[reg].kind =3D=3D TSR_KIND_PERCPU_BASE) { u64 var_addr =3D dloc->op->offset; int var_offset; =20 - pr_debug_dtp(" percpu var\n"); + pr_debug_dtp("percpu var"); =20 if (dloc->op->multi_regs) { int reg2 =3D dloc->op->reg2; @@ -997,14 +996,14 @@ static int check_matching_type(struct type_state *sta= te, if (get_global_var_type(cu_die, dloc, dloc->ip, var_addr, &var_offset, type_die)) { dloc->type_offset =3D var_offset; - return 1; + return PERF_TMR_OK; } /* No need to retry per-cpu (global) variables */ - return -1; + return PERF_TMR_BAIL_OUT; } =20 if (state->regs[reg].ok && state->regs[reg].kind =3D=3D TSR_KIND_POINTER)= { - pr_debug_dtp(" percpu ptr\n"); + pr_debug_dtp("percpu ptr"); =20 /* * It's actaully pointer but the address was calculated using @@ -1017,13 +1016,13 @@ static int check_matching_type(struct type_state *s= tate, /* Get the size of the actual type */ if (dwarf_aggregate_size(type_die, &size) < 0 || (unsigned)dloc->type_offset >=3D size) - return -1; + return PERF_TMR_BAIL_OUT; =20 - return 1; + return PERF_TMR_OK; } =20 if (state->regs[reg].ok && state->regs[reg].kind =3D=3D TSR_KIND_CANARY) { - pr_debug_dtp(" stack canary\n"); + pr_debug_dtp("stack canary"); =20 /* * This is a saved value of the stack canary which will be handled @@ -1032,7 +1031,7 @@ static int check_matching_type(struct type_state *sta= te, */ setup_stack_canary(dloc); =20 - return -1; + return PERF_TMR_BAIL_OUT; } =20 check_kernel: @@ -1043,16 +1042,16 @@ static int check_matching_type(struct type_state *s= tate, /* Direct this-cpu access like "%gs:0x34740" */ if (dloc->op->segment =3D=3D INSN_SEG_X86_GS && dloc->op->imm && arch__is(dloc->arch, "x86")) { - pr_debug_dtp(" this-cpu var\n"); + pr_debug_dtp("this-cpu var"); =20 addr =3D dloc->op->offset; =20 if (get_global_var_type(cu_die, dloc, dloc->ip, addr, &offset, type_die)) { dloc->type_offset =3D offset; - return 1; + return PERF_TMR_OK; } - return -1; + return PERF_TMR_BAIL_OUT; } =20 /* Access to global variable like "-0x7dcf0500(,%rdx,8)" */ @@ -1061,31 +1060,30 @@ static int check_matching_type(struct type_state *s= tate, =20 if (get_global_var_type(cu_die, dloc, dloc->ip, addr, &offset, type_die)) { - pr_debug_dtp(" global var\n"); + pr_debug_dtp("global var"); =20 dloc->type_offset =3D offset; - return 1; + return PERF_TMR_OK; } - pr_debug_dtp(" negative offset\n"); - return -1; + return PERF_TMR_BAIL_OUT; } } =20 - pr_debug_dtp("\n"); - return 0; + return PERF_TMR_UNKNOWN; } =20 /* Iterate instructions in basic blocks and update type table */ -static int find_data_type_insn(struct data_loc_info *dloc, - struct list_head *basic_blocks, - struct die_var_type *var_types, - Dwarf_Die *cu_die, Dwarf_Die *type_die) +static enum type_match_result find_data_type_insn(struct data_loc_info *dl= oc, + struct list_head *basic_blocks, + struct die_var_type *var_types, + Dwarf_Die *cu_die, + Dwarf_Die *type_die) { struct type_state state; struct symbol *sym =3D dloc->ms->sym; struct annotation *notes =3D symbol__annotation(sym); struct annotated_basic_block *bb; - int ret =3D 0; + enum type_match_result ret =3D PERF_TMR_UNKNOWN; =20 init_type_state(&state, dloc->arch); =20 @@ -1111,6 +1109,7 @@ static int find_data_type_insn(struct data_loc_info *= dloc, if (this_ip =3D=3D dloc->ip) { ret =3D check_matching_type(&state, dloc, cu_die, type_die); + pr_debug_dtp(" : %s\n", match_result_str(ret)); goto out; } =20 @@ -1137,24 +1136,25 @@ static int arch_supports_insn_tracking(struct data_= loc_info *dloc) * Construct a list of basic blocks for each scope with variables and try = to find * the data type by updating a type state table through instructions. */ -static int find_data_type_block(struct data_loc_info *dloc, - Dwarf_Die *cu_die, Dwarf_Die *scopes, - int nr_scopes, Dwarf_Die *type_die) +static enum type_match_result find_data_type_block(struct data_loc_info *d= loc, + Dwarf_Die *cu_die, + Dwarf_Die *scopes, + int nr_scopes, + Dwarf_Die *type_die) { LIST_HEAD(basic_blocks); struct die_var_type *var_types =3D NULL; u64 src_ip, dst_ip, prev_dst_ip; - int ret =3D -1; + enum type_match_result ret =3D PERF_TMR_UNKNOWN; =20 /* TODO: other architecture support */ if (!arch_supports_insn_tracking(dloc)) - return -1; + return PERF_TMR_BAIL_OUT; =20 prev_dst_ip =3D dst_ip =3D dloc->ip; for (int i =3D nr_scopes - 1; i >=3D 0; i--) { Dwarf_Addr base, start, end; LIST_HEAD(this_blocks); - int found; =20 if (dwarf_ranges(&scopes[i], 0, &base, &start, &end) < 0) break; @@ -1185,9 +1185,9 @@ static int find_data_type_block(struct data_loc_info = *dloc, fixup_var_address(var_types, start); =20 /* Find from start of this scope to the target instruction */ - found =3D find_data_type_insn(dloc, &basic_blocks, var_types, + ret =3D find_data_type_insn(dloc, &basic_blocks, var_types, cu_die, type_die); - if (found > 0) { + if (ret =3D=3D PERF_TMR_OK) { char buf[64]; =20 if (dloc->op->multi_regs) @@ -1199,11 +1199,10 @@ static int find_data_type_block(struct data_loc_inf= o *dloc, pr_debug_dtp("found by insn track: %#x(%s) type-offset=3D%#x\n", dloc->op->offset, buf, dloc->type_offset); pr_debug_type_name(type_die, TSR_KIND_TYPE); - ret =3D 0; break; } =20 - if (found < 0) + if (ret =3D=3D PERF_TMR_BAIL_OUT) break; =20 /* Go up to the next scope and find blocks to the start */ @@ -1357,11 +1356,11 @@ static int find_data_type_die(struct data_loc_info = *dloc, Dwarf_Die *type_die) } =20 if (reg !=3D DWARF_REG_PC) { - ret =3D find_data_type_block(dloc, &cu_die, scopes, + result =3D find_data_type_block(dloc, &cu_die, scopes, nr_scopes, type_die); - if (ret =3D=3D 0) { + if (result =3D=3D PERF_TMR_OK) { ann_data_stat.insn_track++; - goto out; + ret =3D 0; } } =20 --=20 2.46.0.184.g6999bdac58-goog