Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame^] | 1 | #ifndef AOS_FTRACE_H_ |
| 2 | #define AOS_FTRACE_H_ |
| 3 | |
| 4 | #include <fcntl.h> |
| 5 | #include <sys/stat.h> |
| 6 | #include <sys/types.h> |
| 7 | #include <unistd.h> |
| 8 | |
| 9 | #include <string_view> |
| 10 | |
| 11 | #include "glog/logging.h" |
| 12 | |
| 13 | namespace aos { |
| 14 | |
| 15 | // Manages interacting with ftrace. Silently hides many errors, because they are |
| 16 | // expected to occur when ftrace is not enabled, and we want calling code to |
| 17 | // continue working in that case. |
| 18 | class Ftrace { |
| 19 | public: |
| 20 | Ftrace() |
| 21 | : message_fd_(open("/sys/kernel/debug/tracing/trace_marker", O_WRONLY)), |
| 22 | on_fd_(open("/sys/kernel/debug/tracing/tracing_on", O_WRONLY)) {} |
| 23 | ~Ftrace(); |
| 24 | |
| 25 | // Writes a message with a printf-style format. |
| 26 | // |
| 27 | // Silently does nothing if tracing is disabled. |
| 28 | void FormatMessage(const char *format, ...) |
| 29 | __attribute__((format(__printf__, 2, 3))); |
| 30 | |
| 31 | // Writes a preformatted message. |
| 32 | // |
| 33 | // Silently does nothing if tracing is disabled. |
| 34 | void WriteMessage(std::string_view content); |
| 35 | |
| 36 | // Turns tracing off, or CHECK-fails if tracing is inaccessible. Does nothing |
| 37 | // if tracing is currently available but off. |
| 38 | void TurnOffOrDie() { |
| 39 | CHECK(on_fd_ != -1) |
| 40 | << ": Failed to open tracing_on earlier, cannot turn off tracing"; |
| 41 | char zero = '0'; |
| 42 | CHECK_EQ(write(on_fd_, &zero, 1), 1) << ": Failed to turn tracing off"; |
| 43 | } |
| 44 | |
| 45 | private: |
| 46 | int message_fd_, on_fd_; |
| 47 | }; |
| 48 | |
| 49 | } // namespace aos |
| 50 | |
| 51 | #endif // AOS_FTRACE_H_ |