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>
17 #include "util.h" // rm_rf_perf_data()
20 #include <internal/lib.h>
22 static void close_dir(struct perf_data_file *files, int nr)
26 zfree(&files[nr].path);
31 void perf_data__close_dir(struct perf_data *data)
33 close_dir(data->dir.files, data->dir.nr);
36 int perf_data__create_dir(struct perf_data *data, int nr)
38 struct perf_data_file *files = NULL;
41 if (WARN_ON(!data->is_dir))
44 files = zalloc(nr * sizeof(*files));
48 for (i = 0; i < nr; i++) {
49 struct perf_data_file *file = &files[i];
51 ret = asprintf(&file->path, "%s/data.%d", data->path, i);
57 ret = open(file->path, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
66 data->dir.version = PERF_DIR_VERSION;
67 data->dir.files = files;
76 int perf_data__open_dir(struct perf_data *data)
78 struct perf_data_file *files = NULL;
85 * Directory containing a single regular perf data file which is already
86 * open, means there is nothing more to do here.
88 if (perf_data__is_single_file(data))
91 if (WARN_ON(!data->is_dir))
94 /* The version is provided by DIR_FORMAT feature. */
95 if (WARN_ON(data->dir.version != PERF_DIR_VERSION))
98 dir = opendir(data->path);
102 while ((dent = readdir(dir)) != NULL) {
103 struct perf_data_file *file;
107 snprintf(path, sizeof(path), "%s/%s", data->path, dent->d_name);
111 if (!S_ISREG(st.st_mode) || strncmp(dent->d_name, "data.", 5))
116 file = realloc(files, (nr + 1) * sizeof(*files));
123 file->path = strdup(path);
127 ret = open(file->path, O_RDONLY);
132 file->size = st.st_size;
138 data->dir.files = files;
143 close_dir(files, nr);
147 int perf_data__update_dir(struct perf_data *data)
151 if (WARN_ON(!data->is_dir))
154 for (i = 0; i < data->dir.nr; i++) {
155 struct perf_data_file *file = &data->dir.files[i];
158 if (fstat(file->fd, &st))
161 file->size = st.st_size;
167 static bool check_pipe(struct perf_data *data)
170 bool is_pipe = false;
171 int fd = perf_data__is_read(data) ?
172 STDIN_FILENO : STDOUT_FILENO;
175 if (!fstat(fd, &st) && S_ISFIFO(st.st_mode))
178 if (!strcmp(data->path, "-"))
183 if (data->use_stdio) {
186 mode = perf_data__is_read(data) ? "r" : "w";
187 data->file.fptr = fdopen(fd, mode);
189 if (data->file.fptr == NULL) {
191 data->use_stdio = false;
198 return data->is_pipe = is_pipe;
201 static int check_backup(struct perf_data *data)
205 if (perf_data__is_read(data))
208 if (!stat(data->path, &st) && st.st_size) {
209 char oldname[PATH_MAX];
212 snprintf(oldname, sizeof(oldname), "%s.old",
215 ret = rm_rf_perf_data(oldname);
217 pr_err("Can't remove old data: %s (%s)\n",
219 "Unknown file found" : strerror(errno),
224 if (rename(data->path, oldname)) {
225 pr_err("Can't move data: %s (%s to %s)\n",
227 data->path, oldname);
235 static bool is_dir(struct perf_data *data)
239 if (stat(data->path, &st))
242 return (st.st_mode & S_IFMT) == S_IFDIR;
245 static int open_file_read(struct perf_data *data)
247 int flags = data->in_place_update ? O_RDWR : O_RDONLY;
250 char sbuf[STRERR_BUFSIZE];
252 fd = open(data->file.path, flags);
256 pr_err("failed to open %s: %s", data->file.path,
257 str_error_r(err, sbuf, sizeof(sbuf)));
258 if (err == ENOENT && !strcmp(data->file.path, "perf.data"))
259 pr_err(" (try 'perf record' first)");
264 if (fstat(fd, &st) < 0)
267 if (!data->force && st.st_uid && (st.st_uid != geteuid())) {
268 pr_err("File %s not owned by current user or root (use -f to override)\n",
274 pr_info("zero-sized data (%s), nothing to do!\n",
279 data->file.size = st.st_size;
287 static int open_file_write(struct perf_data *data)
290 char sbuf[STRERR_BUFSIZE];
292 fd = open(data->file.path, O_CREAT|O_RDWR|O_TRUNC|O_CLOEXEC,
296 pr_err("failed to open %s : %s\n", data->file.path,
297 str_error_r(errno, sbuf, sizeof(sbuf)));
302 static int open_file(struct perf_data *data)
306 fd = perf_data__is_read(data) ?
307 open_file_read(data) : open_file_write(data);
310 zfree(&data->file.path);
318 static int open_file_dup(struct perf_data *data)
320 data->file.path = strdup(data->path);
321 if (!data->file.path)
324 return open_file(data);
327 static int open_dir(struct perf_data *data)
332 * So far we open only the header, so we can read the data version and
335 if (asprintf(&data->file.path, "%s/data", data->path) < 0)
338 if (perf_data__is_write(data) &&
339 mkdir(data->path, S_IRWXU) < 0)
342 ret = open_file(data);
344 /* Cleanup whatever we managed to create so far. */
345 if (ret && perf_data__is_write(data))
346 rm_rf_perf_data(data->path);
351 int perf_data__open(struct perf_data *data)
353 if (check_pipe(data))
356 /* currently it allows stdio for pipe only */
357 data->use_stdio = false;
360 data->path = "perf.data";
362 if (check_backup(data))
365 if (perf_data__is_read(data))
366 data->is_dir = is_dir(data);
368 return perf_data__is_dir(data) ?
369 open_dir(data) : open_file_dup(data);
372 void perf_data__close(struct perf_data *data)
374 if (perf_data__is_dir(data))
375 perf_data__close_dir(data);
377 zfree(&data->file.path);
380 fclose(data->file.fptr);
382 close(data->file.fd);
385 ssize_t perf_data__read(struct perf_data *data, void *buf, size_t size)
387 if (data->use_stdio) {
388 if (fread(buf, size, 1, data->file.fptr) == 1)
390 return feof(data->file.fptr) ? 0 : -1;
392 return readn(data->file.fd, buf, size);
395 ssize_t perf_data_file__write(struct perf_data_file *file,
396 void *buf, size_t size)
398 return writen(file->fd, buf, size);
401 ssize_t perf_data__write(struct perf_data *data,
402 void *buf, size_t size)
404 if (data->use_stdio) {
405 if (fwrite(buf, size, 1, data->file.fptr) == 1)
409 return perf_data_file__write(&data->file, buf, size);
412 int perf_data__switch(struct perf_data *data,
414 size_t pos, bool at_exit,
419 if (check_pipe(data))
421 if (perf_data__is_read(data))
424 if (asprintf(new_filepath, "%s.%s", data->path, postfix) < 0)
428 * Only fire a warning, don't return error, continue fill
431 if (rename(data->path, *new_filepath))
432 pr_warning("Failed to rename %s to %s\n", data->path, *new_filepath);
435 close(data->file.fd);
436 ret = perf_data__open(data);
440 if (lseek(data->file.fd, pos, SEEK_SET) == (off_t)-1) {
442 pr_debug("Failed to lseek to %zu: %s",
443 pos, strerror(errno));
452 unsigned long perf_data__size(struct perf_data *data)
454 u64 size = data->file.size;
457 if (perf_data__is_single_file(data))
460 for (i = 0; i < data->dir.nr; i++) {
461 struct perf_data_file *file = &data->dir.files[i];
469 int perf_data__make_kcore_dir(struct perf_data *data, char *buf, size_t buf_sz)
476 ret = snprintf(buf, buf_sz, "%s/kcore_dir", data->path);
477 if (ret < 0 || (size_t)ret >= buf_sz)
480 return mkdir(buf, S_IRWXU);
483 bool has_kcore_dir(const char *path)
485 struct dirent *d = ERR_PTR(-EINVAL);
486 const char *name = "kcore_dir";
487 DIR *dir = opendir(path);
488 size_t n = strlen(name);
492 while (d && !result) {
494 result = d ? strncmp(d->d_name, name, n) : false;
502 char *perf_data__kallsyms_name(struct perf_data *data)
510 if (asprintf(&kallsyms_name, "%s/kcore_dir/kallsyms", data->path) < 0)
513 if (stat(kallsyms_name, &st)) {
518 return kallsyms_name;
521 char *perf_data__guest_kallsyms_name(struct perf_data *data, pid_t machine_pid)
529 if (asprintf(&kallsyms_name, "%s/kcore_dir__%d/kallsyms", data->path, machine_pid) < 0)
532 if (stat(kallsyms_name, &st)) {
537 return kallsyms_name;
540 bool is_perf_data(const char *path)
546 file = fopen(path, "r");
550 if (fread(&magic, 1, 8, file) < 8)
553 ret = is_perf_magic(magic);