lm.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <sstream>
  5. #include <string>
  6. #include <optional>
  7. #include <unordered_map>
  8. #include <boost/container_hash/hash.hpp>
  9. #include "common/logging/log.h"
  10. #include "common/scope_exit.h"
  11. #include "core/core.h"
  12. #include "core/hle/ipc_helpers.h"
  13. #include "core/hle/service/lm/lm.h"
  14. #include "core/hle/service/service.h"
  15. #include "core/memory.h"
  16. namespace Service::LM {
  17. enum class LogSeverity : u8 {
  18. Trace = 0,
  19. Info = 1,
  20. Warning = 2,
  21. Error = 3,
  22. Fatal = 4,
  23. };
  24. // To keep flags out of hashing as well as the payload size
  25. struct LogPacketHeaderEntry {
  26. u64_le pid{};
  27. u64_le tid{};
  28. LogSeverity severity{};
  29. u8 verbosity{};
  30. auto operator<=>(const LogPacketHeaderEntry&) const = default;
  31. };
  32. } // namespace Service::LM
  33. namespace std {
  34. template <>
  35. struct hash<Service::LM::LogPacketHeaderEntry> {
  36. std::size_t operator()(const Service::LM::LogPacketHeaderEntry& k) const noexcept {
  37. std::size_t seed{};
  38. boost::hash_combine(seed, k.pid);
  39. boost::hash_combine(seed, k.tid);
  40. boost::hash_combine(seed, k.severity);
  41. boost::hash_combine(seed, k.verbosity);
  42. return seed;
  43. };
  44. };
  45. } // namespace std
  46. namespace Service::LM {
  47. enum class LogDestination : u32 {
  48. TargetManager = 1 << 0,
  49. Uart = 1 << 1,
  50. UartSleep = 1 << 2,
  51. All = 0xffff,
  52. };
  53. DECLARE_ENUM_FLAG_OPERATORS(LogDestination);
  54. enum class LogPacketFlags : u8 {
  55. Head = 1 << 0,
  56. Tail = 1 << 1,
  57. LittleEndian = 1 << 2,
  58. };
  59. DECLARE_ENUM_FLAG_OPERATORS(LogPacketFlags);
  60. class ILogger final : public ServiceFramework<ILogger> {
  61. public:
  62. explicit ILogger(Core::System& system_) : ServiceFramework{system_, "ILogger"} {
  63. static const FunctionInfo functions[] = {
  64. {0, &ILogger::Log, "Log"},
  65. {1, &ILogger::SetDestination, "SetDestination"},
  66. };
  67. RegisterHandlers(functions);
  68. }
  69. private:
  70. void Log(Kernel::HLERequestContext& ctx) {
  71. std::size_t offset{};
  72. const auto data = ctx.ReadBuffer();
  73. // This function only succeeds - Get that out of the way
  74. IPC::ResponseBuilder rb{ctx, 2};
  75. rb.Push(RESULT_SUCCESS);
  76. if (data.size() < sizeof(LogPacketHeader)) {
  77. LOG_ERROR(Service_LM, "Data size is too small for header! size={}", data.size());
  78. return;
  79. }
  80. LogPacketHeader header{};
  81. std::memcpy(&header, data.data(), sizeof(LogPacketHeader));
  82. offset += sizeof(LogPacketHeader);
  83. LogPacketHeaderEntry entry{
  84. .pid = header.pid,
  85. .tid = header.tid,
  86. .severity = header.severity,
  87. .verbosity = header.verbosity,
  88. };
  89. if (True(header.flags & LogPacketFlags::Head)) {
  90. std::vector<u8> tmp(data.size() - sizeof(LogPacketHeader));
  91. std::memcpy(tmp.data(), data.data() + offset, tmp.size());
  92. entries[entry] = std::move(tmp);
  93. } else {
  94. // Append to existing entry
  95. if (!entries.contains(entry)) {
  96. LOG_ERROR(Service_LM, "Log entry does not exist!");
  97. return;
  98. }
  99. std::vector<u8> tmp(data.size() - sizeof(LogPacketHeader));
  100. auto& existing_entry = entries[entry];
  101. const auto base = existing_entry.size();
  102. existing_entry.resize(base + (data.size() - sizeof(LogPacketHeader)));
  103. std::memcpy(existing_entry.data() + base, data.data() + offset,
  104. (data.size() - sizeof(LogPacketHeader)));
  105. }
  106. if (True(header.flags & LogPacketFlags::Tail)) {
  107. auto it = entries.find(entry);
  108. if (it == entries.end()) {
  109. LOG_ERROR(Service_LM, "Log entry does not exist!");
  110. return;
  111. }
  112. ParseLog(it->first, it->second);
  113. entries.erase(it);
  114. }
  115. }
  116. void SetDestination(Kernel::HLERequestContext& ctx) {
  117. IPC::RequestParser rp{ctx};
  118. const auto log_destination = rp.PopEnum<LogDestination>();
  119. LOG_DEBUG(Service_LM, "called, destination={}", DestinationToString(log_destination));
  120. destination = log_destination;
  121. IPC::ResponseBuilder rb{ctx, 2};
  122. rb.Push(RESULT_SUCCESS);
  123. }
  124. u32 ReadLeb128(const std::vector<u8>& data, std::size_t& offset) {
  125. u32 result{};
  126. u32 shift{};
  127. do {
  128. result |= (data[offset] & 0x7f) << shift;
  129. shift += 7;
  130. offset++;
  131. if (offset >= data.size()) {
  132. break;
  133. }
  134. } while ((data[offset] & 0x80) != 0);
  135. return result;
  136. }
  137. std::optional<std::string> ReadString(const std::vector<u8>& data, std::size_t& offset,
  138. std::size_t length) {
  139. if (length == 0) {
  140. return std::nullopt;
  141. }
  142. const auto length_to_read = std::min(length, data.size() - offset);
  143. std::string output(length_to_read, '\0');
  144. std::memcpy(output.data(), data.data() + offset, length_to_read);
  145. offset += length_to_read;
  146. return output;
  147. }
  148. u32_le ReadAsU32(const std::vector<u8>& data, std::size_t& offset, std::size_t length) {
  149. ASSERT(length == sizeof(u32));
  150. u32_le output{};
  151. std::memcpy(&output, data.data() + offset, sizeof(u32));
  152. offset += length;
  153. return output;
  154. }
  155. u64_le ReadAsU64(const std::vector<u8>& data, std::size_t& offset, std::size_t length) {
  156. ASSERT(length == sizeof(u64));
  157. u64_le output{};
  158. std::memcpy(&output, data.data() + offset, sizeof(u64));
  159. offset += length;
  160. return output;
  161. }
  162. void ParseLog(const LogPacketHeaderEntry entry, const std::vector<u8>& log_data) {
  163. // Possible entries
  164. std::optional<std::string> text_log;
  165. std::optional<u32> line_number;
  166. std::optional<std::string> file_name;
  167. std::optional<std::string> function_name;
  168. std::optional<std::string> module_name;
  169. std::optional<std::string> thread_name;
  170. std::optional<u64> log_pack_drop_count;
  171. std::optional<s64> user_system_clock;
  172. std::optional<std::string> process_name;
  173. std::size_t offset{};
  174. while (offset < log_data.size()) {
  175. const auto key = static_cast<LogDataChunkKey>(ReadLeb128(log_data, offset));
  176. const auto chunk_size = ReadLeb128(log_data, offset);
  177. switch (key) {
  178. case LogDataChunkKey::LogSessionBegin:
  179. case LogDataChunkKey::LogSessionEnd:
  180. break;
  181. case LogDataChunkKey::TextLog:
  182. text_log = ReadString(log_data, offset, chunk_size);
  183. break;
  184. case LogDataChunkKey::LineNumber:
  185. line_number = ReadAsU32(log_data, offset, chunk_size);
  186. break;
  187. case LogDataChunkKey::FileName:
  188. file_name = ReadString(log_data, offset, chunk_size);
  189. break;
  190. case LogDataChunkKey::FunctionName:
  191. function_name = ReadString(log_data, offset, chunk_size);
  192. break;
  193. case LogDataChunkKey::ModuleName:
  194. module_name = ReadString(log_data, offset, chunk_size);
  195. break;
  196. case LogDataChunkKey::ThreadName:
  197. thread_name = ReadString(log_data, offset, chunk_size);
  198. break;
  199. case LogDataChunkKey::LogPacketDropCount:
  200. log_pack_drop_count = ReadAsU64(log_data, offset, chunk_size);
  201. break;
  202. case LogDataChunkKey::UserSystemClock:
  203. user_system_clock = ReadAsU64(log_data, offset, chunk_size);
  204. break;
  205. case LogDataChunkKey::ProcessName:
  206. process_name = ReadString(log_data, offset, chunk_size);
  207. break;
  208. }
  209. }
  210. std::string output_log{};
  211. if (process_name) {
  212. output_log += fmt::format("Process: {}\n", *process_name);
  213. }
  214. if (module_name) {
  215. output_log += fmt::format("Module: {}\n", *module_name);
  216. }
  217. if (file_name) {
  218. output_log += fmt::format("File: {}\n", *file_name);
  219. }
  220. if (function_name) {
  221. output_log += fmt::format("Function: {}\n", *function_name);
  222. }
  223. if (line_number && *line_number != 0) {
  224. output_log += fmt::format("Line: {}\n", *line_number);
  225. }
  226. output_log += fmt::format("ProcessID: {}\n", entry.pid);
  227. output_log += fmt::format("ThreadID: {}\n", entry.tid);
  228. if (text_log) {
  229. output_log += fmt::format("Log Text: {}\n", *text_log);
  230. }
  231. switch (entry.severity) {
  232. case LogSeverity::Trace:
  233. LOG_DEBUG(Service_LM, "LogManager DEBUG ({}):\n{}", DestinationToString(destination),
  234. output_log);
  235. break;
  236. case LogSeverity::Info:
  237. LOG_INFO(Service_LM, "LogManager INFO ({}):\n{}", DestinationToString(destination),
  238. output_log);
  239. break;
  240. case LogSeverity::Warning:
  241. LOG_WARNING(Service_LM, "LogManager WARNING ({}):\n{}",
  242. DestinationToString(destination), output_log);
  243. break;
  244. case LogSeverity::Error:
  245. LOG_ERROR(Service_LM, "LogManager ERROR ({}):\n{}", DestinationToString(destination),
  246. output_log);
  247. break;
  248. case LogSeverity::Fatal:
  249. LOG_CRITICAL(Service_LM, "LogManager FATAL ({}):\n{}", DestinationToString(destination),
  250. output_log);
  251. break;
  252. default:
  253. LOG_CRITICAL(Service_LM, "LogManager UNKNOWN ({}):\n{}",
  254. DestinationToString(destination), output_log);
  255. break;
  256. }
  257. }
  258. static std::string DestinationToString(LogDestination destination) {
  259. if (True(destination & LogDestination::All)) {
  260. return "TargetManager | Uart | UartSleep";
  261. }
  262. std::string output{};
  263. if (True(destination & LogDestination::TargetManager)) {
  264. output += "| TargetManager";
  265. }
  266. if (True(destination & LogDestination::Uart)) {
  267. output += "| Uart";
  268. }
  269. if (True(destination & LogDestination::UartSleep)) {
  270. output += "| UartSleep";
  271. }
  272. if (output.length() > 0) {
  273. return output.substr(2);
  274. }
  275. return "No Destination";
  276. }
  277. enum class LogDataChunkKey : u32 {
  278. LogSessionBegin = 0,
  279. LogSessionEnd = 1,
  280. TextLog = 2,
  281. LineNumber = 3,
  282. FileName = 4,
  283. FunctionName = 5,
  284. ModuleName = 6,
  285. ThreadName = 7,
  286. LogPacketDropCount = 8,
  287. UserSystemClock = 9,
  288. ProcessName = 10,
  289. };
  290. struct LogPacketHeader {
  291. u64_le pid{};
  292. u64_le tid{};
  293. LogPacketFlags flags{};
  294. INSERT_PADDING_BYTES(1);
  295. LogSeverity severity{};
  296. u8 verbosity{};
  297. u32_le payload_size{};
  298. };
  299. static_assert(sizeof(LogPacketHeader) == 0x18, "LogPacketHeader is an invalid size");
  300. std::unordered_map<LogPacketHeaderEntry, std::vector<u8>> entries{};
  301. LogDestination destination{LogDestination::All};
  302. };
  303. class LM final : public ServiceFramework<LM> {
  304. public:
  305. explicit LM(Core::System& system_) : ServiceFramework{system_, "lm"} {
  306. // clang-format off
  307. static const FunctionInfo functions[] = {
  308. {0, &LM::OpenLogger, "OpenLogger"},
  309. };
  310. // clang-format on
  311. RegisterHandlers(functions);
  312. }
  313. private:
  314. void OpenLogger(Kernel::HLERequestContext& ctx) {
  315. LOG_DEBUG(Service_LM, "called");
  316. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  317. rb.Push(RESULT_SUCCESS);
  318. rb.PushIpcInterface<ILogger>(system);
  319. }
  320. };
  321. void InstallInterfaces(Core::System& system) {
  322. std::make_shared<LM>(system)->InstallAsService(system.ServiceManager());
  323. }
  324. } // namespace Service::LM