debugfs.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #define _GNU_SOURCE
  2. #include <errno.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <stdbool.h>
  8. #include <sys/vfs.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <sys/mount.h>
  12. #include <linux/kernel.h>
  13. #include "debugfs.h"
  14. #include "tracefs.h"
  15. #ifndef DEBUGFS_DEFAULT_PATH
  16. #define DEBUGFS_DEFAULT_PATH "/sys/kernel/debug"
  17. #endif
  18. char debugfs_mountpoint[PATH_MAX + 1] = DEBUGFS_DEFAULT_PATH;
  19. static const char * const debugfs_known_mountpoints[] = {
  20. DEBUGFS_DEFAULT_PATH,
  21. "/debug",
  22. 0,
  23. };
  24. static bool debugfs_found;
  25. bool debugfs_configured(void)
  26. {
  27. return debugfs_find_mountpoint() != NULL;
  28. }
  29. /* find the path to the mounted debugfs */
  30. const char *debugfs_find_mountpoint(void)
  31. {
  32. const char *ret;
  33. if (debugfs_found)
  34. return (const char *)debugfs_mountpoint;
  35. ret = find_mountpoint("debugfs", (long) DEBUGFS_MAGIC,
  36. debugfs_mountpoint, PATH_MAX + 1,
  37. debugfs_known_mountpoints);
  38. if (ret)
  39. debugfs_found = true;
  40. return ret;
  41. }
  42. /* mount the debugfs somewhere if it's not mounted */
  43. char *debugfs_mount(const char *mountpoint)
  44. {
  45. /* see if it's already mounted */
  46. if (debugfs_find_mountpoint())
  47. goto out;
  48. /* if not mounted and no argument */
  49. if (mountpoint == NULL) {
  50. /* see if environment variable set */
  51. mountpoint = getenv(PERF_DEBUGFS_ENVIRONMENT);
  52. /* if no environment variable, use default */
  53. if (mountpoint == NULL)
  54. mountpoint = DEBUGFS_DEFAULT_PATH;
  55. }
  56. if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0)
  57. return NULL;
  58. /* save the mountpoint */
  59. debugfs_found = true;
  60. strncpy(debugfs_mountpoint, mountpoint, sizeof(debugfs_mountpoint));
  61. out:
  62. return debugfs_mountpoint;
  63. }
  64. int debugfs__strerror_open(int err, char *buf, size_t size, const char *filename)
  65. {
  66. char sbuf[128];
  67. switch (err) {
  68. case ENOENT:
  69. if (debugfs_found) {
  70. snprintf(buf, size,
  71. "Error:\tFile %s/%s not found.\n"
  72. "Hint:\tPerhaps this kernel misses some CONFIG_ setting to enable this feature?.\n",
  73. debugfs_mountpoint, filename);
  74. break;
  75. }
  76. snprintf(buf, size, "%s",
  77. "Error:\tUnable to find debugfs\n"
  78. "Hint:\tWas your kernel compiled with debugfs support?\n"
  79. "Hint:\tIs the debugfs filesystem mounted?\n"
  80. "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
  81. break;
  82. case EACCES: {
  83. const char *mountpoint = debugfs_mountpoint;
  84. if (!access(debugfs_mountpoint, R_OK) && strncmp(filename, "tracing/", 8) == 0) {
  85. const char *tracefs_mntpoint = tracefs_find_mountpoint();
  86. if (tracefs_mntpoint)
  87. mountpoint = tracefs_mntpoint;
  88. }
  89. snprintf(buf, size,
  90. "Error:\tNo permissions to read %s/%s\n"
  91. "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
  92. debugfs_mountpoint, filename, mountpoint);
  93. }
  94. break;
  95. default:
  96. snprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf)));
  97. break;
  98. }
  99. return 0;
  100. }
  101. int debugfs__strerror_open_tp(int err, char *buf, size_t size, const char *sys, const char *name)
  102. {
  103. char path[PATH_MAX];
  104. snprintf(path, PATH_MAX, "tracing/events/%s/%s", sys, name ?: "*");
  105. return debugfs__strerror_open(err, buf, size, path);
  106. }