[Lldb-commits] [lldb] [lldb][windows] print an error if python.dll is not in the DLL search path (PR #164893)
Charles Zablit via lldb-commits
lldb-commits at lists.llvm.org
Mon Oct 27 14:34:09 PDT 2025
https://github.com/charles-zablit updated https://github.com/llvm/llvm-project/pull/164893
>From 3d2976b3e251bee88b9dee88fef0dcd9460dded2 Mon Sep 17 00:00:00 2001
From: Charles Zablit <c_zablit at apple.com>
Date: Thu, 23 Oct 2025 13:48:29 -0700
Subject: [PATCH 1/8] [lldb][windows] print an error if python.dll is not in
the DLL search path
---
lldb/CMakeLists.txt | 4 +++
lldb/tools/driver/CMakeLists.txt | 3 +++
lldb/tools/driver/Driver.cpp | 45 +++++++++++++++++++++++++-------
3 files changed, 42 insertions(+), 10 deletions(-)
diff --git a/lldb/CMakeLists.txt b/lldb/CMakeLists.txt
index e3b72e94d4beb..7c85c6fa8b825 100644
--- a/lldb/CMakeLists.txt
+++ b/lldb/CMakeLists.txt
@@ -61,6 +61,8 @@ if (LLDB_ENABLE_PYTHON)
"Path to python interpreter exectuable, relative to python's install prefix")
set(cachestring_LLDB_PYTHON_EXT_SUFFIX
"Filename extension for native code python modules")
+ set(cachestring_LLDB_PYTHON_SHARED_LIBRARY_NAME
+ "Filename of Python's shared library")
foreach(var LLDB_PYTHON_RELATIVE_PATH LLDB_PYTHON_EXE_RELATIVE_PATH LLDB_PYTHON_EXT_SUFFIX)
if(NOT DEFINED ${var} AND NOT CMAKE_CROSSCOMPILING)
@@ -87,6 +89,8 @@ if (LLDB_ENABLE_PYTHON)
set(LLDB_PYTHON_EXT_SUFFIX "_d${LLDB_PYTHON_EXT_SUFFIX}")
endif()
endif()
+ set(LLDB_PYTHON_SHARED_LIBRARY_FILENAME
+ "python${Python3_VERSION_MAJOR}${Python3_VERSION_MINOR}${CMAKE_SHARED_LIBRARY_SUFFIX}")
endif ()
if (LLDB_ENABLE_LUA)
diff --git a/lldb/tools/driver/CMakeLists.txt b/lldb/tools/driver/CMakeLists.txt
index 67956af7fe3fb..467ca9f91b3c1 100644
--- a/lldb/tools/driver/CMakeLists.txt
+++ b/lldb/tools/driver/CMakeLists.txt
@@ -37,6 +37,9 @@ add_dependencies(lldb
if(DEFINED LLDB_PYTHON_DLL_RELATIVE_PATH)
target_compile_definitions(lldb PRIVATE LLDB_PYTHON_DLL_RELATIVE_PATH="${LLDB_PYTHON_DLL_RELATIVE_PATH}")
endif()
+if(DEFINED LLDB_PYTHON_SHARED_LIBRARY_FILENAME)
+ target_compile_definitions(lldb PRIVATE LLDB_PYTHON_SHARED_LIBRARY_FILENAME="${LLDB_PYTHON_SHARED_LIBRARY_FILENAME}")
+endif()
if(LLDB_BUILD_FRAMEWORK)
# In the build-tree, we know the exact path to the framework directory.
diff --git a/lldb/tools/driver/Driver.cpp b/lldb/tools/driver/Driver.cpp
index ba0041111045b..96157525f3703 100644
--- a/lldb/tools/driver/Driver.cpp
+++ b/lldb/tools/driver/Driver.cpp
@@ -433,7 +433,8 @@ SBError Driver::ProcessArgs(const opt::InputArgList &args, bool &exiting) {
return error;
}
-#if defined(_WIN32) && defined(LLDB_PYTHON_DLL_RELATIVE_PATH)
+#ifdef _WIN32
+#ifdef LLDB_PYTHON_DLL_RELATIVE_PATH
/// Returns the full path to the lldb.exe executable.
inline std::wstring GetPathToExecutableW() {
// Iterate until we reach the Windows API maximum path length (32,767).
@@ -447,32 +448,51 @@ inline std::wstring GetPathToExecutableW() {
return L"";
}
-/// Resolve the full path of the directory defined by
+/// \brief Resolve the full path of the directory defined by
/// LLDB_PYTHON_DLL_RELATIVE_PATH. If it exists, add it to the list of DLL
/// search directories.
-void AddPythonDLLToSearchPath() {
+/// \return `true` if the library was added to the search path.
+/// `false` otherwise.
+bool AddPythonDLLToSearchPath() {
std::wstring modulePath = GetPathToExecutableW();
if (modulePath.empty()) {
- llvm::errs() << "error: unable to find python.dll." << '\n';
- return;
+ return false;
}
SmallVector<char, MAX_PATH> utf8Path;
if (sys::windows::UTF16ToUTF8(modulePath.c_str(), modulePath.length(),
utf8Path))
- return;
+ return false;
sys::path::remove_filename(utf8Path);
sys::path::append(utf8Path, LLDB_PYTHON_DLL_RELATIVE_PATH);
sys::fs::make_absolute(utf8Path);
SmallVector<wchar_t, 1> widePath;
if (sys::windows::widenPath(utf8Path.data(), widePath))
- return;
+ return false;
if (sys::fs::exists(utf8Path))
- SetDllDirectoryW(widePath.data());
+ return SetDllDirectoryW(widePath.data());
+ return false;
+}
+#endif
+
+#ifdef LLDB_PYTHON_SHARED_LIBRARY_FILENAME
+/// Returns whether `python3x.dll` is in the DLL search path.
+bool IsPythonDLLInPath() {
+#define WIDEN2(x) L##x
+#define WIDEN(x) WIDEN2(x)
+ WCHAR foundPath[MAX_PATH];
+ DWORD result =
+ SearchPathW(nullptr, WIDEN(LLDB_PYTHON_SHARED_LIBRARY_FILENAME), nullptr,
+ MAX_PATH, foundPath, nullptr);
+#undef WIDEN2
+#undef WIDEN
+
+ return result > 0;
}
#endif
+#endif
std::string EscapeString(std::string arg) {
std::string::size_type pos = 0;
@@ -776,8 +796,13 @@ int main(int argc, char const *argv[]) {
"~/Library/Logs/DiagnosticReports/.\n");
#endif
-#if defined(_WIN32) && defined(LLDB_PYTHON_DLL_RELATIVE_PATH)
- AddPythonDLLToSearchPath();
+#if defined(_WIN32) && defined(LLDB_PYTHON_SHARED_LIBRARY_FILENAME)
+ if (!IsPythonDLLInPath())
+#ifdef LLDB_PYTHON_DLL_RELATIVE_PATH
+ if (!AddPythonDLLToSearchPath())
+#endif
+ llvm::errs() << "error: unable to find "
+ << LLDB_PYTHON_SHARED_LIBRARY_FILENAME << ".\n";
#endif
// Parse arguments.
>From d502ba9336e3e94db57149a1dd7237b3b66b2bb3 Mon Sep 17 00:00:00 2001
From: Charles Zablit <c_zablit at apple.com>
Date: Thu, 23 Oct 2025 23:12:25 +0100
Subject: [PATCH 2/8] fixup! [lldb][windows] print an error if python.dll is
not in the DLL search path
---
lldb/CMakeLists.txt | 7 +++----
lldb/tools/driver/Driver.cpp | 25 ++++++++++++++++---------
2 files changed, 19 insertions(+), 13 deletions(-)
diff --git a/lldb/CMakeLists.txt b/lldb/CMakeLists.txt
index 7c85c6fa8b825..1e8530771a754 100644
--- a/lldb/CMakeLists.txt
+++ b/lldb/CMakeLists.txt
@@ -61,8 +61,8 @@ if (LLDB_ENABLE_PYTHON)
"Path to python interpreter exectuable, relative to python's install prefix")
set(cachestring_LLDB_PYTHON_EXT_SUFFIX
"Filename extension for native code python modules")
- set(cachestring_LLDB_PYTHON_SHARED_LIBRARY_NAME
- "Filename of Python's shared library")
+ set(cachestring_LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME
+ "Filename of Python's runtime library")
foreach(var LLDB_PYTHON_RELATIVE_PATH LLDB_PYTHON_EXE_RELATIVE_PATH LLDB_PYTHON_EXT_SUFFIX)
if(NOT DEFINED ${var} AND NOT CMAKE_CROSSCOMPILING)
@@ -89,8 +89,7 @@ if (LLDB_ENABLE_PYTHON)
set(LLDB_PYTHON_EXT_SUFFIX "_d${LLDB_PYTHON_EXT_SUFFIX}")
endif()
endif()
- set(LLDB_PYTHON_SHARED_LIBRARY_FILENAME
- "python${Python3_VERSION_MAJOR}${Python3_VERSION_MINOR}${CMAKE_SHARED_LIBRARY_SUFFIX}")
+ get_filename_component(LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME Python3_RUNTIME_LIBRARY NAME)
endif ()
if (LLDB_ENABLE_LUA)
diff --git a/lldb/tools/driver/Driver.cpp b/lldb/tools/driver/Driver.cpp
index 96157525f3703..8db0eaeffe71f 100644
--- a/lldb/tools/driver/Driver.cpp
+++ b/lldb/tools/driver/Driver.cpp
@@ -477,14 +477,14 @@ bool AddPythonDLLToSearchPath() {
}
#endif
-#ifdef LLDB_PYTHON_SHARED_LIBRARY_FILENAME
+#ifdef LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME
/// Returns whether `python3x.dll` is in the DLL search path.
bool IsPythonDLLInPath() {
#define WIDEN2(x) L##x
#define WIDEN(x) WIDEN2(x)
WCHAR foundPath[MAX_PATH];
DWORD result =
- SearchPathW(nullptr, WIDEN(LLDB_PYTHON_SHARED_LIBRARY_FILENAME), nullptr,
+ SearchPathW(nullptr, WIDEN(LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME), nullptr,
MAX_PATH, foundPath, nullptr);
#undef WIDEN2
#undef WIDEN
@@ -492,6 +492,18 @@ bool IsPythonDLLInPath() {
return result > 0;
}
#endif
+
+void SetupPythonRuntimeLibrary() {
+#ifdef LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME
+ if (!IsPythonDLLInPath())
+#ifdef LLDB_PYTHON_DLL_RELATIVE_PATH
+ if (AddPythonDLLToSearchPath())
+ return
+#endif
+ llvm::errs() << "error: unable to find "
+ << LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME << ".\n";
+#endif
+}
#endif
std::string EscapeString(std::string arg) {
@@ -796,13 +808,8 @@ int main(int argc, char const *argv[]) {
"~/Library/Logs/DiagnosticReports/.\n");
#endif
-#if defined(_WIN32) && defined(LLDB_PYTHON_SHARED_LIBRARY_FILENAME)
- if (!IsPythonDLLInPath())
-#ifdef LLDB_PYTHON_DLL_RELATIVE_PATH
- if (!AddPythonDLLToSearchPath())
-#endif
- llvm::errs() << "error: unable to find "
- << LLDB_PYTHON_SHARED_LIBRARY_FILENAME << ".\n";
+#ifdef _WIN32
+ SetupPythonRuntimeLibrary();
#endif
// Parse arguments.
>From c73bf5fc28b81ede7ed69f2a805027715edb53a9 Mon Sep 17 00:00:00 2001
From: Charles Zablit <c_zablit at apple.com>
Date: Thu, 23 Oct 2025 23:24:08 +0100
Subject: [PATCH 3/8] fixup! [lldb][windows] print an error if python.dll is
not in the DLL search path
---
lldb/tools/driver/Driver.cpp | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/lldb/tools/driver/Driver.cpp b/lldb/tools/driver/Driver.cpp
index 8db0eaeffe71f..acfb678e32c18 100644
--- a/lldb/tools/driver/Driver.cpp
+++ b/lldb/tools/driver/Driver.cpp
@@ -495,13 +495,18 @@ bool IsPythonDLLInPath() {
void SetupPythonRuntimeLibrary() {
#ifdef LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME
- if (!IsPythonDLLInPath())
+ if (IsPythonDLLInPath())
+ return;
#ifdef LLDB_PYTHON_DLL_RELATIVE_PATH
- if (AddPythonDLLToSearchPath())
- return
+ if (AddPythonDLLToSearchPath())
+ return
#endif
- llvm::errs() << "error: unable to find "
- << LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME << ".\n";
+ llvm::errs() << "error: unable to find "
+ << LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME << ".\n";
+ return;
+#elifdef LLDB_PYTHON_DLL_RELATIVE_PATH
+ if (!AddPythonDLLToSearchPath())
+ llvm::errs() << "error: unable to find the Python runtime library.\n";
#endif
}
#endif
>From 71178bb64c52fc381e261f831493837a914e3125 Mon Sep 17 00:00:00 2001
From: Charles Zablit <c_zablit at apple.com>
Date: Thu, 23 Oct 2025 15:41:25 -0700
Subject: [PATCH 4/8] fixup! [lldb][windows] print an error if python.dll is
not in the DLL search path
---
lldb/tools/driver/Driver.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lldb/tools/driver/Driver.cpp b/lldb/tools/driver/Driver.cpp
index acfb678e32c18..e868d4417f91f 100644
--- a/lldb/tools/driver/Driver.cpp
+++ b/lldb/tools/driver/Driver.cpp
@@ -504,7 +504,7 @@ void SetupPythonRuntimeLibrary() {
llvm::errs() << "error: unable to find "
<< LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME << ".\n";
return;
-#elifdef LLDB_PYTHON_DLL_RELATIVE_PATH
+#elif defined(LLDB_PYTHON_DLL_RELATIVE_PATH)
if (!AddPythonDLLToSearchPath())
llvm::errs() << "error: unable to find the Python runtime library.\n";
#endif
>From 6071b08505ede05139d2a733ffa0322ace65b75c Mon Sep 17 00:00:00 2001
From: Charles Zablit <c_zablit at apple.com>
Date: Thu, 23 Oct 2025 15:41:25 -0700
Subject: [PATCH 5/8] fixup! [lldb][windows] print an error if python.dll is
not in the DLL search path
---
lldb/CMakeLists.txt | 6 +++---
lldb/tools/driver/CMakeLists.txt | 4 ++--
lldb/tools/driver/Driver.cpp | 6 +++---
3 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/lldb/CMakeLists.txt b/lldb/CMakeLists.txt
index 1e8530771a754..966414d5ba309 100644
--- a/lldb/CMakeLists.txt
+++ b/lldb/CMakeLists.txt
@@ -61,8 +61,6 @@ if (LLDB_ENABLE_PYTHON)
"Path to python interpreter exectuable, relative to python's install prefix")
set(cachestring_LLDB_PYTHON_EXT_SUFFIX
"Filename extension for native code python modules")
- set(cachestring_LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME
- "Filename of Python's runtime library")
foreach(var LLDB_PYTHON_RELATIVE_PATH LLDB_PYTHON_EXE_RELATIVE_PATH LLDB_PYTHON_EXT_SUFFIX)
if(NOT DEFINED ${var} AND NOT CMAKE_CROSSCOMPILING)
@@ -89,7 +87,9 @@ if (LLDB_ENABLE_PYTHON)
set(LLDB_PYTHON_EXT_SUFFIX "_d${LLDB_PYTHON_EXT_SUFFIX}")
endif()
endif()
- get_filename_component(LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME Python3_RUNTIME_LIBRARY NAME)
+ if(DEFINED Python3_RUNTIME_LIBRARY AND NOT Python3_RUNTIME_LIBRARY STREQUAL Python3_RUNTIME_LIBRARY-NOTFOUND)
+ get_filename_component(LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME Python3_RUNTIME_LIBRARY NAME)
+ endif()
endif ()
if (LLDB_ENABLE_LUA)
diff --git a/lldb/tools/driver/CMakeLists.txt b/lldb/tools/driver/CMakeLists.txt
index 467ca9f91b3c1..efe51506f3545 100644
--- a/lldb/tools/driver/CMakeLists.txt
+++ b/lldb/tools/driver/CMakeLists.txt
@@ -37,8 +37,8 @@ add_dependencies(lldb
if(DEFINED LLDB_PYTHON_DLL_RELATIVE_PATH)
target_compile_definitions(lldb PRIVATE LLDB_PYTHON_DLL_RELATIVE_PATH="${LLDB_PYTHON_DLL_RELATIVE_PATH}")
endif()
-if(DEFINED LLDB_PYTHON_SHARED_LIBRARY_FILENAME)
- target_compile_definitions(lldb PRIVATE LLDB_PYTHON_SHARED_LIBRARY_FILENAME="${LLDB_PYTHON_SHARED_LIBRARY_FILENAME}")
+if(DEFINED LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME)
+ target_compile_definitions(lldb PRIVATE LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME="${LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME}")
endif()
if(LLDB_BUILD_FRAMEWORK)
diff --git a/lldb/tools/driver/Driver.cpp b/lldb/tools/driver/Driver.cpp
index e868d4417f91f..7fefa537be8f5 100644
--- a/lldb/tools/driver/Driver.cpp
+++ b/lldb/tools/driver/Driver.cpp
@@ -499,10 +499,10 @@ void SetupPythonRuntimeLibrary() {
return;
#ifdef LLDB_PYTHON_DLL_RELATIVE_PATH
if (AddPythonDLLToSearchPath())
- return
+ return;
#endif
- llvm::errs() << "error: unable to find "
- << LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME << ".\n";
+ llvm::errs() << "error: unable to find "
+ << LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME << ".\n";
return;
#elif defined(LLDB_PYTHON_DLL_RELATIVE_PATH)
if (!AddPythonDLLToSearchPath())
>From 855a40567db2cf2def6771c6f919f3bdee0f9858 Mon Sep 17 00:00:00 2001
From: Charles Zablit <c_zablit at apple.com>
Date: Fri, 24 Oct 2025 14:34:07 -0700
Subject: [PATCH 6/8] formatting fixup
---
lldb/tools/driver/Driver.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lldb/tools/driver/Driver.cpp b/lldb/tools/driver/Driver.cpp
index 7fefa537be8f5..96d9fc622fb07 100644
--- a/lldb/tools/driver/Driver.cpp
+++ b/lldb/tools/driver/Driver.cpp
@@ -502,7 +502,7 @@ void SetupPythonRuntimeLibrary() {
return;
#endif
llvm::errs() << "error: unable to find "
- << LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME << ".\n";
+ << LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME << ".\n";
return;
#elif defined(LLDB_PYTHON_DLL_RELATIVE_PATH)
if (!AddPythonDLLToSearchPath())
>From 6ee7fced58f28c0cefc55cc883242fe7949738e0 Mon Sep 17 00:00:00 2001
From: Charles Zablit <c_zablit at apple.com>
Date: Fri, 24 Oct 2025 15:31:48 -0700
Subject: [PATCH 7/8] use IMPORTED_LIBRARY_LOCATION
---
lldb/CMakeLists.txt | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/lldb/CMakeLists.txt b/lldb/CMakeLists.txt
index 966414d5ba309..3a09423ea04d6 100644
--- a/lldb/CMakeLists.txt
+++ b/lldb/CMakeLists.txt
@@ -87,8 +87,9 @@ if (LLDB_ENABLE_PYTHON)
set(LLDB_PYTHON_EXT_SUFFIX "_d${LLDB_PYTHON_EXT_SUFFIX}")
endif()
endif()
- if(DEFINED Python3_RUNTIME_LIBRARY AND NOT Python3_RUNTIME_LIBRARY STREQUAL Python3_RUNTIME_LIBRARY-NOTFOUND)
- get_filename_component(LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME Python3_RUNTIME_LIBRARY NAME)
+ get_target_property(_Python3_LIB_PATH Python3::Python IMPORTED_LIBRARY_LOCATION)
+ if(_Python3_LIB_PATH)
+ get_filename_component(LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME "${_Python3_LIB_PATH}" NAME)
endif()
endif ()
>From 8b34fea308d5a985672fd02f9958edae7ed6d86c Mon Sep 17 00:00:00 2001
From: Charles Zablit <c_zablit at apple.com>
Date: Mon, 27 Oct 2025 14:33:52 -0700
Subject: [PATCH 8/8] check if the Python target is defined
---
lldb/CMakeLists.txt | 8 +++++---
lldb/tools/driver/Driver.cpp | 7 +++----
2 files changed, 8 insertions(+), 7 deletions(-)
diff --git a/lldb/CMakeLists.txt b/lldb/CMakeLists.txt
index 3a09423ea04d6..01b5546fee00d 100644
--- a/lldb/CMakeLists.txt
+++ b/lldb/CMakeLists.txt
@@ -87,9 +87,11 @@ if (LLDB_ENABLE_PYTHON)
set(LLDB_PYTHON_EXT_SUFFIX "_d${LLDB_PYTHON_EXT_SUFFIX}")
endif()
endif()
- get_target_property(_Python3_LIB_PATH Python3::Python IMPORTED_LIBRARY_LOCATION)
- if(_Python3_LIB_PATH)
- get_filename_component(LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME "${_Python3_LIB_PATH}" NAME)
+ if(TARGET Python3::Python)
+ get_target_property(_Python3_LIB_PATH Python3::Python IMPORTED_LIBRARY_LOCATION)
+ if(_Python3_LIB_PATH)
+ get_filename_component(LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME "${_Python3_LIB_PATH}" NAME)
+ endif()
endif()
endif ()
diff --git a/lldb/tools/driver/Driver.cpp b/lldb/tools/driver/Driver.cpp
index 96d9fc622fb07..dca23984531c2 100644
--- a/lldb/tools/driver/Driver.cpp
+++ b/lldb/tools/driver/Driver.cpp
@@ -455,9 +455,8 @@ inline std::wstring GetPathToExecutableW() {
/// `false` otherwise.
bool AddPythonDLLToSearchPath() {
std::wstring modulePath = GetPathToExecutableW();
- if (modulePath.empty()) {
+ if (modulePath.empty())
return false;
- }
SmallVector<char, MAX_PATH> utf8Path;
if (sys::windows::UTF16ToUTF8(modulePath.c_str(), modulePath.length(),
@@ -501,8 +500,8 @@ void SetupPythonRuntimeLibrary() {
if (AddPythonDLLToSearchPath())
return;
#endif
- llvm::errs() << "error: unable to find "
- << LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME << ".\n";
+ llvm::errs() << "error: unable to find '"
+ << LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME << "'.\n";
return;
#elif defined(LLDB_PYTHON_DLL_RELATIVE_PATH)
if (!AddPythonDLLToSearchPath())
More information about the lldb-commits
mailing list