LLDB  mainline
HostInfoWindows.cpp
Go to the documentation of this file.
1 //===-- HostInfoWindows.cpp -----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 
11 #include <objbase.h>
12 
13 #include <mutex>
14 
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/Support/ConvertUTF.h"
20 #include "llvm/Support/FileSystem.h"
21 #include "llvm/Support/ManagedStatic.h"
22 #include "llvm/Support/Path.h"
23 #include "llvm/Support/Threading.h"
24 #include "llvm/Support/raw_ostream.h"
25 
26 using namespace lldb_private;
27 
28 namespace {
29 class WindowsUserIDResolver : public UserIDResolver {
30 protected:
31  llvm::Optional<std::string> DoGetUserName(id_t uid) override {
32  return llvm::None;
33  }
34  llvm::Optional<std::string> DoGetGroupName(id_t gid) override {
35  return llvm::None;
36  }
37 };
38 } // namespace
39 
41 
43  ::CoInitializeEx(nullptr, COINIT_MULTITHREADED);
45 }
46 
49  ::CoUninitialize();
50 }
51 
53  SYSTEM_INFO systemInfo;
54  GetNativeSystemInfo(&systemInfo);
55  return systemInfo.dwPageSize;
56 }
57 
58 llvm::VersionTuple HostInfoWindows::GetOSVersion() {
59  OSVERSIONINFOEX info;
60 
61  ZeroMemory(&info, sizeof(OSVERSIONINFOEX));
62  info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
63 #pragma warning(push)
64 #pragma warning(disable : 4996)
65  // Starting with Microsoft SDK for Windows 8.1, this function is deprecated
66  // in favor of the new Windows Version Helper APIs. Since we don't specify a
67  // minimum SDK version, it's easier to simply disable the warning rather than
68  // try to support both APIs.
69  if (GetVersionEx((LPOSVERSIONINFO)&info) == 0)
70  return llvm::VersionTuple();
71 #pragma warning(pop)
72 
73  return llvm::VersionTuple(info.dwMajorVersion, info.dwMinorVersion,
74  info.wServicePackMajor);
75 }
76 
78  s.clear();
79  llvm::VersionTuple version = GetOSVersion();
80  if (version.empty())
81  return false;
82 
83  llvm::raw_string_ostream stream(s);
84  stream << "Windows NT " << version.getAsString();
85  return true;
86 }
87 
89  return GetOSBuildString(s);
90 }
91 
93  wchar_t buffer[MAX_COMPUTERNAME_LENGTH + 1];
94  DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
95  if (!::GetComputerNameW(buffer, &dwSize))
96  return false;
97 
98  // The conversion requires an empty string.
99  s.clear();
100  return llvm::convertWideToUTF8(buffer, s);
101 }
102 
104  static llvm::once_flag g_once_flag;
105  llvm::call_once(g_once_flag, []() {
106  std::vector<wchar_t> buffer(PATH_MAX);
107  ::GetModuleFileNameW(NULL, buffer.data(), buffer.size());
108  std::string path;
109  llvm::convertWideToUTF8(buffer.data(), path);
110  m_program_filespec.SetFile(path, FileSpec::Style::native);
111  });
112  return m_program_filespec;
113 }
114 
116  // Try to retrieve ComSpec from the environment. On the rare occasion
117  // that it fails, try a well-known path for ComSpec instead.
118 
119  std::string shell;
120  if (GetEnvironmentVar("ComSpec", shell))
121  return FileSpec(shell);
122 
123  return FileSpec("C:\\Windows\\system32\\cmd.exe");
124 }
125 
127  std::string &var) {
128  std::wstring wvar_name;
129  if (!llvm::ConvertUTF8toWide(var_name, wvar_name))
130  return false;
131 
132  if (const wchar_t *wvar = _wgetenv(wvar_name.c_str()))
133  return llvm::convertWideToUTF8(wvar, var);
134  return false;
135 }
136 
137 static llvm::ManagedStatic<WindowsUserIDResolver> g_user_id_resolver;
138 
140  return *g_user_id_resolver;
141 }
lldb_private::HostInfoWindows::Terminate
static void Terminate()
Definition: HostInfoWindows.cpp:47
lldb_private::HostInfoWindows::m_program_filespec
static FileSpec m_program_filespec
Definition: HostInfoWindows.h:44
lldb_private::HostInfoWindows::GetHostname
static bool GetHostname(std::string &s)
Definition: HostInfoWindows.cpp:92
lldb_private::HostInfoWindows::GetOSBuildString
static bool GetOSBuildString(std::string &s)
Definition: HostInfoWindows.cpp:77
lldb_private::HostInfoWindows::GetPageSize
static size_t GetPageSize()
Definition: HostInfoWindows.cpp:52
lldb_private::HostInfoBase::Initialize
static void Initialize(SharedLibraryDirectoryHelper *helper=nullptr)
Definition: HostInfoBase.cpp:79
lldb_private::HostInfoBase::SharedLibraryDirectoryHelper
void(FileSpec &this_file) SharedLibraryDirectoryHelper
A helper function for determining the liblldb location.
Definition: HostInfoBase.h:44
lldb_private::HostInfoWindows::GetOSVersion
static llvm::VersionTuple GetOSVersion()
Definition: HostInfoWindows.cpp:58
lldb_private::HostInfoWindows::Initialize
static void Initialize(SharedLibraryDirectoryHelper *helper=nullptr)
Definition: HostInfoWindows.cpp:42
lldb_private::FileSpec
Definition: FileSpec.h:56
lldb_private::HostInfoWindows::GetProgramFileSpec
static FileSpec GetProgramFileSpec()
Definition: HostInfoWindows.cpp:103
lldb_private::HostInfoWindows::GetUserIDResolver
static UserIDResolver & GetUserIDResolver()
Definition: HostInfoWindows.cpp:139
HostInfoWindows.h
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:38
UserIDResolver.h
lldb_private::UserIDResolver
An abstract interface for things that know how to map numeric user/group IDs into names.
Definition: UserIDResolver.h:22
PosixApi.h
lldb_private::HostInfoWindows::GetEnvironmentVar
static bool GetEnvironmentVar(const std::string &var_name, std::string &var)
Definition: HostInfoWindows.cpp:126
windows.h
g_user_id_resolver
static llvm::ManagedStatic< WindowsUserIDResolver > g_user_id_resolver
Definition: HostInfoWindows.cpp:137
lldb_private::HostInfoWindows::GetOSKernelDescription
static bool GetOSKernelDescription(std::string &s)
Definition: HostInfoWindows.cpp:88
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::FileSpec::SetFile
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
Definition: FileSpec.cpp:174
PATH_MAX
#define PATH_MAX
Definition: windows/PosixApi.h:25
lldb_private::HostInfoWindows::GetDefaultShell
static FileSpec GetDefaultShell()
Definition: HostInfoWindows.cpp:115
lldb_private::HostInfoBase::Terminate
static void Terminate()
Definition: HostInfoBase.cpp:84