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