debugfs.c 2.5 KB

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