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 
77 llvm::Optional<std::string> HostInfoWindows::GetOSBuildString() {
78  llvm::VersionTuple version = GetOSVersion();
79  if (version.empty())
80  return llvm::None;
81 
82  return "Windows NT " + version.getAsString();
83 }
84 
85 llvm::Optional<std::string> HostInfoWindows::GetOSKernelDescription() {
86  return GetOSBuildString();
87 }
88 
90  wchar_t buffer[MAX_COMPUTERNAME_LENGTH + 1];
91  DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
92  if (!::GetComputerNameW(buffer, &dwSize))
93  return false;
94 
95  // The conversion requires an empty string.
96  s.clear();
97  return llvm::convertWideToUTF8(buffer, s);
98 }
99 
101  static llvm::once_flag g_once_flag;
102  llvm::call_once(g_once_flag, []() {
103  std::vector<wchar_t> buffer(PATH_MAX);
104  ::GetModuleFileNameW(NULL, buffer.data(), buffer.size());
105  std::string path;
106  llvm::convertWideToUTF8(buffer.data(), path);
107  m_program_filespec.SetFile(path, FileSpec::Style::native);
108  });
109  return m_program_filespec;
110 }
111 
113  // Try to retrieve ComSpec from the environment. On the rare occasion
114  // that it fails, try a well-known path for ComSpec instead.
115 
116  std::string shell;
117  if (GetEnvironmentVar("ComSpec", shell))
118  return FileSpec(shell);
119 
120  return FileSpec("C:\\Windows\\system32\\cmd.exe");
121 }
122 
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;
132 }
133 
134 static llvm::ManagedStatic<WindowsUserIDResolver> g_user_id_resolver;
135 
137  return *g_user_id_resolver;
138 }
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:39
lldb_private::HostInfoWindows::GetHostname
static bool GetHostname(std::string &s)
Definition: HostInfoWindows.cpp:89
lldb_private::HostInfoWindows::GetPageSize
static size_t GetPageSize()
Definition: HostInfoWindows.cpp:52
lldb_private::HostInfoWindows::GetOSBuildString
static llvm::Optional< std::string > GetOSBuildString()
Definition: HostInfoWindows.cpp:77
lldb_private::HostInfoBase::Initialize
static void Initialize(SharedLibraryDirectoryHelper *helper=nullptr)
Definition: HostInfoBase.cpp:75
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::GetOSKernelDescription
static llvm::Optional< std::string > GetOSKernelDescription()
Definition: HostInfoWindows.cpp:85
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:100
lldb_private::HostInfoWindows::GetUserIDResolver
static UserIDResolver & GetUserIDResolver()
Definition: HostInfoWindows.cpp:136
HostInfoWindows.h
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
UserIDResolver.h
lldb_private::UserIDResolver
An abstract interface for things that know how to map numeric user/group IDs into names.
Definition: UserIDResolver.h:23
PosixApi.h
lldb_private::HostInfoWindows::GetEnvironmentVar
static bool GetEnvironmentVar(const std::string &var_name, std::string &var)
Definition: HostInfoWindows.cpp:123
windows.h
g_user_id_resolver
static llvm::ManagedStatic< WindowsUserIDResolver > g_user_id_resolver
Definition: HostInfoWindows.cpp:134
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:172
PATH_MAX
#define PATH_MAX
Definition: windows/PosixApi.h:25
lldb_private::HostInfoWindows::GetDefaultShell
static FileSpec GetDefaultShell()
Definition: HostInfoWindows.cpp:112
lldb_private::HostInfoBase::Terminate
static void Terminate()
Definition: HostInfoBase.cpp:80