Skip to content

Commit 24a30ce

Browse files
Zecheng Linamhyung
authored andcommitted
perf annotate: Track address registers via TSR_KIND_POINTER
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>
1 parent 068b6a4 commit 24a30ce

File tree

3 files changed

+71
-8
lines changed

3 files changed

+71
-8
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ static void update_insn_state_x86(struct type_state *state,
391391
tsr->ok = true;
392392

393393
/* To copy back the variable type later (hopefully) */
394-
if (tsr->kind == TSR_KIND_TYPE)
394+
if (tsr->kind == TSR_KIND_TYPE || tsr->kind == TSR_KIND_POINTER)
395395
tsr->copied_from = src->reg1;
396396

397397
pr_debug_dtp("mov [%x] reg%d -> reg%d",
@@ -455,6 +455,19 @@ static void update_insn_state_x86(struct type_state *state,
455455
insn_offset, src->offset, sreg, dst->reg1);
456456
pr_debug_type_name(&tsr->type, tsr->kind);
457457
}
458+
/* Handle dereference of TSR_KIND_POINTER registers */
459+
else if (has_reg_type(state, sreg) && state->regs[sreg].ok &&
460+
state->regs[sreg].kind == TSR_KIND_POINTER &&
461+
die_get_member_type(&state->regs[sreg].type,
462+
src->offset, &type_die)) {
463+
tsr->type = state->regs[sreg].type;
464+
tsr->kind = TSR_KIND_TYPE;
465+
tsr->ok = true;
466+
467+
pr_debug_dtp("mov [%x] addr %#x(reg%d) -> reg%d",
468+
insn_offset, src->offset, sreg, dst->reg1);
469+
pr_debug_type_name(&tsr->type, tsr->kind);
470+
}
458471
/* Or check if it's a global variable */
459472
else if (sreg == DWARF_REG_PC) {
460473
struct map_symbol *ms = dloc->ms;

tools/perf/util/annotate-data.c

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ void pr_debug_type_name(Dwarf_Die *die, enum type_state_kind kind)
5959
pr_info(" constant\n");
6060
return;
6161
case TSR_KIND_PERCPU_POINTER:
62+
pr_info(" percpu pointer");
63+
/* it also prints the type info */
64+
break;
65+
case TSR_KIND_POINTER:
6266
pr_info(" pointer");
6367
/* it also prints the type info */
6468
break;
@@ -578,16 +582,25 @@ void set_stack_state(struct type_state_stack *stack, int offset, u8 kind,
578582
int tag;
579583
Dwarf_Word size;
580584

581-
if (dwarf_aggregate_size(type_die, &size) < 0)
585+
if (kind == TSR_KIND_POINTER) {
586+
/* TODO: arch-dependent pointer size */
587+
size = sizeof(void *);
588+
}
589+
else if (dwarf_aggregate_size(type_die, &size) < 0)
582590
size = 0;
583591

584-
tag = dwarf_tag(type_die);
585-
586592
stack->type = *type_die;
587593
stack->size = size;
588594
stack->offset = offset;
589595
stack->kind = kind;
590596

597+
if (kind == TSR_KIND_POINTER) {
598+
stack->compound = false;
599+
return;
600+
}
601+
602+
tag = dwarf_tag(type_die);
603+
591604
switch (tag) {
592605
case DW_TAG_structure_type:
593606
case DW_TAG_union_type:
@@ -898,13 +911,25 @@ static void update_var_state(struct type_state *state, struct data_loc_info *dlo
898911

899912
reg = &state->regs[var->reg];
900913

901-
/* For gp registers, skip the address registers for now */
902-
if (var->is_reg_var_addr)
914+
if (reg->ok && reg->kind == TSR_KIND_TYPE &&
915+
(!is_better_type(&reg->type, &mem_die) || var->is_reg_var_addr))
903916
continue;
904917

905-
if (reg->ok && reg->kind == TSR_KIND_TYPE &&
906-
!is_better_type(&reg->type, &mem_die))
918+
/* Handle address registers with TSR_KIND_POINTER */
919+
if (var->is_reg_var_addr) {
920+
if (reg->ok && reg->kind == TSR_KIND_POINTER &&
921+
!is_better_type(&reg->type, &mem_die))
922+
continue;
923+
924+
reg->type = mem_die;
925+
reg->kind = TSR_KIND_POINTER;
926+
reg->ok = true;
927+
928+
pr_debug_dtp("var [%"PRIx64"] reg%d addr offset %x",
929+
insn_offset, var->reg, var->offset);
930+
pr_debug_type_name(&mem_die, TSR_KIND_POINTER);
907931
continue;
932+
}
908933

909934
orig_type = reg->type;
910935

@@ -1116,6 +1141,30 @@ static enum type_match_result check_matching_type(struct type_state *state,
11161141
return PERF_TMR_OK;
11171142
}
11181143

1144+
if (state->regs[reg].kind == TSR_KIND_POINTER) {
1145+
struct strbuf sb;
1146+
1147+
strbuf_init(&sb, 32);
1148+
die_get_typename_from_type(&state->regs[reg].type, &sb);
1149+
pr_debug_dtp("(ptr->%s)", sb.buf);
1150+
strbuf_release(&sb);
1151+
1152+
/*
1153+
* Register holds a pointer (address) to the target variable.
1154+
* The type is the type of the variable it points to.
1155+
*/
1156+
*type_die = state->regs[reg].type;
1157+
1158+
dloc->type_offset = dloc->op->offset;
1159+
1160+
/* Get the size of the actual type */
1161+
if (dwarf_aggregate_size(type_die, &size) < 0 ||
1162+
(unsigned)dloc->type_offset >= size)
1163+
return PERF_TMR_BAD_OFFSET;
1164+
1165+
return PERF_TMR_OK;
1166+
}
1167+
11191168
if (state->regs[reg].kind == TSR_KIND_PERCPU_POINTER) {
11201169
pr_debug_dtp("percpu ptr");
11211170

tools/perf/util/annotate-data.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ enum type_state_kind {
3535
TSR_KIND_PERCPU_BASE,
3636
TSR_KIND_CONST,
3737
TSR_KIND_PERCPU_POINTER,
38+
TSR_KIND_POINTER,
3839
TSR_KIND_CANARY,
3940
};
4041

0 commit comments

Comments
 (0)