reporter.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <ctime>
  4. #include <fstream>
  5. #include <iomanip>
  6. #include <fmt/chrono.h>
  7. #include <fmt/format.h>
  8. #include <nlohmann/json.hpp>
  9. #include "common/fs/file.h"
  10. #include "common/fs/fs.h"
  11. #include "common/fs/path_util.h"
  12. #include "common/hex_util.h"
  13. #include "common/scm_rev.h"
  14. #include "common/settings.h"
  15. #include "core/arm/arm_interface.h"
  16. #include "core/core.h"
  17. #include "core/hle/kernel/k_page_table.h"
  18. #include "core/hle/kernel/k_process.h"
  19. #include "core/hle/result.h"
  20. #include "core/hle/service/hle_ipc.h"
  21. #include "core/memory.h"
  22. #include "core/reporter.h"
  23. namespace {
  24. std::filesystem::path GetPath(std::string_view type, u64 title_id, std::string_view timestamp) {
  25. return Common::FS::GetYuzuPath(Common::FS::YuzuPath::LogDir) / type /
  26. fmt::format("{:016X}_{}.json", title_id, timestamp);
  27. }
  28. std::string GetTimestamp() {
  29. const auto time = std::time(nullptr);
  30. return fmt::format("{:%FT%H-%M-%S}", *std::localtime(&time));
  31. }
  32. using namespace nlohmann;
  33. void SaveToFile(const json& json, const std::filesystem::path& filename) {
  34. if (!Common::FS::CreateParentDirs(filename)) {
  35. LOG_ERROR(Core, "Failed to create path for '{}' to save report!",
  36. Common::FS::PathToUTF8String(filename));
  37. return;
  38. }
  39. std::ofstream file;
  40. Common::FS::OpenFileStream(file, filename, std::ios_base::out | std::ios_base::trunc);
  41. file << std::setw(4) << json << std::endl;
  42. }
  43. json GetYuzuVersionData() {
  44. return {
  45. {"scm_rev", std::string(Common::g_scm_rev)},
  46. {"scm_branch", std::string(Common::g_scm_branch)},
  47. {"scm_desc", std::string(Common::g_scm_desc)},
  48. {"build_name", std::string(Common::g_build_name)},
  49. {"build_date", std::string(Common::g_build_date)},
  50. {"build_fullname", std::string(Common::g_build_fullname)},
  51. {"build_version", std::string(Common::g_build_version)},
  52. };
  53. }
  54. json GetReportCommonData(u64 title_id, Result result, const std::string& timestamp,
  55. std::optional<u128> user_id = {}) {
  56. auto out = json{
  57. {"title_id", fmt::format("{:016X}", title_id)},
  58. {"result_raw", fmt::format("{:08X}", result.raw)},
  59. {"result_module", fmt::format("{:08X}", static_cast<u32>(result.module.Value()))},
  60. {"result_description", fmt::format("{:08X}", result.description.Value())},
  61. {"timestamp", timestamp},
  62. };
  63. if (user_id.has_value()) {
  64. out["user_id"] = fmt::format("{:016X}{:016X}", (*user_id)[1], (*user_id)[0]);
  65. }
  66. return out;
  67. }
  68. json GetProcessorStateData(const std::string& architecture, u64 entry_point, u64 sp, u64 pc,
  69. u64 pstate, const std::array<u64, 31>& registers,
  70. const std::optional<std::array<u64, 32>>& backtrace = {}) {
  71. auto out = json{
  72. {"entry_point", fmt::format("{:016X}", entry_point)},
  73. {"sp", fmt::format("{:016X}", sp)},
  74. {"pc", fmt::format("{:016X}", pc)},
  75. {"pstate", fmt::format("{:016X}", pstate)},
  76. {"architecture", architecture},
  77. };
  78. auto registers_out = json::object();
  79. for (std::size_t i = 0; i < registers.size(); ++i) {
  80. registers_out[fmt::format("X{:02d}", i)] = fmt::format("{:016X}", registers[i]);
  81. }
  82. out["registers"] = std::move(registers_out);
  83. if (backtrace.has_value()) {
  84. auto backtrace_out = json::array();
  85. for (const auto& entry : *backtrace) {
  86. backtrace_out.push_back(fmt::format("{:016X}", entry));
  87. }
  88. out["backtrace"] = std::move(backtrace_out);
  89. }
  90. return out;
  91. }
  92. json GetFullDataAuto(const std::string& timestamp, u64 title_id, Core::System& system) {
  93. json out;
  94. out["yuzu_version"] = GetYuzuVersionData();
  95. out["report_common"] = GetReportCommonData(title_id, ResultSuccess, timestamp);
  96. return out;
  97. }
  98. template <bool read_value, typename DescriptorType>
  99. json GetHLEBufferDescriptorData(const std::vector<DescriptorType>& buffer,
  100. Core::Memory::Memory& memory) {
  101. auto buffer_out = json::array();
  102. for (const auto& desc : buffer) {
  103. auto entry = json{
  104. {"address", fmt::format("{:016X}", desc.Address())},
  105. {"size", fmt::format("{:016X}", desc.Size())},
  106. };
  107. if constexpr (read_value) {
  108. std::vector<u8> data(desc.Size());
  109. memory.ReadBlock(desc.Address(), data.data(), desc.Size());
  110. entry["data"] = Common::HexToString(data);
  111. }
  112. buffer_out.push_back(std::move(entry));
  113. }
  114. return buffer_out;
  115. }
  116. json GetHLERequestContextData(Service::HLERequestContext& ctx, Core::Memory::Memory& memory) {
  117. json out;
  118. auto cmd_buf = json::array();
  119. for (std::size_t i = 0; i < IPC::COMMAND_BUFFER_LENGTH; ++i) {
  120. cmd_buf.push_back(fmt::format("{:08X}", ctx.CommandBuffer()[i]));
  121. }
  122. out["command_buffer"] = std::move(cmd_buf);
  123. out["buffer_descriptor_a"] = GetHLEBufferDescriptorData<true>(ctx.BufferDescriptorA(), memory);
  124. out["buffer_descriptor_b"] = GetHLEBufferDescriptorData<false>(ctx.BufferDescriptorB(), memory);
  125. out["buffer_descriptor_c"] = GetHLEBufferDescriptorData<false>(ctx.BufferDescriptorC(), memory);
  126. out["buffer_descriptor_x"] = GetHLEBufferDescriptorData<true>(ctx.BufferDescriptorX(), memory);
  127. return out;
  128. }
  129. } // Anonymous namespace
  130. namespace Core {
  131. Reporter::Reporter(System& system_) : system(system_) {
  132. ClearFSAccessLog();
  133. }
  134. Reporter::~Reporter() = default;
  135. void Reporter::SaveCrashReport(u64 title_id, Result result, u64 set_flags, u64 entry_point, u64 sp,
  136. u64 pc, u64 pstate, u64 afsr0, u64 afsr1, u64 esr, u64 far,
  137. const std::array<u64, 31>& registers,
  138. const std::array<u64, 32>& backtrace, u32 backtrace_size,
  139. const std::string& arch, u32 unk10) const {
  140. if (!IsReportingEnabled()) {
  141. return;
  142. }
  143. const auto timestamp = GetTimestamp();
  144. json out;
  145. out["yuzu_version"] = GetYuzuVersionData();
  146. out["report_common"] = GetReportCommonData(title_id, result, timestamp);
  147. auto proc_out = GetProcessorStateData(arch, entry_point, sp, pc, pstate, registers, backtrace);
  148. proc_out["set_flags"] = fmt::format("{:016X}", set_flags);
  149. proc_out["afsr0"] = fmt::format("{:016X}", afsr0);
  150. proc_out["afsr1"] = fmt::format("{:016X}", afsr1);
  151. proc_out["esr"] = fmt::format("{:016X}", esr);
  152. proc_out["far"] = fmt::format("{:016X}", far);
  153. proc_out["backtrace_size"] = fmt::format("{:08X}", backtrace_size);
  154. proc_out["unknown_10"] = fmt::format("{:08X}", unk10);
  155. out["processor_state"] = std::move(proc_out);
  156. SaveToFile(out, GetPath("crash_report", title_id, timestamp));
  157. }
  158. void Reporter::SaveSvcBreakReport(u32 type, bool signal_debugger, u64 info1, u64 info2,
  159. const std::optional<std::vector<u8>>& resolved_buffer) const {
  160. if (!IsReportingEnabled()) {
  161. return;
  162. }
  163. const auto timestamp = GetTimestamp();
  164. const auto title_id = system.GetApplicationProcessProgramID();
  165. auto out = GetFullDataAuto(timestamp, title_id, system);
  166. auto break_out = json{
  167. {"type", fmt::format("{:08X}", type)},
  168. {"signal_debugger", fmt::format("{}", signal_debugger)},
  169. {"info1", fmt::format("{:016X}", info1)},
  170. {"info2", fmt::format("{:016X}", info2)},
  171. };
  172. if (resolved_buffer.has_value()) {
  173. break_out["debug_buffer"] = Common::HexToString(*resolved_buffer);
  174. }
  175. out["svc_break"] = std::move(break_out);
  176. SaveToFile(out, GetPath("svc_break_report", title_id, timestamp));
  177. }
  178. void Reporter::SaveUnimplementedFunctionReport(Service::HLERequestContext& ctx, u32 command_id,
  179. const std::string& name,
  180. const std::string& service_name) const {
  181. if (!IsReportingEnabled()) {
  182. return;
  183. }
  184. const auto timestamp = GetTimestamp();
  185. const auto title_id = system.GetApplicationProcessProgramID();
  186. auto out = GetFullDataAuto(timestamp, title_id, system);
  187. auto function_out = GetHLERequestContextData(ctx, system.ApplicationMemory());
  188. function_out["command_id"] = command_id;
  189. function_out["function_name"] = name;
  190. function_out["service_name"] = service_name;
  191. out["function"] = std::move(function_out);
  192. SaveToFile(out, GetPath("unimpl_func_report", title_id, timestamp));
  193. }
  194. void Reporter::SaveUnimplementedAppletReport(
  195. u32 applet_id, u32 common_args_version, u32 library_version, u32 theme_color,
  196. bool startup_sound, u64 system_tick, const std::vector<std::vector<u8>>& normal_channel,
  197. const std::vector<std::vector<u8>>& interactive_channel) const {
  198. if (!IsReportingEnabled()) {
  199. return;
  200. }
  201. const auto timestamp = GetTimestamp();
  202. const auto title_id = system.GetApplicationProcessProgramID();
  203. auto out = GetFullDataAuto(timestamp, title_id, system);
  204. out["applet_common_args"] = {
  205. {"applet_id", fmt::format("{:02X}", applet_id)},
  206. {"common_args_version", fmt::format("{:08X}", common_args_version)},
  207. {"library_version", fmt::format("{:08X}", library_version)},
  208. {"theme_color", fmt::format("{:08X}", theme_color)},
  209. {"startup_sound", fmt::format("{}", startup_sound)},
  210. {"system_tick", fmt::format("{:016X}", system_tick)},
  211. };
  212. auto normal_out = json::array();
  213. for (const auto& data : normal_channel) {
  214. normal_out.push_back(Common::HexToString(data));
  215. }
  216. auto interactive_out = json::array();
  217. for (const auto& data : interactive_channel) {
  218. interactive_out.push_back(Common::HexToString(data));
  219. }
  220. out["applet_normal_data"] = std::move(normal_out);
  221. out["applet_interactive_data"] = std::move(interactive_out);
  222. SaveToFile(out, GetPath("unimpl_applet_report", title_id, timestamp));
  223. }
  224. void Reporter::SavePlayReport(PlayReportType type, u64 title_id,
  225. const std::vector<std::span<const u8>>& data,
  226. std::optional<u64> process_id, std::optional<u128> user_id) const {
  227. if (!IsReportingEnabled()) {
  228. return;
  229. }
  230. const auto timestamp = GetTimestamp();
  231. json out;
  232. out["yuzu_version"] = GetYuzuVersionData();
  233. out["report_common"] = GetReportCommonData(title_id, ResultSuccess, timestamp, user_id);
  234. auto data_out = json::array();
  235. for (const auto& d : data) {
  236. data_out.push_back(Common::HexToString(d));
  237. }
  238. if (process_id.has_value()) {
  239. out["play_report_process_id"] = fmt::format("{:016X}", *process_id);
  240. }
  241. out["play_report_type"] = fmt::format("{:02}", static_cast<u8>(type));
  242. out["play_report_data"] = std::move(data_out);
  243. SaveToFile(out, GetPath("play_report", title_id, timestamp));
  244. }
  245. void Reporter::SaveErrorReport(u64 title_id, Result result,
  246. const std::optional<std::string>& custom_text_main,
  247. const std::optional<std::string>& custom_text_detail) const {
  248. if (!IsReportingEnabled()) {
  249. return;
  250. }
  251. const auto timestamp = GetTimestamp();
  252. json out;
  253. out["yuzu_version"] = GetYuzuVersionData();
  254. out["report_common"] = GetReportCommonData(title_id, result, timestamp);
  255. out["error_custom_text"] = {
  256. {"main", custom_text_main.value_or("")},
  257. {"detail", custom_text_detail.value_or("")},
  258. };
  259. SaveToFile(out, GetPath("error_report", title_id, timestamp));
  260. }
  261. void Reporter::SaveFSAccessLog(std::string_view log_message) const {
  262. const auto access_log_path =
  263. Common::FS::GetYuzuPath(Common::FS::YuzuPath::SDMCDir) / "FsAccessLog.txt";
  264. void(Common::FS::AppendStringToFile(access_log_path, Common::FS::FileType::TextFile,
  265. log_message));
  266. }
  267. void Reporter::SaveUserReport() const {
  268. if (!IsReportingEnabled()) {
  269. return;
  270. }
  271. const auto timestamp = GetTimestamp();
  272. const auto title_id = system.GetApplicationProcessProgramID();
  273. SaveToFile(GetFullDataAuto(timestamp, title_id, system),
  274. GetPath("user_report", title_id, timestamp));
  275. }
  276. void Reporter::ClearFSAccessLog() const {
  277. const auto access_log_path =
  278. Common::FS::GetYuzuPath(Common::FS::YuzuPath::SDMCDir) / "FsAccessLog.txt";
  279. Common::FS::IOFile access_log_file{access_log_path, Common::FS::FileAccessMode::Write,
  280. Common::FS::FileType::TextFile};
  281. if (!access_log_file.IsOpen()) {
  282. LOG_ERROR(Common_Filesystem, "Failed to clear the filesystem access log.");
  283. }
  284. }
  285. bool Reporter::IsReportingEnabled() const {
  286. return Settings::values.reporting_services.GetValue();
  287. }
  288. } // namespace Core