blob: 91e1668348ce6f254047736c167e8817a52c76e3 [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
23/* find the path to the mounted debugfs */
24const char *debugfs_find_mountpoint(void)
25{
Steven Rostedt (Red Hat)cde164a2015-02-02 14:35:03 -050026 const char *ret;
Clark Williamsafe61f62009-11-08 09:01:37 -060027
28 if (debugfs_found)
Borislav Petkov85c66be2013-02-20 16:32:30 +010029 return (const char *)debugfs_mountpoint;
Clark Williamsafe61f62009-11-08 09:01:37 -060030
Steven Rostedt (Red Hat)cde164a2015-02-02 14:35:03 -050031 ret = find_mountpoint("debugfs", (long) DEBUGFS_MAGIC,
32 debugfs_mountpoint, PATH_MAX + 1,
33 debugfs_known_mountpoints);
34 if (ret)
35 debugfs_found = true;
Clark Williamsafe61f62009-11-08 09:01:37 -060036
Steven Rostedt (Red Hat)cde164a2015-02-02 14:35:03 -050037 return ret;
Clark Williamsafe61f62009-11-08 09:01:37 -060038}
39
Xiao Guangrong29c52aa2009-12-28 16:47:12 +080040/* mount the debugfs somewhere if it's not mounted */
Xiao Guangrong29c52aa2009-12-28 16:47:12 +080041char *debugfs_mount(const char *mountpoint)
Clark Williamsafe61f62009-11-08 09:01:37 -060042{
Clark Williamsafe61f62009-11-08 09:01:37 -060043 /* see if it's already mounted */
Borislav Petkovfed12082013-02-20 16:32:27 +010044 if (debugfs_find_mountpoint())
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -020045 goto out;
Clark Williamsafe61f62009-11-08 09:01:37 -060046
47 /* if not mounted and no argument */
48 if (mountpoint == NULL) {
49 /* see if environment variable set */
50 mountpoint = getenv(PERF_DEBUGFS_ENVIRONMENT);
51 /* if no environment variable, use default */
52 if (mountpoint == NULL)
53 mountpoint = "/sys/kernel/debug";
54 }
55
Xiao Guangrong29c52aa2009-12-28 16:47:12 +080056 if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0)
57 return NULL;
58
Clark Williamsafe61f62009-11-08 09:01:37 -060059 /* save the mountpoint */
Borislav Petkovfed12082013-02-20 16:32:27 +010060 debugfs_found = true;
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -020061 strncpy(debugfs_mountpoint, mountpoint, sizeof(debugfs_mountpoint));
62out:
Xiao Guangrong29c52aa2009-12-28 16:47:12 +080063 return debugfs_mountpoint;
Clark Williamsafe61f62009-11-08 09:01:37 -060064}
Arnaldo Carvalho de Meloe2726d92015-01-22 10:34:22 -030065
Arnaldo Carvalho de Melo801c67b2015-01-22 10:52:55 -030066int debugfs__strerror_open(int err, char *buf, size_t size, const char *filename)
Arnaldo Carvalho de Meloe2726d92015-01-22 10:34:22 -030067{
68 char sbuf[128];
69
70 switch (err) {
71 case ENOENT:
Arnaldo Carvalho de Melof816b3c2015-01-22 16:35:42 -030072 if (debugfs_found) {
73 snprintf(buf, size,
74 "Error:\tFile %s/%s not found.\n"
75 "Hint:\tPerhaps this kernel misses some CONFIG_ setting to enable this feature?.\n",
76 debugfs_mountpoint, filename);
77 break;
78 }
Arnaldo Carvalho de Meloe2726d92015-01-22 10:34:22 -030079 snprintf(buf, size, "%s",
80 "Error:\tUnable to find debugfs\n"
81 "Hint:\tWas your kernel compiled with debugfs support?\n"
82 "Hint:\tIs the debugfs filesystem mounted?\n"
83 "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
84 break;
85 case EACCES:
86 snprintf(buf, size,
Arnaldo Carvalho de Melo801c67b2015-01-22 10:52:55 -030087 "Error:\tNo permissions to read %s/%s\n"
Arnaldo Carvalho de Meloe2726d92015-01-22 10:34:22 -030088 "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
Arnaldo Carvalho de Melo801c67b2015-01-22 10:52:55 -030089 debugfs_mountpoint, filename, debugfs_mountpoint);
Arnaldo Carvalho de Meloe2726d92015-01-22 10:34:22 -030090 break;
91 default:
92 snprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf)));
93 break;
94 }
95
96 return 0;
97}
Arnaldo Carvalho de Melo2cc990b2015-01-22 11:13:43 -030098
99int debugfs__strerror_open_tp(int err, char *buf, size_t size, const char *sys, const char *name)
100{
101 char path[PATH_MAX];
102
103 snprintf(path, PATH_MAX, "tracing/events/%s/%s", sys, name ?: "*");
104
105 return debugfs__strerror_open(err, buf, size, path);
106}