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> // std::once 15 16 #include "lldb/Host/windows/HostInfoWindows.h" 17 #include "llvm/ADT/SmallString.h" 18 #include "llvm/Support/ConvertUTF.h" 19 #include "llvm/Support/FileSystem.h" 20 #include "llvm/Support/Path.h" 21 #include "llvm/Support/raw_ostream.h" 22 23 using namespace lldb_private; 24 25 FileSpec HostInfoWindows::m_program_filespec; 26 27 void HostInfoWindows::Initialize() { 28 ::CoInitializeEx(nullptr, COINIT_MULTITHREADED); 29 HostInfoBase::Initialize(); 30 } 31 32 void HostInfoWindows::Terminate() { 33 HostInfoBase::Terminate(); 34 ::CoUninitialize(); 35 } 36 37 size_t HostInfoWindows::GetPageSize() { 38 SYSTEM_INFO systemInfo; 39 GetNativeSystemInfo(&systemInfo); 40 return systemInfo.dwPageSize; 41 } 42 43 bool HostInfoWindows::GetOSVersion(uint32_t &major, uint32_t &minor, 44 uint32_t &update) { 45 OSVERSIONINFOEX info; 46 47 ZeroMemory(&info, sizeof(OSVERSIONINFOEX)); 48 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); 49 #pragma warning(push) 50 #pragma warning(disable : 4996) 51 // Starting with Microsoft SDK for Windows 8.1, this function is deprecated in 52 // favor of the 53 // new Windows Version Helper APIs. Since we don't specify a minimum SDK 54 // version, it's easier 55 // to simply disable the warning rather than try to support both APIs. 56 if (GetVersionEx((LPOSVERSIONINFO)&info) == 0) { 57 return false; 58 } 59 #pragma warning(pop) 60 61 major = info.dwMajorVersion; 62 minor = info.dwMinorVersion; 63 update = info.wServicePackMajor; 64 65 return true; 66 } 67 68 bool HostInfoWindows::GetOSBuildString(std::string &s) { 69 s.clear(); 70 uint32_t major, minor, update; 71 if (!GetOSVersion(major, minor, update)) 72 return false; 73 74 llvm::raw_string_ostream stream(s); 75 stream << "Windows NT " << major << "." << minor << "." << update; 76 return true; 77 } 78 79 bool HostInfoWindows::GetOSKernelDescription(std::string &s) { 80 return GetOSBuildString(s); 81 } 82 83 bool HostInfoWindows::GetHostname(std::string &s) { 84 wchar_t buffer[MAX_COMPUTERNAME_LENGTH + 1]; 85 DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1; 86 if (!::GetComputerNameW(buffer, &dwSize)) 87 return false; 88 89 return llvm::convertWideToUTF8(buffer, s); 90 } 91 92 FileSpec HostInfoWindows::GetProgramFileSpec() { 93 static std::once_flag g_once_flag; 94 std::call_once(g_once_flag, []() { 95 std::vector<wchar_t> buffer(PATH_MAX); 96 ::GetModuleFileNameW(NULL, buffer.data(), buffer.size()); 97 std::string path; 98 llvm::convertWideToUTF8(buffer.data(), path); 99 m_program_filespec.SetFile(path, false); 100 }); 101 return m_program_filespec; 102 } 103 104 FileSpec HostInfoWindows::GetDefaultShell() { 105 std::string shell; 106 GetEnvironmentVar("ComSpec", shell); 107 return FileSpec(shell, false); 108 } 109 110 bool HostInfoWindows::ComputePythonDirectory(FileSpec &file_spec) { 111 FileSpec lldb_file_spec; 112 if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec)) 113 return false; 114 llvm::SmallString<64> path(lldb_file_spec.GetDirectory().AsCString()); 115 llvm::sys::path::remove_filename(path); 116 llvm::sys::path::append(path, "lib", "site-packages"); 117 std::replace(path.begin(), path.end(), '\\', '/'); 118 file_spec.GetDirectory().SetString(path.c_str()); 119 return true; 120 } 121 122 bool HostInfoWindows::GetEnvironmentVar(const std::string &var_name, 123 std::string &var) { 124 std::wstring wvar_name; 125 if (!llvm::ConvertUTF8toWide(var_name, wvar_name)) 126 return false; 127 128 if (const wchar_t *wvar = _wgetenv(wvar_name.c_str())) 129 return llvm::convertWideToUTF8(wvar, var); 130 return false; 131 } 132