8 #include <linux/kernel.h>
12 char debugfs_mountpoint
[PATH_MAX
+ 1] = "/sys/kernel/debug";
14 static const char * const debugfs_known_mountpoints
[] = {
20 static bool debugfs_found
;
22 /* find the path to the mounted debugfs */
23 const char *debugfs_find_mountpoint(void)
25 const char * const *ptr
;
30 return (const char *)debugfs_mountpoint
;
32 ptr
= debugfs_known_mountpoints
;
34 if (debugfs_valid_mountpoint(*ptr
) == 0) {
36 strcpy(debugfs_mountpoint
, *ptr
);
37 return debugfs_mountpoint
;
42 /* give up and parse /proc/mounts */
43 fp
= fopen("/proc/mounts", "r");
47 while (fscanf(fp
, "%*s %" STR(PATH_MAX
) "s %99s %*s %*d %*d\n",
48 debugfs_mountpoint
, type
) == 2) {
49 if (strcmp(type
, "debugfs") == 0)
54 if (strcmp(type
, "debugfs") != 0)
59 return debugfs_mountpoint
;
62 /* verify that a mountpoint is actually a debugfs instance */
64 int debugfs_valid_mountpoint(const char *debugfs
)
68 if (statfs(debugfs
, &st_fs
) < 0)
70 else if (st_fs
.f_type
!= (long) DEBUGFS_MAGIC
)
76 /* mount the debugfs somewhere if it's not mounted */
77 char *debugfs_mount(const char *mountpoint
)
79 /* see if it's already mounted */
80 if (debugfs_find_mountpoint())
83 /* if not mounted and no argument */
84 if (mountpoint
== NULL
) {
85 /* see if environment variable set */
86 mountpoint
= getenv(PERF_DEBUGFS_ENVIRONMENT
);
87 /* if no environment variable, use default */
88 if (mountpoint
== NULL
)
89 mountpoint
= "/sys/kernel/debug";
92 if (mount(NULL
, mountpoint
, "debugfs", 0, NULL
) < 0)
95 /* save the mountpoint */
97 strncpy(debugfs_mountpoint
, mountpoint
, sizeof(debugfs_mountpoint
));
99 return debugfs_mountpoint
;