perf annotate-data: Add annotated_data_type__get_member_name()

Factor out a function to get the name of member field at the given
offset.  This will be used in other places.

Also update the output of typeoff sort key a little bit.  As we know
that some special types like (stack operation), (stack canary) and
(unknown) won't have fields, skip printing the offset and field.

For example, the following change is expected.

  "(stack operation) +0 (no field)"   ==>   "(stack operation)"

Reviewed-by: Ian Rogers <irogers@google.com>
Link: https://lore.kernel.org/r/20250310224925.799005-2-namhyung@kernel.org
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
This commit is contained in:
Namhyung Kim 2025-03-10 15:49:19 -07:00
parent e1cde2d5e9
commit ce2289ad0a
3 changed files with 54 additions and 32 deletions

View File

@ -314,6 +314,40 @@ static void delete_members(struct annotated_member *member)
}
}
static int fill_member_name(char *buf, size_t sz, struct annotated_member *m,
int offset, bool first)
{
struct annotated_member *child;
if (list_empty(&m->children))
return 0;
list_for_each_entry(child, &m->children, node) {
int len;
if (offset < child->offset || offset >= child->offset + child->size)
continue;
/* It can have anonymous struct/union members */
if (child->var_name) {
len = scnprintf(buf, sz, "%s%s",
first ? "" : ".", child->var_name);
first = false;
} else {
len = 0;
}
return fill_member_name(buf + len, sz - len, child, offset, first) + len;
}
return 0;
}
int annotated_data_type__get_member_name(struct annotated_data_type *adt,
char *buf, size_t sz, int member_offset)
{
return fill_member_name(buf, sz, &adt->self, member_offset, /*first=*/true);
}
static struct annotated_data_type *dso__findnew_data_type(struct dso *dso,
Dwarf_Die *type_die)
{

View File

@ -227,8 +227,13 @@ void annotated_data_type__tree_delete(struct rb_root *root);
/* Release all global variable information in the tree */
void global_var_type__tree_delete(struct rb_root *root);
/* Print data type annotation (including members) on stdout */
int hist_entry__annotate_data_tty(struct hist_entry *he, struct evsel *evsel);
/* Get name of member field at the given offset in the data type */
int annotated_data_type__get_member_name(struct annotated_data_type *adt,
char *buf, size_t sz, int member_offset);
bool has_reg_type(struct type_state *state, int reg);
struct type_state_stack *findnew_stack_state(struct type_state *state,
int offset, u8 kind,
@ -276,6 +281,14 @@ static inline int hist_entry__annotate_data_tty(struct hist_entry *he __maybe_un
return -1;
}
static inline int annotated_data_type__get_member_name(struct annotated_data_type *adt __maybe_unused,
char *buf __maybe_unused,
size_t sz __maybe_unused,
int member_offset __maybe_unused)
{
return -1;
}
#endif /* HAVE_LIBDW_SUPPORT */
#ifdef HAVE_SLANG_SUPPORT

View File

@ -2403,44 +2403,19 @@ sort__typeoff_sort(struct hist_entry *left, struct hist_entry *right)
return left->mem_type_off - right->mem_type_off;
}
static void fill_member_name(char *buf, size_t sz, struct annotated_member *m,
int offset, bool first)
{
struct annotated_member *child;
if (list_empty(&m->children))
return;
list_for_each_entry(child, &m->children, node) {
if (child->offset <= offset && offset < child->offset + child->size) {
int len = 0;
/* It can have anonymous struct/union members */
if (child->var_name) {
len = scnprintf(buf, sz, "%s%s",
first ? "" : ".", child->var_name);
first = false;
}
fill_member_name(buf + len, sz - len, child, offset, first);
return;
}
}
}
static int hist_entry__typeoff_snprintf(struct hist_entry *he, char *bf,
size_t size, unsigned int width __maybe_unused)
{
struct annotated_data_type *he_type = he->mem_type;
char buf[4096];
buf[0] = '\0';
if (list_empty(&he_type->self.children))
snprintf(buf, sizeof(buf), "no field");
else
fill_member_name(buf, sizeof(buf), &he_type->self,
he->mem_type_off, true);
buf[4095] = '\0';
if (he_type == &unknown_type || he_type == &stackop_type ||
he_type == &canary_type)
return repsep_snprintf(bf, size, "%s", he_type->self.type_name);
if (!annotated_data_type__get_member_name(he_type, buf, sizeof(buf),
he->mem_type_off))
scnprintf(buf, sizeof(buf), "no field");
return repsep_snprintf(bf, size, "%s +%#x (%s)", he_type->self.type_name,
he->mem_type_off, buf);