]> www.infradead.org Git - users/hch/misc.git/commitdiff
perf dso: Clean up read_symbol() error handling
authorIan Rogers <irogers@google.com>
Sun, 5 Oct 2025 21:22:06 +0000 (14:22 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 6 Oct 2025 19:35:28 +0000 (16:35 -0300)
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>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexandre Ghiti <alexghiti@rivosinc.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Athira Rajeev <atrajeev@linux.ibm.com>
Cc: Bill Wendling <morbo@google.com>
Cc: Charlie Jenkins <charlie@rivosinc.com>
Cc: Collin Funk <collin.funk1@gmail.com>
Cc: Dmitriy Vyukov <dvyukov@google.com>
Cc: Dr. David Alan Gilbert <linux@treblig.org>
Cc: Eric Biggers <ebiggers@kernel.org>
Cc: Haibo Xu <haibo1.xu@intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@linaro.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Justin Stitt <justinstitt@google.com>
Cc: Li Huafei <lihuafei1@huawei.com>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Nick Desaulniers <nick.desaulniers+lkml@gmail.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Song Liu <song@kernel.org>
Cc: Stephen Brennan <stephen.s.brennan@oracle.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/capstone.c
tools/perf/util/dso.c
tools/perf/util/llvm.c

index c23df911e91c1dc7851470093b1f7f1ad0cb1a68..be5fd44b1f9dc39679c5b05d8dd8f5f1e1d4ea6b 100644 (file)
@@ -11,6 +11,7 @@
 #include "print_insn.h"
 #include "symbol.h"
 #include "thread.h"
+#include <errno.h>
 #include <fcntl.h>
 #include <string.h>
 
@@ -245,7 +246,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>:",
index 0aed5c8691bdd7ede4974d009e0fcff751e72181..344e689567ee1a2aae49f5807e68a6e5e07937c5 100644 (file)
@@ -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
        }
index ec41d0f27000f7ced867da5788cbedde48c6d145..43e6fecfce59fbb60730c310a596b64b3cace0d1 100644 (file)
@@ -7,6 +7,7 @@
 #include "namespaces.h"
 #include "srcline.h"
 #include "symbol.h"
+#include <errno.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <linux/zalloc.h>
@@ -147,7 +148,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")) {