perf dwarf-aux: Add die_collect_vars()

The die_collect_vars() is to find all variable information in the scope
including function parameters.  The struct die_var_type is to save the
type of the variable with the location (reg and offset) as well as where
it's defined in the code (addr).

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Acked-by: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20240319055115.4063940-3-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
Namhyung Kim 2024-03-18 22:50:54 -07:00 committed by Arnaldo Carvalho de Melo
parent b508965d35
commit 932dcc2c39
2 changed files with 111 additions and 32 deletions

View file

@ -1136,6 +1136,40 @@ int die_get_varname(Dwarf_Die *vr_die, struct strbuf *buf)
return ret < 0 ? ret : strbuf_addf(buf, "\t%s", dwarf_diename(vr_die));
}
#if defined(HAVE_DWARF_GETLOCATIONS_SUPPORT) || defined(HAVE_DWARF_CFI_SUPPORT)
static int reg_from_dwarf_op(Dwarf_Op *op)
{
switch (op->atom) {
case DW_OP_reg0 ... DW_OP_reg31:
return op->atom - DW_OP_reg0;
case DW_OP_breg0 ... DW_OP_breg31:
return op->atom - DW_OP_breg0;
case DW_OP_regx:
case DW_OP_bregx:
return op->number;
default:
break;
}
return -1;
}
static int offset_from_dwarf_op(Dwarf_Op *op)
{
switch (op->atom) {
case DW_OP_reg0 ... DW_OP_reg31:
case DW_OP_regx:
return 0;
case DW_OP_breg0 ... DW_OP_breg31:
return op->number;
case DW_OP_bregx:
return op->number2;
default:
break;
}
return -1;
}
#endif /* HAVE_DWARF_GETLOCATIONS_SUPPORT || HAVE_DWARF_CFI_SUPPORT */
#ifdef HAVE_DWARF_GETLOCATIONS_SUPPORT
/**
* die_get_var_innermost_scope - Get innermost scope range of given variable DIE
@ -1476,41 +1510,69 @@ Dwarf_Die *die_find_variable_by_addr(Dwarf_Die *sc_die, Dwarf_Addr addr,
*offset = data.offset;
return result;
}
static int __die_collect_vars_cb(Dwarf_Die *die_mem, void *arg)
{
struct die_var_type **var_types = arg;
Dwarf_Die type_die;
int tag = dwarf_tag(die_mem);
Dwarf_Attribute attr;
Dwarf_Addr base, start, end;
Dwarf_Op *ops;
size_t nops;
struct die_var_type *vt;
if (tag != DW_TAG_variable && tag != DW_TAG_formal_parameter)
return DIE_FIND_CB_SIBLING;
if (dwarf_attr(die_mem, DW_AT_location, &attr) == NULL)
return DIE_FIND_CB_SIBLING;
/*
* Only collect the first location as it can reconstruct the
* remaining state by following the instructions.
* start = 0 means it covers the whole range.
*/
if (dwarf_getlocations(&attr, 0, &base, &start, &end, &ops, &nops) <= 0)
return DIE_FIND_CB_SIBLING;
if (die_get_real_type(die_mem, &type_die) == NULL)
return DIE_FIND_CB_SIBLING;
vt = malloc(sizeof(*vt));
if (vt == NULL)
return DIE_FIND_CB_END;
vt->die_off = dwarf_dieoffset(&type_die);
vt->addr = start;
vt->reg = reg_from_dwarf_op(ops);
vt->offset = offset_from_dwarf_op(ops);
vt->next = *var_types;
*var_types = vt;
return DIE_FIND_CB_SIBLING;
}
/**
* die_collect_vars - Save all variables and parameters
* @sc_die: a scope DIE
* @var_types: a pointer to save the resulting list
*
* Save all variables and parameters in the @sc_die and save them to @var_types.
* The @var_types is a singly-linked list containing type and location info.
* Actual type can be retrieved using dwarf_offdie() with 'die_off' later.
*
* Callers should free @var_types.
*/
void die_collect_vars(Dwarf_Die *sc_die, struct die_var_type **var_types)
{
Dwarf_Die die_mem;
die_find_child(sc_die, __die_collect_vars_cb, (void *)var_types, &die_mem);
}
#endif /* HAVE_DWARF_GETLOCATIONS_SUPPORT */
#ifdef HAVE_DWARF_CFI_SUPPORT
static int reg_from_dwarf_op(Dwarf_Op *op)
{
switch (op->atom) {
case DW_OP_reg0 ... DW_OP_reg31:
return op->atom - DW_OP_reg0;
case DW_OP_breg0 ... DW_OP_breg31:
return op->atom - DW_OP_breg0;
case DW_OP_regx:
case DW_OP_bregx:
return op->number;
default:
break;
}
return -1;
}
static int offset_from_dwarf_op(Dwarf_Op *op)
{
switch (op->atom) {
case DW_OP_reg0 ... DW_OP_reg31:
case DW_OP_regx:
return 0;
case DW_OP_breg0 ... DW_OP_breg31:
return op->number;
case DW_OP_bregx:
return op->number2;
default:
break;
}
return -1;
}
/**
* die_get_cfa - Get frame base information
* @dwarf: a Dwarf info

View file

@ -135,6 +135,15 @@ void die_skip_prologue(Dwarf_Die *sp_die, Dwarf_Die *cu_die,
/* Get the list of including scopes */
int die_get_scopes(Dwarf_Die *cu_die, Dwarf_Addr pc, Dwarf_Die **scopes);
/* Variable type information */
struct die_var_type {
struct die_var_type *next;
u64 die_off;
u64 addr;
int reg;
int offset;
};
#ifdef HAVE_DWARF_GETLOCATIONS_SUPPORT
/* Get byte offset range of given variable DIE */
@ -149,6 +158,9 @@ Dwarf_Die *die_find_variable_by_reg(Dwarf_Die *sc_die, Dwarf_Addr pc, int reg,
Dwarf_Die *die_find_variable_by_addr(Dwarf_Die *sc_die, Dwarf_Addr addr,
Dwarf_Die *die_mem, int *offset);
/* Save all variables and parameters in this scope */
void die_collect_vars(Dwarf_Die *sc_die, struct die_var_type **var_types);
#else /* HAVE_DWARF_GETLOCATIONS_SUPPORT */
static inline int die_get_var_range(Dwarf_Die *sp_die __maybe_unused,
@ -176,6 +188,11 @@ static inline Dwarf_Die *die_find_variable_by_addr(Dwarf_Die *sc_die __maybe_unu
return NULL;
}
static inline void die_collect_vars(Dwarf_Die *sc_die __maybe_unused,
struct die_var_type **var_types __maybe_unused)
{
}
#endif /* HAVE_DWARF_GETLOCATIONS_SUPPORT */
#ifdef HAVE_DWARF_CFI_SUPPORT