5 static int debugfs_premounted
;
6 static char debugfs_mountpoint
[MAX_PATH
+1];
8 static const char *debugfs_known_mountpoints
[] = {
14 /* use this to force a umount */
15 void debugfs_force_cleanup(void)
17 debugfs_find_mountpoint();
18 debugfs_premounted
= 0;
22 /* construct a full path to a debugfs element */
23 int debugfs_make_path(const char *element
, char *buffer
, int size
)
27 if (strlen(debugfs_mountpoint
) == 0) {
32 len
= strlen(debugfs_mountpoint
) + strlen(element
) + 1;
36 snprintf(buffer
, size
-1, "%s/%s", debugfs_mountpoint
, element
);
40 static int debugfs_found
;
42 /* find the path to the mounted debugfs */
43 const char *debugfs_find_mountpoint(void)
50 return (const char *) debugfs_mountpoint
;
52 ptr
= debugfs_known_mountpoints
;
54 if (debugfs_valid_mountpoint(*ptr
) == 0) {
56 strcpy(debugfs_mountpoint
, *ptr
);
57 return debugfs_mountpoint
;
62 /* give up and parse /proc/mounts */
63 fp
= fopen("/proc/mounts", "r");
65 die("Can't open /proc/mounts for read");
67 while (fscanf(fp
, "%*s %"
69 "s %99s %*s %*d %*d\n",
70 debugfs_mountpoint
, type
) == 2) {
71 if (strcmp(type
, "debugfs") == 0)
76 if (strcmp(type
, "debugfs") != 0)
81 return debugfs_mountpoint
;
84 /* verify that a mountpoint is actually a debugfs instance */
86 int debugfs_valid_mountpoint(const char *debugfs
)
90 if (statfs(debugfs
, &st_fs
) < 0)
92 else if (st_fs
.f_type
!= (long) DEBUGFS_MAGIC
)
99 int debugfs_valid_entry(const char *path
)
109 /* mount the debugfs somewhere */
111 int debugfs_mount(const char *mountpoint
)
115 /* see if it's already mounted */
116 if (debugfs_find_mountpoint()) {
117 debugfs_premounted
= 1;
121 /* if not mounted and no argument */
122 if (mountpoint
== NULL
) {
123 /* see if environment variable set */
124 mountpoint
= getenv(PERF_DEBUGFS_ENVIRONMENT
);
125 /* if no environment variable, use default */
126 if (mountpoint
== NULL
)
127 mountpoint
= "/sys/kernel/debug";
130 /* save the mountpoint */
131 strncpy(debugfs_mountpoint
, mountpoint
, sizeof(debugfs_mountpoint
));
134 snprintf(mountcmd
, sizeof(mountcmd
),
135 "/bin/mount -t debugfs debugfs %s", mountpoint
);
136 return system(mountcmd
);
139 /* umount the debugfs */
141 int debugfs_umount(void)
146 /* if it was already mounted, leave it */
147 if (debugfs_premounted
)
150 /* make sure it's a valid mount point */
151 ret
= debugfs_valid_mountpoint(debugfs_mountpoint
);
155 snprintf(umountcmd
, sizeof(umountcmd
),
156 "/bin/umount %s", debugfs_mountpoint
);
157 return system(umountcmd
);
160 int debugfs_write(const char *entry
, const char *value
)
162 char path
[MAX_PATH
+1];
166 /* construct the path */
167 snprintf(path
, sizeof(path
), "%s/%s", debugfs_mountpoint
, entry
);
169 /* verify that it exists */
170 ret
= debugfs_valid_entry(path
);
174 /* get how many chars we're going to write */
175 count
= strlen(value
);
177 /* open the debugfs entry */
178 fd
= open(path
, O_RDWR
);
184 ret
= write(fd
, value
, count
);
202 * read a debugfs entry
203 * returns the number of chars read or a negative errno
205 int debugfs_read(const char *entry
, char *buffer
, size_t size
)
207 char path
[MAX_PATH
+1];
211 /* construct the path */
212 snprintf(path
, sizeof(path
), "%s/%s", debugfs_mountpoint
, entry
);
214 /* verify that it exists */
215 ret
= debugfs_valid_entry(path
);
219 /* open the debugfs entry */
220 fd
= open(path
, O_RDONLY
);
226 ret
= read(fd
, buffer
, size
);
231 } while (ret
< 0 && errno
== EAGAIN
);
236 /* make *sure* there's a null character at the end */
239 /* return the number of chars read */