30#include "llvm/ADT/STLExtras.h"
31#include "llvm/Support/ConvertUTF.h"
32#include "llvm/Support/Threading.h"
33#include "llvm/Support/raw_ostream.h"
39#ifndef STATUS_WX86_BREAKPOINT
40#define STATUS_WX86_BREAKPOINT 0x4000001FL
55 L
"kernel32.dll",
"WaitForDebugEventEx"};
60 if (!s_wait_for_debug_event_ex) {
63 "WaitForDebugEventEx unavailable, using WaitForDebugEvent instead. "
64 "Unicode strings from OutputDebugStringW might show incorrectly.");
86 "lldb.plugin.process-windows.secondary[?]",
88 if (!secondary_thread) {
90 LLDB_LOG(log,
"couldn't launch debugger thread. {0}", result);
99 LLDB_LOG(log,
"attaching to '{0}'", pid);
103 "lldb.plugin.process-windows.secondary[?]", [
this, pid, attach_info] {
106 if (!secondary_thread) {
108 LLDB_LOG(log,
"couldn't attach to process '{0}'. {1}", pid, result);
118 std::shared_ptr<DebuggerThread> this_ref(shared_from_this());
121 LLDB_LOG(log,
"preparing to launch '{0}' on background thread.",
145 std::shared_ptr<DebuggerThread> this_ref(shared_from_this());
148 LLDB_LOG(log,
"preparing to attach to process '{0}' on background thread.",
151 if (!DebugActiveProcess(
static_cast<DWORD
>(pid))) {
171 LLDB_LOG(log,
"terminate = {0}, inferior={1}.", terminate, pid);
175 bool expected =
false;
190 BOOL terminate_suceeded = TerminateProcess(handle, 0);
192 "calling TerminateProcess({0}, 0) (inferior={1}), success={2}",
193 handle, pid, terminate_suceeded);
196 "NOT calling TerminateProcess because the inferior is not valid "
197 "({0}, 0) (inferior={1})",
207 LLDB_LOG(log,
"masking active exception");
217 if (!::DebugBreakProcess(
218 GetProcess().GetNativeProcess().GetSystemHandle())) {
223 LLDB_LOG(log,
"waiting for detach from process {0} to complete.", pid);
226 if (wait_result != WAIT_OBJECT_0) {
228 LLDB_LOG(log,
"error: WaitForSingleObject({0}, 5000) returned {1}",
231 LLDB_LOG(log,
"detach from process {0} completed successfully.", pid);
233 if (!
error.Success()) {
234 LLDB_LOG(log,
"encountered an error while trying to stop process {0}. {1}",
245 LLDB_LOG(log,
"broadcasting for inferior process {0}.",
263 DEBUG_EVENT dbe = {};
264 bool should_debug =
true;
266 while (should_debug) {
270 DWORD continue_status = DBG_CONTINUE;
272 switch (dbe.dwDebugEventCode) {
274 llvm_unreachable(
"Unhandle debug event code!");
275 case EXCEPTION_DEBUG_EVENT: {
277 dbe.u.Exception, dbe.dwThreadId, shutting_down);
280 continue_status = DBG_CONTINUE;
282 continue_status = DBG_EXCEPTION_NOT_HANDLED;
286 case CREATE_THREAD_DEBUG_EVENT:
290 case CREATE_PROCESS_DEBUG_EVENT:
294 case EXIT_THREAD_DEBUG_EVENT:
298 case EXIT_PROCESS_DEBUG_EVENT:
301 should_debug =
false;
303 case LOAD_DLL_DEBUG_EVENT:
306 case UNLOAD_DLL_DEBUG_EVENT:
309 case OUTPUT_DEBUG_STRING_EVENT:
310 continue_status =
HandleODSEvent(dbe.u.DebugString, dbe.dwThreadId);
314 if (dbe.u.RipInfo.dwType == SLE_ERROR)
315 should_debug =
false;
320 log,
"calling ContinueDebugEvent({0}, {1}, {2}) on thread {3}.",
321 dbe.dwProcessId, dbe.dwThreadId, continue_status,
322 ::GetCurrentThreadId());
324 ::ContinueDebugEvent(dbe.dwProcessId, dbe.dwThreadId, continue_status);
333 (dbe.u.Exception.ExceptionRecord.ExceptionCode ==
334 EXCEPTION_BREAKPOINT ||
335 dbe.u.Exception.ExceptionRecord.ExceptionCode ==
338 "Breakpoint exception is cue to detach from process {0:x}",
347 continue_status = DBG_CONTINUE;
348 if (dbe.dwDebugEventCode == EXCEPTION_DEBUG_EVENT &&
349 !(dbe.u.Exception.ExceptionRecord.ExceptionCode ==
350 EXCEPTION_BREAKPOINT ||
351 dbe.u.Exception.ExceptionRecord.ExceptionCode ==
353 dbe.u.Exception.ExceptionRecord.ExceptionCode ==
354 EXCEPTION_SINGLE_STEP))
355 continue_status = DBG_EXCEPTION_NOT_HANDLED;
356 ::ContinueDebugEvent(dbe.dwProcessId, dbe.dwThreadId,
366 should_debug =
false;
369 LLDB_LOG(log,
"returned FALSE from WaitForDebugEventEx. Error = {0}",
372 should_debug =
false;
377 LLDB_LOG(log,
"WaitForDebugEventEx loop completed, exiting.");
383 DWORD thread_id,
bool shutting_down) {
387 (info.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT ||
397 bool first_chance = (info.dwFirstChance != 0);
401 LLDB_LOG(log,
"encountered {0} chance exception {1:x} on thread {2:x}",
402 first_chance ?
"first" :
"second",
403 info.ExceptionRecord.ExceptionCode, thread_id);
409 LLDB_LOG(log,
"waiting for ExceptionPred != BreakInDebugger");
421 LLDB_LOG(log,
"Thread {0} spawned in process {1}", thread_id,
424 thread.GetNativeThread().SetOwnsHandle(
false);
433 uint32_t process_id = ::GetProcessId(info.hProcess);
435 LLDB_LOG(log,
"process {0} spawned", process_id);
437 std::string thread_name;
438 llvm::raw_string_ostream name_stream(thread_name);
439 name_stream <<
"lldb.plugin.process-windows.secondary[" << process_id <<
"]";
440 llvm::set_thread_name(thread_name);
460 LLDB_LOG(log,
"Thread {0} exited with code {1} in process {2}", thread_id,
461 info.dwExitCode,
m_process.GetProcessId());
478static std::optional<std::string>
482 llvm::SmallVector<wchar_t, MAX_PATH> vol_name(MAX_PATH);
483 HANDLE vol_iter = ::FindFirstVolumeW(vol_name.data(), vol_name.size());
484 if (vol_iter == INVALID_HANDLE_VALUE) {
486 "ConvertNtDevicePathToDosPath: FindFirstVolumeW failed, "
491 llvm::scope_exit close_iter([&] { ::FindVolumeClose(vol_iter); });
496 size_t vol_len = ::wcsnlen(vol_name.data(), vol_name.size());
497 if (vol_len < 5 || vol_name[vol_len - 1] != L
'\\')
500 vol_name[vol_len - 1] = L
'\0';
501 llvm::SmallVector<wchar_t, MAX_PATH> dev_name(MAX_PATH);
502 bool ok = ::QueryDosDeviceW(vol_name.data() + 4,
503 dev_name.data(), dev_name.size());
504 vol_name[vol_len - 1] = L
'\\';
509 size_t dev_len = ::wcsnlen(dev_name.data(), dev_name.size());
510 if (dev_len == 0 || dev_len >= nt_path.size())
512 if (_wcsnicmp(nt_path.data(), dev_name.data(), dev_len) != 0)
514 if (nt_path[dev_len] != L
'\\')
518 llvm::ArrayRef<wchar_t> mount(vol_name.data(), vol_len);
519 llvm::SmallVector<wchar_t> mount_names;
520 DWORD names_size = 0;
521 ::GetVolumePathNamesForVolumeNameW(vol_name.data(),
nullptr, 0,
523 if (names_size > 1) {
524 mount_names.resize(names_size);
526 if (::GetVolumePathNamesForVolumeNameW(
527 vol_name.data(), mount_names.data(), names_size, &written) &&
528 mount_names[0] != L
'\0') {
529 mount = llvm::ArrayRef<wchar_t>(
531 ::wcsnlen(mount_names.data(), mount_names.size()));
536 llvm::SmallVector<wchar_t> dos_wide(mount.begin(), mount.end());
537 if (!dos_wide.empty() && dos_wide.back() == L
'\\')
539 dos_wide.append(nt_path.begin() + dev_len, nt_path.end());
542 llvm::convertWideToUTF8(std::wstring(dos_wide.begin(), dos_wide.end()),
545 }
while (::FindNextVolumeW(vol_iter, vol_name.data(), vol_name.size()));
547 LLDB_LOG(log,
"ConvertNtDevicePathToDosPath: no matching volume found");
553 DWORD dwFileSizeHi = 0;
554 DWORD dwFileSizeLo = ::GetFileSize(hFile, &dwFileSizeHi);
555 if (dwFileSizeLo == 0 && dwFileSizeHi == 0)
559 ::CreateFileMappingW(hFile,
nullptr, PAGE_READONLY, 0, 1,
nullptr),
564 auto view_deleter = [](
void *pMem) { ::UnmapViewOfFile(pMem); };
565 std::unique_ptr<void,
decltype(view_deleter)> pMem(
566 ::MapViewOfFile(filemap.
get(), FILE_MAP_READ, 0, 0, 1), view_deleter);
570 std::array<wchar_t, MAX_PATH + 1> mapped_filename;
571 if (!::GetMappedFileNameW(::GetCurrentProcess(), pMem.get(),
572 mapped_filename.data(), mapped_filename.size()))
580 std::array<wchar_t, MAX_PATH + 1> module_filename;
582 ::GetModuleFileNameExW(process,
reinterpret_cast<HMODULE
>(base_addr),
583 module_filename.data(), module_filename.size());
584 if (len > 0 && len < module_filename.size()) {
585 std::string path_utf8;
586 llvm::convertWideToUTF8(std::wstring(module_filename.data(), len),
592 std::vector<wchar_t> mapped_filename(MAX_PATH + 1);
593 DWORD mapped_len = 0;
594 while (mapped_filename.size() <= PATHCCH_MAX_CCH) {
595 mapped_len = ::GetMappedFileNameW(
596 process, base_addr, mapped_filename.data(), mapped_filename.size());
597 if (mapped_len < mapped_filename.size())
599 if (::GetLastError() != ERROR_INSUFFICIENT_BUFFER)
601 mapped_filename.resize(mapped_filename.size() * 2);
604 llvm::ArrayRef<wchar_t>(mapped_filename.data(), mapped_len + 1));
612 MEMORY_BASIC_INFORMATION mbi{};
613 if (!::VirtualQueryEx(process, addr, &mbi,
sizeof(mbi)))
615 if (mbi.State != MEM_COMMIT)
617 uintptr_t region_end =
618 reinterpret_cast<uintptr_t
>(mbi.BaseAddress) + mbi.RegionSize;
619 uintptr_t a =
reinterpret_cast<uintptr_t
>(addr);
620 assert(a < region_end);
621 return region_end - a;
626 SIZE_T to_read = std::min<SIZE_T>((MAX_PATH + 1) *
sizeof(
wchar_t),
628 to_read &= ~SIZE_T(1);
629 if (to_read <
sizeof(
wchar_t))
632 std::array<wchar_t, MAX_PATH + 1> buf{};
633 SIZE_T bytes_read = 0;
637 size_t max_chars = bytes_read /
sizeof(wchar_t);
638 size_t len = ::wcsnlen(buf.data(), max_chars);
639 if (len == max_chars)
645 llvm::convertWideToUTF8(std::wstring(buf.data(), len), result);
656 std::array<char, MAX_PATH + 1> buf{};
657 SIZE_T bytes_read = 0;
661 size_t len = ::strnlen(buf.data(), bytes_read);
662 if (len == bytes_read)
667 return std::string(buf.data(), len);
671static std::optional<std::string>
673 if (info.lpImageName ==
nullptr)
676 LPVOID string_addr =
nullptr;
677 SIZE_T bytes_read = 0;
679 sizeof(string_addr), &bytes_read) ||
680 bytes_read !=
sizeof(string_addr))
693 auto on_load_dll = [&](llvm::StringRef path) {
698 LLDB_LOG(log,
"Inferior {0} - DLL '{1}' loaded at address {2:x}...",
699 m_process.GetProcessId(), path, info.lpBaseOfDll);
704 std::optional<std::string> resolved_path;
705 if (info.hFile !=
nullptr) {
706 std::vector<wchar_t> buffer(1);
707 DWORD required_size =
708 GetFinalPathNameByHandleW(info.hFile, &buffer[0], 0, VOLUME_NAME_DOS);
709 if (required_size > 0) {
710 buffer.resize(required_size + 1);
711 GetFinalPathNameByHandleW(info.hFile, &buffer[0], required_size,
713 std::string path_str_utf8;
714 llvm::convertWideToUTF8(buffer.data(), path_str_utf8);
715 llvm::StringRef path_str = path_str_utf8;
716 path_str.consume_front(
"\\\\?\\");
717 resolved_path = path_str.str();
730 on_load_dll(*resolved_path);
733 "Inferior {0} - could not resolve path for LOAD_DLL_DEBUG_EVENT "
734 "(hFile={1}, base={2:x}, last error={3})",
735 m_process.GetProcessId(), info.hFile, info.lpBaseOfDll,
739 if (info.hFile !=
nullptr)
740 ::CloseHandle(info.hFile);
748 LLDB_LOG(log,
"process {0} unloading DLL at addr {1:x}.",
749 m_process.GetProcessId(), info.lpBaseOfDll);
761 reinterpret_cast<uintptr_t
>(info.lpDebugStringData)),
762 info.fUnicode == TRUE, info.nDebugStringLength);
769 LLDB_LOG(log,
"encountered error {0} (type={1}) in process {2} thread {3}",
770 info.dwError, info.dwType,
m_process.GetProcessId(), thread_id);
static llvm::raw_ostream & error(Stream &strm)
static SIZE_T BytesReadableAt(HANDLE process, LPCVOID addr)
static WaitForDebugEventFn * g_wait_for_debug_event
static std::optional< std::string > GetFileNameFromImageNameField(HANDLE process, const LOAD_DLL_DEBUG_INFO &info)
static std::optional< std::string > GetFileNameByLoadAddress(HANDLE process, LPVOID base_addr)
static std::optional< std::string > GetFileNameFromHandleFallback(HANDLE hFile)
static std::optional< std::string > ConvertNtDevicePathToDosPath(llvm::ArrayRef< wchar_t > nt_path)
#define STATUS_WX86_BREAKPOINT
BOOL WINAPI WaitForDebugEventFn(LPDEBUG_EVENT, DWORD)
static std::optional< std::string > ReadRemotePathStringW(HANDLE process, LPCVOID addr)
static void InitializeWaitForDebugEvent()
WaitForDebugEventEx is only available on Windows 10+.
static std::optional< std::string > ReadRemotePathStringA(HANDLE process, LPCVOID addr)
static int ReadProcessMemory(uint8_t *buffer, size_t size, const pt_asid *, uint64_t pc, void *context)
Callback used by libipt for reading the process memory.
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
#define LLDB_LOG_VERBOSE(log,...)
std::atomic< bool > m_is_shutting_down
std::atomic< DWORD > m_pid_to_detach
DWORD HandleExitThreadEvent(const EXIT_THREAD_DEBUG_INFO &info, DWORD thread_id)
DWORD HandleODSEvent(const OUTPUT_DEBUG_STRING_INFO &info, DWORD thread_id)
DWORD HandleLoadDllEvent(const LOAD_DLL_DEBUG_INFO &info, DWORD thread_id)
DWORD HandleExitProcessEvent(const EXIT_PROCESS_DEBUG_INFO &info, DWORD thread_id)
Status StopDebugging(bool terminate)
DWORD HandleRipEvent(const RIP_INFO &info, DWORD thread_id)
void ContinueAsyncException(ExceptionResult result)
HostProcess GetProcess() const
lldb::thread_result_t DebuggerThreadAttachRoutine(lldb::pid_t pid, const ProcessAttachInfo &launch_info)
DebuggerThread(DebugDelegateSP debug_delegate)
void FreeProcessHandles()
virtual ~DebuggerThread()
DWORD HandleCreateProcessEvent(const CREATE_PROCESS_DEBUG_INFO &info, DWORD thread_id)
ExceptionResult HandleExceptionEvent(const EXCEPTION_DEBUG_INFO &info, DWORD thread_id, bool shutting_down)
Status DebugAttach(lldb::pid_t pid, const ProcessAttachInfo &attach_info)
lldb::thread_result_t DebuggerThreadLaunchRoutine(const ProcessLaunchInfo &launch_info)
ExceptionRecordSP m_active_exception
Predicate< ExceptionResult > m_exception_pred
HANDLE m_debugging_ended_event
Status DebugLaunch(const ProcessLaunchInfo &launch_info)
DWORD HandleUnloadDllEvent(const UNLOAD_DLL_DEBUG_INFO &info, DWORD thread_id)
DebugDelegateSP m_debug_delegate
DWORD HandleCreateThreadEvent(const CREATE_THREAD_DEBUG_INFO &info, DWORD thread_id)
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
lldb::pid_t GetProcessId() const
FileSpec & GetExecutableFile()
HostProcess LaunchProcess(const ProcessLaunchInfo &launch_info, Status &error) override
static Status FromError(llvm::Error error)
Avoid using this in new code. Migrate APIs to llvm::Expected instead.
static llvm::Expected< HostThread > LaunchThread(llvm::StringRef name, std::function< lldb::thread_result_t()> thread_function, size_t min_stack_byte_size=0)
#define LLDB_INVALID_PROCESS
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
std::shared_ptr< IDebugDelegate > DebugDelegateSP
@ eBroadcastNever
No broadcast will be sent when the value is modified.
@ eBroadcastAlways
Always send a broadcast when the value is modified.
@ eErrorTypeWin32
Standard Win32 error codes.