Linux Audio

Check our new training course

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