util.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "util.h"
  3. #include "debug.h"
  4. #include "event.h"
  5. #include <api/fs/fs.h>
  6. #include <sys/stat.h>
  7. #include <sys/utsname.h>
  8. #include <dirent.h>
  9. #include <fcntl.h>
  10. #include <inttypes.h>
  11. #include <signal.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <errno.h>
  16. #include <limits.h>
  17. #include <linux/capability.h>
  18. #include <linux/kernel.h>
  19. #include <linux/log2.h>
  20. #include <linux/time64.h>
  21. #include <unistd.h>
  22. #include "cap.h"
  23. #include "strlist.h"
  24. #include "string2.h"
  25. /*
  26. * XXX We need to find a better place for these things...
  27. */
  28. bool perf_singlethreaded = true;
  29. void perf_set_singlethreaded(void)
  30. {
  31. perf_singlethreaded = true;
  32. }
  33. void perf_set_multithreaded(void)
  34. {
  35. perf_singlethreaded = false;
  36. }
  37. int sysctl_perf_event_max_stack = PERF_MAX_STACK_DEPTH;
  38. int sysctl_perf_event_max_contexts_per_stack = PERF_MAX_CONTEXTS_PER_STACK;
  39. int sysctl__max_stack(void)
  40. {
  41. int value;
  42. if (sysctl__read_int("kernel/perf_event_max_stack", &value) == 0)
  43. sysctl_perf_event_max_stack = value;
  44. if (sysctl__read_int("kernel/perf_event_max_contexts_per_stack", &value) == 0)
  45. sysctl_perf_event_max_contexts_per_stack = value;
  46. return sysctl_perf_event_max_stack;
  47. }
  48. bool sysctl__nmi_watchdog_enabled(void)
  49. {
  50. static bool cached;
  51. static bool nmi_watchdog;
  52. int value;
  53. if (cached)
  54. return nmi_watchdog;
  55. if (sysctl__read_int("kernel/nmi_watchdog", &value) < 0)
  56. return false;
  57. nmi_watchdog = (value > 0) ? true : false;
  58. cached = true;
  59. return nmi_watchdog;
  60. }
  61. bool test_attr__enabled;
  62. bool perf_host = true;
  63. bool perf_guest = false;
  64. void event_attr_init(struct perf_event_attr *attr)
  65. {
  66. if (!perf_host)
  67. attr->exclude_host = 1;
  68. if (!perf_guest)
  69. attr->exclude_guest = 1;
  70. /* to capture ABI version */
  71. attr->size = sizeof(*attr);
  72. }
  73. int mkdir_p(char *path, mode_t mode)
  74. {
  75. struct stat st;
  76. int err;
  77. char *d = path;
  78. if (*d != '/')
  79. return -1;
  80. if (stat(path, &st) == 0)
  81. return 0;
  82. while (*++d == '/');
  83. while ((d = strchr(d, '/'))) {
  84. *d = '\0';
  85. err = stat(path, &st) && mkdir(path, mode);
  86. *d++ = '/';
  87. if (err)
  88. return -1;
  89. while (*d == '/')
  90. ++d;
  91. }
  92. return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
  93. }
  94. static bool match_pat(char *file, const char **pat)
  95. {
  96. int i = 0;
  97. if (!pat)
  98. return true;
  99. while (pat[i]) {
  100. if (strglobmatch(file, pat[i]))
  101. return true;
  102. i++;
  103. }
  104. return false;
  105. }
  106. /*
  107. * The depth specify how deep the removal will go.
  108. * 0 - will remove only files under the 'path' directory
  109. * 1 .. x - will dive in x-level deep under the 'path' directory
  110. *
  111. * If specified the pat is array of string patterns ended with NULL,
  112. * which are checked upon every file/directory found. Only matching
  113. * ones are removed.
  114. *
  115. * The function returns:
  116. * 0 on success
  117. * -1 on removal failure with errno set
  118. * -2 on pattern failure
  119. */
  120. static int rm_rf_depth_pat(const char *path, int depth, const char **pat)
  121. {
  122. DIR *dir;
  123. int ret;
  124. struct dirent *d;
  125. char namebuf[PATH_MAX];
  126. struct stat statbuf;
  127. /* Do not fail if there's no file. */
  128. ret = lstat(path, &statbuf);
  129. if (ret)
  130. return 0;
  131. /* Try to remove any file we get. */
  132. if (!(statbuf.st_mode & S_IFDIR))
  133. return unlink(path);
  134. /* We have directory in path. */
  135. dir = opendir(path);
  136. if (dir == NULL)
  137. return -1;
  138. while ((d = readdir(dir)) != NULL && !ret) {
  139. if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
  140. continue;
  141. if (!match_pat(d->d_name, pat)) {
  142. ret = -2;
  143. break;
  144. }
  145. scnprintf(namebuf, sizeof(namebuf), "%s/%s",
  146. path, d->d_name);
  147. /* We have to check symbolic link itself */
  148. ret = lstat(namebuf, &statbuf);
  149. if (ret < 0) {
  150. pr_debug("stat failed: %s\n", namebuf);
  151. break;
  152. }
  153. if (S_ISDIR(statbuf.st_mode))
  154. ret = depth ? rm_rf_depth_pat(namebuf, depth - 1, pat) : 0;
  155. else
  156. ret = unlink(namebuf);
  157. }
  158. closedir(dir);
  159. if (ret < 0)
  160. return ret;
  161. return rmdir(path);
  162. }
  163. static int rm_rf_kcore_dir(const char *path)
  164. {
  165. char kcore_dir_path[PATH_MAX];
  166. const char *pat[] = {
  167. "kcore",
  168. "kallsyms",
  169. "modules",
  170. NULL,
  171. };
  172. snprintf(kcore_dir_path, sizeof(kcore_dir_path), "%s/kcore_dir", path);
  173. return rm_rf_depth_pat(kcore_dir_path, 0, pat);
  174. }
  175. int rm_rf_perf_data(const char *path)
  176. {
  177. const char *pat[] = {
  178. "data",
  179. "data.*",
  180. NULL,
  181. };
  182. rm_rf_kcore_dir(path);
  183. return rm_rf_depth_pat(path, 0, pat);
  184. }
  185. int rm_rf(const char *path)
  186. {
  187. return rm_rf_depth_pat(path, INT_MAX, NULL);
  188. }
  189. /* A filter which removes dot files */
  190. bool lsdir_no_dot_filter(const char *name __maybe_unused, struct dirent *d)
  191. {
  192. return d->d_name[0] != '.';
  193. }
  194. /* lsdir reads a directory and store it in strlist */
  195. struct strlist *lsdir(const char *name,
  196. bool (*filter)(const char *, struct dirent *))
  197. {
  198. struct strlist *list = NULL;
  199. DIR *dir;
  200. struct dirent *d;
  201. dir = opendir(name);
  202. if (!dir)
  203. return NULL;
  204. list = strlist__new(NULL, NULL);
  205. if (!list) {
  206. errno = ENOMEM;
  207. goto out;
  208. }
  209. while ((d = readdir(dir)) != NULL) {
  210. if (!filter || filter(name, d))
  211. strlist__add(list, d->d_name);
  212. }
  213. out:
  214. closedir(dir);
  215. return list;
  216. }
  217. size_t hex_width(u64 v)
  218. {
  219. size_t n = 1;
  220. while ((v >>= 4))
  221. ++n;
  222. return n;
  223. }
  224. int perf_event_paranoid(void)
  225. {
  226. int value;
  227. if (sysctl__read_int("kernel/perf_event_paranoid", &value))
  228. return INT_MAX;
  229. return value;
  230. }
  231. bool perf_event_paranoid_check(int max_level)
  232. {
  233. return perf_cap__capable(CAP_SYS_ADMIN) ||
  234. perf_cap__capable(CAP_PERFMON) ||
  235. perf_event_paranoid() <= max_level;
  236. }
  237. static int
  238. fetch_ubuntu_kernel_version(unsigned int *puint)
  239. {
  240. ssize_t len;
  241. size_t line_len = 0;
  242. char *ptr, *line = NULL;
  243. int version, patchlevel, sublevel, err;
  244. FILE *vsig;
  245. if (!puint)
  246. return 0;
  247. vsig = fopen("/proc/version_signature", "r");
  248. if (!vsig) {
  249. pr_debug("Open /proc/version_signature failed: %s\n",
  250. strerror(errno));
  251. return -1;
  252. }
  253. len = getline(&line, &line_len, vsig);
  254. fclose(vsig);
  255. err = -1;
  256. if (len <= 0) {
  257. pr_debug("Reading from /proc/version_signature failed: %s\n",
  258. strerror(errno));
  259. goto errout;
  260. }
  261. ptr = strrchr(line, ' ');
  262. if (!ptr) {
  263. pr_debug("Parsing /proc/version_signature failed: %s\n", line);
  264. goto errout;
  265. }
  266. err = sscanf(ptr + 1, "%d.%d.%d",
  267. &version, &patchlevel, &sublevel);
  268. if (err != 3) {
  269. pr_debug("Unable to get kernel version from /proc/version_signature '%s'\n",
  270. line);
  271. goto errout;
  272. }
  273. *puint = (version << 16) + (patchlevel << 8) + sublevel;
  274. err = 0;
  275. errout:
  276. free(line);
  277. return err;
  278. }
  279. int
  280. fetch_kernel_version(unsigned int *puint, char *str,
  281. size_t str_size)
  282. {
  283. struct utsname utsname;
  284. int version, patchlevel, sublevel, err;
  285. bool int_ver_ready = false;
  286. if (access("/proc/version_signature", R_OK) == 0)
  287. if (!fetch_ubuntu_kernel_version(puint))
  288. int_ver_ready = true;
  289. if (uname(&utsname))
  290. return -1;
  291. if (str && str_size) {
  292. strncpy(str, utsname.release, str_size);
  293. str[str_size - 1] = '\0';
  294. }
  295. if (!puint || int_ver_ready)
  296. return 0;
  297. err = sscanf(utsname.release, "%d.%d.%d",
  298. &version, &patchlevel, &sublevel);
  299. if (err != 3) {
  300. pr_debug("Unable to get kernel version from uname '%s'\n",
  301. utsname.release);
  302. return -1;
  303. }
  304. *puint = (version << 16) + (patchlevel << 8) + sublevel;
  305. return 0;
  306. }
  307. int perf_tip(char **strp, const char *dirpath)
  308. {
  309. struct strlist *tips;
  310. struct str_node *node;
  311. struct strlist_config conf = {
  312. .dirname = dirpath,
  313. .file_only = true,
  314. };
  315. int ret = 0;
  316. *strp = NULL;
  317. tips = strlist__new("tips.txt", &conf);
  318. if (tips == NULL)
  319. return -errno;
  320. if (strlist__nr_entries(tips) == 0)
  321. goto out;
  322. node = strlist__entry(tips, random() % strlist__nr_entries(tips));
  323. if (asprintf(strp, "Tip: %s", node->s) < 0)
  324. ret = -ENOMEM;
  325. out:
  326. strlist__delete(tips);
  327. return ret;
  328. }
  329. char *perf_exe(char *buf, int len)
  330. {
  331. int n = readlink("/proc/self/exe", buf, len);
  332. if (n > 0) {
  333. buf[n] = 0;
  334. return buf;
  335. }
  336. return strcpy(buf, "perf");
  337. }