Skip to content

Commit 37cf46e

Browse files
[lldb][windows] add support for out of PATH python.dll resolution
1 parent ed0e531 commit 37cf46e

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

lldb/cmake/modules/AddLLDB.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ function(add_lldb_executable name)
167167
)
168168

169169
target_link_libraries(${name} PRIVATE ${ARG_LINK_LIBS})
170+
if(WIN32)
171+
list(FIND ARG_LINK_LIBS liblldb LIBLLD_INDEX)
172+
if(NOT LIBLLD_INDEX EQUAL -1)
173+
target_link_options(${name} PRIVATE "/DELAYLOAD:$<TARGET_FILE_BASE_NAME:liblldb>.dll")
174+
endif()
175+
endif()
170176
if(CLANG_LINK_CLANG_DYLIB)
171177
target_link_libraries(${name} PRIVATE clang-cpp)
172178
else()

lldb/tools/driver/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ add_dependencies(lldb
3434
${tablegen_deps}
3535
)
3636

37+
if(DEFINED LLDB_PYTHON_DLL_RELATIVE_PATH)
38+
target_compile_definitions(lldb PRIVATE LLDB_PYTHON_DLL_RELATIVE_PATH="${LLDB_PYTHON_DLL_RELATIVE_PATH}")
39+
endif()
40+
3741
if(LLDB_BUILD_FRAMEWORK)
3842
# In the build-tree, we know the exact path to the framework directory.
3943
# The installed framework can be in different locations.

lldb/tools/driver/Driver.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@
2222
#include "lldb/Host/MainLoop.h"
2323
#include "lldb/Host/MainLoopBase.h"
2424
#include "lldb/Utility/Status.h"
25+
#include "llvm/ADT/SmallString.h"
2526
#include "llvm/ADT/StringRef.h"
27+
#include "llvm/Support/ConvertUTF.h"
28+
#include "llvm/Support/FileSystem.h"
2629
#include "llvm/Support/Format.h"
2730
#include "llvm/Support/InitLLVM.h"
2831
#include "llvm/Support/Path.h"
2932
#include "llvm/Support/Signals.h"
33+
#include "llvm/Support/Windows/WindowsSupport.h"
3034
#include "llvm/Support/WithColor.h"
3135
#include "llvm/Support/raw_ostream.h"
3236

@@ -426,6 +430,47 @@ SBError Driver::ProcessArgs(const opt::InputArgList &args, bool &exiting) {
426430
return error;
427431
}
428432

433+
#ifdef _WIN32
434+
// Returns the full path to the lldb.exe executable
435+
inline std::wstring GetPathToExecutableW() {
436+
// Iterate until we reach the Windows max path length (32,767).
437+
std::vector<WCHAR> buffer;
438+
buffer.resize(MAX_PATH);
439+
while (buffer.size() < 32767) {
440+
if (GetModuleFileNameW(NULL, buffer.data(), buffer.size()) < buffer.size())
441+
return std::wstring(buffer.begin(), buffer.end());
442+
buffer.resize(buffer.size() * 2);
443+
}
444+
return L"";
445+
}
446+
447+
// Resolve the full path of the directory defined by
448+
// LLDB_PYTHON_DLL_RELATIVE_PATH. If it exists, add it to the list of DLL search
449+
// directories.
450+
void AddPythonDLLToSearchPath() {
451+
std::wstring modulePath = GetPathToExecutableW();
452+
if (modulePath.empty()) {
453+
WithColor::error() << "Unable to find python: " << GetLastError() << '\n';
454+
return;
455+
}
456+
457+
SmallVector<char, MAX_PATH> utf8Path;
458+
if (sys::windows::UTF16ToUTF8(modulePath.c_str(), modulePath.length(),
459+
utf8Path))
460+
return;
461+
sys::path::remove_filename(utf8Path);
462+
sys::path::append(utf8Path, LLDB_PYTHON_DLL_RELATIVE_PATH);
463+
sys::fs::make_absolute(utf8Path);
464+
465+
SmallVector<wchar_t, 1> widePath;
466+
if (sys::windows::widenPath(utf8Path.data(), widePath))
467+
return;
468+
469+
if (sys::fs::exists(utf8Path))
470+
SetDllDirectoryW(widePath.data());
471+
}
472+
#endif
473+
429474
std::string EscapeString(std::string arg) {
430475
std::string::size_type pos = 0;
431476
while ((pos = arg.find_first_of("\"\\", pos)) != std::string::npos) {
@@ -728,6 +773,10 @@ int main(int argc, char const *argv[]) {
728773
"~/Library/Logs/DiagnosticReports/.\n");
729774
#endif
730775

776+
#ifdef _WIN32
777+
AddPythonDLLToSearchPath();
778+
#endif
779+
731780
// Parse arguments.
732781
LLDBOptTable T;
733782
unsigned MissingArgIndex;

0 commit comments

Comments
 (0)