Merge commit 'ed30f24e8d07d30aa3e69d1f508f4d7bd2e8ea14' of git://git.linaro.org/landi...
[firefly-linux-kernel-4.4.55.git] / tools / lib / lk / debugfs.c
1 #include <errno.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <stdbool.h>
6 #include <sys/vfs.h>
7 #include <sys/mount.h>
8 #include <linux/magic.h>
9 #include <linux/kernel.h>
10
11 #include "debugfs.h"
12
13 char debugfs_mountpoint[PATH_MAX + 1] = "/sys/kernel/debug";
14
15 static const char * const debugfs_known_mountpoints[] = {
16         "/sys/kernel/debug/",
17         "/debug/",
18         0,
19 };
20
21 static bool debugfs_found;
22
23 /* find the path to the mounted debugfs */
24 const char *debugfs_find_mountpoint(void)
25 {
26         const char * const *ptr;
27         char type[100];
28         FILE *fp;
29
30         if (debugfs_found)
31                 return (const char *)debugfs_mountpoint;
32
33         ptr = debugfs_known_mountpoints;
34         while (*ptr) {
35                 if (debugfs_valid_mountpoint(*ptr) == 0) {
36                         debugfs_found = true;
37                         strcpy(debugfs_mountpoint, *ptr);
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
55         if (strcmp(type, "debugfs") != 0)
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 (st_fs.f_type != (long) DEBUGFS_MAGIC)
72                 return -ENOENT;
73
74         return 0;
75 }
76
77 /* mount the debugfs somewhere if it's not mounted */
78 char *debugfs_mount(const char *mountpoint)
79 {
80         /* see if it's already mounted */
81         if (debugfs_find_mountpoint())
82                 goto out;
83
84         /* if not mounted and no argument */
85         if (mountpoint == NULL) {
86                 /* see if environment variable set */
87                 mountpoint = getenv(PERF_DEBUGFS_ENVIRONMENT);
88                 /* if no environment variable, use default */
89                 if (mountpoint == NULL)
90                         mountpoint = "/sys/kernel/debug";
91         }
92
93         if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0)
94                 return NULL;
95
96         /* save the mountpoint */
97         debugfs_found = true;
98         strncpy(debugfs_mountpoint, mountpoint, sizeof(debugfs_mountpoint));
99 out:
100         return debugfs_mountpoint;
101 }