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