1 //===-- HostInfoWindows.cpp -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Host/windows/windows.h"
14 #include <mutex> // std::once
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"
25 using namespace lldb_private;
27 FileSpec HostInfoWindows::m_program_filespec;
29 void HostInfoWindows::Initialize() {
30 ::CoInitializeEx(nullptr, COINIT_MULTITHREADED);
31 HostInfoBase::Initialize();
34 void HostInfoWindows::Terminate() {
35 HostInfoBase::Terminate();
39 size_t HostInfoWindows::GetPageSize() {
40 SYSTEM_INFO systemInfo;
41 GetNativeSystemInfo(&systemInfo);
42 return systemInfo.dwPageSize;
45 llvm::VersionTuple HostInfoWindows::GetOSVersion() {
48 ZeroMemory(&info, sizeof(OSVERSIONINFOEX));
49 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
51 #pragma warning(disable : 4996)
52 // Starting with Microsoft SDK for Windows 8.1, this function is deprecated
53 // in favor of the new Windows Version Helper APIs. Since we don't specify a
54 // minimum SDK version, it's easier to simply disable the warning rather than
55 // try to support both APIs.
56 if (GetVersionEx((LPOSVERSIONINFO)&info) == 0)
57 return llvm::VersionTuple();
60 return llvm::VersionTuple(info.dwMajorVersion, info.dwMinorVersion,
61 info.wServicePackMajor);
64 bool HostInfoWindows::GetOSBuildString(std::string &s) {
66 llvm::VersionTuple version = GetOSVersion();
70 llvm::raw_string_ostream stream(s);
71 stream << "Windows NT " << version.getAsString();
75 bool HostInfoWindows::GetOSKernelDescription(std::string &s) {
76 return GetOSBuildString(s);
79 bool HostInfoWindows::GetHostname(std::string &s) {
80 wchar_t buffer[MAX_COMPUTERNAME_LENGTH + 1];
81 DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
82 if (!::GetComputerNameW(buffer, &dwSize))
85 return llvm::convertWideToUTF8(buffer, s);
88 FileSpec HostInfoWindows::GetProgramFileSpec() {
89 static llvm::once_flag g_once_flag;
90 llvm::call_once(g_once_flag, []() {
91 std::vector<wchar_t> buffer(PATH_MAX);
92 ::GetModuleFileNameW(NULL, buffer.data(), buffer.size());
94 llvm::convertWideToUTF8(buffer.data(), path);
95 m_program_filespec.SetFile(path, false, FileSpec::Style::native);
97 return m_program_filespec;
100 FileSpec HostInfoWindows::GetDefaultShell() {
101 // Try to retrieve ComSpec from the environment. On the rare occasion
102 // that it fails, try a well-known path for ComSpec instead.
105 if (GetEnvironmentVar("ComSpec", shell))
106 return FileSpec(shell, false);
108 return FileSpec("C:\\Windows\\system32\\cmd.exe", false);
111 bool HostInfoWindows::GetEnvironmentVar(const std::string &var_name,
113 std::wstring wvar_name;
114 if (!llvm::ConvertUTF8toWide(var_name, wvar_name))
117 if (const wchar_t *wvar = _wgetenv(wvar_name.c_str()))
118 return llvm::convertWideToUTF8(wvar, var);