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