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