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 data->dir.version = PERF_DIR_VERSION;
48 data->dir.files = files;
51 for (i = 0; i < nr; i++) {
52 struct perf_data_file *file = &files[i];
54 ret = asprintf(&file->path, "%s/data.%d", data->path, i);
58 ret = open(file->path, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
72 int perf_data__open_dir(struct perf_data *data)
74 struct perf_data_file *files = NULL;
81 * Directory containing a single regular perf data file which is already
82 * open, means there is nothing more to do here.
84 if (perf_data__is_single_file(data))
87 if (WARN_ON(!data->is_dir))
90 /* The version is provided by DIR_FORMAT feature. */
91 if (WARN_ON(data->dir.version != PERF_DIR_VERSION))
94 dir = opendir(data->path);
98 while ((dent = readdir(dir)) != NULL) {
99 struct perf_data_file *file;
103 snprintf(path, sizeof(path), "%s/%s", data->path, dent->d_name);
107 if (!S_ISREG(st.st_mode) || strncmp(dent->d_name, "data.", 5))
112 file = realloc(files, (nr + 1) * sizeof(*files));
119 file->path = strdup(path);
123 ret = open(file->path, O_RDONLY);
128 file->size = st.st_size;
134 data->dir.files = files;
139 close_dir(files, nr);
143 int perf_data__update_dir(struct perf_data *data)
147 if (WARN_ON(!data->is_dir))
150 for (i = 0; i < data->dir.nr; i++) {
151 struct perf_data_file *file = &data->dir.files[i];
154 if (fstat(file->fd, &st))
157 file->size = st.st_size;
163 static bool check_pipe(struct perf_data *data)
166 bool is_pipe = false;
167 int fd = perf_data__is_read(data) ?
168 STDIN_FILENO : STDOUT_FILENO;
171 if (!fstat(fd, &st) && S_ISFIFO(st.st_mode))
174 if (!strcmp(data->path, "-"))
179 if (data->use_stdio) {
182 mode = perf_data__is_read(data) ? "r" : "w";
183 data->file.fptr = fdopen(fd, mode);
185 if (data->file.fptr == NULL) {
187 data->use_stdio = false;
194 return data->is_pipe = is_pipe;
197 static int check_backup(struct perf_data *data)
201 if (perf_data__is_read(data))
204 if (!stat(data->path, &st) && st.st_size) {
205 char oldname[PATH_MAX];
208 snprintf(oldname, sizeof(oldname), "%s.old",
211 ret = rm_rf_perf_data(oldname);
213 pr_err("Can't remove old data: %s (%s)\n",
215 "Unknown file found" : strerror(errno),
220 if (rename(data->path, oldname)) {
221 pr_err("Can't move data: %s (%s to %s)\n",
223 data->path, oldname);
231 static bool is_dir(struct perf_data *data)
235 if (stat(data->path, &st))
238 return (st.st_mode & S_IFMT) == S_IFDIR;
241 static int open_file_read(struct perf_data *data)
243 int flags = data->in_place_update ? O_RDWR : O_RDONLY;
246 char sbuf[STRERR_BUFSIZE];
248 fd = open(data->file.path, flags);
252 pr_err("failed to open %s: %s", data->file.path,
253 str_error_r(err, sbuf, sizeof(sbuf)));
254 if (err == ENOENT && !strcmp(data->file.path, "perf.data"))
255 pr_err(" (try 'perf record' first)");
260 if (fstat(fd, &st) < 0)
263 if (!data->force && st.st_uid && (st.st_uid != geteuid())) {
264 pr_err("File %s not owned by current user or root (use -f to override)\n",
270 pr_info("zero-sized data (%s), nothing to do!\n",
275 data->file.size = st.st_size;
283 static int open_file_write(struct perf_data *data)
286 char sbuf[STRERR_BUFSIZE];
288 fd = open(data->file.path, O_CREAT|O_RDWR|O_TRUNC|O_CLOEXEC,
292 pr_err("failed to open %s : %s\n", data->file.path,
293 str_error_r(errno, sbuf, sizeof(sbuf)));
298 static int open_file(struct perf_data *data)
302 fd = perf_data__is_read(data) ?
303 open_file_read(data) : open_file_write(data);
306 zfree(&data->file.path);
314 static int open_file_dup(struct perf_data *data)
316 data->file.path = strdup(data->path);
317 if (!data->file.path)
320 return open_file(data);
323 static int open_dir(struct perf_data *data)
328 * So far we open only the header, so we can read the data version and
331 if (asprintf(&data->file.path, "%s/data", data->path) < 0)
334 if (perf_data__is_write(data) &&
335 mkdir(data->path, S_IRWXU) < 0)
338 ret = open_file(data);
340 /* Cleanup whatever we managed to create so far. */
341 if (ret && perf_data__is_write(data))
342 rm_rf_perf_data(data->path);
347 int perf_data__open(struct perf_data *data)
349 if (check_pipe(data))
352 /* currently it allows stdio for pipe only */
353 data->use_stdio = false;
356 data->path = "perf.data";
358 if (check_backup(data))
361 if (perf_data__is_read(data))
362 data->is_dir = is_dir(data);
364 return perf_data__is_dir(data) ?
365 open_dir(data) : open_file_dup(data);
368 void perf_data__close(struct perf_data *data)
370 if (perf_data__is_dir(data))
371 perf_data__close_dir(data);
373 zfree(&data->file.path);
376 fclose(data->file.fptr);
378 close(data->file.fd);
381 ssize_t perf_data__read(struct perf_data *data, void *buf, size_t size)
383 if (data->use_stdio) {
384 if (fread(buf, size, 1, data->file.fptr) == 1)
386 return feof(data->file.fptr) ? 0 : -1;
388 return readn(data->file.fd, buf, size);
391 ssize_t perf_data_file__write(struct perf_data_file *file,
392 void *buf, size_t size)
394 return writen(file->fd, buf, size);
397 ssize_t perf_data__write(struct perf_data *data,
398 void *buf, size_t size)
400 if (data->use_stdio) {
401 if (fwrite(buf, size, 1, data->file.fptr) == 1)
405 return perf_data_file__write(&data->file, buf, size);
408 int perf_data__switch(struct perf_data *data,
410 size_t pos, bool at_exit,
415 if (check_pipe(data))
417 if (perf_data__is_read(data))
420 if (asprintf(new_filepath, "%s.%s", data->path, postfix) < 0)
424 * Only fire a warning, don't return error, continue fill
427 if (rename(data->path, *new_filepath))
428 pr_warning("Failed to rename %s to %s\n", data->path, *new_filepath);
431 close(data->file.fd);
432 ret = perf_data__open(data);
436 if (lseek(data->file.fd, pos, SEEK_SET) == (off_t)-1) {
438 pr_debug("Failed to lseek to %zu: %s",
439 pos, strerror(errno));
448 unsigned long perf_data__size(struct perf_data *data)
450 u64 size = data->file.size;
453 if (perf_data__is_single_file(data))
456 for (i = 0; i < data->dir.nr; i++) {
457 struct perf_data_file *file = &data->dir.files[i];
465 int perf_data__make_kcore_dir(struct perf_data *data, char *buf, size_t buf_sz)
472 ret = snprintf(buf, buf_sz, "%s/kcore_dir", data->path);
473 if (ret < 0 || (size_t)ret >= buf_sz)
476 return mkdir(buf, S_IRWXU);
479 char *perf_data__kallsyms_name(struct perf_data *data)
487 if (asprintf(&kallsyms_name, "%s/kcore_dir/kallsyms", data->path) < 0)
490 if (stat(kallsyms_name, &st)) {
495 return kallsyms_name;
498 bool is_perf_data(const char *path)
504 file = fopen(path, "r");
508 if (fread(&magic, 1, 8, file) < 8)
511 ret = is_perf_magic(magic);