13 #include <sys/mount.h>
16 #include "debug-internal.h"
19 #define STR(x) _STR(x)
22 #define SYSFS_MAGIC 0x62656572
25 #ifndef PROC_SUPER_MAGIC
26 #define PROC_SUPER_MAGIC 0x9fa0
30 #define DEBUGFS_MAGIC 0x64626720
34 #define TRACEFS_MAGIC 0x74726163
37 #ifndef HUGETLBFS_MAGIC
38 #define HUGETLBFS_MAGIC 0x958458f6
41 static const char * const sysfs__fs_known_mountpoints[] = {
46 static const char * const procfs__known_mountpoints[] = {
51 #ifndef DEBUGFS_DEFAULT_PATH
52 #define DEBUGFS_DEFAULT_PATH "/sys/kernel/debug"
55 static const char * const debugfs__known_mountpoints[] = {
62 #ifndef TRACEFS_DEFAULT_PATH
63 #define TRACEFS_DEFAULT_PATH "/sys/kernel/tracing"
66 static const char * const tracefs__known_mountpoints[] = {
68 "/sys/kernel/debug/tracing",
74 static const char * const hugetlbfs__known_mountpoints[] = {
80 const char * const *mounts;
95 #define TRACEFS_MAGIC 0x74726163
98 static struct fs fs__entries[] = {
101 .mounts = sysfs__fs_known_mountpoints,
102 .magic = SYSFS_MAGIC,
106 .mounts = procfs__known_mountpoints,
107 .magic = PROC_SUPER_MAGIC,
111 .mounts = debugfs__known_mountpoints,
112 .magic = DEBUGFS_MAGIC,
116 .mounts = tracefs__known_mountpoints,
117 .magic = TRACEFS_MAGIC,
121 .mounts = hugetlbfs__known_mountpoints,
122 .magic = HUGETLBFS_MAGIC,
126 static bool fs__read_mounts(struct fs *fs)
132 fp = fopen("/proc/mounts", "r");
137 fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
138 fs->path, type) == 2) {
140 if (strcmp(type, fs->name) == 0)
145 return fs->found = found;
148 static int fs__valid_mount(const char *fs, long magic)
152 if (statfs(fs, &st_fs) < 0)
154 else if ((long)st_fs.f_type != magic)
160 static bool fs__check_mounts(struct fs *fs)
162 const char * const *ptr;
166 if (fs__valid_mount(*ptr, fs->magic) == 0) {
168 strcpy(fs->path, *ptr);
177 static void mem_toupper(char *f, size_t len)
187 * Check for "NAME_PATH" environment variable to override fs location (for
188 * testing). This matches the recommendation in Documentation/sysfs-rules.txt
191 static bool fs__env_override(struct fs *fs)
194 size_t name_len = strlen(fs->name);
195 /* name + "_PATH" + '\0' */
196 char upper_name[name_len + 5 + 1];
198 memcpy(upper_name, fs->name, name_len);
199 mem_toupper(upper_name, name_len);
200 strcpy(&upper_name[name_len], "_PATH");
202 override_path = getenv(upper_name);
207 strncpy(fs->path, override_path, sizeof(fs->path) - 1);
208 fs->path[sizeof(fs->path) - 1] = '\0';
212 static const char *fs__get_mountpoint(struct fs *fs)
214 if (fs__env_override(fs))
217 if (fs__check_mounts(fs))
220 if (fs__read_mounts(fs))
226 static const char *fs__mountpoint(int idx)
228 struct fs *fs = &fs__entries[idx];
231 return (const char *)fs->path;
233 return fs__get_mountpoint(fs);
236 static const char *mount_overload(struct fs *fs)
238 size_t name_len = strlen(fs->name);
239 /* "PERF_" + name + "_ENVIRONMENT" + '\0' */
240 char upper_name[5 + name_len + 12 + 1];
242 snprintf(upper_name, name_len, "PERF_%s_ENVIRONMENT", fs->name);
243 mem_toupper(upper_name, name_len);
245 return getenv(upper_name) ?: *fs->mounts;
248 static const char *fs__mount(int idx)
250 struct fs *fs = &fs__entries[idx];
251 const char *mountpoint;
253 if (fs__mountpoint(idx))
254 return (const char *)fs->path;
256 mountpoint = mount_overload(fs);
258 if (mount(NULL, mountpoint, fs->name, 0, NULL) < 0)
261 return fs__check_mounts(fs) ? fs->path : NULL;
264 #define FS(name, idx) \
265 const char *name##__mountpoint(void) \
267 return fs__mountpoint(idx); \
270 const char *name##__mount(void) \
272 return fs__mount(idx); \
275 bool name##__configured(void) \
277 return name##__mountpoint() != NULL; \
280 FS(sysfs, FS__SYSFS);
281 FS(procfs, FS__PROCFS);
282 FS(debugfs, FS__DEBUGFS);
283 FS(tracefs, FS__TRACEFS);
284 FS(hugetlbfs, FS__HUGETLBFS);
286 int filename__read_int(const char *filename, int *value)
289 int fd = open(filename, O_RDONLY), err = -1;
294 if (read(fd, line, sizeof(line)) > 0) {
304 * Parses @value out of @filename with strtoull.
305 * By using 0 for base, the strtoull detects the
306 * base automatically (see man strtoull).
308 int filename__read_ull(const char *filename, unsigned long long *value)
311 int fd = open(filename, O_RDONLY), err = -1;
316 if (read(fd, line, sizeof(line)) > 0) {
317 *value = strtoull(line, NULL, 0);
318 if (*value != ULLONG_MAX)
326 #define STRERR_BUFSIZE 128 /* For the buffer size of strerror_r */
328 int filename__read_str(const char *filename, char **buf, size_t *sizep)
330 size_t size = 0, alloc_size = 0;
331 void *bf = NULL, *nbf;
333 char sbuf[STRERR_BUFSIZE];
335 fd = open(filename, O_RDONLY);
340 if (size == alloc_size) {
341 alloc_size += BUFSIZ;
342 nbf = realloc(bf, alloc_size);
351 n = read(fd, bf + size, alloc_size - size);
354 pr_warning("read failed %d: %s\n", errno,
355 strerror_r(errno, sbuf, sizeof(sbuf)));
376 int procfs__read_str(const char *entry, char **buf, size_t *sizep)
379 const char *procfs = procfs__mountpoint();
384 snprintf(path, sizeof(path), "%s/%s", procfs, entry);
386 return filename__read_str(path, buf, sizep);
389 int sysfs__read_ull(const char *entry, unsigned long long *value)
392 const char *sysfs = sysfs__mountpoint();
397 snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
399 return filename__read_ull(path, value);
402 int sysfs__read_int(const char *entry, int *value)
405 const char *sysfs = sysfs__mountpoint();
410 snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
412 return filename__read_int(path, value);
415 int sysfs__read_str(const char *entry, char **buf, size_t *sizep)
418 const char *sysfs = sysfs__mountpoint();
423 snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
425 return filename__read_str(path, buf, sizep);
428 int sysctl__read_int(const char *sysctl, int *value)
431 const char *procfs = procfs__mountpoint();
436 snprintf(path, sizeof(path), "%s/sys/%s", procfs, sysctl);
438 return filename__read_int(path, value);