diff --git a/stratosphere/ams_mitm/source/uart_mitm/uart_mitm_logger.cpp b/stratosphere/ams_mitm/source/uart_mitm/uart_mitm_logger.cpp index 34ff1dc9e..9341c8cac 100644 --- a/stratosphere/ams_mitm/source/uart_mitm/uart_mitm_logger.cpp +++ b/stratosphere/ams_mitm/source/uart_mitm/uart_mitm_logger.cpp @@ -138,7 +138,7 @@ namespace ams::mitm::uart { void UartLogger::FlushCache() { for (size_t i=0; im_cache_count; i++) { UartLogMessage *cache_msg=&this->m_cache_list[i]; - this->WriteLogPacket(cache_msg->datalog_file, cache_msg->file_pos, cache_msg->dir, cache_msg->data, cache_msg->size); + this->WriteLogPacket(cache_msg->datalog_file, cache_msg->file_pos, cache_msg->timestamp, cache_msg->dir, cache_msg->data, cache_msg->size); } this->m_cache_count = 0; @@ -184,7 +184,7 @@ namespace ams::mitm::uart { /* Append the specified packet to the datalog via WriteLog. */ /* dir: false = Send (host->controller), true = Receive (controller->host). */ - void UartLogger::WriteLogPacket(FsFile *f, size_t *datalog_pos, bool dir, const void* buffer, size_t size) { + void UartLogger::WriteLogPacket(FsFile *f, size_t *datalog_pos, s64 timestamp, bool dir, const void* buffer, size_t size) { struct { u32 original_length; u32 included_length; @@ -200,8 +200,7 @@ namespace ams::mitm::uart { ams::util::StoreBigEndian(&pkt_hdr.original_length, static_cast(size)); ams::util::StoreBigEndian(&pkt_hdr.included_length, static_cast(size)); ams::util::StoreBigEndian(&pkt_hdr.packet_flags, flags); - - /* Currently we leave the timestamp at value 0. */ + ams::util::StoreBigEndian(&pkt_hdr.timestamp_microseconds, timestamp); this->WriteLog(f, datalog_pos, &pkt_hdr, sizeof(pkt_hdr)); this->WriteLog(f, datalog_pos, buffer, size); @@ -209,7 +208,7 @@ namespace ams::mitm::uart { /* Send the specified data to the Logger thread. */ /* dir: false = Send (host->controller), true = Receive (controller->host). */ - void UartLogger::SendLogData(FsFile *f, size_t *file_pos, bool dir, const void* buffer, size_t size) { + void UartLogger::SendLogData(FsFile *f, size_t *file_pos, s64 timestamp_base, s64 tick_base, bool dir, const void* buffer, size_t size) { /* Ignore log data which is too large. */ if (size > this->QueueBufferSize) return; @@ -220,6 +219,12 @@ namespace ams::mitm::uart { /* Setup the msg and send it. */ msg->type = 1; msg->dir = dir; + if (timestamp_base) { + msg->timestamp = (armTicksToNs(armGetSystemTick() - tick_base) / 1000) + timestamp_base; + } + else { + msg->timestamp = 0; + } msg->datalog_file = f; msg->file_pos = file_pos; msg->size = size; diff --git a/stratosphere/ams_mitm/source/uart_mitm/uart_mitm_logger.hpp b/stratosphere/ams_mitm/source/uart_mitm/uart_mitm_logger.hpp index db52c265f..4aea59fa5 100644 --- a/stratosphere/ams_mitm/source/uart_mitm/uart_mitm_logger.hpp +++ b/stratosphere/ams_mitm/source/uart_mitm/uart_mitm_logger.hpp @@ -21,6 +21,7 @@ namespace ams::mitm::uart { struct UartLogMessage { u8 type; bool dir; + s64 timestamp; FsFile *datalog_file; size_t *file_pos; size_t size; @@ -57,7 +58,7 @@ namespace ams::mitm::uart { void WriteCmdLog(const char *path, const char *str, size_t *file_pos); void WriteLog(FsFile *f, size_t *datalog_pos, const void* buffer, size_t size); - void WriteLogPacket(FsFile *f, size_t *datalog_pos, bool dir, const void* buffer, size_t size); + void WriteLogPacket(FsFile *f, size_t *datalog_pos, s64 timestamp, bool dir, const void* buffer, size_t size); public: UartLogger(); ~UartLogger(); @@ -66,7 +67,7 @@ namespace ams::mitm::uart { void InitializeDataLog(FsFile *f, size_t *datalog_pos); - void SendLogData(FsFile *f, size_t *file_pos, bool dir, const void* buffer, size_t size); + void SendLogData(FsFile *f, size_t *file_pos, s64 timestamp_base, s64 tick_base, bool dir, const void* buffer, size_t size); void SendTextLogData(const char *path, size_t *file_pos, const char *str); }; diff --git a/stratosphere/ams_mitm/source/uart_mitm/uart_mitm_service.cpp b/stratosphere/ams_mitm/source/uart_mitm/uart_mitm_service.cpp index 1fb3a5773..776c8e4a0 100644 --- a/stratosphere/ams_mitm/source/uart_mitm/uart_mitm_service.cpp +++ b/stratosphere/ams_mitm/source/uart_mitm/uart_mitm_service.cpp @@ -45,7 +45,14 @@ namespace ams::mitm::uart { /* Get a timestamp. */ u64 timestamp0=0, timestamp1; this->TryGetCurrentTimestamp(×tamp0); - timestamp1 = svcGetSystemTick(); + timestamp1 = armGetSystemTick(); + + /* Setup the btsnoop base timestamps. */ + this->m_timestamp_base = timestamp0; + if (this->m_timestamp_base) { + this->m_timestamp_base = 0x00E03AB44A676000 + (this->m_timestamp_base - 946684800) * 1000000; + } + this->m_tick_base = timestamp1; /* Setup/create the logging directory. */ std::snprintf(this->m_base_path, sizeof(this->m_base_path), "uart_logs/%011lu_%011lu_%016lx", timestamp0, timestamp1, static_cast(this->m_client_info.program_id)); @@ -204,7 +211,7 @@ namespace ams::mitm::uart { /* Only write to the file if data-logging is enabled and initialized. */ if (this->m_data_logging_enabled && this->m_datalog_ready) { std::shared_ptr logger = mitm::uart::g_logger; - logger->SendLogData(&this->m_datalog_file, &this->m_datalog_pos, dir, cache_buffer, pkt_len); + logger->SendLogData(&this->m_datalog_file, &this->m_datalog_pos, this->m_timestamp_base, this->m_tick_base, dir, cache_buffer, pkt_len); } (*cache_pos)-= pkt_len; if (*cache_pos) { diff --git a/stratosphere/ams_mitm/source/uart_mitm/uart_mitm_service.hpp b/stratosphere/ams_mitm/source/uart_mitm/uart_mitm_service.hpp index 2a9c0917d..5291dcd30 100644 --- a/stratosphere/ams_mitm/source/uart_mitm/uart_mitm_service.hpp +++ b/stratosphere/ams_mitm/source/uart_mitm/uart_mitm_service.hpp @@ -45,6 +45,9 @@ namespace ams::mitm::uart { static constexpr inline size_t CacheBufferSize = 0x1000; + s64 m_timestamp_base; + s64 m_tick_base; + char m_base_path[256]; size_t m_cmdlog_pos;