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