blob: 099e7cd022e46d47260103b226b3410beac27133 [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/magic.h>
9#include <linux/kernel.h>
10
11#include "debugfs.h"
Arnaldo Carvalho de Meloc168fbf2011-11-16 12:55:59 -020012
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -020013char debugfs_mountpoint[PATH_MAX + 1] = "/sys/kernel/debug";
Clark Williamsafe61f62009-11-08 09:01:37 -060014
Borislav Petkov85c66be2013-02-20 16:32:30 +010015static const char * const debugfs_known_mountpoints[] = {
Clark Williamsafe61f62009-11-08 09:01:37 -060016 "/sys/kernel/debug/",
17 "/debug/",
18 0,
19};
20
Borislav Petkovfed12082013-02-20 16:32:27 +010021static bool debugfs_found;
Clark Williamsafe61f62009-11-08 09:01:37 -060022
23/* find the path to the mounted debugfs */
24const char *debugfs_find_mountpoint(void)
25{
Borislav Petkov85c66be2013-02-20 16:32:30 +010026 const char * const *ptr;
Clark Williamsafe61f62009-11-08 09:01:37 -060027 char type[100];
28 FILE *fp;
29
30 if (debugfs_found)
Borislav Petkov85c66be2013-02-20 16:32:30 +010031 return (const char *)debugfs_mountpoint;
Clark Williamsafe61f62009-11-08 09:01:37 -060032
33 ptr = debugfs_known_mountpoints;
34 while (*ptr) {
35 if (debugfs_valid_mountpoint(*ptr) == 0) {
Borislav Petkovfed12082013-02-20 16:32:27 +010036 debugfs_found = true;
Clark Williamsafe61f62009-11-08 09:01:37 -060037 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)
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -020046 return NULL;
Clark Williamsafe61f62009-11-08 09:01:37 -060047
Arnaldo Carvalho de Meloc168fbf2011-11-16 12:55:59 -020048 while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
Clark Williamsafe61f62009-11-08 09:01:37 -060049 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
Borislav Petkovfed12082013-02-20 16:32:27 +010058 debugfs_found = true;
Clark Williamsafe61f62009-11-08 09:01:37 -060059
60 return debugfs_mountpoint;
61}
62
63/* verify that a mountpoint is actually a debugfs instance */
64
65int 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
Xiao Guangrong29c52aa2009-12-28 16:47:12 +080077/* mount the debugfs somewhere if it's not mounted */
Xiao Guangrong29c52aa2009-12-28 16:47:12 +080078char *debugfs_mount(const char *mountpoint)
Clark Williamsafe61f62009-11-08 09:01:37 -060079{
Clark Williamsafe61f62009-11-08 09:01:37 -060080 /* see if it's already mounted */
Borislav Petkovfed12082013-02-20 16:32:27 +010081 if (debugfs_find_mountpoint())
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -020082 goto out;
Clark Williamsafe61f62009-11-08 09:01:37 -060083
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
Xiao Guangrong29c52aa2009-12-28 16:47:12 +080093 if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0)
94 return NULL;
95
Clark Williamsafe61f62009-11-08 09:01:37 -060096 /* save the mountpoint */
Borislav Petkovfed12082013-02-20 16:32:27 +010097 debugfs_found = true;
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -020098 strncpy(debugfs_mountpoint, mountpoint, sizeof(debugfs_mountpoint));
99out:
Xiao Guangrong29c52aa2009-12-28 16:47:12 +0800100 return debugfs_mountpoint;
Clark Williamsafe61f62009-11-08 09:01:37 -0600101}