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