tracing_path.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. static int strerror_open(int err, char *buf, size_t size, const char *filename)
  66. {
  67. char sbuf[128];
  68. switch (err) {
  69. case ENOENT:
  70. /*
  71. * We will get here if we can't find the tracepoint, but one of
  72. * debugfs or tracefs is configured, which means you probably
  73. * want some tracepoint which wasn't compiled in your kernel.
  74. * - jirka
  75. */
  76. if (debugfs__configured() || tracefs__configured()) {
  77. snprintf(buf, size,
  78. "Error:\tFile %s/%s not found.\n"
  79. "Hint:\tPerhaps this kernel misses some CONFIG_ setting to enable this feature?.\n",
  80. tracing_events_path, filename);
  81. break;
  82. }
  83. snprintf(buf, size, "%s",
  84. "Error:\tUnable to find debugfs/tracefs\n"
  85. "Hint:\tWas your kernel compiled with debugfs/tracefs support?\n"
  86. "Hint:\tIs the debugfs/tracefs filesystem mounted?\n"
  87. "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
  88. break;
  89. case EACCES: {
  90. snprintf(buf, size,
  91. "Error:\tNo permissions to read %s/%s\n"
  92. "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
  93. tracing_events_path, filename, tracing_mnt);
  94. }
  95. break;
  96. default:
  97. snprintf(buf, size, "%s", str_error_r(err, sbuf, sizeof(sbuf)));
  98. break;
  99. }
  100. return 0;
  101. }
  102. int tracing_path__strerror_open_tp(int err, char *buf, size_t size, const char *sys, const char *name)
  103. {
  104. char path[PATH_MAX];
  105. snprintf(path, PATH_MAX, "%s/%s", sys, name ?: "*");
  106. return strerror_open(err, buf, size, path);
  107. }