[PATCH v2 06/10] perf tools: Factor out dso__find_binary_type()

From: Namhyung Kim

Date: Tue Oct 06 2026 - 19:44:15 EST


Separate out the logic to detect binary type of a DSO. It checks if the
candidate file contains ".text" section. It should be called under the
_dso__data_open_lock.

Introduce bintype_searched flag and it accessors to prevent repeated
(failed) search.

Signed-off-by: Namhyung Kim <namhyung@xxxxxxxxxx>
---
tools/perf/util/dso.c | 100 +++++++++++++++++++++++++++++++++++++-----
tools/perf/util/dso.h | 13 ++++++
2 files changed, 102 insertions(+), 11 deletions(-)

diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
index 913877b84b2d437e..f5054c18a2e931e7 100644
--- a/tools/perf/util/dso.c
+++ b/tools/perf/util/dso.c
@@ -833,7 +833,7 @@ void dso__data_close(struct dso *dso)
mutex_unlock(dso__data_open_lock());
}

-static void try_to_open_dso(struct dso *dso, struct machine *machine)
+static enum dso_binary_type __dso__find_binary_type(struct dso *dso)
EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock)
{
enum dso_binary_type binary_type_data[] = {
@@ -842,25 +842,103 @@ static void try_to_open_dso(struct dso *dso, struct machine *machine)
DSO_BINARY_TYPE__NOT_FOUND,
};
int i = 0;
- struct dso_data *dso_data = dso__data(dso);
+ char *path;
+ bool found = false;
+ bool decomp = false;

- if (dso_data->fd >= 0)
- return;
+ if (dso__bintype_searched(dso))
+ return dso__binary_type(dso);
+ dso__set_bintype_searched(dso);

- if (dso__binary_type(dso) != DSO_BINARY_TYPE__NOT_FOUND) {
- dso_data->fd = open_dso(dso, machine);
- goto out;
+ switch (dso__binary_type(dso)) {
+ case DSO_BINARY_TYPE__KALLSYMS:
+ case DSO_BINARY_TYPE__GUEST_KALLSYMS:
+ case DSO_BINARY_TYPE__VMLINUX:
+ case DSO_BINARY_TYPE__GUEST_VMLINUX:
+ case DSO_BINARY_TYPE__KCORE:
+ case DSO_BINARY_TYPE__GUEST_KCORE:
+ /* Nothing to do with kernel images */
+ WARN_ON(!dso__kernel(dso));
+ return dso__binary_type(dso);
+ case DSO_BINARY_TYPE__BPF_PROG_INFO:
+ case DSO_BINARY_TYPE__BPF_IMAGE:
+ case DSO_BINARY_TYPE__OOL:
+ case DSO_BINARY_TYPE__JAVA_JIT:
+ /* Same for the special DSOs */
+ return dso__binary_type(dso);
+ case DSO_BINARY_TYPE__GUEST_KMODULE:
+ case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
+ case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
+ case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
+ case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
+ case DSO_BINARY_TYPE__BUILD_ID_CACHE:
+ /* We don't expect these are set; fall through */
+ case DSO_BINARY_TYPE__NOT_FOUND:
+ /* Let's find it out (for user DSOs or kernel modules) */
+ break;
+ case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
+ case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
+ case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
+ case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
+ case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
+ case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
+ case DSO_BINARY_TYPE__GNU_DEBUGDATA:
+ case DSO_BINARY_TYPE__DEBUGLINK:
+ default:
+ /* Unexpected binary types (usually debug only) */
+ break;
}

do {
dso__set_binary_type(dso, binary_type_data[i++]);

- dso_data->fd = open_dso(dso, machine);
- if (dso_data->fd >= 0)
- goto out;
+ path = dso__get_filename(dso, "", &decomp, dso__binary_type(dso));
+ if (path == NULL)
+ continue;
+
+ found = filename__has_section(path, ".text");
+ dso__put_filename(dso, path, decomp);
+ if (found)
+ return dso__binary_type(dso);

} while (dso__binary_type(dso) != DSO_BINARY_TYPE__NOT_FOUND);
-out:
+
+ if (dso__symtab_type(dso) != DSO_BINARY_TYPE__BUILD_ID_CACHE &&
+ dso__symtab_type(dso) != DSO_BINARY_TYPE__SYSTEM_PATH_DSO) {
+ path = dso__get_filename(dso, "", &decomp, dso__symtab_type(dso));
+ if (path == NULL)
+ return dso__binary_type(dso);
+
+ found = filename__has_section(path, ".text");
+ dso__put_filename(dso, path, decomp);
+ if (found)
+ dso__set_binary_type(dso, dso__symtab_type(dso));
+ }
+ return dso__binary_type(dso);
+}
+
+enum dso_binary_type dso__find_binary_type(struct dso *dso)
+{
+ enum dso_binary_type ret;
+
+ mutex_lock(dso__data_open_lock());
+ ret = __dso__find_binary_type(dso);
+ mutex_unlock(dso__data_open_lock());
+
+ return ret;
+}
+
+static void try_to_open_dso(struct dso *dso, struct machine *machine)
+ EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock)
+{
+ struct dso_data *dso_data = dso__data(dso);
+
+ if (dso_data->fd >= 0)
+ return;
+
+ __dso__find_binary_type(dso);
+ dso_data->fd = open_dso(dso, machine);
+
if (dso_data->fd >= 0)
dso_data->status = DSO_DATA_STATUS_OK;
else
diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h
index 3f08d45e7f53a508..3329f417810c25a4 100644
--- a/tools/perf/util/dso.h
+++ b/tools/perf/util/dso.h
@@ -349,6 +349,7 @@ DECLARE_RC_STRUCT(dso) {
u8 short_name_allocated:1;
u8 long_name_allocated:1;
u8 is_64_bit:1;
+ u8 bintype_searched:1;
u8 debuginfo_searched:1;
bool sorted_by_name;
bool loaded;
@@ -957,6 +958,8 @@ void dso__reset_find_symbol_cache(struct dso *dso);
size_t dso__fprintf_symbols_by_name(struct dso *dso, FILE *fp);
size_t dso__fprintf(struct dso *dso, FILE *fp);

+enum dso_binary_type dso__find_binary_type(struct dso *dso);
+
static inline enum dso_binary_type dso__binary_type(const struct dso *dso)
{
return RC_CHK_ACCESS(dso)->binary_type;
@@ -967,6 +970,16 @@ static inline void dso__set_binary_type(struct dso *dso, enum dso_binary_type bt
RC_CHK_ACCESS(dso)->binary_type = bt;
}

+static inline bool dso__bintype_searched(const struct dso *dso)
+{
+ return RC_CHK_ACCESS(dso)->bintype_searched;
+}
+
+static inline void dso__set_bintype_searched(struct dso *dso)
+{
+ RC_CHK_ACCESS(dso)->bintype_searched = 1;
+}
+
static inline bool dso__is_vmlinux(const struct dso *dso)
{
enum dso_binary_type bt = dso__binary_type(dso);
--
2.56.0.rc1.315.gc6ed9934b7-goog