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