tracing_path.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // SPDX-License-Identifier: GPL-2.0
  2. #ifndef _GNU_SOURCE
  3. # define _GNU_SOURCE
  4. #endif
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <linux/string.h>
  9. #include <errno.h>
  10. #include <unistd.h>
  11. #include "fs.h"
  12. #include "tracing_path.h"
  13. static char tracing_mnt[PATH_MAX] = "/sys/kernel/debug";
  14. static char tracing_path[PATH_MAX] = "/sys/kernel/debug/tracing";
  15. static char tracing_events_path[PATH_MAX] = "/sys/kernel/debug/tracing/events";
  16. static void __tracing_path_set(const char *tracing, const char *mountpoint)
  17. {
  18. snprintf(tracing_mnt, sizeof(tracing_mnt), "%s", mountpoint);
  19. snprintf(tracing_path, sizeof(tracing_path), "%s/%s",
  20. mountpoint, tracing);
  21. snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s%s",
  22. mountpoint, tracing, "events");
  23. }
  24. static const char *tracing_path_tracefs_mount(void)
  25. {
  26. const char *mnt;
  27. mnt = tracefs__mount();
  28. if (!mnt)
  29. return NULL;
  30. __tracing_path_set("", mnt);
  31. return tracing_path;
  32. }
  33. static const char *tracing_path_debugfs_mount(void)
  34. {
  35. const char *mnt;
  36. mnt = debugfs__mount();
  37. if (!mnt)
  38. return NULL;
  39. __tracing_path_set("tracing/", mnt);
  40. return tracing_path;
  41. }
  42. const char *tracing_path_mount(void)
  43. {
  44. const char *mnt;
  45. mnt = tracing_path_tracefs_mount();
  46. if (mnt)
  47. return mnt;
  48. mnt = tracing_path_debugfs_mount();
  49. return mnt;
  50. }
  51. void tracing_path_set(const char *mntpt)
  52. {
  53. __tracing_path_set("tracing/", mntpt);
  54. }
  55. char *get_tracing_file(const char *name)
  56. {
  57. char *file;
  58. if (asprintf(&file, "%s/%s", tracing_path_mount(), name) < 0)
  59. return NULL;
  60. return file;
  61. }
  62. void put_tracing_file(char *file)
  63. {
  64. free(file);
  65. }
  66. char *get_events_file(const char *name)
  67. {
  68. char *file;
  69. if (asprintf(&file, "%s/events/%s", tracing_path_mount(), name) < 0)
  70. return NULL;
  71. return file;
  72. }
  73. void put_events_file(char *file)
  74. {
  75. free(file);
  76. }
  77. DIR *tracing_events__opendir(void)
  78. {
  79. DIR *dir = NULL;
  80. char *path = get_tracing_file("events");
  81. if (path) {
  82. dir = opendir(path);
  83. put_events_file(path);
  84. }
  85. return dir;
  86. }
  87. int tracing_path__strerror_open_tp(int err, char *buf, size_t size,
  88. const char *sys, const char *name)
  89. {
  90. char sbuf[128];
  91. char filename[PATH_MAX];
  92. snprintf(filename, PATH_MAX, "%s/%s", sys, name ?: "*");
  93. switch (err) {
  94. case ENOENT:
  95. /*
  96. * We will get here if we can't find the tracepoint, but one of
  97. * debugfs or tracefs is configured, which means you probably
  98. * want some tracepoint which wasn't compiled in your kernel.
  99. * - jirka
  100. */
  101. if (debugfs__configured() || tracefs__configured()) {
  102. /* sdt markers */
  103. if (!strncmp(filename, "sdt_", 4)) {
  104. snprintf(buf, size,
  105. "Error:\tFile %s/%s not found.\n"
  106. "Hint:\tSDT event cannot be directly recorded on.\n"
  107. "\tPlease first use 'perf probe %s:%s' before recording it.\n",
  108. tracing_events_path, filename, sys, name);
  109. } else {
  110. snprintf(buf, size,
  111. "Error:\tFile %s/%s not found.\n"
  112. "Hint:\tPerhaps this kernel misses some CONFIG_ setting to enable this feature?.\n",
  113. tracing_events_path, filename);
  114. }
  115. break;
  116. }
  117. snprintf(buf, size, "%s",
  118. "Error:\tUnable to find debugfs/tracefs\n"
  119. "Hint:\tWas your kernel compiled with debugfs/tracefs support?\n"
  120. "Hint:\tIs the debugfs/tracefs filesystem mounted?\n"
  121. "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
  122. break;
  123. case EACCES: {
  124. snprintf(buf, size,
  125. "Error:\tNo permissions to read %s/%s\n"
  126. "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
  127. tracing_events_path, filename, tracing_path_mount());
  128. }
  129. break;
  130. default:
  131. snprintf(buf, size, "%s", str_error_r(err, sbuf, sizeof(sbuf)));
  132. break;
  133. }
  134. return 0;
  135. }