blob: d21d4d6b4fd2c79e5fe3b3ade3edc1dbec033c97 [file] [log] [blame]
Arnaldo Carvalho de Meloe2726d92015-01-22 10:34:22 -03001#define _GNU_SOURCE
Borislav Petkov85c66be2013-02-20 16:32:30 +01002#include <errno.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6#include <stdbool.h>
7#include <sys/vfs.h>
Arnaldo Carvalho de Meloc168fbf2011-11-16 12:55:59 -02008#include <sys/mount.h>
Borislav Petkov85c66be2013-02-20 16:32:30 +01009#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[] = {
Xia Kaixu603940b2014-04-26 15:55:12 +080016 "/sys/kernel/debug",
17 "/debug",
Clark Williamsafe61f62009-11-08 09:01:37 -060018 0,
19};
20
Borislav Petkovfed12082013-02-20 16:32:27 +010021static bool debugfs_found;
Clark Williamsafe61f62009-11-08 09:01:37 -060022
Steven Rostedt (Red Hat)5693c922015-02-02 14:35:02 -050023/* verify that a mountpoint is actually a debugfs instance */
24
25static int debugfs_valid_mountpoint(const char *debugfs)
26{
27 struct statfs st_fs;
28
29 if (statfs(debugfs, &st_fs) < 0)
30 return -ENOENT;
31 else if ((long)st_fs.f_type != (long)DEBUGFS_MAGIC)
32 return -ENOENT;
33
34 return 0;
35}
36
Clark Williamsafe61f62009-11-08 09:01:37 -060037/* find the path to the mounted debugfs */
38const char *debugfs_find_mountpoint(void)
39{
Borislav Petkov85c66be2013-02-20 16:32:30 +010040 const char * const *ptr;
Clark Williamsafe61f62009-11-08 09:01:37 -060041 char type[100];
42 FILE *fp;
43
44 if (debugfs_found)
Borislav Petkov85c66be2013-02-20 16:32:30 +010045 return (const char *)debugfs_mountpoint;
Clark Williamsafe61f62009-11-08 09:01:37 -060046
47 ptr = debugfs_known_mountpoints;
48 while (*ptr) {
49 if (debugfs_valid_mountpoint(*ptr) == 0) {
Borislav Petkovfed12082013-02-20 16:32:27 +010050 debugfs_found = true;
Clark Williamsafe61f62009-11-08 09:01:37 -060051 strcpy(debugfs_mountpoint, *ptr);
52 return debugfs_mountpoint;
53 }
54 ptr++;
55 }
56
57 /* give up and parse /proc/mounts */
58 fp = fopen("/proc/mounts", "r");
59 if (fp == NULL)
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -020060 return NULL;
Clark Williamsafe61f62009-11-08 09:01:37 -060061
Arnaldo Carvalho de Meloc168fbf2011-11-16 12:55:59 -020062 while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
Clark Williamsafe61f62009-11-08 09:01:37 -060063 debugfs_mountpoint, type) == 2) {
64 if (strcmp(type, "debugfs") == 0)
65 break;
66 }
67 fclose(fp);
68
69 if (strcmp(type, "debugfs") != 0)
70 return NULL;
71
Borislav Petkovfed12082013-02-20 16:32:27 +010072 debugfs_found = true;
Clark Williamsafe61f62009-11-08 09:01:37 -060073
74 return debugfs_mountpoint;
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}
Arnaldo Carvalho de Meloe2726d92015-01-22 10:34:22 -0300102
Arnaldo Carvalho de Melo801c67b2015-01-22 10:52:55 -0300103int debugfs__strerror_open(int err, char *buf, size_t size, const char *filename)
Arnaldo Carvalho de Meloe2726d92015-01-22 10:34:22 -0300104{
105 char sbuf[128];
106
107 switch (err) {
108 case ENOENT:
Arnaldo Carvalho de Melof816b3c2015-01-22 16:35:42 -0300109 if (debugfs_found) {
110 snprintf(buf, size,
111 "Error:\tFile %s/%s not found.\n"
112 "Hint:\tPerhaps this kernel misses some CONFIG_ setting to enable this feature?.\n",
113 debugfs_mountpoint, filename);
114 break;
115 }
Arnaldo Carvalho de Meloe2726d92015-01-22 10:34:22 -0300116 snprintf(buf, size, "%s",
117 "Error:\tUnable to find debugfs\n"
118 "Hint:\tWas your kernel compiled with debugfs support?\n"
119 "Hint:\tIs the debugfs filesystem mounted?\n"
120 "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
121 break;
122 case EACCES:
123 snprintf(buf, size,
Arnaldo Carvalho de Melo801c67b2015-01-22 10:52:55 -0300124 "Error:\tNo permissions to read %s/%s\n"
Arnaldo Carvalho de Meloe2726d92015-01-22 10:34:22 -0300125 "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
Arnaldo Carvalho de Melo801c67b2015-01-22 10:52:55 -0300126 debugfs_mountpoint, filename, debugfs_mountpoint);
Arnaldo Carvalho de Meloe2726d92015-01-22 10:34:22 -0300127 break;
128 default:
129 snprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf)));
130 break;
131 }
132
133 return 0;
134}
Arnaldo Carvalho de Melo2cc990b2015-01-22 11:13:43 -0300135
136int debugfs__strerror_open_tp(int err, char *buf, size_t size, const char *sys, const char *name)
137{
138 char path[PATH_MAX];
139
140 snprintf(path, PATH_MAX, "tracing/events/%s/%s", sys, name ?: "*");
141
142 return debugfs__strerror_open(err, buf, size, path);
143}