]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
perf annotate: Track address registers via TSR_KIND_POINTER
authorZecheng Li <zecheng@google.com>
Mon, 13 Oct 2025 18:15:59 +0000 (18:15 +0000)
committerNamhyung Kim <namhyung@kernel.org>
Tue, 21 Oct 2025 13:02:49 +0000 (06:02 -0700)
Introduce TSR_KIND_POINTER to improve the data type profiler's ability
to track pointer-based memory accesses and address register variables.

TSR_KIND_POINTER represents that the location holds a pointer type to
the type in the type state. The semantics match the `breg` registers
that describe a memory location.

This change implements handling for this new kind in mov instructions
and in the check_matching_type() function. When a TSR_KIND_POINTER is
moved to the stack, the stack state size is set to the architecture's
pointer size.

Signed-off-by: Zecheng Li <zecheng@google.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
tools/perf/arch/x86/annotate/instructions.c
tools/perf/util/annotate-data.c
tools/perf/util/annotate-data.h

index da98a4e3c52c212e75302705a32c84ea8d37c346..746443e0408968e76ff233b06b4250af709844d0 100644 (file)
@@ -391,7 +391,7 @@ static void update_insn_state_x86(struct type_state *state,
                tsr->ok = true;
 
                /* To copy back the variable type later (hopefully) */
-               if (tsr->kind == TSR_KIND_TYPE)
+               if (tsr->kind == TSR_KIND_TYPE || tsr->kind == TSR_KIND_POINTER)
                        tsr->copied_from = src->reg1;
 
                pr_debug_dtp("mov [%x] reg%d -> reg%d",
@@ -455,6 +455,19 @@ retry:
                                     insn_offset, src->offset, sreg, dst->reg1);
                        pr_debug_type_name(&tsr->type, tsr->kind);
                }
+               /* Handle dereference of TSR_KIND_POINTER registers */
+               else if (has_reg_type(state, sreg) && state->regs[sreg].ok &&
+                        state->regs[sreg].kind == TSR_KIND_POINTER &&
+                        die_get_member_type(&state->regs[sreg].type,
+                                            src->offset, &type_die)) {
+                       tsr->type = state->regs[sreg].type;
+                       tsr->kind = TSR_KIND_TYPE;
+                       tsr->ok = true;
+
+                       pr_debug_dtp("mov [%x] addr %#x(reg%d) -> reg%d",
+                                    insn_offset, src->offset, sreg, dst->reg1);
+                       pr_debug_type_name(&tsr->type, tsr->kind);
+               }
                /* Or check if it's a global variable */
                else if (sreg == DWARF_REG_PC) {
                        struct map_symbol *ms = dloc->ms;
index 903027a6fb7d3aeebcfc46c2cf5e145496036aa8..48f3bf20070f03c08e8be246f8d28ba6feae475a 100644 (file)
@@ -59,6 +59,10 @@ void pr_debug_type_name(Dwarf_Die *die, enum type_state_kind kind)
                pr_info(" constant\n");
                return;
        case TSR_KIND_PERCPU_POINTER:
+               pr_info(" percpu pointer");
+               /* it also prints the type info */
+               break;
+       case TSR_KIND_POINTER:
                pr_info(" pointer");
                /* it also prints the type info */
                break;
@@ -578,16 +582,25 @@ void set_stack_state(struct type_state_stack *stack, int offset, u8 kind,
        int tag;
        Dwarf_Word size;
 
-       if (dwarf_aggregate_size(type_die, &size) < 0)
+       if (kind == TSR_KIND_POINTER) {
+               /* TODO: arch-dependent pointer size */
+               size = sizeof(void *);
+       }
+       else 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;
        stack->kind = kind;
 
+       if (kind == TSR_KIND_POINTER) {
+               stack->compound = false;
+               return;
+       }
+
+       tag = dwarf_tag(type_die);
+
        switch (tag) {
        case DW_TAG_structure_type:
        case DW_TAG_union_type:
@@ -898,13 +911,25 @@ static void update_var_state(struct type_state *state, struct data_loc_info *dlo
 
                        reg = &state->regs[var->reg];
 
-                       /* For gp registers, skip the address registers for now */
-                       if (var->is_reg_var_addr)
+                       if (reg->ok && reg->kind == TSR_KIND_TYPE &&
+                          (!is_better_type(&reg->type, &mem_die) || var->is_reg_var_addr))
                                continue;
 
-                       if (reg->ok && reg->kind == TSR_KIND_TYPE &&
-                           !is_better_type(&reg->type, &mem_die))
+                       /* Handle address registers with TSR_KIND_POINTER */
+                       if (var->is_reg_var_addr) {
+                               if (reg->ok && reg->kind == TSR_KIND_POINTER &&
+                                   !is_better_type(&reg->type, &mem_die))
+                                       continue;
+
+                               reg->type = mem_die;
+                               reg->kind = TSR_KIND_POINTER;
+                               reg->ok = true;
+
+                               pr_debug_dtp("var [%"PRIx64"] reg%d addr offset %x",
+                                            insn_offset, var->reg, var->offset);
+                               pr_debug_type_name(&mem_die, TSR_KIND_POINTER);
                                continue;
+                       }
 
                        orig_type = reg->type;
 
@@ -1116,6 +1141,30 @@ again:
                return PERF_TMR_OK;
        }
 
+       if (state->regs[reg].kind == TSR_KIND_POINTER) {
+               struct strbuf sb;
+
+               strbuf_init(&sb, 32);
+               die_get_typename_from_type(&state->regs[reg].type, &sb);
+               pr_debug_dtp("(ptr->%s)", sb.buf);
+               strbuf_release(&sb);
+
+               /*
+                * Register holds a pointer (address) to the target variable.
+                * The type is the type of the variable it points to.
+                */
+               *type_die = state->regs[reg].type;
+
+               dloc->type_offset = dloc->op->offset;
+
+               /* Get the size of the actual type */
+               if (dwarf_aggregate_size(type_die, &size) < 0 ||
+                   (unsigned)dloc->type_offset >= size)
+                       return PERF_TMR_BAD_OFFSET;
+
+               return PERF_TMR_OK;
+       }
+
        if (state->regs[reg].kind == TSR_KIND_PERCPU_POINTER) {
                pr_debug_dtp("percpu ptr");
 
index df52a0a1f49653fa1bb6a440f7a4294c1991b467..0267834420569f72bd18865d67f954cd442ef672 100644 (file)
@@ -35,6 +35,7 @@ enum type_state_kind {
        TSR_KIND_PERCPU_BASE,
        TSR_KIND_CONST,
        TSR_KIND_PERCPU_POINTER,
+       TSR_KIND_POINTER,
        TSR_KIND_CANARY,
 };