Merge tag 'spi-fix-v6.9-merge-window' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-block.git] / tools / perf / util / data.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
6a4d98d7
JO
2#include <linux/compiler.h>
3#include <linux/kernel.h>
8520a98d 4#include <linux/string.h>
7f7c536f 5#include <linux/zalloc.h>
65691e9f 6#include <linux/err.h>
6a4d98d7
JO
7#include <sys/types.h>
8#include <sys/stat.h>
a43783ae 9#include <errno.h>
c23c2a0f 10#include <fcntl.h>
6a4d98d7
JO
11#include <unistd.h>
12#include <string.h>
14552063 13#include <asm/bug.h>
eb617670 14#include <dirent.h>
6a4d98d7
JO
15
16#include "data.h"
fb71c86c 17#include "util.h" // rm_rf_perf_data()
84f5d36f 18#include "debug.h"
258031c0 19#include "header.h"
e093a222 20#include "rlimit.h"
fb71c86c 21#include <internal/lib.h>
6a4d98d7 22
14552063
JO
23static void close_dir(struct perf_data_file *files, int nr)
24{
d4b3eedc 25 while (--nr >= 0) {
14552063 26 close(files[nr].fd);
d8f9da24 27 zfree(&files[nr].path);
14552063
JO
28 }
29 free(files);
30}
31
32void perf_data__close_dir(struct perf_data *data)
33{
34 close_dir(data->dir.files, data->dir.nr);
35}
36
37int perf_data__create_dir(struct perf_data *data, int nr)
38{
c4a85263 39 enum rlimit_action set_rlimit = NO_CHANGE;
14552063 40 struct perf_data_file *files = NULL;
f2211881 41 int i, ret;
14552063 42
ec65def1
JO
43 if (WARN_ON(!data->is_dir))
44 return -EINVAL;
45
14552063
JO
46 files = zalloc(nr * sizeof(*files));
47 if (!files)
48 return -ENOMEM;
49
14552063
JO
50 for (i = 0; i < nr; i++) {
51 struct perf_data_file *file = &files[i];
52
f2211881 53 ret = asprintf(&file->path, "%s/data.%d", data->path, i);
65e7c963
AB
54 if (ret < 0) {
55 ret = -ENOMEM;
14552063 56 goto out_err;
65e7c963 57 }
14552063 58
c4a85263 59retry_open:
14552063 60 ret = open(file->path, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
65e7c963 61 if (ret < 0) {
c4a85263
YJ
62 /*
63 * If using parallel threads to collect data,
64 * perf record needs at least 6 fds per CPU.
65 * When we run out of them try to increase the limits.
66 */
e093a222 67 if (errno == EMFILE && rlimit__increase_nofile(&set_rlimit))
c4a85263
YJ
68 goto retry_open;
69
65e7c963 70 ret = -errno;
14552063 71 goto out_err;
65e7c963 72 }
c4a85263 73 set_rlimit = NO_CHANGE;
14552063
JO
74
75 file->fd = ret;
76 }
77
69560e36
AB
78 data->dir.version = PERF_DIR_VERSION;
79 data->dir.files = files;
80 data->dir.nr = nr;
14552063
JO
81 return 0;
82
83out_err:
84 close_dir(files, i);
85 return ret;
86}
87
eb617670
JO
88int perf_data__open_dir(struct perf_data *data)
89{
90 struct perf_data_file *files = NULL;
91 struct dirent *dent;
92 int ret = -1;
93 DIR *dir;
94 int nr = 0;
95
46e201ef
AH
96 /*
97 * Directory containing a single regular perf data file which is already
98 * open, means there is nothing more to do here.
99 */
100 if (perf_data__is_single_file(data))
101 return 0;
102
ec65def1
JO
103 if (WARN_ON(!data->is_dir))
104 return -EINVAL;
105
258031c0
JO
106 /* The version is provided by DIR_FORMAT feature. */
107 if (WARN_ON(data->dir.version != PERF_DIR_VERSION))
108 return -1;
109
eb617670
JO
110 dir = opendir(data->path);
111 if (!dir)
112 return -EINVAL;
113
114 while ((dent = readdir(dir)) != NULL) {
115 struct perf_data_file *file;
116 char path[PATH_MAX];
117 struct stat st;
118
119 snprintf(path, sizeof(path), "%s/%s", data->path, dent->d_name);
120 if (stat(path, &st))
121 continue;
122
490e6db0 123 if (!S_ISREG(st.st_mode) || strncmp(dent->d_name, "data.", 5))
eb617670
JO
124 continue;
125
126 ret = -ENOMEM;
127
128 file = realloc(files, (nr + 1) * sizeof(*files));
129 if (!file)
130 goto out_err;
131
132 files = file;
133 file = &files[nr++];
134
135 file->path = strdup(path);
136 if (!file->path)
137 goto out_err;
138
139 ret = open(file->path, O_RDONLY);
140 if (ret < 0)
141 goto out_err;
142
143 file->fd = ret;
144 file->size = st.st_size;
145 }
146
0a6564eb 147 closedir(dir);
eb617670
JO
148 if (!files)
149 return -EINVAL;
150
151 data->dir.files = files;
152 data->dir.nr = nr;
153 return 0;
154
155out_err:
0a6564eb 156 closedir(dir);
eb617670
JO
157 close_dir(files, nr);
158 return ret;
159}
160
e8be1357
JO
161int perf_data__update_dir(struct perf_data *data)
162{
163 int i;
164
165 if (WARN_ON(!data->is_dir))
166 return -EINVAL;
167
168 for (i = 0; i < data->dir.nr; i++) {
169 struct perf_data_file *file = &data->dir.files[i];
170 struct stat st;
171
172 if (fstat(file->fd, &st))
173 return -1;
174
175 file->size = st.st_size;
176 }
177
178 return 0;
179}
180
8ceb41d7 181static bool check_pipe(struct perf_data *data)
6a4d98d7
JO
182{
183 struct stat st;
184 bool is_pipe = false;
8ceb41d7 185 int fd = perf_data__is_read(data) ?
6a4d98d7
JO
186 STDIN_FILENO : STDOUT_FILENO;
187
2d4f2799 188 if (!data->path) {
6a4d98d7
JO
189 if (!fstat(fd, &st) && S_ISFIFO(st.st_mode))
190 is_pipe = true;
191 } else {
2d4f2799 192 if (!strcmp(data->path, "-"))
6a4d98d7
JO
193 is_pipe = true;
194 }
195
60136667
NK
196 if (is_pipe) {
197 if (data->use_stdio) {
198 const char *mode;
199
200 mode = perf_data__is_read(data) ? "r" : "w";
201 data->file.fptr = fdopen(fd, mode);
202
203 if (data->file.fptr == NULL) {
204 data->file.fd = fd;
205 data->use_stdio = false;
206 }
207 } else {
208 data->file.fd = fd;
209 }
210 }
6a4d98d7 211
8ceb41d7 212 return data->is_pipe = is_pipe;
6a4d98d7
JO
213}
214
8ceb41d7 215static int check_backup(struct perf_data *data)
6a4d98d7
JO
216{
217 struct stat st;
218
5021fc4e
JO
219 if (perf_data__is_read(data))
220 return 0;
221
2d4f2799 222 if (!stat(data->path, &st) && st.st_size) {
6a4d98d7 223 char oldname[PATH_MAX];
ccb7a71d
JO
224 int ret;
225
6a4d98d7 226 snprintf(oldname, sizeof(oldname), "%s.old",
2d4f2799 227 data->path);
ccb7a71d
JO
228
229 ret = rm_rf_perf_data(oldname);
230 if (ret) {
231 pr_err("Can't remove old data: %s (%s)\n",
232 ret == -2 ?
233 "Unknown file found" : strerror(errno),
234 oldname);
235 return -1;
236 }
237
238 if (rename(data->path, oldname)) {
239 pr_err("Can't move data: %s (%s to %s)\n",
240 strerror(errno),
241 data->path, oldname);
242 return -1;
243 }
6a4d98d7
JO
244 }
245
246 return 0;
247}
248
ec65def1
JO
249static bool is_dir(struct perf_data *data)
250{
251 struct stat st;
252
253 if (stat(data->path, &st))
254 return false;
255
256 return (st.st_mode & S_IFMT) == S_IFDIR;
257}
258
8ceb41d7 259static int open_file_read(struct perf_data *data)
6a4d98d7 260{
2a525f6a 261 int flags = data->in_place_update ? O_RDWR : O_RDONLY;
6a4d98d7
JO
262 struct stat st;
263 int fd;
6e81c74c 264 char sbuf[STRERR_BUFSIZE];
6a4d98d7 265
2a525f6a 266 fd = open(data->file.path, flags);
6a4d98d7
JO
267 if (fd < 0) {
268 int err = errno;
269
eae8ad80 270 pr_err("failed to open %s: %s", data->file.path,
c8b5f2c9 271 str_error_r(err, sbuf, sizeof(sbuf)));
eae8ad80 272 if (err == ENOENT && !strcmp(data->file.path, "perf.data"))
6a4d98d7
JO
273 pr_err(" (try 'perf record' first)");
274 pr_err("\n");
275 return -err;
276 }
277
278 if (fstat(fd, &st) < 0)
279 goto out_close;
280
8ceb41d7 281 if (!data->force && st.st_uid && (st.st_uid != geteuid())) {
071266bf 282 pr_err("File %s not owned by current user or root (use -f to override)\n",
eae8ad80 283 data->file.path);
6a4d98d7
JO
284 goto out_close;
285 }
286
287 if (!st.st_size) {
8ceb41d7 288 pr_info("zero-sized data (%s), nothing to do!\n",
eae8ad80 289 data->file.path);
6a4d98d7
JO
290 goto out_close;
291 }
292
45112e89 293 data->file.size = st.st_size;
6a4d98d7
JO
294 return fd;
295
296 out_close:
297 close(fd);
298 return -1;
299}
300
8ceb41d7 301static int open_file_write(struct perf_data *data)
6a4d98d7 302{
ffa91880 303 int fd;
6e81c74c 304 char sbuf[STRERR_BUFSIZE];
ffa91880 305
eae8ad80 306 fd = open(data->file.path, O_CREAT|O_RDWR|O_TRUNC|O_CLOEXEC,
cd6379eb 307 S_IRUSR|S_IWUSR);
ffa91880
AB
308
309 if (fd < 0)
eae8ad80 310 pr_err("failed to open %s : %s\n", data->file.path,
c8b5f2c9 311 str_error_r(errno, sbuf, sizeof(sbuf)));
ffa91880
AB
312
313 return fd;
6a4d98d7
JO
314}
315
8ceb41d7 316static int open_file(struct perf_data *data)
6a4d98d7
JO
317{
318 int fd;
319
8ceb41d7
JO
320 fd = perf_data__is_read(data) ?
321 open_file_read(data) : open_file_write(data);
6a4d98d7 322
2d4f2799 323 if (fd < 0) {
b8f7d86b 324 zfree(&data->file.path);
2d4f2799
JO
325 return -1;
326 }
327
eae8ad80 328 data->file.fd = fd;
2d4f2799
JO
329 return 0;
330}
331
332static int open_file_dup(struct perf_data *data)
333{
334 data->file.path = strdup(data->path);
335 if (!data->file.path)
336 return -ENOMEM;
337
338 return open_file(data);
6a4d98d7
JO
339}
340
ec65def1
JO
341static int open_dir(struct perf_data *data)
342{
343 int ret;
344
345 /*
346 * So far we open only the header, so we can read the data version and
347 * layout.
348 */
9b70b9db 349 if (asprintf(&data->file.path, "%s/data", data->path) < 0)
ec65def1
JO
350 return -1;
351
352 if (perf_data__is_write(data) &&
353 mkdir(data->path, S_IRWXU) < 0)
354 return -1;
355
356 ret = open_file(data);
357
358 /* Cleanup whatever we managed to create so far. */
359 if (ret && perf_data__is_write(data))
360 rm_rf_perf_data(data->path);
361
362 return ret;
363}
364
8ceb41d7 365int perf_data__open(struct perf_data *data)
6a4d98d7 366{
8ceb41d7 367 if (check_pipe(data))
6a4d98d7
JO
368 return 0;
369
60136667
NK
370 /* currently it allows stdio for pipe only */
371 data->use_stdio = false;
372
2d4f2799
JO
373 if (!data->path)
374 data->path = "perf.data";
6a4d98d7 375
5021fc4e
JO
376 if (check_backup(data))
377 return -1;
378
ec65def1
JO
379 if (perf_data__is_read(data))
380 data->is_dir = is_dir(data);
381
382 return perf_data__is_dir(data) ?
383 open_dir(data) : open_file_dup(data);
6a4d98d7
JO
384}
385
8ceb41d7 386void perf_data__close(struct perf_data *data)
6a4d98d7 387{
ec65def1
JO
388 if (perf_data__is_dir(data))
389 perf_data__close_dir(data);
390
b8f7d86b 391 zfree(&data->file.path);
60136667
NK
392
393 if (data->use_stdio)
394 fclose(data->file.fptr);
395 else
396 close(data->file.fd);
397}
398
399ssize_t perf_data__read(struct perf_data *data, void *buf, size_t size)
400{
401 if (data->use_stdio) {
402 if (fread(buf, size, 1, data->file.fptr) == 1)
403 return size;
404 return feof(data->file.fptr) ? 0 : -1;
405 }
406 return readn(data->file.fd, buf, size);
6a4d98d7 407}
6f9a317f 408
e268687b
JO
409ssize_t perf_data_file__write(struct perf_data_file *file,
410 void *buf, size_t size)
411{
412 return writen(file->fd, buf, size);
413}
414
8ceb41d7 415ssize_t perf_data__write(struct perf_data *data,
57c8f107 416 void *buf, size_t size)
6f9a317f 417{
60136667
NK
418 if (data->use_stdio) {
419 if (fwrite(buf, size, 1, data->file.fptr) == 1)
420 return size;
421 return -1;
422 }
e268687b 423 return perf_data_file__write(&data->file, buf, size);
6f9a317f 424}
040f9915 425
8ceb41d7 426int perf_data__switch(struct perf_data *data,
57c8f107
YJ
427 const char *postfix,
428 size_t pos, bool at_exit,
429 char **new_filepath)
040f9915 430{
040f9915
WN
431 int ret;
432
8ceb41d7 433 if (perf_data__is_read(data))
040f9915
WN
434 return -EINVAL;
435
03724b2e 436 if (asprintf(new_filepath, "%s.%s", data->path, postfix) < 0)
040f9915
WN
437 return -ENOMEM;
438
439 /*
440 * Only fire a warning, don't return error, continue fill
441 * original file.
442 */
03724b2e
AK
443 if (rename(data->path, *new_filepath))
444 pr_warning("Failed to rename %s to %s\n", data->path, *new_filepath);
040f9915
WN
445
446 if (!at_exit) {
eae8ad80 447 close(data->file.fd);
8ceb41d7 448 ret = perf_data__open(data);
040f9915
WN
449 if (ret < 0)
450 goto out;
451
eae8ad80 452 if (lseek(data->file.fd, pos, SEEK_SET) == (off_t)-1) {
040f9915
WN
453 ret = -errno;
454 pr_debug("Failed to lseek to %zu: %s",
455 pos, strerror(errno));
456 goto out;
457 }
458 }
eae8ad80 459 ret = data->file.fd;
040f9915 460out:
040f9915
WN
461 return ret;
462}
29583c17
JO
463
464unsigned long perf_data__size(struct perf_data *data)
465{
466 u64 size = data->file.size;
467 int i;
468
46e201ef 469 if (perf_data__is_single_file(data))
29583c17
JO
470 return size;
471
472 for (i = 0; i < data->dir.nr; i++) {
473 struct perf_data_file *file = &data->dir.files[i];
474
475 size += file->size;
476 }
477
478 return size;
479}
eeb399b5
AH
480
481int perf_data__make_kcore_dir(struct perf_data *data, char *buf, size_t buf_sz)
482{
483 int ret;
484
485 if (!data->is_dir)
486 return -1;
487
488 ret = snprintf(buf, buf_sz, "%s/kcore_dir", data->path);
489 if (ret < 0 || (size_t)ret >= buf_sz)
490 return -1;
491
492 return mkdir(buf, S_IRWXU);
493}
494
a4455e00
AH
495bool has_kcore_dir(const char *path)
496{
65691e9f
AH
497 struct dirent *d = ERR_PTR(-EINVAL);
498 const char *name = "kcore_dir";
499 DIR *dir = opendir(path);
500 size_t n = strlen(name);
501 bool result = false;
502
503 if (dir) {
504 while (d && !result) {
505 d = readdir(dir);
506 result = d ? strncmp(d->d_name, name, n) : false;
507 }
508 closedir(dir);
509 }
a4455e00 510
65691e9f 511 return result;
a4455e00
AH
512}
513
eeb399b5
AH
514char *perf_data__kallsyms_name(struct perf_data *data)
515{
516 char *kallsyms_name;
517 struct stat st;
518
519 if (!data->is_dir)
520 return NULL;
521
522 if (asprintf(&kallsyms_name, "%s/kcore_dir/kallsyms", data->path) < 0)
523 return NULL;
524
525 if (stat(kallsyms_name, &st)) {
526 free(kallsyms_name);
527 return NULL;
528 }
529
530 return kallsyms_name;
531}
058f1511 532
a5367ecb
AH
533char *perf_data__guest_kallsyms_name(struct perf_data *data, pid_t machine_pid)
534{
535 char *kallsyms_name;
536 struct stat st;
537
538 if (!data->is_dir)
539 return NULL;
540
541 if (asprintf(&kallsyms_name, "%s/kcore_dir__%d/kallsyms", data->path, machine_pid) < 0)
542 return NULL;
543
544 if (stat(kallsyms_name, &st)) {
545 free(kallsyms_name);
546 return NULL;
547 }
548
549 return kallsyms_name;
550}
551
058f1511
JO
552bool is_perf_data(const char *path)
553{
554 bool ret = false;
555 FILE *file;
556 u64 magic;
557
558 file = fopen(path, "r");
559 if (!file)
560 return false;
561
562 if (fread(&magic, 1, 8, file) < 8)
563 goto out;
564
565 ret = is_perf_magic(magic);
566out:
567 fclose(file);
568 return ret;
569}