1 // SPDX-License-Identifier: GPL-2.0
7 #include <sys/utsname.h>
17 #include <linux/capability.h>
18 #include <linux/kernel.h>
19 #include <linux/log2.h>
20 #include <linux/time64.h>
21 #include <linux/overflow.h>
28 * XXX We need to find a better place for these things...
31 bool perf_singlethreaded = true;
33 void perf_set_singlethreaded(void)
35 perf_singlethreaded = true;
38 void perf_set_multithreaded(void)
40 perf_singlethreaded = false;
43 int sysctl_perf_event_max_stack = PERF_MAX_STACK_DEPTH;
44 int sysctl_perf_event_max_contexts_per_stack = PERF_MAX_CONTEXTS_PER_STACK;
46 int sysctl__max_stack(void)
50 if (sysctl__read_int("kernel/perf_event_max_stack", &value) == 0)
51 sysctl_perf_event_max_stack = value;
53 if (sysctl__read_int("kernel/perf_event_max_contexts_per_stack", &value) == 0)
54 sysctl_perf_event_max_contexts_per_stack = value;
56 return sysctl_perf_event_max_stack;
59 bool sysctl__nmi_watchdog_enabled(void)
62 static bool nmi_watchdog;
68 if (sysctl__read_int("kernel/nmi_watchdog", &value) < 0)
71 nmi_watchdog = (value > 0) ? true : false;
77 bool test_attr__enabled;
79 bool perf_host = true;
80 bool perf_guest = false;
82 void event_attr_init(struct perf_event_attr *attr)
85 attr->exclude_host = 1;
87 attr->exclude_guest = 1;
88 /* to capture ABI version */
89 attr->size = sizeof(*attr);
92 int mkdir_p(char *path, mode_t mode)
101 if (stat(path, &st) == 0)
106 while ((d = strchr(d, '/'))) {
108 err = stat(path, &st) && mkdir(path, mode);
115 return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
118 static bool match_pat(char *file, const char **pat)
126 if (strglobmatch(file, pat[i]))
136 * The depth specify how deep the removal will go.
137 * 0 - will remove only files under the 'path' directory
138 * 1 .. x - will dive in x-level deep under the 'path' directory
140 * If specified the pat is array of string patterns ended with NULL,
141 * which are checked upon every file/directory found. Only matching
144 * The function returns:
146 * -1 on removal failure with errno set
147 * -2 on pattern failure
149 static int rm_rf_depth_pat(const char *path, int depth, const char **pat)
154 char namebuf[PATH_MAX];
157 /* Do not fail if there's no file. */
158 ret = lstat(path, &statbuf);
162 /* Try to remove any file we get. */
163 if (!(statbuf.st_mode & S_IFDIR))
166 /* We have directory in path. */
171 while ((d = readdir(dir)) != NULL && !ret) {
173 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
176 if (!match_pat(d->d_name, pat)) {
181 scnprintf(namebuf, sizeof(namebuf), "%s/%s",
184 /* We have to check symbolic link itself */
185 ret = lstat(namebuf, &statbuf);
187 pr_debug("stat failed: %s\n", namebuf);
191 if (S_ISDIR(statbuf.st_mode))
192 ret = depth ? rm_rf_depth_pat(namebuf, depth - 1, pat) : 0;
194 ret = unlink(namebuf);
204 static int rm_rf_a_kcore_dir(const char *path, const char *name)
206 char kcore_dir_path[PATH_MAX];
207 const char *pat[] = {
214 snprintf(kcore_dir_path, sizeof(kcore_dir_path), "%s/%s", path, name);
216 return rm_rf_depth_pat(kcore_dir_path, 0, pat);
219 static bool kcore_dir_filter(const char *name __maybe_unused, struct dirent *d)
221 const char *pat[] = {
227 return match_pat(d->d_name, pat);
230 static int rm_rf_kcore_dir(const char *path)
232 struct strlist *kcore_dirs;
236 kcore_dirs = lsdir(path, kcore_dir_filter);
241 strlist__for_each_entry(nd, kcore_dirs) {
242 ret = rm_rf_a_kcore_dir(path, nd->s);
247 strlist__delete(kcore_dirs);
252 int rm_rf_perf_data(const char *path)
254 const char *pat[] = {
260 rm_rf_kcore_dir(path);
262 return rm_rf_depth_pat(path, 0, pat);
265 int rm_rf(const char *path)
267 return rm_rf_depth_pat(path, INT_MAX, NULL);
270 /* A filter which removes dot files */
271 bool lsdir_no_dot_filter(const char *name __maybe_unused, struct dirent *d)
273 return d->d_name[0] != '.';
276 /* lsdir reads a directory and store it in strlist */
277 struct strlist *lsdir(const char *name,
278 bool (*filter)(const char *, struct dirent *))
280 struct strlist *list = NULL;
288 list = strlist__new(NULL, NULL);
294 while ((d = readdir(dir)) != NULL) {
295 if (!filter || filter(name, d))
296 strlist__add(list, d->d_name);
304 size_t hex_width(u64 v)
314 int perf_event_paranoid(void)
318 if (sysctl__read_int("kernel/perf_event_paranoid", &value))
324 bool perf_event_paranoid_check(int max_level)
326 return perf_cap__capable(CAP_SYS_ADMIN) ||
327 perf_cap__capable(CAP_PERFMON) ||
328 perf_event_paranoid() <= max_level;
332 fetch_ubuntu_kernel_version(unsigned int *puint)
336 char *ptr, *line = NULL;
337 int version, patchlevel, sublevel, err;
343 vsig = fopen("/proc/version_signature", "r");
345 pr_debug("Open /proc/version_signature failed: %s\n",
350 len = getline(&line, &line_len, vsig);
354 pr_debug("Reading from /proc/version_signature failed: %s\n",
359 ptr = strrchr(line, ' ');
361 pr_debug("Parsing /proc/version_signature failed: %s\n", line);
365 err = sscanf(ptr + 1, "%d.%d.%d",
366 &version, &patchlevel, &sublevel);
368 pr_debug("Unable to get kernel version from /proc/version_signature '%s'\n",
373 *puint = (version << 16) + (patchlevel << 8) + sublevel;
381 fetch_kernel_version(unsigned int *puint, char *str,
384 struct utsname utsname;
385 int version, patchlevel, sublevel, err;
386 bool int_ver_ready = false;
388 if (access("/proc/version_signature", R_OK) == 0)
389 if (!fetch_ubuntu_kernel_version(puint))
390 int_ver_ready = true;
395 if (str && str_size) {
396 strncpy(str, utsname.release, str_size);
397 str[str_size - 1] = '\0';
400 if (!puint || int_ver_ready)
403 err = sscanf(utsname.release, "%d.%d.%d",
404 &version, &patchlevel, &sublevel);
407 pr_debug("Unable to get kernel version from uname '%s'\n",
412 *puint = (version << 16) + (patchlevel << 8) + sublevel;
416 int perf_tip(char **strp, const char *dirpath)
418 struct strlist *tips;
419 struct str_node *node;
420 struct strlist_config conf = {
427 tips = strlist__new("tips.txt", &conf);
431 if (strlist__nr_entries(tips) == 0)
434 node = strlist__entry(tips, random() % strlist__nr_entries(tips));
435 if (asprintf(strp, "Tip: %s", node->s) < 0)
439 strlist__delete(tips);
444 char *perf_exe(char *buf, int len)
446 int n = readlink("/proc/self/exe", buf, len);
451 return strcpy(buf, "perf");
454 void perf_debuginfod_setup(struct perf_debuginfod *di)
457 * By default '!di->set' we clear DEBUGINFOD_URLS, so debuginfod
458 * processing is not triggered, otherwise we set it to 'di->urls'
459 * value. If 'di->urls' is "system" we keep DEBUGINFOD_URLS value.
462 setenv("DEBUGINFOD_URLS", "", 1);
463 else if (di->urls && strcmp(di->urls, "system"))
464 setenv("DEBUGINFOD_URLS", di->urls, 1);
466 pr_debug("DEBUGINFOD_URLS=%s\n", getenv("DEBUGINFOD_URLS"));
468 #ifndef HAVE_DEBUGINFOD_SUPPORT
470 pr_warning("WARNING: debuginfod support requested, but perf is not built with it\n");
475 * Return a new filename prepended with task's root directory if it's in
476 * a chroot. Callers should free the returned string.
478 char *filename_with_chroot(int pid, const char *filename)
482 char *new_name = NULL;
485 scnprintf(proc_root, sizeof(proc_root), "/proc/%d/root", pid);
486 ret = readlink(proc_root, buf, sizeof(buf) - 1);
490 /* readlink(2) does not append a null byte to buf */
493 if (!strcmp(buf, "/"))
496 if (strstr(buf, "(deleted)"))
499 if (asprintf(&new_name, "%s/%s", buf, filename) < 0)
506 * Reallocate an array *arr of size *arr_sz so that it is big enough to contain
507 * x elements of size msz, initializing new entries to *init_val or zero if
510 int do_realloc_array_as_needed(void **arr, size_t *arr_sz, size_t x, size_t msz, const void *init_val)
512 size_t new_sz = *arr_sz;
517 new_sz = msz >= 64 ? 1 : roundup(64, msz); /* Start with at least 64 bytes */
518 while (x >= new_sz) {
519 if (check_mul_overflow(new_sz, (size_t)2, &new_sz))
522 if (new_sz == *arr_sz)
524 new_arr = calloc(new_sz, msz);
527 memcpy(new_arr, *arr, *arr_sz * msz);
529 for (i = *arr_sz; i < new_sz; i++)
530 memcpy(new_arr + (i * msz), init_val, msz);