Skip to content

Commit 67bc54b

Browse files
namhyungacmel
authored andcommitted
perf annotate: Implement hist_entry__get_data_type()
It's the function to find out the type info from the given sample data and will be called from the hist_entry sort logic when 'type' sort key is used. It first calls objdump to disassemble the instructions and figure out information about memory access at the location. Maybe we can do it better by analyzing the instruction directly, but I'll leave it for later work. The memory access is determined by checking instruction operands to have "(" and then extract register name and offset. It'll return NULL if no data type is found. Signed-off-by: Namhyung Kim <namhyung@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: Masami Hiramatsu <mhiramat@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Stephane Eranian <eranian@google.com> Cc: linux-toolchains@vger.kernel.org Cc: linux-trace-devel@vger.kernel.org Link: https://lore.kernel.org/r/20231213001323.718046-8-namhyung@kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 3a0c26e commit 67bc54b

2 files changed

Lines changed: 92 additions & 0 deletions

File tree

tools/perf/util/annotate.c

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "units.h"
2626
#include "debug.h"
2727
#include "annotate.h"
28+
#include "annotate-data.h"
2829
#include "evsel.h"
2930
#include "evlist.h"
3031
#include "bpf-event.h"
@@ -3625,3 +3626,90 @@ int annotate_get_insn_location(struct arch *arch, struct disasm_line *dl,
36253626

36263627
return 0;
36273628
}
3629+
3630+
static void symbol__ensure_annotate(struct map_symbol *ms, struct evsel *evsel)
3631+
{
3632+
struct disasm_line *dl, *tmp_dl;
3633+
struct annotation *notes;
3634+
3635+
notes = symbol__annotation(ms->sym);
3636+
if (!list_empty(&notes->src->source))
3637+
return;
3638+
3639+
if (symbol__annotate(ms, evsel, NULL) < 0)
3640+
return;
3641+
3642+
/* remove non-insn disasm lines for simplicity */
3643+
list_for_each_entry_safe(dl, tmp_dl, &notes->src->source, al.node) {
3644+
if (dl->al.offset == -1) {
3645+
list_del(&dl->al.node);
3646+
free(dl);
3647+
}
3648+
}
3649+
}
3650+
3651+
static struct disasm_line *find_disasm_line(struct symbol *sym, u64 ip)
3652+
{
3653+
struct disasm_line *dl;
3654+
struct annotation *notes;
3655+
3656+
notes = symbol__annotation(sym);
3657+
3658+
list_for_each_entry(dl, &notes->src->source, al.node) {
3659+
if (sym->start + dl->al.offset == ip)
3660+
return dl;
3661+
}
3662+
return NULL;
3663+
}
3664+
3665+
/**
3666+
* hist_entry__get_data_type - find data type for given hist entry
3667+
* @he: hist entry
3668+
*
3669+
* This function first annotates the instruction at @he->ip and extracts
3670+
* register and offset info from it. Then it searches the DWARF debug
3671+
* info to get a variable and type information using the address, register,
3672+
* and offset.
3673+
*/
3674+
struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he)
3675+
{
3676+
struct map_symbol *ms = &he->ms;
3677+
struct evsel *evsel = hists_to_evsel(he->hists);
3678+
struct arch *arch;
3679+
struct disasm_line *dl;
3680+
struct annotated_insn_loc loc;
3681+
struct annotated_op_loc *op_loc;
3682+
u64 ip = he->ip;
3683+
int i;
3684+
3685+
if (ms->map == NULL || ms->sym == NULL)
3686+
return NULL;
3687+
3688+
if (!symbol_conf.init_annotation)
3689+
return NULL;
3690+
3691+
if (evsel__get_arch(evsel, &arch) < 0)
3692+
return NULL;
3693+
3694+
/* Make sure it runs objdump to get disasm of the function */
3695+
symbol__ensure_annotate(ms, evsel);
3696+
3697+
/*
3698+
* Get a disasm to extract the location from the insn.
3699+
* This is too slow...
3700+
*/
3701+
dl = find_disasm_line(ms->sym, ip);
3702+
if (dl == NULL)
3703+
return NULL;
3704+
3705+
if (annotate_get_insn_location(arch, dl, &loc) < 0)
3706+
return NULL;
3707+
3708+
for_each_insn_op_loc(&loc, i, op_loc) {
3709+
if (!op_loc->mem_ref)
3710+
continue;
3711+
3712+
return find_data_type(ms, ip, op_loc->reg, op_loc->offset);
3713+
}
3714+
return NULL;
3715+
}

tools/perf/util/annotate.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct option;
2323
struct perf_sample;
2424
struct evsel;
2525
struct symbol;
26+
struct annotated_data_type;
2627

2728
struct ins {
2829
const char *name;
@@ -475,4 +476,7 @@ struct annotated_insn_loc {
475476
int annotate_get_insn_location(struct arch *arch, struct disasm_line *dl,
476477
struct annotated_insn_loc *loc);
477478

479+
/* Returns a data type from the sample instruction (if any) */
480+
struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he);
481+
478482
#endif /* __PERF_ANNOTATE_H */

0 commit comments

Comments
 (0)