Commit | Line | Data |
---|---|---|
afe61f67 CW |
1 | #include "util.h" |
2 | #include "debugfs.h" | |
3 | #include "cache.h" | |
4 | ||
ebf294bf | 5 | #include <linux/kernel.h> |
c168fbfb ACM |
6 | #include <sys/mount.h> |
7 | ||
ebf294bf ACM |
8 | char debugfs_mountpoint[PATH_MAX + 1] = "/sys/kernel/debug"; |
9 | char tracing_events_path[PATH_MAX + 1] = "/sys/kernel/debug/tracing/events"; | |
afe61f67 CW |
10 | |
11 | static const char *debugfs_known_mountpoints[] = { | |
12 | "/sys/kernel/debug/", | |
13 | "/debug/", | |
14 | 0, | |
15 | }; | |
16 | ||
fed12088 | 17 | static bool debugfs_found; |
afe61f67 CW |
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) { | |
fed12088 | 32 | debugfs_found = true; |
afe61f67 CW |
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) | |
ebf294bf | 42 | return NULL; |
afe61f67 | 43 | |
c168fbfb | 44 | while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n", |
afe61f67 CW |
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 | ||
fed12088 | 54 | debugfs_found = true; |
afe61f67 CW |
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 | ||
ebf294bf ACM |
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 | ||
29c52aa2 | 79 | /* mount the debugfs somewhere if it's not mounted */ |
afe61f67 | 80 | |
29c52aa2 | 81 | char *debugfs_mount(const char *mountpoint) |
afe61f67 | 82 | { |
afe61f67 | 83 | /* see if it's already mounted */ |
fed12088 | 84 | if (debugfs_find_mountpoint()) |
ebf294bf | 85 | goto out; |
afe61f67 CW |
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 | ||
29c52aa2 XG |
96 | if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0) |
97 | return NULL; | |
98 | ||
afe61f67 | 99 | /* save the mountpoint */ |
fed12088 | 100 | debugfs_found = true; |
ebf294bf ACM |
101 | strncpy(debugfs_mountpoint, mountpoint, sizeof(debugfs_mountpoint)); |
102 | out: | |
103 | debugfs_set_tracing_events_path(debugfs_mountpoint); | |
29c52aa2 | 104 | return debugfs_mountpoint; |
afe61f67 CW |
105 | } |
106 | ||
ebf294bf ACM |
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 | } |