tracing_path.c 2.8 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 <errno.h>
  8. #include <unistd.h>
  9. #include "debugfs.h"
  10. #include "tracefs.h"
  11. #include "tracing_path.h"
  12. char tracing_path[PATH_MAX + 1] = "/sys/kernel/debug/tracing";
  13. char tracing_events_path[PATH_MAX + 1] = "/sys/kernel/debug/tracing/events";
  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. snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s%s",
  19. mountpoint, tracing, "events");
  20. }
  21. static const char *tracing_path_tracefs_mount(void)
  22. {
  23. const char *mnt;
  24. mnt = tracefs_mount(NULL);
  25. if (!mnt)
  26. return NULL;
  27. __tracing_path_set("", mnt);
  28. return mnt;
  29. }
  30. static const char *tracing_path_debugfs_mount(void)
  31. {
  32. const char *mnt;
  33. mnt = debugfs_mount(NULL);
  34. if (!mnt)
  35. return NULL;
  36. __tracing_path_set("tracing/", mnt);
  37. return mnt;
  38. }
  39. const char *tracing_path_mount(void)
  40. {
  41. const char *mnt;
  42. mnt = tracing_path_tracefs_mount();
  43. if (mnt)
  44. return mnt;
  45. mnt = tracing_path_debugfs_mount();
  46. return mnt;
  47. }
  48. void tracing_path_set(const char *mntpt)
  49. {
  50. __tracing_path_set("tracing/", mntpt);
  51. }
  52. char *get_tracing_file(const char *name)
  53. {
  54. char *file;
  55. if (asprintf(&file, "%s/%s", tracing_path, name) < 0)
  56. return NULL;
  57. return file;
  58. }
  59. void put_tracing_file(char *file)
  60. {
  61. free(file);
  62. }
  63. static int strerror_open(int err, char *buf, size_t size, const char *filename)
  64. {
  65. char sbuf[128];
  66. switch (err) {
  67. case ENOENT:
  68. if (debugfs_configured()) {
  69. snprintf(buf, size,
  70. "Error:\tFile %s/%s not found.\n"
  71. "Hint:\tPerhaps this kernel misses some CONFIG_ setting to enable this feature?.\n",
  72. debugfs_mountpoint, filename);
  73. break;
  74. }
  75. snprintf(buf, size, "%s",
  76. "Error:\tUnable to find debugfs\n"
  77. "Hint:\tWas your kernel compiled with debugfs support?\n"
  78. "Hint:\tIs the debugfs filesystem mounted?\n"
  79. "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
  80. break;
  81. case EACCES: {
  82. const char *mountpoint = debugfs_mountpoint;
  83. if (!access(debugfs_mountpoint, R_OK) && strncmp(filename, "tracing/", 8) == 0) {
  84. const char *tracefs_mntpoint = tracefs_find_mountpoint();
  85. if (tracefs_mntpoint)
  86. mountpoint = tracefs_mntpoint;
  87. }
  88. snprintf(buf, size,
  89. "Error:\tNo permissions to read %s/%s\n"
  90. "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
  91. debugfs_mountpoint, filename, mountpoint);
  92. }
  93. break;
  94. default:
  95. snprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf)));
  96. break;
  97. }
  98. return 0;
  99. }
  100. int tracing_path__strerror_open_tp(int err, char *buf, size_t size, const char *sys, const char *name)
  101. {
  102. char path[PATH_MAX];
  103. snprintf(path, PATH_MAX, "tracing/events/%s/%s", sys, name ?: "*");
  104. return strerror_open(err, buf, size, path);
  105. }