Merge tag 'powerpc-5.11-3' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[linux/fpc-iii.git] / tools / perf / util / data.c
blobf29af4fc3d09390c90aa53c7e5f5434fb979e342
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>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <asm/bug.h>
13 #include <dirent.h>
15 #include "data.h"
16 #include "util.h" // rm_rf_perf_data()
17 #include "debug.h"
18 #include "header.h"
19 #include <internal/lib.h>
21 static void close_dir(struct perf_data_file *files, int nr)
23 while (--nr >= 1) {
24 close(files[nr].fd);
25 zfree(&files[nr].path);
27 free(files);
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;
38 int i, ret = -1;
40 if (WARN_ON(!data->is_dir))
41 return -EINVAL;
43 files = zalloc(nr * sizeof(*files));
44 if (!files)
45 return -ENOMEM;
47 data->dir.version = PERF_DIR_VERSION;
48 data->dir.files = files;
49 data->dir.nr = nr;
51 for (i = 0; i < nr; i++) {
52 struct perf_data_file *file = &files[i];
54 if (asprintf(&file->path, "%s/data.%d", data->path, i) < 0)
55 goto out_err;
57 ret = open(file->path, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
58 if (ret < 0)
59 goto out_err;
61 file->fd = ret;
64 return 0;
66 out_err:
67 close_dir(files, i);
68 return ret;
71 int perf_data__open_dir(struct perf_data *data)
73 struct perf_data_file *files = NULL;
74 struct dirent *dent;
75 int ret = -1;
76 DIR *dir;
77 int nr = 0;
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))
84 return 0;
86 if (WARN_ON(!data->is_dir))
87 return -EINVAL;
89 /* The version is provided by DIR_FORMAT feature. */
90 if (WARN_ON(data->dir.version != PERF_DIR_VERSION))
91 return -1;
93 dir = opendir(data->path);
94 if (!dir)
95 return -EINVAL;
97 while ((dent = readdir(dir)) != NULL) {
98 struct perf_data_file *file;
99 char path[PATH_MAX];
100 struct stat st;
102 snprintf(path, sizeof(path), "%s/%s", data->path, dent->d_name);
103 if (stat(path, &st))
104 continue;
106 if (!S_ISREG(st.st_mode) || strncmp(dent->d_name, "data.", 5))
107 continue;
109 ret = -ENOMEM;
111 file = realloc(files, (nr + 1) * sizeof(*files));
112 if (!file)
113 goto out_err;
115 files = file;
116 file = &files[nr++];
118 file->path = strdup(path);
119 if (!file->path)
120 goto out_err;
122 ret = open(file->path, O_RDONLY);
123 if (ret < 0)
124 goto out_err;
126 file->fd = ret;
127 file->size = st.st_size;
130 if (!files)
131 return -EINVAL;
133 data->dir.files = files;
134 data->dir.nr = nr;
135 return 0;
137 out_err:
138 close_dir(files, nr);
139 return ret;
142 int perf_data__update_dir(struct perf_data *data)
144 int i;
146 if (WARN_ON(!data->is_dir))
147 return -EINVAL;
149 for (i = 0; i < data->dir.nr; i++) {
150 struct perf_data_file *file = &data->dir.files[i];
151 struct stat st;
153 if (fstat(file->fd, &st))
154 return -1;
156 file->size = st.st_size;
159 return 0;
162 static bool check_pipe(struct perf_data *data)
164 struct stat st;
165 bool is_pipe = false;
166 int fd = perf_data__is_read(data) ?
167 STDIN_FILENO : STDOUT_FILENO;
169 if (!data->path) {
170 if (!fstat(fd, &st) && S_ISFIFO(st.st_mode))
171 is_pipe = true;
172 } else {
173 if (!strcmp(data->path, "-"))
174 is_pipe = true;
177 if (is_pipe) {
178 if (data->use_stdio) {
179 const char *mode;
181 mode = perf_data__is_read(data) ? "r" : "w";
182 data->file.fptr = fdopen(fd, mode);
184 if (data->file.fptr == NULL) {
185 data->file.fd = fd;
186 data->use_stdio = false;
188 } else {
189 data->file.fd = fd;
193 return data->is_pipe = is_pipe;
196 static int check_backup(struct perf_data *data)
198 struct stat st;
200 if (perf_data__is_read(data))
201 return 0;
203 if (!stat(data->path, &st) && st.st_size) {
204 char oldname[PATH_MAX];
205 int ret;
207 snprintf(oldname, sizeof(oldname), "%s.old",
208 data->path);
210 ret = rm_rf_perf_data(oldname);
211 if (ret) {
212 pr_err("Can't remove old data: %s (%s)\n",
213 ret == -2 ?
214 "Unknown file found" : strerror(errno),
215 oldname);
216 return -1;
219 if (rename(data->path, oldname)) {
220 pr_err("Can't move data: %s (%s to %s)\n",
221 strerror(errno),
222 data->path, oldname);
223 return -1;
227 return 0;
230 static bool is_dir(struct perf_data *data)
232 struct stat st;
234 if (stat(data->path, &st))
235 return false;
237 return (st.st_mode & S_IFMT) == S_IFDIR;
240 static int open_file_read(struct perf_data *data)
242 struct stat st;
243 int fd;
244 char sbuf[STRERR_BUFSIZE];
246 fd = open(data->file.path, O_RDONLY);
247 if (fd < 0) {
248 int err = errno;
250 pr_err("failed to open %s: %s", data->file.path,
251 str_error_r(err, sbuf, sizeof(sbuf)));
252 if (err == ENOENT && !strcmp(data->file.path, "perf.data"))
253 pr_err(" (try 'perf record' first)");
254 pr_err("\n");
255 return -err;
258 if (fstat(fd, &st) < 0)
259 goto out_close;
261 if (!data->force && st.st_uid && (st.st_uid != geteuid())) {
262 pr_err("File %s not owned by current user or root (use -f to override)\n",
263 data->file.path);
264 goto out_close;
267 if (!st.st_size) {
268 pr_info("zero-sized data (%s), nothing to do!\n",
269 data->file.path);
270 goto out_close;
273 data->file.size = st.st_size;
274 return fd;
276 out_close:
277 close(fd);
278 return -1;
281 static int open_file_write(struct perf_data *data)
283 int fd;
284 char sbuf[STRERR_BUFSIZE];
286 fd = open(data->file.path, O_CREAT|O_RDWR|O_TRUNC|O_CLOEXEC,
287 S_IRUSR|S_IWUSR);
289 if (fd < 0)
290 pr_err("failed to open %s : %s\n", data->file.path,
291 str_error_r(errno, sbuf, sizeof(sbuf)));
293 return fd;
296 static int open_file(struct perf_data *data)
298 int fd;
300 fd = perf_data__is_read(data) ?
301 open_file_read(data) : open_file_write(data);
303 if (fd < 0) {
304 zfree(&data->file.path);
305 return -1;
308 data->file.fd = fd;
309 return 0;
312 static int open_file_dup(struct perf_data *data)
314 data->file.path = strdup(data->path);
315 if (!data->file.path)
316 return -ENOMEM;
318 return open_file(data);
321 static int open_dir(struct perf_data *data)
323 int ret;
326 * So far we open only the header, so we can read the data version and
327 * layout.
329 if (asprintf(&data->file.path, "%s/data", data->path) < 0)
330 return -1;
332 if (perf_data__is_write(data) &&
333 mkdir(data->path, S_IRWXU) < 0)
334 return -1;
336 ret = open_file(data);
338 /* Cleanup whatever we managed to create so far. */
339 if (ret && perf_data__is_write(data))
340 rm_rf_perf_data(data->path);
342 return ret;
345 int perf_data__open(struct perf_data *data)
347 if (check_pipe(data))
348 return 0;
350 /* currently it allows stdio for pipe only */
351 data->use_stdio = false;
353 if (!data->path)
354 data->path = "perf.data";
356 if (check_backup(data))
357 return -1;
359 if (perf_data__is_read(data))
360 data->is_dir = is_dir(data);
362 return perf_data__is_dir(data) ?
363 open_dir(data) : open_file_dup(data);
366 void perf_data__close(struct perf_data *data)
368 if (perf_data__is_dir(data))
369 perf_data__close_dir(data);
371 zfree(&data->file.path);
373 if (data->use_stdio)
374 fclose(data->file.fptr);
375 else
376 close(data->file.fd);
379 ssize_t perf_data__read(struct perf_data *data, void *buf, size_t size)
381 if (data->use_stdio) {
382 if (fread(buf, size, 1, data->file.fptr) == 1)
383 return size;
384 return feof(data->file.fptr) ? 0 : -1;
386 return readn(data->file.fd, buf, size);
389 ssize_t perf_data_file__write(struct perf_data_file *file,
390 void *buf, size_t size)
392 return writen(file->fd, buf, size);
395 ssize_t perf_data__write(struct perf_data *data,
396 void *buf, size_t size)
398 if (data->use_stdio) {
399 if (fwrite(buf, size, 1, data->file.fptr) == 1)
400 return size;
401 return -1;
403 return perf_data_file__write(&data->file, buf, size);
406 int perf_data__switch(struct perf_data *data,
407 const char *postfix,
408 size_t pos, bool at_exit,
409 char **new_filepath)
411 int ret;
413 if (check_pipe(data))
414 return -EINVAL;
415 if (perf_data__is_read(data))
416 return -EINVAL;
418 if (asprintf(new_filepath, "%s.%s", data->path, postfix) < 0)
419 return -ENOMEM;
422 * Only fire a warning, don't return error, continue fill
423 * original file.
425 if (rename(data->path, *new_filepath))
426 pr_warning("Failed to rename %s to %s\n", data->path, *new_filepath);
428 if (!at_exit) {
429 close(data->file.fd);
430 ret = perf_data__open(data);
431 if (ret < 0)
432 goto out;
434 if (lseek(data->file.fd, pos, SEEK_SET) == (off_t)-1) {
435 ret = -errno;
436 pr_debug("Failed to lseek to %zu: %s",
437 pos, strerror(errno));
438 goto out;
441 ret = data->file.fd;
442 out:
443 return ret;
446 unsigned long perf_data__size(struct perf_data *data)
448 u64 size = data->file.size;
449 int i;
451 if (perf_data__is_single_file(data))
452 return size;
454 for (i = 0; i < data->dir.nr; i++) {
455 struct perf_data_file *file = &data->dir.files[i];
457 size += file->size;
460 return size;
463 int perf_data__make_kcore_dir(struct perf_data *data, char *buf, size_t buf_sz)
465 int ret;
467 if (!data->is_dir)
468 return -1;
470 ret = snprintf(buf, buf_sz, "%s/kcore_dir", data->path);
471 if (ret < 0 || (size_t)ret >= buf_sz)
472 return -1;
474 return mkdir(buf, S_IRWXU);
477 char *perf_data__kallsyms_name(struct perf_data *data)
479 char *kallsyms_name;
480 struct stat st;
482 if (!data->is_dir)
483 return NULL;
485 if (asprintf(&kallsyms_name, "%s/kcore_dir/kallsyms", data->path) < 0)
486 return NULL;
488 if (stat(kallsyms_name, &st)) {
489 free(kallsyms_name);
490 return NULL;
493 return kallsyms_name;
496 bool is_perf_data(const char *path)
498 bool ret = false;
499 FILE *file;
500 u64 magic;
502 file = fopen(path, "r");
503 if (!file)
504 return false;
506 if (fread(&magic, 1, 8, file) < 8)
507 goto out;
509 ret = is_perf_magic(magic);
510 out:
511 fclose(file);
512 return ret;