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>
{
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)
}
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);
#include <errno.h>
#include <inttypes.h>
-#include <libgen.h>
#include <stdlib.h>
#include "util.h" // hex_width()
#include "ui/ui.h"
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));
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;
};
struct perf_data data = {
.mode = PERF_DATA_MODE_READ,
- .path = input_name,
+ .path = _input_name,
.force = opts->force,
};
#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
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)
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
-#include <libgen.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
if (!exec_copy)
return -ENOMEM;
- ptr1 = basename(exec_copy);
+ ptr1 = (char *)perf_basename(exec_copy);
if (!ptr1) {
ret = -EINVAL;
goto out;
#include "symbol.h"
#include "libdw.h"
#include "debug.h"
+#include "util.h"
#include <inttypes.h>
#include <string.h>
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;
return NULL;
if (!srcline_full_filename)
- file = gnu_basename(file);
+ file = perf_basename(file);
if (asprintf(&srcline, "%s:%u", file, line) < 0)
return NULL;
#include "path.h"
#include "symbol_conf.h"
#include "spark.h"
+#include "util.h"
#ifdef HAVE_LIBELF_SUPPORT
#include <libelf.h>
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);
}
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;
+}
};
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,