From: Zecheng Li Date: Mon, 25 Aug 2025 19:54:04 +0000 (+0000) Subject: perf dwarf-aux: More accurate variable type match for breg X-Git-Tag: v6.18-rc1~35^2~108 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=8c6d842302f3d070a5f41c296f1668fd69dae468;p=thirdparty%2Fkernel%2Flinux.git perf dwarf-aux: More accurate variable type match for breg Introduces the function is_breg_access_indirect to determine whether a memory access involving a DW_OP_breg* operation refers to the variable's value directly or requires dereferencing the variable's type as a pointer based on the DWARF expression. Previously, all breg based accesses were assumed to directly access the variable's value (is_pointer = false). The is_breg_access_indirect function handles three cases: 1. Base register + offset only: (e.g., DW_OP_breg7 RSP+88) The calculated address is the location of the variable. The access is direct, so no type dereference is needed. Returns false. 2. Base register + offset, followed by other operations ending in DW_OP_stack_value, including DW_OP_deref: (e.g., DW_OP_breg*, DW_OP_deref, DW_OP_stack_value) The DWARF expression computes the variable's value, but that value requires a dereference. The memory access is fetching that value, so no type dereference is needed. Returns false. 3. Base register + offset, followed only by DW_OP_stack_value: (e.g., DW_OP_breg13 R13+256, DW_OP_stack_value) This indicates the value at the base + offset is the variable's value. Since this value is being used as an address in the memory access, the variable's type is treated as a pointer and requires a type dereference. Returns true. The is_pointer argument passed to match_var_offset is now set by is_breg_access_indirect for breg accesses. There are more complex expressions that includes multiple operations and may require additional handling, such as DW_OP_deref without a DW_OP_stack_value, or including multiple base registers. They are less common in the Linux kernel dwarf and are skipped in check_allowed_ops. Reviewed-by: Namhyung Kim Signed-off-by: Zecheng Li Cc: Adrian Hunter Cc: Alexander Shishkin Cc: Ian Rogers Cc: Ingo Molnar Cc: Jiri Olsa Cc: Kan Liang Cc: Mark Rutland Cc: Masami Hiramatsu Cc: Peter Zijlstra Cc: Xu Liu Link: https://lore.kernel.org/r/CAJUgMyK2wTiEZB__dtgCELmaNGFWhG1j0g9rv_C=cLD6Zq4_5w@mail.gmail.com Signed-off-by: Arnaldo Carvalho de Melo --- diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c index 920054425578..449bc9ad7aff 100644 --- a/tools/perf/util/dwarf-aux.c +++ b/tools/perf/util/dwarf-aux.c @@ -1423,6 +1423,34 @@ static bool match_var_offset(Dwarf_Die *die_mem, struct find_var_data *data, return true; } +/** + * is_breg_access_indirect - Check if breg based access implies type + * dereference + * @ops: DWARF operations array + * @nops: Number of operations in @ops + * + * Returns true if the DWARF expression evaluates to the variable's + * value, so the memory access on that register needs type dereference. + * Returns false if the expression evaluates to the variable's address. + * This is called after check_allowed_ops. + */ +static bool is_breg_access_indirect(Dwarf_Op *ops, size_t nops) +{ + /* only the base register */ + if (nops == 1) + return false; + + if (nops == 2 && ops[1].atom == DW_OP_stack_value) + return true; + + if (nops == 3 && (ops[1].atom == DW_OP_deref || + ops[1].atom == DW_OP_deref_size) && + ops[2].atom == DW_OP_stack_value) + return false; + /* unreachable, OP not supported */ + return false; +} + /* Only checks direct child DIEs in the given scope. */ static int __die_find_var_reg_cb(Dwarf_Die *die_mem, void *arg) { @@ -1451,7 +1479,7 @@ static int __die_find_var_reg_cb(Dwarf_Die *die_mem, void *arg) if (data->is_fbreg && ops->atom == DW_OP_fbreg && check_allowed_ops(ops, nops) && match_var_offset(die_mem, data, data->offset, ops->number, - /*is_pointer=*/false)) + is_breg_access_indirect(ops, nops))) return DIE_FIND_CB_END; /* Only match with a simple case */ @@ -1463,11 +1491,11 @@ static int __die_find_var_reg_cb(Dwarf_Die *die_mem, void *arg) /*is_pointer=*/true)) return DIE_FIND_CB_END; - /* Local variables accessed by a register + offset */ + /* variables accessed by a register + offset */ if (ops->atom == (DW_OP_breg0 + data->reg) && check_allowed_ops(ops, nops) && match_var_offset(die_mem, data, data->offset, ops->number, - /*is_pointer=*/false)) + is_breg_access_indirect(ops, nops))) return DIE_FIND_CB_END; } else { /* pointer variables saved in a register 32 or above */ @@ -1477,11 +1505,11 @@ static int __die_find_var_reg_cb(Dwarf_Die *die_mem, void *arg) /*is_pointer=*/true)) return DIE_FIND_CB_END; - /* Local variables accessed by a register + offset */ + /* variables accessed by a register + offset */ if (ops->atom == DW_OP_bregx && data->reg == ops->number && check_allowed_ops(ops, nops) && match_var_offset(die_mem, data, data->offset, ops->number2, - /*is_poitner=*/false)) + is_breg_access_indirect(ops, nops))) return DIE_FIND_CB_END; } }