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