Skip to content

Commit b56cae7

Browse files
[lldb][windows] print an error if python.dll is not in the DLL search path (llvm#164893)
This is a follow up to llvm#162509. Using the `SearchPathW` API, we can ensure that the correct version of Python is installed before `liblldb` is loaded (and `python.dll` subsequently). If it's not, we try to add it to the search path with the methods introduced in llvm#162509. If that fails or if that method is `#ifdef`'d out, we print an error which will appear before lldb crashes due to the missing dll. Before llvm#162509, when invoked from Powershell, lldb would silently crash (no error message/crash report). After llvm#162509, it crashes without any indications that the root cause is the missing python.dll. With this patch, we print the error before crashing.
1 parent 10999b6 commit b56cae7

File tree

3 files changed

+65
-12
lines changed

3 files changed

+65
-12
lines changed

lldb/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ if (LLDB_ENABLE_PYTHON)
110110
set(LLDB_PYTHON_EXT_SUFFIX "_d${LLDB_PYTHON_EXT_SUFFIX}")
111111
endif()
112112
endif()
113+
if(TARGET Python3::Python)
114+
get_target_property(_Python3_LIB_PATH Python3::Python IMPORTED_LIBRARY_LOCATION)
115+
if(_Python3_LIB_PATH)
116+
get_filename_component(LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME "${_Python3_LIB_PATH}" NAME)
117+
endif()
118+
endif()
113119
endif ()
114120

115121
if (LLDB_ENABLE_LUA)

lldb/tools/driver/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ add_dependencies(lldb
3131
if(DEFINED LLDB_PYTHON_DLL_RELATIVE_PATH)
3232
target_compile_definitions(lldb PRIVATE LLDB_PYTHON_DLL_RELATIVE_PATH="${LLDB_PYTHON_DLL_RELATIVE_PATH}")
3333
endif()
34+
if(DEFINED LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME)
35+
target_compile_definitions(lldb PRIVATE LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME="${LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME}")
36+
endif()
3437

3538
if(LLDB_BUILD_FRAMEWORK)
3639
# In the build-tree, we know the exact path to the framework directory.

lldb/tools/driver/Driver.cpp

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,8 @@ SBError Driver::ProcessArgs(const opt::InputArgList &args, bool &exiting) {
430430
return error;
431431
}
432432

433-
#if defined(_WIN32) && defined(LLDB_PYTHON_DLL_RELATIVE_PATH)
433+
#ifdef _WIN32
434+
#ifdef LLDB_PYTHON_DLL_RELATIVE_PATH
434435
/// Returns the full path to the lldb.exe executable.
435436
inline std::wstring GetPathToExecutableW() {
436437
// Iterate until we reach the Windows API maximum path length (32,767).
@@ -444,30 +445,73 @@ inline std::wstring GetPathToExecutableW() {
444445
return L"";
445446
}
446447

447-
/// Resolve the full path of the directory defined by
448+
/// \brief Resolve the full path of the directory defined by
448449
/// LLDB_PYTHON_DLL_RELATIVE_PATH. If it exists, add it to the list of DLL
449450
/// search directories.
450-
void AddPythonDLLToSearchPath() {
451+
/// \return `true` if the library was added to the search path.
452+
/// `false` otherwise.
453+
bool AddPythonDLLToSearchPath() {
451454
std::wstring modulePath = GetPathToExecutableW();
452-
if (modulePath.empty()) {
453-
llvm::errs() << "error: unable to find python.dll." << '\n';
454-
return;
455-
}
455+
if (modulePath.empty())
456+
return false;
456457

457458
SmallVector<char, MAX_PATH> utf8Path;
458459
if (sys::windows::UTF16ToUTF8(modulePath.c_str(), modulePath.length(),
459460
utf8Path))
460-
return;
461+
return false;
461462
sys::path::remove_filename(utf8Path);
462463
sys::path::append(utf8Path, LLDB_PYTHON_DLL_RELATIVE_PATH);
463464
sys::fs::make_absolute(utf8Path);
464465

465466
SmallVector<wchar_t, 1> widePath;
466467
if (sys::windows::widenPath(utf8Path.data(), widePath))
467-
return;
468+
return false;
468469

469470
if (sys::fs::exists(utf8Path))
470-
SetDllDirectoryW(widePath.data());
471+
return SetDllDirectoryW(widePath.data());
472+
return false;
473+
}
474+
#endif
475+
476+
#ifdef LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME
477+
/// Returns whether `python3x.dll` is in the DLL search path.
478+
bool IsPythonDLLInPath() {
479+
#define WIDEN2(x) L##x
480+
#define WIDEN(x) WIDEN2(x)
481+
WCHAR foundPath[MAX_PATH];
482+
DWORD result =
483+
SearchPathW(nullptr, WIDEN(LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME), nullptr,
484+
MAX_PATH, foundPath, nullptr);
485+
#undef WIDEN2
486+
#undef WIDEN
487+
488+
return result > 0;
489+
}
490+
#endif
491+
492+
/// Try to setup the DLL search path for the Python Runtime Library
493+
/// (python3xx.dll).
494+
///
495+
/// If `LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME` is set, we first check if
496+
/// python3xx.dll is in the search path. If it's not, we try to add it and
497+
/// check for it a second time.
498+
/// If only `LLDB_PYTHON_DLL_RELATIVE_PATH` is set, we try to add python3xx.dll
499+
/// to the search path python.dll is already in the search path or not.
500+
void SetupPythonRuntimeLibrary() {
501+
#ifdef LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME
502+
if (IsPythonDLLInPath())
503+
return;
504+
#ifdef LLDB_PYTHON_DLL_RELATIVE_PATH
505+
if (AddPythonDLLToSearchPath() && IsPythonDLLInPath())
506+
return;
507+
#endif
508+
llvm::errs() << "error: unable to find '"
509+
<< LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME << "'.\n";
510+
return;
511+
#elif defined(LLDB_PYTHON_DLL_RELATIVE_PATH)
512+
if (!AddPythonDLLToSearchPath())
513+
llvm::errs() << "error: unable to find the Python runtime library.\n";
514+
#endif
471515
}
472516
#endif
473517

@@ -801,8 +845,8 @@ int main(int argc, char const *argv[]) {
801845
"~/Library/Logs/DiagnosticReports/.\n");
802846
#endif
803847

804-
#if defined(_WIN32) && defined(LLDB_PYTHON_DLL_RELATIVE_PATH)
805-
AddPythonDLLToSearchPath();
848+
#ifdef _WIN32
849+
SetupPythonRuntimeLibrary();
806850
#endif
807851

808852
// Parse arguments.

0 commit comments

Comments
 (0)