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