tracing_path.c 3.1 KB

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