5 #include <linux/kernel.h>
8 static int debugfs_premounted
;
9 char debugfs_mountpoint
[PATH_MAX
+ 1] = "/sys/kernel/debug";
10 char tracing_events_path
[PATH_MAX
+ 1] = "/sys/kernel/debug/tracing/events";
12 static const char *debugfs_known_mountpoints
[] = {
18 static int debugfs_found
;
20 /* find the path to the mounted debugfs */
21 const char *debugfs_find_mountpoint(void)
28 return (const char *) debugfs_mountpoint
;
30 ptr
= debugfs_known_mountpoints
;
32 if (debugfs_valid_mountpoint(*ptr
) == 0) {
34 strcpy(debugfs_mountpoint
, *ptr
);
35 return debugfs_mountpoint
;
40 /* give up and parse /proc/mounts */
41 fp
= fopen("/proc/mounts", "r");
45 while (fscanf(fp
, "%*s %" STR(PATH_MAX
) "s %99s %*s %*d %*d\n",
46 debugfs_mountpoint
, type
) == 2) {
47 if (strcmp(type
, "debugfs") == 0)
52 if (strcmp(type
, "debugfs") != 0)
57 return debugfs_mountpoint
;
60 /* verify that a mountpoint is actually a debugfs instance */
62 int debugfs_valid_mountpoint(const char *debugfs
)
66 if (statfs(debugfs
, &st_fs
) < 0)
68 else if (st_fs
.f_type
!= (long) DEBUGFS_MAGIC
)
74 static void debugfs_set_tracing_events_path(const char *mountpoint
)
76 snprintf(tracing_events_path
, sizeof(tracing_events_path
), "%s/%s",
77 mountpoint
, "tracing/events");
80 /* mount the debugfs somewhere if it's not mounted */
82 char *debugfs_mount(const char *mountpoint
)
84 /* see if it's already mounted */
85 if (debugfs_find_mountpoint()) {
86 debugfs_premounted
= 1;
90 /* if not mounted and no argument */
91 if (mountpoint
== NULL
) {
92 /* see if environment variable set */
93 mountpoint
= getenv(PERF_DEBUGFS_ENVIRONMENT
);
94 /* if no environment variable, use default */
95 if (mountpoint
== NULL
)
96 mountpoint
= "/sys/kernel/debug";
99 if (mount(NULL
, mountpoint
, "debugfs", 0, NULL
) < 0)
102 /* save the mountpoint */
104 strncpy(debugfs_mountpoint
, mountpoint
, sizeof(debugfs_mountpoint
));
106 debugfs_set_tracing_events_path(debugfs_mountpoint
);
107 return debugfs_mountpoint
;
110 void debugfs_set_path(const char *mountpoint
)
112 snprintf(debugfs_mountpoint
, sizeof(debugfs_mountpoint
), "%s", mountpoint
);
113 debugfs_set_tracing_events_path(mountpoint
);