tracelog.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
  2. /* Copyright (c) 2015-2017 Daniel Borkmann */
  3. /* Copyright (c) 2018 Netronome Systems, Inc. */
  4. #include <errno.h>
  5. #include <limits.h>
  6. #include <signal.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <linux/magic.h>
  11. #include <fcntl.h>
  12. #include <sys/vfs.h>
  13. #include "main.h"
  14. #ifndef TRACEFS_MAGIC
  15. # define TRACEFS_MAGIC 0x74726163
  16. #endif
  17. #define _textify(x) #x
  18. #define textify(x) _textify(x)
  19. FILE *trace_pipe_fd;
  20. char *buff;
  21. static int validate_tracefs_mnt(const char *mnt, unsigned long magic)
  22. {
  23. struct statfs st_fs;
  24. if (statfs(mnt, &st_fs) < 0)
  25. return -ENOENT;
  26. if ((unsigned long)st_fs.f_type != magic)
  27. return -ENOENT;
  28. return 0;
  29. }
  30. static bool
  31. find_tracefs_mnt_single(unsigned long magic, char *mnt, const char *mntpt)
  32. {
  33. size_t src_len;
  34. if (validate_tracefs_mnt(mntpt, magic))
  35. return false;
  36. src_len = strlen(mntpt);
  37. if (src_len + 1 >= PATH_MAX) {
  38. p_err("tracefs mount point name too long");
  39. return false;
  40. }
  41. strcpy(mnt, mntpt);
  42. return true;
  43. }
  44. static bool get_tracefs_pipe(char *mnt)
  45. {
  46. static const char * const known_mnts[] = {
  47. "/sys/kernel/debug/tracing",
  48. "/sys/kernel/tracing",
  49. "/tracing",
  50. "/trace",
  51. };
  52. const char *pipe_name = "/trace_pipe";
  53. const char *fstype = "tracefs";
  54. char type[100], format[32];
  55. const char * const *ptr;
  56. bool found = false;
  57. FILE *fp;
  58. for (ptr = known_mnts; ptr < known_mnts + ARRAY_SIZE(known_mnts); ptr++)
  59. if (find_tracefs_mnt_single(TRACEFS_MAGIC, mnt, *ptr))
  60. goto exit_found;
  61. fp = fopen("/proc/mounts", "r");
  62. if (!fp)
  63. return false;
  64. /* Allow room for NULL terminating byte and pipe file name */
  65. snprintf(format, sizeof(format), "%%*s %%%zds %%99s %%*s %%*d %%*d\\n",
  66. PATH_MAX - strlen(pipe_name) - 1);
  67. while (fscanf(fp, format, mnt, type) == 2)
  68. if (strcmp(type, fstype) == 0) {
  69. found = true;
  70. break;
  71. }
  72. fclose(fp);
  73. /* The string from fscanf() might be truncated, check mnt is valid */
  74. if (found && validate_tracefs_mnt(mnt, TRACEFS_MAGIC))
  75. goto exit_found;
  76. if (block_mount)
  77. return false;
  78. p_info("could not find tracefs, attempting to mount it now");
  79. /* Most of the time, tracefs is automatically mounted by debugfs at
  80. * /sys/kernel/debug/tracing when we try to access it. If we could not
  81. * find it, it is likely that debugfs is not mounted. Let's give one
  82. * attempt at mounting just tracefs at /sys/kernel/tracing.
  83. */
  84. strcpy(mnt, known_mnts[1]);
  85. if (mount_tracefs(mnt))
  86. return false;
  87. exit_found:
  88. strcat(mnt, pipe_name);
  89. return true;
  90. }
  91. static void exit_tracelog(int signum)
  92. {
  93. fclose(trace_pipe_fd);
  94. free(buff);
  95. if (json_output) {
  96. jsonw_end_array(json_wtr);
  97. jsonw_destroy(&json_wtr);
  98. }
  99. exit(0);
  100. }
  101. int do_tracelog(int argc, char **argv)
  102. {
  103. const struct sigaction act = {
  104. .sa_handler = exit_tracelog
  105. };
  106. char trace_pipe[PATH_MAX];
  107. size_t buff_len = 0;
  108. if (json_output)
  109. jsonw_start_array(json_wtr);
  110. if (!get_tracefs_pipe(trace_pipe))
  111. return -1;
  112. trace_pipe_fd = fopen(trace_pipe, "r");
  113. if (!trace_pipe_fd) {
  114. p_err("could not open trace pipe: %s", strerror(errno));
  115. return -1;
  116. }
  117. sigaction(SIGHUP, &act, NULL);
  118. sigaction(SIGINT, &act, NULL);
  119. sigaction(SIGTERM, &act, NULL);
  120. while (1) {
  121. ssize_t ret;
  122. ret = getline(&buff, &buff_len, trace_pipe_fd);
  123. if (ret <= 0) {
  124. p_err("failed to read content from trace pipe: %s",
  125. strerror(errno));
  126. break;
  127. }
  128. if (json_output)
  129. jsonw_string(json_wtr, buff);
  130. else
  131. printf("%s", buff);
  132. }
  133. fclose(trace_pipe_fd);
  134. free(buff);
  135. return -1;
  136. }