reporter.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. // Copyright 2019 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <ctime>
  5. #include <fstream>
  6. #include <iomanip>
  7. #include <fmt/chrono.h>
  8. #include <fmt/format.h>
  9. #include <fmt/ostream.h>
  10. #include <nlohmann/json.hpp>
  11. #include "common/file_util.h"
  12. #include "common/hex_util.h"
  13. #include "common/scm_rev.h"
  14. #include "core/arm/arm_interface.h"
  15. #include "core/core.h"
  16. #include "core/hle/kernel/hle_ipc.h"
  17. #include "core/hle/kernel/memory/page_table.h"
  18. #include "core/hle/kernel/process.h"
  19. #include "core/hle/result.h"
  20. #include "core/hle/service/lm/manager.h"
  21. #include "core/memory.h"
  22. #include "core/reporter.h"
  23. #include "core/settings.h"
  24. namespace {
  25. std::string GetPath(std::string_view type, u64 title_id, std::string_view timestamp) {
  26. return fmt::format("{}{}/{:016X}_{}.json",
  27. Common::FS::GetUserPath(Common::FS::UserPath::LogDir), type, title_id,
  28. timestamp);
  29. }
  30. std::string GetTimestamp() {
  31. const auto time = std::time(nullptr);
  32. return fmt::format("{:%FT%H-%M-%S}", *std::localtime(&time));
  33. }
  34. using namespace nlohmann;
  35. void SaveToFile(json json, const std::string& filename) {
  36. if (!Common::FS::CreateFullPath(filename)) {
  37. LOG_ERROR(Core, "Failed to create path for '{}' to save report!", filename);
  38. return;
  39. }
  40. std::ofstream file(
  41. Common::FS::SanitizePath(filename, Common::FS::DirectorySeparator::PlatformDefault));
  42. file << std::setw(4) << json << std::endl;
  43. }
  44. json GetYuzuVersionData() {
  45. return {
  46. {"scm_rev", std::string(Common::g_scm_rev)},
  47. {"scm_branch", std::string(Common::g_scm_branch)},
  48. {"scm_desc", std::string(Common::g_scm_desc)},
  49. {"build_name", std::string(Common::g_build_name)},
  50. {"build_date", std::string(Common::g_build_date)},
  51. {"build_fullname", std::string(Common::g_build_fullname)},
  52. {"build_version", std::string(Common::g_build_version)},
  53. {"shader_cache_version", std::string(Common::g_shader_cache_version)},
  54. };
  55. }
  56. json GetReportCommonData(u64 title_id, ResultCode result, const std::string& timestamp,
  57. std::optional<u128> user_id = {}) {
  58. auto out = json{
  59. {"title_id", fmt::format("{:016X}", title_id)},
  60. {"result_raw", fmt::format("{:08X}", result.raw)},
  61. {"result_module", fmt::format("{:08X}", static_cast<u32>(result.module.Value()))},
  62. {"result_description", fmt::format("{:08X}", result.description.Value())},
  63. {"timestamp", timestamp},
  64. };
  65. if (user_id.has_value()) {
  66. out["user_id"] = fmt::format("{:016X}{:016X}", (*user_id)[1], (*user_id)[0]);
  67. }
  68. return out;
  69. }
  70. json GetProcessorStateData(const std::string& architecture, u64 entry_point, u64 sp, u64 pc,
  71. u64 pstate, std::array<u64, 31> registers,
  72. std::optional<std::array<u64, 32>> backtrace = {}) {
  73. auto out = json{
  74. {"entry_point", fmt::format("{:016X}", entry_point)},
  75. {"sp", fmt::format("{:016X}", sp)},
  76. {"pc", fmt::format("{:016X}", pc)},
  77. {"pstate", fmt::format("{:016X}", pstate)},
  78. {"architecture", architecture},
  79. };
  80. auto registers_out = json::object();
  81. for (std::size_t i = 0; i < registers.size(); ++i) {
  82. registers_out[fmt::format("X{:02d}", i)] = fmt::format("{:016X}", registers[i]);
  83. }
  84. out["registers"] = std::move(registers_out);
  85. if (backtrace.has_value()) {
  86. auto backtrace_out = json::array();
  87. for (const auto& entry : *backtrace) {
  88. backtrace_out.push_back(fmt::format("{:016X}", entry));
  89. }
  90. out["backtrace"] = std::move(backtrace_out);
  91. }
  92. return out;
  93. }
  94. json GetProcessorStateDataAuto(Core::System& system) {
  95. const auto* process{system.CurrentProcess()};
  96. auto& arm{system.CurrentArmInterface()};
  97. Core::ARM_Interface::ThreadContext64 context{};
  98. arm.SaveContext(context);
  99. return GetProcessorStateData(process->Is64BitProcess() ? "AArch64" : "AArch32",
  100. process->PageTable().GetCodeRegionStart(), context.sp, context.pc,
  101. context.pstate, context.cpu_registers);
  102. }
  103. json GetBacktraceData(Core::System& system) {
  104. auto out = json::array();
  105. const auto& backtrace{system.CurrentArmInterface().GetBacktrace()};
  106. for (const auto& entry : backtrace) {
  107. out.push_back({
  108. {"module", entry.module},
  109. {"address", fmt::format("{:016X}", entry.address)},
  110. {"original_address", fmt::format("{:016X}", entry.original_address)},
  111. {"offset", fmt::format("{:016X}", entry.offset)},
  112. {"symbol_name", entry.name},
  113. });
  114. }
  115. return out;
  116. }
  117. json GetFullDataAuto(const std::string& timestamp, u64 title_id, Core::System& system) {
  118. json out;
  119. out["yuzu_version"] = GetYuzuVersionData();
  120. out["report_common"] = GetReportCommonData(title_id, RESULT_SUCCESS, timestamp);
  121. out["processor_state"] = GetProcessorStateDataAuto(system);
  122. out["backtrace"] = GetBacktraceData(system);
  123. return out;
  124. }
  125. template <bool read_value, typename DescriptorType>
  126. json GetHLEBufferDescriptorData(const std::vector<DescriptorType>& buffer,
  127. Core::Memory::Memory& memory) {
  128. auto buffer_out = json::array();
  129. for (const auto& desc : buffer) {
  130. auto entry = json{
  131. {"address", fmt::format("{:016X}", desc.Address())},
  132. {"size", fmt::format("{:016X}", desc.Size())},
  133. };
  134. if constexpr (read_value) {
  135. std::vector<u8> data(desc.Size());
  136. memory.ReadBlock(desc.Address(), data.data(), desc.Size());
  137. entry["data"] = Common::HexToString(data);
  138. }
  139. buffer_out.push_back(std::move(entry));
  140. }
  141. return buffer_out;
  142. }
  143. json GetHLERequestContextData(Kernel::HLERequestContext& ctx, Core::Memory::Memory& memory) {
  144. json out;
  145. auto cmd_buf = json::array();
  146. for (std::size_t i = 0; i < IPC::COMMAND_BUFFER_LENGTH; ++i) {
  147. cmd_buf.push_back(fmt::format("{:08X}", ctx.CommandBuffer()[i]));
  148. }
  149. out["command_buffer"] = std::move(cmd_buf);
  150. out["buffer_descriptor_a"] = GetHLEBufferDescriptorData<true>(ctx.BufferDescriptorA(), memory);
  151. out["buffer_descriptor_b"] = GetHLEBufferDescriptorData<false>(ctx.BufferDescriptorB(), memory);
  152. out["buffer_descriptor_c"] = GetHLEBufferDescriptorData<false>(ctx.BufferDescriptorC(), memory);
  153. out["buffer_descriptor_x"] = GetHLEBufferDescriptorData<true>(ctx.BufferDescriptorX(), memory);
  154. return out;
  155. }
  156. } // Anonymous namespace
  157. namespace Core {
  158. Reporter::Reporter(System& system) : system(system) {}
  159. Reporter::~Reporter() = default;
  160. void Reporter::SaveCrashReport(u64 title_id, ResultCode result, u64 set_flags, u64 entry_point,
  161. u64 sp, u64 pc, u64 pstate, u64 afsr0, u64 afsr1, u64 esr, u64 far,
  162. const std::array<u64, 31>& registers,
  163. const std::array<u64, 32>& backtrace, u32 backtrace_size,
  164. const std::string& arch, u32 unk10) const {
  165. if (!IsReportingEnabled()) {
  166. return;
  167. }
  168. const auto timestamp = GetTimestamp();
  169. json out;
  170. out["yuzu_version"] = GetYuzuVersionData();
  171. out["report_common"] = GetReportCommonData(title_id, result, timestamp);
  172. auto proc_out = GetProcessorStateData(arch, entry_point, sp, pc, pstate, registers, backtrace);
  173. proc_out["set_flags"] = fmt::format("{:016X}", set_flags);
  174. proc_out["afsr0"] = fmt::format("{:016X}", afsr0);
  175. proc_out["afsr1"] = fmt::format("{:016X}", afsr1);
  176. proc_out["esr"] = fmt::format("{:016X}", esr);
  177. proc_out["far"] = fmt::format("{:016X}", far);
  178. proc_out["backtrace_size"] = fmt::format("{:08X}", backtrace_size);
  179. proc_out["unknown_10"] = fmt::format("{:08X}", unk10);
  180. out["processor_state"] = std::move(proc_out);
  181. SaveToFile(std::move(out), GetPath("crash_report", title_id, timestamp));
  182. }
  183. void Reporter::SaveSvcBreakReport(u32 type, bool signal_debugger, u64 info1, u64 info2,
  184. std::optional<std::vector<u8>> resolved_buffer) const {
  185. if (!IsReportingEnabled()) {
  186. return;
  187. }
  188. const auto timestamp = GetTimestamp();
  189. const auto title_id = system.CurrentProcess()->GetTitleID();
  190. auto out = GetFullDataAuto(timestamp, title_id, system);
  191. auto break_out = json{
  192. {"type", fmt::format("{:08X}", type)},
  193. {"signal_debugger", fmt::format("{}", signal_debugger)},
  194. {"info1", fmt::format("{:016X}", info1)},
  195. {"info2", fmt::format("{:016X}", info2)},
  196. };
  197. if (resolved_buffer.has_value()) {
  198. break_out["debug_buffer"] = Common::HexToString(*resolved_buffer);
  199. }
  200. out["svc_break"] = std::move(break_out);
  201. SaveToFile(std::move(out), GetPath("svc_break_report", title_id, timestamp));
  202. }
  203. void Reporter::SaveUnimplementedFunctionReport(Kernel::HLERequestContext& ctx, u32 command_id,
  204. const std::string& name,
  205. const std::string& service_name) const {
  206. if (!IsReportingEnabled()) {
  207. return;
  208. }
  209. const auto timestamp = GetTimestamp();
  210. const auto title_id = system.CurrentProcess()->GetTitleID();
  211. auto out = GetFullDataAuto(timestamp, title_id, system);
  212. auto function_out = GetHLERequestContextData(ctx, system.Memory());
  213. function_out["command_id"] = command_id;
  214. function_out["function_name"] = name;
  215. function_out["service_name"] = service_name;
  216. out["function"] = std::move(function_out);
  217. SaveToFile(std::move(out), GetPath("unimpl_func_report", title_id, timestamp));
  218. }
  219. void Reporter::SaveUnimplementedAppletReport(
  220. u32 applet_id, u32 common_args_version, u32 library_version, u32 theme_color,
  221. bool startup_sound, u64 system_tick, std::vector<std::vector<u8>> normal_channel,
  222. std::vector<std::vector<u8>> interactive_channel) const {
  223. if (!IsReportingEnabled()) {
  224. return;
  225. }
  226. const auto timestamp = GetTimestamp();
  227. const auto title_id = system.CurrentProcess()->GetTitleID();
  228. auto out = GetFullDataAuto(timestamp, title_id, system);
  229. out["applet_common_args"] = {
  230. {"applet_id", fmt::format("{:02X}", applet_id)},
  231. {"common_args_version", fmt::format("{:08X}", common_args_version)},
  232. {"library_version", fmt::format("{:08X}", library_version)},
  233. {"theme_color", fmt::format("{:08X}", theme_color)},
  234. {"startup_sound", fmt::format("{}", startup_sound)},
  235. {"system_tick", fmt::format("{:016X}", system_tick)},
  236. };
  237. auto normal_out = json::array();
  238. for (const auto& data : normal_channel) {
  239. normal_out.push_back(Common::HexToString(data));
  240. }
  241. auto interactive_out = json::array();
  242. for (const auto& data : interactive_channel) {
  243. interactive_out.push_back(Common::HexToString(data));
  244. }
  245. out["applet_normal_data"] = std::move(normal_out);
  246. out["applet_interactive_data"] = std::move(interactive_out);
  247. SaveToFile(std::move(out), GetPath("unimpl_applet_report", title_id, timestamp));
  248. }
  249. void Reporter::SavePlayReport(PlayReportType type, u64 title_id, std::vector<std::vector<u8>> data,
  250. std::optional<u64> process_id, std::optional<u128> user_id) const {
  251. if (!IsReportingEnabled()) {
  252. return;
  253. }
  254. const auto timestamp = GetTimestamp();
  255. json out;
  256. out["yuzu_version"] = GetYuzuVersionData();
  257. out["report_common"] = GetReportCommonData(title_id, RESULT_SUCCESS, timestamp, user_id);
  258. auto data_out = json::array();
  259. for (const auto& d : data) {
  260. data_out.push_back(Common::HexToString(d));
  261. }
  262. if (process_id.has_value()) {
  263. out["play_report_process_id"] = fmt::format("{:016X}", *process_id);
  264. }
  265. out["play_report_type"] = fmt::format("{:02}", static_cast<u8>(type));
  266. out["play_report_data"] = std::move(data_out);
  267. SaveToFile(std::move(out), GetPath("play_report", title_id, timestamp));
  268. }
  269. void Reporter::SaveErrorReport(u64 title_id, ResultCode result,
  270. std::optional<std::string> custom_text_main,
  271. std::optional<std::string> custom_text_detail) const {
  272. if (!IsReportingEnabled()) {
  273. return;
  274. }
  275. const auto timestamp = GetTimestamp();
  276. json out;
  277. out["yuzu_version"] = GetYuzuVersionData();
  278. out["report_common"] = GetReportCommonData(title_id, result, timestamp);
  279. out["processor_state"] = GetProcessorStateDataAuto(system);
  280. out["backtrace"] = GetBacktraceData(system);
  281. out["error_custom_text"] = {
  282. {"main", *custom_text_main},
  283. {"detail", *custom_text_detail},
  284. };
  285. SaveToFile(std::move(out), GetPath("error_report", title_id, timestamp));
  286. }
  287. void Reporter::SaveLogReport(u32 destination, std::vector<Service::LM::LogMessage> messages) const {
  288. if (!IsReportingEnabled()) {
  289. return;
  290. }
  291. const auto timestamp = GetTimestamp();
  292. json out;
  293. out["yuzu_version"] = GetYuzuVersionData();
  294. out["report_common"] =
  295. GetReportCommonData(system.CurrentProcess()->GetTitleID(), RESULT_SUCCESS, timestamp);
  296. out["log_destination"] =
  297. fmt::format("{}", static_cast<Service::LM::DestinationFlag>(destination));
  298. auto json_messages = json::array();
  299. std::transform(messages.begin(), messages.end(), std::back_inserter(json_messages),
  300. [](const Service::LM::LogMessage& message) {
  301. json out;
  302. out["is_head"] = fmt::format("{}", message.header.IsHeadLog());
  303. out["is_tail"] = fmt::format("{}", message.header.IsTailLog());
  304. out["pid"] = fmt::format("{:016X}", message.header.pid);
  305. out["thread_context"] =
  306. fmt::format("{:016X}", message.header.thread_context);
  307. out["payload_size"] = fmt::format("{:016X}", message.header.payload_size);
  308. out["flags"] = fmt::format("{:04X}", message.header.flags.Value());
  309. out["severity"] = fmt::format("{}", message.header.severity.Value());
  310. out["verbosity"] = fmt::format("{:02X}", message.header.verbosity);
  311. auto fields = json::array();
  312. std::transform(message.fields.begin(), message.fields.end(),
  313. std::back_inserter(fields), [](const auto& kv) {
  314. json out;
  315. out["type"] = fmt::format("{}", kv.first);
  316. out["data"] =
  317. Service::LM::FormatField(kv.first, kv.second);
  318. return out;
  319. });
  320. out["fields"] = std::move(fields);
  321. return out;
  322. });
  323. out["log_messages"] = std::move(json_messages);
  324. SaveToFile(std::move(out),
  325. GetPath("log_report", system.CurrentProcess()->GetTitleID(), timestamp));
  326. }
  327. void Reporter::SaveFilesystemAccessReport(Service::FileSystem::LogMode log_mode,
  328. std::string log_message) const {
  329. if (!IsReportingEnabled())
  330. return;
  331. const auto timestamp = GetTimestamp();
  332. const auto title_id = system.CurrentProcess()->GetTitleID();
  333. json out;
  334. out["yuzu_version"] = GetYuzuVersionData();
  335. out["report_common"] = GetReportCommonData(title_id, RESULT_SUCCESS, timestamp);
  336. out["log_mode"] = fmt::format("{:08X}", static_cast<u32>(log_mode));
  337. out["log_message"] = std::move(log_message);
  338. SaveToFile(std::move(out), GetPath("filesystem_access_report", title_id, timestamp));
  339. }
  340. void Reporter::SaveUserReport() const {
  341. if (!IsReportingEnabled()) {
  342. return;
  343. }
  344. const auto timestamp = GetTimestamp();
  345. const auto title_id = system.CurrentProcess()->GetTitleID();
  346. SaveToFile(GetFullDataAuto(timestamp, title_id, system),
  347. GetPath("user_report", title_id, timestamp));
  348. }
  349. bool Reporter::IsReportingEnabled() const {
  350. return Settings::values.reporting_services;
  351. }
  352. } // namespace Core