Linux Audio

Check our new training course

In-person Linux kernel drivers training

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