1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __PERF_DATA_H 3 #define __PERF_DATA_H 4 5 #include <stdio.h> 6 #include <stdbool.h> 7 #include <unistd.h> 8 #include <linux/types.h> 9 10 enum perf_data_mode { 11 PERF_DATA_MODE_WRITE, 12 PERF_DATA_MODE_READ, 13 }; 14 15 enum perf_dir_version { 16 PERF_DIR_SINGLE_FILE = 0, 17 PERF_DIR_VERSION = 1, 18 }; 19 20 /** 21 * struct perf_data_file: A wrapper around a file used for perf.data reading or writing. Generally 22 * part of struct perf_data. 23 */ 24 struct perf_data_file { 25 /** 26 * @path: Path of file. Generally a copy of perf_data.path but for a 27 * directory it is the file within the directory. 28 */ 29 char *path; 30 union { 31 /** @fd: File descriptor for read/writes. Valid if use_stdio is false. */ 32 int fd; 33 /** 34 * @fptr: Stdio FILE. Valid if use_stdio is true, currently just 35 * pipes in perf inject. 36 */ 37 FILE *fptr; 38 }; 39 /** @size: Size of file when opened. */ 40 unsigned long size; 41 /** @use_stdio: Use buffered stdio operations. */ 42 bool use_stdio; 43 }; 44 45 /** 46 * struct perf_data: A wrapper around a file used for perf.data reading or writing. 47 */ 48 struct perf_data { 49 /** @path: Path to open and of the file. NULL implies 'perf.data' will be used. */ 50 const char *path; 51 /** @file: Underlying file to be used. */ 52 struct perf_data_file file; 53 /** @open: Has the file or directory been opened. */ 54 bool open; 55 /** @is_pipe: Underlying file is a pipe. */ 56 bool is_pipe; 57 /** @is_dir: Underlying file is a directory. */ 58 bool is_dir; 59 /** @force: Ignore opening a file creating created by a different user. */ 60 bool force; 61 /** @in_place_update: A file opened for reading but will be written to. */ 62 bool in_place_update; 63 /** @mode: Read or write mode. */ 64 enum perf_data_mode mode:8; 65 66 struct { 67 /** @version: perf_dir_version. */ 68 u64 version; 69 /** @files: perf data files for the directory. */ 70 struct perf_data_file *files; 71 /** @nr: Number of perf data files for the directory. */ 72 int nr; 73 } dir; 74 }; 75 76 static inline int perf_data_file__fd(struct perf_data_file *file) 77 { 78 return file->use_stdio ? fileno(file->fptr) : file->fd; 79 } 80 81 ssize_t perf_data_file__write(struct perf_data_file *file, 82 void *buf, size_t size); 83 off_t perf_data_file__seek(struct perf_data_file *file, off_t offset, int whence); 84 85 86 static inline bool perf_data__is_read(struct perf_data *data) 87 { 88 return data->mode == PERF_DATA_MODE_READ; 89 } 90 91 static inline bool perf_data__is_write(struct perf_data *data) 92 { 93 return data->mode == PERF_DATA_MODE_WRITE; 94 } 95 96 static inline int perf_data__is_pipe(struct perf_data *data) 97 { 98 return data->is_pipe; 99 } 100 101 static inline bool perf_data__is_dir(struct perf_data *data) 102 { 103 return data->is_dir; 104 } 105 106 static inline bool perf_data__is_single_file(struct perf_data *data) 107 { 108 return data->dir.version == PERF_DIR_SINGLE_FILE; 109 } 110 111 static inline int perf_data__fd(struct perf_data *data) 112 { 113 return perf_data_file__fd(&data->file); 114 } 115 116 int perf_data__open(struct perf_data *data); 117 void perf_data__close(struct perf_data *data); 118 ssize_t perf_data__read(struct perf_data *data, void *buf, size_t size); 119 ssize_t perf_data__write(struct perf_data *data, 120 void *buf, size_t size); 121 off_t perf_data__seek(struct perf_data *data, off_t offset, int whence); 122 /* 123 * If at_exit is set, only rename current perf.data to 124 * perf.data.<postfix>, continue write on original data. 125 * Set at_exit when flushing the last output. 126 * 127 * Return value is fd of new output. 128 */ 129 int perf_data__switch(struct perf_data *data, 130 const char *postfix, 131 size_t pos, bool at_exit, char **new_filepath); 132 133 int perf_data__create_dir(struct perf_data *data, int nr); 134 int perf_data__open_dir(struct perf_data *data); 135 void perf_data__close_dir(struct perf_data *data); 136 unsigned long perf_data__size(struct perf_data *data); 137 int perf_data__make_kcore_dir(struct perf_data *data, char *buf, size_t buf_sz); 138 char *perf_data__kallsyms_name(struct perf_data *data); 139 char *perf_data__guest_kallsyms_name(struct perf_data *data, pid_t machine_pid); 140 141 bool has_kcore_dir(const char *path); 142 bool is_perf_data(const char *path); 143 144 #endif /* __PERF_DATA_H */ 145