blob: e55495c7823aada12bf0f50ef7a4fe088418a353 [file] [log] [blame]
Clark Williamsafe61f62009-11-08 09:01:37 -06001#include "util.h"
2#include "debugfs.h"
3#include "cache.h"
4
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02005#include <linux/kernel.h>
Arnaldo Carvalho de Meloc168fbf2011-11-16 12:55:59 -02006#include <sys/mount.h>
7
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -02008char debugfs_mountpoint[PATH_MAX + 1] = "/sys/kernel/debug";
9char tracing_events_path[PATH_MAX + 1] = "/sys/kernel/debug/tracing/events";
Clark Williamsafe61f62009-11-08 09:01:37 -060010
11static const char *debugfs_known_mountpoints[] = {
12 "/sys/kernel/debug/",
13 "/debug/",
14 0,
15};
16
Borislav Petkovfed12082013-02-20 16:32:27 +010017static bool debugfs_found;
Clark Williamsafe61f62009-11-08 09:01:37 -060018
19/* find the path to the mounted debugfs */
20const 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 Petkovfed12082013-02-20 16:32:27 +010032 debugfs_found = true;
Clark Williamsafe61f62009-11-08 09:01:37 -060033 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 Meloebf294b2011-11-16 14:03:07 -020042 return NULL;
Clark Williamsafe61f62009-11-08 09:01:37 -060043
Arnaldo Carvalho de Meloc168fbf2011-11-16 12:55:59 -020044 while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
Clark Williamsafe61f62009-11-08 09:01:37 -060045 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 Petkovfed12082013-02-20 16:32:27 +010054 debugfs_found = true;
Clark Williamsafe61f62009-11-08 09:01:37 -060055
56 return debugfs_mountpoint;
57}
58
59/* verify that a mountpoint is actually a debugfs instance */
60
61int 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 Meloebf294b2011-11-16 14:03:07 -020073static 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 Guangrong29c52aa2009-12-28 16:47:12 +080079/* mount the debugfs somewhere if it's not mounted */
Clark Williamsafe61f62009-11-08 09:01:37 -060080
Xiao Guangrong29c52aa2009-12-28 16:47:12 +080081char *debugfs_mount(const char *mountpoint)
Clark Williamsafe61f62009-11-08 09:01:37 -060082{
Clark Williamsafe61f62009-11-08 09:01:37 -060083 /* see if it's already mounted */
Borislav Petkovfed12082013-02-20 16:32:27 +010084 if (debugfs_find_mountpoint())
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -020085 goto out;
Clark Williamsafe61f62009-11-08 09:01:37 -060086
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 Guangrong29c52aa2009-12-28 16:47:12 +080096 if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0)
97 return NULL;
98
Clark Williamsafe61f62009-11-08 09:01:37 -060099 /* save the mountpoint */
Borislav Petkovfed12082013-02-20 16:32:27 +0100100 debugfs_found = true;
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200101 strncpy(debugfs_mountpoint, mountpoint, sizeof(debugfs_mountpoint));
102out:
103 debugfs_set_tracing_events_path(debugfs_mountpoint);
Xiao Guangrong29c52aa2009-12-28 16:47:12 +0800104 return debugfs_mountpoint;
Clark Williamsafe61f62009-11-08 09:01:37 -0600105}
106
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200107void debugfs_set_path(const char *mountpoint)
108{
109 snprintf(debugfs_mountpoint, sizeof(debugfs_mountpoint), "%s", mountpoint);
110 debugfs_set_tracing_events_path(mountpoint);
111}