tracefs.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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/kernel.h>
  9. #include "tracefs.h"
  10. #ifndef TRACEFS_DEFAULT_PATH
  11. #define TRACEFS_DEFAULT_PATH "/sys/kernel/tracing"
  12. #endif
  13. char tracefs_mountpoint[PATH_MAX + 1] = TRACEFS_DEFAULT_PATH;
  14. static const char * const tracefs_known_mountpoints[] = {
  15. TRACEFS_DEFAULT_PATH,
  16. "/sys/kernel/debug/tracing",
  17. "/tracing",
  18. "/trace",
  19. 0,
  20. };
  21. static bool tracefs_found;
  22. /* find the path to the mounted tracefs */
  23. const char *tracefs_find_mountpoint(void)
  24. {
  25. const char *ret;
  26. if (tracefs_found)
  27. return (const char *)tracefs_mountpoint;
  28. ret = find_mountpoint("tracefs", (long) TRACEFS_MAGIC,
  29. tracefs_mountpoint, PATH_MAX + 1,
  30. tracefs_known_mountpoints);
  31. if (ret)
  32. tracefs_found = true;
  33. return ret;
  34. }
  35. /* mount the tracefs somewhere if it's not mounted */
  36. char *tracefs_mount(const char *mountpoint)
  37. {
  38. /* see if it's already mounted */
  39. if (tracefs_find_mountpoint())
  40. goto out;
  41. /* if not mounted and no argument */
  42. if (mountpoint == NULL) {
  43. /* see if environment variable set */
  44. mountpoint = getenv(PERF_TRACEFS_ENVIRONMENT);
  45. /* if no environment variable, use default */
  46. if (mountpoint == NULL)
  47. mountpoint = TRACEFS_DEFAULT_PATH;
  48. }
  49. if (mount(NULL, mountpoint, "tracefs", 0, NULL) < 0)
  50. return NULL;
  51. /* save the mountpoint */
  52. tracefs_found = true;
  53. strncpy(tracefs_mountpoint, mountpoint, sizeof(tracefs_mountpoint));
  54. out:
  55. return tracefs_mountpoint;
  56. }