lm.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright 2018 yuzu emulator team
  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. Skip = 1,
  43. Message = 2,
  44. Line = 3,
  45. Filename = 4,
  46. Function = 5,
  47. Module = 6,
  48. Thread = 7,
  49. };
  50. /**
  51. * LM::Initialize service function
  52. * Inputs:
  53. * 0: 0x00000000
  54. * Outputs:
  55. * 0: ResultCode
  56. */
  57. void Log(Kernel::HLERequestContext& ctx) {
  58. // This function only succeeds - Get that out of the way
  59. IPC::RequestBuilder rb{ctx, 1};
  60. rb.Push(RESULT_SUCCESS);
  61. // Read MessageHeader, despite not doing anything with it right now
  62. MessageHeader header{};
  63. VAddr addr{ctx.BufferDescriptorX()[0].Address()};
  64. const VAddr end_addr{addr + ctx.BufferDescriptorX()[0].size};
  65. Memory::ReadBlock(addr, &header, sizeof(MessageHeader));
  66. addr += sizeof(MessageHeader);
  67. if (!header.IsSingleMessage()) {
  68. LOG_WARNING(Service, "Multi message logs are unimplemeneted");
  69. return;
  70. }
  71. // Parse out log metadata
  72. u32 line{};
  73. std::string message, filename, function;
  74. while (addr < end_addr) {
  75. const Field field{static_cast<Field>(Memory::Read8(addr++))};
  76. size_t length{Memory::Read8(addr++)};
  77. if (static_cast<Field>(Memory::Read8(addr)) == Field::Skip) {
  78. ++addr;
  79. }
  80. switch (field) {
  81. case Field::Message:
  82. message = Memory::ReadCString(addr, length);
  83. break;
  84. case Field::Line:
  85. line = Memory::Read32(addr);
  86. break;
  87. case Field::Filename:
  88. filename = Memory::ReadCString(addr, length);
  89. break;
  90. case Field::Function:
  91. function = Memory::ReadCString(addr, length);
  92. break;
  93. }
  94. addr += length;
  95. }
  96. // Empty log - nothing to do here
  97. if (message.empty()) {
  98. return;
  99. }
  100. // Format a nicely printable string out of the log metadata
  101. std::string output;
  102. if (filename.size()) {
  103. output += filename + ':';
  104. }
  105. if (function.size()) {
  106. output += function + ':';
  107. }
  108. if (line) {
  109. output += std::to_string(line) + ':';
  110. }
  111. if (output.back() == ':') {
  112. output += ' ';
  113. }
  114. output += message;
  115. LOG_DEBUG(Debug_Emulated, "%s", output.c_str());
  116. }
  117. };
  118. void InstallInterfaces(SM::ServiceManager& service_manager) {
  119. std::make_shared<LM>()->InstallAsService(service_manager);
  120. }
  121. /**
  122. * LM::Initialize service function
  123. * Inputs:
  124. * 0: 0x00000000
  125. * Outputs:
  126. * 0: ResultCode
  127. */
  128. void LM::Initialize(Kernel::HLERequestContext& ctx) {
  129. auto client_port = std::make_shared<Logger>()->CreatePort();
  130. auto session = client_port->Connect();
  131. if (session.Succeeded()) {
  132. LOG_DEBUG(Service_SM, "called, initialized logger -> session=%u",
  133. (*session)->GetObjectId());
  134. IPC::RequestBuilder rb{ctx, 2, 0, 1};
  135. rb.Push(RESULT_SUCCESS);
  136. rb.PushMoveObjects(std::move(session).Unwrap());
  137. registered_loggers.emplace_back(std::move(client_port));
  138. } else {
  139. UNIMPLEMENTED();
  140. }
  141. LOG_INFO(Service_SM, "called");
  142. }
  143. LM::LM() : ServiceFramework("lm") {
  144. static const FunctionInfo functions[] = {
  145. {0x00000000, &LM::Initialize, "Initialize"},
  146. };
  147. RegisterHandlers(functions);
  148. }
  149. } // namespace LM
  150. } // namespace Service