tracing_path.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #ifndef _GNU_SOURCE
  2. # define _GNU_SOURCE
  3. #endif
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <errno.h>
  8. #include <unistd.h>
  9. #include "fs.h"
  10. #include "tracing_path.h"
  11. char tracing_path[PATH_MAX + 1] = "/sys/kernel/debug/tracing";
  12. char tracing_events_path[PATH_MAX + 1] = "/sys/kernel/debug/tracing/events";
  13. static void __tracing_path_set(const char *tracing, const char *mountpoint)
  14. {
  15. snprintf(tracing_path, sizeof(tracing_path), "%s/%s",
  16. mountpoint, tracing);
  17. snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s%s",
  18. mountpoint, tracing, "events");
  19. }
  20. static const char *tracing_path_tracefs_mount(void)
  21. {
  22. const char *mnt;
  23. mnt = tracefs__mount();
  24. if (!mnt)
  25. return NULL;
  26. __tracing_path_set("", mnt);
  27. return mnt;
  28. }
  29. static const char *tracing_path_debugfs_mount(void)
  30. {
  31. const char *mnt;
  32. mnt = debugfs__mount();
  33. if (!mnt)
  34. return NULL;
  35. __tracing_path_set("tracing/", mnt);
  36. return mnt;
  37. }
  38. const char *tracing_path_mount(void)
  39. {
  40. const char *mnt;
  41. mnt = tracing_path_tracefs_mount();
  42. if (mnt)
  43. return mnt;
  44. mnt = tracing_path_debugfs_mount();
  45. return mnt;
  46. }
  47. void tracing_path_set(const char *mntpt)
  48. {
  49. __tracing_path_set("tracing/", mntpt);
  50. }
  51. char *get_tracing_file(const char *name)
  52. {
  53. char *file;
  54. if (asprintf(&file, "%s/%s", tracing_path, name) < 0)
  55. return NULL;
  56. return file;
  57. }
  58. void put_tracing_file(char *file)
  59. {
  60. free(file);
  61. }
  62. static int strerror_open(int err, char *buf, size_t size, const char *filename)
  63. {
  64. char sbuf[128];
  65. switch (err) {
  66. case ENOENT:
  67. /*
  68. * We will get here if we can't find the tracepoint, but one of
  69. * debugfs or tracefs is configured, which means you probably
  70. * want some tracepoint which wasn't compiled in your kernel.
  71. * - jirka
  72. */
  73. if (debugfs__configured() || tracefs__configured()) {
  74. snprintf(buf, size,
  75. "Error:\tFile %s/%s not found.\n"
  76. "Hint:\tPerhaps this kernel misses some CONFIG_ setting to enable this feature?.\n",
  77. tracing_events_path, filename);
  78. break;
  79. }
  80. snprintf(buf, size, "%s",
  81. "Error:\tUnable to find debugfs/tracefs\n"
  82. "Hint:\tWas your kernel compiled with debugfs/tracefs support?\n"
  83. "Hint:\tIs the debugfs/tracefs filesystem mounted?\n"
  84. "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
  85. break;
  86. case EACCES: {
  87. const char *mountpoint = debugfs__mountpoint();
  88. if (!access(mountpoint, R_OK) && strncmp(filename, "tracing/", 8) == 0) {
  89. const char *tracefs_mntpoint = tracefs__mountpoint();
  90. if (tracefs_mntpoint)
  91. mountpoint = tracefs__mountpoint();
  92. }
  93. snprintf(buf, size,
  94. "Error:\tNo permissions to read %s/%s\n"
  95. "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
  96. tracing_events_path, filename, mountpoint);
  97. }
  98. break;
  99. default:
  100. snprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf)));
  101. break;
  102. }
  103. return 0;
  104. }
  105. int tracing_path__strerror_open_tp(int err, char *buf, size_t size, const char *sys, const char *name)
  106. {
  107. char path[PATH_MAX];
  108. snprintf(path, PATH_MAX, "%s/%s", sys, name ?: "*");
  109. return strerror_open(err, buf, size, path);
  110. }