Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
6a4d98d7 JO |
2 | #include <linux/compiler.h> |
3 | #include <linux/kernel.h> | |
8520a98d | 4 | #include <linux/string.h> |
7f7c536f | 5 | #include <linux/zalloc.h> |
6a4d98d7 JO |
6 | #include <sys/types.h> |
7 | #include <sys/stat.h> | |
a43783ae | 8 | #include <errno.h> |
c23c2a0f | 9 | #include <fcntl.h> |
6a4d98d7 JO |
10 | #include <unistd.h> |
11 | #include <string.h> | |
14552063 | 12 | #include <asm/bug.h> |
eb617670 | 13 | #include <dirent.h> |
6a4d98d7 JO |
14 | |
15 | #include "data.h" | |
fb71c86c | 16 | #include "util.h" // rm_rf_perf_data() |
84f5d36f | 17 | #include "debug.h" |
258031c0 | 18 | #include "header.h" |
fb71c86c | 19 | #include <internal/lib.h> |
6a4d98d7 | 20 | |
14552063 JO |
21 | static void close_dir(struct perf_data_file *files, int nr) |
22 | { | |
23 | while (--nr >= 1) { | |
24 | close(files[nr].fd); | |
d8f9da24 | 25 | zfree(&files[nr].path); |
14552063 JO |
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; | |
f2211881 | 38 | int i, ret; |
14552063 | 39 | |
ec65def1 JO |
40 | if (WARN_ON(!data->is_dir)) |
41 | return -EINVAL; | |
42 | ||
14552063 JO |
43 | files = zalloc(nr * sizeof(*files)); |
44 | if (!files) | |
45 | return -ENOMEM; | |
46 | ||
258031c0 JO |
47 | data->dir.version = PERF_DIR_VERSION; |
48 | data->dir.files = files; | |
49 | data->dir.nr = nr; | |
14552063 JO |
50 | |
51 | for (i = 0; i < nr; i++) { | |
52 | struct perf_data_file *file = &files[i]; | |
53 | ||
f2211881 ZL |
54 | ret = asprintf(&file->path, "%s/data.%d", data->path, i); |
55 | if (ret < 0) | |
14552063 JO |
56 | goto out_err; |
57 | ||
58 | ret = open(file->path, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR); | |
59 | if (ret < 0) | |
60 | goto out_err; | |
61 | ||
62 | file->fd = ret; | |
63 | } | |
64 | ||
65 | return 0; | |
66 | ||
67 | out_err: | |
68 | close_dir(files, i); | |
69 | return ret; | |
70 | } | |
71 | ||
eb617670 JO |
72 | int perf_data__open_dir(struct perf_data *data) |
73 | { | |
74 | struct perf_data_file *files = NULL; | |
75 | struct dirent *dent; | |
76 | int ret = -1; | |
77 | DIR *dir; | |
78 | int nr = 0; | |
79 | ||
46e201ef AH |
80 | /* |
81 | * Directory containing a single regular perf data file which is already | |
82 | * open, means there is nothing more to do here. | |
83 | */ | |
84 | if (perf_data__is_single_file(data)) | |
85 | return 0; | |
86 | ||
ec65def1 JO |
87 | if (WARN_ON(!data->is_dir)) |
88 | return -EINVAL; | |
89 | ||
258031c0 JO |
90 | /* The version is provided by DIR_FORMAT feature. */ |
91 | if (WARN_ON(data->dir.version != PERF_DIR_VERSION)) | |
92 | return -1; | |
93 | ||
eb617670 JO |
94 | dir = opendir(data->path); |
95 | if (!dir) | |
96 | return -EINVAL; | |
97 | ||
98 | while ((dent = readdir(dir)) != NULL) { | |
99 | struct perf_data_file *file; | |
100 | char path[PATH_MAX]; | |
101 | struct stat st; | |
102 | ||
103 | snprintf(path, sizeof(path), "%s/%s", data->path, dent->d_name); | |
104 | if (stat(path, &st)) | |
105 | continue; | |
106 | ||
490e6db0 | 107 | if (!S_ISREG(st.st_mode) || strncmp(dent->d_name, "data.", 5)) |
eb617670 JO |
108 | continue; |
109 | ||
110 | ret = -ENOMEM; | |
111 | ||
112 | file = realloc(files, (nr + 1) * sizeof(*files)); | |
113 | if (!file) | |
114 | goto out_err; | |
115 | ||
116 | files = file; | |
117 | file = &files[nr++]; | |
118 | ||
119 | file->path = strdup(path); | |
120 | if (!file->path) | |
121 | goto out_err; | |
122 | ||
123 | ret = open(file->path, O_RDONLY); | |
124 | if (ret < 0) | |
125 | goto out_err; | |
126 | ||
127 | file->fd = ret; | |
128 | file->size = st.st_size; | |
129 | } | |
130 | ||
131 | if (!files) | |
132 | return -EINVAL; | |
133 | ||
134 | data->dir.files = files; | |
135 | data->dir.nr = nr; | |
136 | return 0; | |
137 | ||
138 | out_err: | |
139 | close_dir(files, nr); | |
140 | return ret; | |
141 | } | |
142 | ||
e8be1357 JO |
143 | int perf_data__update_dir(struct perf_data *data) |
144 | { | |
145 | int i; | |
146 | ||
147 | if (WARN_ON(!data->is_dir)) | |
148 | return -EINVAL; | |
149 | ||
150 | for (i = 0; i < data->dir.nr; i++) { | |
151 | struct perf_data_file *file = &data->dir.files[i]; | |
152 | struct stat st; | |
153 | ||
154 | if (fstat(file->fd, &st)) | |
155 | return -1; | |
156 | ||
157 | file->size = st.st_size; | |
158 | } | |
159 | ||
160 | return 0; | |
161 | } | |
162 | ||
8ceb41d7 | 163 | static bool check_pipe(struct perf_data *data) |
6a4d98d7 JO |
164 | { |
165 | struct stat st; | |
166 | bool is_pipe = false; | |
8ceb41d7 | 167 | int fd = perf_data__is_read(data) ? |
6a4d98d7 JO |
168 | STDIN_FILENO : STDOUT_FILENO; |
169 | ||
2d4f2799 | 170 | if (!data->path) { |
6a4d98d7 JO |
171 | if (!fstat(fd, &st) && S_ISFIFO(st.st_mode)) |
172 | is_pipe = true; | |
173 | } else { | |
2d4f2799 | 174 | if (!strcmp(data->path, "-")) |
6a4d98d7 JO |
175 | is_pipe = true; |
176 | } | |
177 | ||
60136667 NK |
178 | if (is_pipe) { |
179 | if (data->use_stdio) { | |
180 | const char *mode; | |
181 | ||
182 | mode = perf_data__is_read(data) ? "r" : "w"; | |
183 | data->file.fptr = fdopen(fd, mode); | |
184 | ||
185 | if (data->file.fptr == NULL) { | |
186 | data->file.fd = fd; | |
187 | data->use_stdio = false; | |
188 | } | |
189 | } else { | |
190 | data->file.fd = fd; | |
191 | } | |
192 | } | |
6a4d98d7 | 193 | |
8ceb41d7 | 194 | return data->is_pipe = is_pipe; |
6a4d98d7 JO |
195 | } |
196 | ||
8ceb41d7 | 197 | static int check_backup(struct perf_data *data) |
6a4d98d7 JO |
198 | { |
199 | struct stat st; | |
200 | ||
5021fc4e JO |
201 | if (perf_data__is_read(data)) |
202 | return 0; | |
203 | ||
2d4f2799 | 204 | if (!stat(data->path, &st) && st.st_size) { |
6a4d98d7 | 205 | char oldname[PATH_MAX]; |
ccb7a71d JO |
206 | int ret; |
207 | ||
6a4d98d7 | 208 | snprintf(oldname, sizeof(oldname), "%s.old", |
2d4f2799 | 209 | data->path); |
ccb7a71d JO |
210 | |
211 | ret = rm_rf_perf_data(oldname); | |
212 | if (ret) { | |
213 | pr_err("Can't remove old data: %s (%s)\n", | |
214 | ret == -2 ? | |
215 | "Unknown file found" : strerror(errno), | |
216 | oldname); | |
217 | return -1; | |
218 | } | |
219 | ||
220 | if (rename(data->path, oldname)) { | |
221 | pr_err("Can't move data: %s (%s to %s)\n", | |
222 | strerror(errno), | |
223 | data->path, oldname); | |
224 | return -1; | |
225 | } | |
6a4d98d7 JO |
226 | } |
227 | ||
228 | return 0; | |
229 | } | |
230 | ||
ec65def1 JO |
231 | static bool is_dir(struct perf_data *data) |
232 | { | |
233 | struct stat st; | |
234 | ||
235 | if (stat(data->path, &st)) | |
236 | return false; | |
237 | ||
238 | return (st.st_mode & S_IFMT) == S_IFDIR; | |
239 | } | |
240 | ||
8ceb41d7 | 241 | static int open_file_read(struct perf_data *data) |
6a4d98d7 JO |
242 | { |
243 | struct stat st; | |
244 | int fd; | |
6e81c74c | 245 | char sbuf[STRERR_BUFSIZE]; |
6a4d98d7 | 246 | |
eae8ad80 | 247 | fd = open(data->file.path, O_RDONLY); |
6a4d98d7 JO |
248 | if (fd < 0) { |
249 | int err = errno; | |
250 | ||
eae8ad80 | 251 | pr_err("failed to open %s: %s", data->file.path, |
c8b5f2c9 | 252 | str_error_r(err, sbuf, sizeof(sbuf))); |
eae8ad80 | 253 | if (err == ENOENT && !strcmp(data->file.path, "perf.data")) |
6a4d98d7 JO |
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 | ||
8ceb41d7 | 262 | if (!data->force && st.st_uid && (st.st_uid != geteuid())) { |
071266bf | 263 | pr_err("File %s not owned by current user or root (use -f to override)\n", |
eae8ad80 | 264 | data->file.path); |
6a4d98d7 JO |
265 | goto out_close; |
266 | } | |
267 | ||
268 | if (!st.st_size) { | |
8ceb41d7 | 269 | pr_info("zero-sized data (%s), nothing to do!\n", |
eae8ad80 | 270 | data->file.path); |
6a4d98d7 JO |
271 | goto out_close; |
272 | } | |
273 | ||
45112e89 | 274 | data->file.size = st.st_size; |
6a4d98d7 JO |
275 | return fd; |
276 | ||
277 | out_close: | |
278 | close(fd); | |
279 | return -1; | |
280 | } | |
281 | ||
8ceb41d7 | 282 | static int open_file_write(struct perf_data *data) |
6a4d98d7 | 283 | { |
ffa91880 | 284 | int fd; |
6e81c74c | 285 | char sbuf[STRERR_BUFSIZE]; |
ffa91880 | 286 | |
eae8ad80 | 287 | fd = open(data->file.path, O_CREAT|O_RDWR|O_TRUNC|O_CLOEXEC, |
cd6379eb | 288 | S_IRUSR|S_IWUSR); |
ffa91880 AB |
289 | |
290 | if (fd < 0) | |
eae8ad80 | 291 | pr_err("failed to open %s : %s\n", data->file.path, |
c8b5f2c9 | 292 | str_error_r(errno, sbuf, sizeof(sbuf))); |
ffa91880 AB |
293 | |
294 | return fd; | |
6a4d98d7 JO |
295 | } |
296 | ||
8ceb41d7 | 297 | static int open_file(struct perf_data *data) |
6a4d98d7 JO |
298 | { |
299 | int fd; | |
300 | ||
8ceb41d7 JO |
301 | fd = perf_data__is_read(data) ? |
302 | open_file_read(data) : open_file_write(data); | |
6a4d98d7 | 303 | |
2d4f2799 | 304 | if (fd < 0) { |
b8f7d86b | 305 | zfree(&data->file.path); |
2d4f2799 JO |
306 | return -1; |
307 | } | |
308 | ||
eae8ad80 | 309 | data->file.fd = fd; |
2d4f2799 JO |
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); | |
6a4d98d7 JO |
320 | } |
321 | ||
ec65def1 JO |
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 | */ | |
9b70b9db | 330 | if (asprintf(&data->file.path, "%s/data", data->path) < 0) |
ec65def1 JO |
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 | ||
8ceb41d7 | 346 | int perf_data__open(struct perf_data *data) |
6a4d98d7 | 347 | { |
8ceb41d7 | 348 | if (check_pipe(data)) |
6a4d98d7 JO |
349 | return 0; |
350 | ||
60136667 NK |
351 | /* currently it allows stdio for pipe only */ |
352 | data->use_stdio = false; | |
353 | ||
2d4f2799 JO |
354 | if (!data->path) |
355 | data->path = "perf.data"; | |
6a4d98d7 | 356 | |
5021fc4e JO |
357 | if (check_backup(data)) |
358 | return -1; | |
359 | ||
ec65def1 JO |
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); | |
6a4d98d7 JO |
365 | } |
366 | ||
8ceb41d7 | 367 | void perf_data__close(struct perf_data *data) |
6a4d98d7 | 368 | { |
ec65def1 JO |
369 | if (perf_data__is_dir(data)) |
370 | perf_data__close_dir(data); | |
371 | ||
b8f7d86b | 372 | zfree(&data->file.path); |
60136667 NK |
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); | |
6a4d98d7 | 388 | } |
6f9a317f | 389 | |
e268687b JO |
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 | ||
8ceb41d7 | 396 | ssize_t perf_data__write(struct perf_data *data, |
6f9a317f JO |
397 | void *buf, size_t size) |
398 | { | |
60136667 NK |
399 | if (data->use_stdio) { |
400 | if (fwrite(buf, size, 1, data->file.fptr) == 1) | |
401 | return size; | |
402 | return -1; | |
403 | } | |
e268687b | 404 | return perf_data_file__write(&data->file, buf, size); |
6f9a317f | 405 | } |
040f9915 | 406 | |
8ceb41d7 | 407 | int perf_data__switch(struct perf_data *data, |
040f9915 | 408 | const char *postfix, |
03724b2e AK |
409 | size_t pos, bool at_exit, |
410 | char **new_filepath) | |
040f9915 | 411 | { |
040f9915 WN |
412 | int ret; |
413 | ||
8ceb41d7 | 414 | if (check_pipe(data)) |
040f9915 | 415 | return -EINVAL; |
8ceb41d7 | 416 | if (perf_data__is_read(data)) |
040f9915 WN |
417 | return -EINVAL; |
418 | ||
03724b2e | 419 | if (asprintf(new_filepath, "%s.%s", data->path, postfix) < 0) |
040f9915 WN |
420 | return -ENOMEM; |
421 | ||
422 | /* | |
423 | * Only fire a warning, don't return error, continue fill | |
424 | * original file. | |
425 | */ | |
03724b2e AK |
426 | if (rename(data->path, *new_filepath)) |
427 | pr_warning("Failed to rename %s to %s\n", data->path, *new_filepath); | |
040f9915 WN |
428 | |
429 | if (!at_exit) { | |
eae8ad80 | 430 | close(data->file.fd); |
8ceb41d7 | 431 | ret = perf_data__open(data); |
040f9915 WN |
432 | if (ret < 0) |
433 | goto out; | |
434 | ||
eae8ad80 | 435 | if (lseek(data->file.fd, pos, SEEK_SET) == (off_t)-1) { |
040f9915 WN |
436 | ret = -errno; |
437 | pr_debug("Failed to lseek to %zu: %s", | |
438 | pos, strerror(errno)); | |
439 | goto out; | |
440 | } | |
441 | } | |
eae8ad80 | 442 | ret = data->file.fd; |
040f9915 | 443 | out: |
040f9915 WN |
444 | return ret; |
445 | } | |
29583c17 JO |
446 | |
447 | unsigned long perf_data__size(struct perf_data *data) | |
448 | { | |
449 | u64 size = data->file.size; | |
450 | int i; | |
451 | ||
46e201ef | 452 | if (perf_data__is_single_file(data)) |
29583c17 JO |
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 | } | |
eeb399b5 AH |
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 | } | |
058f1511 JO |
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 | } |