debugfs.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. }