5 #include <linux/kernel.h>
8 char debugfs_mountpoint[PATH_MAX + 1] = "/sys/kernel/debug";
9 char tracing_events_path[PATH_MAX + 1] = "/sys/kernel/debug/tracing/events";
11 static const char *debugfs_known_mountpoints[] = {
17 static bool debugfs_found;
19 /* find the path to the mounted debugfs */
20 const char *debugfs_find_mountpoint(void)
27 return (const char *) debugfs_mountpoint;
29 ptr = debugfs_known_mountpoints;
31 if (debugfs_valid_mountpoint(*ptr) == 0) {
33 strcpy(debugfs_mountpoint, *ptr);
34 return debugfs_mountpoint;
39 /* give up and parse /proc/mounts */
40 fp = fopen("/proc/mounts", "r");
44 while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
45 debugfs_mountpoint, type) == 2) {
46 if (strcmp(type, "debugfs") == 0)
51 if (strcmp(type, "debugfs") != 0)
56 return debugfs_mountpoint;
59 /* verify that a mountpoint is actually a debugfs instance */
61 int debugfs_valid_mountpoint(const char *debugfs)
65 if (statfs(debugfs, &st_fs) < 0)
67 else if (st_fs.f_type != (long) DEBUGFS_MAGIC)
73 static void debugfs_set_tracing_events_path(const char *mountpoint)
75 snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s",
76 mountpoint, "tracing/events");
79 /* mount the debugfs somewhere if it's not mounted */
81 char *debugfs_mount(const char *mountpoint)
83 /* see if it's already mounted */
84 if (debugfs_find_mountpoint())
87 /* if not mounted and no argument */
88 if (mountpoint == NULL) {
89 /* see if environment variable set */
90 mountpoint = getenv(PERF_DEBUGFS_ENVIRONMENT);
91 /* if no environment variable, use default */
92 if (mountpoint == NULL)
93 mountpoint = "/sys/kernel/debug";
96 if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0)
99 /* save the mountpoint */
100 debugfs_found = true;
101 strncpy(debugfs_mountpoint, mountpoint, sizeof(debugfs_mountpoint));
103 debugfs_set_tracing_events_path(debugfs_mountpoint);
104 return debugfs_mountpoint;
107 void debugfs_set_path(const char *mountpoint)
109 snprintf(debugfs_mountpoint, sizeof(debugfs_mountpoint), "%s", mountpoint);
110 debugfs_set_tracing_events_path(mountpoint);