[PATCH v6 12/15] perf dso: Clean up read_symbol error handling

Ian Rogers
2025-09-29T19:09:15Z

Ensure errno is set and return to caller for error handling. Unusually
for perf the value isn't negated as expected by
symbol__strerror_disassemble.

Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/capstone.c | 3 ++-
tools/perf/util/dso.c | 15 ++++++++++++---
tools/perf/util/llvm.c | 3 ++-
3 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/tools/perf/util/capstone.c b/tools/perf/util/capstone.c
index 5aeae261f7ee..88e270237443 100644
--- a/tools/perf/util/capstone.c
+++ b/tools/perf/util/capstone.c
@@ -12,6 +12,7 @@
#include "symbol.h"
#include "thread.h"
#include <dlfcn.h>
+#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <string.h>
@@ -463,7 +464,7 @@ int symbol__disassemble_capstone(const char *filename __maybe_unused,
buf = dso__read_symbol(dso, filename, map, sym,
&code_buf, &buf_len, &is_64bit);
if (buf == NULL)
- return -1;
+ return errno;

/* add the function address and name */
scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:",
diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
index 0aed5c8691bd..344e689567ee 100644
--- a/tools/perf/util/dso.c
+++ b/tools/perf/util/dso.c
@@ -1827,26 +1827,33 @@ static const u8 *__dso__read_symbol(struct dso *dso, const char *symfs_filename,
.ip = start,
};
u8 *code_buf = NULL;
+ int saved_errno;

nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
fd = open(symfs_filename, O_RDONLY);
+ saved_errno = errno;
nsinfo__mountns_exit(&nsc);
- if (fd < 0)
+ if (fd < 0) {
+ errno = saved_errno;
return NULL;
-
- if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data, is_64bit) == 0) {
+ }
+ if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data, is_64bit) <= 0) {
close(fd);
+ errno = ENOENT;
return NULL;
}
code_buf = malloc(len);
if (code_buf == NULL) {
close(fd);
+ errno = ENOMEM;
return NULL;
}
count = pread(fd, code_buf, len, data.offset);
+ saved_errno = errno;
close(fd);
if ((u64)count != len) {
free(code_buf);
+ errno = saved_errno;
return NULL;
}
*out_buf = code_buf;
@@ -1875,6 +1882,7 @@ const u8 *dso__read_symbol(struct dso *dso, const char *symfs_filename,
* Note, there is fallback BPF image disassembly in the objdump
* version but it currently does nothing.
*/
+ errno = EOPNOTSUPP;
return NULL;
}
if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_PROG_INFO) {
@@ -1895,6 +1903,7 @@ const u8 *dso__read_symbol(struct dso *dso, const char *symfs_filename,
return (const u8 *)(uintptr_t)(info_linear->info.jited_prog_insns);
#else
pr_debug("No BPF program disassembly support\n");
+ errno = EOPNOTSUPP;
return NULL;
#endif
}
diff --git a/tools/perf/util/llvm.c b/tools/perf/util/llvm.c
index a28f130c8951..1607364ee736 100644
--- a/tools/perf/util/llvm.c
+++ b/tools/perf/util/llvm.c
@@ -9,6 +9,7 @@
#include "srcline.h"
#include "symbol.h"
#include <dlfcn.h>
+#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <unistd.h>
@@ -365,7 +366,7 @@ int symbol__disassemble_llvm(const char *filename, struct symbol *sym,
buf = dso__read_symbol(dso, filename, map, sym,
&code_buf, &buf_len, &is_64bit);
if (buf == NULL)
- return -1;
+ return errno;

init_llvm();
if (arch__is(args->arch, "x86")) {
--
2.51.0.570.gb178f27e6d-goog