8 #include <linux/magic.h>
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 (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
;