]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
perf tools: Replace basename() calls with perf_basename()
authorArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 8 Apr 2026 17:32:03 +0000 (14:32 -0300)
committerNamhyung Kim <namhyung@kernel.org>
Thu, 9 Apr 2026 02:21:05 +0000 (19:21 -0700)
As noticed in a sashiko review for a patch adding a missing libgen.h
in a file using basename():

  https://sashiko.dev/#/patchset/20260402001740.2220481-1-acme%40kernel.org

So avoid these subtleties and instead reuse the gnu_basename() function
we had in srcline.c, renaming it to perf_basename() and replace
basename() calls with it, simplifying several cases by removing now
needless strdups.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
tools/perf/builtin-daemon.c
tools/perf/util/annotate.c
tools/perf/util/data-convert-json.c
tools/perf/util/dsos.c
tools/perf/util/probe-event.c
tools/perf/util/srcline.c
tools/perf/util/symbol.h
tools/perf/util/util.c
tools/perf/util/util.h

index 33473e071392601e3b2bbd37cd6c326844259535..c4632577d12963507dce6925934848158dde88ef 100644 (file)
@@ -1016,7 +1016,7 @@ static int setup_config_changes(struct daemon *daemon)
 {
        char *basen = strdup(daemon->config_real);
        char *dirn  = strdup(daemon->config_real);
-       char *base, *dir;
+       const char *base, *dir;
        int fd, wd = -1;
 
        if (!dirn || !basen)
@@ -1029,7 +1029,7 @@ static int setup_config_changes(struct daemon *daemon)
        }
 
        dir = dirname(dirn);
-       base = basename(basen);
+       base = perf_basename(basen);
        pr_debug("config file: %s, dir: %s\n", base, dir);
 
        wd = inotify_add_watch(fd, dir, IN_CLOSE_WRITE);
index 63f0ee9d4c03ce4ed22b06a495e5074ee96a3f78..e745f3034a0e1c52078653d7bf63b22ccb79f5c2 100644 (file)
@@ -8,7 +8,6 @@
 
 #include <errno.h>
 #include <inttypes.h>
-#include <libgen.h>
 #include <stdlib.h>
 #include "util.h" // hex_width()
 #include "ui/ui.h"
@@ -1245,7 +1244,7 @@ int hist_entry__annotate_printf(struct hist_entry *he, struct evsel *evsel)
        if (opts->full_path)
                d_filename = filename;
        else
-               d_filename = basename(filename);
+               d_filename = perf_basename(filename);
 
        if (evsel__is_group_event(evsel)) {
                evsel__group_desc(evsel, buf, sizeof(buf));
index 4b1b2f7bed2516913ef1f76cf963fa095d639ab9..d526c91312ed8b93388a05df8d08aa5eb4ac51ca 100644 (file)
@@ -326,7 +326,7 @@ static void output_headers(struct perf_session *session, struct convert_json *c)
        output_json_format(out, false, 2, "]");
 }
 
