tracefs.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include <errno.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <stdbool.h>
  7. #include <sys/vfs.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <sys/mount.h>
  11. #include <linux/kernel.h>
  12. #include "tracefs.h"
  13. #ifndef TRACEFS_DEFAULT_PATH
  14. #define TRACEFS_DEFAULT_PATH "/sys/kernel/tracing"
  15. #endif
  16. char tracefs_mountpoint[PATH_MAX + 1] = TRACEFS_DEFAULT_PATH;
  17. static const char * const tracefs_known_mountpoints[] = {
  18. TRACEFS_DEFAULT_PATH,
  19. "/sys/kernel/debug/tracing",
  20. "/tracing",
  21. "/trace",
  22. 0,
  23. };
  24. static bool tracefs_found;
  25. bool tracefs_configured(void)
  26. {
  27. return tracefs_find_mountpoint() != NULL;
  28. }
  29. /* find the path to the mounted tracefs */
  30. const char *tracefs_find_mountpoint(void)
  31. {
  32. const char *ret;
  33. if (tracefs_found)
  34. return (const char *)tracefs_mountpoint;
  35. ret = find_mountpoint("tracefs", (long) TRACEFS_MAGIC,
  36. tracefs_mountpoint, PATH_MAX + 1,
  37. tracefs_known_mountpoints);
  38. if (ret)
  39. tracefs_found = true;
  40. return ret;
  41. }
  42. /* mount the tracefs somewhere if it's not mounted */
  43. char *tracefs_mount(const char *mountpoint)
  44. {
  45. /* see if it's already mounted */
  46. if (tracefs_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_TRACEFS_ENVIRONMENT);
  52. /* if no environment variable, use default */
  53. if (mountpoint == NULL)
  54. mountpoint = TRACEFS_DEFAULT_PATH;
  55. }
  56. if (mount(NULL, mountpoint, "tracefs", 0, NULL) < 0)
  57. return NULL;
  58. /* save the mountpoint */
  59. tracefs_found = true;
  60. strncpy(tracefs_mountpoint, mountpoint, sizeof(tracefs_mountpoint));
  61. out:
  62. return tracefs_mountpoint;
  63. }