LLDB mainline
DebuggerThread.cpp
Go to the documentation of this file.
1//===-- DebuggerThread.cpp ------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "DebuggerThread.h"
10#include "ExceptionRecord.h"
11#include "IDebugDelegate.h"
12
21#include "lldb/Target/Process.h"
23#include "lldb/Utility/Log.h"
25#include "lldb/Utility/Status.h"
26
28
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"
34
35#include <optional>
36#include <pathcch.h>
37#include <psapi.h>
38
39#ifndef STATUS_WX86_BREAKPOINT
40#define STATUS_WX86_BREAKPOINT 0x4000001FL // For WOW64
41#endif
42
43using namespace lldb;
44using namespace lldb_private;
45
46typedef BOOL WINAPI WaitForDebugEventFn(LPDEBUG_EVENT, DWORD);
48
49/// WaitForDebugEventEx is only available on Windows 10+. This lazily checks if
50/// the function is available and falls back to WaitForDebugEvent if
51/// unavailable. The -Ex version ensures correct forwarding of
52/// OutputDebugStringW events.
54 static LazyImport<WaitForDebugEventFn *> s_wait_for_debug_event_ex = {
55 L"kernel32.dll", "WaitForDebugEventEx"};
56
58 return;
59
60 if (!s_wait_for_debug_event_ex) {
63 "WaitForDebugEventEx unavailable, using WaitForDebugEvent instead. "
64 "Unicode strings from OutputDebugStringW might show incorrectly.");
65 g_wait_for_debug_event = &WaitForDebugEvent;
66 } else {
67 g_wait_for_debug_event = *s_wait_for_debug_event_ex;
68 }
69}
70
72 : m_debug_delegate(debug_delegate), m_pid_to_detach(0),
73 m_is_shutting_down(false) {
75 m_debugging_ended_event = ::CreateEvent(nullptr, TRUE, FALSE, nullptr);
76}
77
79
82 LLDB_LOG(log, "launching '{0}'", launch_info.GetExecutableFile().GetPath());
83
84 Status result;
85 llvm::Expected<HostThread> secondary_thread = ThreadLauncher::LaunchThread(
86 "lldb.plugin.process-windows.secondary[?]",
87 [this, launch_info] { return DebuggerThreadLaunchRoutine(launch_info); });
88 if (!secondary_thread) {
89 result = Status::FromError(secondary_thread.takeError());
90 LLDB_LOG(log, "couldn't launch debugger thread. {0}", result);
91 }
92
93 return result;
94}
95
97 const ProcessAttachInfo &attach_info) {
99 LLDB_LOG(log, "attaching to '{0}'", pid);
100
101 Status result;
102 llvm::Expected<HostThread> secondary_thread = ThreadLauncher::LaunchThread(
103 "lldb.plugin.process-windows.secondary[?]", [this, pid, attach_info] {
104 return DebuggerThreadAttachRoutine(pid, attach_info);
105 });
106 if (!secondary_thread) {
107 result = Status::FromError(secondary_thread.takeError());
108 LLDB_LOG(log, "couldn't attach to process '{0}'. {1}", pid, result);
109 }
110
111 return result;
112}
113
115 const ProcessLaunchInfo &launch_info) {
116 // Grab a shared_ptr reference to this so that we know it won't get deleted
117 // until after the thread routine has exited.
118 std::shared_ptr<DebuggerThread> this_ref(shared_from_this());
119
121 LLDB_LOG(log, "preparing to launch '{0}' on background thread.",
122 launch_info.GetExecutableFile().GetPath());
123
125 ProcessLauncherWindows launcher;
126 HostProcess process(launcher.LaunchProcess(launch_info, error));
127 // If we couldn't create the process, notify waiters immediately. Otherwise
128 // enter the debug loop and wait until we get the create process debug
129 // notification. Note that if the process was created successfully, we can
130 // throw away the process handle we got from CreateProcess because Windows
131 // will give us another (potentially more useful?) handle when it sends us
132 // the CREATE_PROCESS_DEBUG_EVENT.
133 if (error.Success())
134 DebugLoop();
135 else
136 m_debug_delegate->OnDebuggerError(error, 0);
137
138 return {};
139}
140
142 lldb::pid_t pid, const ProcessAttachInfo &attach_info) {
143 // Grab a shared_ptr reference to this so that we know it won't get deleted
144 // until after the thread routine has exited.
145 std::shared_ptr<DebuggerThread> this_ref(shared_from_this());
146
148 LLDB_LOG(log, "preparing to attach to process '{0}' on background thread.",
149 pid);
150
151 if (!DebugActiveProcess(static_cast<DWORD>(pid))) {
152 Status error(::GetLastError(), eErrorTypeWin32);
153 m_debug_delegate->OnDebuggerError(error, 0);
154 return {};
155 }
156
157 // The attach was successful, enter the debug loop. From here on out, this
158 // is no different than a create process operation, so all the same comments
159 // in DebugLaunch should apply from this point out.
160 DebugLoop();
161
162 return {};
163}
164
167
168 lldb::pid_t pid = m_process.GetProcessId();
169
171 LLDB_LOG(log, "terminate = {0}, inferior={1}.", terminate, pid);
172
173 // Set m_is_shutting_down to true if it was false. Return if it was already
174 // true.
175 bool expected = false;
176 if (!m_is_shutting_down.compare_exchange_strong(expected, true))
177 return error;
178
179 // Make a copy of the process, since the termination sequence will reset
180 // DebuggerThread's internal copy and it needs to remain open for the Wait
181 // operation.
182 HostProcess process_copy = m_process;
183 lldb::process_t handle = m_process.GetNativeProcess().GetSystemHandle();
184
185 if (terminate) {
186 if (handle != nullptr && handle != LLDB_INVALID_PROCESS) {
187 // Initiate the termination before continuing the exception, so that the
188 // next debug event we get is the exit process event, and not some other
189 // event.
190 BOOL terminate_suceeded = TerminateProcess(handle, 0);
191 LLDB_LOG(log,
192 "calling TerminateProcess({0}, 0) (inferior={1}), success={2}",
193 handle, pid, terminate_suceeded);
194 } else {
195 LLDB_LOG(log,
196 "NOT calling TerminateProcess because the inferior is not valid "
197 "({0}, 0) (inferior={1})",
198 handle, pid);
199 }
200 }
201
202 // If we're stuck waiting for an exception to continue (e.g. the user is at a
203 // breakpoint messing around in the debugger), continue it now. But only
204 // AFTER calling TerminateProcess to make sure that the very next call to
205 // WaitForDebugEventEx is an exit process event.
206 if (m_active_exception.get()) {
207 LLDB_LOG(log, "masking active exception");
209 }
210
211 if (!terminate) {
212 // Indicate that we want to detach.
214
215 // Force a fresh break so that the detach can happen from the debugger
216 // thread.
217 if (!::DebugBreakProcess(
218 GetProcess().GetNativeProcess().GetSystemHandle())) {
219 error = Status(::GetLastError(), eErrorTypeWin32);
220 }
221 }
222
223 LLDB_LOG(log, "waiting for detach from process {0} to complete.", pid);
224
225 DWORD wait_result = WaitForSingleObject(m_debugging_ended_event, 5000);
226 if (wait_result != WAIT_OBJECT_0) {
227 error = Status(GetLastError(), eErrorTypeWin32);
228 LLDB_LOG(log, "error: WaitForSingleObject({0}, 5000) returned {1}",
229 m_debugging_ended_event, wait_result);
230 } else
231 LLDB_LOG(log, "detach from process {0} completed successfully.", pid);
232
233 if (!error.Success()) {
234 LLDB_LOG(log, "encountered an error while trying to stop process {0}. {1}",
235 pid, error);
236 }
237 return error;
238}
239
241 if (!m_active_exception.get())
242 return;
243
245 LLDB_LOG(log, "broadcasting for inferior process {0}.",
246 m_process.GetProcessId());
247
248 m_active_exception.reset();
249 m_exception_pred.SetValue(result, eBroadcastAlways);
250}
251
255 if (m_image_file) {
256 ::CloseHandle(m_image_file);
257 m_image_file = nullptr;
258 }
259}
260
263 DEBUG_EVENT dbe = {};
264 bool should_debug = true;
265 LLDB_LOG_VERBOSE(log, "Entering WaitForDebugEventEx loop");
266 while (should_debug) {
267 LLDB_LOG_VERBOSE(log, "Calling WaitForDebugEvent");
268 BOOL wait_result = g_wait_for_debug_event(&dbe, INFINITE);
269 if (wait_result) {
270 DWORD continue_status = DBG_CONTINUE;
271 bool shutting_down = m_is_shutting_down;
272 switch (dbe.dwDebugEventCode) {
273 default:
274 llvm_unreachable("Unhandle debug event code!");
275 case EXCEPTION_DEBUG_EVENT: {
277 dbe.u.Exception, dbe.dwThreadId, shutting_down);
278
279 if (status == ExceptionResult::MaskException)
280 continue_status = DBG_CONTINUE;
281 else if (status == ExceptionResult::SendToApplication)
282 continue_status = DBG_EXCEPTION_NOT_HANDLED;
283
284 break;
285 }
286 case CREATE_THREAD_DEBUG_EVENT:
287 continue_status =
288 HandleCreateThreadEvent(dbe.u.CreateThread, dbe.dwThreadId);
289 break;
290 case CREATE_PROCESS_DEBUG_EVENT:
291 continue_status =
292 HandleCreateProcessEvent(dbe.u.CreateProcessInfo, dbe.dwThreadId);
293 break;
294 case EXIT_THREAD_DEBUG_EVENT:
295 continue_status =
296 HandleExitThreadEvent(dbe.u.ExitThread, dbe.dwThreadId);
297 break;
298 case EXIT_PROCESS_DEBUG_EVENT:
299 continue_status =
300 HandleExitProcessEvent(dbe.u.ExitProcess, dbe.dwThreadId);
301 should_debug = false;
302 break;
303 case LOAD_DLL_DEBUG_EVENT:
304 continue_status = HandleLoadDllEvent(dbe.u.LoadDll, dbe.dwThreadId);
305 break;
306 case UNLOAD_DLL_DEBUG_EVENT:
307 continue_status = HandleUnloadDllEvent(dbe.u.UnloadDll, dbe.dwThreadId);
308 break;
309 case OUTPUT_DEBUG_STRING_EVENT:
310 continue_status = HandleODSEvent(dbe.u.DebugString, dbe.dwThreadId);
311 break;
312 case RIP_EVENT:
313 continue_status = HandleRipEvent(dbe.u.RipInfo, dbe.dwThreadId);
314 if (dbe.u.RipInfo.dwType == SLE_ERROR)
315 should_debug = false;
316 break;
317 }
318
320 log, "calling ContinueDebugEvent({0}, {1}, {2}) on thread {3}.",
321 dbe.dwProcessId, dbe.dwThreadId, continue_status,
322 ::GetCurrentThreadId());
323
324 ::ContinueDebugEvent(dbe.dwProcessId, dbe.dwThreadId, continue_status);
325
326 // We have to DebugActiveProcessStop after ContinueDebugEvent, otherwise
327 // the target process will crash
328 if (shutting_down) {
329 // A breakpoint that occurs while `m_pid_to_detach` is non-zero is a
330 // magic exception that we use simply to wake up the DebuggerThread so
331 // that we can close out the debug loop.
332 if (m_pid_to_detach != 0 &&
333 (dbe.u.Exception.ExceptionRecord.ExceptionCode ==
334 EXCEPTION_BREAKPOINT ||
335 dbe.u.Exception.ExceptionRecord.ExceptionCode ==
337 LLDB_LOG(log,
338 "Breakpoint exception is cue to detach from process {0:x}",
339 m_pid_to_detach.load());
340
341 // detaching with leaving breakpoint exception event on the queue may
342 // cause target process to crash so process events as possible since
343 // target threads are running at this time, there is possibility to
344 // have some breakpoint exception between last WaitForDebugEventEx and
345 // DebugActiveProcessStop but ignore for now.
346 while (g_wait_for_debug_event(&dbe, 0)) {
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,
357 continue_status);
358 }
359
360 ::DebugActiveProcessStop(m_pid_to_detach);
361 m_detached = true;
362 }
363 }
364
365 if (m_detached) {
366 should_debug = false;
367 }
368 } else {
369 LLDB_LOG(log, "returned FALSE from WaitForDebugEventEx. Error = {0}",
370 ::GetLastError());
371
372 should_debug = false;
373 }
374 }
376
377 LLDB_LOG(log, "WaitForDebugEventEx loop completed, exiting.");
378 ::SetEvent(m_debugging_ended_event);
379}
380
382DebuggerThread::HandleExceptionEvent(const EXCEPTION_DEBUG_INFO &info,
383 DWORD thread_id, bool shutting_down) {
385 if (shutting_down) {
386 bool is_breakpoint =
387 (info.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT ||
388 info.ExceptionRecord.ExceptionCode == STATUS_WX86_BREAKPOINT);
389
390 // Don't perform any blocking operations while we're shutting down. That
391 // will cause TerminateProcess -> WaitForSingleObject to time out.
392 // We should not send breakpoint exceptions to the application.
393 return is_breakpoint ? ExceptionResult::MaskException
395 }
396
397 bool first_chance = (info.dwFirstChance != 0);
398
399 m_active_exception.reset(
400 new ExceptionRecord(info.ExceptionRecord, thread_id));
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);
404
405 ExceptionResult result =
406 m_debug_delegate->OnDebugException(first_chance, *m_active_exception);
407 m_exception_pred.SetValue(result, eBroadcastNever);
408
409 LLDB_LOG(log, "waiting for ExceptionPred != BreakInDebugger");
410 result = *m_exception_pred.WaitForValueNotEqualTo(
412
413 LLDB_LOG(log, "got ExceptionPred = {0}", (int)m_exception_pred.GetValue());
414 return result;
415}
416
417DWORD
418DebuggerThread::HandleCreateThreadEvent(const CREATE_THREAD_DEBUG_INFO &info,
419 DWORD thread_id) {
421 LLDB_LOG(log, "Thread {0} spawned in process {1}", thread_id,
422 m_process.GetProcessId());
423 HostThread thread(info.hThread);
424 thread.GetNativeThread().SetOwnsHandle(false);
425 m_debug_delegate->OnCreateThread(thread);
426 return DBG_CONTINUE;
427}
428
429DWORD
430DebuggerThread::HandleCreateProcessEvent(const CREATE_PROCESS_DEBUG_INFO &info,
431 DWORD thread_id) {
433 uint32_t process_id = ::GetProcessId(info.hProcess);
434
435 LLDB_LOG(log, "process {0} spawned", process_id);
436
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);
441
442 // info.hProcess and info.hThread are closed automatically by Windows when
443 // EXIT_PROCESS_DEBUG_EVENT is received.
444 m_process = HostProcess(info.hProcess);
445 ((HostProcessWindows &)m_process.GetNativeProcess()).SetOwnsHandle(false);
446 m_main_thread = HostThread(info.hThread);
447 m_main_thread.GetNativeThread().SetOwnsHandle(false);
448 m_image_file = info.hFile;
449
450 lldb::addr_t load_addr = reinterpret_cast<lldb::addr_t>(info.lpBaseOfImage);
451 m_debug_delegate->OnDebuggerConnected(load_addr);
452
453 return DBG_CONTINUE;
454}
455
456DWORD
457DebuggerThread::HandleExitThreadEvent(const EXIT_THREAD_DEBUG_INFO &info,
458 DWORD thread_id) {
460 LLDB_LOG(log, "Thread {0} exited with code {1} in process {2}", thread_id,
461 info.dwExitCode, m_process.GetProcessId());
462 m_debug_delegate->OnExitThread(thread_id, info.dwExitCode);
463 return DBG_CONTINUE;
464}
465
466DWORD
467DebuggerThread::HandleExitProcessEvent(const EXIT_PROCESS_DEBUG_INFO &info,
468 DWORD thread_id) {
470 LLDB_LOG(log, "process {0} exited with code {1}", m_process.GetProcessId(),
471 info.dwExitCode);
472
473 m_debug_delegate->OnExitProcess(info.dwExitCode);
474
475 return DBG_CONTINUE;
476}
477
478static std::optional<std::string>
479ConvertNtDevicePathToDosPath(llvm::ArrayRef<wchar_t> nt_path) {
481
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) {
485 LLDB_LOG(log,
486 "ConvertNtDevicePathToDosPath: FindFirstVolumeW failed, "
487 "error={0}",
488 ::GetLastError());
489 return std::nullopt;
490 }
491 llvm::scope_exit close_iter([&] { ::FindVolumeClose(vol_iter); });
492
493 do {
494 // FindFirstVolumeW yields "\\?\Volume{GUID}\".
495 // QueryDosDeviceW expects "Volume{GUID}".
496 size_t vol_len = ::wcsnlen(vol_name.data(), vol_name.size());
497 if (vol_len < 5 || vol_name[vol_len - 1] != L'\\')
498 continue;
499
500 vol_name[vol_len - 1] = L'\0'; // strip trailing '\' for QueryDosDeviceW
501 llvm::SmallVector<wchar_t, MAX_PATH> dev_name(MAX_PATH);
502 bool ok = ::QueryDosDeviceW(vol_name.data() + 4, // skip "\\?\"
503 dev_name.data(), dev_name.size());
504 vol_name[vol_len - 1] = L'\\'; // restore
505 if (!ok)
506 continue;
507
508 // Check that nt_path begins with this device name followed by '\'.
509 size_t dev_len = ::wcsnlen(dev_name.data(), dev_name.size());
510 if (dev_len == 0 || dev_len >= nt_path.size())
511 continue;
512 if (_wcsnicmp(nt_path.data(), dev_name.data(), dev_len) != 0)
513 continue;
514 if (nt_path[dev_len] != L'\\')
515 continue;
516
517 // Prefer a drive-letter/mount-point over the raw volume GUID path.
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,
522 &names_size);
523 if (names_size > 1) {
524 mount_names.resize(names_size);
525 DWORD written = 0;
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>(
530 mount_names.data(),
531 ::wcsnlen(mount_names.data(), mount_names.size()));
532 }
533 }
534
535 // Build the final path: mount point + rest of nt_path.
536 llvm::SmallVector<wchar_t> dos_wide(mount.begin(), mount.end());
537 if (!dos_wide.empty() && dos_wide.back() == L'\\')
538 dos_wide.pop_back();
539 dos_wide.append(nt_path.begin() + dev_len, nt_path.end());
540
541 std::string result;
542 llvm::convertWideToUTF8(std::wstring(dos_wide.begin(), dos_wide.end()),
543 result);
544 return result;
545 } while (::FindNextVolumeW(vol_iter, vol_name.data(), vol_name.size()));
546
547 LLDB_LOG(log, "ConvertNtDevicePathToDosPath: no matching volume found");
548 return std::nullopt;
549}
550
551static std::optional<std::string> GetFileNameFromHandleFallback(HANDLE hFile) {
552 // Check that file is not empty as we cannot map a file with zero length.
553 DWORD dwFileSizeHi = 0;
554 DWORD dwFileSizeLo = ::GetFileSize(hFile, &dwFileSizeHi);
555 if (dwFileSizeLo == 0 && dwFileSizeHi == 0)
556 return std::nullopt;
557
558 AutoHandle filemap(
559 ::CreateFileMappingW(hFile, nullptr, PAGE_READONLY, 0, 1, nullptr),
560 nullptr);
561 if (!filemap.IsValid())
562 return std::nullopt;
563
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);
567 if (!pMem)
568 return std::nullopt;
569
570 std::array<wchar_t, MAX_PATH + 1> mapped_filename;
571 if (!::GetMappedFileNameW(::GetCurrentProcess(), pMem.get(),
572 mapped_filename.data(), mapped_filename.size()))
573 return std::nullopt;
574
575 return ConvertNtDevicePathToDosPath(mapped_filename);
576}
577
578static std::optional<std::string> GetFileNameByLoadAddress(HANDLE process,
579 LPVOID base_addr) {
580 std::array<wchar_t, MAX_PATH + 1> module_filename;
581 DWORD len =
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),
587 path_utf8);
588 return path_utf8;
589 }
590
591 // Fallback: ask the kernel for the file backing the mapping at this address.
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())
598 break;
599 if (::GetLastError() != ERROR_INSUFFICIENT_BUFFER)
600 return std::nullopt;
601 mapped_filename.resize(mapped_filename.size() * 2);
602 }
603 std::optional<std::string> dos_path = ConvertNtDevicePathToDosPath(
604 llvm::ArrayRef<wchar_t>(mapped_filename.data(), mapped_len + 1));
605 return dos_path;
606}
607
608// Determine how many bytes can be read at `addr` in `process` before crossing
609// out of the committed memory region containing it. Returns 0 if the address is
610// not within a committed region.
611static SIZE_T BytesReadableAt(HANDLE process, LPCVOID addr) {
612 MEMORY_BASIC_INFORMATION mbi{};
613 if (!::VirtualQueryEx(process, addr, &mbi, sizeof(mbi)))
614 return 0;
615 if (mbi.State != MEM_COMMIT)
616 return 0;
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;
622}
623
624static std::optional<std::string> ReadRemotePathStringW(HANDLE process,
625 LPCVOID addr) {
626 SIZE_T to_read = std::min<SIZE_T>((MAX_PATH + 1) * sizeof(wchar_t),
627 BytesReadableAt(process, addr));
628 to_read &= ~SIZE_T(1); // round down to a wchar_t boundary
629 if (to_read < sizeof(wchar_t))
630 return std::nullopt;
631
632 std::array<wchar_t, MAX_PATH + 1> buf{};
633 SIZE_T bytes_read = 0;
634 if (!::ReadProcessMemory(process, addr, buf.data(), to_read, &bytes_read))
635 return std::nullopt;
636
637 size_t max_chars = bytes_read / sizeof(wchar_t);
638 size_t len = ::wcsnlen(buf.data(), max_chars);
639 if (len == max_chars) // no null terminator found
640 return std::nullopt;
641 if (len == 0) // empty string
642 return std::nullopt;
643
644 std::string result;
645 llvm::convertWideToUTF8(std::wstring(buf.data(), len), result);
646 return result;
647}
648
649static std::optional<std::string> ReadRemotePathStringA(HANDLE process,
650 LPCVOID addr) {
651 SIZE_T to_read =
652 std::min<SIZE_T>(MAX_PATH + 1, BytesReadableAt(process, addr));
653 if (to_read == 0)
654 return std::nullopt;
655
656 std::array<char, MAX_PATH + 1> buf{};
657 SIZE_T bytes_read = 0;
658 if (!::ReadProcessMemory(process, addr, buf.data(), to_read, &bytes_read))
659 return std::nullopt;
660
661 size_t len = ::strnlen(buf.data(), bytes_read);
662 if (len == bytes_read) // no null terminator found
663 return std::nullopt;
664 if (len == 0) // empty string
665 return std::nullopt;
666
667 return std::string(buf.data(), len);
668}
669
670// Resolve the LOAD_DLL_DEBUG_INFO::lpImageName field.
671static std::optional<std::string>
672GetFileNameFromImageNameField(HANDLE process, const LOAD_DLL_DEBUG_INFO &info) {
673 if (info.lpImageName == nullptr)
674 return std::nullopt;
675
676 LPVOID string_addr = nullptr;
677 SIZE_T bytes_read = 0;
678 if (!::ReadProcessMemory(process, info.lpImageName, &string_addr,
679 sizeof(string_addr), &bytes_read) ||
680 bytes_read != sizeof(string_addr))
681 return std::nullopt;
682
683 if (info.fUnicode)
684 return ReadRemotePathStringW(process, string_addr);
685 return ReadRemotePathStringA(process, string_addr);
686}
687
688DWORD
689DebuggerThread::HandleLoadDllEvent(const LOAD_DLL_DEBUG_INFO &info,
690 DWORD thread_id) {
692
693 auto on_load_dll = [&](llvm::StringRef path) {
694 FileSpec file_spec(path);
695 ModuleSpec module_spec(file_spec);
696 lldb::addr_t load_addr = reinterpret_cast<lldb::addr_t>(info.lpBaseOfDll);
697
698 LLDB_LOG(log, "Inferior {0} - DLL '{1}' loaded at address {2:x}...",
699 m_process.GetProcessId(), path, info.lpBaseOfDll);
700
701 m_debug_delegate->OnLoadDll(module_spec, load_addr);
702 };
703
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,
712 VOLUME_NAME_DOS);
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();
718 } else {
719 resolved_path = GetFileNameFromHandleFallback(info.hFile);
720 }
721 }
722
723 HANDLE process = m_process.GetNativeProcess().GetSystemHandle();
724 if (!resolved_path)
725 resolved_path = GetFileNameFromImageNameField(process, info);
726 if (!resolved_path)
727 resolved_path = GetFileNameByLoadAddress(process, info.lpBaseOfDll);
728
729 if (resolved_path)
730 on_load_dll(*resolved_path);
731 else
732 LLDB_LOG(log,
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,
736 ::GetLastError());
737
738 // Windows does not automatically close info.hFile, so we need to do it.
739 if (info.hFile != nullptr)
740 ::CloseHandle(info.hFile);
741 return DBG_CONTINUE;
742}
743
744DWORD
745DebuggerThread::HandleUnloadDllEvent(const UNLOAD_DLL_DEBUG_INFO &info,
746 DWORD thread_id) {
748 LLDB_LOG(log, "process {0} unloading DLL at addr {1:x}.",
749 m_process.GetProcessId(), info.lpBaseOfDll);
750
751 m_debug_delegate->OnUnloadDll(
752 reinterpret_cast<lldb::addr_t>(info.lpBaseOfDll));
753 return DBG_CONTINUE;
754}
755
756DWORD
757DebuggerThread::HandleODSEvent(const OUTPUT_DEBUG_STRING_INFO &info,
758 DWORD thread_id) {
759 m_debug_delegate->OnDebugString(
760 static_cast<lldb::addr_t>(
761 reinterpret_cast<uintptr_t>(info.lpDebugStringData)),
762 info.fUnicode == TRUE, info.nDebugStringLength);
763 return DBG_CONTINUE;
764}
765
766DWORD
767DebuggerThread::HandleRipEvent(const RIP_INFO &info, DWORD thread_id) {
769 LLDB_LOG(log, "encountered error {0} (type={1}) in process {2} thread {3}",
770 info.dwError, info.dwType, m_process.GetProcessId(), thread_id);
771
772 Status error(info.dwError, eErrorTypeWin32);
773 m_debug_delegate->OnDebuggerError(error, info.dwType);
774
775 return DBG_CONTINUE;
776}
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.
Definition Log.h:364
#define LLDB_LOG_VERBOSE(log,...)
Definition Log.h:371
void * HANDLE
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)
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
Status DebugLaunch(const ProcessLaunchInfo &launch_info)
DWORD HandleUnloadDllEvent(const UNLOAD_DLL_DEBUG_INFO &info, DWORD thread_id)
DWORD HandleCreateThreadEvent(const CREATE_THREAD_DEBUG_INFO &info, DWORD thread_id)
A file utility class.
Definition FileSpec.h:57
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition FileSpec.cpp:374
lldb::pid_t GetProcessId() const
FileSpec & GetExecutableFile()
Definition ProcessInfo.h:43
HostProcess LaunchProcess(const ProcessLaunchInfo &launch_info, Status &error) override
An error handling class.
Definition Status.h:118
static Status FromError(llvm::Error error)
Avoid using this in new code. Migrate APIs to llvm::Expected instead.
Definition Status.cpp:136
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
Definition lldb-types.h:68
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.
Definition Log.h:327
std::shared_ptr< IDebugDelegate > DebugDelegateSP
Definition ForwardDecl.h:35
@ eBroadcastNever
No broadcast will be sent when the value is modified.
Definition Predicate.h:28
@ eBroadcastAlways
Always send a broadcast when the value is modified.
Definition Predicate.h:29
void * thread_result_t
Definition lldb-types.h:62
@ eErrorTypeWin32
Standard Win32 error codes.
uint64_t pid_t
Definition lldb-types.h:83
uint64_t addr_t
Definition lldb-types.h:80
uint64_t process_t
Definition lldb-types.h:57