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 const char *input_name;
33 bool perf_singlethreaded = true;
35 void perf_set_singlethreaded(void)
37 perf_singlethreaded = true;
40 void perf_set_multithreaded(void)
42 perf_singlethreaded = false;
45 int sysctl_perf_event_max_stack = PERF_MAX_STACK_DEPTH;
46 int sysctl_perf_event_max_contexts_per_stack = PERF_MAX_CONTEXTS_PER_STACK;
48 int sysctl__max_stack(void)
52 if (sysctl__read_int("kernel/perf_event_max_stack", &value) == 0)
53 sysctl_perf_event_max_stack = value;
55 if (sysctl__read_int("kernel/perf_event_max_contexts_per_stack", &value) == 0)
56 sysctl_perf_event_max_contexts_per_stack = value;
58 return sysctl_perf_event_max_stack;
61 bool sysctl__nmi_watchdog_enabled(void)
64 static bool nmi_watchdog;
70 if (sysctl__read_int("kernel/nmi_watchdog", &value) < 0)
73 nmi_watchdog = (value > 0) ? true : false;
79 bool test_attr__enabled;
81 bool perf_host = true;
82 bool perf_guest = false;
84 void event_attr_init(struct perf_event_attr *attr)
87 attr->exclude_host = 1;
89 attr->exclude_guest = 1;
90 /* to capture ABI version */
91 attr->size = sizeof(*attr);
94 int mkdir_p(char *path, mode_t mode)
103 if (stat(path, &st) == 0)
108 while ((d = strchr(d, '/'))) {
110 err = stat(path, &st) && mkdir(path, mode);
117 return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
120 static bool match_pat(char *file, const char **pat)
128 if (strglobmatch(file, pat[i]))
138 * The depth specify how deep the removal will go.
139 * 0 - will remove only files under the 'path' directory
140 * 1 .. x - will dive in x-level deep under the 'path' directory
142 * If specified the pat is array of string patterns ended with NULL,
143 * which are checked upon every file/directory found. Only matching
146 * The function returns:
148 * -1 on removal failure with errno set
149 * -2 on pattern failure
151 static int rm_rf_depth_pat(const char *path, int depth, const char **pat)
156 char namebuf[PATH_MAX];
159 /* Do not fail if there's no file. */
160 ret = lstat(path, &statbuf);
164 /* Try to remove any file we get. */
165 if (!(statbuf.st_mode & S_IFDIR))
168 /* We have directory in path. */
173 while ((d = readdir(dir)) != NULL && !ret) {
175 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
178 if (!match_pat(d->d_name, pat)) {
183 scnprintf(namebuf, sizeof(namebuf), "%s/%s",
186 /* We have to check symbolic link itself */
187 ret = lstat(namebuf, &statbuf);
189 pr_debug("stat failed: %s\n", namebuf);
193 if (S_ISDIR(statbuf.st_mode))
194 ret = depth ? rm_rf_depth_pat(namebuf, depth - 1, pat) : 0;
196 ret = unlink(namebuf);
206 static int rm_rf_a_kcore_dir(const char *path, const char *name)
208 char kcore_dir_path[PATH_MAX];
209 const char *pat[] = {
216 snprintf(kcore_dir_path, sizeof(kcore_dir_path), "%s/%s", path, name);
218 return rm_rf_depth_pat(kcore_dir_path, 0, pat);
221 static bool kcore_dir_filter(const char *name __maybe_unused, struct dirent *d)
223 const char *pat[] = {
229 return match_pat(d->d_name, pat);
232 static int rm_rf_kcore_dir(const char *path)
234 struct strlist *kcore_dirs;
238 kcore_dirs = lsdir(path, kcore_dir_filter);
243 strlist__for_each_entry(nd, kcore_dirs) {
244 ret = rm_rf_a_kcore_dir(path, nd->s);
249 strlist__delete(kcore_dirs);
254 int rm_rf_perf_data(const char *path)
256 const char *pat[] = {
262 rm_rf_kcore_dir(path);
264 return rm_rf_depth_pat(path, 0, pat);
267 int rm_rf(const char *path)
269 return rm_rf_depth_pat(path, INT_MAX, NULL);
272 /* A filter which removes dot files */
273 bool lsdir_no_dot_filter(const char *name __maybe_unused, struct dirent *d)
275 return d->d_name[0] != '.';
278 /* lsdir reads a directory and store it in strlist */
279 struct strlist *lsdir(const char *name,
280 bool (*filter)(const char *, struct dirent *))
282 struct strlist *list = NULL;
290 list = strlist__new(NULL, NULL);
296 while ((d = readdir(dir)) != NULL) {
297 if (!filter || filter(name, d))
298 strlist__add(list, d->d_name);
306 size_t hex_width(u64 v)
316 int perf_event_paranoid(void)
320 if (sysctl__read_int("kernel/perf_event_paranoid", &value))
326 bool perf_event_paranoid_check(int max_level)
328 return perf_cap__capable(CAP_SYS_ADMIN) ||
329 perf_cap__capable(CAP_PERFMON) ||
330 perf_event_paranoid() <= max_level;
334 fetch_ubuntu_kernel_version(unsigned int *puint)
338 char *ptr, *line = NULL;
339 int version, patchlevel, sublevel, err;
345 vsig = fopen("/proc/version_signature", "r");
347 pr_debug("Open /proc/version_signature failed: %s\n",
352 len = getline(&line, &line_len, vsig);
356 pr_debug("Reading from /proc/version_signature failed: %s\n",
361 ptr = strrchr(line, ' ');
363 pr_debug("Parsing /proc/version_signature failed: %s\n", line);
367 err = sscanf(ptr + 1, "%d.%d.%d",
368 &version, &patchlevel, &sublevel);
370 pr_debug("Unable to get kernel version from /proc/version_signature '%s'\n",
375 *puint = (version << 16) + (patchlevel << 8) + sublevel;
383 fetch_kernel_version(unsigned int *puint, char *str,
386 struct utsname utsname;
387 int version, patchlevel, sublevel, err;
388 bool int_ver_ready = false;
390 if (access("/proc/version_signature", R_OK) == 0)
391 if (!fetch_ubuntu_kernel_version(puint))
392 int_ver_ready = true;
397 if (str && str_size) {
398 strncpy(str, utsname.release, str_size);
399 str[str_size - 1] = '\0';
402 if (!puint || int_ver_ready)
405 err = sscanf(utsname.release, "%d.%d.%d",
406 &version, &patchlevel, &sublevel);
409 pr_debug("Unable to get kernel version from uname '%s'\n",
414 *puint = (version << 16) + (patchlevel << 8) + sublevel;
418 int perf_tip(char **strp, const char *dirpath)
420 struct strlist *tips;
421 struct str_node *node;
422 struct strlist_config conf = {
429 tips = strlist__new("tips.txt", &conf);
433 if (strlist__nr_entries(tips) == 0)
436 node = strlist__entry(tips, random() % strlist__nr_entries(tips));
437 if (asprintf(strp, "Tip: %s", node->s) < 0)
441 strlist__delete(tips);
446 char *perf_exe(char *buf, int len)
448 int n = readlink("/proc/self/exe", buf, len);
453 return strcpy(buf, "perf");
456 void perf_debuginfod_setup(struct perf_debuginfod *di)
459 * By default '!di->set' we clear DEBUGINFOD_URLS, so debuginfod
460 * processing is not triggered, otherwise we set it to 'di->urls'
461 * value. If 'di->urls' is "system" we keep DEBUGINFOD_URLS value.
464 setenv("DEBUGINFOD_URLS", "", 1);
465 else if (di->urls && strcmp(di->urls, "system"))
466 setenv("DEBUGINFOD_URLS", di->urls, 1);
468 pr_debug("DEBUGINFOD_URLS=%s\n", getenv("DEBUGINFOD_URLS"));
470 #ifndef HAVE_DEBUGINFOD_SUPPORT
472 pr_warning("WARNING: debuginfod support requested, but perf is not built with it\n");
477 * Return a new filename prepended with task's root directory if it's in
478 * a chroot. Callers should free the returned string.
480 char *filename_with_chroot(int pid, const char *filename)
484 char *new_name = NULL;
487 scnprintf(proc_root, sizeof(proc_root), "/proc/%d/root", pid);
488 ret = readlink(proc_root, buf, sizeof(buf) - 1);
492 /* readlink(2) does not append a null byte to buf */
495 if (!strcmp(buf, "/"))
498 if (strstr(buf, "(deleted)"))
501 if (asprintf(&new_name, "%s/%s", buf, filename) < 0)
508 * Reallocate an array *arr of size *arr_sz so that it is big enough to contain
509 * x elements of size msz, initializing new entries to *init_val or zero if
512 int do_realloc_array_as_needed(void **arr, size_t *arr_sz, size_t x, size_t msz, const void *init_val)
514 size_t new_sz = *arr_sz;
519 new_sz = msz >= 64 ? 1 : roundup(64, msz); /* Start with at least 64 bytes */
520 while (x >= new_sz) {
521 if (check_mul_overflow(new_sz, (size_t)2, &new_sz))
524 if (new_sz == *arr_sz)
526 new_arr = calloc(new_sz, msz);
530 memcpy(new_arr, *arr, *arr_sz * msz);
532 for (i = *arr_sz; i < new_sz; i++)
533 memcpy(new_arr + (i * msz), init_val, msz);
540 #ifndef HAVE_SCHED_GETCPU_SUPPORT
541 int sched_getcpu(void)
545 int err = syscall(__NR_getcpu, &cpu, NULL, NULL);