Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 1 | #include "aos/ftrace.h" |
| 2 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 3 | #include <cstdarg> |
| 4 | #include <cstdio> |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 5 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 6 | #include "absl/flags/flag.h" |
| 7 | #include "absl/log/check.h" |
Austin Schuh | c9de013 | 2022-12-26 18:04:53 -0800 | [diff] [blame] | 8 | #include "absl/strings/str_cat.h" |
| 9 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 10 | ABSL_FLAG(bool, enable_ftrace, false, |
| 11 | "If false, disable logging to /sys/kernel/tracing/trace_marker"); |
Austin Schuh | 2723736 | 2021-11-06 16:29:02 -0700 | [diff] [blame] | 12 | |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 13 | namespace aos { |
| 14 | |
Austin Schuh | c9de013 | 2022-12-26 18:04:53 -0800 | [diff] [blame] | 15 | namespace { |
Austin Schuh | 3458e49 | 2022-12-26 13:41:54 -0800 | [diff] [blame] | 16 | int MaybeCheckOpen(const char *file) { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 17 | if (!absl::GetFlag(FLAGS_enable_ftrace)) return -1; |
Austin Schuh | c9de013 | 2022-12-26 18:04:53 -0800 | [diff] [blame] | 18 | int result = |
| 19 | open(absl::StrCat("/sys/kernel/tracing/", file).c_str(), O_WRONLY); |
| 20 | if (result == -1) { |
| 21 | result = open(absl::StrCat("/sys/kernel/debug/tracing/", file).c_str(), |
| 22 | O_WRONLY); |
| 23 | } |
| 24 | |
| 25 | // New kernels prefer /sys/kernel/tracing, and old kernels prefer |
| 26 | // /sys/kernel/debug/tracing... When Ubuntu 18.04 and the 4.9 kernel |
| 27 | // disappear finally, we can switch fully to /sys/kernel/tracing. |
| 28 | PCHECK(result >= 0) << ": Failed to open /sys/kernel/tracing/" << file |
| 29 | << " or legacy /sys/kernel/debug/tracing/" << file; |
Austin Schuh | 3458e49 | 2022-12-26 13:41:54 -0800 | [diff] [blame] | 30 | return result; |
| 31 | } |
Austin Schuh | c9de013 | 2022-12-26 18:04:53 -0800 | [diff] [blame] | 32 | } // namespace |
Austin Schuh | 3458e49 | 2022-12-26 13:41:54 -0800 | [diff] [blame] | 33 | |
Austin Schuh | 2723736 | 2021-11-06 16:29:02 -0700 | [diff] [blame] | 34 | Ftrace::Ftrace() |
Austin Schuh | c9de013 | 2022-12-26 18:04:53 -0800 | [diff] [blame] | 35 | : message_fd_(MaybeCheckOpen("trace_marker")), |
| 36 | on_fd_(MaybeCheckOpen("tracing_on")) {} |
Austin Schuh | 2723736 | 2021-11-06 16:29:02 -0700 | [diff] [blame] | 37 | |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 38 | Ftrace::~Ftrace() { |
| 39 | if (message_fd_ != -1) { |
| 40 | PCHECK(close(message_fd_) == 0); |
| 41 | } |
| 42 | if (message_fd_ != -1) { |
| 43 | PCHECK(close(on_fd_) == 0); |
| 44 | } |
| 45 | } |
| 46 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 47 | void Ftrace::TurnOffOrDie() { |
| 48 | CHECK(on_fd_ != -1) |
| 49 | << ": Failed to open tracing_on earlier, cannot turn off tracing"; |
| 50 | char zero = '0'; |
| 51 | CHECK_EQ(write(on_fd_, &zero, 1), 1) << ": Failed to turn tracing off"; |
| 52 | } |
| 53 | |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 54 | void Ftrace::FormatMessage(const char *format, ...) { |
| 55 | if (message_fd_ == -1) { |
| 56 | return; |
| 57 | } |
| 58 | char buffer[512]; |
| 59 | va_list ap; |
| 60 | va_start(ap, format); |
| 61 | const int result = vsnprintf(buffer, sizeof(buffer), format, ap); |
| 62 | va_end(ap); |
| 63 | CHECK_LE(static_cast<size_t>(result), sizeof(buffer)) |
| 64 | << ": Format string ended up too long: " << format; |
| 65 | WriteMessage(std::string_view(buffer, result)); |
| 66 | } |
| 67 | |
| 68 | void Ftrace::WriteMessage(std::string_view content) { |
| 69 | if (message_fd_ == -1) { |
| 70 | return; |
| 71 | } |
| 72 | const int result = write(message_fd_, content.data(), content.size()); |
| 73 | if (result == -1 && errno == EBADF) { |
| 74 | // This just means tracing is turned off. Ignore it. |
| 75 | return; |
| 76 | } |
| 77 | PCHECK(result >= 0) << ": Failed to write ftrace message: " << content; |
| 78 | CHECK_EQ(static_cast<size_t>(result), content.size()) |
| 79 | << ": Failed to write complete ftrace message: " << content; |
| 80 | } |
| 81 | |
| 82 | } // namespace aos |