debugfs.c 2.6 KB

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