-int bt_convert__perf2json(const char *input_name, const char *output_name,
+int bt_convert__perf2json(const char *_input_name, const char *output_name,
                struct perf_data_convert_opts *opts __maybe_unused)
 {
        struct perf_session *session;
@@ -342,7 +342,7 @@ int bt_convert__perf2json(const char *input_name, const char *output_name,
        };
        struct perf_data data = {
                .mode = PERF_DATA_MODE_READ,
-               .path = input_name,
+               .path = _input_name,
                .force = opts->force,
        };
 
index 5cf8c878bab2a4a48a2a91707c7839493be4f43a..e927e707abace9c64912c11a65fa9bf9e8e5ac1e 100644 (file)
@@ -6,7 +6,6 @@
 #include "vdso.h"
 #include "namespaces.h"
 #include <errno.h>
-#include <libgen.h>
 #include <stdlib.h>
 #include <string.h>
 #include <symbol.h> // filename__read_build_id
@@ -297,34 +296,21 @@ struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
 
 static void dso__set_basename(struct dso *dso)
 {
-       char *base, *lname;
+       bool allocated = false;
+       const char *base;
        int tid;
 
        if (perf_pid_map_tid(dso__long_name(dso), &tid)) {
-               if (asprintf(&base, "[JIT] tid %d", tid) < 0)
-                       return;
-       } else {
-             /*
-              * basename() may modify path buffer, so we must pass
-               * a copy.
-               */
-               lname = strdup(dso__long_name(dso));
-               if (!lname)
-                       return;
-
-               /*
-                * basename() may return a pointer to internal
-                * storage which is reused in subsequent calls
-                * so copy the result.
-                */
-               base = strdup(basename(lname));
+               char *jitname;
 
-               free(lname);
-
-               if (!base)
+               if (asprintf(&jitname, "[JIT] tid %d", tid) < 0)
                        return;
+               allocated = true;
+               base = jitname;
+       } else {
+               base = perf_basename(dso__long_name(dso));
        }
-       dso__set_short_name(dso, base, true);
+       dso__set_short_name(dso, base, allocated);
 }
 
 static struct dso *__dsos__addnew_id(struct dsos *dsos, const char *name, const struct dso_id *id)
index f37a783ea7723197014c09afd580d6470f2e5583..34b4badd2c14ade0ba8d9011c0451e9cedb9f909 100644 (file)
@@ -11,7 +11,6 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <errno.h>
-#include <libgen.h>
 #include <stdio.h>
 #include <unistd.h>
 #include <stdlib.h>
@@ -229,7 +228,7 @@ static int convert_exec_to_group(const char *exec, char **result)
        if (!exec_copy)
                return -ENOMEM;
 
-       ptr1 = basename(exec_copy);
+       ptr1 = (char *)perf_basename(exec_copy);
        if (!ptr1) {
                ret = -EINVAL;
                goto out;
index b58710624eadfe1b7a4a4a7a58db34e9b410b638..db164d25816347acb8f51076da12fd25ad409f83 100644 (file)
@@ -8,6 +8,7 @@
 #include "symbol.h"
 #include "libdw.h"
 #include "debug.h"
+#include "util.h"
 
 #include <inttypes.h>
 #include <string.h>
@@ -74,14 +75,6 @@ int inline_list__append_tail(struct symbol *symbol, char *srcline, struct inline
        return 0;
 }
 
-/* basename version that takes a const input string */
-static const char *gnu_basename(const char *path)
-{
-       const char *base = strrchr(path, '/');
-
-       return base ? base + 1 : path;
-}
-
 char *srcline_from_fileline(const char *file, unsigned int line)
 {
        char *srcline;
@@ -90,7 +83,7 @@ char *srcline_from_fileline(const char *file, unsigned int line)
                return NULL;
 
        if (!srcline_full_filename)
-               file = gnu_basename(file);
+               file = perf_basename(file);
 
        if (asprintf(&srcline, "%s:%u", file, line) < 0)
                return NULL;
index c67814d6d6d6f64a2d9773c8ac2c98a91ff198b5..bd6eb90c86683762c7dbe4139053b78b5f8d5e65 100644 (file)
@@ -14,6 +14,7 @@
 #include "path.h"
 #include "symbol_conf.h"
 #include "spark.h"
+#include "util.h"
 
 #ifdef HAVE_LIBELF_SUPPORT
 #include <libelf.h>
@@ -97,18 +98,9 @@ struct intlist;
 
 static inline int __symbol__join_symfs(char *bf, size_t size, const char *path)
 {
-       if (symbol_conf.symfs_layout_flat) {
-               char *path_copy = strdup(path);
-               char *base;
-               int ret;
-
-               if (!path_copy)
-                       return -ENOMEM;
-               base = basename(path_copy);
-               ret = path__join(bf, size, symbol_conf.symfs, base);
-               free(path_copy);
-               return ret;
-       }
+       if (symbol_conf.symfs_layout_flat)
+               return path__join(bf, size, symbol_conf.symfs, perf_basename(path));
+
        return path__join(bf, size, symbol_conf.symfs, path);
 }
 
index c5fee8e394805544ca8230730b61937a9cacef89..25849434f0a4e5fd1baf22114d393219451bc5ee 100644 (file)
@@ -545,3 +545,11 @@ int scandirat(int dirfd, const char *dirp,
        return err;
 }
 #endif
+
+/* basename version that takes a const input string */
+const char *perf_basename(const char *path)
+{
+       const char *base = strrchr(path, '/');
+
+       return base ? base + 1 : path;
+}
index e935438451b81fda79e34e03073f78e81e6a0d93..87a0818a8c765c458c8aa8fbb1f426302762efe3 100644 (file)
@@ -86,6 +86,8 @@ struct perf_debuginfod {
 };
 void perf_debuginfod_setup(struct perf_debuginfod *di);
 
+const char *perf_basename(const char *path);
+
 char *filename_with_chroot(int pid, const char *filename);
 
 int do_realloc_array_as_needed(void **arr, size_t *arr_sz, size_t x,