lm.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. 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. LOG_WARNING(Service, "Multi message logs are unimplemeneted");
  68. return;
  69. }
  70. // Parse out log metadata
  71. u32 line{};
  72. std::string message, filename, function;
  73. while (addr < end_addr) {
  74. const Field field{static_cast<Field>(Memory::Read8(addr++))};
  75. size_t length{Memory::Read8(addr++)};
  76. switch (field) {
  77. case Field::Message:
  78. message = Memory::ReadCString(addr, length);
  79. break;
  80. case Field::Line:
  81. line = Memory::Read32(addr);
  82. break;
  83. case Field::Filename:
  84. filename = Memory::ReadCString(addr, length);
  85. break;
  86. case Field::Function:
  87. function = Memory::ReadCString(addr, length);
  88. break;
  89. }
  90. addr += length;
  91. }
  92. // Empty log - nothing to do here
  93. if (message.empty()) {
  94. return;
  95. }
  96. // Format a nicely printable string out of the log metadata
  97. std::string output;
  98. if (filename.size()) {
  99. output += filename + ':';
  100. }
  101. if (function.size()) {
  102. output += function + ':';
  103. }
  104. if (line) {
  105. output += std::to_string(line) + ':';
  106. }
  107. if (output.back() == ':') {
  108. output += ' ';
  109. }
  110. output += message;
  111. LOG_DEBUG(Debug_Emulated, "%s", output.c_str());
  112. }
  113. };
  114. void InstallInterfaces(SM::ServiceManager& service_manager) {
  115. std::make_shared<LM>()->InstallAsService(service_manager);
  116. }
  117. /**
  118. * LM::Initialize service function
  119. * Inputs:
  120. * 0: 0x00000000
  121. * Outputs:
  122. * 0: ResultCode
  123. */
  124. void LM::Initialize(Kernel::HLERequestContext& ctx) {
  125. auto client_port = std::make_shared<Logger>()->CreatePort();
  126. auto session = client_port->Connect();
  127. if (session.Succeeded()) {
  128. LOG_DEBUG(Service_SM, "called, initialized logger -> session=%u",
  129. (*session)->GetObjectId());
  130. IPC::RequestBuilder rb{ctx, 1, 0, 1};
  131. rb.Push(RESULT_SUCCESS);
  132. rb.PushMoveObjects(std::move(session).Unwrap());
  133. registered_loggers.emplace_back(std::move(client_port));
  134. } else {
  135. UNIMPLEMENTED();
  136. }
  137. LOG_INFO(Service_SM, "called");
  138. }
  139. LM::LM() : ServiceFramework("lm") {
  140. static const FunctionInfo functions[] = {
  141. {0x00000000, &LM::Initialize, "Initialize"},
  142. };
  143. RegisterHandlers(functions);
  144. }
  145. } // namespace LM
  146. } // namespace Service