LLDB mainline
PythonRuntimeLoaderWindows.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
9#include "lldb/Host/Config.h"
10
11#if LLDB_ENABLE_PYTHON
12
15#include "llvm/ADT/STLFunctionalExtras.h"
16#include "llvm/ADT/SmallString.h"
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/Support/ConvertUTF.h"
20#include "llvm/Support/FileSystem.h"
21#include "llvm/Support/Path.h"
22#include "llvm/Support/Windows/WindowsSupport.h"
23
24#include <algorithm>
25#include <cwchar>
26#include <iterator>
27#include <pathcch.h>
28#include <string>
29#include <tuple>
30#include <vector>
31
32namespace lldb_private {
33
34namespace {
35
36#ifdef LLDB_PYTHON_DLL_RELATIVE_PATH
37#ifdef LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME
38/// Absolute path of \p module, or the running executable when null.
39std::string GetModulePath(HMODULE module) {
40 std::vector<WCHAR> buffer(MAX_PATH);
41 while (buffer.size() <= PATHCCH_MAX_CCH) {
42 DWORD len = ::GetModuleFileNameW(module, buffer.data(), buffer.size());
43 if (len == 0)
44 return "";
45 if (len < buffer.size()) {
46 std::string utf8;
47 if (llvm::convertWideToUTF8(std::wstring(buffer.data(), len), utf8))
48 return utf8;
49 return "";
50 }
51 if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER)
52 buffer.resize(buffer.size() * 2);
53 }
54 return "";
55}
56#endif // ifdef LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME
57
58std::string ExeRelativeCandidate() {
59#ifdef LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME
60 std::string exe = GetModulePath(nullptr);
61 if (exe.empty())
62 return "";
63 llvm::SmallString<MAX_PATH> path(exe);
64 llvm::sys::path::remove_filename(path);
65 llvm::sys::path::append(path, LLDB_PYTHON_DLL_RELATIVE_PATH);
66 llvm::sys::path::append(path, LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME);
67 llvm::sys::fs::make_absolute(path);
68 return std::string(path);
69#else
70 return "";
71#endif // ifdef LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME
72}
73#endif // ifdef LLDB_PYTHON_DLL_RELATIVE_PATH
74
75// MinGW builds link against a MinGW Python, so a PEP 514 registered install
76// (built with MSVC) would not be compatible.
77#if defined(LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME) && !defined(__MINGW32__)
78/// PEP 514 tag suffix for installs matching this process' architecture.
79const wchar_t *RegistryTagSuffix() {
80#if defined(_M_ARM64) || defined(__aarch64__)
81 return L"-arm64";
82#elif defined(_M_X64) || defined(__x86_64__)
83 return L"";
84#elif defined(_M_IX86) || defined(__i386__)
85 return L"-32";
86#else
87 return nullptr;
88#endif
89}
90
91struct RegistryInstall {
92 unsigned long major;
93 unsigned long minor;
94 std::wstring install_path;
95};
96
97void CollectRegistryInstalls(HKEY root, REGSAM view, const wchar_t *suffix,
98 std::vector<RegistryInstall> &installs) {
99 HKEY core;
100 if (::RegOpenKeyExW(root, L"Software\\Python\\PythonCore", 0, KEY_READ | view,
101 &core) != ERROR_SUCCESS)
102 return;
103
104 wchar_t tag[256];
105 for (DWORD i = 0;; ++i) {
106 DWORD tag_len = std::size(tag);
107 LONG status = ::RegEnumKeyExW(core, i, tag, &tag_len, nullptr, nullptr,
108 nullptr, nullptr);
109 if (status == ERROR_NO_MORE_ITEMS)
110 break;
111 if (status != ERROR_SUCCESS)
112 continue;
113
114 wchar_t *end;
115 unsigned long major = wcstoul(tag, &end, 10);
116 if (end == tag || *end != L'.')
117 continue;
118 wchar_t *minor_start = end + 1;
119 unsigned long minor = wcstoul(minor_start, &end, 10);
120 if (end == minor_start || wcscmp(end, suffix) != 0)
121 continue;
122
123 std::wstring tag_key = std::wstring(tag) + L"\\InstallPath";
124 DWORD size = 0;
125 if (::RegGetValueW(core, tag_key.c_str(), nullptr, RRF_RT_REG_SZ, nullptr,
126 nullptr, &size) != ERROR_SUCCESS ||
127 size == 0)
128 continue;
129 std::wstring path(size / sizeof(wchar_t), L'\0');
130 if (::RegGetValueW(core, tag_key.c_str(), nullptr, RRF_RT_REG_SZ, nullptr,
131 path.data(), &size) != ERROR_SUCCESS)
132 continue;
133 path.resize(wcslen(path.c_str()));
134 if (!path.empty())
135 installs.push_back({major, minor, std::move(path)});
136 }
137 ::RegCloseKey(core);
138}
139
140/// Runtime DLLs of the Python installs registered per PEP 514, which is
141/// where the py launcher finds interpreters that are not on PATH. Newest
142/// version first.
143std::vector<std::string> RegistryInstallCandidates() {
144 std::vector<std::string> candidates;
145 const wchar_t *suffix = RegistryTagSuffix();
146 if (!suffix)
147 return candidates;
148
149 std::vector<RegistryInstall> installs;
150 CollectRegistryInstalls(HKEY_CURRENT_USER, 0, suffix, installs);
151 CollectRegistryInstalls(HKEY_LOCAL_MACHINE, KEY_WOW64_64KEY, suffix,
152 installs);
153 CollectRegistryInstalls(HKEY_LOCAL_MACHINE, KEY_WOW64_32KEY, suffix,
154 installs);
155 std::stable_sort(installs.begin(), installs.end(),
156 [](const RegistryInstall &a, const RegistryInstall &b) {
157 return std::tie(a.major, a.minor) >
158 std::tie(b.major, b.minor);
159 });
160
161 for (const RegistryInstall &install : installs) {
162 std::string dir;
163 if (!llvm::convertWideToUTF8(install.install_path, dir))
164 continue;
165 llvm::SmallString<MAX_PATH> path(dir);
166 llvm::sys::path::append(path, LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME);
167 if (!llvm::sys::fs::exists(path))
168 continue;
169 if (std::find(candidates.begin(), candidates.end(), path.str()) ==
170 candidates.end())
171 candidates.emplace_back(path.str());
172 }
173 return candidates;
174}
175#endif // defined(LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME) && !defined(__MINGW32__)
176
177} // namespace
178
180 llvm::function_ref<bool(const char *)> callback) {
181 // Mapping the DLL by base name first lets the plugin's delay-load thunks
182 // (which LoadLibrary by base name) resolve against the already-mapped
183 // module on first use, bypassing DLL search rules.
184#ifdef LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME
185 if (callback(LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME))
186 return;
187#endif
188
189#ifdef LLDB_PYTHON_DLL_RELATIVE_PATH
190 std::string exe_relative = ExeRelativeCandidate();
191 if (!exe_relative.empty() && callback(exe_relative.c_str()))
192 return;
193#endif
194
195#if defined(LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME) && !defined(__MINGW32__)
196 for (const std::string &candidate : RegistryInstallCandidates())
197 if (callback(candidate.c_str()))
198 return;
199#endif
200}
201
202} // namespace lldb_private
203
204#endif // LLDB_ENABLE_PYTHON
#define PATHCCH_MAX_CCH
#define MAX_PATH
A class that represents a running process on the host machine.
void ForEachPythonRuntimeCandidate(llvm::function_ref< bool(const char *)> callback)
Visits candidate Python runtime paths in priority order, stopping at the first call that returns true...