8 #include <linux/magic.h>
9 #include <linux/kernel.h>
13 char debugfs_mountpoint[PATH_MAX + 1] = "/sys/kernel/debug";
14 char tracing_events_path[PATH_MAX + 1] = "/sys/kernel/debug/tracing/events";
16 static const char * const debugfs_known_mountpoints[] = {
22 static bool debugfs_found;
24 /* find the path to the mounted debugfs */
25 const char *debugfs_find_mountpoint(void)
27 const char * const *ptr;
32 return (const char *)debugfs_mountpoint;
34 ptr = debugfs_known_mountpoints;
36 if (debugfs_valid_mountpoint(*ptr) == 0) {
38 strcpy(debugfs_mountpoint, *ptr);
39 return debugfs_mountpoint;
44 /* give up and parse /proc/mounts */
45 fp = fopen("/proc/mounts", "r");
49 while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
50 debugfs_mountpoint, type) == 2) {
51 if (strcmp(type, "debugfs") == 0)
56 if (strcmp(type, "debugfs") != 0)
61 return debugfs_mountpoint;
64 /* verify that a mountpoint is actually a debugfs instance */
66 int debugfs_valid_mountpoint(const char *debugfs)
70 if (statfs(debugfs, &st_fs) < 0)
72 else if (st_fs.f_type != (long) DEBUGFS_MAGIC)
78 static void debugfs_set_tracing_events_path(const char *mountpoint)
80 snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s",
81 mountpoint, "tracing/events");
84 /* mount the debugfs somewhere if it's not mounted */
86 char *debugfs_mount(const char *mountpoint)
88 /* see if it's already mounted */
89 if (debugfs_find_mountpoint())
92 /* if not mounted and no argument */
93 if (mountpoint == NULL) {
94 /* see if environment variable set */
95 mountpoint = getenv(PERF_DEBUGFS_ENVIRONMENT);
96 /* if no environment variable, use default */
97 if (mountpoint == NULL)
98 mountpoint = "/sys/kernel/debug";
101 if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0)
104 /* save the mountpoint */
105 debugfs_found = true;
106 strncpy(debugfs_mountpoint, mountpoint, sizeof(debugfs_mountpoint));
108 debugfs_set_tracing_events_path(debugfs_mountpoint);
109 return debugfs_mountpoint;
112 void debugfs_set_path(const char *mountpoint)
114 snprintf(debugfs_mountpoint, sizeof(debugfs_mountpoint), "%s", mountpoint);
115 debugfs_set_tracing_events_path(mountpoint);