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