9#include "lldb/Host/Config.h"
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"
36#ifdef LLDB_PYTHON_DLL_RELATIVE_PATH
37#ifdef LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME
39std::string GetModulePath(HMODULE module) {
42 DWORD len = ::GetModuleFileNameW(module, buffer.data(), buffer.size());
45 if (len < buffer.size()) {
47 if (llvm::convertWideToUTF8(std::wstring(buffer.data(), len), utf8))
51 if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER)
52 buffer.resize(buffer.size() * 2);
58std::string ExeRelativeCandidate() {
59#ifdef LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME
60 std::string exe = GetModulePath(
nullptr);
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);
77#if defined(LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME) && !defined(__MINGW32__)
79const wchar_t *RegistryTagSuffix() {
80#if defined(_M_ARM64) || defined(__aarch64__)
82#elif defined(_M_X64) || defined(__x86_64__)
84#elif defined(_M_IX86) || defined(__i386__)
91struct RegistryInstall {
94 std::wstring install_path;
97void CollectRegistryInstalls(HKEY root, REGSAM view,
const wchar_t *suffix,
98 std::vector<RegistryInstall> &installs) {
100 if (::RegOpenKeyExW(root, L
"Software\\Python\\PythonCore", 0, KEY_READ | view,
101 &core) != ERROR_SUCCESS)
105 for (DWORD i = 0;; ++i) {
106 DWORD tag_len = std::size(tag);
107 LONG status = ::RegEnumKeyExW(core, i, tag, &tag_len,
nullptr,
nullptr,
109 if (status == ERROR_NO_MORE_ITEMS)
111 if (status != ERROR_SUCCESS)
115 unsigned long major = wcstoul(tag, &end, 10);
116 if (end == tag || *end != L
'.')
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)
123 std::wstring tag_key = std::wstring(tag) + L
"\\InstallPath";
125 if (::RegGetValueW(core, tag_key.c_str(),
nullptr, RRF_RT_REG_SZ,
nullptr,
126 nullptr, &size) != ERROR_SUCCESS ||
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)
133 path.resize(wcslen(path.c_str()));
135 installs.push_back({major, minor, std::move(path)});
143std::vector<std::string> RegistryInstallCandidates() {
144 std::vector<std::string> candidates;
145 const wchar_t *suffix = RegistryTagSuffix();
149 std::vector<RegistryInstall> installs;
150 CollectRegistryInstalls(HKEY_CURRENT_USER, 0, suffix, installs);
151 CollectRegistryInstalls(HKEY_LOCAL_MACHINE, KEY_WOW64_64KEY, suffix,
153 CollectRegistryInstalls(HKEY_LOCAL_MACHINE, KEY_WOW64_32KEY, suffix,
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);
161 for (
const RegistryInstall &install : installs) {
163 if (!llvm::convertWideToUTF8(install.install_path, dir))
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))
169 if (std::find(candidates.begin(), candidates.end(), path.str()) ==
171 candidates.emplace_back(path.str());
180 llvm::function_ref<
bool(
const char *)> callback) {
184#ifdef LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME
185 if (callback(LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME))
189#ifdef LLDB_PYTHON_DLL_RELATIVE_PATH
190 std::string exe_relative = ExeRelativeCandidate();
191 if (!exe_relative.empty() && callback(exe_relative.c_str()))
195#if defined(LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME) && !defined(__MINGW32__)
196 for (
const std::string &candidate : RegistryInstallCandidates())
197 if (callback(candidate.c_str()))
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...