1 //===-- HostInfoWindows.cpp -----------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lldb/Host/windows/windows.h" 10 11 #include <objbase.h> 12 13 #include <mutex> 14 15 #include "lldb/Host/windows/HostInfoWindows.h" 16 #include "lldb/Host/windows/PosixApi.h" 17 #include "lldb/Utility/UserIDResolver.h" 18 #include "llvm/ADT/SmallString.h" 19 #include "llvm/Support/ConvertUTF.h" 20 #include "llvm/Support/FileSystem.h" 21 #include "llvm/Support/ManagedStatic.h" 22 #include "llvm/Support/Path.h" 23 #include "llvm/Support/Threading.h" 24 #include "llvm/Support/raw_ostream.h" 25 26 using namespace lldb_private; 27 28 namespace { 29 class WindowsUserIDResolver : public UserIDResolver { 30 protected: 31 llvm::Optional<std::string> DoGetUserName(id_t uid) override { 32 return llvm::None; 33 } 34 llvm::Optional<std::string> DoGetGroupName(id_t gid) override { 35 return llvm::None; 36 } 37 }; 38 } // namespace 39 40 FileSpec HostInfoWindows::m_program_filespec; 41 42 void HostInfoWindows::Initialize(SharedLibraryDirectoryHelper *helper) { 43 ::CoInitializeEx(nullptr, COINIT_MULTITHREADED); 44 HostInfoBase::Initialize(helper); 45 } 46 47 void HostInfoWindows::Terminate() { 48 HostInfoBase::Terminate(); 49 ::CoUninitialize(); 50 } 51 52 size_t HostInfoWindows::GetPageSize() { 53 SYSTEM_INFO systemInfo; 54 GetNativeSystemInfo(&systemInfo); 55 return systemInfo.dwPageSize; 56 } 57 58 llvm::VersionTuple HostInfoWindows::GetOSVersion() { 59 OSVERSIONINFOEX info; 60 61 ZeroMemory(&info, sizeof(OSVERSIONINFOEX)); 62 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); 63 #pragma warning(push) 64 #pragma warning(disable : 4996) 65 // Starting with Microsoft SDK for Windows 8.1, this function is deprecated 66 // in favor of the new Windows Version Helper APIs. Since we don't specify a 67 // minimum SDK version, it's easier to simply disable the warning rather than 68 // try to support both APIs. 69 if (GetVersionEx((LPOSVERSIONINFO)&info) == 0) 70 return llvm::VersionTuple(); 71 #pragma warning(pop) 72 73 return llvm::VersionTuple(info.dwMajorVersion, info.dwMinorVersion, 74 info.wServicePackMajor); 75 } 76 77 llvm::Optional<std::string> HostInfoWindows::GetOSBuildString() { 78 llvm::VersionTuple version = GetOSVersion(); 79 if (version.empty()) 80 return llvm::None; 81 82 return "Windows NT " + version.getAsString(); 83 } 84 85 llvm::Optional<std::string> HostInfoWindows::GetOSKernelDescription() { 86 return GetOSBuildString(); 87 } 88 89 bool HostInfoWindows::GetHostname(std::string &s) { 90 wchar_t buffer[MAX_COMPUTERNAME_LENGTH + 1]; 91 DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1; 92 if (!::GetComputerNameW(buffer, &dwSize)) 93 return false; 94 95 // The conversion requires an empty string. 96 s.clear(); 97 return llvm::convertWideToUTF8(buffer, s); 98 } 99 100 FileSpec HostInfoWindows::GetProgramFileSpec() { 101 static llvm::once_flag g_once_flag; 102 llvm::call_once(g_once_flag, []() { 103 std::vector<wchar_t> buffer(PATH_MAX); 104 ::GetModuleFileNameW(NULL, buffer.data(), buffer.size()); 105 std::string path; 106 llvm::convertWideToUTF8(buffer.data(), path); 107 m_program_filespec.SetFile(path, FileSpec::Style::native); 108 }); 109 return m_program_filespec; 110 } 111 112 FileSpec HostInfoWindows::GetDefaultShell() { 113 // Try to retrieve ComSpec from the environment. On the rare occasion 114 // that it fails, try a well-known path for ComSpec instead. 115 116 std::string shell; 117 if (GetEnvironmentVar("ComSpec", shell)) 118 return FileSpec(shell); 119 120 return FileSpec("C:\\Windows\\system32\\cmd.exe"); 121 } 122 123 bool HostInfoWindows::GetEnvironmentVar(const std::string &var_name, 124 std::string &var) { 125 std::wstring wvar_name; 126 if (!llvm::ConvertUTF8toWide(var_name, wvar_name)) 127 return false; 128 129 if (const wchar_t *wvar = _wgetenv(wvar_name.c_str())) 130 return llvm::convertWideToUTF8(wvar, var); 131 return false; 132 } 133 134 static llvm::ManagedStatic<WindowsUserIDResolver> g_user_id_resolver; 135 136 UserIDResolver &HostInfoWindows::GetUserIDResolver() { 137 return *g_user_id_resolver; 138 } 139