perf annotate: Be robust to annotating without a thread

If a thread isn't given to map_symbol__get_arch(), try harder to determine
the arch for disassembly. Do this by utilizing fallback paths such as
reading the e_machine from a map's DSO ELF header for user-space libraries.
Additionally, rely on map__kmaps() and maps__machine() to reliably extract
the recorded machine environment and e_machine for kernel and kallsyms maps,
perfectly preventing silent, incorrect host fallbacks to uname() during
cross-platform Capstone annotation sessions.

At the same time, ensure all remaining uses of a map_symbol's thread pointer
do not assume it is non-NULL to eliminate UI segmentation faults, and remove
the fragile, redundant thread__get_arch() function to streamline the
annotate and disassembly subsystem architecture.

Fixes: 0e26ba5a87 ("perf disasm: Refactor arch__find and initialization of arch structs")
Assisted-by: Antigravity:gemini-3.5-flash
Signed-off-by: Ian Rogers <irogers@google.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
This commit is contained in:
Ian Rogers
2026-07-27 17:30:46 -07:00
committed by Namhyung Kim
parent bd86119c3e
commit b84e081e07
4 changed files with 74 additions and 24 deletions
+1 -1
View File
@@ -1201,7 +1201,7 @@ int __hist_entry__tui_annotate(struct hist_entry *he, struct map_symbol *ms,
ui__warning("Annotation has no source code.");
}
} else {
err = thread__get_arch(ms->thread, &browser.arch);
err = map_symbol__get_arch(ms, &browser.arch);
if (err) {
annotate_browser__symbol_annotate_error(&browser, err);
return -1;
+37 -14
View File
@@ -982,24 +982,43 @@ void symbol__calc_percent(struct symbol *sym, struct evsel *evsel)
annotation__calc_percent(notes, evsel, symbol__size(sym));
}
int thread__get_arch(struct thread *thread, const struct arch **parch)
int map_symbol__get_arch(struct map_symbol *ms, const struct arch **parch)
{
const struct arch *arch;
struct machine *machine;
uint32_t e_flags;
uint16_t e_machine;
struct machine *machine = NULL;
struct map *map = ms->map;
struct dso *dso = map ? map__dso(map) : NULL;
uint32_t e_flags = 0;
uint16_t e_machine = EM_NONE;
if (!thread) {
*parch = NULL;
return -1;
const char *cpuid = NULL;
if (ms->thread) {
machine = maps__machine(thread__maps(ms->thread));
e_machine = thread__e_machine(ms->thread, machine, &e_flags);
if (machine && machine->env)
cpuid = machine->env->cpuid;
} else if (dso) {
struct maps *kmaps = (map && dso__kernel(dso)) ? map__kmaps(map) : NULL;
struct machine *kmap_machine = kmaps ? maps__machine(kmaps) : NULL;
e_machine = dso__e_machine(dso, kmap_machine, &e_flags);
if (kmap_machine && kmap_machine->env)
cpuid = kmap_machine->env->cpuid;
}
machine = maps__machine(thread__maps(thread));
e_machine = thread__e_machine(thread, machine, &e_flags);
arch = arch__find(e_machine, e_flags, machine->env ? machine->env->cpuid : NULL);
if (e_machine == EM_NONE)
e_machine = thread__e_machine(NULL, NULL, &e_flags);
arch = arch__find(e_machine, e_flags, cpuid);
if (arch == NULL) {
pr_err("%s: unsupported arch %d\n", __func__, e_machine);
return errno;
/* TODO: Refactor annotate/disassemble subsystem error
* codes to uniformly return negative integers.
*/
return errno ? errno : ENOTSUP;
}
if (parch)
*parch = arch;
@@ -1018,7 +1037,7 @@ int symbol__annotate(struct map_symbol *ms, struct evsel *evsel,
const struct arch *arch = NULL;
int err, nr;
err = thread__get_arch(ms->thread, &arch);
err = map_symbol__get_arch(ms, &arch);
if (err)
return err;
@@ -1251,6 +1270,11 @@ int hist_entry__annotate_printf(struct hist_entry *he, struct evsel *evsel)
evsel_name = buf;
}
if (map_symbol__get_arch(ms, &apd.arch)) {
free(filename);
return ENOTSUP;
}
graph_dotted_len = printf(" %-*.*s| Source code & Disassembly of %s for %s (%" PRIu64 " samples, "
"percent: %s)\n",
width, width, symbol_conf.show_total_period ? "Period" :
@@ -1266,7 +1290,6 @@ int hist_entry__annotate_printf(struct hist_entry *he, struct evsel *evsel)
apd.addr_fmt_width = annotated_source__addr_fmt_width(&notes->src->source,
notes->src->start);
thread__get_arch(ms->thread, &apd.arch);
apd.dbg = dso__debuginfo(dso);
list_for_each_entry(pos, &notes->src->source, node) {
@@ -1371,7 +1394,7 @@ static int symbol__annotate_fprintf2(struct symbol *sym, FILE *fp,
struct annotation_line *al;
if (annotate_opts.code_with_type) {
thread__get_arch(apd->he->ms.thread, &apd->arch);
map_symbol__get_arch(&apd->he->ms, &apd->arch);
apd->dbg = dso__debuginfo(map__dso(apd->he->ms.map));
}
+2 -1
View File
@@ -584,5 +584,6 @@ int annotation_br_cntr_entry(char **str, int br_cntr_nr, u64 *br_cntr,
int num_aggr, struct evsel *evsel);
int annotation_br_cntr_abbr_list(char **str, struct evsel *evsel, bool header);
int thread__get_arch(struct thread *thread, const struct arch **parch);
int map_symbol__get_arch(struct map_symbol *ms, const struct arch **parch);
#endif /* __PERF_ANNOTATE_H */
+34 -8
View File
@@ -392,7 +392,7 @@ int symbol__disassemble_capstone(const char *filename, struct symbol *sym,
char disasm_buf[512];
struct disasm_line *dl;
bool disassembler_style = false;
uint16_t e_machine;
uint16_t e_machine = EM_NONE;
bool is_big_endian = false;
if (args->options->objdump_path)
@@ -423,9 +423,22 @@ int symbol__disassemble_capstone(const char *filename, struct symbol *sym,
!strcmp(args->options->disassembler_style, "att"))
disassembler_style = true;
e_machine = thread__e_machine_endian(args->ms->thread,
/*machine=*/NULL,
/*e_flags=*/NULL, &is_big_endian);
if (args->ms->thread) {
e_machine = thread__e_machine_endian(args->ms->thread,
/*machine=*/NULL,
/*e_flags=*/NULL, &is_big_endian);
} else if (dso) {
struct maps *kmaps = (map && dso__kernel(dso)) ? map__kmaps(map) : NULL;
struct machine *kmap_machine = kmaps ? maps__machine(kmaps) : NULL;
e_machine = dso__e_machine_endian(dso, kmap_machine, /*e_flags=*/NULL,
&is_big_endian);
}
if (!e_machine || e_machine == EM_NONE) {
e_machine = thread__e_machine_endian(NULL,
/*machine=*/NULL,
/*e_flags=*/NULL, &is_big_endian);
}
if (capstone_init(e_machine, &handle, is_64bit, is_big_endian, disassembler_style) < 0)
goto err;
@@ -518,7 +531,7 @@ int symbol__disassemble_capstone_powerpc(const char *filename __maybe_unused,
struct disasm_line *dl;
u32 *line;
bool disassembler_style = false;
uint16_t e_machine;
uint16_t e_machine = EM_NONE;
bool is_big_endian = false;
if (args->options->objdump_path)
@@ -538,9 +551,22 @@ int symbol__disassemble_capstone_powerpc(const char *filename __maybe_unused,
!strcmp(args->options->disassembler_style, "att"))
disassembler_style = true;
e_machine = thread__e_machine_endian(args->ms->thread,
/*machine=*/NULL,
/*e_flags=*/NULL, &is_big_endian);
if (args->ms->thread) {
e_machine = thread__e_machine_endian(args->ms->thread,
/*machine=*/NULL,
/*e_flags=*/NULL, &is_big_endian);
} else if (dso) {
struct maps *kmaps = (map && dso__kernel(dso)) ? map__kmaps(map) : NULL;
struct machine *kmap_machine = kmaps ? maps__machine(kmaps) : NULL;
e_machine = dso__e_machine_endian(dso, kmap_machine, /*e_flags=*/NULL,
&is_big_endian);
}
if (!e_machine || e_machine == EM_NONE) {
e_machine = thread__e_machine_endian(NULL,
/*machine=*/NULL,
/*e_flags=*/NULL, &is_big_endian);
}
if (capstone_init(e_machine, &handle, is_64bit, is_big_endian, disassembler_style) < 0)
goto err;