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 "lldb/Host/windows/HostInfoWindows.h"
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/Support/raw_ostream.h"
15 
16 using namespace lldb_private;
17 
18 FileSpec HostInfoWindows::m_program_filespec;
19 
20 size_t
21 HostInfoWindows::GetPageSize()
22 {
23     SYSTEM_INFO systemInfo;
24     GetNativeSystemInfo(&systemInfo);
25     return systemInfo.dwPageSize;
26 }
27 
28 bool
29 HostInfoWindows::GetOSVersion(uint32_t &major, uint32_t &minor, uint32_t &update)
30 {
31     OSVERSIONINFOEX info;
32 
33     ZeroMemory(&info, sizeof(OSVERSIONINFOEX));
34     info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
35 #pragma warning(push)
36 #pragma warning(disable : 4996)
37     // Starting with Microsoft SDK for Windows 8.1, this function is deprecated in favor of the
38     // new Windows Version Helper APIs.  Since we don't specify a minimum SDK version, it's easier
39     // to simply disable the warning rather than try to support both APIs.
40     if (GetVersionEx((LPOSVERSIONINFO)&info) == 0)
41     {
42         return false;
43     }
44 #pragma warning(pop)
45 
46     major = info.dwMajorVersion;
47     minor = info.dwMinorVersion;
48     update = info.wServicePackMajor;
49 
50     return true;
51 }
52 
53 bool
54 HostInfoWindows::GetOSBuildString(std::string &s)
55 {
56     s.clear();
57     uint32_t major, minor, update;
58     if (!GetOSVersion(major, minor, update))
59         return false;
60 
61     llvm::raw_string_ostream stream(s);
62     stream << "Windows NT " << major << "." << minor << "." << update;
63     return true;
64 }
65 
66 bool
67 HostInfoWindows::GetOSKernelDescription(std::string &s)
68 {
69     return GetOSBuildString(s);
70 }
71 
72 bool
73 HostInfoWindows::GetHostname(std::string &s)
74 {
75     char buffer[MAX_COMPUTERNAME_LENGTH + 1];
76     DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
77     if (!::GetComputerName(buffer, &dwSize))
78         return false;
79 
80     s.assign(buffer, buffer + dwSize);
81     return true;
82 }
83 
84 FileSpec
85 HostInfoWindows::GetProgramFileSpec()
86 {
87     static bool is_initialized = false;
88     if (!is_initialized)
89     {
90         is_initialized = true;
91 
92         std::vector<char> buffer(PATH_MAX);
93         ::GetModuleFileName(NULL, &buffer[0], buffer.size());
94         m_program_filespec.SetFile(&buffer[0], false);
95     }
96     return m_program_filespec;
97 }
98 
99 FileSpec
100 HostInfoWindows::GetDefaultShell()
101 {
102     return FileSpec(::getenv("ComSpec"), false);
103 }
104 
105 bool
106 HostInfoWindows::ComputePythonDirectory(FileSpec &file_spec)
107 {
108     FileSpec lldb_file_spec;
109     if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec))
110         return false;
111 
112     char raw_path[PATH_MAX];
113     lldb_file_spec.AppendPathComponent("../lib/site-packages");
114     lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
115 
116     file_spec.GetDirectory().SetCString(raw_path);
117     return true;
118 }
119