1 //===-- ProcessWindows.cpp --------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "ProcessWindows.h"
11 
12 // Windows includes
13 #include "lldb/Host/windows/windows.h"
14 #include <psapi.h>
15 
16 // Other libraries and framework includes
17 #include "lldb/Core/Module.h"
18 #include "lldb/Core/ModuleSpec.h"
19 #include "lldb/Core/PluginManager.h"
20 #include "lldb/Core/Section.h"
21 #include "lldb/Core/State.h"
22 #include "lldb/Host/HostNativeProcessBase.h"
23 #include "lldb/Host/HostProcess.h"
24 #include "lldb/Host/windows/HostThreadWindows.h"
25 #include "lldb/Host/windows/windows.h"
26 #include "lldb/Symbol/ObjectFile.h"
27 #include "lldb/Target/DynamicLoader.h"
28 #include "lldb/Target/MemoryRegionInfo.h"
29 #include "lldb/Target/StopInfo.h"
30 #include "lldb/Target/Target.h"
31 
32 #include "llvm/Support/ConvertUTF.h"
33 #include "llvm/Support/Format.h"
34 #include "llvm/Support/Threading.h"
35 #include "llvm/Support/raw_ostream.h"
36 
37 #include "DebuggerThread.h"
38 #include "ExceptionRecord.h"
39 #include "ForwardDecl.h"
40 #include "LocalDebugDelegate.h"
41 #include "ProcessWindowsLog.h"
42 #include "TargetThreadWindows.h"
43 
44 using namespace lldb;
45 using namespace lldb_private;
46 
47 namespace {
48 std::string GetProcessExecutableName(HANDLE process_handle) {
49   std::vector<wchar_t> file_name;
50   DWORD file_name_size = MAX_PATH; // first guess, not an absolute limit
51   DWORD copied = 0;
52   do {
53     file_name_size *= 2;
54     file_name.resize(file_name_size);
55     copied = ::GetModuleFileNameExW(process_handle, NULL, file_name.data(),
56                                     file_name_size);
57   } while (copied >= file_name_size);
58   file_name.resize(copied);
59   std::string result;
60   llvm::convertWideToUTF8(file_name.data(), result);
61   return result;
62 }
63 
64 std::string GetProcessExecutableName(DWORD pid) {
65   std::string file_name;
66   HANDLE process_handle =
67       ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
68   if (process_handle != NULL) {
69     file_name = GetProcessExecutableName(process_handle);
70     ::CloseHandle(process_handle);
71   }
72   return file_name;
73 }
74 
75 } // anonymous namespace
76 
77 namespace lldb_private {
78 
79 // We store a pointer to this class in the ProcessWindows, so that we don't
80 // expose Windows-specific types and implementation details from a public header
81 // file.
82 class ProcessWindowsData {
83 public:
84   ProcessWindowsData(bool stop_at_entry) : m_stop_at_entry(stop_at_entry) {
85     m_initial_stop_event = ::CreateEvent(nullptr, TRUE, FALSE, nullptr);
86   }
87 
88   ~ProcessWindowsData() { ::CloseHandle(m_initial_stop_event); }
89 
90   Error m_launch_error;
91   DebuggerThreadSP m_debugger;
92   StopInfoSP m_pending_stop_info;
93   HANDLE m_initial_stop_event = nullptr;
94   bool m_initial_stop_received = false;
95   bool m_stop_at_entry;
96   std::map<lldb::tid_t, HostThread> m_new_threads;
97   std::set<lldb::tid_t> m_exited_threads;
98 };
99 
100 ProcessSP ProcessWindows::CreateInstance(lldb::TargetSP target_sp,
101                                          lldb::ListenerSP listener_sp,
102                                          const FileSpec *) {
103   return ProcessSP(new ProcessWindows(target_sp, listener_sp));
104 }
105 
106 void ProcessWindows::Initialize() {
107   static llvm::once_flag g_once_flag;
108 
109   llvm::call_once(g_once_flag, []() {
110     PluginManager::RegisterPlugin(GetPluginNameStatic(),
111                                   GetPluginDescriptionStatic(), CreateInstance);
112   });
113 }
114 
115 void ProcessWindows::Terminate() {}
116 
117 lldb_private::ConstString ProcessWindows::GetPluginNameStatic() {
118   static ConstString g_name("windows");
119   return g_name;
120 }
121 
122 const char *ProcessWindows::GetPluginDescriptionStatic() {
123   return "Process plugin for Windows";
124 }
125 
126 //------------------------------------------------------------------------------
127 // Constructors and destructors.
128 
129 ProcessWindows::ProcessWindows(lldb::TargetSP target_sp,
130                                lldb::ListenerSP listener_sp)
131     : lldb_private::Process(target_sp, listener_sp) {}
132 
133 ProcessWindows::~ProcessWindows() {}
134 
135 size_t ProcessWindows::GetSTDOUT(char *buf, size_t buf_size, Error &error) {
136   error.SetErrorString("GetSTDOUT unsupported on Windows");
137   return 0;
138 }
139 
140 size_t ProcessWindows::GetSTDERR(char *buf, size_t buf_size, Error &error) {
141   error.SetErrorString("GetSTDERR unsupported on Windows");
142   return 0;
143 }
144 
145 size_t ProcessWindows::PutSTDIN(const char *buf, size_t buf_size,
146                                 Error &error) {
147   error.SetErrorString("PutSTDIN unsupported on Windows");
148   return 0;
149 }
150 
151 //------------------------------------------------------------------------------
152 // ProcessInterface protocol.
153 
154 lldb_private::ConstString ProcessWindows::GetPluginName() {
155   return GetPluginNameStatic();
156 }
157 
158 uint32_t ProcessWindows::GetPluginVersion() { return 1; }
159 
160 Error ProcessWindows::EnableBreakpointSite(BreakpointSite *bp_site) {
161   Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_BREAKPOINTS);
162   LLDB_LOG(log, "bp_site = {0:x}, id={1}, addr={2:x}", bp_site,
163            bp_site->GetID(), bp_site->GetLoadAddress());
164 
165   Error error = EnableSoftwareBreakpoint(bp_site);
166   if (!error.Success())
167     LLDB_LOG(log, "error: {0}", error);
168   return error;
169 }
170 
171 Error ProcessWindows::DisableBreakpointSite(BreakpointSite *bp_site) {
172   Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_BREAKPOINTS);
173   LLDB_LOG(log, "bp_site = {0:x}, id={1}, addr={2:x}", bp_site,
174            bp_site->GetID(), bp_site->GetLoadAddress());
175 
176   Error error = DisableSoftwareBreakpoint(bp_site);
177 
178   if (!error.Success())
179     LLDB_LOG(log, "error: {0}", error);
180   return error;
181 }
182 
183 Error ProcessWindows::DoDetach(bool keep_stopped) {
184   Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS);
185   DebuggerThreadSP debugger_thread;
186   StateType private_state;
187   {
188     // Acquire the lock only long enough to get the DebuggerThread.
189     // StopDebugging() will trigger a call back into ProcessWindows which
190     // will also acquire the lock.  Thus we have to release the lock before
191     // calling StopDebugging().
192     llvm::sys::ScopedLock lock(m_mutex);
193 
194     private_state = GetPrivateState();
195 
196     if (!m_session_data) {
197       LLDB_LOG(log, "state = {0}, but there is no active session.",
198                private_state);
199       return Error();
200     }
201 
202     debugger_thread = m_session_data->m_debugger;
203   }
204 
205   Error error;
206   if (private_state != eStateExited && private_state != eStateDetached) {
207     LLDB_LOG(log, "detaching from process {0} while state = {1}.",
208              debugger_thread->GetProcess().GetNativeProcess().GetSystemHandle(),
209              private_state);
210     error = debugger_thread->StopDebugging(false);
211     if (error.Success()) {
212       SetPrivateState(eStateDetached);
213     }
214 
215     // By the time StopDebugging returns, there is no more debugger thread, so
216     // we can be assured that no other thread will race for the session data.
217     m_session_data.reset();
218   } else {
219     LLDB_LOG(
220         log,
221         "error: process {0} in state = {1}, but cannot destroy in this state.",
222         debugger_thread->GetProcess().GetNativeProcess().GetSystemHandle(),
223         private_state);
224   }
225 
226   return error;
227 }
228 
229 Error ProcessWindows::DoLaunch(Module *exe_module,
230                                ProcessLaunchInfo &launch_info) {
231   // Even though m_session_data is accessed here, it is before a debugger thread
232   // has been
233   // kicked off.  So there's no race conditions, and it shouldn't be necessary
234   // to acquire
235   // the mutex.
236 
237   Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS);
238   Error result;
239   if (!launch_info.GetFlags().Test(eLaunchFlagDebug)) {
240     StreamString stream;
241     stream.Printf("ProcessWindows unable to launch '%s'.  ProcessWindows can "
242                   "only be used for debug launches.",
243                   launch_info.GetExecutableFile().GetPath().c_str());
244     std::string message = stream.GetString();
245     result.SetErrorString(message.c_str());
246 
247     LLDB_LOG(log, "error: {0}", message);
248     return result;
249   }
250 
251   bool stop_at_entry = launch_info.GetFlags().Test(eLaunchFlagStopAtEntry);
252   m_session_data.reset(new ProcessWindowsData(stop_at_entry));
253 
254   SetPrivateState(eStateLaunching);
255   DebugDelegateSP delegate(new LocalDebugDelegate(shared_from_this()));
256   m_session_data->m_debugger.reset(new DebuggerThread(delegate));
257   DebuggerThreadSP debugger = m_session_data->m_debugger;
258 
259   // Kick off the DebugLaunch asynchronously and wait for it to complete.
260   result = debugger->DebugLaunch(launch_info);
261   if (result.Fail()) {
262     LLDB_LOG(log, "failed launching '{0}'. {1}",
263              launch_info.GetExecutableFile().GetPath(), result);
264     return result;
265   }
266 
267   HostProcess process;
268   Error error = WaitForDebuggerConnection(debugger, process);
269   if (error.Fail()) {
270     LLDB_LOG(log, "failed launching '{0}'. {1}",
271              launch_info.GetExecutableFile().GetPath(), error);
272     return error;
273   }
274 
275   LLDB_LOG(log, "successfully launched '{0}'",
276            launch_info.GetExecutableFile().GetPath());
277 
278   // We've hit the initial stop.  If eLaunchFlagsStopAtEntry was specified, the
279   // private state
280   // should already be set to eStateStopped as a result of hitting the initial
281   // breakpoint.  If
282   // it was not set, the breakpoint should have already been resumed from and
283   // the private state
284   // should already be eStateRunning.
285   launch_info.SetProcessID(process.GetProcessId());
286   SetID(process.GetProcessId());
287 
288   return result;
289 }
290 
291 Error ProcessWindows::DoAttachToProcessWithID(
292     lldb::pid_t pid, const ProcessAttachInfo &attach_info) {
293   Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS);
294   m_session_data.reset(
295       new ProcessWindowsData(!attach_info.GetContinueOnceAttached()));
296 
297   DebugDelegateSP delegate(new LocalDebugDelegate(shared_from_this()));
298   DebuggerThreadSP debugger(new DebuggerThread(delegate));
299 
300   m_session_data->m_debugger = debugger;
301 
302   DWORD process_id = static_cast<DWORD>(pid);
303   Error error = debugger->DebugAttach(process_id, attach_info);
304   if (error.Fail()) {
305     LLDB_LOG(
306         log,
307         "encountered an error occurred initiating the asynchronous attach. {0}",
308         error);
309     return error;
310   }
311 
312   HostProcess process;
313   error = WaitForDebuggerConnection(debugger, process);
314   if (error.Fail()) {
315     LLDB_LOG(log,
316              "encountered an error waiting for the debugger to connect. {0}",
317              error);
318     return error;
319   }
320 
321   LLDB_LOG(log, "successfully attached to process with pid={0}", process_id);
322 
323   // We've hit the initial stop.  If eLaunchFlagsStopAtEntry was specified, the
324   // private state
325   // should already be set to eStateStopped as a result of hitting the initial
326   // breakpoint.  If
327   // it was not set, the breakpoint should have already been resumed from and
328   // the private state
329   // should already be eStateRunning.
330   SetID(process.GetProcessId());
331   return error;
332 }
333 
334 Error ProcessWindows::DoResume() {
335   Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS);
336   llvm::sys::ScopedLock lock(m_mutex);
337   Error error;
338 
339   StateType private_state = GetPrivateState();
340   if (private_state == eStateStopped || private_state == eStateCrashed) {
341     LLDB_LOG(log, "process {0} is in state {1}.  Resuming...",
342              m_session_data->m_debugger->GetProcess().GetProcessId(),
343              GetPrivateState());
344 
345     ExceptionRecordSP active_exception =
346         m_session_data->m_debugger->GetActiveException().lock();
347     if (active_exception) {
348       // Resume the process and continue processing debug events.  Mask
349       // the exception so that from the process's view, there is no
350       // indication that anything happened.
351       m_session_data->m_debugger->ContinueAsyncException(
352           ExceptionResult::MaskException);
353     }
354 
355     LLDB_LOG(log, "resuming {0} threads.", m_thread_list.GetSize());
356 
357     for (uint32_t i = 0; i < m_thread_list.GetSize(); ++i) {
358       auto thread = std::static_pointer_cast<TargetThreadWindows>(
359           m_thread_list.GetThreadAtIndex(i));
360       thread->DoResume();
361     }
362 
363     SetPrivateState(eStateRunning);
364   } else {
365     LLDB_LOG(log, "error: process %I64u is in state %u.  Returning...",
366              m_session_data->m_debugger->GetProcess().GetProcessId(),
367              GetPrivateState());
368   }
369   return error;
370 }
371 
372 Error ProcessWindows::DoDestroy() {
373   Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS);
374   DebuggerThreadSP debugger_thread;
375   StateType private_state;
376   {
377     // Acquire this lock inside an inner scope, only long enough to get the
378     // DebuggerThread.
379     // StopDebugging() will trigger a call back into ProcessWindows which will
380     // acquire the lock
381     // again, so we need to not deadlock.
382     llvm::sys::ScopedLock lock(m_mutex);
383 
384     private_state = GetPrivateState();
385 
386     if (!m_session_data) {
387       LLDB_LOG(log, "warning: state = {0}, but there is no active session.",
388                private_state);
389       return Error();
390     }
391 
392     debugger_thread = m_session_data->m_debugger;
393   }
394 
395   Error error;
396   if (private_state != eStateExited && private_state != eStateDetached) {
397     LLDB_LOG(log, "Shutting down process {0} while state = {1}.",
398              debugger_thread->GetProcess().GetNativeProcess().GetSystemHandle(),
399              private_state);
400     error = debugger_thread->StopDebugging(true);
401 
402     // By the time StopDebugging returns, there is no more debugger thread, so
403     // we can be assured that no other thread will race for the session data.
404     m_session_data.reset();
405   } else {
406     LLDB_LOG(log, "cannot destroy process {0} while state = {1}",
407              debugger_thread->GetProcess().GetNativeProcess().GetSystemHandle(),
408              private_state);
409   }
410 
411   return error;
412 }
413 
414 Error ProcessWindows::DoHalt(bool &caused_stop) {
415   Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS);
416   Error error;
417   StateType state = GetPrivateState();
418   if (state == eStateStopped)
419     caused_stop = false;
420   else {
421     llvm::sys::ScopedLock lock(m_mutex);
422     caused_stop = ::DebugBreakProcess(m_session_data->m_debugger->GetProcess()
423                                           .GetNativeProcess()
424                                           .GetSystemHandle());
425     if (!caused_stop) {
426       error.SetError(::GetLastError(), eErrorTypeWin32);
427       LLDB_LOG(log, "DebugBreakProcess failed with error {0}", error);
428     }
429   }
430   return error;
431 }
432 
433 void ProcessWindows::DidLaunch() {
434   ArchSpec arch_spec;
435   DidAttach(arch_spec);
436 }
437 
438 void ProcessWindows::DidAttach(ArchSpec &arch_spec) {
439   llvm::sys::ScopedLock lock(m_mutex);
440 
441   // The initial stop won't broadcast the state change event, so account for
442   // that here.
443   if (m_session_data && GetPrivateState() == eStateStopped &&
444       m_session_data->m_stop_at_entry)
445     RefreshStateAfterStop();
446 }
447 
448 void ProcessWindows::RefreshStateAfterStop() {
449   Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EXCEPTION);
450   llvm::sys::ScopedLock lock(m_mutex);
451 
452   if (!m_session_data) {
453     LLDB_LOG(log, "no active session.  Returning...");
454     return;
455   }
456 
457   m_thread_list.RefreshStateAfterStop();
458 
459   std::weak_ptr<ExceptionRecord> exception_record =
460       m_session_data->m_debugger->GetActiveException();
461   ExceptionRecordSP active_exception = exception_record.lock();
462   if (!active_exception) {
463     LLDB_LOG(log, "there is no active exception in process {0}.  Why is the "
464                   "process stopped?",
465              m_session_data->m_debugger->GetProcess().GetProcessId());
466     return;
467   }
468 
469   StopInfoSP stop_info;
470   m_thread_list.SetSelectedThreadByID(active_exception->GetThreadID());
471   ThreadSP stop_thread = m_thread_list.GetSelectedThread();
472   if (!stop_thread)
473     return;
474 
475   switch (active_exception->GetExceptionCode()) {
476   case EXCEPTION_SINGLE_STEP: {
477     RegisterContextSP register_context = stop_thread->GetRegisterContext();
478     const uint64_t pc = register_context->GetPC();
479     BreakpointSiteSP site(GetBreakpointSiteList().FindByAddress(pc));
480     if (site && site->ValidForThisThread(stop_thread.get())) {
481       LLDB_LOG(log, "Single-stepped onto a breakpoint in process {0} at "
482                     "address {1:x} with breakpoint site {2}",
483                m_session_data->m_debugger->GetProcess().GetProcessId(), pc,
484                site->GetID());
485       stop_info = StopInfo::CreateStopReasonWithBreakpointSiteID(*stop_thread,
486                                                                  site->GetID());
487       stop_thread->SetStopInfo(stop_info);
488     } else {
489       LLDB_LOG(log, "single stepping thread {0}", stop_thread->GetID());
490       stop_info = StopInfo::CreateStopReasonToTrace(*stop_thread);
491       stop_thread->SetStopInfo(stop_info);
492     }
493     return;
494   }
495 
496   case EXCEPTION_BREAKPOINT: {
497     RegisterContextSP register_context = stop_thread->GetRegisterContext();
498 
499     // The current EIP is AFTER the BP opcode, which is one byte.
500     uint64_t pc = register_context->GetPC() - 1;
501 
502     BreakpointSiteSP site(GetBreakpointSiteList().FindByAddress(pc));
503     if (site) {
504       LLDB_LOG(log, "detected breakpoint in process {0} at address {1:x} with "
505                     "breakpoint site {2}",
506                m_session_data->m_debugger->GetProcess().GetProcessId(), pc,
507                site->GetID());
508 
509       if (site->ValidForThisThread(stop_thread.get())) {
510         LLDB_LOG(log, "Breakpoint site {0} is valid for this thread ({1:x}), "
511                       "creating stop info.",
512                  site->GetID(), stop_thread->GetID());
513 
514         stop_info = StopInfo::CreateStopReasonWithBreakpointSiteID(
515             *stop_thread, site->GetID());
516         register_context->SetPC(pc);
517       } else {
518         LLDB_LOG(log, "Breakpoint site {0} is not valid for this thread, "
519                       "creating empty stop info.",
520                  site->GetID());
521       }
522       stop_thread->SetStopInfo(stop_info);
523       return;
524     } else {
525       // The thread hit a hard-coded breakpoint like an `int 3` or
526       // `__debugbreak()`.
527       LLDB_LOG(log,
528                "No breakpoint site matches for this thread. __debugbreak()?  "
529                "Creating stop info with the exception.");
530       // FALLTHROUGH:  We'll treat this as a generic exception record in the
531       // default case.
532     }
533   }
534 
535   default: {
536     std::string desc;
537     llvm::raw_string_ostream desc_stream(desc);
538     desc_stream << "Exception "
539                 << llvm::format_hex(active_exception->GetExceptionCode(), 8)
540                 << " encountered at address "
541                 << llvm::format_hex(active_exception->GetExceptionAddress(), 8);
542     stop_info = StopInfo::CreateStopReasonWithException(
543         *stop_thread, desc_stream.str().c_str());
544     stop_thread->SetStopInfo(stop_info);
545     LLDB_LOG(log, "{0}", desc_stream.str());
546     return;
547   }
548   }
549 }
550 
551 bool ProcessWindows::CanDebug(lldb::TargetSP target_sp,
552                               bool plugin_specified_by_name) {
553   if (plugin_specified_by_name)
554     return true;
555 
556   // For now we are just making sure the file exists for a given module
557   ModuleSP exe_module_sp(target_sp->GetExecutableModule());
558   if (exe_module_sp.get())
559     return exe_module_sp->GetFileSpec().Exists();
560   // However, if there is no executable module, we return true since we might be
561   // preparing to attach.
562   return true;
563 }
564 
565 bool ProcessWindows::UpdateThreadList(ThreadList &old_thread_list,
566                                       ThreadList &new_thread_list) {
567   Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_THREAD);
568   // Add all the threads that were previously running and for which we did not
569   // detect a thread exited event.
570   int new_size = 0;
571   int continued_threads = 0;
572   int exited_threads = 0;
573   int new_threads = 0;
574 
575   for (ThreadSP old_thread : old_thread_list.Threads()) {
576     lldb::tid_t old_thread_id = old_thread->GetID();
577     auto exited_thread_iter =
578         m_session_data->m_exited_threads.find(old_thread_id);
579     if (exited_thread_iter == m_session_data->m_exited_threads.end()) {
580       new_thread_list.AddThread(old_thread);
581       ++new_size;
582       ++continued_threads;
583       LLDB_LOGV(log, "Thread {0} was running and is still running.",
584                 old_thread_id);
585     } else {
586       LLDB_LOGV(log, "Thread {0} was running and has exited.", old_thread_id);
587       ++exited_threads;
588     }
589   }
590 
591   // Also add all the threads that are new since the last time we broke into the
592   // debugger.
593   for (const auto &thread_info : m_session_data->m_new_threads) {
594     ThreadSP thread(new TargetThreadWindows(*this, thread_info.second));
595     thread->SetID(thread_info.first);
596     new_thread_list.AddThread(thread);
597     ++new_size;
598     ++new_threads;
599     LLDB_LOGV(log, "Thread {0} is new since last update.", thread_info.first);
600   }
601 
602   LLDB_LOG(log, "{0} new threads, {1} old threads, {2} exited threads.",
603            new_threads, continued_threads, exited_threads);
604 
605   m_session_data->m_new_threads.clear();
606   m_session_data->m_exited_threads.clear();
607 
608   return new_size > 0;
609 }
610 
611 bool ProcessWindows::IsAlive() {
612   StateType state = GetPrivateState();
613   switch (state) {
614   case eStateCrashed:
615   case eStateDetached:
616   case eStateUnloaded:
617   case eStateExited:
618   case eStateInvalid:
619     return false;
620   default:
621     return true;
622   }
623 }
624 
625 size_t ProcessWindows::DoReadMemory(lldb::addr_t vm_addr, void *buf,
626                                     size_t size, Error &error) {
627   Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_MEMORY);
628   llvm::sys::ScopedLock lock(m_mutex);
629 
630   if (!m_session_data)
631     return 0;
632 
633   LLDB_LOG(log, "attempting to read {0} bytes from address {1:x}", size,
634            vm_addr);
635 
636   HostProcess process = m_session_data->m_debugger->GetProcess();
637   void *addr = reinterpret_cast<void *>(vm_addr);
638   SIZE_T bytes_read = 0;
639   if (!ReadProcessMemory(process.GetNativeProcess().GetSystemHandle(), addr,
640                          buf, size, &bytes_read)) {
641     error.SetError(GetLastError(), eErrorTypeWin32);
642     LLDB_LOG(log, "reading failed with error: {0}", error);
643   }
644   return bytes_read;
645 }
646 
647 size_t ProcessWindows::DoWriteMemory(lldb::addr_t vm_addr, const void *buf,
648                                      size_t size, Error &error) {
649   Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_MEMORY);
650   llvm::sys::ScopedLock lock(m_mutex);
651   LLDB_LOG(log, "attempting to write {0} bytes into address {1:x}", size,
652            vm_addr);
653 
654   if (!m_session_data) {
655     LLDB_LOG(log, "cannot write, there is no active debugger connection.");
656     return 0;
657   }
658 
659   HostProcess process = m_session_data->m_debugger->GetProcess();
660   void *addr = reinterpret_cast<void *>(vm_addr);
661   SIZE_T bytes_written = 0;
662   lldb::process_t handle = process.GetNativeProcess().GetSystemHandle();
663   if (WriteProcessMemory(handle, addr, buf, size, &bytes_written))
664     FlushInstructionCache(handle, addr, bytes_written);
665   else {
666     error.SetError(GetLastError(), eErrorTypeWin32);
667     LLDB_LOG(log, "writing failed with error: {0}", error);
668   }
669   return bytes_written;
670 }
671 
672 Error ProcessWindows::GetMemoryRegionInfo(lldb::addr_t vm_addr,
673                                           MemoryRegionInfo &info) {
674   Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_MEMORY);
675   Error error;
676   llvm::sys::ScopedLock lock(m_mutex);
677   info.Clear();
678 
679   if (!m_session_data) {
680     error.SetErrorString(
681         "GetMemoryRegionInfo called with no debugging session.");
682     LLDB_LOG(log, "error: {0}", error);
683     return error;
684   }
685   HostProcess process = m_session_data->m_debugger->GetProcess();
686   lldb::process_t handle = process.GetNativeProcess().GetSystemHandle();
687   if (handle == nullptr || handle == LLDB_INVALID_PROCESS) {
688     error.SetErrorString(
689         "GetMemoryRegionInfo called with an invalid target process.");
690     LLDB_LOG(log, "error: {0}", error);
691     return error;
692   }
693 
694   LLDB_LOG(log, "getting info for address {0:x}", vm_addr);
695 
696   void *addr = reinterpret_cast<void *>(vm_addr);
697   MEMORY_BASIC_INFORMATION mem_info = {};
698   SIZE_T result = ::VirtualQueryEx(handle, addr, &mem_info, sizeof(mem_info));
699   if (result == 0) {
700     if (::GetLastError() == ERROR_INVALID_PARAMETER) {
701       // ERROR_INVALID_PARAMETER is returned if VirtualQueryEx is called with an
702       // address
703       // past the highest accessible address. We should return a range from the
704       // vm_addr
705       // to LLDB_INVALID_ADDRESS
706       info.GetRange().SetRangeBase(vm_addr);
707       info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS);
708       info.SetReadable(MemoryRegionInfo::eNo);
709       info.SetExecutable(MemoryRegionInfo::eNo);
710       info.SetWritable(MemoryRegionInfo::eNo);
711       info.SetMapped(MemoryRegionInfo::eNo);
712       return error;
713     } else {
714       error.SetError(::GetLastError(), eErrorTypeWin32);
715       LLDB_LOG(log, "VirtualQueryEx returned error {0} while getting memory "
716                     "region info for address {1:x}",
717                error, vm_addr);
718       return error;
719     }
720   }
721 
722   // Protect bits are only valid for MEM_COMMIT regions.
723   if (mem_info.State == MEM_COMMIT) {
724     const bool readable = IsPageReadable(mem_info.Protect);
725     const bool executable = IsPageExecutable(mem_info.Protect);
726     const bool writable = IsPageWritable(mem_info.Protect);
727     info.SetReadable(readable ? MemoryRegionInfo::eYes : MemoryRegionInfo::eNo);
728     info.SetExecutable(executable ? MemoryRegionInfo::eYes
729                                   : MemoryRegionInfo::eNo);
730     info.SetWritable(writable ? MemoryRegionInfo::eYes : MemoryRegionInfo::eNo);
731   } else {
732     info.SetReadable(MemoryRegionInfo::eNo);
733     info.SetExecutable(MemoryRegionInfo::eNo);
734     info.SetWritable(MemoryRegionInfo::eNo);
735   }
736 
737   // AllocationBase is defined for MEM_COMMIT and MEM_RESERVE but not MEM_FREE.
738   if (mem_info.State != MEM_FREE) {
739     info.GetRange().SetRangeBase(
740         reinterpret_cast<addr_t>(mem_info.AllocationBase));
741     info.GetRange().SetRangeEnd(reinterpret_cast<addr_t>(mem_info.BaseAddress) +
742                                 mem_info.RegionSize);
743     info.SetMapped(MemoryRegionInfo::eYes);
744   } else {
745     // In the unmapped case we need to return the distance to the next block of
746     // memory.
747     // VirtualQueryEx nearly does that except that it gives the distance from
748     // the start
749     // of the page containing vm_addr.
750     SYSTEM_INFO data;
751     GetSystemInfo(&data);
752     DWORD page_offset = vm_addr % data.dwPageSize;
753     info.GetRange().SetRangeBase(vm_addr);
754     info.GetRange().SetByteSize(mem_info.RegionSize - page_offset);
755     info.SetMapped(MemoryRegionInfo::eNo);
756   }
757 
758   error.SetError(::GetLastError(), eErrorTypeWin32);
759   LLDB_LOGV(log, "Memory region info for address {0}: readable={1}, "
760                  "executable={2}, writable={3}",
761             vm_addr, info.GetReadable(), info.GetExecutable(),
762             info.GetWritable());
763   return error;
764 }
765 
766 lldb::addr_t ProcessWindows::GetImageInfoAddress() {
767   Target &target = GetTarget();
768   ObjectFile *obj_file = target.GetExecutableModule()->GetObjectFile();
769   Address addr = obj_file->GetImageInfoAddress(&target);
770   if (addr.IsValid())
771     return addr.GetLoadAddress(&target);
772   else
773     return LLDB_INVALID_ADDRESS;
774 }
775 
776 void ProcessWindows::OnExitProcess(uint32_t exit_code) {
777   // No need to acquire the lock since m_session_data isn't accessed.
778   Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS);
779   LLDB_LOG(log, "Process {0} exited with code {1}", GetID(), exit_code);
780 
781   TargetSP target = m_target_sp.lock();
782   if (target) {
783     ModuleSP executable_module = target->GetExecutableModule();
784     ModuleList unloaded_modules;
785     unloaded_modules.Append(executable_module);
786     target->ModulesDidUnload(unloaded_modules, true);
787   }
788 
789   SetProcessExitStatus(GetID(), true, 0, exit_code);
790   SetPrivateState(eStateExited);
791 }
792 
793 void ProcessWindows::OnDebuggerConnected(lldb::addr_t image_base) {
794   DebuggerThreadSP debugger = m_session_data->m_debugger;
795   Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS);
796   LLDB_LOG(log, "Debugger connected to process {0}.  Image base = {1:x}",
797            debugger->GetProcess().GetProcessId(), image_base);
798 
799   ModuleSP module = GetTarget().GetExecutableModule();
800   if (!module) {
801     // During attach, we won't have the executable module, so find it now.
802     const DWORD pid = debugger->GetProcess().GetProcessId();
803     const std::string file_name = GetProcessExecutableName(pid);
804     if (file_name.empty()) {
805       return;
806     }
807 
808     FileSpec executable_file(file_name, true);
809     ModuleSpec module_spec(executable_file);
810     Error error;
811     module = GetTarget().GetSharedModule(module_spec, &error);
812     if (!module) {
813       return;
814     }
815 
816     GetTarget().SetExecutableModule(module, false);
817   }
818 
819   bool load_addr_changed;
820   module->SetLoadAddress(GetTarget(), image_base, false, load_addr_changed);
821 
822   ModuleList loaded_modules;
823   loaded_modules.Append(module);
824   GetTarget().ModulesDidLoad(loaded_modules);
825 
826   // Add the main executable module to the list of pending module loads.  We
827   // can't call
828   // GetTarget().ModulesDidLoad() here because we still haven't returned from
829   // DoLaunch() / DoAttach() yet
830   // so the target may not have set the process instance to `this` yet.
831   llvm::sys::ScopedLock lock(m_mutex);
832   const HostThreadWindows &wmain_thread =
833       debugger->GetMainThread().GetNativeThread();
834   m_session_data->m_new_threads[wmain_thread.GetThreadId()] =
835       debugger->GetMainThread();
836 }
837 
838 ExceptionResult
839 ProcessWindows::OnDebugException(bool first_chance,
840                                  const ExceptionRecord &record) {
841   Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EXCEPTION);
842   llvm::sys::ScopedLock lock(m_mutex);
843 
844   // FIXME: Without this check, occasionally when running the test suite there
845   // is
846   // an issue where m_session_data can be null.  It's not clear how this could
847   // happen
848   // but it only surfaces while running the test suite.  In order to properly
849   // diagnose
850   // this, we probably need to first figure allow the test suite to print out
851   // full
852   // lldb logs, and then add logging to the process plugin.
853   if (!m_session_data) {
854     LLDB_LOG(log, "Debugger thread reported exception {0:x} at address {1:x}, "
855                   "but there is no session.",
856              record.GetExceptionCode(), record.GetExceptionAddress());
857     return ExceptionResult::SendToApplication;
858   }
859 
860   if (!first_chance) {
861     // Any second chance exception is an application crash by definition.
862     SetPrivateState(eStateCrashed);
863   }
864 
865   ExceptionResult result = ExceptionResult::SendToApplication;
866   switch (record.GetExceptionCode()) {
867   case EXCEPTION_BREAKPOINT:
868     // Handle breakpoints at the first chance.
869     result = ExceptionResult::BreakInDebugger;
870 
871     if (!m_session_data->m_initial_stop_received) {
872       LLDB_LOG(
873           log,
874           "Hit loader breakpoint at address {0:x}, setting initial stop event.",
875           record.GetExceptionAddress());
876       m_session_data->m_initial_stop_received = true;
877       ::SetEvent(m_session_data->m_initial_stop_event);
878     } else {
879       LLDB_LOG(log, "Hit non-loader breakpoint at address {0:x}.",
880                record.GetExceptionAddress());
881     }
882     SetPrivateState(eStateStopped);
883     break;
884   case EXCEPTION_SINGLE_STEP:
885     result = ExceptionResult::BreakInDebugger;
886     SetPrivateState(eStateStopped);
887     break;
888   default:
889     LLDB_LOG(log, "Debugger thread reported exception {0:x} at address {1:x} "
890                   "(first_chance={2})",
891              record.GetExceptionCode(), record.GetExceptionAddress(),
892              first_chance);
893     // For non-breakpoints, give the application a chance to handle the
894     // exception first.
895     if (first_chance)
896       result = ExceptionResult::SendToApplication;
897     else
898       result = ExceptionResult::BreakInDebugger;
899   }
900 
901   return result;
902 }
903 
904 void ProcessWindows::OnCreateThread(const HostThread &new_thread) {
905   llvm::sys::ScopedLock lock(m_mutex);
906   const HostThreadWindows &wnew_thread = new_thread.GetNativeThread();
907   m_session_data->m_new_threads[wnew_thread.GetThreadId()] = new_thread;
908 }
909 
910 void ProcessWindows::OnExitThread(lldb::tid_t thread_id, uint32_t exit_code) {
911   llvm::sys::ScopedLock lock(m_mutex);
912 
913   // On a forced termination, we may get exit thread events after the session
914   // data has been cleaned up.
915   if (!m_session_data)
916     return;
917 
918   // A thread may have started and exited before the debugger stopped allowing a
919   // refresh.
920   // Just remove it from the new threads list in that case.
921   auto iter = m_session_data->m_new_threads.find(thread_id);
922   if (iter != m_session_data->m_new_threads.end())
923     m_session_data->m_new_threads.erase(iter);
924   else
925     m_session_data->m_exited_threads.insert(thread_id);
926 }
927 
928 void ProcessWindows::OnLoadDll(const ModuleSpec &module_spec,
929                                lldb::addr_t module_addr) {
930   // Confusingly, there is no Target::AddSharedModule.  Instead, calling
931   // GetSharedModule() with
932   // a new module will add it to the module list and return a corresponding
933   // ModuleSP.
934   Error error;
935   ModuleSP module = GetTarget().GetSharedModule(module_spec, &error);
936   bool load_addr_changed = false;
937   module->SetLoadAddress(GetTarget(), module_addr, false, load_addr_changed);
938 
939   ModuleList loaded_modules;
940   loaded_modules.Append(module);
941   GetTarget().ModulesDidLoad(loaded_modules);
942 }
943 
944 void ProcessWindows::OnUnloadDll(lldb::addr_t module_addr) {
945   Address resolved_addr;
946   if (GetTarget().ResolveLoadAddress(module_addr, resolved_addr)) {
947     ModuleSP module = resolved_addr.GetModule();
948     if (module) {
949       ModuleList unloaded_modules;
950       unloaded_modules.Append(module);
951       GetTarget().ModulesDidUnload(unloaded_modules, false);
952     }
953   }
954 }
955 
956 void ProcessWindows::OnDebugString(const std::string &string) {}
957 
958 void ProcessWindows::OnDebuggerError(const Error &error, uint32_t type) {
959   llvm::sys::ScopedLock lock(m_mutex);
960   Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS);
961 
962   if (m_session_data->m_initial_stop_received) {
963     // This happened while debugging.  Do we shutdown the debugging session, try
964     // to continue, or do something else?
965     LLDB_LOG(log, "Error {0} occurred during debugging.  Unexpected behavior "
966                   "may result.  {1}",
967              error.GetError(), error);
968   } else {
969     // If we haven't actually launched the process yet, this was an error
970     // launching the
971     // process.  Set the internal error and signal the initial stop event so
972     // that the DoLaunch
973     // method wakes up and returns a failure.
974     m_session_data->m_launch_error = error;
975     ::SetEvent(m_session_data->m_initial_stop_event);
976     LLDB_LOG(
977         log,
978         "Error {0} occurred launching the process before the initial stop. {1}",
979         error.GetError(), error);
980     return;
981   }
982 }
983 
984 Error ProcessWindows::WaitForDebuggerConnection(DebuggerThreadSP debugger,
985                                                 HostProcess &process) {
986   Error result;
987   Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS |
988                                             WINDOWS_LOG_BREAKPOINTS);
989   LLDB_LOG(log, "Waiting for loader breakpoint.");
990 
991   // Block this function until we receive the initial stop from the process.
992   if (::WaitForSingleObject(m_session_data->m_initial_stop_event, INFINITE) ==
993       WAIT_OBJECT_0) {
994     LLDB_LOG(log, "hit loader breakpoint, returning.");
995 
996     process = debugger->GetProcess();
997     return m_session_data->m_launch_error;
998   } else
999     return Error(::GetLastError(), eErrorTypeWin32);
1000 }
1001 
1002 // The Windows page protection bits are NOT independent masks that can be
1003 // bitwise-ORed together.  For example, PAGE_EXECUTE_READ is not
1004 // (PAGE_EXECUTE | PAGE_READ).  To test for an access type, it's necessary to
1005 // test for any of the bits that provide that access type.
1006 bool ProcessWindows::IsPageReadable(uint32_t protect) {
1007   return (protect & PAGE_NOACCESS) == 0;
1008 }
1009 
1010 bool ProcessWindows::IsPageWritable(uint32_t protect) {
1011   return (protect & (PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY |
1012                      PAGE_READWRITE | PAGE_WRITECOPY)) != 0;
1013 }
1014 
1015 bool ProcessWindows::IsPageExecutable(uint32_t protect) {
1016   return (protect & (PAGE_EXECUTE | PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE |
1017                      PAGE_EXECUTE_WRITECOPY)) != 0;
1018 }
1019 
1020 } // namespace lldb_private
1021