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 /* use this to force a umount */
19 void debugfs_force_cleanup(void)
21 debugfs_find_mountpoint();
22 debugfs_premounted
= 0;
26 /* construct a full path to a debugfs element */
27 int debugfs_make_path(const char *element
, char *buffer
, int size
)
31 if (strlen(debugfs_mountpoint
) == 0) {
36 len
= strlen(debugfs_mountpoint
) + strlen(element
) + 1;
40 snprintf(buffer
, size
-1, "%s/%s", debugfs_mountpoint
, element
);
44 static int debugfs_found
;
46 /* find the path to the mounted debugfs */
47 const char *debugfs_find_mountpoint(void)
54 return (const char *) debugfs_mountpoint
;
56 ptr
= debugfs_known_mountpoints
;
58 if (debugfs_valid_mountpoint(*ptr
) == 0) {
60 strcpy(debugfs_mountpoint
, *ptr
);
61 return debugfs_mountpoint
;
66 /* give up and parse /proc/mounts */
67 fp
= fopen("/proc/mounts", "r");
71 while (fscanf(fp
, "%*s %" STR(PATH_MAX
) "s %99s %*s %*d %*d\n",
72 debugfs_mountpoint
, type
) == 2) {
73 if (strcmp(type
, "debugfs") == 0)
78 if (strcmp(type
, "debugfs") != 0)
83 return debugfs_mountpoint
;
86 /* verify that a mountpoint is actually a debugfs instance */
88 int debugfs_valid_mountpoint(const char *debugfs
)
92 if (statfs(debugfs
, &st_fs
) < 0)
94 else if (st_fs
.f_type
!= (long) DEBUGFS_MAGIC
)
101 int debugfs_valid_entry(const char *path
)
111 static void debugfs_set_tracing_events_path(const char *mountpoint
)
113 snprintf(tracing_events_path
, sizeof(tracing_events_path
), "%s/%s",
114 mountpoint
, "tracing/events");
117 /* mount the debugfs somewhere if it's not mounted */
119 char *debugfs_mount(const char *mountpoint
)
121 /* see if it's already mounted */
122 if (debugfs_find_mountpoint()) {
123 debugfs_premounted
= 1;
127 /* if not mounted and no argument */
128 if (mountpoint
== NULL
) {
129 /* see if environment variable set */
130 mountpoint
= getenv(PERF_DEBUGFS_ENVIRONMENT
);
131 /* if no environment variable, use default */
132 if (mountpoint
== NULL
)
133 mountpoint
= "/sys/kernel/debug";
136 if (mount(NULL
, mountpoint
, "debugfs", 0, NULL
) < 0)
139 /* save the mountpoint */
141 strncpy(debugfs_mountpoint
, mountpoint
, sizeof(debugfs_mountpoint
));
143 debugfs_set_tracing_events_path(debugfs_mountpoint
);
144 return debugfs_mountpoint
;
147 void debugfs_set_path(const char *mountpoint
)
149 snprintf(debugfs_mountpoint
, sizeof(debugfs_mountpoint
), "%s", mountpoint
);
150 debugfs_set_tracing_events_path(mountpoint
);
153 /* umount the debugfs */
155 int debugfs_umount(void)
160 /* if it was already mounted, leave it */
161 if (debugfs_premounted
)
164 /* make sure it's a valid mount point */
165 ret
= debugfs_valid_mountpoint(debugfs_mountpoint
);
169 snprintf(umountcmd
, sizeof(umountcmd
),
170 "/bin/umount %s", debugfs_mountpoint
);
171 return system(umountcmd
);
174 int debugfs_write(const char *entry
, const char *value
)
176 char path
[PATH_MAX
+ 1];
180 /* construct the path */
181 snprintf(path
, sizeof(path
), "%s/%s", debugfs_mountpoint
, entry
);
183 /* verify that it exists */
184 ret
= debugfs_valid_entry(path
);
188 /* get how many chars we're going to write */
189 count
= strlen(value
);
191 /* open the debugfs entry */
192 fd
= open(path
, O_RDWR
);
198 ret
= write(fd
, value
, count
);
216 * read a debugfs entry
217 * returns the number of chars read or a negative errno
219 int debugfs_read(const char *entry
, char *buffer
, size_t size
)
221 char path
[PATH_MAX
+ 1];
225 /* construct the path */
226 snprintf(path
, sizeof(path
), "%s/%s", debugfs_mountpoint
, entry
);
228 /* verify that it exists */
229 ret
= debugfs_valid_entry(path
);
233 /* open the debugfs entry */
234 fd
= open(path
, O_RDONLY
);
240 ret
= read(fd
, buffer
, size
);
245 } while (ret
< 0 && errno
== EAGAIN
);
250 /* make *sure* there's a null character at the end */
253 /* return the number of chars read */