[PATCH v2 5/8] perf tools: Add dso__put_filename()

From: Namhyung Kim

Date: Mon Sep 28 2026 - 21:14:02 EST


It's a counterpart of the dso__get_filename() as the returned string
should be free. Optionally it needs to remove a temporary file if it's
decompressed on the fly.

Currently the dso argument is not used, but it's better to pair with the
matching dso.

Reviewed-by: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
Signed-off-by: Namhyung Kim <namhyung@xxxxxxxxxx>
---
tools/perf/util/debuginfo.c | 5 +----
tools/perf/util/dso.c | 27 +++++++++++++--------------
tools/perf/util/dso.h | 2 ++
3 files changed, 16 insertions(+), 18 deletions(-)

diff --git a/tools/perf/util/debuginfo.c b/tools/perf/util/debuginfo.c
index 38e9299765182bd7..fc2640dcd29d3aaa 100644
--- a/tools/perf/util/debuginfo.c
+++ b/tools/perf/util/debuginfo.c
@@ -118,10 +118,7 @@ struct debuginfo *debuginfo__new(const char *path)
return NULL;

dinfo = __debuginfo__new(filename);
-
- if (decomp)
- unlink(filename);
- free(filename);
+ dso__put_filename(dso, filename, decomp);
dso__put(dso);

return dinfo;
diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
index 7fd6b64583d8b61a..c765c9b80e957871 100644
--- a/tools/perf/util/dso.c
+++ b/tools/perf/util/dso.c
@@ -661,6 +661,14 @@ char *dso__get_filename(struct dso *dso, const char *root_dir, bool *decomp,
return NULL;
}

+void dso__put_filename(struct dso *dso __maybe_unused, char *filename, bool decomp)
+{
+ if (decomp)
+ unlink(filename);
+
+ free(filename);
+}
+
static int __open_dso(struct dso *dso, struct machine *machine)
EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock)
{
@@ -680,11 +688,8 @@ static int __open_dso(struct dso *dso, struct machine *machine)
fd = -errno;
}

- if (decomp)
- unlink(name);
-
+ dso__put_filename(dso, name, decomp);
mutex_unlock(dso__lock(dso));
- free(name);
return fd;
}

@@ -2088,14 +2093,11 @@ struct debuginfo *dso__debuginfo(struct dso *dso)
mutex_lock(dso__lock(dso));

name = dso__get_filename(dso, "", &decomp, dso__dbginfo_type(dso));
- if (name)
+ if (name) {
dinfo = debuginfo__new(name);
-
- if (decomp)
- unlink(name);
-
+ dso__put_filename(dso, name, decomp);
+ }
mutex_unlock(dso__lock(dso));
- free(name);
return dinfo;
}

@@ -2135,10 +2137,7 @@ void dso__find_dbginfo_type(struct dso *dso)
found = filename__has_section(path, ".debug_info") ||
filename__has_section(path, ".zdebug_info");

- if (decomp)
- unlink(path);
- free(path);
-
+ dso__put_filename(dso, path, decomp);
if (found)
break;
}
diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h
index 94cc7ea89ceb5bd9..45c4954d5eaf9ffa 100644
--- a/tools/perf/util/dso.h
+++ b/tools/perf/util/dso.h
@@ -841,8 +841,10 @@ int dso__kernel_module_get_build_id(struct dso *dso, const char *root_dir);
char dso__symtab_origin(const struct dso *dso);
int dso__read_binary_type_filename(const struct dso *dso, enum dso_binary_type type,
const char *root_dir, char *filename, size_t size);
+/* returned filename should be freed by dso__put_filename() */
char *dso__get_filename(struct dso *dso, const char *root_dir, bool *decomp,
enum dso_binary_type type);
+void dso__put_filename(struct dso *dso, char *filename, bool decomp);
bool is_kernel_module(const char *pathname, int cpumode);
bool dso__needs_decompress(struct dso *dso);
int dso__decompress_kmodule_fd(struct dso *dso, const char *name);
--
2.55.0