From be6d9af1abef5dd2d9a4bd38c56facf0440358de Mon Sep 17 00:00:00 2001 From: msuih Date: Fri, 19 Apr 2019 15:05:47 +0300 Subject: [PATCH] Print OS info to log Credit to @maximilian578 for help --- Utilities/Log.cpp | 11 ++++++++++ Utilities/sysinfo.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++ Utilities/sysinfo.h | 2 ++ 3 files changed, 61 insertions(+) diff --git a/Utilities/Log.cpp b/Utilities/Log.cpp index 1d60e1b3fb..103e01e4be 100644 --- a/Utilities/Log.cpp +++ b/Utilities/Log.cpp @@ -602,6 +602,17 @@ logs::file_listener::file_listener(const std::string& name) file_writer::log(logs::level::always, ver.text.data(), ver.text.size()); file_writer::log(logs::level::always, "\n", 1); messages.emplace_back(std::move(ver)); + + // Write OS version + stored_message os; + os.m.ch = nullptr; + os.m.sev = level::notice; + os.stamp = 0; + os.text = utils::get_OS_version(); + + file_writer::log(logs::level::notice, os.text.data(), os.text.size()); + file_writer::log(logs::level::notice, "\n", 1); + messages.emplace_back(std::move(os)); } void logs::file_listener::log(u64 stamp, const logs::message& msg, const std::string& prefix, const std::string& _text) diff --git a/Utilities/sysinfo.cpp b/Utilities/sysinfo.cpp index 96226362dd..59af3afd9e 100644 --- a/Utilities/sysinfo.cpp +++ b/Utilities/sysinfo.cpp @@ -5,8 +5,13 @@ #ifdef _WIN32 #include "windows.h" +#include "sysinfoapi.h" +#include "subauth.h" +#include "stringapiset.h" #else #include +#include +#include #endif bool utils::has_ssse3() @@ -153,3 +158,46 @@ std::string utils::get_firmware_version() } return ""; } + +std::string utils::get_OS_version() +{ + std::string output = ""; +#ifdef _WIN32 + // GetVersionEx is deprecated, RtlGetVersion is kernel-mode only and AnalyticsInfo is UWP only. + // So we're forced to read PEB instead to get Windows version info. It's ugly but works. + + const DWORD peb_offset = 0x60; + const INT_PTR peb = __readgsqword(peb_offset); + + const DWORD version_major = *reinterpret_cast(peb + 0x118); + const DWORD version_minor = *reinterpret_cast(peb + 0x11c); + const WORD build = *reinterpret_cast(peb + 0x120); + const UNICODE_STRING service_pack = *reinterpret_cast(peb + 0x02E8); + const u64 compatibility_mode = *reinterpret_cast(peb + 0x02C8); // Two DWORDs, major & minor version + + const bool has_sp = service_pack.Length > 0; + std::vector holder(service_pack.Length + 1, '\0'); + if (has_sp) + { + WideCharToMultiByte(CP_UTF8, NULL, service_pack.Buffer, service_pack.Length, + (LPSTR) holder.data(), static_cast(holder.size()), NULL, NULL); + } + + fmt::append(output, + "Operating system: Windows, Major: %lu, Minor: %lu, Build: %u, Service Pack: %s, Compatibility mode: %llu", + version_major, version_minor, build, has_sp ? holder.data() : "none", compatibility_mode); +#else + struct utsname details = {}; + + if (!uname(&details)) + { + fmt::append(output, "Operating system: POSIX, Name: %s, Release: %s, Version: %s", + details.sysname, details.release, details.version); + } + else + { + fmt::append(output, "Operating system: POSIX, Unknown version! (Error: %d)", errno); + } +#endif + return output; +} diff --git a/Utilities/sysinfo.h b/Utilities/sysinfo.h index 5f2bc1f3ed..ba07c22a9f 100644 --- a/Utilities/sysinfo.h +++ b/Utilities/sysinfo.h @@ -46,4 +46,6 @@ namespace utils std::string get_system_info(); std::string get_firmware_version(); + + std::string get_OS_version(); }