debugfs.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <errno.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdbool.h>
  6. #include <sys/vfs.h>
  7. #include <sys/mount.h>
  8. #include <linux/magic.h>
  9. #include <linux/kernel.h>
  10. #include "debugfs.h"
  11. char debugfs_mountpoint[PATH_MAX + 1] = "/sys/kernel/debug";
  12. char tracing_events_path[PATH_MAX + 1] = "/sys/kernel/debug/tracing/events";
  13. static const char * const debugfs_known_mountpoints[] = {
  14. "/sys/kernel/debug/",
  15. "/debug/",
  16. 0,
  17. };
  18. static bool debugfs_found;
  19. /* find the path to the mounted debugfs */
  20. const char *debugfs_find_mountpoint(void)
  21. {
  22. const char * const *ptr;
  23. char type[100];
  24. FILE *fp;
  25. if (debugfs_found)
  26. return (const char *)debugfs_mountpoint;
  27. ptr = debugfs_known_mountpoints;
  28. while (*ptr) {
  29. if (debugfs_valid_mountpoint(*ptr) == 0) {
  30. debugfs_found = true;
  31. strcpy(debugfs_mountpoint, *ptr);
  32. return debugfs_mountpoint;
  33. }
  34. ptr++;
  35. }
  36. /* give up and parse /proc/mounts */
  37. fp = fopen("/proc/mounts", "r");
  38. if (fp == NULL)
  39. return NULL;
  40. while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
  41. debugfs_mountpoint, type) == 2) {
  42. if (strcmp(type, "debugfs") == 0)
  43. break;
  44. }
  45. fclose(fp);
  46. if (strcmp(type, "debugfs") != 0)
  47. return NULL;
  48. debugfs_found = true;
  49. return debugfs_mountpoint;
  50. }
  51. /* verify that a mountpoint is actually a debugfs instance */
  52. int debugfs_valid_mountpoint(const char *debugfs)
  53. {
  54. struct statfs st_fs;
  55. if (statfs(debugfs, &st_fs) < 0)
  56. return -ENOENT;
  57. else if (st_fs.f_type != (long) DEBUGFS_MAGIC)
  58. return -ENOENT;
  59. return 0;
  60. }
  61. static void debugfs_set_tracing_events_path(const char *mountpoint)
  62. {
  63. snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s",
  64. mountpoint, "tracing/events");
  65. }
  66. /* mount the debugfs somewhere if it's not mounted */
  67. char *debugfs_mount(const char *mountpoint)
  68. {
  69. /* see if it's already mounted */
  70. if (debugfs_find_mountpoint())
  71. goto out;
  72. /* if not mounted and no argument */
  73. if (mountpoint == NULL) {
  74. /* see if environment variable set */
  75. mountpoint = getenv(PERF_DEBUGFS_ENVIRONMENT);
  76. /* if no environment variable, use default */
  77. if (mountpoint == NULL)
  78. mountpoint = "/sys/kernel/debug";
  79. }
  80. if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0)
  81. return NULL;
  82. /* save the mountpoint */
  83. debugfs_found = true;
  84. strncpy(debugfs_mountpoint, mountpoint, sizeof(debugfs_mountpoint));
  85. out:
  86. debugfs_set_tracing_events_path(debugfs_mountpoint);
  87. return debugfs_mountpoint;
  88. }
  89. void debugfs_set_path(const char *mountpoint)
  90. {
  91. snprintf(debugfs_mountpoint, sizeof(debugfs_mountpoint), "%s", mountpoint);
  92. debugfs_set_tracing_events_path(mountpoint);
  93. }