data.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. #include "data.h"
  15. #include "util.h" // rm_rf_perf_data()
  16. #include "debug.h"
  17. #include "header.h"
  18. #include <internal/lib.h>
  19. static void close_dir(struct perf_data_file *files, int nr)
  20. {
  21. while (--nr >= 0) {
  22. close(files[nr].fd);
  23. zfree(&files[nr].path);
  24. }
  25. free(files);
  26. }
  27. void perf_data__close_dir(struct perf_data *data)
  28. {
  29. close_dir(data->dir.files, data->dir.nr);
  30. }
  31. int perf_data__create_dir(struct perf_data *data, int nr)
  32. {
  33. struct perf_data_file *files = NULL;
  34. int i, ret;
  35. if (WARN_ON(!data->is_dir))
  36. return -EINVAL;
  37. files = zalloc(nr * sizeof(*files));
  38. if (!files)
  39. return -ENOMEM;
  40. for (i = 0; i < nr; i++) {
  41. struct perf_data_file *file = &files[i];
  42. ret = asprintf(&file->path, "%s/data.%d", data->path, i);
  43. if (ret < 0)
  44. goto out_err;
  45. ret = open(file->path, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
  46. if (ret < 0)
  47. goto out_err;
  48. file->fd = ret;
  49. }
  50. data->dir.version = PERF_DIR_VERSION;
  51. data->dir.files = files;
  52. data->dir.nr = nr;
  53. return 0;
  54. out_err:
  55. close_dir(files, i);
  56. return ret;
  57. }
  58. int perf_data__open_dir(struct perf_data *data)
  59. {
  60. struct perf_data_file *files = NULL;
  61. struct dirent *dent;
  62. int ret = -1;
  63. DIR *dir;
  64. int nr = 0;
  65. /*
  66. * Directory containing a single regular perf data file which is already
  67. * open, means there is nothing more to do here.
  68. */
  69. if (perf_data__is_single_file(data))
  70. return 0;
  71. if (WARN_ON(!data->is_dir))
  72. return -EINVAL;
  73. /* The version is provided by DIR_FORMAT feature. */
  74. if (WARN_ON(data->dir.version != PERF_DIR_VERSION))
  75. return -1;
  76. dir = opendir(data->path);
  77. if (!dir)
  78. return -EINVAL;
  79. while ((dent = readdir(dir)) != NULL) {
  80. struct perf_data_file *file;
  81. char path[PATH_MAX];
  82. struct stat st;
  83. snprintf(path, sizeof(path), "%s/%s", data->path, dent->d_name);
  84. if (stat(path, &st))
  85. continue;
  86. if (!S_ISREG(st.st_mode) || strncmp(dent->d_name, "data.", 5))
  87. continue;
  88. ret = -ENOMEM;
  89. file = realloc(files, (nr + 1) * sizeof(*files));
  90. if (!file)
  91. goto out_err;
  92. files = file;
  93. file = &files[nr++];
  94. file->path = strdup(path);
  95. if (!file->path)
  96. goto out_err;
  97. ret = open(file->path, O_RDONLY);
  98. if (ret < 0)
  99. goto out_err;
  100. file->fd = ret;
  101. file->size = st.st_size;
  102. }
  103. if (!files)
  104. return -EINVAL;
  105. data->dir.files = files;
  106. data->dir.nr = nr;
  107. return 0;
  108. out_err:
  109. close_dir(files, nr);
  110. return ret;
  111. }
  112. int perf_data__update_dir(struct perf_data *data)
  113. {
  114. int i;
  115. if (WARN_ON(!data->is_dir))
  116. return -EINVAL;
  117. for (i = 0; i < data->dir.nr; i++) {
  118. struct perf_data_file *file = &data->dir.files[i];
  119. struct stat st;
  120. if (fstat(file->fd, &st))
  121. return -1;
  122. file->size = st.st_size;
  123. }
  124. return 0;
  125. }
  126. static bool check_pipe(struct perf_data *data)
  127. {
  128. struct stat st;
  129. bool is_pipe = false;
  130. int fd = perf_data__is_read(data) ?
  131. STDIN_FILENO : STDOUT_FILENO;
  132. if (!data->path) {
  133. if (!fstat(fd, &st) && S_ISFIFO(st.st_mode))
  134. is_pipe = true;
  135. } else {
  136. if (!strcmp(data->path, "-"))
  137. is_pipe = true;
  138. }
  139. if (is_pipe)
  140. data->file.fd = fd;
  141. return data->is_pipe = is_pipe;
  142. }
  143. static int check_backup(struct perf_data *data)
  144. {
  145. struct stat st;
  146. if (perf_data__is_read(data))
  147. return 0;
  148. if (!stat(data->path, &st) && st.st_size) {
  149. char oldname[PATH_MAX];
  150. int ret;
  151. snprintf(oldname, sizeof(oldname), "%s.old",
  152. data->path);
  153. ret = rm_rf_perf_data(oldname);
  154. if (ret) {
  155. pr_err("Can't remove old data: %s (%s)\n",
  156. ret == -2 ?
  157. "Unknown file found" : strerror(errno),
  158. oldname);
  159. return -1;
  160. }
  161. if (rename(data->path, oldname)) {
  162. pr_err("Can't move data: %s (%s to %s)\n",
  163. strerror(errno),
  164. data->path, oldname);
  165. return -1;
  166. }
  167. }
  168. return 0;
  169. }
  170. static bool is_dir(struct perf_data *data)
  171. {
  172. struct stat st;
  173. if (stat(data->path, &st))
  174. return false;
  175. return (st.st_mode & S_IFMT) == S_IFDIR;
  176. }
  177. static int open_file_read(struct perf_data *data)
  178. {
  179. struct stat st;
  180. int fd;
  181. char sbuf[STRERR_BUFSIZE];
  182. fd = open(data->file.path, O_RDONLY);
  183. if (fd < 0) {
  184. int err = errno;
  185. pr_err("failed to open %s: %s", data->file.path,
  186. str_error_r(err, sbuf, sizeof(sbuf)));
  187. if (err == ENOENT && !strcmp(data->file.path, "perf.data"))
  188. pr_err(" (try 'perf record' first)");
  189. pr_err("\n");
  190. return -err;
  191. }
  192. if (fstat(fd, &st) < 0)
  193. goto out_close;
  194. if (!data->force && st.st_uid && (st.st_uid != geteuid())) {
  195. pr_err("File %s not owned by current user or root (use -f to override)\n",
  196. data->file.path);
  197. goto out_close;
  198. }
  199. if (!st.st_size) {
  200. pr_info("zero-sized data (%s), nothing to do!\n",
  201. data->file.path);
  202. goto out_close;
  203. }
  204. data->file.size = st.st_size;
  205. return fd;
  206. out_close:
  207. close(fd);
  208. return -1;
  209. }
  210. static int open_file_write(struct perf_data *data)
  211. {
  212. int fd;
  213. char sbuf[STRERR_BUFSIZE];
  214. fd = open(data->file.path, O_CREAT|O_RDWR|O_TRUNC|O_CLOEXEC,
  215. S_IRUSR|S_IWUSR);
  216. if (fd < 0)
  217. pr_err("failed to open %s : %s\n", data->file.path,
  218. str_error_r(errno, sbuf, sizeof(sbuf)));
  219. return fd;
  220. }
  221. static int open_file(struct perf_data *data)
  222. {
  223. int fd;
  224. fd = perf_data__is_read(data) ?
  225. open_file_read(data) : open_file_write(data);
  226. if (fd < 0) {
  227. zfree(&data->file.path);
  228. return -1;
  229. }
  230. data->file.fd = fd;
  231. return 0;
  232. }
  233. static int open_file_dup(struct perf_data *data)
  234. {
  235. data->file.path = strdup(data->path);
  236. if (!data->file.path)
  237. return -ENOMEM;
  238. return open_file(data);
  239. }
  240. static int open_dir(struct perf_data *data)
  241. {
  242. int ret;
  243. /*
  244. * So far we open only the header, so we can read the data version and
  245. * layout.
  246. */
  247. if (asprintf(&data->file.path, "%s/data", data->path) < 0)
  248. return -1;
  249. if (perf_data__is_write(data) &&
  250. mkdir(data->path, S_IRWXU) < 0)
  251. return -1;
  252. ret = open_file(data);
  253. /* Cleanup whatever we managed to create so far. */
  254. if (ret && perf_data__is_write(data))
  255. rm_rf_perf_data(data->path);
  256. return ret;
  257. }
  258. int perf_data__open(struct perf_data *data)
  259. {
  260. if (check_pipe(data))
  261. return 0;
  262. if (!data->path)
  263. data->path = "perf.data";
  264. if (check_backup(data))
  265. return -1;
  266. if (perf_data__is_read(data))
  267. data->is_dir = is_dir(data);
  268. return perf_data__is_dir(data) ?
  269. open_dir(data) : open_file_dup(data);
  270. }
  271. void perf_data__close(struct perf_data *data)
  272. {
  273. if (perf_data__is_dir(data))
  274. perf_data__close_dir(data);
  275. zfree(&data->file.path);
  276. close(data->file.fd);
  277. }
  278. ssize_t perf_data_file__write(struct perf_data_file *file,
  279. void *buf, size_t size)
  280. {
  281. return writen(file->fd, buf, size);
  282. }
  283. ssize_t perf_data__write(struct perf_data *data,
  284. void *buf, size_t size)
  285. {
  286. return perf_data_file__write(&data->file, buf, size);
  287. }
  288. int perf_data__switch(struct perf_data *data,
  289. const char *postfix,
  290. size_t pos, bool at_exit,
  291. char **new_filepath)
  292. {
  293. int ret;
  294. if (check_pipe(data))
  295. return -EINVAL;
  296. if (perf_data__is_read(data))
  297. return -EINVAL;
  298. if (asprintf(new_filepath, "%s.%s", data->path, postfix) < 0)
  299. return -ENOMEM;
  300. /*
  301. * Only fire a warning, don't return error, continue fill
  302. * original file.
  303. */
  304. if (rename(data->path, *new_filepath))
  305. pr_warning("Failed to rename %s to %s\n", data->path, *new_filepath);
  306. if (!at_exit) {
  307. close(data->file.fd);
  308. ret = perf_data__open(data);
  309. if (ret < 0)
  310. goto out;
  311. if (lseek(data->file.fd, pos, SEEK_SET) == (off_t)-1) {
  312. ret = -errno;
  313. pr_debug("Failed to lseek to %zu: %s",
  314. pos, strerror(errno));
  315. goto out;
  316. }
  317. }
  318. ret = data->file.fd;
  319. out:
  320. return ret;
  321. }
  322. unsigned long perf_data__size(struct perf_data *data)
  323. {
  324. u64 size = data->file.size;
  325. int i;
  326. if (perf_data__is_single_file(data))
  327. return size;
  328. for (i = 0; i < data->dir.nr; i++) {
  329. struct perf_data_file *file = &data->dir.files[i];
  330. size += file->size;
  331. }
  332. return size;
  333. }
  334. int perf_data__make_kcore_dir(struct perf_data *data, char *buf, size_t buf_sz)
  335. {
  336. int ret;
  337. if (!data->is_dir)
  338. return -1;
  339. ret = snprintf(buf, buf_sz, "%s/kcore_dir", data->path);
  340. if (ret < 0 || (size_t)ret >= buf_sz)
  341. return -1;
  342. return mkdir(buf, S_IRWXU);
  343. }
  344. char *perf_data__kallsyms_name(struct perf_data *data)
  345. {
  346. char *kallsyms_name;
  347. struct stat st;
  348. if (!data->is_dir)
  349. return NULL;
  350. if (asprintf(&kallsyms_name, "%s/kcore_dir/kallsyms", data->path) < 0)
  351. return NULL;
  352. if (stat(kallsyms_name, &st)) {
  353. free(kallsyms_name);
  354. return NULL;
  355. }
  356. return kallsyms_name;
  357. }