diff options
Diffstat (limited to 'tools/lib/api/fs/debugfs.c')
| -rw-r--r-- | tools/lib/api/fs/debugfs.c | 69 |
1 files changed, 22 insertions, 47 deletions
diff --git a/tools/lib/api/fs/debugfs.c b/tools/lib/api/fs/debugfs.c index d2b18e887071..8305b3e9d48e 100644 --- a/tools/lib/api/fs/debugfs.c +++ b/tools/lib/api/fs/debugfs.c | |||
| @@ -3,75 +3,50 @@ | |||
| 3 | #include <stdio.h> | 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> | 4 | #include <stdlib.h> |
| 5 | #include <string.h> | 5 | #include <string.h> |
| 6 | #include <unistd.h> | ||
| 6 | #include <stdbool.h> | 7 | #include <stdbool.h> |
| 7 | #include <sys/vfs.h> | 8 | #include <sys/vfs.h> |
| 9 | #include <sys/types.h> | ||
| 10 | #include <sys/stat.h> | ||
| 8 | #include <sys/mount.h> | 11 | #include <sys/mount.h> |
| 9 | #include <linux/kernel.h> | 12 | #include <linux/kernel.h> |
| 10 | 13 | ||
| 11 | #include "debugfs.h" | 14 | #include "debugfs.h" |
| 12 | 15 | ||
| 13 | char debugfs_mountpoint[PATH_MAX + 1] = "/sys/kernel/debug"; | 16 | #ifndef DEBUGFS_DEFAULT_PATH |
| 17 | #define DEBUGFS_DEFAULT_PATH "/sys/kernel/debug" | ||
| 18 | #endif | ||
| 19 | |||
| 20 | char debugfs_mountpoint[PATH_MAX + 1] = DEBUGFS_DEFAULT_PATH; | ||
| 14 | 21 | ||
| 15 | static const char * const debugfs_known_mountpoints[] = { | 22 | static const char * const debugfs_known_mountpoints[] = { |
| 16 | "/sys/kernel/debug", | 23 | DEBUGFS_DEFAULT_PATH, |
| 17 | "/debug", | 24 | "/debug", |
| 18 | 0, | 25 | 0, |
| 19 | }; | 26 | }; |
| 20 | 27 | ||
| 21 | static bool debugfs_found; | 28 | static bool debugfs_found; |
| 22 | 29 | ||
| 30 | bool debugfs_configured(void) | ||
| 31 | { | ||
| 32 | return debugfs_find_mountpoint() != NULL; | ||
| 33 | } | ||
| 34 | |||
| 23 | /* find the path to the mounted debugfs */ | 35 | /* find the path to the mounted debugfs */ |
| 24 | const char *debugfs_find_mountpoint(void) | 36 | const char *debugfs_find_mountpoint(void) |
| 25 | { | 37 | { |
| 26 | const char * const *ptr; | 38 | const char *ret; |
| 27 | char type[100]; | ||
| 28 | FILE *fp; | ||
| 29 | 39 | ||
| 30 | if (debugfs_found) | 40 | if (debugfs_found) |
| 31 | return (const char *)debugfs_mountpoint; | 41 | return (const char *)debugfs_mountpoint; |
| 32 | 42 | ||
| 33 | ptr = debugfs_known_mountpoints; | 43 | ret = find_mountpoint("debugfs", (long) DEBUGFS_MAGIC, |
| 34 | while (*ptr) { | 44 | debugfs_mountpoint, PATH_MAX + 1, |
| 35 | if (debugfs_valid_mountpoint(*ptr) == 0) { | 45 | debugfs_known_mountpoints); |
| 36 | debugfs_found = true; | 46 | if (ret) |
| 37 | strcpy(debugfs_mountpoint, *ptr); | 47 | debugfs_found = true; |
| 38 | return debugfs_mountpoint; | ||
| 39 | } | ||
| 40 | ptr++; | ||
| 41 | } | ||
| 42 | |||
| 43 | /* give up and parse /proc/mounts */ | ||
| 44 | fp = fopen("/proc/mounts", "r"); | ||
| 45 | if (fp == NULL) | ||
| 46 | return NULL; | ||
| 47 | |||
| 48 | while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n", | ||
| 49 | debugfs_mountpoint, type) == 2) { | ||
| 50 | if (strcmp(type, "debugfs") == 0) | ||
| 51 | break; | ||
| 52 | } | ||
| 53 | fclose(fp); | ||
| 54 | 48 | ||
| 55 | if (strcmp(type, "debugfs") != 0) | 49 | return ret; |
| 56 | return NULL; | ||
| 57 | |||
| 58 | debugfs_found = true; | ||
| 59 | |||
| 60 | return debugfs_mountpoint; | ||
| 61 | } | ||
| 62 | |||
| 63 | /* verify that a mountpoint is actually a debugfs instance */ | ||
| 64 | |||
| 65 | int debugfs_valid_mountpoint(const char *debugfs) | ||
| 66 | { | ||
| 67 | struct statfs st_fs; | ||
| 68 | |||
| 69 | if (statfs(debugfs, &st_fs) < 0) | ||
| 70 | return -ENOENT; | ||
| 71 | else if ((long)st_fs.f_type != (long)DEBUGFS_MAGIC) | ||
| 72 | return -ENOENT; | ||
| 73 | |||
| 74 | return 0; | ||
| 75 | } | 50 | } |
| 76 | 51 | ||
| 77 | /* mount the debugfs somewhere if it's not mounted */ | 52 | /* mount the debugfs somewhere if it's not mounted */ |
| @@ -87,7 +62,7 @@ char *debugfs_mount(const char *mountpoint) | |||
| 87 | mountpoint = getenv(PERF_DEBUGFS_ENVIRONMENT); | 62 | mountpoint = getenv(PERF_DEBUGFS_ENVIRONMENT); |
| 88 | /* if no environment variable, use default */ | 63 | /* if no environment variable, use default */ |
| 89 | if (mountpoint == NULL) | 64 | if (mountpoint == NULL) |
| 90 | mountpoint = "/sys/kernel/debug"; | 65 | mountpoint = DEBUGFS_DEFAULT_PATH; |
| 91 | } | 66 | } |
| 92 | 67 | ||
| 93 | if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0) | 68 | if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0) |
