diff options
| author | 2019-05-17 21:45:56 -0400 | |
|---|---|---|
| committer | 2019-05-25 16:09:20 -0400 | |
| commit | 2dde8f5cfe81648d05d60285ab5b17a6f61c486e (patch) | |
| tree | 7d9c44658779ad92d66c54069ec1a3fa4d437aed /src/core/reporter.cpp | |
| parent | qt: Make UI option for 'Reporting Services' temporary (diff) | |
| download | yuzu-2dde8f5cfe81648d05d60285ab5b17a6f61c486e.tar.gz yuzu-2dde8f5cfe81648d05d60285ab5b17a6f61c486e.tar.xz yuzu-2dde8f5cfe81648d05d60285ab5b17a6f61c486e.zip | |
core: Add Reporter class to take/save reports
Diffstat (limited to 'src/core/reporter.cpp')
| -rw-r--r-- | src/core/reporter.cpp | 351 |
1 files changed, 351 insertions, 0 deletions
diff --git a/src/core/reporter.cpp b/src/core/reporter.cpp new file mode 100644 index 000000000..95dcfffb5 --- /dev/null +++ b/src/core/reporter.cpp | |||
| @@ -0,0 +1,351 @@ | |||
| 1 | // Copyright 2019 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <fstream> | ||
| 6 | #include <json.hpp> | ||
| 7 | #include "common/file_util.h" | ||
| 8 | #include "common/hex_util.h" | ||
| 9 | #include "common/scm_rev.h" | ||
| 10 | #include "core/arm/arm_interface.h" | ||
| 11 | #include "core/core.h" | ||
| 12 | #include "core/hle/kernel/hle_ipc.h" | ||
| 13 | #include "core/hle/kernel/process.h" | ||
| 14 | #include "core/reporter.h" | ||
| 15 | #include "core/settings.h" | ||
| 16 | #include "fmt/time.h" | ||
| 17 | |||
| 18 | namespace { | ||
| 19 | |||
| 20 | std::string GetPath(std::string_view type, u64 title_id, std::string_view timestamp) { | ||
| 21 | return fmt::format("{}{}/{:016X}_{}.json", FileUtil::GetUserPath(FileUtil::UserPath::LogDir), | ||
| 22 | type, title_id, timestamp); | ||
| 23 | } | ||
| 24 | |||
| 25 | std::string GetTimestamp() { | ||
| 26 | const auto time = std::time(nullptr); | ||
| 27 | return fmt::format("{:%FT%H-%M-%S}", *std::localtime(&time)); | ||
| 28 | } | ||
| 29 | |||
| 30 | using namespace nlohmann; | ||
| 31 | |||
| 32 | void SaveToFile(const json& json, const std::string& filename) { | ||
| 33 | FileUtil::CreateFullPath(filename); | ||
| 34 | std::ofstream file( | ||
| 35 | FileUtil::SanitizePath(filename, FileUtil::DirectorySeparator::PlatformDefault)); | ||
| 36 | file << std::setw(4) << json << std::endl; | ||
| 37 | file.flush(); | ||
| 38 | file.close(); | ||
| 39 | } | ||
| 40 | |||
| 41 | json GetYuzuVersionData() { | ||
| 42 | return { | ||
| 43 | {"scm_rev", std::string(Common::g_scm_rev)}, | ||
| 44 | {"scm_branch", std::string(Common::g_scm_branch)}, | ||
| 45 | {"scm_desc", std::string(Common::g_scm_desc)}, | ||
| 46 | {"build_name", std::string(Common::g_build_name)}, | ||
| 47 | {"build_date", std::string(Common::g_build_date)}, | ||
| 48 | {"build_fullname", std::string(Common::g_build_fullname)}, | ||
| 49 | {"build_version", std::string(Common::g_build_version)}, | ||
| 50 | {"shader_cache_version", std::string(Common::g_shader_cache_version)}, | ||
| 51 | }; | ||
| 52 | } | ||
| 53 | |||
| 54 | json GetReportCommonData(u64 title_id, ResultCode 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 | return std::move(out); | ||
| 66 | } | ||
| 67 | |||
| 68 | json GetProcessorStateData(const std::string& architecture, u64 entry_point, u64 sp, u64 pc, | ||
| 69 | u64 pstate, std::array<u64, 31> registers, | ||
| 70 | 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 | |||
| 79 | auto registers_out = json::object(); | ||
| 80 | for (std::size_t i = 0; i < registers.size(); ++i) { | ||
| 81 | registers_out[fmt::format("X{:02d}", i)] = fmt::format("{:016X}", registers[i]); | ||
| 82 | } | ||
| 83 | |||
| 84 | out["registers"] = std::move(registers_out); | ||
| 85 | |||
| 86 | if (backtrace.has_value()) { | ||
| 87 | auto backtrace_out = json::array(); | ||
| 88 | for (const auto& entry : *backtrace) { | ||
| 89 | backtrace_out.push_back(fmt::format("{:016X}", entry)); | ||
| 90 | } | ||
| 91 | out["backtrace"] = std::move(backtrace_out); | ||
| 92 | } | ||
| 93 | |||
| 94 | return std::move(out); | ||
| 95 | } | ||
| 96 | |||
| 97 | json GetProcessorStateDataAuto() { | ||
| 98 | const auto* process{Core::CurrentProcess()}; | ||
| 99 | const auto& vm_manager{process->VMManager()}; | ||
| 100 | auto& arm{Core::CurrentArmInterface()}; | ||
| 101 | |||
| 102 | Core::ARM_Interface::ThreadContext context{}; | ||
| 103 | arm.SaveContext(context); | ||
| 104 | |||
| 105 | return GetProcessorStateData(process->Is64BitProcess() ? "AArch64" : "AArch32", | ||
| 106 | vm_manager.GetCodeRegionBaseAddress(), context.sp, context.pc, | ||
| 107 | context.pstate, context.cpu_registers); | ||
| 108 | } | ||
| 109 | |||
| 110 | json GetBacktraceData() { | ||
| 111 | auto out = json::array(); | ||
| 112 | const auto& backtrace{Core::CurrentArmInterface().GetBacktrace()}; | ||
| 113 | for (const auto& entry : backtrace) { | ||
| 114 | out.push_back({ | ||
| 115 | {"module", entry.module}, | ||
| 116 | {"address", fmt::format("{:016X}", entry.address)}, | ||
| 117 | {"original_address", fmt::format("{:016X}", entry.original_address)}, | ||
| 118 | {"offset", fmt::format("{:016X}", entry.offset)}, | ||
| 119 | {"symbol_name", entry.name}, | ||
| 120 | }); | ||
| 121 | } | ||
| 122 | |||
| 123 | return std::move(out); | ||
| 124 | } | ||
| 125 | |||
| 126 | json GetFullDataAuto(const std::string& timestamp, u64 title_id) { | ||
| 127 | json out; | ||
| 128 | |||
| 129 | out["yuzu_version"] = GetYuzuVersionData(); | ||
| 130 | out["report_common"] = GetReportCommonData(title_id, RESULT_SUCCESS, timestamp); | ||
| 131 | out["processor_state"] = GetProcessorStateDataAuto(); | ||
| 132 | out["backtrace"] = GetBacktraceData(); | ||
| 133 | |||
| 134 | return std::move(out); | ||
| 135 | } | ||
| 136 | |||
| 137 | template <bool read_value, typename DescriptorType> | ||
| 138 | json GetHLEBufferDescriptorData(const std::vector<DescriptorType>& buffer) { | ||
| 139 | auto buffer_out = json::array(); | ||
| 140 | for (const auto& desc : buffer) { | ||
| 141 | auto entry = json{ | ||
| 142 | {"address", fmt::format("{:016X}", desc.Address())}, | ||
| 143 | {"size", fmt::format("{:016X}", desc.Size())}, | ||
| 144 | }; | ||
| 145 | |||
| 146 | if constexpr (read_value) { | ||
| 147 | std::vector<u8> data(desc.Size()); | ||
| 148 | Memory::ReadBlock(desc.Address(), data.data(), desc.Size()); | ||
| 149 | entry["data"] = Common::HexVectorToString(data); | ||
| 150 | } | ||
| 151 | |||
| 152 | buffer_out.push_back(std::move(entry)); | ||
| 153 | } | ||
| 154 | |||
| 155 | return std::move(buffer_out); | ||
| 156 | } | ||
| 157 | |||
| 158 | json GetHLERequestContextData(Kernel::HLERequestContext& ctx) { | ||
| 159 | json out; | ||
| 160 | |||
| 161 | auto cmd_buf = json::array(); | ||
| 162 | for (std::size_t i = 0; i < IPC::COMMAND_BUFFER_LENGTH; ++i) { | ||
| 163 | cmd_buf.push_back(fmt::format("{:08X}", ctx.CommandBuffer()[i])); | ||
| 164 | } | ||
| 165 | |||
| 166 | out["command_buffer"] = std::move(cmd_buf); | ||
| 167 | |||
| 168 | out["buffer_descriptor_a"] = GetHLEBufferDescriptorData<true>(ctx.BufferDescriptorA()); | ||
| 169 | out["buffer_descriptor_b"] = GetHLEBufferDescriptorData<false>(ctx.BufferDescriptorB()); | ||
| 170 | out["buffer_descriptor_c"] = GetHLEBufferDescriptorData<false>(ctx.BufferDescriptorC()); | ||
| 171 | out["buffer_descriptor_x"] = GetHLEBufferDescriptorData<true>(ctx.BufferDescriptorX()); | ||
| 172 | |||
| 173 | return std::move(out); | ||
| 174 | } | ||
| 175 | |||
| 176 | } // Anonymous namespace | ||
| 177 | |||
| 178 | namespace Core { | ||
| 179 | |||
| 180 | Reporter::Reporter() = default; | ||
| 181 | |||
| 182 | Reporter::~Reporter() = default; | ||
| 183 | |||
| 184 | void Reporter::SaveCrashReport(u64 title_id, ResultCode result, u64 set_flags, u64 entry_point, | ||
| 185 | u64 sp, u64 pc, u64 pstate, u64 afsr0, u64 afsr1, u64 esr, u64 far, | ||
| 186 | const std::array<u64, 31>& registers, | ||
| 187 | const std::array<u64, 32>& backtrace, u32 backtrace_size, | ||
| 188 | const std::string& arch, u32 unk10) const { | ||
| 189 | if (!IsReportingEnabled()) | ||
| 190 | return; | ||
| 191 | |||
| 192 | const auto timestamp{GetTimestamp()}; | ||
| 193 | json out; | ||
| 194 | |||
| 195 | out["yuzu_version"] = GetYuzuVersionData(); | ||
| 196 | out["report_common"] = GetReportCommonData(title_id, result, timestamp); | ||
| 197 | |||
| 198 | auto proc_out = GetProcessorStateData(arch, entry_point, sp, pc, pstate, registers, backtrace); | ||
| 199 | proc_out["set_flags"] = fmt::format("{:016X}", set_flags); | ||
| 200 | proc_out["afsr0"] = fmt::format("{:016X}", afsr0); | ||
| 201 | proc_out["afsr1"] = fmt::format("{:016X}", afsr1); | ||
| 202 | proc_out["esr"] = fmt::format("{:016X}", esr); | ||
| 203 | proc_out["far"] = fmt::format("{:016X}", far); | ||
| 204 | proc_out["backtrace_size"] = fmt::format("{:08X}", backtrace_size); | ||
| 205 | proc_out["unknown_10"] = fmt::format("{:08X}", unk10); | ||
| 206 | |||
| 207 | out["processor_state"] = std::move(proc_out); | ||
| 208 | |||
| 209 | SaveToFile(std::move(out), GetPath("crash_report", title_id, timestamp)); | ||
| 210 | } | ||
| 211 | |||
| 212 | void Reporter::SaveSvcBreakReport(u32 type, bool signal_debugger, u64 info1, u64 info2, | ||
| 213 | std::optional<std::vector<u8>> resolved_buffer) const { | ||
| 214 | if (!IsReportingEnabled()) | ||
| 215 | return; | ||
| 216 | |||
| 217 | const auto timestamp{GetTimestamp()}; | ||
| 218 | const auto title_id{Core::CurrentProcess()->GetTitleID()}; | ||
| 219 | auto out = GetFullDataAuto(timestamp, title_id); | ||
| 220 | |||
| 221 | auto break_out = json{ | ||
| 222 | {"type", fmt::format("{:08X}", type)}, | ||
| 223 | {"signal_debugger", fmt::format("{}", signal_debugger)}, | ||
| 224 | {"info1", fmt::format("{:016X}", info1)}, | ||
| 225 | {"info2", fmt::format("{:016X}", info2)}, | ||
| 226 | }; | ||
| 227 | |||
| 228 | if (resolved_buffer.has_value()) { | ||
| 229 | break_out["debug_buffer"] = Common::HexVectorToString(*resolved_buffer); | ||
| 230 | } | ||
| 231 | |||
| 232 | out["svc_break"] = std::move(break_out); | ||
| 233 | |||
| 234 | SaveToFile(std::move(out), GetPath("svc_break_report", title_id, timestamp)); | ||
| 235 | } | ||
| 236 | |||
| 237 | void Reporter::SaveUnimplementedFunctionReport(Kernel::HLERequestContext& ctx, u32 command_id, | ||
| 238 | const std::string& name, | ||
| 239 | const std::string& service_name) const { | ||
| 240 | if (!IsReportingEnabled()) | ||
| 241 | return; | ||
| 242 | |||
| 243 | const auto timestamp{GetTimestamp()}; | ||
| 244 | const auto title_id{Core::CurrentProcess()->GetTitleID()}; | ||
| 245 | auto out = GetFullDataAuto(timestamp, title_id); | ||
| 246 | |||
| 247 | auto function_out = GetHLERequestContextData(ctx); | ||
| 248 | function_out["command_id"] = command_id; | ||
| 249 | function_out["function_name"] = name; | ||
| 250 | function_out["service_name"] = service_name; | ||
| 251 | |||
| 252 | out["function"] = std::move(function_out); | ||
| 253 | |||
| 254 | SaveToFile(std::move(out), GetPath("unimpl_func_report", title_id, timestamp)); | ||
| 255 | } | ||
| 256 | |||
| 257 | void Reporter::SaveUnimplementedAppletReport( | ||
| 258 | u32 applet_id, u32 common_args_version, u32 library_version, u32 theme_color, | ||
| 259 | bool startup_sound, u64 system_tick, std::vector<std::vector<u8>> normal_channel, | ||
| 260 | std::vector<std::vector<u8>> interactive_channel) const { | ||
| 261 | if (!IsReportingEnabled()) | ||
| 262 | return; | ||
| 263 | |||
| 264 | const auto timestamp{GetTimestamp()}; | ||
| 265 | const auto title_id{Core::CurrentProcess()->GetTitleID()}; | ||
| 266 | auto out = GetFullDataAuto(timestamp, title_id); | ||
| 267 | |||
| 268 | out["applet_common_args"] = { | ||
| 269 | {"applet_id", fmt::format("{:02X}", applet_id)}, | ||
| 270 | {"common_args_version", fmt::format("{:08X}", common_args_version)}, | ||
| 271 | {"library_version", fmt::format("{:08X}", library_version)}, | ||
| 272 | {"theme_color", fmt::format("{:08X}", theme_color)}, | ||
| 273 | {"startup_sound", fmt::format("{}", startup_sound)}, | ||
| 274 | {"system_tick", fmt::format("{:016X}", system_tick)}, | ||
| 275 | }; | ||
| 276 | |||
| 277 | auto normal_out = json::array(); | ||
| 278 | for (const auto& data : normal_channel) { | ||
| 279 | normal_out.push_back(Common::HexVectorToString(data)); | ||
| 280 | } | ||
| 281 | |||
| 282 | auto interactive_out = json::array(); | ||
| 283 | for (const auto& data : interactive_channel) { | ||
| 284 | interactive_out.push_back(Common::HexVectorToString(data)); | ||
| 285 | } | ||
| 286 | |||
| 287 | out["applet_normal_data"] = std::move(normal_out); | ||
| 288 | out["applet_interactive_data"] = std::move(interactive_out); | ||
| 289 | |||
| 290 | SaveToFile(std::move(out), GetPath("unimpl_applet_report", title_id, timestamp)); | ||
| 291 | } | ||
| 292 | |||
| 293 | void Reporter::SavePlayReport(u64 title_id, u64 unk1, std::vector<std::vector<u8>> data, | ||
| 294 | std::optional<u128> user_id) const { | ||
| 295 | if (!IsReportingEnabled()) | ||
| 296 | return; | ||
| 297 | |||
| 298 | const auto timestamp{GetTimestamp()}; | ||
| 299 | json out; | ||
| 300 | |||
| 301 | out["yuzu_version"] = GetYuzuVersionData(); | ||
| 302 | out["report_common"] = GetReportCommonData(title_id, RESULT_SUCCESS, timestamp, user_id); | ||
| 303 | |||
| 304 | auto data_out = json::array(); | ||
| 305 | for (const auto& d : data) { | ||
| 306 | data_out.push_back(Common::HexVectorToString(d)); | ||
| 307 | } | ||
| 308 | |||
| 309 | out["play_report_unk1"] = fmt::format("{:016X}", unk1); | ||
| 310 | out["play_report_data"] = std::move(data_out); | ||
| 311 | |||
| 312 | SaveToFile(std::move(out), GetPath("play_report", title_id, timestamp)); | ||
| 313 | } | ||
| 314 | |||
| 315 | void Reporter::SaveErrorReport(u64 title_id, ResultCode result, | ||
| 316 | std::optional<std::string> custom_text_main, | ||
| 317 | std::optional<std::string> custom_text_detail) const { | ||
| 318 | if (!IsReportingEnabled()) | ||
| 319 | return; | ||
| 320 | |||
| 321 | const auto timestamp{GetTimestamp()}; | ||
| 322 | json out; | ||
| 323 | |||
| 324 | out["yuzu_version"] = GetYuzuVersionData(); | ||
| 325 | out["report_common"] = GetReportCommonData(title_id, result, timestamp); | ||
| 326 | out["processor_state"] = GetProcessorStateDataAuto(); | ||
| 327 | out["backtrace"] = GetBacktraceData(); | ||
| 328 | |||
| 329 | out["error_custom_text"] = { | ||
| 330 | {"main", *custom_text_main}, | ||
| 331 | {"detail", *custom_text_detail}, | ||
| 332 | }; | ||
| 333 | |||
| 334 | SaveToFile(std::move(out), GetPath("error_report", title_id, timestamp)); | ||
| 335 | } | ||
| 336 | |||
| 337 | void Reporter::SaveUserReport() const { | ||
| 338 | if (!IsReportingEnabled()) | ||
| 339 | return; | ||
| 340 | |||
| 341 | const auto timestamp{GetTimestamp()}; | ||
| 342 | const auto title_id{Core::CurrentProcess()->GetTitleID()}; | ||
| 343 | |||
| 344 | SaveToFile(GetFullDataAuto(timestamp, title_id), GetPath("user_report", title_id, timestamp)); | ||
| 345 | } | ||
| 346 | |||
| 347 | bool Reporter::IsReportingEnabled() const { | ||
| 348 | return Settings::values.reporting_services; | ||
| 349 | } | ||
| 350 | |||
| 351 | } // namespace Core | ||