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);
56 ret = open(file->path, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
65 data->dir.version = PERF_DIR_VERSION;
66 data->dir.files = files;
75 int perf_data__open_dir(struct perf_data *data)
77 struct perf_data_file *files = NULL;
84 * Directory containing a single regular perf data file which is already
85 * open, means there is nothing more to do here.
87 if (perf_data__is_single_file(data))
90 if (WARN_ON(!data->is_dir))
93 /* The version is provided by DIR_FORMAT feature. */
94 if (WARN_ON(data->dir.version != PERF_DIR_VERSION))
97 dir = opendir(data->path);
101 while ((dent = readdir(dir)) != NULL) {
102 struct perf_data_file *file;
106 snprintf(path, sizeof(path), "%s/%s", data->path, dent->d_name);
110 if (!S_ISREG(st.st_mode) || strncmp(dent->d_name, "data.", 5))
115 file = realloc(files, (nr + 1) * sizeof(*files));
122 file->path = strdup(path);
126 ret = open(file->path, O_RDONLY);
131 file->size = st.st_size;
137 data->dir.files = files;
142 close_dir(files, nr);
146 int perf_data__update_dir(struct perf_data *data)
150 if (WARN_ON(!data->is_dir))
153 for (i = 0; i < data->dir.nr; i++) {
154 struct perf_data_file *file = &data->dir.files[i];
157 if (fstat(file->fd, &st))
160 file->size = st.st_size;
166 static bool check_pipe(struct perf_data *data)
169 bool is_pipe = false;
170 int fd = perf_data__is_read(data) ?
171 STDIN_FILENO : STDOUT_FILENO;
174 if (!fstat(fd, &st) && S_ISFIFO(st.st_mode))
177 if (!strcmp(data->path, "-"))
182 if (data->use_stdio) {
185 mode = perf_data__is_read(data) ? "r" : "w";
186 data->file.fptr = fdopen(fd, mode);
188 if (data->file.fptr == NULL) {
190 data->use_stdio = false;
197 return data->is_pipe = is_pipe;
200 static int check_backup(struct perf_data *data)
204 if (perf_data__is_read(data))
207 if (!stat(data->path, &st) && st.st_size) {
208 char oldname[PATH_MAX];
211 snprintf(oldname, sizeof(oldname), "%s.old",
214 ret = rm_rf_perf_data(oldname);
216 pr_err("Can't remove old data: %s (%s)\n",
218 "Unknown file found" : strerror(errno),
223 if (rename(data->path, oldname)) {
224 pr_err("Can't move data: %s (%s to %s)\n",
226 data->path, oldname);
234 static bool is_dir(struct perf_data *data)
238 if (stat(data->path, &st))
241 return (st.st_mode & S_IFMT) == S_IFDIR;
244 static int open_file_read(struct perf_data *data)
246 int flags = data->in_place_update ? O_RDWR : O_RDONLY;
249 char sbuf[STRERR_BUFSIZE];
251 fd = open(data->file.path, flags);
255 pr_err("failed to open %s: %s", data->file.path,
256 str_error_r(err, sbuf, sizeof(sbuf)));
257 if (err == ENOENT && !strcmp(data->file.path, "perf.data"))
258 pr_err(" (try 'perf record' first)");
263 if (fstat(fd, &st) < 0)
266 if (!data->force && st.st_uid && (st.st_uid != geteuid())) {
267 pr_err("File %s not owned by current user or root (use -f to override)\n",
273 pr_info("zero-sized data (%s), nothing to do!\n",
278 data->file.size = st.st_size;
286 static int open_file_write(struct perf_data *data)
289 char sbuf[STRERR_BUFSIZE];
291 fd = open(data->file.path, O_CREAT|O_RDWR|O_TRUNC|O_CLOEXEC,
295 pr_err("failed to open %s : %s\n", data->file.path,
296 str_error_r(errno, sbuf, sizeof(sbuf)));
301 static int open_file(struct perf_data *data)
305 fd = perf_data__is_read(data) ?
306 open_file_read(data) : open_file_write(data);
309 zfree(&data->file.path);
317 static int open_file_dup(struct perf_data *data)
319 data->file.path = strdup(data->path);
320 if (!data->file.path)
323 return open_file(data);
326 static int open_dir(struct perf_data *data)
331 * So far we open only the header, so we can read the data version and
334 if (asprintf(&data->file.path, "%s/data", data->path) < 0)
337 if (perf_data__is_write(data) &&
338 mkdir(data->path, S_IRWXU) < 0)
341 ret = open_file(data);
343 /* Cleanup whatever we managed to create so far. */
344 if (ret && perf_data__is_write(data))
345 rm_rf_perf_data(data->path);
350 int perf_data__open(struct perf_data *data)
352 if (check_pipe(data))
355 /* currently it allows stdio for pipe only */
356 data->use_stdio = false;
359 data->path = "perf.data";
361 if (check_backup(data))
364 if (perf_data__is_read(data))
365 data->is_dir = is_dir(data);
367 return perf_data__is_dir(data) ?
368 open_dir(data) : open_file_dup(data);
371 void perf_data__close(struct perf_data *data)
373 if (perf_data__is_dir(data))
374 perf_data__close_dir(data);
376 zfree(&data->file.path);
379 fclose(data->file.fptr);
381 close(data->file.fd);
384 ssize_t perf_data__read(struct perf_data *data, void *buf, size_t size)
386 if (data->use_stdio) {
387 if (fread(buf, size, 1, data->file.fptr) == 1)
389 return feof(data->file.fptr) ? 0 : -1;
391 return readn(data->file.fd, buf, size);
394 ssize_t perf_data_file__write(struct perf_data_file *file,
395 void *buf, size_t size)
397 return writen(file->fd, buf, size);
400 ssize_t perf_data__write(struct perf_data *data,
401 void *buf, size_t size)
403 if (data->use_stdio) {
404 if (fwrite(buf, size, 1, data->file.fptr) == 1)
408 return perf_data_file__write(&data->file, buf, size);
411 int perf_data__switch(struct perf_data *data,
413 size_t pos, bool at_exit,
418 if (check_pipe(data))
420 if (perf_data__is_read(data))
423 if (asprintf(new_filepath, "%s.%s", data->path, postfix) < 0)
427 * Only fire a warning, don't return error, continue fill
430 if (rename(data->path, *new_filepath))
431 pr_warning("Failed to rename %s to %s\n", data->path, *new_filepath);
434 close(data->file.fd);
435 ret = perf_data__open(data);
439 if (lseek(data->file.fd, pos, SEEK_SET) == (off_t)-1) {
441 pr_debug("Failed to lseek to %zu: %s",
442 pos, strerror(errno));
451 unsigned long perf_data__size(struct perf_data *data)
453 u64 size = data->file.size;
456 if (perf_data__is_single_file(data))
459 for (i = 0; i < data->dir.nr; i++) {
460 struct perf_data_file *file = &data->dir.files[i];
468 int perf_data__make_kcore_dir(struct perf_data *data, char *buf, size_t buf_sz)
475 ret = snprintf(buf, buf_sz, "%s/kcore_dir", data->path);
476 if (ret < 0 || (size_t)ret >= buf_sz)
479 return mkdir(buf, S_IRWXU);
482 char *perf_data__kallsyms_name(struct perf_data *data)
490 if (asprintf(&kallsyms_name, "%s/kcore_dir/kallsyms", data->path) < 0)
493 if (stat(kallsyms_name, &st)) {
498 return kallsyms_name;
501 bool is_perf_data(const char *path)
507 file = fopen(path, "r");
511 if (fread(&magic, 1, 8, file) < 8)
514 ret = is_perf_magic(magic);