Skip to content

Commit 1092187

Browse files
Zecheng Linamhyung
authored andcommitted
perf annotate: Save pointer offset in stack state
The tracked pointer offset was not being preserved in the stack state, which could lead to incorrect type analysis. This change adds a ptr_offset field to the type_state_stack struct and passes it to set_stack_state and findnew_stack_state to ensure the offset is preserved after the pointer is loaded from a stack location. It improves the type annotation coverage and quality. Signed-off-by: Zecheng Li <zecheng@google.com> Signed-off-by: Namhyung Kim <namhyung@kernel.org>
1 parent 1f4cc4a commit 1092187

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

tools/perf/arch/x86/annotate/instructions.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ static void update_insn_state_x86(struct type_state *state,
541541
} else if (!stack->compound) {
542542
tsr->type = stack->type;
543543
tsr->kind = stack->kind;
544-
tsr->offset = 0;
544+
tsr->offset = stack->ptr_offset;
545545
tsr->ok = true;
546546
} else if (die_get_member_type(&stack->type,
547547
offset - stack->offset,
@@ -724,10 +724,10 @@ static void update_insn_state_x86(struct type_state *state,
724724
*/
725725
if (!stack->compound)
726726
set_stack_state(stack, offset, tsr->kind,
727-
&tsr->type);
727+
&tsr->type, tsr->offset);
728728
} else {
729729
findnew_stack_state(state, offset, tsr->kind,
730-
&tsr->type);
730+
&tsr->type, tsr->offset);
731731
}
732732

733733
if (dst->reg1 == fbreg) {

tools/perf/util/annotate-data.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ struct type_state_stack *find_stack_state(struct type_state *state,
577577
}
578578

579579
void set_stack_state(struct type_state_stack *stack, int offset, u8 kind,
580-
Dwarf_Die *type_die)
580+
Dwarf_Die *type_die, int ptr_offset)
581581
{
582582
int tag;
583583
Dwarf_Word size;
@@ -592,6 +592,7 @@ void set_stack_state(struct type_state_stack *stack, int offset, u8 kind,
592592
stack->type = *type_die;
593593
stack->size = size;
594594
stack->offset = offset;
595+
stack->ptr_offset = ptr_offset;
595596
stack->kind = kind;
596597

597598
if (kind == TSR_KIND_POINTER) {
@@ -614,18 +615,19 @@ void set_stack_state(struct type_state_stack *stack, int offset, u8 kind,
614615

615616
struct type_state_stack *findnew_stack_state(struct type_state *state,
616617
int offset, u8 kind,
617-
Dwarf_Die *type_die)
618+
Dwarf_Die *type_die,
619+
int ptr_offset)
618620
{
619621
struct type_state_stack *stack = find_stack_state(state, offset);
620622

621623
if (stack) {
622-
set_stack_state(stack, offset, kind, type_die);
624+
set_stack_state(stack, offset, kind, type_die, ptr_offset);
623625
return stack;
624626
}
625627

626628
stack = malloc(sizeof(*stack));
627629
if (stack) {
628-
set_stack_state(stack, offset, kind, type_die);
630+
set_stack_state(stack, offset, kind, type_die, ptr_offset);
629631
list_add(&stack->list, &state->stack_vars);
630632
}
631633
return stack;
@@ -895,7 +897,7 @@ static void update_var_state(struct type_state *state, struct data_loc_info *dlo
895897
continue;
896898

897899
findnew_stack_state(state, offset, TSR_KIND_TYPE,
898-
&mem_die);
900+
&mem_die, /*ptr_offset=*/0);
899901

900902
if (var->reg == state->stack_reg) {
901903
pr_debug_dtp("var [%"PRIx64"] %#x(reg%d)",

tools/perf/util/annotate-data.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ struct type_state_stack {
191191
struct list_head list;
192192
Dwarf_Die type;
193193
int offset;
194+
/* pointer offset, saves tsr->offset on the stack state */
195+
int ptr_offset;
194196
int size;
195197
bool compound;
196198
u8 kind;
@@ -247,9 +249,10 @@ int annotated_data_type__get_member_name(struct annotated_data_type *adt,
247249
bool has_reg_type(struct type_state *state, int reg);
248250
struct type_state_stack *findnew_stack_state(struct type_state *state,
249251
int offset, u8 kind,
250-
Dwarf_Die *type_die);
252+
Dwarf_Die *type_die,
253+
int ptr_offset);
251254
void set_stack_state(struct type_state_stack *stack, int offset, u8 kind,
252-
Dwarf_Die *type_die);
255+
Dwarf_Die *type_die, int ptr_offset);
253256
struct type_state_stack *find_stack_state(struct type_state *state,
254257
int offset);
255258
bool get_global_var_type(Dwarf_Die *cu_die, struct data_loc_info *dloc,

0 commit comments

Comments
 (0)