lm.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright 2017 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <string>
  5. #include "common/logging/log.h"
  6. #include "core/hle/ipc_helpers.h"
  7. #include "core/hle/kernel/client_session.h"
  8. #include "core/hle/service/lm/lm.h"
  9. namespace Service {
  10. namespace LM {
  11. class Logger final : public ServiceFramework<Logger> {
  12. public:
  13. Logger() : ServiceFramework("Logger") {
  14. static const FunctionInfo functions[] = {
  15. {0x00000000, &Logger::Log, "Log"},
  16. };
  17. RegisterHandlers(functions);
  18. }
  19. ~Logger() = default;
  20. private:
  21. struct MessageHeader {
  22. enum Flags : u32_le {
  23. IsHead = 1,
  24. IsTail = 2,
  25. };
  26. u64_le pid;
  27. u64_le threadContext;
  28. union {
  29. BitField<0, 16, Flags> flags;
  30. BitField<16, 8, u32_le> severity;
  31. BitField<24, 8, u32_le> verbosity;
  32. };
  33. u32_le payload_size;
  34. /// Returns true if this is part of a single log message
  35. bool IsSingleMessage() const {
  36. return (flags & Flags::IsHead) && (flags & Flags::IsTail);
  37. }
  38. };
  39. static_assert(sizeof(MessageHeader) == 0x18, "MessageHeader is incorrect size");
  40. /// Log field type
  41. enum class Field : u8 {
  42. Message = 2,
  43. Line = 3,
  44. Filename = 4,
  45. Function = 5,
  46. Module = 6,
  47. Thread = 7,
  48. };
  49. /**
  50. * LM::Initialize service function
  51. * Inputs:
  52. * 0: 0x00000000
  53. * Outputs:
  54. * 0: ResultCode
  55. */
  56. void Log(Kernel::HLERequestContext& ctx) {
  57. // This function only succeeds - Get that out of the way
  58. IPC::RequestBuilder rb{ctx, 1};
  59. rb.Push(RESULT_SUCCESS);
  60. // Read MessageHeader, despite not doing anything with it right now
  61. MessageHeader header{};
  62. VAddr addr{ctx.BufferDescriptorX()[0].Address()};
  63. const VAddr end_addr{addr + ctx.BufferDescriptorX()[0].size};
  64. Memory::ReadBlock(addr, &header, sizeof(MessageHeader));
  65. addr += sizeof(MessageHeader);
  66. if (!header.IsSingleMessage()) {
  67. UNIMPLEMENTED_MSG("Multi message logs are unimplemeneted");
  68. }
  69. // Parse out log metadata
  70. u32 line{};
  71. std::string message, filename, function;
  72. while (addr < end_addr) {
  73. const Field field{static_cast<Field>(Memory::Read8(addr++))};
  74. size_t length{Memory::Read8(addr++)};
  75. switch (field) {
  76. case Field::Message:
  77. message = Memory::ReadCString(addr, length);
  78. break;
  79. case Field::Line:
  80. line = Memory::Read32(addr);
  81. break;
  82. case Field::Filename:
  83. filename = Memory::ReadCString(addr, length);
  84. break;
  85. case Field::Function:
  86. function = Memory::ReadCString(addr, length);
  87. break;
  88. }
  89. addr += length;
  90. }
  91. // Empty log - nothing to do here
  92. if (message.size() <= 1) {
  93. return;
  94. }
  95. // Remove trailing new line
  96. if (message[message.length() - 1] == '\n') {
  97. message.erase(message.length() - 1);
  98. }
  99. // Format a nicely printable string out of the log metadata
  100. std::string output;
  101. if (filename.size()) {
  102. output += filename + ':';
  103. }
  104. if (function.size()) {
  105. output += function + ':';
  106. }
  107. if (line) {
  108. output += std::to_string(line) + ':';
  109. }
  110. if (output.back() == ':') {
  111. output += ' ';
  112. }
  113. output += message;
  114. LOG_DEBUG(Debug_Emulated, "%s", output.c_str());
  115. }
  116. };
  117. void InstallInterfaces(SM::ServiceManager& service_manager) {
  118. std::make_shared<LM>()->InstallAsService(service_manager);
  119. }
  120. /**
  121. * LM::Initialize service function
  122. * Inputs:
  123. * 0: 0x00000000
  124. * Outputs:
  125. * 0: ResultCode
  126. */
  127. void LM::Initialize(Kernel::HLERequestContext& ctx) {
  128. auto client_port = std::make_shared<Logger>()->CreatePort();
  129. auto session = client_port->Connect();
  130. if (session.Succeeded()) {
  131. LOG_DEBUG(Service_SM, "called, initialized logger -> session=%u",
  132. (*session)->GetObjectId());
  133. IPC::RequestBuilder rb{ctx, 1, 0, 1};
  134. rb.Push(RESULT_SUCCESS);
  135. rb.PushObjects(std::move(session).Unwrap());
  136. registered_loggers.emplace_back(std::move(client_port));
  137. } else {
  138. UNIMPLEMENTED();
  139. }
  140. LOG_INFO(Service_SM, "called");
  141. }
  142. LM::LM() : ServiceFramework("lm") {
  143. static const FunctionInfo functions[] = {
  144. {0x00000000, &LM::Initialize, "Initialize"},
  145. };
  146. RegisterHandlers(functions);
  147. }
  148. } // namespace LM
  149. } // namespace Service