xref: /linux/tools/perf/util/data.c (revision 80b549be27de0f11124c66eaeb5307c7b4582edd)
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) &&
349 	    mkdir(data->path, S_IRWXU) < 0)
350 		return -1;
351 
352 	ret = open_file(data);
353 
354 	/* Cleanup whatever we managed to create so far. */
355 	if (ret && perf_data__is_write(data))
356 		rm_rf_perf_data(data->path);
357 
358 	return ret;
359 }
360 
361 int perf_data__open(struct perf_data *data)
362 {
363 	if (check_pipe(data))
364 		return 0;
365 
366 	/* currently it allows stdio for pipe only */
367 	data->file.use_stdio = false;
368 
369 	if (!data->path)
370 		data->path = "perf.data";
371 
372 	if (check_backup(data))
373 		return -1;
374 
375 	if (perf_data__is_read(data))
376 		data->is_dir = is_dir(data);
377 
378 	return perf_data__is_dir(data) ?
379 	       open_dir(data) : open_file_dup(data);
380 }
381 
382 void perf_data__close(struct perf_data *data)
383 {
384 	if (perf_data__is_dir(data))
385 		perf_data__close_dir(data);
386 
387 	perf_data_file__close(&data->file);
388 }
389 
390 static ssize_t perf_data_file__read(struct perf_data_file *file, void *buf, size_t size)
391 {
392 	if (file->use_stdio) {
393 		if (fread(buf, size, 1, file->fptr) == 1)
394 			return size;
395 		return feof(file->fptr) ? 0 : -1;
396 	}
397 	return readn(file->fd, buf, size);
398 }
399 
400 ssize_t perf_data__read(struct perf_data *data, void *buf, size_t size)
401 {
402 	return perf_data_file__read(&data->file, buf, size);
403 }
404 
405 ssize_t perf_data_file__write(struct perf_data_file *file,
406 			      void *buf, size_t size)
407 {
408 	if (file->use_stdio) {
409 		if (fwrite(buf, size, /*nmemb=*/1, file->fptr) == 1)
410 			return size;
411 		return -1;
412 	}
413 	return writen(file->fd, buf, size);
414 }
415 
416 ssize_t perf_data__write(struct perf_data *data,
417 			 void *buf, size_t size)
418 {
419 	return perf_data_file__write(&data->file, buf, size);
420 }
421 
422 off_t perf_data_file__seek(struct perf_data_file *file, off_t offset, int whence)
423 {
424 	if (file->use_stdio) {
425 		off_t res = fseeko(file->fptr, offset, whence);
426 
427 		return res < 0 ? -1 : ftello(file->fptr);
428 	}
429 	return lseek(file->fd, offset, whence);
430 }
431 
432 off_t perf_data__seek(struct perf_data *data, off_t offset, int whence)
433 {
434 	/* Note, a pipe fd will fail with -1 with errno of ESPIPE. */
435 	return perf_data_file__seek(&data->file, offset, whence);
436 }
437 
438 int perf_data__switch(struct perf_data *data,
439 		      const char *postfix,
440 		      size_t pos, bool at_exit,
441 		      char **new_filepath)
442 {
443 	int ret;
444 
445 	if (perf_data__is_read(data))
446 		return -EINVAL;
447 
448 	if (asprintf(new_filepath, "%s.%s", data->path, postfix) < 0)
449 		return -ENOMEM;
450 
451 	/*
452 	 * Only fire a warning, don't return error, continue fill
453 	 * original file.
454 	 */
455 	if (rename(data->path, *new_filepath))
456 		pr_warning("Failed to rename %s to %s\n", data->path, *new_filepath);
457 
458 	if (!at_exit) {
459 		perf_data_file__close(&data->file);
460 		ret = perf_data__open(data);
461 		if (ret < 0)
462 			goto out;
463 
464 		if (perf_data__seek(data, pos, SEEK_SET) == (off_t)-1) {
465 			ret = -errno;
466 			pr_debug("Failed to seek to %zu: %m", pos);
467 			goto out;
468 		}
469 	}
470 	ret = perf_data__fd(data);
471 out:
472 	return ret;
473 }
474 
475 unsigned long perf_data__size(struct perf_data *data)
476 {
477 	u64 size = data->file.size;
478 	int i;
479 
480 	if (perf_data__is_single_file(data))
481 		return size;
482 
483 	for (i = 0; i < data->dir.nr; i++) {
484 		struct perf_data_file *file = &data->dir.files[i];
485 
486 		size += file->size;
487 	}
488 
489 	return size;
490 }
491 
492 int perf_data__make_kcore_dir(struct perf_data *data, char *buf, size_t buf_sz)
493 {
494 	int ret;
495 
496 	if (!data->is_dir)
497 		return -1;
498 
499 	ret = snprintf(buf, buf_sz, "%s/kcore_dir", data->path);
500 	if (ret < 0 || (size_t)ret >= buf_sz)
501 		return -1;
502 
503 	return mkdir(buf, S_IRWXU);
504 }
505 
506 bool has_kcore_dir(const char *path)
507 {
508 	struct dirent *d = ERR_PTR(-EINVAL);
509 	const char *name = "kcore_dir";
510 	DIR *dir = opendir(path);
511 	size_t n = strlen(name);
512 	bool result = false;
513 
514 	if (dir) {
515 		while (d && !result) {
516 			d = readdir(dir);
517 			result = d ? strncmp(d->d_name, name, n) : false;
518 		}
519 		closedir(dir);
520 	}
521 
522 	return result;
523 }
524 
525 char *perf_data__kallsyms_name(struct perf_data *data)
526 {
527 	char *kallsyms_name;
528 	struct stat st;
529 
530 	if (!data->is_dir)
531 		return NULL;
532 
533 	if (asprintf(&kallsyms_name, "%s/kcore_dir/kallsyms", data->path) < 0)
534 		return NULL;
535 
536 	if (stat(kallsyms_name, &st)) {
537 		free(kallsyms_name);
538 		return NULL;
539 	}
540 
541 	return kallsyms_name;
542 }
543 
544 char *perf_data__guest_kallsyms_name(struct perf_data *data, pid_t machine_pid)
545 {
546 	char *kallsyms_name;
547 	struct stat st;
548 
549 	if (!data->is_dir)
550 		return NULL;
551 
552 	if (asprintf(&kallsyms_name, "%s/kcore_dir__%d/kallsyms", data->path, machine_pid) < 0)
553 		return NULL;
554 
555 	if (stat(kallsyms_name, &st)) {
556 		free(kallsyms_name);
557 		return NULL;
558 	}
559 
560 	return kallsyms_name;
561 }
562 
563 bool is_perf_data(const char *path)
564 {
565 	bool ret = false;
566 	FILE *file;
567 	u64 magic;
568 
569 	file = fopen(path, "r");
570 	if (!file)
571 		return false;
572 
573 	if (fread(&magic, 1, 8, file) < 8)
574 		goto out;
575 
576 	ret = is_perf_magic(magic);
577 out:
578 	fclose(file);
579 	return ret;
580 }
581