Author: Charles Zablit
Date: 2026-02-11T18:48:31+01:00
New Revision: 8d34f2851fbea06b36a1e8ea25c44c14354a7b66

URL: 
https://github.com/llvm/llvm-project/commit/8d34f2851fbea06b36a1e8ea25c44c14354a7b66
DIFF: 
https://github.com/llvm/llvm-project/commit/8d34f2851fbea06b36a1e8ea25c44c14354a7b66.diff

LOG: [lldb-dap][windows] add --check-python command (#180784)

Implement [`[RFC] Add a Python check command to lldb-dap and
lldb`](https://discourse.llvm.org/t/rfc-add-a-python-check-command-to-lldb-dap-and-lldb/88972).

This patch adds the `--check-python` flag to `lldb-dap` on Windows, to
allow dap clients to verify the correct Python version is available
before trying to start `lldb-dap`.

Depends on:
- https://github.com/llvm/llvm-project/pull/180786

rdar://165442474

Added: 
    

Modified: 
    lldb/include/lldb/Host/windows/PythonPathSetup/PythonPathSetup.h
    lldb/source/Host/windows/PythonPathSetup/PythonPathSetup.cpp
    lldb/tools/driver/Driver.cpp
    lldb/tools/lldb-dap/tool/Options.td
    lldb/tools/lldb-dap/tool/lldb-dap.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Host/windows/PythonPathSetup/PythonPathSetup.h 
b/lldb/include/lldb/Host/windows/PythonPathSetup/PythonPathSetup.h
index 5016e35304f70..ca46c8b823cd1 100644
--- a/lldb/include/lldb/Host/windows/PythonPathSetup/PythonPathSetup.h
+++ b/lldb/include/lldb/Host/windows/PythonPathSetup/PythonPathSetup.h
@@ -42,6 +42,11 @@ bool AddPythonDLLToSearchPath();
 ///   can be loaded. If successful, returns immediately. Otherwise, attempts to
 ///   resolve the relative path and add it to the DLL search path, then checks
 ///   again if python3xx.dll can be loaded.
-llvm::Error SetupPythonRuntimeLibrary();
+///
+/// \return If LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME is defined, return the
+/// absolute path of the Python shared library which was resolved or an error 
if
+/// it could not be found. If LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME and
+/// LLDB_PYTHON_DLL_RELATIVE_PATH are not defined, return an empty string.
+llvm::Expected<std::string> SetupPythonRuntimeLibrary();
 
 #endif // LLDB_SOURCE_HOST_PYTHONPATHSETUP_H

diff  --git a/lldb/source/Host/windows/PythonPathSetup/PythonPathSetup.cpp 
b/lldb/source/Host/windows/PythonPathSetup/PythonPathSetup.cpp
index 0ca1189bec283..937977a6851c6 100644
--- a/lldb/source/Host/windows/PythonPathSetup/PythonPathSetup.cpp
+++ b/lldb/source/Host/windows/PythonPathSetup/PythonPathSetup.cpp
@@ -20,11 +20,10 @@
 using namespace llvm;
 
 #ifdef LLDB_PYTHON_DLL_RELATIVE_PATH
-/// Returns the full path to the lldb.exe executable.
-static std::string GetPathToExecutable() {
+static std::string GetModulePath(HMODULE module) {
   std::vector<WCHAR> buffer(MAX_PATH);
   while (buffer.size() <= PATHCCH_MAX_CCH) {
-    DWORD len = GetModuleFileNameW(NULL, buffer.data(), buffer.size());
+    DWORD len = GetModuleFileNameW(module, buffer.data(), buffer.size());
     if (len == 0)
       return "";
     if (len < buffer.size()) {
@@ -39,6 +38,9 @@ static std::string GetPathToExecutable() {
   return "";
 }
 
+/// Returns the full path to the lldb.exe executable.
+static std::string GetPathToExecutable() { return GetModulePath(NULL); }
+
 bool AddPythonDLLToSearchPath() {
   std::string path_str = GetPathToExecutable();
   if (path_str.empty())
@@ -60,26 +62,31 @@ bool AddPythonDLLToSearchPath() {
 #endif
 
 #ifdef LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME
-bool IsPythonDLLInPath() {
+std::optional<std::string> GetPythonDLLPath() {
 #define WIDEN2(x) L##x
 #define WIDEN(x) WIDEN2(x)
   HMODULE h = LoadLibraryW(WIDEN(LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME));
   if (!h)
-    return false;
+    return std::nullopt;
+
+  std::string path = GetModulePath(h);
   FreeLibrary(h);
-  return true;
+
+  return path;
 #undef WIDEN2
 #undef WIDEN
 }
 #endif
 
-llvm::Error SetupPythonRuntimeLibrary() {
+llvm::Expected<std::string> SetupPythonRuntimeLibrary() {
 #ifdef LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME
-  if (IsPythonDLLInPath())
-    return Error::success();
+  if (std::optional<std::string> python_path = GetPythonDLLPath())
+    return *python_path;
 #ifdef LLDB_PYTHON_DLL_RELATIVE_PATH
-  if (AddPythonDLLToSearchPath() && IsPythonDLLInPath())
-    return Error::success();
+  if (AddPythonDLLToSearchPath()) {
+    if (std::optional<std::string> python_path = GetPythonDLLPath())
+      return *python_path;
+  }
 #endif
   return createStringError(
       inconvertibleErrorCode(),
@@ -89,5 +96,5 @@ llvm::Error SetupPythonRuntimeLibrary() {
     return createStringError(inconvertibleErrorCode(),
                              "unable to find the Python runtime library");
 #endif
-  return Error::success();
+  return "";
 }

diff  --git a/lldb/tools/driver/Driver.cpp b/lldb/tools/driver/Driver.cpp
index 1f333cea9b50d..f0742e13c36f9 100644
--- a/lldb/tools/driver/Driver.cpp
+++ b/lldb/tools/driver/Driver.cpp
@@ -737,8 +737,10 @@ int main(int argc, char const *argv[]) {
 #endif
 
 #ifdef _WIN32
-  if (llvm::Error error = SetupPythonRuntimeLibrary())
-    llvm::WithColor::error() << llvm::toString(std::move(error)) << '\n';
+  auto python_path_or_err = SetupPythonRuntimeLibrary();
+  if (!python_path_or_err)
+    llvm::WithColor::error()
+        << llvm::toString(python_path_or_err.takeError()) << '\n';
 #endif
 
   // Parse arguments.

diff  --git a/lldb/tools/lldb-dap/tool/Options.td 
b/lldb/tools/lldb-dap/tool/Options.td
index 339a64fed6c32..f8adef7a19672 100644
--- a/lldb/tools/lldb-dap/tool/Options.td
+++ b/lldb/tools/lldb-dap/tool/Options.td
@@ -17,6 +17,10 @@ def: Flag<["-"], "v">,
   Alias<version>,
   HelpText<"Alias for --version">;
 
+def check_python : F<"check-python">,
+                   HelpText<"Prints the path to the resolved Python DLL. "
+                            "(Windows only).">;
+
 def wait_for_debugger: F<"wait-for-debugger">,
   HelpText<"Pause the program at startup.">;
 def: Flag<["-"], "g">,

diff  --git a/lldb/tools/lldb-dap/tool/lldb-dap.cpp 
b/lldb/tools/lldb-dap/tool/lldb-dap.cpp
index bfd7c5d39ec4a..af5c743a5edf1 100644
--- a/lldb/tools/lldb-dap/tool/lldb-dap.cpp
+++ b/lldb/tools/lldb-dap/tool/lldb-dap.cpp
@@ -523,11 +523,6 @@ int main(int argc, char *argv[]) {
                         "~/Library/Logs/DiagnosticReports/.\n");
 #endif
 
-#ifdef _WIN32
-  if (llvm::Error error = SetupPythonRuntimeLibrary())
-    llvm::WithColor::error() << llvm::toString(std::move(error)) << '\n';
-#endif
-
   llvm::SmallString<256> program_path(argv[0]);
   llvm::sys::fs::make_absolute(program_path);
   DAP::debug_adapter_path = program_path;
@@ -547,6 +542,30 @@ int main(int argc, char *argv[]) {
     return EXIT_SUCCESS;
   }
 
+#ifdef _WIN32
+  if (input_args.hasArg(OPT_check_python)) {
+    auto python_path_or_err = SetupPythonRuntimeLibrary();
+    if (!python_path_or_err) {
+      llvm::WithColor::error()
+          << llvm::toString(python_path_or_err.takeError()) << '\n';
+      return EXIT_FAILURE;
+    }
+    std::string python_path = *python_path_or_err;
+    if (python_path.empty()) {
+      llvm::WithColor::error()
+          << "unable to look for the Python shared library" << '\n';
+      return EXIT_FAILURE;
+    }
+    llvm::outs() << python_path << '\n';
+    return EXIT_SUCCESS;
+  }
+
+  auto python_path_or_err = SetupPythonRuntimeLibrary();
+  if (!python_path_or_err)
+    llvm::WithColor::error()
+        << llvm::toString(python_path_or_err.takeError()) << '\n';
+#endif
+
   if (input_args.hasArg(OPT_client)) {
     if (llvm::Error error = LaunchClient(input_args)) {
       llvm::WithColor::error() << llvm::toString(std::move(error)) << '\n';


        
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to