Skip to content

Commit 64aa88f

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 f8a6917 commit 64aa88f

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
@@ -113,6 +113,12 @@ if (LLDB_ENABLE_PYTHON)
113113
set(LLDB_PYTHON_EXT_SUFFIX "_d${LLDB_PYTHON_EXT_SUFFIX}")
114114
endif()
115115
endif()
116+
if(TARGET Python3::Python)
117+
get_target_property(_Python3_LIB_PATH Python3::Python IMPORTED_LIBRARY_LOCATION)
118+
if(_Python3_LIB_PATH)
119+
get_filename_component(LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME "${_Python3_LIB_PATH}" NAME)
120+
endif()
121+
endif()
116122
endif ()
117123

118124
if (LLDB_ENABLE_LUA)

lldb/tools/driver/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ add_dependencies(lldb
3737
if(DEFINED LLDB_PYTHON_DLL_RELATIVE_PATH)
3838
target_compile_definitions(lldb PRIVATE LLDB_PYTHON_DLL_RELATIVE_PATH="${LLDB_PYTHON_DLL_RELATIVE_PATH}")
3939
endif()
40+
if(DEFINED LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME)
41+
target_compile_definitions(lldb PRIVATE LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME="${LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME}")
42+
endif()
4043

4144
if(LLDB_BUILD_FRAMEWORK)
4245
# 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
@@ -443,7 +443,8 @@ SBError Driver::ProcessArgs(const opt::InputArgList &args, bool &exiting) {
443443
return error;
444444
}
445445

446-
#if defined(_WIN32) && defined(LLDB_PYTHON_DLL_RELATIVE_PATH)
446+
#ifdef _WIN32
447+
#ifdef LLDB_PYTHON_DLL_RELATIVE_PATH
447448
/// Returns the full path to the lldb.exe executable.
448449
inline std::wstring GetPathToExecutableW() {
449450
// Iterate until we reach the Windows API maximum path length (32,767).
@@ -457,30 +458,73 @@ inline std::wstring GetPathToExecutableW() {
457458
return L"";
458459
}
459460

460-
/// Resolve the full path of the directory defined by
461+
/// \brief Resolve the full path of the directory defined by
461462
/// LLDB_PYTHON_DLL_RELATIVE_PATH. If it exists, add it to the list of DLL
462463
/// search directories.
463-
void AddPythonDLLToSearchPath() {
464+
/// \return `true` if the library was added to the search path.
465+
/// `false` otherwise.
466+
bool AddPythonDLLToSearchPath() {
464467
std::wstring modulePath = GetPathToExecutableW();
465-
if (modulePath.empty()) {
466-
llvm::errs() << "error: unable to find python.dll." << '\n';
467-
return;
468-
}
468+
if (modulePath.empty())
469+
return false;
469470

470471
SmallVector<char, MAX_PATH> utf8Path;
471472
if (sys::windows::UTF16ToUTF8(modulePath.c_str(), modulePath.length(),
472473
utf8Path))
473-
return;
474+
return false;
474475
sys::path::remove_filename(utf8Path);
475476
sys::path::append(utf8Path, LLDB_PYTHON_DLL_RELATIVE_PATH);
476477
sys::fs::make_absolute(utf8Path);
477478

478479
SmallVector<wchar_t, 1> widePath;
479480
if (sys::windows::widenPath(utf8Path.data(), widePath))
480-
return;
481+
return false;
481482

482483
if (sys::fs::exists(utf8Path))
483-
SetDllDirectoryW(widePath.data());
484+
return SetDllDirectoryW(widePath.data());
485+
return false;
486+
}
487+
#endif
488+
489+
#ifdef LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME
490+
/// Returns whether `python3x.dll` is in the DLL search path.
491+
bool IsPythonDLLInPath() {
492+
#define WIDEN2(x) L##x
493+
#define WIDEN(x) WIDEN2(x)
494+
WCHAR foundPath[MAX_PATH];
495+
DWORD result =
496+
SearchPathW(nullptr, WIDEN(LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME), nullptr,
497+
MAX_PATH, foundPath, nullptr);
498+
#undef WIDEN2
499+
#undef WIDEN
500+
501+
return result > 0;
502+
}
503+
#endif
504+
505+
/// Try to setup the DLL search path for the Python Runtime Library
506+
/// (python3xx.dll).
507+
///
508+
/// If `LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME` is set, we first check if
509+
/// python3xx.dll is in the search path. If it's not, we try to add it and
510+
/// check for it a second time.
511+
/// If only `LLDB_PYTHON_DLL_RELATIVE_PATH` is set, we try to add python3xx.dll
512+
/// to the search path python.dll is already in the search path or not.
513+
void SetupPythonRuntimeLibrary() {
514+
#ifdef LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME
515+
if (IsPythonDLLInPath())
516+
return;
517+
#ifdef LLDB_PYTHON_DLL_RELATIVE_PATH
518+
if (AddPythonDLLToSearchPath() && IsPythonDLLInPath())
519+
return;
520+
#endif
521+
llvm::errs() << "error: unable to find '"
522+
<< LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME << "'.\n";
523+
return;
524+
#elif defined(LLDB_PYTHON_DLL_RELATIVE_PATH)
525+
if (!AddPythonDLLToSearchPath())
526+
llvm::errs() << "error: unable to find the Python runtime library.\n";
527+
#endif
484528
}
485529
#endif
486530

@@ -786,8 +830,8 @@ int main(int argc, char const *argv[]) {
786830
"~/Library/Logs/DiagnosticReports/.\n");
787831
#endif
788832

789-
#if defined(_WIN32) && defined(LLDB_PYTHON_DLL_RELATIVE_PATH)
790-
AddPythonDLLToSearchPath();
833+
#ifdef _WIN32
834+
SetupPythonRuntimeLibrary();
791835
#endif
792836

793837
// Parse arguments.

0 commit comments

Comments
 (0)