1 #define _GNU_SOURCE 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> 8 #include <sys/mount.h> 9 #include <linux/kernel.h> 10 11 #include "debugfs.h" 12 13 char debugfs_mountpoint[PATH_MAX + 1] = "/sys/kernel/debug"; 14 15 static const char * const debugfs_known_mountpoints[] = { 16 "/sys/kernel/debug", 17 "/debug", 18 0, 19 }; 20 21 static bool debugfs_found; 22 23 /* find the path to the mounted debugfs */ 24 const char *debugfs_find_mountpoint(void) 25 { 26 const char * const *ptr; 27 char type[100]; 28 FILE *fp; 29 30 if (debugfs_found) 31 return (const char *)debugfs_mountpoint; 32 33 ptr = debugfs_known_mountpoints; 34 while (*ptr) { 35 if (debugfs_valid_mountpoint(*ptr) == 0) { 36 debugfs_found = true; 37 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) 46 return NULL; 47 48 while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n", 49 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 58 debugfs_found = true; 59 60 return debugfs_mountpoint; 61 } 62 63 /* verify that a mountpoint is actually a debugfs instance */ 64 65 int 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 ((long)st_fs.f_type != (long)DEBUGFS_MAGIC) 72 return -ENOENT; 73 74 return 0; 75 } 76 77 /* mount the debugfs somewhere if it's not mounted */ 78 char *debugfs_mount(const char *mountpoint) 79 { 80 /* see if it's already mounted */ 81 if (debugfs_find_mountpoint()) 82 goto out; 83 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 93 if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0) 94 return NULL; 95 96 /* save the mountpoint */ 97 debugfs_found = true; 98 strncpy(debugfs_mountpoint, mountpoint, sizeof(debugfs_mountpoint)); 99 out: 100 return debugfs_mountpoint; 101 } 102 103 int debugfs__strerror_open(int err, char *buf, size_t size, const char *filename) 104 { 105 char sbuf[128]; 106 107 switch (err) { 108 case ENOENT: 109 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 } 116 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, 124 "Error:\tNo permissions to read %s/%s\n" 125 "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n", 126 debugfs_mountpoint, filename, debugfs_mountpoint); 127 break; 128 default: 129 snprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf))); 130 break; 131 } 132 133 return 0; 134 } 135 136 int 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 } 144
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.