tracing_path.c 2.9 KB

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