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 | |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 5 | #include <linux/kernel.h> |
Arnaldo Carvalho de Melo | c168fbf | 2011-11-16 12:55:59 -0200 | [diff] [blame] | 6 | #include <sys/mount.h> |
| 7 | |
Clark Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 8 | static int debugfs_premounted; |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 9 | char debugfs_mountpoint[PATH_MAX + 1] = "/sys/kernel/debug"; |
| 10 | char tracing_events_path[PATH_MAX + 1] = "/sys/kernel/debug/tracing/events"; |
Clark Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 11 | |
| 12 | static const char *debugfs_known_mountpoints[] = { |
| 13 | "/sys/kernel/debug/", |
| 14 | "/debug/", |
| 15 | 0, |
| 16 | }; |
| 17 | |
Clark Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 18 | static int debugfs_found; |
| 19 | |
| 20 | /* find the path to the mounted debugfs */ |
| 21 | const char *debugfs_find_mountpoint(void) |
| 22 | { |
| 23 | const char **ptr; |
| 24 | char type[100]; |
| 25 | FILE *fp; |
| 26 | |
| 27 | if (debugfs_found) |
| 28 | return (const char *) debugfs_mountpoint; |
| 29 | |
| 30 | ptr = debugfs_known_mountpoints; |
| 31 | while (*ptr) { |
| 32 | if (debugfs_valid_mountpoint(*ptr) == 0) { |
| 33 | debugfs_found = 1; |
| 34 | strcpy(debugfs_mountpoint, *ptr); |
| 35 | return debugfs_mountpoint; |
| 36 | } |
| 37 | ptr++; |
| 38 | } |
| 39 | |
| 40 | /* give up and parse /proc/mounts */ |
| 41 | fp = fopen("/proc/mounts", "r"); |
| 42 | if (fp == NULL) |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 43 | return NULL; |
Clark Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 44 | |
Arnaldo Carvalho de Melo | c168fbf | 2011-11-16 12:55:59 -0200 | [diff] [blame] | 45 | while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n", |
Clark Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 46 | debugfs_mountpoint, type) == 2) { |
| 47 | if (strcmp(type, "debugfs") == 0) |
| 48 | break; |
| 49 | } |
| 50 | fclose(fp); |
| 51 | |
| 52 | if (strcmp(type, "debugfs") != 0) |
| 53 | return NULL; |
| 54 | |
| 55 | debugfs_found = 1; |
| 56 | |
| 57 | return debugfs_mountpoint; |
| 58 | } |
| 59 | |
| 60 | /* verify that a mountpoint is actually a debugfs instance */ |
| 61 | |
| 62 | int debugfs_valid_mountpoint(const char *debugfs) |
| 63 | { |
| 64 | struct statfs st_fs; |
| 65 | |
| 66 | if (statfs(debugfs, &st_fs) < 0) |
| 67 | return -ENOENT; |
| 68 | else if (st_fs.f_type != (long) DEBUGFS_MAGIC) |
| 69 | return -ENOENT; |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 74 | static void debugfs_set_tracing_events_path(const char *mountpoint) |
| 75 | { |
| 76 | snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s", |
| 77 | mountpoint, "tracing/events"); |
| 78 | } |
| 79 | |
Xiao Guangrong | 29c52aa | 2009-12-28 16:47:12 +0800 | [diff] [blame] | 80 | /* mount the debugfs somewhere if it's not mounted */ |
Clark Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 81 | |
Xiao Guangrong | 29c52aa | 2009-12-28 16:47:12 +0800 | [diff] [blame] | 82 | char *debugfs_mount(const char *mountpoint) |
Clark Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 83 | { |
Clark Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 84 | /* see if it's already mounted */ |
| 85 | if (debugfs_find_mountpoint()) { |
| 86 | debugfs_premounted = 1; |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 87 | goto out; |
Clark Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 88 | } |
| 89 | |
| 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"; |
| 97 | } |
| 98 | |
Xiao Guangrong | 29c52aa | 2009-12-28 16:47:12 +0800 | [diff] [blame] | 99 | if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0) |
| 100 | return NULL; |
| 101 | |
Clark Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 102 | /* save the mountpoint */ |
Xiao Guangrong | 61be3e5 | 2009-12-28 16:48:30 +0800 | [diff] [blame] | 103 | debugfs_found = 1; |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 104 | strncpy(debugfs_mountpoint, mountpoint, sizeof(debugfs_mountpoint)); |
| 105 | out: |
| 106 | debugfs_set_tracing_events_path(debugfs_mountpoint); |
Xiao Guangrong | 29c52aa | 2009-12-28 16:47:12 +0800 | [diff] [blame] | 107 | return debugfs_mountpoint; |
Clark Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 108 | } |
| 109 | |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 110 | void debugfs_set_path(const char *mountpoint) |
| 111 | { |
| 112 | snprintf(debugfs_mountpoint, sizeof(debugfs_mountpoint), "%s", mountpoint); |
| 113 | debugfs_set_tracing_events_path(mountpoint); |
| 114 | } |