perf tools: Introduce tools/lib/lk library
[linux-3.10.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 char tracing_events_path[PATH_MAX + 1] = "/sys/kernel/debug/tracing/events";
15
16 static const char * const debugfs_known_mountpoints[] = {
17         "/sys/kernel/debug/",
18         "/debug/",
19         0,
20 };
21
22 static bool debugfs_found;
23
24 /* find the path to the mounted debugfs */
25 const char *debugfs_find_mountpoint(void)
26 {
27         const char * const *ptr;
28         char type[100];
29         FILE *fp;
30
31         if (debugfs_found)
32                 return (const char *)debugfs_mountpoint;
33
34         ptr = debugfs_known_mountpoints;
35         while (*ptr) {
36                 if (debugfs_valid_mountpoint(*ptr) == 0) {
37                         debugfs_found = true;
38                         strcpy(debugfs_mountpoint, *ptr);
39                         return debugfs_mountpoint;
40                 }
41                 ptr++;
42         }
43
44         /* give up and parse /proc/mounts */
45         fp = fopen("/proc/mounts", "r");
46         if (fp == NULL)
47                 return NULL;
48
49         while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
50                       debugfs_mountpoint, type) == 2) {
51                 if (strcmp(type, "debugfs") == 0)
52                         break;
53         }
54         fclose(fp);
55
56         if (strcmp(type, "debugfs") != 0)
57                 return NULL;
58
59         debugfs_found = true;
60
61         return debugfs_mountpoint;
62 }
63
64 /* verify that a mountpoint is actually a debugfs instance */
65
66 int debugfs_valid_mountpoint(const char *debugfs)
67 {
68         struct statfs st_fs;
69
70         if (statfs(debugfs, &st_fs) < 0)
71                 return -ENOENT;
72         else if (st_fs.f_type != (long) DEBUGFS_MAGIC)
73                 return -ENOENT;
74
75         return 0;
76 }
77
78 static void debugfs_set_tracing_events_path(const char *mountpoint)
79 {
80         snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s",
81                  mountpoint, "tracing/events");
82 }
83
84 /* mount the debugfs somewhere if it's not mounted */
85
86 char *debugfs_mount(const char *mountpoint)
87 {
88         /* see if it's already mounted */
89         if (debugfs_find_mountpoint())
90                 goto out;
91
92         /* if not mounted and no argument */
93         if (mountpoint == NULL) {
94                 /* see if environment variable set */
95                 mountpoint = getenv(PERF_DEBUGFS_ENVIRONMENT);
96                 /* if no environment variable, use default */
97                 if (mountpoint == NULL)
98                         mountpoint = "/sys/kernel/debug";
99         }
100
101         if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0)
102                 return NULL;
103
104         /* save the mountpoint */
105         debugfs_found = true;
106         strncpy(debugfs_mountpoint, mountpoint, sizeof(debugfs_mountpoint));
107 out:
108         debugfs_set_tracing_events_path(debugfs_mountpoint);
109         return debugfs_mountpoint;
110 }
111
112 void debugfs_set_path(const char *mountpoint)
113 {
114         snprintf(debugfs_mountpoint, sizeof(debugfs_mountpoint), "%s", mountpoint);
115         debugfs_set_tracing_events_path(mountpoint);
116 }