Arnaldo Carvalho de Melo | e2726d9 | 2015-01-22 10:34:22 -0300 | [diff] [blame] | 1 | #define _GNU_SOURCE |
Borislav Petkov | 85c66be | 2013-02-20 16:32:30 +0100 | [diff] [blame] | 2 | #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 Melo | c168fbf | 2011-11-16 12:55:59 -0200 | [diff] [blame] | 8 | #include <sys/mount.h> |
Borislav Petkov | 85c66be | 2013-02-20 16:32:30 +0100 | [diff] [blame] | 9 | #include <linux/kernel.h> |
| 10 | |
| 11 | #include "debugfs.h" |
Arnaldo Carvalho de Melo | c168fbf | 2011-11-16 12:55:59 -0200 | [diff] [blame] | 12 | |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 13 | char debugfs_mountpoint[PATH_MAX + 1] = "/sys/kernel/debug"; |
Clark Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 14 | |
Borislav Petkov | 85c66be | 2013-02-20 16:32:30 +0100 | [diff] [blame] | 15 | static const char * const debugfs_known_mountpoints[] = { |
Xia Kaixu | 603940b | 2014-04-26 15:55:12 +0800 | [diff] [blame] | 16 | "/sys/kernel/debug", |
| 17 | "/debug", |
Clark Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 18 | 0, |
| 19 | }; |
| 20 | |
Borislav Petkov | fed1208 | 2013-02-20 16:32:27 +0100 | [diff] [blame] | 21 | static bool debugfs_found; |
Clark Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 22 | |
| 23 | /* find the path to the mounted debugfs */ |
| 24 | const char *debugfs_find_mountpoint(void) |
| 25 | { |
Steven Rostedt (Red Hat) | cde164a | 2015-02-02 14:35:03 -0500 | [diff] [blame^] | 26 | const char *ret; |
Clark Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 27 | |
| 28 | if (debugfs_found) |
Borislav Petkov | 85c66be | 2013-02-20 16:32:30 +0100 | [diff] [blame] | 29 | return (const char *)debugfs_mountpoint; |
Clark Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 30 | |
Steven Rostedt (Red Hat) | cde164a | 2015-02-02 14:35:03 -0500 | [diff] [blame^] | 31 | 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 Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 36 | |
Steven Rostedt (Red Hat) | cde164a | 2015-02-02 14:35:03 -0500 | [diff] [blame^] | 37 | return ret; |
Clark Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 38 | } |
| 39 | |
Xiao Guangrong | 29c52aa | 2009-12-28 16:47:12 +0800 | [diff] [blame] | 40 | /* mount the debugfs somewhere if it's not mounted */ |
Xiao Guangrong | 29c52aa | 2009-12-28 16:47:12 +0800 | [diff] [blame] | 41 | char *debugfs_mount(const char *mountpoint) |
Clark Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 42 | { |
Clark Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 43 | /* see if it's already mounted */ |
Borislav Petkov | fed1208 | 2013-02-20 16:32:27 +0100 | [diff] [blame] | 44 | if (debugfs_find_mountpoint()) |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 45 | goto out; |
Clark Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 46 | |
| 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 Guangrong | 29c52aa | 2009-12-28 16:47:12 +0800 | [diff] [blame] | 56 | if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0) |
| 57 | return NULL; |
| 58 | |
Clark Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 59 | /* save the mountpoint */ |
Borislav Petkov | fed1208 | 2013-02-20 16:32:27 +0100 | [diff] [blame] | 60 | debugfs_found = true; |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 61 | strncpy(debugfs_mountpoint, mountpoint, sizeof(debugfs_mountpoint)); |
| 62 | out: |
Xiao Guangrong | 29c52aa | 2009-12-28 16:47:12 +0800 | [diff] [blame] | 63 | return debugfs_mountpoint; |
Clark Williams | afe61f6 | 2009-11-08 09:01:37 -0600 | [diff] [blame] | 64 | } |
Arnaldo Carvalho de Melo | e2726d9 | 2015-01-22 10:34:22 -0300 | [diff] [blame] | 65 | |
Arnaldo Carvalho de Melo | 801c67b | 2015-01-22 10:52:55 -0300 | [diff] [blame] | 66 | int debugfs__strerror_open(int err, char *buf, size_t size, const char *filename) |
Arnaldo Carvalho de Melo | e2726d9 | 2015-01-22 10:34:22 -0300 | [diff] [blame] | 67 | { |
| 68 | char sbuf[128]; |
| 69 | |
| 70 | switch (err) { |
| 71 | case ENOENT: |
Arnaldo Carvalho de Melo | f816b3c | 2015-01-22 16:35:42 -0300 | [diff] [blame] | 72 | 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 Melo | e2726d9 | 2015-01-22 10:34:22 -0300 | [diff] [blame] | 79 | 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 Melo | 801c67b | 2015-01-22 10:52:55 -0300 | [diff] [blame] | 87 | "Error:\tNo permissions to read %s/%s\n" |
Arnaldo Carvalho de Melo | e2726d9 | 2015-01-22 10:34:22 -0300 | [diff] [blame] | 88 | "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n", |
Arnaldo Carvalho de Melo | 801c67b | 2015-01-22 10:52:55 -0300 | [diff] [blame] | 89 | debugfs_mountpoint, filename, debugfs_mountpoint); |
Arnaldo Carvalho de Melo | e2726d9 | 2015-01-22 10:34:22 -0300 | [diff] [blame] | 90 | 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 Melo | 2cc990b | 2015-01-22 11:13:43 -0300 | [diff] [blame] | 98 | |
| 99 | int 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 | } |