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