1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compiler.h>
3 #include <linux/kernel.h>
4 #include <linux/string.h>
5 #include <linux/zalloc.h>
16 #include "util.h" // rm_rf_perf_data()
19 #include <internal/lib.h>
21 static void close_dir(struct perf_data_file *files, int nr)
25 zfree(&files[nr].path);
30 void perf_data__close_dir(struct perf_data *data)
32 close_dir(data->dir.files, data->dir.nr);
35 int perf_data__create_dir(struct perf_data *data, int nr)
37 struct perf_data_file *files = NULL;
40 if (WARN_ON(!data->is_dir))
43 files = zalloc(nr * sizeof(*files));
47 for (i = 0; i < nr; i++) {
48 struct perf_data_file *file = &files[i];
50 ret = asprintf(&file->path, "%s/data.%d", data->path, i);
54 ret = open(file->path, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
61 data->dir.version = PERF_DIR_VERSION;
62 data->dir.files = files;
71 int perf_data__open_dir(struct perf_data *data)
73 struct perf_data_file *files = NULL;
80 * Directory containing a single regular perf data file which is already
81 * open, means there is nothing more to do here.
83 if (perf_data__is_single_file(data))
86 if (WARN_ON(!data->is_dir))
89 /* The version is provided by DIR_FORMAT feature. */
90 if (WARN_ON(data->dir.version != PERF_DIR_VERSION))
93 dir = opendir(data->path);
97 while ((dent = readdir(dir)) != NULL) {
98 struct perf_data_file *file;
102 snprintf(path, sizeof(path), "%s/%s", data->path, dent->d_name);
106 if (!S_ISREG(st.st_mode) || strncmp(dent->d_name, "data.", 5))
111 file = realloc(files, (nr + 1) * sizeof(*files));
118 file->path = strdup(path);
122 ret = open(file->path, O_RDONLY);
127 file->size = st.st_size;
134 data->dir.files = files;
140 close_dir(files, nr);
144 int perf_data__update_dir(struct perf_data *data)
148 if (WARN_ON(!data->is_dir))
151 for (i = 0; i < data->dir.nr; i++) {
152 struct perf_data_file *file = &data->dir.files[i];
155 if (fstat(file->fd, &st))
158 file->size = st.st_size;
164 static bool check_pipe(struct perf_data *data)
167 bool is_pipe = false;
168 int fd = perf_data__is_read(data) ?
169 STDIN_FILENO : STDOUT_FILENO;
172 if (!fstat(fd, &st) && S_ISFIFO(st.st_mode))
175 if (!strcmp(data->path, "-"))
182 return data->is_pipe = is_pipe;
185 static int check_backup(struct perf_data *data)
189 if (perf_data__is_read(data))
192 if (!stat(data->path, &st) && st.st_size) {
193 char oldname[PATH_MAX];
196 snprintf(oldname, sizeof(oldname), "%s.old",
199 ret = rm_rf_perf_data(oldname);
201 pr_err("Can't remove old data: %s (%s)\n",
203 "Unknown file found" : strerror(errno),
208 if (rename(data->path, oldname)) {
209 pr_err("Can't move data: %s (%s to %s)\n",
211 data->path, oldname);
219 static bool is_dir(struct perf_data *data)
223 if (stat(data->path, &st))
226 return (st.st_mode & S_IFMT) == S_IFDIR;
229 static int open_file_read(struct perf_data *data)
233 char sbuf[STRERR_BUFSIZE];
235 fd = open(data->file.path, O_RDONLY);
239 pr_err("failed to open %s: %s", data->file.path,
240 str_error_r(err, sbuf, sizeof(sbuf)));
241 if (err == ENOENT && !strcmp(data->file.path, "perf.data"))
242 pr_err(" (try 'perf record' first)");
247 if (fstat(fd, &st) < 0)
250 if (!data->force && st.st_uid && (st.st_uid != geteuid())) {
251 pr_err("File %s not owned by current user or root (use -f to override)\n",
257 pr_info("zero-sized data (%s), nothing to do!\n",
262 data->file.size = st.st_size;
270 static int open_file_write(struct perf_data *data)
273 char sbuf[STRERR_BUFSIZE];
275 fd = open(data->file.path, O_CREAT|O_RDWR|O_TRUNC|O_CLOEXEC,
279 pr_err("failed to open %s : %s\n", data->file.path,
280 str_error_r(errno, sbuf, sizeof(sbuf)));
285 static int open_file(struct perf_data *data)
289 fd = perf_data__is_read(data) ?
290 open_file_read(data) : open_file_write(data);
293 zfree(&data->file.path);
301 static int open_file_dup(struct perf_data *data)
303 data->file.path = strdup(data->path);
304 if (!data->file.path)
307 return open_file(data);
310 static int open_dir(struct perf_data *data)
315 * So far we open only the header, so we can read the data version and
318 if (asprintf(&data->file.path, "%s/data", data->path) < 0)
321 if (perf_data__is_write(data) &&
322 mkdir(data->path, S_IRWXU) < 0)
325 ret = open_file(data);
327 /* Cleanup whatever we managed to create so far. */
328 if (ret && perf_data__is_write(data))
329 rm_rf_perf_data(data->path);
334 int perf_data__open(struct perf_data *data)
336 if (check_pipe(data))
340 data->path = "perf.data";
342 if (check_backup(data))
345 if (perf_data__is_read(data))
346 data->is_dir = is_dir(data);
348 return perf_data__is_dir(data) ?
349 open_dir(data) : open_file_dup(data);
352 void perf_data__close(struct perf_data *data)
354 if (perf_data__is_dir(data))
355 perf_data__close_dir(data);
357 zfree(&data->file.path);
358 close(data->file.fd);
361 ssize_t perf_data_file__write(struct perf_data_file *file,
362 void *buf, size_t size)
364 return writen(file->fd, buf, size);
367 ssize_t perf_data__write(struct perf_data *data,
368 void *buf, size_t size)
370 return perf_data_file__write(&data->file, buf, size);
373 int perf_data__switch(struct perf_data *data,
375 size_t pos, bool at_exit,
380 if (check_pipe(data))
382 if (perf_data__is_read(data))
385 if (asprintf(new_filepath, "%s.%s", data->path, postfix) < 0)
389 * Only fire a warning, don't return error, continue fill
392 if (rename(data->path, *new_filepath))
393 pr_warning("Failed to rename %s to %s\n", data->path, *new_filepath);
396 close(data->file.fd);
397 ret = perf_data__open(data);
401 if (lseek(data->file.fd, pos, SEEK_SET) == (off_t)-1) {
403 pr_debug("Failed to lseek to %zu: %s",
404 pos, strerror(errno));
413 unsigned long perf_data__size(struct perf_data *data)
415 u64 size = data->file.size;
418 if (perf_data__is_single_file(data))
421 for (i = 0; i < data->dir.nr; i++) {
422 struct perf_data_file *file = &data->dir.files[i];
430 int perf_data__make_kcore_dir(struct perf_data *data, char *buf, size_t buf_sz)
437 ret = snprintf(buf, buf_sz, "%s/kcore_dir", data->path);
438 if (ret < 0 || (size_t)ret >= buf_sz)
441 return mkdir(buf, S_IRWXU);
444 char *perf_data__kallsyms_name(struct perf_data *data)
452 if (asprintf(&kallsyms_name, "%s/kcore_dir/kallsyms", data->path) < 0)
455 if (stat(kallsyms_name, &st)) {
460 return kallsyms_name;