11 #include <sys/mount.h>
12 #include <linux/kernel.h>
17 #ifndef DEBUGFS_DEFAULT_PATH
18 #define DEBUGFS_DEFAULT_PATH "/sys/kernel/debug"
21 char debugfs_mountpoint[PATH_MAX + 1] = DEBUGFS_DEFAULT_PATH;
23 static const char * const debugfs_known_mountpoints[] = {
29 static bool debugfs_found;
31 bool debugfs_configured(void)
33 return debugfs_find_mountpoint() != NULL;
36 /* find the path to the mounted debugfs */
37 const char *debugfs_find_mountpoint(void)
42 return (const char *)debugfs_mountpoint;
44 ret = find_mountpoint("debugfs", (long) DEBUGFS_MAGIC,
45 debugfs_mountpoint, PATH_MAX + 1,
46 debugfs_known_mountpoints);
53 /* mount the debugfs somewhere if it's not mounted */
54 char *debugfs_mount(const char *mountpoint)
56 /* see if it's already mounted */
57 if (debugfs_find_mountpoint())
60 /* if not mounted and no argument */
61 if (mountpoint == NULL) {
62 /* see if environment variable set */
63 mountpoint = getenv(PERF_DEBUGFS_ENVIRONMENT);
64 /* if no environment variable, use default */
65 if (mountpoint == NULL)
66 mountpoint = DEBUGFS_DEFAULT_PATH;
69 if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0)
72 /* save the mountpoint */
74 strncpy(debugfs_mountpoint, mountpoint, sizeof(debugfs_mountpoint));
76 return debugfs_mountpoint;
79 int debugfs__strerror_open(int err, char *buf, size_t size, const char *filename)
87 "Error:\tFile %s/%s not found.\n"
88 "Hint:\tPerhaps this kernel misses some CONFIG_ setting to enable this feature?.\n",
89 debugfs_mountpoint, filename);
92 snprintf(buf, size, "%s",
93 "Error:\tUnable to find debugfs\n"
94 "Hint:\tWas your kernel compiled with debugfs support?\n"
95 "Hint:\tIs the debugfs filesystem mounted?\n"
96 "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
99 const char *mountpoint = debugfs_mountpoint;
101 if (!access(debugfs_mountpoint, R_OK) && strncmp(filename, "tracing/", 8) == 0) {
102 const char *tracefs_mntpoint = tracefs_find_mountpoint();
104 if (tracefs_mntpoint)
105 mountpoint = tracefs_mntpoint;
109 "Error:\tNo permissions to read %s/%s\n"
110 "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
111 debugfs_mountpoint, filename, mountpoint);
115 snprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf)));
122 int debugfs__strerror_open_tp(int err, char *buf, size_t size, const char *sys, const char *name)
126 snprintf(path, PATH_MAX, "tracing/events/%s/%s", sys, name ?: "*");
128 return debugfs__strerror_open(err, buf, size, path);