blob: 7c43479623537af4f0d4179f4cce195cbea3e9b1 [file] [log] [blame]
Borislav Petkov85c66be2013-02-20 16:32:30 +01001#include <errno.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <stdbool.h>
6#include <sys/vfs.h>
Arnaldo Carvalho de Meloc168fbf2011-11-16 12:55:59 -02007#include <sys/mount.h>
Borislav Petkov85c66be2013-02-20 16:32:30 +01008#include <linux/kernel.h>
9
10#include "debugfs.h"
Arnaldo Carvalho de Meloc168fbf2011-11-16 12:55:59 -020011
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -020012char debugfs_mountpoint[PATH_MAX + 1] = "/sys/kernel/debug";
Clark Williamsafe61f62009-11-08 09:01:37 -060013
Borislav Petkov85c66be2013-02-20 16:32:30 +010014static const char * const debugfs_known_mountpoints[] = {
Clark Williamsafe61f62009-11-08 09:01:37 -060015 "/sys/kernel/debug/",
16 "/debug/",
17 0,
18};
19
Borislav Petkovfed12082013-02-20 16:32:27 +010020static bool debugfs_found;
Clark Williamsafe61f62009-11-08 09:01:37 -060021
22/* find the path to the mounted debugfs */
23const char *debugfs_find_mountpoint(void)
24{
Borislav Petkov85c66be2013-02-20 16:32:30 +010025 const char * const *ptr;
Clark Williamsafe61f62009-11-08 09:01:37 -060026 char type[100];
27 FILE *fp;
28
29 if (debugfs_found)
Borislav Petkov85c66be2013-02-20 16:32:30 +010030 return (const char *)debugfs_mountpoint;
Clark Williamsafe61f62009-11-08 09:01:37 -060031
32 ptr = debugfs_known_mountpoints;
33 while (*ptr) {
34 if (debugfs_valid_mountpoint(*ptr) == 0) {
Borislav Petkovfed12082013-02-20 16:32:27 +010035 debugfs_found = true;
Clark Williamsafe61f62009-11-08 09:01:37 -060036 strcpy(debugfs_mountpoint, *ptr);
37 return debugfs_mountpoint;
38 }
39 ptr++;
40 }
41
42 /* give up and parse /proc/mounts */
43 fp = fopen("/proc/mounts", "r");
44 if (fp == NULL)
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -020045 return NULL;
Clark Williamsafe61f62009-11-08 09:01:37 -060046
Arnaldo Carvalho de Meloc168fbf2011-11-16 12:55:59 -020047 while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
Clark Williamsafe61f62009-11-08 09:01:37 -060048 debugfs_mountpoint, type) == 2) {
49 if (strcmp(type, "debugfs") == 0)
50 break;
51 }
52 fclose(fp);
53
54 if (strcmp(type, "debugfs") != 0)
55 return NULL;
56
Borislav Petkovfed12082013-02-20 16:32:27 +010057 debugfs_found = true;
Clark Williamsafe61f62009-11-08 09:01:37 -060058
59 return debugfs_mountpoint;
60}
61
62/* verify that a mountpoint is actually a debugfs instance */
63
64int debugfs_valid_mountpoint(const char *debugfs)
65{
66 struct statfs st_fs;
67
68 if (statfs(debugfs, &st_fs) < 0)
69 return -ENOENT;
70 else if (st_fs.f_type != (long) DEBUGFS_MAGIC)
71 return -ENOENT;
72
73 return 0;
74}
75
Xiao Guangrong29c52aa2009-12-28 16:47:12 +080076/* mount the debugfs somewhere if it's not mounted */
Xiao Guangrong29c52aa2009-12-28 16:47:12 +080077char *debugfs_mount(const char *mountpoint)
Clark Williamsafe61f62009-11-08 09:01:37 -060078{
Clark Williamsafe61f62009-11-08 09:01:37 -060079 /* see if it's already mounted */
Borislav Petkovfed12082013-02-20 16:32:27 +010080 if (debugfs_find_mountpoint())
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -020081 goto out;
Clark Williamsafe61f62009-11-08 09:01:37 -060082
83 /* if not mounted and no argument */
84 if (mountpoint == NULL) {
85 /* see if environment variable set */
86 mountpoint = getenv(PERF_DEBUGFS_ENVIRONMENT);
87 /* if no environment variable, use default */
88 if (mountpoint == NULL)
89 mountpoint = "/sys/kernel/debug";
90 }
91
Xiao Guangrong29c52aa2009-12-28 16:47:12 +080092 if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0)
93 return NULL;
94
Clark Williamsafe61f62009-11-08 09:01:37 -060095 /* save the mountpoint */
Borislav Petkovfed12082013-02-20 16:32:27 +010096 debugfs_found = true;
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -020097 strncpy(debugfs_mountpoint, mountpoint, sizeof(debugfs_mountpoint));
98out:
Xiao Guangrong29c52aa2009-12-28 16:47:12 +080099 return debugfs_mountpoint;
Clark Williamsafe61f62009-11-08 09:01:37 -0600100}