blob: 8305b3e9d48e29da0152592524af2e364a3b92f9 [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>
Steven Rostedt (Red Hat)dd6dda22015-02-02 14:35:06 -05006#include <unistd.h>
Borislav Petkov85c66be2013-02-20 16:32:30 +01007#include <stdbool.h>
8#include <sys/vfs.h>
Steven Rostedt (Red Hat)dd6dda22015-02-02 14:35:06 -05009#include <sys/types.h>
10#include <sys/stat.h>
Arnaldo Carvalho de Meloc168fbf2011-11-16 12:55:59 -020011#include <sys/mount.h>
Borislav Petkov85c66be2013-02-20 16:32:30 +010012#include <linux/kernel.h>
13
14#include "debugfs.h"
Arnaldo Carvalho de Meloc168fbf2011-11-16 12:55:59 -020015
Steven Rostedt (Red Hat)a9edf602015-02-02 14:35:05 -050016#ifndef DEBUGFS_DEFAULT_PATH
17#define DEBUGFS_DEFAULT_PATH "/sys/kernel/debug"
18#endif
19
20char debugfs_mountpoint[PATH_MAX + 1] = DEBUGFS_DEFAULT_PATH;
Clark Williamsafe61f62009-11-08 09:01:37 -060021
Borislav Petkov85c66be2013-02-20 16:32:30 +010022static const char * const debugfs_known_mountpoints[] = {
Steven Rostedt (Red Hat)a9edf602015-02-02 14:35:05 -050023 DEBUGFS_DEFAULT_PATH,
Xia Kaixu603940b2014-04-26 15:55:12 +080024 "/debug",
Clark Williamsafe61f62009-11-08 09:01:37 -060025 0,
26};
27
Borislav Petkovfed12082013-02-20 16:32:27 +010028static bool debugfs_found;
Clark Williamsafe61f62009-11-08 09:01:37 -060029
Steven Rostedt (Red Hat)dd6dda22015-02-02 14:35:06 -050030bool debugfs_configured(void)
31{
32 return debugfs_find_mountpoint() != NULL;
33}
34
Clark Williamsafe61f62009-11-08 09:01:37 -060035/* find the path to the mounted debugfs */
36const char *debugfs_find_mountpoint(void)
37{
Steven Rostedt (Red Hat)cde164a2015-02-02 14:35:03 -050038 const char *ret;
Clark Williamsafe61f62009-11-08 09:01:37 -060039
40 if (debugfs_found)
Borislav Petkov85c66be2013-02-20 16:32:30 +010041 return (const char *)debugfs_mountpoint;
Clark Williamsafe61f62009-11-08 09:01:37 -060042
Steven Rostedt (Red Hat)cde164a2015-02-02 14:35:03 -050043 ret = find_mountpoint("debugfs", (long) DEBUGFS_MAGIC,
44 debugfs_mountpoint, PATH_MAX + 1,
45 debugfs_known_mountpoints);
46 if (ret)
47 debugfs_found = true;
Clark Williamsafe61f62009-11-08 09:01:37 -060048
Steven Rostedt (Red Hat)cde164a2015-02-02 14:35:03 -050049 return ret;
Clark Williamsafe61f62009-11-08 09:01:37 -060050}
51
Xiao Guangrong29c52aa2009-12-28 16:47:12 +080052/* mount the debugfs somewhere if it's not mounted */
Xiao Guangrong29c52aa2009-12-28 16:47:12 +080053char *debugfs_mount(const char *mountpoint)
Clark Williamsafe61f62009-11-08 09:01:37 -060054{
Clark Williamsafe61f62009-11-08 09:01:37 -060055 /* see if it's already mounted */
Borislav Petkovfed12082013-02-20 16:32:27 +010056 if (debugfs_find_mountpoint())
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -020057 goto out;
Clark Williamsafe61f62009-11-08 09:01:37 -060058
59 /* if not mounted and no argument */
60 if (mountpoint == NULL) {
61 /* see if environment variable set */
62 mountpoint = getenv(PERF_DEBUGFS_ENVIRONMENT);
63 /* if no environment variable, use default */
64 if (mountpoint == NULL)
Steven Rostedt (Red Hat)a9edf602015-02-02 14:35:05 -050065 mountpoint = DEBUGFS_DEFAULT_PATH;
Clark Williamsafe61f62009-11-08 09:01:37 -060066 }
67
Xiao Guangrong29c52aa2009-12-28 16:47:12 +080068 if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0)
69 return NULL;
70
Clark Williamsafe61f62009-11-08 09:01:37 -060071 /* save the mountpoint */
Borislav Petkovfed12082013-02-20 16:32:27 +010072 debugfs_found = true;
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -020073 strncpy(debugfs_mountpoint, mountpoint, sizeof(debugfs_mountpoint));
74out:
Xiao Guangrong29c52aa2009-12-28 16:47:12 +080075 return debugfs_mountpoint;
Clark Williamsafe61f62009-11-08 09:01:37 -060076}
Arnaldo Carvalho de Meloe2726d92015-01-22 10:34:22 -030077
Arnaldo Carvalho de Melo801c67b2015-01-22 10:52:55 -030078int debugfs__strerror_open(int err, char *buf, size_t size, const char *filename)
Arnaldo Carvalho de Meloe2726d92015-01-22 10:34:22 -030079{
80 char sbuf[128];
81
82 switch (err) {
83 case ENOENT:
Arnaldo Carvalho de Melof816b3c2015-01-22 16:35:42 -030084 if (debugfs_found) {
85 snprintf(buf, size,
86 "Error:\tFile %s/%s not found.\n"
87 "Hint:\tPerhaps this kernel misses some CONFIG_ setting to enable this feature?.\n",
88 debugfs_mountpoint, filename);
89 break;
90 }
Arnaldo Carvalho de Meloe2726d92015-01-22 10:34:22 -030091 snprintf(buf, size, "%s",
92 "Error:\tUnable to find debugfs\n"
93 "Hint:\tWas your kernel compiled with debugfs support?\n"
94 "Hint:\tIs the debugfs filesystem mounted?\n"
95 "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
96 break;
97 case EACCES:
98 snprintf(buf, size,
Arnaldo Carvalho de Melo801c67b2015-01-22 10:52:55 -030099 "Error:\tNo permissions to read %s/%s\n"
Arnaldo Carvalho de Meloe2726d92015-01-22 10:34:22 -0300100 "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
Arnaldo Carvalho de Melo801c67b2015-01-22 10:52:55 -0300101 debugfs_mountpoint, filename, debugfs_mountpoint);
Arnaldo Carvalho de Meloe2726d92015-01-22 10:34:22 -0300102 break;
103 default:
104 snprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf)));
105 break;
106 }
107
108 return 0;
109}
Arnaldo Carvalho de Melo2cc990b2015-01-22 11:13:43 -0300110
111int debugfs__strerror_open_tp(int err, char *buf, size_t size, const char *sys, const char *name)
112{
113 char path[PATH_MAX];
114
115 snprintf(path, PATH_MAX, "tracing/events/%s/%s", sys, name ?: "*");
116
117 return debugfs__strerror_open(err, buf, size, path);
118}