Skip to content

Commit 3d2976b

Browse files
[lldb][windows] print an error if python.dll is not in the DLL search path
1 parent 4d80e0c commit 3d2976b

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

lldb/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ if (LLDB_ENABLE_PYTHON)
6161
"Path to python interpreter exectuable, relative to python's install prefix")
6262
set(cachestring_LLDB_PYTHON_EXT_SUFFIX
6363
"Filename extension for native code python modules")
64+
set(cachestring_LLDB_PYTHON_SHARED_LIBRARY_NAME
65+
"Filename of Python's shared library")
6466

6567
foreach(var LLDB_PYTHON_RELATIVE_PATH LLDB_PYTHON_EXE_RELATIVE_PATH LLDB_PYTHON_EXT_SUFFIX)
6668
if(NOT DEFINED ${var} AND NOT CMAKE_CROSSCOMPILING)
@@ -87,6 +89,8 @@ if (LLDB_ENABLE_PYTHON)
8789
set(LLDB_PYTHON_EXT_SUFFIX "_d${LLDB_PYTHON_EXT_SUFFIX}")
8890
endif()
8991
endif()
92+
set(LLDB_PYTHON_SHARED_LIBRARY_FILENAME
93+
"python${Python3_VERSION_MAJOR}${Python3_VERSION_MINOR}${CMAKE_SHARED_LIBRARY_SUFFIX}")
9094
endif ()
9195

9296
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_SHARED_LIBRARY_FILENAME)
41+
target_compile_definitions(lldb PRIVATE LLDB_PYTHON_SHARED_LIBRARY_FILENAME="${LLDB_PYTHON_SHARED_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: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,8 @@ SBError Driver::ProcessArgs(const opt::InputArgList &args, bool &exiting) {
433433
return error;
434434
}
435435

436-
#if defined(_WIN32) && defined(LLDB_PYTHON_DLL_RELATIVE_PATH)
436+
#ifdef _WIN32
437+
#ifdef LLDB_PYTHON_DLL_RELATIVE_PATH
437438
/// Returns the full path to the lldb.exe executable.
438439
inline std::wstring GetPathToExecutableW() {
439440
// Iterate until we reach the Windows API maximum path length (32,767).
@@ -447,32 +448,51 @@ inline std::wstring GetPathToExecutableW() {
447448
return L"";
448449
}
449450

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

460462
SmallVector<char, MAX_PATH> utf8Path;
461463
if (sys::windows::UTF16ToUTF8(modulePath.c_str(), modulePath.length(),
462464
utf8Path))
463-
return;
465+
return false;
464466
sys::path::remove_filename(utf8Path);
465467
sys::path::append(utf8Path, LLDB_PYTHON_DLL_RELATIVE_PATH);
466468
sys::fs::make_absolute(utf8Path);
467469

468470
SmallVector<wchar_t, 1> widePath;
469471
if (sys::windows::widenPath(utf8Path.data(), widePath))
470-
return;
472+
return false;
471473

472474
if (sys::fs::exists(utf8Path))
473-
SetDllDirectoryW(widePath.data());
475+
return SetDllDirectoryW(widePath.data());
476+
return false;
477+
}
478+
#endif
479+
480+
#ifdef LLDB_PYTHON_SHARED_LIBRARY_FILENAME
481+
/// Returns whether `python3x.dll` is in the DLL search path.
482+
bool IsPythonDLLInPath() {
483+
#define WIDEN2(x) L##x
484+
#define WIDEN(x) WIDEN2(x)
485+
WCHAR foundPath[MAX_PATH];
486+
DWORD result =
487+
SearchPathW(nullptr, WIDEN(LLDB_PYTHON_SHARED_LIBRARY_FILENAME), nullptr,
488+
MAX_PATH, foundPath, nullptr);
489+
#undef WIDEN2
490+
#undef WIDEN
491+
492+
return result > 0;
474493
}
475494
#endif
495+
#endif
476496

477497
std::string EscapeString(std::string arg) {
478498
std::string::size_type pos = 0;
@@ -776,8 +796,13 @@ int main(int argc, char const *argv[]) {
776796
"~/Library/Logs/DiagnosticReports/.\n");
777797
#endif
778798

779-
#if defined(_WIN32) && defined(LLDB_PYTHON_DLL_RELATIVE_PATH)
780-
AddPythonDLLToSearchPath();
799+
#if defined(_WIN32) && defined(LLDB_PYTHON_SHARED_LIBRARY_FILENAME)
800+
if (!IsPythonDLLInPath())
801+
#ifdef LLDB_PYTHON_DLL_RELATIVE_PATH
802+
if (!AddPythonDLLToSearchPath())
803+
#endif
804+
llvm::errs() << "error: unable to find "
805+
<< LLDB_PYTHON_SHARED_LIBRARY_FILENAME << ".\n";
781806
#endif
782807

783808
// Parse arguments.

0 commit comments

Comments
 (0)