debugfs.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. /* use this to force a umount */
  15. void debugfs_force_cleanup(void)
  16. {
  17. debugfs_find_mountpoint();
  18. debugfs_premounted = 0;
  19. debugfs_umount();
  20. }
  21. /* construct a full path to a debugfs element */
  22. int debugfs_make_path(const char *element, char *buffer, int size)
  23. {
  24. int len;
  25. if (strlen(debugfs_mountpoint) == 0) {
  26. buffer[0] = '\0';
  27. return -1;
  28. }
  29. len = strlen(debugfs_mountpoint) + strlen(element) + 1;
  30. if (len >= size)
  31. return len+1;
  32. snprintf(buffer, size-1, "%s/%s", debugfs_mountpoint, element);
  33. return 0;
  34. }
  35. static int debugfs_found;
  36. /* find the path to the mounted debugfs */
  37. const char *debugfs_find_mountpoint(void)
  38. {
  39. const char **ptr;
  40. char type[100];
  41. FILE *fp;
  42. if (debugfs_found)
  43. return (const char *) debugfs_mountpoint;
  44. ptr = debugfs_known_mountpoints;
  45. while (*ptr) {
  46. if (debugfs_valid_mountpoint(*ptr) == 0) {
  47. debugfs_found = 1;
  48. strcpy(debugfs_mountpoint, *ptr);
  49. return debugfs_mountpoint;
  50. }
  51. ptr++;
  52. }
  53. /* give up and parse /proc/mounts */
  54. fp = fopen("/proc/mounts", "r");
  55. if (fp == NULL)
  56. return NULL;
  57. while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
  58. debugfs_mountpoint, type) == 2) {
  59. if (strcmp(type, "debugfs") == 0)
  60. break;
  61. }
  62. fclose(fp);
  63. if (strcmp(type, "debugfs") != 0)
  64. return NULL;
  65. debugfs_found = 1;
  66. return debugfs_mountpoint;
  67. }
  68. /* verify that a mountpoint is actually a debugfs instance */
  69. int debugfs_valid_mountpoint(const char *debugfs)
  70. {
  71. struct statfs st_fs;
  72. if (statfs(debugfs, &st_fs) < 0)
  73. return -ENOENT;
  74. else if (st_fs.f_type != (long) DEBUGFS_MAGIC)
  75. return -ENOENT;
  76. return 0;
  77. }
  78. int debugfs_valid_entry(const char *path)
  79. {
  80. struct stat st;
  81. if (stat(path, &st))
  82. return -errno;
  83. return 0;
  84. }
  85. static void debugfs_set_tracing_events_path(const char *mountpoint)
  86. {
  87. snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s",
  88. mountpoint, "tracing/events");
  89. }
  90. /* mount the debugfs somewhere if it's not mounted */
  91. char *debugfs_mount(const char *mountpoint)
  92. {
  93. /* see if it's already mounted */
  94. if (debugfs_find_mountpoint()) {
  95. debugfs_premounted = 1;
  96. goto out;
  97. }
  98. /* if not mounted and no argument */
  99. if (mountpoint == NULL) {
  100. /* see if environment variable set */
  101. mountpoint = getenv(PERF_DEBUGFS_ENVIRONMENT);
  102. /* if no environment variable, use default */
  103. if (mountpoint == NULL)
  104. mountpoint = "/sys/kernel/debug";
  105. }
  106. if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0)
  107. return NULL;
  108. /* save the mountpoint */
  109. debugfs_found = 1;
  110. strncpy(debugfs_mountpoint, mountpoint, sizeof(debugfs_mountpoint));
  111. out:
  112. debugfs_set_tracing_events_path(debugfs_mountpoint);
  113. return debugfs_mountpoint;
  114. }
  115. void debugfs_set_path(const char *mountpoint)
  116. {
  117. snprintf(debugfs_mountpoint, sizeof(debugfs_mountpoint), "%s", mountpoint);
  118. debugfs_set_tracing_events_path(mountpoint);
  119. }
  120. /* umount the debugfs */
  121. int debugfs_umount(void)
  122. {
  123. char umountcmd[128];
  124. int ret;
  125. /* if it was already mounted, leave it */
  126. if (debugfs_premounted)
  127. return 0;
  128. /* make sure it's a valid mount point */
  129. ret = debugfs_valid_mountpoint(debugfs_mountpoint);
  130. if (ret)
  131. return ret;
  132. snprintf(umountcmd, sizeof(umountcmd),
  133. "/bin/umount %s", debugfs_mountpoint);
  134. return system(umountcmd);
  135. }
  136. int debugfs_write(const char *entry, const char *value)
  137. {
  138. char path[PATH_MAX + 1];
  139. int ret, count;
  140. int fd;
  141. /* construct the path */
  142. snprintf(path, sizeof(path), "%s/%s", debugfs_mountpoint, entry);
  143. /* verify that it exists */
  144. ret = debugfs_valid_entry(path);
  145. if (ret)
  146. return ret;
  147. /* get how many chars we're going to write */
  148. count = strlen(value);
  149. /* open the debugfs entry */
  150. fd = open(path, O_RDWR);
  151. if (fd < 0)
  152. return -errno;
  153. while (count > 0) {
  154. /* write it */
  155. ret = write(fd, value, count);
  156. if (ret <= 0) {
  157. if (ret == EAGAIN)
  158. continue;
  159. close(fd);
  160. return -errno;
  161. }
  162. count -= ret;
  163. }
  164. /* close it */
  165. close(fd);
  166. /* return success */
  167. return 0;
  168. }
  169. /*
  170. * read a debugfs entry
  171. * returns the number of chars read or a negative errno
  172. */
  173. int debugfs_read(const char *entry, char *buffer, size_t size)
  174. {
  175. char path[PATH_MAX + 1];
  176. int ret;
  177. int fd;
  178. /* construct the path */
  179. snprintf(path, sizeof(path), "%s/%s", debugfs_mountpoint, entry);
  180. /* verify that it exists */
  181. ret = debugfs_valid_entry(path);
  182. if (ret)
  183. return ret;
  184. /* open the debugfs entry */
  185. fd = open(path, O_RDONLY);
  186. if (fd < 0)
  187. return -errno;
  188. do {
  189. /* read it */
  190. ret = read(fd, buffer, size);
  191. if (ret == 0) {
  192. close(fd);
  193. return EOF;
  194. }
  195. } while (ret < 0 && errno == EAGAIN);
  196. /* close it */
  197. close(fd);
  198. /* make *sure* there's a null character at the end */
  199. buffer[ret] = '\0';
  200. /* return the number of chars read */
  201. return ret;
  202. }