Clark Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 1 | #include "util.h" |
| 2 | #include "debugfs.h" |
| 3 | #include "cache.h" |
| 4 | |
| 5 | static int debugfs_premounted; |
| 6 | static char debugfs_mountpoint[MAX_PATH+1]; |
| 7 | |
| 8 | static const char *debugfs_known_mountpoints[] = { |
| 9 | "/sys/kernel/debug/", |
| 10 | "/debug/", |
| 11 | 0, |
| 12 | }; |
| 13 | |
| 14 | /* use this to force a umount */ |
| 15 | void debugfs_force_cleanup(void) |
| 16 | { |
| 17 | debugfs_find_mountpoint(); |
| 18 | debugfs_premounted = 0; |
| 19 | debugfs_umount(); |
| 20 | } |
| 21 | |
| 22 | /* construct a full path to a debugfs element */ |
| 23 | int debugfs_make_path(const char *element, char *buffer, int size) |
| 24 | { |
| 25 | int len; |
| 26 | |
| 27 | if (strlen(debugfs_mountpoint) == 0) { |
| 28 | buffer[0] = '\0'; |
| 29 | return -1; |
| 30 | } |
| 31 | |
| 32 | len = strlen(debugfs_mountpoint) + strlen(element) + 1; |
| 33 | if (len >= size) |
| 34 | return len+1; |
| 35 | |
| 36 | snprintf(buffer, size-1, "%s/%s", debugfs_mountpoint, element); |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | static int debugfs_found; |
| 41 | |
| 42 | /* find the path to the mounted debugfs */ |
| 43 | const char *debugfs_find_mountpoint(void) |
| 44 | { |
| 45 | const char **ptr; |
| 46 | char type[100]; |
| 47 | FILE *fp; |
| 48 | |
| 49 | if (debugfs_found) |
| 50 | return (const char *) debugfs_mountpoint; |
| 51 | |
| 52 | ptr = debugfs_known_mountpoints; |
| 53 | while (*ptr) { |
| 54 | if (debugfs_valid_mountpoint(*ptr) == 0) { |
| 55 | debugfs_found = 1; |
| 56 | strcpy(debugfs_mountpoint, *ptr); |
| 57 | return debugfs_mountpoint; |
| 58 | } |
| 59 | ptr++; |
| 60 | } |
| 61 | |
| 62 | /* give up and parse /proc/mounts */ |
| 63 | fp = fopen("/proc/mounts", "r"); |
| 64 | if (fp == NULL) |
| 65 | die("Can't open /proc/mounts for read"); |
| 66 | |
| 67 | while (fscanf(fp, "%*s %" |
| 68 | STR(MAX_PATH) |
| 69 | "s %99s %*s %*d %*d\n", |
| 70 | debugfs_mountpoint, type) == 2) { |
| 71 | if (strcmp(type, "debugfs") == 0) |
| 72 | break; |
| 73 | } |
| 74 | fclose(fp); |
| 75 | |
| 76 | if (strcmp(type, "debugfs") != 0) |
| 77 | return NULL; |
| 78 | |
| 79 | debugfs_found = 1; |
| 80 | |
| 81 | return debugfs_mountpoint; |
| 82 | } |
| 83 | |
| 84 | /* verify that a mountpoint is actually a debugfs instance */ |
| 85 | |
| 86 | int debugfs_valid_mountpoint(const char *debugfs) |
| 87 | { |
| 88 | struct statfs st_fs; |
| 89 | |
| 90 | if (statfs(debugfs, &st_fs) < 0) |
| 91 | return -ENOENT; |
| 92 | else if (st_fs.f_type != (long) DEBUGFS_MAGIC) |
| 93 | return -ENOENT; |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | int debugfs_valid_entry(const char *path) |
| 100 | { |
| 101 | struct stat st; |
| 102 | |
| 103 | if (stat(path, &st)) |
| 104 | return -errno; |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
Xiao Guangrong | 29c52aa | 2009-12-28 16:47:12 +0800 | [diff] [blame^] | 109 | /* mount the debugfs somewhere if it's not mounted */ |
Clark Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 110 | |
Xiao Guangrong | 29c52aa | 2009-12-28 16:47:12 +0800 | [diff] [blame^] | 111 | char *debugfs_mount(const char *mountpoint) |
Clark Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 112 | { |
Clark Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 113 | /* see if it's already mounted */ |
| 114 | if (debugfs_find_mountpoint()) { |
| 115 | debugfs_premounted = 1; |
Xiao Guangrong | 29c52aa | 2009-12-28 16:47:12 +0800 | [diff] [blame^] | 116 | return debugfs_mountpoint; |
Clark Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /* if not mounted and no argument */ |
| 120 | if (mountpoint == NULL) { |
| 121 | /* see if environment variable set */ |
| 122 | mountpoint = getenv(PERF_DEBUGFS_ENVIRONMENT); |
| 123 | /* if no environment variable, use default */ |
| 124 | if (mountpoint == NULL) |
| 125 | mountpoint = "/sys/kernel/debug"; |
| 126 | } |
| 127 | |
Xiao Guangrong | 29c52aa | 2009-12-28 16:47:12 +0800 | [diff] [blame^] | 128 | if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0) |
| 129 | return NULL; |
| 130 | |
Clark Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 131 | /* save the mountpoint */ |
| 132 | strncpy(debugfs_mountpoint, mountpoint, sizeof(debugfs_mountpoint)); |
| 133 | |
Xiao Guangrong | 29c52aa | 2009-12-28 16:47:12 +0800 | [diff] [blame^] | 134 | return debugfs_mountpoint; |
Clark Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /* umount the debugfs */ |
| 138 | |
| 139 | int debugfs_umount(void) |
| 140 | { |
| 141 | char umountcmd[128]; |
| 142 | int ret; |
| 143 | |
| 144 | /* if it was already mounted, leave it */ |
| 145 | if (debugfs_premounted) |
| 146 | return 0; |
| 147 | |
| 148 | /* make sure it's a valid mount point */ |
| 149 | ret = debugfs_valid_mountpoint(debugfs_mountpoint); |
| 150 | if (ret) |
| 151 | return ret; |
| 152 | |
| 153 | snprintf(umountcmd, sizeof(umountcmd), |
| 154 | "/bin/umount %s", debugfs_mountpoint); |
| 155 | return system(umountcmd); |
| 156 | } |
| 157 | |
| 158 | int debugfs_write(const char *entry, const char *value) |
| 159 | { |
| 160 | char path[MAX_PATH+1]; |
| 161 | int ret, count; |
| 162 | int fd; |
| 163 | |
| 164 | /* construct the path */ |
| 165 | snprintf(path, sizeof(path), "%s/%s", debugfs_mountpoint, entry); |
| 166 | |
| 167 | /* verify that it exists */ |
| 168 | ret = debugfs_valid_entry(path); |
| 169 | if (ret) |
| 170 | return ret; |
| 171 | |
| 172 | /* get how many chars we're going to write */ |
| 173 | count = strlen(value); |
| 174 | |
| 175 | /* open the debugfs entry */ |
| 176 | fd = open(path, O_RDWR); |
| 177 | if (fd < 0) |
| 178 | return -errno; |
| 179 | |
| 180 | while (count > 0) { |
| 181 | /* write it */ |
| 182 | ret = write(fd, value, count); |
| 183 | if (ret <= 0) { |
| 184 | if (ret == EAGAIN) |
| 185 | continue; |
| 186 | close(fd); |
| 187 | return -errno; |
| 188 | } |
| 189 | count -= ret; |
| 190 | } |
| 191 | |
| 192 | /* close it */ |
| 193 | close(fd); |
| 194 | |
| 195 | /* return success */ |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * read a debugfs entry |
| 201 | * returns the number of chars read or a negative errno |
| 202 | */ |
| 203 | int debugfs_read(const char *entry, char *buffer, size_t size) |
| 204 | { |
| 205 | char path[MAX_PATH+1]; |
| 206 | int ret; |
| 207 | int fd; |
| 208 | |
| 209 | /* construct the path */ |
| 210 | snprintf(path, sizeof(path), "%s/%s", debugfs_mountpoint, entry); |
| 211 | |
| 212 | /* verify that it exists */ |
| 213 | ret = debugfs_valid_entry(path); |
| 214 | if (ret) |
| 215 | return ret; |
| 216 | |
| 217 | /* open the debugfs entry */ |
| 218 | fd = open(path, O_RDONLY); |
| 219 | if (fd < 0) |
| 220 | return -errno; |
| 221 | |
| 222 | do { |
| 223 | /* read it */ |
| 224 | ret = read(fd, buffer, size); |
| 225 | if (ret == 0) { |
| 226 | close(fd); |
| 227 | return EOF; |
| 228 | } |
| 229 | } while (ret < 0 && errno == EAGAIN); |
| 230 | |
| 231 | /* close it */ |
| 232 | close(fd); |
| 233 | |
| 234 | /* make *sure* there's a null character at the end */ |
| 235 | buffer[ret] = '\0'; |
| 236 | |
| 237 | /* return the number of chars read */ |
| 238 | return ret; |
| 239 | } |