9 #include <linux/kernel.h>
13 char debugfs_mountpoint[PATH_MAX + 1] = "/sys/kernel/debug";
15 static const char * const debugfs_known_mountpoints[] = {
21 static bool debugfs_found;
23 /* find the path to the mounted debugfs */
24 const char *debugfs_find_mountpoint(void)
26 const char * const *ptr;
31 return (const char *)debugfs_mountpoint;
33 ptr = debugfs_known_mountpoints;
35 if (debugfs_valid_mountpoint(*ptr) == 0) {
37 strcpy(debugfs_mountpoint, *ptr);
38 return debugfs_mountpoint;
43 /* give up and parse /proc/mounts */
44 fp = fopen("/proc/mounts", "r");
48 while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
49 debugfs_mountpoint, type) == 2) {
50 if (strcmp(type, "debugfs") == 0)
55 if (strcmp(type, "debugfs") != 0)
60 return debugfs_mountpoint;
63 /* verify that a mountpoint is actually a debugfs instance */
65 int debugfs_valid_mountpoint(const char *debugfs)
69 if (statfs(debugfs, &st_fs) < 0)
71 else if ((long)st_fs.f_type != (long)DEBUGFS_MAGIC)
77 /* mount the debugfs somewhere if it's not mounted */
78 char *debugfs_mount(const char *mountpoint)
80 /* see if it's already mounted */
81 if (debugfs_find_mountpoint())
84 /* if not mounted and no argument */
85 if (mountpoint == NULL) {
86 /* see if environment variable set */
87 mountpoint = getenv(PERF_DEBUGFS_ENVIRONMENT);
88 /* if no environment variable, use default */
89 if (mountpoint == NULL)
90 mountpoint = "/sys/kernel/debug";
93 if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0)
96 /* save the mountpoint */
98 strncpy(debugfs_mountpoint, mountpoint, sizeof(debugfs_mountpoint));
100 return debugfs_mountpoint;
103 int debugfs__strerror_open(int err, char *buf, size_t size, const char *filename)
111 "Error:\tFile %s/%s not found.\n"
112 "Hint:\tPerhaps this kernel misses some CONFIG_ setting to enable this feature?.\n",
113 debugfs_mountpoint, filename);
116 snprintf(buf, size, "%s",
117 "Error:\tUnable to find debugfs\n"
118 "Hint:\tWas your kernel compiled with debugfs support?\n"
119 "Hint:\tIs the debugfs filesystem mounted?\n"
120 "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
124 "Error:\tNo permissions to read %s/%s\n"
125 "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
126 debugfs_mountpoint, filename, debugfs_mountpoint);
129 snprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf)));
136 int debugfs__strerror_open_tp(int err, char *buf, size_t size, const char *sys, const char *name)
140 snprintf(path, PATH_MAX, "tracing/events/%s/%s", sys, name ?: "*");
142 return debugfs__strerror_open(err, buf, size, path);