blob: 53a24ad1893ecde11924c7b6b9889e1e060d6ae9 [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"
Zachary Turner8d48cd62017-03-22 17:33:23 +000017#include "lldb/Host/windows/PosixApi.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000018#include "llvm/ADT/SmallString.h"
Zachary Turner190fadc2016-03-22 17:58:09 +000019#include "llvm/Support/ConvertUTF.h"
Zachary Turner4e8ddf52015-04-09 18:08:50 +000020#include "llvm/Support/FileSystem.h"
Zachary Turner4e8ddf52015-04-09 18:08:50 +000021#include "llvm/Support/Path.h"
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000022#include "llvm/Support/Threading.h"
Zachary Turner190fadc2016-03-22 17:58:09 +000023#include "llvm/Support/raw_ostream.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000024
25using namespace lldb_private;
26
Zachary Turnera21fee02014-08-21 21:49:24 +000027FileSpec HostInfoWindows::m_program_filespec;
28
Kate Stoneb9c1b512016-09-06 20:57:50 +000029void HostInfoWindows::Initialize() {
30 ::CoInitializeEx(nullptr, COINIT_MULTITHREADED);
31 HostInfoBase::Initialize();
Zachary Turner74e08ca2016-03-02 22:05:52 +000032}
33
Kate Stoneb9c1b512016-09-06 20:57:50 +000034void HostInfoWindows::Terminate() {
35 HostInfoBase::Terminate();
36 ::CoUninitialize();
Zachary Turner74e08ca2016-03-02 22:05:52 +000037}
38
Kate Stoneb9c1b512016-09-06 20:57:50 +000039size_t HostInfoWindows::GetPageSize() {
40 SYSTEM_INFO systemInfo;
41 GetNativeSystemInfo(&systemInfo);
42 return systemInfo.dwPageSize;
Zachary Turner97a14e62014-08-19 17:18:29 +000043}
44
Kate Stoneb9c1b512016-09-06 20:57:50 +000045bool HostInfoWindows::GetOSVersion(uint32_t &major, uint32_t &minor,
46 uint32_t &update) {
47 OSVERSIONINFOEX info;
Zachary Turner97a14e62014-08-19 17:18:29 +000048
Kate Stoneb9c1b512016-09-06 20:57:50 +000049 ZeroMemory(&info, sizeof(OSVERSIONINFOEX));
50 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
Zachary Turner97a14e62014-08-19 17:18:29 +000051#pragma warning(push)
52#pragma warning(disable : 4996)
Kate Stoneb9c1b512016-09-06 20:57:50 +000053 // 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 }
Zachary Turner97a14e62014-08-19 17:18:29 +000061#pragma warning(pop)
62
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 major = info.dwMajorVersion;
64 minor = info.dwMinorVersion;
65 update = info.wServicePackMajor;
Zachary Turner97a14e62014-08-19 17:18:29 +000066
Kate Stoneb9c1b512016-09-06 20:57:50 +000067 return true;
Zachary Turner97a14e62014-08-19 17:18:29 +000068}
69
Kate Stoneb9c1b512016-09-06 20:57:50 +000070bool HostInfoWindows::GetOSBuildString(std::string &s) {
71 s.clear();
72 uint32_t major, minor, update;
73 if (!GetOSVersion(major, minor, update))
Zachary Turner190fadc2016-03-22 17:58:09 +000074 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +000075
76 llvm::raw_string_ostream stream(s);
77 stream << "Windows NT " << major << "." << minor << "." << update;
78 return true;
79}
80
81bool HostInfoWindows::GetOSKernelDescription(std::string &s) {
82 return GetOSBuildString(s);
83}
84
85bool 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
94FileSpec HostInfoWindows::GetProgramFileSpec() {
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000095 static llvm::once_flag g_once_flag;
96 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +000097 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
106FileSpec HostInfoWindows::GetDefaultShell() {
107 std::string shell;
108 GetEnvironmentVar("ComSpec", shell);
109 return FileSpec(shell, false);
110}
111
112bool 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
124bool 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;
Zachary Turner190fadc2016-03-22 17:58:09 +0000133}