blob: 5b38e6021e00155f145ab51eca1b51c5e8b66bc5 [file] [log] [blame]
Zachary Turner97a14e62014-08-19 17:18:29 +00001//===-- 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
Zachary Turner74e08ca2016-03-02 22:05:52 +000012#include <objbase.h>
13
Greg Claytonff48e4b2015-02-03 02:05:44 +000014#include <mutex> // std::once
15
Zachary Turner97a14e62014-08-19 17:18:29 +000016#include "lldb/Host/windows/HostInfoWindows.h"
17#include "llvm/ADT/SmallString.h"
Zachary Turner190fadc2016-03-22 17:58:09 +000018#include "llvm/Support/ConvertUTF.h"
Zachary Turner4e8ddf52015-04-09 18:08:50 +000019#include "llvm/Support/FileSystem.h"
Zachary Turner4e8ddf52015-04-09 18:08:50 +000020#include "llvm/Support/Path.h"
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000021#include "llvm/Support/Threading.h"
Zachary Turner190fadc2016-03-22 17:58:09 +000022#include "llvm/Support/raw_ostream.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000023
24using namespace lldb_private;
25
Zachary Turnera21fee02014-08-21 21:49:24 +000026FileSpec HostInfoWindows::m_program_filespec;
27
Kate Stoneb9c1b512016-09-06 20:57:50 +000028void HostInfoWindows::Initialize() {
29 ::CoInitializeEx(nullptr, COINIT_MULTITHREADED);
30 HostInfoBase::Initialize();
Zachary Turner74e08ca2016-03-02 22:05:52 +000031}
32
Kate Stoneb9c1b512016-09-06 20:57:50 +000033void HostInfoWindows::Terminate() {
34 HostInfoBase::Terminate();
35 ::CoUninitialize();
Zachary Turner74e08ca2016-03-02 22:05:52 +000036}
37
Kate Stoneb9c1b512016-09-06 20:57:50 +000038size_t HostInfoWindows::GetPageSize() {
39 SYSTEM_INFO systemInfo;
40 GetNativeSystemInfo(&systemInfo);
41 return systemInfo.dwPageSize;
Zachary Turner97a14e62014-08-19 17:18:29 +000042}
43
Kate Stoneb9c1b512016-09-06 20:57:50 +000044bool HostInfoWindows::GetOSVersion(uint32_t &major, uint32_t &minor,
45 uint32_t &update) {
46 OSVERSIONINFOEX info;
Zachary Turner97a14e62014-08-19 17:18:29 +000047
Kate Stoneb9c1b512016-09-06 20:57:50 +000048 ZeroMemory(&info, sizeof(OSVERSIONINFOEX));
49 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
Zachary Turner97a14e62014-08-19 17:18:29 +000050#pragma warning(push)
51#pragma warning(disable : 4996)
Kate Stoneb9c1b512016-09-06 20:57:50 +000052 // Starting with Microsoft SDK for Windows 8.1, this function is deprecated in
53 // favor of the
54 // new Windows Version Helper APIs. Since we don't specify a minimum SDK
55 // version, it's easier
56 // to simply disable the warning rather than try to support both APIs.
57 if (GetVersionEx((LPOSVERSIONINFO)&info) == 0) {
58 return false;
59 }
Zachary Turner97a14e62014-08-19 17:18:29 +000060#pragma warning(pop)
61
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 major = info.dwMajorVersion;
63 minor = info.dwMinorVersion;
64 update = info.wServicePackMajor;
Zachary Turner97a14e62014-08-19 17:18:29 +000065
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 return true;
Zachary Turner97a14e62014-08-19 17:18:29 +000067}
68
Kate Stoneb9c1b512016-09-06 20:57:50 +000069bool HostInfoWindows::GetOSBuildString(std::string &s) {
70 s.clear();
71 uint32_t major, minor, update;
72 if (!GetOSVersion(major, minor, update))
Zachary Turner190fadc2016-03-22 17:58:09 +000073 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +000074
75 llvm::raw_string_ostream stream(s);
76 stream << "Windows NT " << major << "." << minor << "." << update;
77 return true;
78}
79
80bool HostInfoWindows::GetOSKernelDescription(std::string &s) {
81 return GetOSBuildString(s);
82}
83
84bool 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
93FileSpec HostInfoWindows::GetProgramFileSpec() {
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000094 static llvm::once_flag g_once_flag;
95 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 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
105FileSpec HostInfoWindows::GetDefaultShell() {
106 std::string shell;
107 GetEnvironmentVar("ComSpec", shell);
108 return FileSpec(shell, false);
109}
110
111bool 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
123bool 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;
Zachary Turner190fadc2016-03-22 17:58:09 +0000132}