tracing_path.c 3.5 KB

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