^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/zalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <asm/bug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <dirent.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "data.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "util.h" // rm_rf_perf_data()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "header.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <internal/lib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static void close_dir(struct perf_data_file *files, int nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) while (--nr >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) close(files[nr].fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) zfree(&files[nr].path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) free(files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) void perf_data__close_dir(struct perf_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) close_dir(data->dir.files, data->dir.nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) int perf_data__create_dir(struct perf_data *data, int nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct perf_data_file *files = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (WARN_ON(!data->is_dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) files = zalloc(nr * sizeof(*files));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (!files)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) for (i = 0; i < nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct perf_data_file *file = &files[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) ret = asprintf(&file->path, "%s/data.%d", data->path, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) ret = open(file->path, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) file->fd = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) data->dir.version = PERF_DIR_VERSION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) data->dir.files = files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) data->dir.nr = nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) close_dir(files, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int perf_data__open_dir(struct perf_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct perf_data_file *files = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct dirent *dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) int ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) DIR *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) int nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * Directory containing a single regular perf data file which is already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * open, means there is nothing more to do here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (perf_data__is_single_file(data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (WARN_ON(!data->is_dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /* The version is provided by DIR_FORMAT feature. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (WARN_ON(data->dir.version != PERF_DIR_VERSION))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) dir = opendir(data->path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (!dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) while ((dent = readdir(dir)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct perf_data_file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) char path[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) snprintf(path, sizeof(path), "%s/%s", data->path, dent->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (stat(path, &st))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (!S_ISREG(st.st_mode) || strncmp(dent->d_name, "data.", 5))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) file = realloc(files, (nr + 1) * sizeof(*files));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (!file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) files = file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) file = &files[nr++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) file->path = strdup(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (!file->path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) ret = open(file->path, O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) file->fd = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) file->size = st.st_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (!files)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) data->dir.files = files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) data->dir.nr = nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) close_dir(files, nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) int perf_data__update_dir(struct perf_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (WARN_ON(!data->is_dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) for (i = 0; i < data->dir.nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct perf_data_file *file = &data->dir.files[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (fstat(file->fd, &st))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) file->size = st.st_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static bool check_pipe(struct perf_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) struct stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) bool is_pipe = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) int fd = perf_data__is_read(data) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) STDIN_FILENO : STDOUT_FILENO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (!data->path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (!fstat(fd, &st) && S_ISFIFO(st.st_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) is_pipe = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (!strcmp(data->path, "-"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) is_pipe = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (is_pipe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) data->file.fd = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return data->is_pipe = is_pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static int check_backup(struct perf_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) struct stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (perf_data__is_read(data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (!stat(data->path, &st) && st.st_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) char oldname[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) snprintf(oldname, sizeof(oldname), "%s.old",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) data->path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) ret = rm_rf_perf_data(oldname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) pr_err("Can't remove old data: %s (%s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) ret == -2 ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) "Unknown file found" : strerror(errno),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) oldname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (rename(data->path, oldname)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) pr_err("Can't move data: %s (%s to %s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) strerror(errno),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) data->path, oldname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) static bool is_dir(struct perf_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) struct stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (stat(data->path, &st))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) return (st.st_mode & S_IFMT) == S_IFDIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) static int open_file_read(struct perf_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) struct stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) char sbuf[STRERR_BUFSIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) fd = open(data->file.path, O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) int err = errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) pr_err("failed to open %s: %s", data->file.path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) str_error_r(err, sbuf, sizeof(sbuf)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (err == ENOENT && !strcmp(data->file.path, "perf.data"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) pr_err(" (try 'perf record' first)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) pr_err("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) return -err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (fstat(fd, &st) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) goto out_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (!data->force && st.st_uid && (st.st_uid != geteuid())) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) pr_err("File %s not owned by current user or root (use -f to override)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) data->file.path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) goto out_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (!st.st_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) pr_info("zero-sized data (%s), nothing to do!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) data->file.path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) goto out_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) data->file.size = st.st_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) return fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) out_close:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static int open_file_write(struct perf_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) char sbuf[STRERR_BUFSIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) fd = open(data->file.path, O_CREAT|O_RDWR|O_TRUNC|O_CLOEXEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) S_IRUSR|S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) pr_err("failed to open %s : %s\n", data->file.path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) str_error_r(errno, sbuf, sizeof(sbuf)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) return fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) static int open_file(struct perf_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) fd = perf_data__is_read(data) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) open_file_read(data) : open_file_write(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) zfree(&data->file.path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) data->file.fd = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) static int open_file_dup(struct perf_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) data->file.path = strdup(data->path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (!data->file.path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return open_file(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) static int open_dir(struct perf_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) * So far we open only the header, so we can read the data version and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) * layout.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (asprintf(&data->file.path, "%s/data", data->path) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (perf_data__is_write(data) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) mkdir(data->path, S_IRWXU) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) ret = open_file(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) /* Cleanup whatever we managed to create so far. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (ret && perf_data__is_write(data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) rm_rf_perf_data(data->path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) int perf_data__open(struct perf_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (check_pipe(data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (!data->path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) data->path = "perf.data";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (check_backup(data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if (perf_data__is_read(data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) data->is_dir = is_dir(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) return perf_data__is_dir(data) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) open_dir(data) : open_file_dup(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) void perf_data__close(struct perf_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (perf_data__is_dir(data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) perf_data__close_dir(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) zfree(&data->file.path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) close(data->file.fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) ssize_t perf_data_file__write(struct perf_data_file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) void *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) return writen(file->fd, buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) ssize_t perf_data__write(struct perf_data *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) void *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) return perf_data_file__write(&data->file, buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) int perf_data__switch(struct perf_data *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) const char *postfix,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) size_t pos, bool at_exit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) char **new_filepath)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (check_pipe(data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (perf_data__is_read(data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) if (asprintf(new_filepath, "%s.%s", data->path, postfix) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) * Only fire a warning, don't return error, continue fill
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) * original file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) if (rename(data->path, *new_filepath))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) pr_warning("Failed to rename %s to %s\n", data->path, *new_filepath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) if (!at_exit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) close(data->file.fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) ret = perf_data__open(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) if (lseek(data->file.fd, pos, SEEK_SET) == (off_t)-1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) ret = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) pr_debug("Failed to lseek to %zu: %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) pos, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) ret = data->file.fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) unsigned long perf_data__size(struct perf_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) u64 size = data->file.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) if (perf_data__is_single_file(data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) for (i = 0; i < data->dir.nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) struct perf_data_file *file = &data->dir.files[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) size += file->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) int perf_data__make_kcore_dir(struct perf_data *data, char *buf, size_t buf_sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) if (!data->is_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) ret = snprintf(buf, buf_sz, "%s/kcore_dir", data->path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) if (ret < 0 || (size_t)ret >= buf_sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) return mkdir(buf, S_IRWXU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) char *perf_data__kallsyms_name(struct perf_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) char *kallsyms_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) struct stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) if (!data->is_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) if (asprintf(&kallsyms_name, "%s/kcore_dir/kallsyms", data->path) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) if (stat(kallsyms_name, &st)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) free(kallsyms_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) return kallsyms_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }