perf annotate: Implement hist_entry__get_data_type()
authorNamhyung Kim <namhyung@kernel.org>
Wed, 13 Dec 2023 00:13:13 +0000 (16:13 -0800)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Sun, 24 Dec 2023 01:39:42 +0000 (22:39 -0300)
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>
tools/perf/util/annotate.c
tools/perf/util/annotate.h

index 7c59744..8673eac 100644 (file)
@@ -25,6 +25,7 @@
 #include "units.h"
 #include "debug.h"
 #include "annotate.h"
+#include "annotate-data.h"
 #include "evsel.h"
 #include "evlist.h"
 #include "bpf-event.h"
@@ -3625,3 +3626,90 @@ int annotate_get_insn_location(struct arch *arch, struct disasm_line *dl,
 
        return 0;
 }
+
+static void symbol__ensure_annotate(struct map_symbol *ms, struct evsel *evsel)
+{
+       struct disasm_line *dl, *tmp_dl;
+       struct annotation *notes;
+
+       notes = symbol__annotation(ms->sym);
+       if (!list_empty(&notes->src->source))
+               return;
+
+       if (symbol__annotate(ms, evsel, NULL) < 0)
+               return;
+
+       /* remove non-insn disasm lines for simplicity */
+       list_for_each_entry_safe(dl, tmp_dl, &notes->src->source, al.node) {
+               if (dl->al.offset == -1) {
+                       list_del(&dl->al.node);
+                       free(dl);
+               }
+       }
+}
+
+static struct disasm_line *find_disasm_line(struct symbol *sym, u64 ip)
+{
+       struct disasm_line *dl;
+       struct annotation *notes;
+
+       notes = symbol__annotation(sym);
+
+       list_for_each_entry(dl, &notes->src->source, al.node) {
+               if (sym->start + dl->al.offset == ip)
+                       return dl;
+       }
+       return NULL;
+}
+
+/**
+ * hist_entry__get_data_type - find data type for given hist entry
+ * @he: hist entry
+ *
+ * This function first annotates the instruction at @he->ip and extracts
+ * register and offset info from it.  Then it searches the DWARF debug
+ * info to get a variable and type information using the address, register,
+ * and offset.
+ */
+struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he)
+{
+       struct map_symbol *ms = &he->ms;
+       struct evsel *evsel = hists_to_evsel(he->hists);
+       struct arch *arch;
+       struct disasm_line *dl;
+       struct annotated_insn_loc loc;
+       struct annotated_op_loc *op_loc;
+       u64 ip = he->ip;
+       int i;
+
+       if (ms->map == NULL || ms->sym == NULL)
+               return NULL;
+
+       if (!symbol_conf.init_annotation)
+               return NULL;
+
+       if (evsel__get_arch(evsel, &arch) < 0)
+               return NULL;
+
+       /* Make sure it runs objdump to get disasm of the function */
+       symbol__ensure_annotate(ms, evsel);
+
+       /*
+        * Get a disasm to extract the location from the insn.
+        * This is too slow...
+        */
+       dl = find_disasm_line(ms->sym, ip);
+       if (dl == NULL)
+               return NULL;
+
+       if (annotate_get_insn_location(arch, dl, &loc) < 0)
+               return NULL;
+
+       for_each_insn_op_loc(&loc, i, op_loc) {
+               if (!op_loc->mem_ref)
+                       continue;
+
+               return find_data_type(ms, ip, op_loc->reg, op_loc->offset);
+       }
+       return NULL;
+}
index 25ae889..6c75b28 100644 (file)
@@ -23,6 +23,7 @@ struct option;
 struct perf_sample;
 struct evsel;
 struct symbol;
+struct annotated_data_type;
 
 struct ins {
        const char     *name;
@@ -475,4 +476,7 @@ struct annotated_insn_loc {
 int annotate_get_insn_location(struct arch *arch, struct disasm_line *dl,
                               struct annotated_insn_loc *loc);
 
+/* Returns a data type from the sample instruction (if any) */
+struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he);
+
 #endif /* __PERF_ANNOTATE_H */