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 "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 bool HostInfoWindows::GetOSVersion(uint32_t &major, uint32_t &minor, 46 uint32_t &update) { 47 OSVERSIONINFOEX info; 48 49 ZeroMemory(&info, sizeof(OSVERSIONINFOEX)); 50 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); 51 #pragma warning(push) 52 #pragma warning(disable : 4996) 53 // Starting with Microsoft SDK for Windows 8.1, this function is deprecated 54 // in favor of the new Windows Version Helper APIs. Since we don't specify a 55 // minimum SDK version, it's easier to simply disable the warning rather than 56 // try to support both APIs. 57 if (GetVersionEx((LPOSVERSIONINFO)&info) == 0) { 58 return false; 59 } 60 #pragma warning(pop) 61 62 major = info.dwMajorVersion; 63 minor = info.dwMinorVersion; 64 update = info.wServicePackMajor; 65 66 return true; 67 } 68 69 bool HostInfoWindows::GetOSBuildString(std::string &s) { 70 s.clear(); 71 uint32_t major, minor, update; 72 if (!GetOSVersion(major, minor, update)) 73 return false; 74 75 llvm::raw_string_ostream stream(s); 76 stream << "Windows NT " << major << "." << minor << "." << update; 77 return true; 78 } 79 80 bool HostInfoWindows::GetOSKernelDescription(std::string &s) { 81 return GetOSBuildString(s); 82 } 83 84 bool HostInfoWindows::GetHostname(std::string &s) { 85 wchar_t buffer[MAX_COMPUTERNAME_LENGTH + 1]; 86 DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1; 87 if (!::GetComputerNameW(buffer, &dwSize)) 88 return false; 89 90 return llvm::convertWideToUTF8(buffer, s); 91 } 92 93 FileSpec HostInfoWindows::GetProgramFileSpec() { 94 static llvm::once_flag g_once_flag; 95 llvm::call_once(g_once_flag, []() { 96 std::vector<wchar_t> buffer(PATH_MAX); 97 ::GetModuleFileNameW(NULL, buffer.data(), buffer.size()); 98 std::string path; 99 llvm::convertWideToUTF8(buffer.data(), path); 100 m_program_filespec.SetFile(path, false); 101 }); 102 return m_program_filespec; 103 } 104 105 FileSpec HostInfoWindows::GetDefaultShell() { 106 std::string shell; 107 GetEnvironmentVar("ComSpec", shell); 108 return FileSpec(shell, false); 109 } 110 111 bool HostInfoWindows::ComputePythonDirectory(FileSpec &file_spec) { 112 FileSpec lldb_file_spec; 113 if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec)) 114 return false; 115 llvm::SmallString<64> path(lldb_file_spec.GetDirectory().AsCString()); 116 llvm::sys::path::remove_filename(path); 117 llvm::sys::path::append(path, "lib", "site-packages"); 118 std::replace(path.begin(), path.end(), '\\', '/'); 119 file_spec.GetDirectory().SetString(path.c_str()); 120 return true; 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