1 // SPDX-License-Identifier: GPL-2.0
10 #include <sys/types.h>
14 #include <sys/mount.h>
17 #include "debug-internal.h"
20 #define STR(x) _STR(x)
23 #define SYSFS_MAGIC 0x62656572
26 #ifndef PROC_SUPER_MAGIC
27 #define PROC_SUPER_MAGIC 0x9fa0
31 #define DEBUGFS_MAGIC 0x64626720
35 #define TRACEFS_MAGIC 0x74726163
38 #ifndef HUGETLBFS_MAGIC
39 #define HUGETLBFS_MAGIC 0x958458f6
43 #define BPF_FS_MAGIC 0xcafe4a11
46 static const char * const sysfs__fs_known_mountpoints[] = {
51 static const char * const procfs__known_mountpoints[] = {
56 #ifndef DEBUGFS_DEFAULT_PATH
57 #define DEBUGFS_DEFAULT_PATH "/sys/kernel/debug"
60 static const char * const debugfs__known_mountpoints[] = {
67 #ifndef TRACEFS_DEFAULT_PATH
68 #define TRACEFS_DEFAULT_PATH "/sys/kernel/tracing"
71 static const char * const tracefs__known_mountpoints[] = {
73 "/sys/kernel/debug/tracing",
79 static const char * const hugetlbfs__known_mountpoints[] = {
83 static const char * const bpf_fs__known_mountpoints[] = {
90 const char * const *mounts;
106 #ifndef TRACEFS_MAGIC
107 #define TRACEFS_MAGIC 0x74726163
110 static struct fs fs__entries[] = {
113 .mounts = sysfs__fs_known_mountpoints,
114 .magic = SYSFS_MAGIC,
119 .mounts = procfs__known_mountpoints,
120 .magic = PROC_SUPER_MAGIC,
125 .mounts = debugfs__known_mountpoints,
126 .magic = DEBUGFS_MAGIC,
131 .mounts = tracefs__known_mountpoints,
132 .magic = TRACEFS_MAGIC,
137 .mounts = hugetlbfs__known_mountpoints,
138 .magic = HUGETLBFS_MAGIC,
143 .mounts = bpf_fs__known_mountpoints,
144 .magic = BPF_FS_MAGIC,
149 static bool fs__read_mounts(struct fs *fs)
155 fp = fopen("/proc/mounts", "r");
160 fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
161 fs->path, type) == 2) {
163 if (strcmp(type, fs->name) == 0)
169 return fs->found = found;
172 static int fs__valid_mount(const char *fs, long magic)
176 if (statfs(fs, &st_fs) < 0)
178 else if ((long)st_fs.f_type != magic)
184 static bool fs__check_mounts(struct fs *fs)
186 const char * const *ptr;
190 if (fs__valid_mount(*ptr, fs->magic) == 0) {
192 strcpy(fs->path, *ptr);
201 static void mem_toupper(char *f, size_t len)
211 * Check for "NAME_PATH" environment variable to override fs location (for
212 * testing). This matches the recommendation in Documentation/sysfs-rules.txt
215 static bool fs__env_override(struct fs *fs)
218 size_t name_len = strlen(fs->name);
219 /* name + "_PATH" + '\0' */
220 char upper_name[name_len + 5 + 1];
222 memcpy(upper_name, fs->name, name_len);
223 mem_toupper(upper_name, name_len);
224 strcpy(&upper_name[name_len], "_PATH");
226 override_path = getenv(upper_name);
232 strncpy(fs->path, override_path, sizeof(fs->path) - 1);
233 fs->path[sizeof(fs->path) - 1] = '\0';
237 static const char *fs__get_mountpoint(struct fs *fs)
239 if (fs__env_override(fs))
242 if (fs__check_mounts(fs))
245 if (fs__read_mounts(fs))
251 static const char *fs__mountpoint(int idx)
253 struct fs *fs = &fs__entries[idx];
256 return (const char *)fs->path;
258 /* the mount point was already checked for the mount point
259 * but and did not exist, so return NULL to avoid scanning again.
260 * This makes the found and not found paths cost equivalent
261 * in case of multiple calls.
266 return fs__get_mountpoint(fs);
269 static const char *mount_overload(struct fs *fs)
271 size_t name_len = strlen(fs->name);
272 /* "PERF_" + name + "_ENVIRONMENT" + '\0' */
273 char upper_name[5 + name_len + 12 + 1];
275 snprintf(upper_name, name_len, "PERF_%s_ENVIRONMENT", fs->name);
276 mem_toupper(upper_name, name_len);
278 return getenv(upper_name) ?: *fs->mounts;
281 static const char *fs__mount(int idx)
283 struct fs *fs = &fs__entries[idx];
284 const char *mountpoint;
286 if (fs__mountpoint(idx))
287 return (const char *)fs->path;
289 mountpoint = mount_overload(fs);
291 if (mount(NULL, mountpoint, fs->name, 0, NULL) < 0)
294 return fs__check_mounts(fs) ? fs->path : NULL;
297 #define FS(name, idx) \
298 const char *name##__mountpoint(void) \
300 return fs__mountpoint(idx); \
303 const char *name##__mount(void) \
305 return fs__mount(idx); \
308 bool name##__configured(void) \
310 return name##__mountpoint() != NULL; \
313 FS(sysfs, FS__SYSFS);
314 FS(procfs, FS__PROCFS);
315 FS(debugfs, FS__DEBUGFS);
316 FS(tracefs, FS__TRACEFS);
317 FS(hugetlbfs, FS__HUGETLBFS);
318 FS(bpf_fs, FS__BPF_FS);
320 int filename__read_int(const char *filename, int *value)
323 int fd = open(filename, O_RDONLY), err = -1;
328 if (read(fd, line, sizeof(line)) > 0) {
338 * Parses @value out of @filename with strtoull.
339 * By using 0 for base, the strtoull detects the
340 * base automatically (see man strtoull).
342 int filename__read_ull(const char *filename, unsigned long long *value)
345 int fd = open(filename, O_RDONLY), err = -1;
350 if (read(fd, line, sizeof(line)) > 0) {
351 *value = strtoull(line, NULL, 0);
352 if (*value != ULLONG_MAX)
360 #define STRERR_BUFSIZE 128 /* For the buffer size of strerror_r */
362 int filename__read_str(const char *filename, char **buf, size_t *sizep)
364 size_t size = 0, alloc_size = 0;
365 void *bf = NULL, *nbf;
367 char sbuf[STRERR_BUFSIZE];
369 fd = open(filename, O_RDONLY);
374 if (size == alloc_size) {
375 alloc_size += BUFSIZ;
376 nbf = realloc(bf, alloc_size);
385 n = read(fd, bf + size, alloc_size - size);
388 pr_warning("read failed %d: %s\n", errno,
389 strerror_r(errno, sbuf, sizeof(sbuf)));
410 int filename__write_int(const char *filename, int value)
412 int fd = open(filename, O_WRONLY), err = -1;
418 sprintf(buf, "%d", value);
419 if (write(fd, buf, sizeof(buf)) == sizeof(buf))
426 int procfs__read_str(const char *entry, char **buf, size_t *sizep)
429 const char *procfs = procfs__mountpoint();
434 snprintf(path, sizeof(path), "%s/%s", procfs, entry);
436 return filename__read_str(path, buf, sizep);
439 int sysfs__read_ull(const char *entry, unsigned long long *value)
442 const char *sysfs = sysfs__mountpoint();
447 snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
449 return filename__read_ull(path, value);
452 int sysfs__read_int(const char *entry, int *value)
455 const char *sysfs = sysfs__mountpoint();
460 snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
462 return filename__read_int(path, value);
465 int sysfs__read_str(const char *entry, char **buf, size_t *sizep)
468 const char *sysfs = sysfs__mountpoint();
473 snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
475 return filename__read_str(path, buf, sizep);
478 int sysfs__read_bool(const char *entry, bool *value)
484 ret = sysfs__read_str(entry, &buf, &size);
507 int sysctl__read_int(const char *sysctl, int *value)
510 const char *procfs = procfs__mountpoint();
515 snprintf(path, sizeof(path), "%s/sys/%s", procfs, sysctl);
517 return filename__read_int(path, value);
520 int sysfs__write_int(const char *entry, int value)
523 const char *sysfs = sysfs__mountpoint();
528 if (snprintf(path, sizeof(path), "%s/%s", sysfs, entry) >= PATH_MAX)
531 return filename__write_int(path, value);