118a9135dSAdrian McCarthy //===-- ProcessWindows.cpp --------------------------------------*- C++ -*-===// 218a9135dSAdrian McCarthy // 318a9135dSAdrian McCarthy // The LLVM Compiler Infrastructure 418a9135dSAdrian McCarthy // 518a9135dSAdrian McCarthy // This file is distributed under the University of Illinois Open Source 618a9135dSAdrian McCarthy // License. See LICENSE.TXT for details. 718a9135dSAdrian McCarthy // 818a9135dSAdrian McCarthy //===----------------------------------------------------------------------===// 918a9135dSAdrian McCarthy 1018a9135dSAdrian McCarthy #include "ProcessWindows.h" 1118a9135dSAdrian McCarthy 124ad5def9SAdrian McCarthy // Windows includes 134ad5def9SAdrian McCarthy #include "lldb/Host/windows/windows.h" 144ad5def9SAdrian McCarthy #include <psapi.h> 154ad5def9SAdrian McCarthy 1618a9135dSAdrian McCarthy // Other libraries and framework includes 1718a9135dSAdrian McCarthy #include "lldb/Core/Module.h" 1818a9135dSAdrian McCarthy #include "lldb/Core/ModuleSpec.h" 1918a9135dSAdrian McCarthy #include "lldb/Core/PluginManager.h" 2018a9135dSAdrian McCarthy #include "lldb/Core/Section.h" 214ad5def9SAdrian McCarthy #include "lldb/Host/HostNativeProcessBase.h" 224ad5def9SAdrian McCarthy #include "lldb/Host/HostProcess.h" 234ad5def9SAdrian McCarthy #include "lldb/Host/windows/HostThreadWindows.h" 240c35cde9SAdrian McCarthy #include "lldb/Host/windows/windows.h" 252f3df613SZachary Turner #include "lldb/Symbol/ObjectFile.h" 2618a9135dSAdrian McCarthy #include "lldb/Target/DynamicLoader.h" 2718a9135dSAdrian McCarthy #include "lldb/Target/MemoryRegionInfo.h" 284ad5def9SAdrian McCarthy #include "lldb/Target/StopInfo.h" 2918a9135dSAdrian McCarthy #include "lldb/Target/Target.h" 30d821c997SPavel Labath #include "lldb/Utility/State.h" 3118a9135dSAdrian McCarthy 324ad5def9SAdrian McCarthy #include "llvm/Support/ConvertUTF.h" 334ad5def9SAdrian McCarthy #include "llvm/Support/Format.h" 34c5f28e2aSKamil Rytarowski #include "llvm/Support/Threading.h" 354ad5def9SAdrian McCarthy #include "llvm/Support/raw_ostream.h" 364ad5def9SAdrian McCarthy 374ad5def9SAdrian McCarthy #include "DebuggerThread.h" 384ad5def9SAdrian McCarthy #include "ExceptionRecord.h" 394ad5def9SAdrian McCarthy #include "ForwardDecl.h" 404ad5def9SAdrian McCarthy #include "LocalDebugDelegate.h" 414ad5def9SAdrian McCarthy #include "ProcessWindowsLog.h" 424ad5def9SAdrian McCarthy #include "TargetThreadWindows.h" 434ad5def9SAdrian McCarthy 4418a9135dSAdrian McCarthy using namespace lldb; 4518a9135dSAdrian McCarthy using namespace lldb_private; 4618a9135dSAdrian McCarthy 474ad5def9SAdrian McCarthy namespace { 484ad5def9SAdrian McCarthy std::string GetProcessExecutableName(HANDLE process_handle) { 494ad5def9SAdrian McCarthy std::vector<wchar_t> file_name; 504ad5def9SAdrian McCarthy DWORD file_name_size = MAX_PATH; // first guess, not an absolute limit 514ad5def9SAdrian McCarthy DWORD copied = 0; 524ad5def9SAdrian McCarthy do { 534ad5def9SAdrian McCarthy file_name_size *= 2; 544ad5def9SAdrian McCarthy file_name.resize(file_name_size); 554ad5def9SAdrian McCarthy copied = ::GetModuleFileNameExW(process_handle, NULL, file_name.data(), 564ad5def9SAdrian McCarthy file_name_size); 574ad5def9SAdrian McCarthy } while (copied >= file_name_size); 584ad5def9SAdrian McCarthy file_name.resize(copied); 594ad5def9SAdrian McCarthy std::string result; 604ad5def9SAdrian McCarthy llvm::convertWideToUTF8(file_name.data(), result); 614ad5def9SAdrian McCarthy return result; 624ad5def9SAdrian McCarthy } 634ad5def9SAdrian McCarthy 644ad5def9SAdrian McCarthy std::string GetProcessExecutableName(DWORD pid) { 654ad5def9SAdrian McCarthy std::string file_name; 664ad5def9SAdrian McCarthy HANDLE process_handle = 674ad5def9SAdrian McCarthy ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid); 684ad5def9SAdrian McCarthy if (process_handle != NULL) { 694ad5def9SAdrian McCarthy file_name = GetProcessExecutableName(process_handle); 704ad5def9SAdrian McCarthy ::CloseHandle(process_handle); 714ad5def9SAdrian McCarthy } 724ad5def9SAdrian McCarthy return file_name; 734ad5def9SAdrian McCarthy } 744ad5def9SAdrian McCarthy 754ad5def9SAdrian McCarthy } // anonymous namespace 764ad5def9SAdrian McCarthy 77b9c1b51eSKate Stone namespace lldb_private { 7818a9135dSAdrian McCarthy 794ad5def9SAdrian McCarthy // We store a pointer to this class in the ProcessWindows, so that we don't 8005097246SAdrian Prantl // expose Windows-specific types and implementation details from a public 8105097246SAdrian Prantl // header file. 824ad5def9SAdrian McCarthy class ProcessWindowsData { 834ad5def9SAdrian McCarthy public: 844ad5def9SAdrian McCarthy ProcessWindowsData(bool stop_at_entry) : m_stop_at_entry(stop_at_entry) { 854ad5def9SAdrian McCarthy m_initial_stop_event = ::CreateEvent(nullptr, TRUE, FALSE, nullptr); 864ad5def9SAdrian McCarthy } 874ad5def9SAdrian McCarthy 884ad5def9SAdrian McCarthy ~ProcessWindowsData() { ::CloseHandle(m_initial_stop_event); } 894ad5def9SAdrian McCarthy 9097206d57SZachary Turner Status m_launch_error; 914ad5def9SAdrian McCarthy DebuggerThreadSP m_debugger; 924ad5def9SAdrian McCarthy StopInfoSP m_pending_stop_info; 934ad5def9SAdrian McCarthy HANDLE m_initial_stop_event = nullptr; 944ad5def9SAdrian McCarthy bool m_initial_stop_received = false; 954ad5def9SAdrian McCarthy bool m_stop_at_entry; 964ad5def9SAdrian McCarthy std::map<lldb::tid_t, HostThread> m_new_threads; 974ad5def9SAdrian McCarthy std::set<lldb::tid_t> m_exited_threads; 984ad5def9SAdrian McCarthy }; 994ad5def9SAdrian McCarthy 1004ad5def9SAdrian McCarthy ProcessSP ProcessWindows::CreateInstance(lldb::TargetSP target_sp, 1014ad5def9SAdrian McCarthy lldb::ListenerSP listener_sp, 1024ad5def9SAdrian McCarthy const FileSpec *) { 1034ad5def9SAdrian McCarthy return ProcessSP(new ProcessWindows(target_sp, listener_sp)); 1044ad5def9SAdrian McCarthy } 1054ad5def9SAdrian McCarthy 1064ad5def9SAdrian McCarthy void ProcessWindows::Initialize() { 107c5f28e2aSKamil Rytarowski static llvm::once_flag g_once_flag; 1084ad5def9SAdrian McCarthy 109c5f28e2aSKamil Rytarowski llvm::call_once(g_once_flag, []() { 1104ad5def9SAdrian McCarthy PluginManager::RegisterPlugin(GetPluginNameStatic(), 1114ad5def9SAdrian McCarthy GetPluginDescriptionStatic(), CreateInstance); 1124ad5def9SAdrian McCarthy }); 1134ad5def9SAdrian McCarthy } 1144ad5def9SAdrian McCarthy 1154ad5def9SAdrian McCarthy void ProcessWindows::Terminate() {} 1164ad5def9SAdrian McCarthy 1174ad5def9SAdrian McCarthy lldb_private::ConstString ProcessWindows::GetPluginNameStatic() { 1184ad5def9SAdrian McCarthy static ConstString g_name("windows"); 1194ad5def9SAdrian McCarthy return g_name; 1204ad5def9SAdrian McCarthy } 1214ad5def9SAdrian McCarthy 1224ad5def9SAdrian McCarthy const char *ProcessWindows::GetPluginDescriptionStatic() { 1234ad5def9SAdrian McCarthy return "Process plugin for Windows"; 1244ad5def9SAdrian McCarthy } 1254ad5def9SAdrian McCarthy 12618a9135dSAdrian McCarthy //------------------------------------------------------------------------------ 12718a9135dSAdrian McCarthy // Constructors and destructors. 12818a9135dSAdrian McCarthy 129b9c1b51eSKate Stone ProcessWindows::ProcessWindows(lldb::TargetSP target_sp, 130b9c1b51eSKate Stone lldb::ListenerSP listener_sp) 131b9c1b51eSKate Stone : lldb_private::Process(target_sp, listener_sp) {} 13218a9135dSAdrian McCarthy 133b9c1b51eSKate Stone ProcessWindows::~ProcessWindows() {} 13418a9135dSAdrian McCarthy 13597206d57SZachary Turner size_t ProcessWindows::GetSTDOUT(char *buf, size_t buf_size, Status &error) { 13618a9135dSAdrian McCarthy error.SetErrorString("GetSTDOUT unsupported on Windows"); 13718a9135dSAdrian McCarthy return 0; 13818a9135dSAdrian McCarthy } 13918a9135dSAdrian McCarthy 14097206d57SZachary Turner size_t ProcessWindows::GetSTDERR(char *buf, size_t buf_size, Status &error) { 14118a9135dSAdrian McCarthy error.SetErrorString("GetSTDERR unsupported on Windows"); 14218a9135dSAdrian McCarthy return 0; 14318a9135dSAdrian McCarthy } 14418a9135dSAdrian McCarthy 145b9c1b51eSKate Stone size_t ProcessWindows::PutSTDIN(const char *buf, size_t buf_size, 14697206d57SZachary Turner Status &error) { 14718a9135dSAdrian McCarthy error.SetErrorString("PutSTDIN unsupported on Windows"); 14818a9135dSAdrian McCarthy return 0; 14918a9135dSAdrian McCarthy } 15018a9135dSAdrian McCarthy 15118a9135dSAdrian McCarthy //------------------------------------------------------------------------------ 15218a9135dSAdrian McCarthy // ProcessInterface protocol. 15318a9135dSAdrian McCarthy 1544ad5def9SAdrian McCarthy lldb_private::ConstString ProcessWindows::GetPluginName() { 1554ad5def9SAdrian McCarthy return GetPluginNameStatic(); 1564ad5def9SAdrian McCarthy } 1574ad5def9SAdrian McCarthy 1584ad5def9SAdrian McCarthy uint32_t ProcessWindows::GetPluginVersion() { return 1; } 1594ad5def9SAdrian McCarthy 16097206d57SZachary Turner Status ProcessWindows::EnableBreakpointSite(BreakpointSite *bp_site) { 161a385d2c1SPavel Labath Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_BREAKPOINTS); 162a385d2c1SPavel Labath LLDB_LOG(log, "bp_site = {0:x}, id={1}, addr={2:x}", bp_site, 163a385d2c1SPavel Labath bp_site->GetID(), bp_site->GetLoadAddress()); 1644ad5def9SAdrian McCarthy 16597206d57SZachary Turner Status error = EnableSoftwareBreakpoint(bp_site); 166a385d2c1SPavel Labath if (!error.Success()) 167a385d2c1SPavel Labath LLDB_LOG(log, "error: {0}", error); 1684ad5def9SAdrian McCarthy return error; 1694ad5def9SAdrian McCarthy } 1704ad5def9SAdrian McCarthy 17197206d57SZachary Turner Status ProcessWindows::DisableBreakpointSite(BreakpointSite *bp_site) { 172a385d2c1SPavel Labath Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_BREAKPOINTS); 173a385d2c1SPavel Labath LLDB_LOG(log, "bp_site = {0:x}, id={1}, addr={2:x}", bp_site, 174a385d2c1SPavel Labath bp_site->GetID(), bp_site->GetLoadAddress()); 1754ad5def9SAdrian McCarthy 17697206d57SZachary Turner Status error = DisableSoftwareBreakpoint(bp_site); 1774ad5def9SAdrian McCarthy 178a385d2c1SPavel Labath if (!error.Success()) 179a385d2c1SPavel Labath LLDB_LOG(log, "error: {0}", error); 1804ad5def9SAdrian McCarthy return error; 1814ad5def9SAdrian McCarthy } 1824ad5def9SAdrian McCarthy 18397206d57SZachary Turner Status ProcessWindows::DoDetach(bool keep_stopped) { 184a385d2c1SPavel Labath Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS); 1854ad5def9SAdrian McCarthy DebuggerThreadSP debugger_thread; 1864ad5def9SAdrian McCarthy StateType private_state; 1874ad5def9SAdrian McCarthy { 1884ad5def9SAdrian McCarthy // Acquire the lock only long enough to get the DebuggerThread. 18905097246SAdrian Prantl // StopDebugging() will trigger a call back into ProcessWindows which will 19005097246SAdrian Prantl // also acquire the lock. Thus we have to release the lock before calling 19105097246SAdrian Prantl // StopDebugging(). 1924ad5def9SAdrian McCarthy llvm::sys::ScopedLock lock(m_mutex); 1934ad5def9SAdrian McCarthy 1944ad5def9SAdrian McCarthy private_state = GetPrivateState(); 1954ad5def9SAdrian McCarthy 1964ad5def9SAdrian McCarthy if (!m_session_data) { 197a385d2c1SPavel Labath LLDB_LOG(log, "state = {0}, but there is no active session.", 1984ad5def9SAdrian McCarthy private_state); 19997206d57SZachary Turner return Status(); 2004ad5def9SAdrian McCarthy } 2014ad5def9SAdrian McCarthy 2024ad5def9SAdrian McCarthy debugger_thread = m_session_data->m_debugger; 2034ad5def9SAdrian McCarthy } 2044ad5def9SAdrian McCarthy 20597206d57SZachary Turner Status error; 2064ad5def9SAdrian McCarthy if (private_state != eStateExited && private_state != eStateDetached) { 207a385d2c1SPavel Labath LLDB_LOG(log, "detaching from process {0} while state = {1}.", 2084ad5def9SAdrian McCarthy debugger_thread->GetProcess().GetNativeProcess().GetSystemHandle(), 2094ad5def9SAdrian McCarthy private_state); 2104ad5def9SAdrian McCarthy error = debugger_thread->StopDebugging(false); 2114ad5def9SAdrian McCarthy if (error.Success()) { 2124ad5def9SAdrian McCarthy SetPrivateState(eStateDetached); 2134ad5def9SAdrian McCarthy } 2144ad5def9SAdrian McCarthy 2154ad5def9SAdrian McCarthy // By the time StopDebugging returns, there is no more debugger thread, so 2164ad5def9SAdrian McCarthy // we can be assured that no other thread will race for the session data. 2174ad5def9SAdrian McCarthy m_session_data.reset(); 2184ad5def9SAdrian McCarthy } else { 219a385d2c1SPavel Labath LLDB_LOG( 220a385d2c1SPavel Labath log, 221a385d2c1SPavel Labath "error: process {0} in state = {1}, but cannot destroy in this state.", 2224ad5def9SAdrian McCarthy debugger_thread->GetProcess().GetNativeProcess().GetSystemHandle(), 2234ad5def9SAdrian McCarthy private_state); 2244ad5def9SAdrian McCarthy } 2254ad5def9SAdrian McCarthy 2264ad5def9SAdrian McCarthy return error; 2274ad5def9SAdrian McCarthy } 2284ad5def9SAdrian McCarthy 22997206d57SZachary Turner Status ProcessWindows::DoLaunch(Module *exe_module, 2304ad5def9SAdrian McCarthy ProcessLaunchInfo &launch_info) { 23105097246SAdrian Prantl // Even though m_session_data is accessed here, it is before a debugger 23205097246SAdrian Prantl // thread has been kicked off. So there's no race conditions, and it 23305097246SAdrian Prantl // shouldn't be necessary to acquire the mutex. 2344ad5def9SAdrian McCarthy 235a385d2c1SPavel Labath Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS); 23697206d57SZachary Turner Status result; 2379d6fabf9SStella Stamenova 2389d6fabf9SStella Stamenova FileSpec working_dir = launch_info.GetWorkingDirectory(); 2399d6fabf9SStella Stamenova namespace fs = llvm::sys::fs; 2409d6fabf9SStella Stamenova if (working_dir && (!working_dir.ResolvePath() || 2419d6fabf9SStella Stamenova !fs::is_directory(working_dir.GetPath()))) { 2429d6fabf9SStella Stamenova result.SetErrorStringWithFormat("No such file or directory: %s", 2439d6fabf9SStella Stamenova working_dir.GetCString()); 2449d6fabf9SStella Stamenova return result; 2459d6fabf9SStella Stamenova } 2469d6fabf9SStella Stamenova 2474ad5def9SAdrian McCarthy if (!launch_info.GetFlags().Test(eLaunchFlagDebug)) { 2484ad5def9SAdrian McCarthy StreamString stream; 2494ad5def9SAdrian McCarthy stream.Printf("ProcessWindows unable to launch '%s'. ProcessWindows can " 2504ad5def9SAdrian McCarthy "only be used for debug launches.", 2514ad5def9SAdrian McCarthy launch_info.GetExecutableFile().GetPath().c_str()); 2524ad5def9SAdrian McCarthy std::string message = stream.GetString(); 2534ad5def9SAdrian McCarthy result.SetErrorString(message.c_str()); 2544ad5def9SAdrian McCarthy 255a385d2c1SPavel Labath LLDB_LOG(log, "error: {0}", message); 2564ad5def9SAdrian McCarthy return result; 2574ad5def9SAdrian McCarthy } 2584ad5def9SAdrian McCarthy 2594ad5def9SAdrian McCarthy bool stop_at_entry = launch_info.GetFlags().Test(eLaunchFlagStopAtEntry); 2604ad5def9SAdrian McCarthy m_session_data.reset(new ProcessWindowsData(stop_at_entry)); 2614ad5def9SAdrian McCarthy 2624ad5def9SAdrian McCarthy DebugDelegateSP delegate(new LocalDebugDelegate(shared_from_this())); 2634ad5def9SAdrian McCarthy m_session_data->m_debugger.reset(new DebuggerThread(delegate)); 2644ad5def9SAdrian McCarthy DebuggerThreadSP debugger = m_session_data->m_debugger; 2654ad5def9SAdrian McCarthy 2664ad5def9SAdrian McCarthy // Kick off the DebugLaunch asynchronously and wait for it to complete. 2674ad5def9SAdrian McCarthy result = debugger->DebugLaunch(launch_info); 2684ad5def9SAdrian McCarthy if (result.Fail()) { 269a385d2c1SPavel Labath LLDB_LOG(log, "failed launching '{0}'. {1}", 270a385d2c1SPavel Labath launch_info.GetExecutableFile().GetPath(), result); 2714ad5def9SAdrian McCarthy return result; 2724ad5def9SAdrian McCarthy } 2734ad5def9SAdrian McCarthy 2744ad5def9SAdrian McCarthy HostProcess process; 27597206d57SZachary Turner Status error = WaitForDebuggerConnection(debugger, process); 2764ad5def9SAdrian McCarthy if (error.Fail()) { 277a385d2c1SPavel Labath LLDB_LOG(log, "failed launching '{0}'. {1}", 278a385d2c1SPavel Labath launch_info.GetExecutableFile().GetPath(), error); 2794ad5def9SAdrian McCarthy return error; 2804ad5def9SAdrian McCarthy } 2814ad5def9SAdrian McCarthy 282a385d2c1SPavel Labath LLDB_LOG(log, "successfully launched '{0}'", 283a385d2c1SPavel Labath launch_info.GetExecutableFile().GetPath()); 2844ad5def9SAdrian McCarthy 2854ad5def9SAdrian McCarthy // We've hit the initial stop. If eLaunchFlagsStopAtEntry was specified, the 28605097246SAdrian Prantl // private state should already be set to eStateStopped as a result of 28705097246SAdrian Prantl // hitting the initial breakpoint. If it was not set, the breakpoint should 28805097246SAdrian Prantl // have already been resumed from and the private state should already be 28905097246SAdrian Prantl // eStateRunning. 2904ad5def9SAdrian McCarthy launch_info.SetProcessID(process.GetProcessId()); 2914ad5def9SAdrian McCarthy SetID(process.GetProcessId()); 2924ad5def9SAdrian McCarthy 2934ad5def9SAdrian McCarthy return result; 2944ad5def9SAdrian McCarthy } 2954ad5def9SAdrian McCarthy 29697206d57SZachary Turner Status 29797206d57SZachary Turner ProcessWindows::DoAttachToProcessWithID(lldb::pid_t pid, 29897206d57SZachary Turner const ProcessAttachInfo &attach_info) { 299a385d2c1SPavel Labath Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS); 3004ad5def9SAdrian McCarthy m_session_data.reset( 3014ad5def9SAdrian McCarthy new ProcessWindowsData(!attach_info.GetContinueOnceAttached())); 3024ad5def9SAdrian McCarthy 3034ad5def9SAdrian McCarthy DebugDelegateSP delegate(new LocalDebugDelegate(shared_from_this())); 3044ad5def9SAdrian McCarthy DebuggerThreadSP debugger(new DebuggerThread(delegate)); 3054ad5def9SAdrian McCarthy 3064ad5def9SAdrian McCarthy m_session_data->m_debugger = debugger; 3074ad5def9SAdrian McCarthy 3084ad5def9SAdrian McCarthy DWORD process_id = static_cast<DWORD>(pid); 30997206d57SZachary Turner Status error = debugger->DebugAttach(process_id, attach_info); 3104ad5def9SAdrian McCarthy if (error.Fail()) { 311a385d2c1SPavel Labath LLDB_LOG( 312a385d2c1SPavel Labath log, 313a385d2c1SPavel Labath "encountered an error occurred initiating the asynchronous attach. {0}", 314a385d2c1SPavel Labath error); 3154ad5def9SAdrian McCarthy return error; 3164ad5def9SAdrian McCarthy } 3174ad5def9SAdrian McCarthy 3184ad5def9SAdrian McCarthy HostProcess process; 3194ad5def9SAdrian McCarthy error = WaitForDebuggerConnection(debugger, process); 3204ad5def9SAdrian McCarthy if (error.Fail()) { 321a385d2c1SPavel Labath LLDB_LOG(log, 322a385d2c1SPavel Labath "encountered an error waiting for the debugger to connect. {0}", 323a385d2c1SPavel Labath error); 3244ad5def9SAdrian McCarthy return error; 3254ad5def9SAdrian McCarthy } 3264ad5def9SAdrian McCarthy 327a385d2c1SPavel Labath LLDB_LOG(log, "successfully attached to process with pid={0}", process_id); 3284ad5def9SAdrian McCarthy 3294ad5def9SAdrian McCarthy // We've hit the initial stop. If eLaunchFlagsStopAtEntry was specified, the 33005097246SAdrian Prantl // private state should already be set to eStateStopped as a result of 33105097246SAdrian Prantl // hitting the initial breakpoint. If it was not set, the breakpoint should 33205097246SAdrian Prantl // have already been resumed from and the private state should already be 33305097246SAdrian Prantl // eStateRunning. 3344ad5def9SAdrian McCarthy SetID(process.GetProcessId()); 3354ad5def9SAdrian McCarthy return error; 3364ad5def9SAdrian McCarthy } 3374ad5def9SAdrian McCarthy 33897206d57SZachary Turner Status ProcessWindows::DoResume() { 339a385d2c1SPavel Labath Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS); 3404ad5def9SAdrian McCarthy llvm::sys::ScopedLock lock(m_mutex); 34197206d57SZachary Turner Status error; 3424ad5def9SAdrian McCarthy 3434ad5def9SAdrian McCarthy StateType private_state = GetPrivateState(); 3444ad5def9SAdrian McCarthy if (private_state == eStateStopped || private_state == eStateCrashed) { 345a385d2c1SPavel Labath LLDB_LOG(log, "process {0} is in state {1}. Resuming...", 3464ad5def9SAdrian McCarthy m_session_data->m_debugger->GetProcess().GetProcessId(), 3474ad5def9SAdrian McCarthy GetPrivateState()); 3484ad5def9SAdrian McCarthy 3494ad5def9SAdrian McCarthy ExceptionRecordSP active_exception = 3504ad5def9SAdrian McCarthy m_session_data->m_debugger->GetActiveException().lock(); 3514ad5def9SAdrian McCarthy if (active_exception) { 35205097246SAdrian Prantl // Resume the process and continue processing debug events. Mask the 35305097246SAdrian Prantl // exception so that from the process's view, there is no indication that 35405097246SAdrian Prantl // anything happened. 3554ad5def9SAdrian McCarthy m_session_data->m_debugger->ContinueAsyncException( 3564ad5def9SAdrian McCarthy ExceptionResult::MaskException); 3574ad5def9SAdrian McCarthy } 3584ad5def9SAdrian McCarthy 359a385d2c1SPavel Labath LLDB_LOG(log, "resuming {0} threads.", m_thread_list.GetSize()); 3604ad5def9SAdrian McCarthy 3610fd67b53SStella Stamenova bool failed = false; 3624ad5def9SAdrian McCarthy for (uint32_t i = 0; i < m_thread_list.GetSize(); ++i) { 3634ad5def9SAdrian McCarthy auto thread = std::static_pointer_cast<TargetThreadWindows>( 3644ad5def9SAdrian McCarthy m_thread_list.GetThreadAtIndex(i)); 3650fd67b53SStella Stamenova Status result = thread->DoResume(); 3660fd67b53SStella Stamenova if (result.Fail()) { 3670fd67b53SStella Stamenova failed = true; 36862c76db4SStella Stamenova LLDB_LOG( 36962c76db4SStella Stamenova log, 37062c76db4SStella Stamenova "Trying to resume thread at index {0}, but failed with error {1}.", 37162c76db4SStella Stamenova i, result); 3720fd67b53SStella Stamenova } 3734ad5def9SAdrian McCarthy } 3744ad5def9SAdrian McCarthy 3750fd67b53SStella Stamenova if (failed) { 3760fd67b53SStella Stamenova error.SetErrorString("ProcessWindows::DoResume failed"); 3770fd67b53SStella Stamenova return error; 3780fd67b53SStella Stamenova } else { 3794ad5def9SAdrian McCarthy SetPrivateState(eStateRunning); 3800fd67b53SStella Stamenova } 3814ad5def9SAdrian McCarthy } else { 382a385d2c1SPavel Labath LLDB_LOG(log, "error: process %I64u is in state %u. Returning...", 3834ad5def9SAdrian McCarthy m_session_data->m_debugger->GetProcess().GetProcessId(), 3844ad5def9SAdrian McCarthy GetPrivateState()); 3854ad5def9SAdrian McCarthy } 3864ad5def9SAdrian McCarthy return error; 3874ad5def9SAdrian McCarthy } 3884ad5def9SAdrian McCarthy 38997206d57SZachary Turner Status ProcessWindows::DoDestroy() { 390a385d2c1SPavel Labath Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS); 3914ad5def9SAdrian McCarthy DebuggerThreadSP debugger_thread; 3924ad5def9SAdrian McCarthy StateType private_state; 3934ad5def9SAdrian McCarthy { 3944ad5def9SAdrian McCarthy // Acquire this lock inside an inner scope, only long enough to get the 39505097246SAdrian Prantl // DebuggerThread. StopDebugging() will trigger a call back into 39605097246SAdrian Prantl // ProcessWindows which will acquire the lock again, so we need to not 39705097246SAdrian Prantl // deadlock. 3984ad5def9SAdrian McCarthy llvm::sys::ScopedLock lock(m_mutex); 3994ad5def9SAdrian McCarthy 4004ad5def9SAdrian McCarthy private_state = GetPrivateState(); 4014ad5def9SAdrian McCarthy 4024ad5def9SAdrian McCarthy if (!m_session_data) { 403a385d2c1SPavel Labath LLDB_LOG(log, "warning: state = {0}, but there is no active session.", 4044ad5def9SAdrian McCarthy private_state); 40597206d57SZachary Turner return Status(); 4064ad5def9SAdrian McCarthy } 4074ad5def9SAdrian McCarthy 4084ad5def9SAdrian McCarthy debugger_thread = m_session_data->m_debugger; 4094ad5def9SAdrian McCarthy } 4104ad5def9SAdrian McCarthy 41197206d57SZachary Turner Status error; 4124ad5def9SAdrian McCarthy if (private_state != eStateExited && private_state != eStateDetached) { 413a385d2c1SPavel Labath LLDB_LOG(log, "Shutting down process {0} while state = {1}.", 4144ad5def9SAdrian McCarthy debugger_thread->GetProcess().GetNativeProcess().GetSystemHandle(), 4154ad5def9SAdrian McCarthy private_state); 4164ad5def9SAdrian McCarthy error = debugger_thread->StopDebugging(true); 4174ad5def9SAdrian McCarthy 4184ad5def9SAdrian McCarthy // By the time StopDebugging returns, there is no more debugger thread, so 4194ad5def9SAdrian McCarthy // we can be assured that no other thread will race for the session data. 4204ad5def9SAdrian McCarthy m_session_data.reset(); 4214ad5def9SAdrian McCarthy } else { 422a385d2c1SPavel Labath LLDB_LOG(log, "cannot destroy process {0} while state = {1}", 4234ad5def9SAdrian McCarthy debugger_thread->GetProcess().GetNativeProcess().GetSystemHandle(), 4244ad5def9SAdrian McCarthy private_state); 4254ad5def9SAdrian McCarthy } 4264ad5def9SAdrian McCarthy 4274ad5def9SAdrian McCarthy return error; 4284ad5def9SAdrian McCarthy } 4294ad5def9SAdrian McCarthy 43097206d57SZachary Turner Status ProcessWindows::DoHalt(bool &caused_stop) { 431a385d2c1SPavel Labath Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS); 43297206d57SZachary Turner Status error; 4334ad5def9SAdrian McCarthy StateType state = GetPrivateState(); 4344ad5def9SAdrian McCarthy if (state == eStateStopped) 4354ad5def9SAdrian McCarthy caused_stop = false; 4364ad5def9SAdrian McCarthy else { 4374ad5def9SAdrian McCarthy llvm::sys::ScopedLock lock(m_mutex); 4384ad5def9SAdrian McCarthy caused_stop = ::DebugBreakProcess(m_session_data->m_debugger->GetProcess() 4394ad5def9SAdrian McCarthy .GetNativeProcess() 4404ad5def9SAdrian McCarthy .GetSystemHandle()); 4414ad5def9SAdrian McCarthy if (!caused_stop) { 4424ad5def9SAdrian McCarthy error.SetError(::GetLastError(), eErrorTypeWin32); 443a385d2c1SPavel Labath LLDB_LOG(log, "DebugBreakProcess failed with error {0}", error); 4444ad5def9SAdrian McCarthy } 4454ad5def9SAdrian McCarthy } 4464ad5def9SAdrian McCarthy return error; 4474ad5def9SAdrian McCarthy } 4484ad5def9SAdrian McCarthy 4494ad5def9SAdrian McCarthy void ProcessWindows::DidLaunch() { 4504ad5def9SAdrian McCarthy ArchSpec arch_spec; 4514ad5def9SAdrian McCarthy DidAttach(arch_spec); 4524ad5def9SAdrian McCarthy } 4534ad5def9SAdrian McCarthy 4544ad5def9SAdrian McCarthy void ProcessWindows::DidAttach(ArchSpec &arch_spec) { 4554ad5def9SAdrian McCarthy llvm::sys::ScopedLock lock(m_mutex); 4564ad5def9SAdrian McCarthy 4574ad5def9SAdrian McCarthy // The initial stop won't broadcast the state change event, so account for 4584ad5def9SAdrian McCarthy // that here. 4594ad5def9SAdrian McCarthy if (m_session_data && GetPrivateState() == eStateStopped && 4604ad5def9SAdrian McCarthy m_session_data->m_stop_at_entry) 4614ad5def9SAdrian McCarthy RefreshStateAfterStop(); 4624ad5def9SAdrian McCarthy } 4634ad5def9SAdrian McCarthy 4644ad5def9SAdrian McCarthy void ProcessWindows::RefreshStateAfterStop() { 465a385d2c1SPavel Labath Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EXCEPTION); 4664ad5def9SAdrian McCarthy llvm::sys::ScopedLock lock(m_mutex); 4674ad5def9SAdrian McCarthy 4684ad5def9SAdrian McCarthy if (!m_session_data) { 469a385d2c1SPavel Labath LLDB_LOG(log, "no active session. Returning..."); 4704ad5def9SAdrian McCarthy return; 4714ad5def9SAdrian McCarthy } 4724ad5def9SAdrian McCarthy 4734ad5def9SAdrian McCarthy m_thread_list.RefreshStateAfterStop(); 4744ad5def9SAdrian McCarthy 4754ad5def9SAdrian McCarthy std::weak_ptr<ExceptionRecord> exception_record = 4764ad5def9SAdrian McCarthy m_session_data->m_debugger->GetActiveException(); 4774ad5def9SAdrian McCarthy ExceptionRecordSP active_exception = exception_record.lock(); 4784ad5def9SAdrian McCarthy if (!active_exception) { 47962c76db4SStella Stamenova LLDB_LOG(log, 48062c76db4SStella Stamenova "there is no active exception in process {0}. Why is the " 481a385d2c1SPavel Labath "process stopped?", 4824ad5def9SAdrian McCarthy m_session_data->m_debugger->GetProcess().GetProcessId()); 4834ad5def9SAdrian McCarthy return; 4844ad5def9SAdrian McCarthy } 4854ad5def9SAdrian McCarthy 4864ad5def9SAdrian McCarthy StopInfoSP stop_info; 4874ad5def9SAdrian McCarthy m_thread_list.SetSelectedThreadByID(active_exception->GetThreadID()); 4884ad5def9SAdrian McCarthy ThreadSP stop_thread = m_thread_list.GetSelectedThread(); 4894ad5def9SAdrian McCarthy if (!stop_thread) 4904ad5def9SAdrian McCarthy return; 4914ad5def9SAdrian McCarthy 4924ad5def9SAdrian McCarthy switch (active_exception->GetExceptionCode()) { 4934ad5def9SAdrian McCarthy case EXCEPTION_SINGLE_STEP: { 4944ad5def9SAdrian McCarthy RegisterContextSP register_context = stop_thread->GetRegisterContext(); 4954ad5def9SAdrian McCarthy const uint64_t pc = register_context->GetPC(); 4964ad5def9SAdrian McCarthy BreakpointSiteSP site(GetBreakpointSiteList().FindByAddress(pc)); 4974ad5def9SAdrian McCarthy if (site && site->ValidForThisThread(stop_thread.get())) { 49862c76db4SStella Stamenova LLDB_LOG(log, 49962c76db4SStella Stamenova "Single-stepped onto a breakpoint in process {0} at " 500a385d2c1SPavel Labath "address {1:x} with breakpoint site {2}", 5014ad5def9SAdrian McCarthy m_session_data->m_debugger->GetProcess().GetProcessId(), pc, 5024ad5def9SAdrian McCarthy site->GetID()); 5034ad5def9SAdrian McCarthy stop_info = StopInfo::CreateStopReasonWithBreakpointSiteID(*stop_thread, 5044ad5def9SAdrian McCarthy site->GetID()); 5054ad5def9SAdrian McCarthy stop_thread->SetStopInfo(stop_info); 5064ad5def9SAdrian McCarthy } else { 507a385d2c1SPavel Labath LLDB_LOG(log, "single stepping thread {0}", stop_thread->GetID()); 5084ad5def9SAdrian McCarthy stop_info = StopInfo::CreateStopReasonToTrace(*stop_thread); 5094ad5def9SAdrian McCarthy stop_thread->SetStopInfo(stop_info); 5104ad5def9SAdrian McCarthy } 5114ad5def9SAdrian McCarthy return; 5124ad5def9SAdrian McCarthy } 5134ad5def9SAdrian McCarthy 5144ad5def9SAdrian McCarthy case EXCEPTION_BREAKPOINT: { 5154ad5def9SAdrian McCarthy RegisterContextSP register_context = stop_thread->GetRegisterContext(); 5164ad5def9SAdrian McCarthy 5174ad5def9SAdrian McCarthy // The current EIP is AFTER the BP opcode, which is one byte. 5184ad5def9SAdrian McCarthy uint64_t pc = register_context->GetPC() - 1; 5194ad5def9SAdrian McCarthy 5204ad5def9SAdrian McCarthy BreakpointSiteSP site(GetBreakpointSiteList().FindByAddress(pc)); 5214ad5def9SAdrian McCarthy if (site) { 52262c76db4SStella Stamenova LLDB_LOG(log, 52362c76db4SStella Stamenova "detected breakpoint in process {0} at address {1:x} with " 524a385d2c1SPavel Labath "breakpoint site {2}", 5254ad5def9SAdrian McCarthy m_session_data->m_debugger->GetProcess().GetProcessId(), pc, 5264ad5def9SAdrian McCarthy site->GetID()); 5274ad5def9SAdrian McCarthy 5284ad5def9SAdrian McCarthy if (site->ValidForThisThread(stop_thread.get())) { 52962c76db4SStella Stamenova LLDB_LOG(log, 53062c76db4SStella Stamenova "Breakpoint site {0} is valid for this thread ({1:x}), " 5314ad5def9SAdrian McCarthy "creating stop info.", 5324ad5def9SAdrian McCarthy site->GetID(), stop_thread->GetID()); 5334ad5def9SAdrian McCarthy 5344ad5def9SAdrian McCarthy stop_info = StopInfo::CreateStopReasonWithBreakpointSiteID( 5354ad5def9SAdrian McCarthy *stop_thread, site->GetID()); 5364ad5def9SAdrian McCarthy register_context->SetPC(pc); 5374ad5def9SAdrian McCarthy } else { 53862c76db4SStella Stamenova LLDB_LOG(log, 53962c76db4SStella Stamenova "Breakpoint site {0} is not valid for this thread, " 5404ad5def9SAdrian McCarthy "creating empty stop info.", 5414ad5def9SAdrian McCarthy site->GetID()); 5424ad5def9SAdrian McCarthy } 5434ad5def9SAdrian McCarthy stop_thread->SetStopInfo(stop_info); 5444ad5def9SAdrian McCarthy return; 5454ad5def9SAdrian McCarthy } else { 5464ad5def9SAdrian McCarthy // The thread hit a hard-coded breakpoint like an `int 3` or 5474ad5def9SAdrian McCarthy // `__debugbreak()`. 548a385d2c1SPavel Labath LLDB_LOG(log, 5494ad5def9SAdrian McCarthy "No breakpoint site matches for this thread. __debugbreak()? " 5504ad5def9SAdrian McCarthy "Creating stop info with the exception."); 5514ad5def9SAdrian McCarthy // FALLTHROUGH: We'll treat this as a generic exception record in the 5524ad5def9SAdrian McCarthy // default case. 5534ad5def9SAdrian McCarthy } 5544ad5def9SAdrian McCarthy } 5554ad5def9SAdrian McCarthy 5564ad5def9SAdrian McCarthy default: { 5574ad5def9SAdrian McCarthy std::string desc; 5584ad5def9SAdrian McCarthy llvm::raw_string_ostream desc_stream(desc); 5594ad5def9SAdrian McCarthy desc_stream << "Exception " 5604ad5def9SAdrian McCarthy << llvm::format_hex(active_exception->GetExceptionCode(), 8) 5614ad5def9SAdrian McCarthy << " encountered at address " 5624ad5def9SAdrian McCarthy << llvm::format_hex(active_exception->GetExceptionAddress(), 8); 5634ad5def9SAdrian McCarthy stop_info = StopInfo::CreateStopReasonWithException( 5644ad5def9SAdrian McCarthy *stop_thread, desc_stream.str().c_str()); 5654ad5def9SAdrian McCarthy stop_thread->SetStopInfo(stop_info); 566a385d2c1SPavel Labath LLDB_LOG(log, "{0}", desc_stream.str()); 5674ad5def9SAdrian McCarthy return; 5684ad5def9SAdrian McCarthy } 5694ad5def9SAdrian McCarthy } 5704ad5def9SAdrian McCarthy } 5714ad5def9SAdrian McCarthy 5724ad5def9SAdrian McCarthy bool ProcessWindows::CanDebug(lldb::TargetSP target_sp, 5734ad5def9SAdrian McCarthy bool plugin_specified_by_name) { 5744ad5def9SAdrian McCarthy if (plugin_specified_by_name) 5754ad5def9SAdrian McCarthy return true; 5764ad5def9SAdrian McCarthy 5774ad5def9SAdrian McCarthy // For now we are just making sure the file exists for a given module 5784ad5def9SAdrian McCarthy ModuleSP exe_module_sp(target_sp->GetExecutableModule()); 5794ad5def9SAdrian McCarthy if (exe_module_sp.get()) 5804ad5def9SAdrian McCarthy return exe_module_sp->GetFileSpec().Exists(); 58105097246SAdrian Prantl // However, if there is no executable module, we return true since we might 58205097246SAdrian Prantl // be preparing to attach. 5834ad5def9SAdrian McCarthy return true; 5844ad5def9SAdrian McCarthy } 5854ad5def9SAdrian McCarthy 5864ad5def9SAdrian McCarthy bool ProcessWindows::UpdateThreadList(ThreadList &old_thread_list, 5874ad5def9SAdrian McCarthy ThreadList &new_thread_list) { 588a385d2c1SPavel Labath Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_THREAD); 5894ad5def9SAdrian McCarthy // Add all the threads that were previously running and for which we did not 5904ad5def9SAdrian McCarthy // detect a thread exited event. 5914ad5def9SAdrian McCarthy int new_size = 0; 5924ad5def9SAdrian McCarthy int continued_threads = 0; 5934ad5def9SAdrian McCarthy int exited_threads = 0; 5944ad5def9SAdrian McCarthy int new_threads = 0; 5954ad5def9SAdrian McCarthy 5964ad5def9SAdrian McCarthy for (ThreadSP old_thread : old_thread_list.Threads()) { 5974ad5def9SAdrian McCarthy lldb::tid_t old_thread_id = old_thread->GetID(); 5984ad5def9SAdrian McCarthy auto exited_thread_iter = 5994ad5def9SAdrian McCarthy m_session_data->m_exited_threads.find(old_thread_id); 6004ad5def9SAdrian McCarthy if (exited_thread_iter == m_session_data->m_exited_threads.end()) { 6014ad5def9SAdrian McCarthy new_thread_list.AddThread(old_thread); 6024ad5def9SAdrian McCarthy ++new_size; 6034ad5def9SAdrian McCarthy ++continued_threads; 604a385d2c1SPavel Labath LLDB_LOGV(log, "Thread {0} was running and is still running.", 6054ad5def9SAdrian McCarthy old_thread_id); 6064ad5def9SAdrian McCarthy } else { 607a385d2c1SPavel Labath LLDB_LOGV(log, "Thread {0} was running and has exited.", old_thread_id); 6084ad5def9SAdrian McCarthy ++exited_threads; 6094ad5def9SAdrian McCarthy } 6104ad5def9SAdrian McCarthy } 6114ad5def9SAdrian McCarthy 61205097246SAdrian Prantl // Also add all the threads that are new since the last time we broke into 61305097246SAdrian Prantl // the debugger. 6144ad5def9SAdrian McCarthy for (const auto &thread_info : m_session_data->m_new_threads) { 6154ad5def9SAdrian McCarthy ThreadSP thread(new TargetThreadWindows(*this, thread_info.second)); 6164ad5def9SAdrian McCarthy thread->SetID(thread_info.first); 6174ad5def9SAdrian McCarthy new_thread_list.AddThread(thread); 6184ad5def9SAdrian McCarthy ++new_size; 6194ad5def9SAdrian McCarthy ++new_threads; 620a385d2c1SPavel Labath LLDB_LOGV(log, "Thread {0} is new since last update.", thread_info.first); 6214ad5def9SAdrian McCarthy } 6224ad5def9SAdrian McCarthy 623a385d2c1SPavel Labath LLDB_LOG(log, "{0} new threads, {1} old threads, {2} exited threads.", 6244ad5def9SAdrian McCarthy new_threads, continued_threads, exited_threads); 6254ad5def9SAdrian McCarthy 6264ad5def9SAdrian McCarthy m_session_data->m_new_threads.clear(); 6274ad5def9SAdrian McCarthy m_session_data->m_exited_threads.clear(); 6284ad5def9SAdrian McCarthy 6294ad5def9SAdrian McCarthy return new_size > 0; 6304ad5def9SAdrian McCarthy } 6314ad5def9SAdrian McCarthy 6324ad5def9SAdrian McCarthy bool ProcessWindows::IsAlive() { 6334ad5def9SAdrian McCarthy StateType state = GetPrivateState(); 6344ad5def9SAdrian McCarthy switch (state) { 6354ad5def9SAdrian McCarthy case eStateCrashed: 6364ad5def9SAdrian McCarthy case eStateDetached: 6374ad5def9SAdrian McCarthy case eStateUnloaded: 6384ad5def9SAdrian McCarthy case eStateExited: 6394ad5def9SAdrian McCarthy case eStateInvalid: 6404ad5def9SAdrian McCarthy return false; 6414ad5def9SAdrian McCarthy default: 6424ad5def9SAdrian McCarthy return true; 6434ad5def9SAdrian McCarthy } 6444ad5def9SAdrian McCarthy } 6454ad5def9SAdrian McCarthy 6464ad5def9SAdrian McCarthy size_t ProcessWindows::DoReadMemory(lldb::addr_t vm_addr, void *buf, 64797206d57SZachary Turner size_t size, Status &error) { 648a385d2c1SPavel Labath Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_MEMORY); 6494ad5def9SAdrian McCarthy llvm::sys::ScopedLock lock(m_mutex); 6504ad5def9SAdrian McCarthy 6514ad5def9SAdrian McCarthy if (!m_session_data) 6524ad5def9SAdrian McCarthy return 0; 6534ad5def9SAdrian McCarthy 654a385d2c1SPavel Labath LLDB_LOG(log, "attempting to read {0} bytes from address {1:x}", size, 655a385d2c1SPavel Labath vm_addr); 6564ad5def9SAdrian McCarthy 6574ad5def9SAdrian McCarthy HostProcess process = m_session_data->m_debugger->GetProcess(); 6584ad5def9SAdrian McCarthy void *addr = reinterpret_cast<void *>(vm_addr); 6594ad5def9SAdrian McCarthy SIZE_T bytes_read = 0; 6604ad5def9SAdrian McCarthy if (!ReadProcessMemory(process.GetNativeProcess().GetSystemHandle(), addr, 6614ad5def9SAdrian McCarthy buf, size, &bytes_read)) { 66262c76db4SStella Stamenova // Reading from the process can fail for a number of reasons - set the 66362c76db4SStella Stamenova // error code and make sure that the number of bytes read is set back to 0 66462c76db4SStella Stamenova // because in some scenarios the value of bytes_read returned from the API 66562c76db4SStella Stamenova // is garbage. 6664ad5def9SAdrian McCarthy error.SetError(GetLastError(), eErrorTypeWin32); 667a385d2c1SPavel Labath LLDB_LOG(log, "reading failed with error: {0}", error); 66862c76db4SStella Stamenova bytes_read = 0; 6694ad5def9SAdrian McCarthy } 6704ad5def9SAdrian McCarthy return bytes_read; 6714ad5def9SAdrian McCarthy } 6724ad5def9SAdrian McCarthy 6734ad5def9SAdrian McCarthy size_t ProcessWindows::DoWriteMemory(lldb::addr_t vm_addr, const void *buf, 67497206d57SZachary Turner size_t size, Status &error) { 675a385d2c1SPavel Labath Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_MEMORY); 6764ad5def9SAdrian McCarthy llvm::sys::ScopedLock lock(m_mutex); 677a385d2c1SPavel Labath LLDB_LOG(log, "attempting to write {0} bytes into address {1:x}", size, 6784ad5def9SAdrian McCarthy vm_addr); 6794ad5def9SAdrian McCarthy 6804ad5def9SAdrian McCarthy if (!m_session_data) { 681a385d2c1SPavel Labath LLDB_LOG(log, "cannot write, there is no active debugger connection."); 6824ad5def9SAdrian McCarthy return 0; 6834ad5def9SAdrian McCarthy } 6844ad5def9SAdrian McCarthy 6854ad5def9SAdrian McCarthy HostProcess process = m_session_data->m_debugger->GetProcess(); 6864ad5def9SAdrian McCarthy void *addr = reinterpret_cast<void *>(vm_addr); 6874ad5def9SAdrian McCarthy SIZE_T bytes_written = 0; 6884ad5def9SAdrian McCarthy lldb::process_t handle = process.GetNativeProcess().GetSystemHandle(); 6894ad5def9SAdrian McCarthy if (WriteProcessMemory(handle, addr, buf, size, &bytes_written)) 6904ad5def9SAdrian McCarthy FlushInstructionCache(handle, addr, bytes_written); 6914ad5def9SAdrian McCarthy else { 6924ad5def9SAdrian McCarthy error.SetError(GetLastError(), eErrorTypeWin32); 693a385d2c1SPavel Labath LLDB_LOG(log, "writing failed with error: {0}", error); 6944ad5def9SAdrian McCarthy } 6954ad5def9SAdrian McCarthy return bytes_written; 6964ad5def9SAdrian McCarthy } 6974ad5def9SAdrian McCarthy 69897206d57SZachary Turner Status ProcessWindows::GetMemoryRegionInfo(lldb::addr_t vm_addr, 6994ad5def9SAdrian McCarthy MemoryRegionInfo &info) { 700a385d2c1SPavel Labath Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_MEMORY); 70197206d57SZachary Turner Status error; 7024ad5def9SAdrian McCarthy llvm::sys::ScopedLock lock(m_mutex); 7034ad5def9SAdrian McCarthy info.Clear(); 7044ad5def9SAdrian McCarthy 7054ad5def9SAdrian McCarthy if (!m_session_data) { 7064ad5def9SAdrian McCarthy error.SetErrorString( 7074ad5def9SAdrian McCarthy "GetMemoryRegionInfo called with no debugging session."); 708a385d2c1SPavel Labath LLDB_LOG(log, "error: {0}", error); 7094ad5def9SAdrian McCarthy return error; 7104ad5def9SAdrian McCarthy } 7114ad5def9SAdrian McCarthy HostProcess process = m_session_data->m_debugger->GetProcess(); 7124ad5def9SAdrian McCarthy lldb::process_t handle = process.GetNativeProcess().GetSystemHandle(); 7134ad5def9SAdrian McCarthy if (handle == nullptr || handle == LLDB_INVALID_PROCESS) { 7144ad5def9SAdrian McCarthy error.SetErrorString( 7154ad5def9SAdrian McCarthy "GetMemoryRegionInfo called with an invalid target process."); 716a385d2c1SPavel Labath LLDB_LOG(log, "error: {0}", error); 7174ad5def9SAdrian McCarthy return error; 7184ad5def9SAdrian McCarthy } 7194ad5def9SAdrian McCarthy 720a385d2c1SPavel Labath LLDB_LOG(log, "getting info for address {0:x}", vm_addr); 7214ad5def9SAdrian McCarthy 7224ad5def9SAdrian McCarthy void *addr = reinterpret_cast<void *>(vm_addr); 7234ad5def9SAdrian McCarthy MEMORY_BASIC_INFORMATION mem_info = {}; 7244ad5def9SAdrian McCarthy SIZE_T result = ::VirtualQueryEx(handle, addr, &mem_info, sizeof(mem_info)); 7254ad5def9SAdrian McCarthy if (result == 0) { 7264ad5def9SAdrian McCarthy if (::GetLastError() == ERROR_INVALID_PARAMETER) { 72705097246SAdrian Prantl // ERROR_INVALID_PARAMETER is returned if VirtualQueryEx is called with 72805097246SAdrian Prantl // an address past the highest accessible address. We should return a 72905097246SAdrian Prantl // range from the vm_addr to LLDB_INVALID_ADDRESS 7304ad5def9SAdrian McCarthy info.GetRange().SetRangeBase(vm_addr); 7314ad5def9SAdrian McCarthy info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS); 7324ad5def9SAdrian McCarthy info.SetReadable(MemoryRegionInfo::eNo); 7334ad5def9SAdrian McCarthy info.SetExecutable(MemoryRegionInfo::eNo); 7344ad5def9SAdrian McCarthy info.SetWritable(MemoryRegionInfo::eNo); 7354ad5def9SAdrian McCarthy info.SetMapped(MemoryRegionInfo::eNo); 7364ad5def9SAdrian McCarthy return error; 7374ad5def9SAdrian McCarthy } else { 7384ad5def9SAdrian McCarthy error.SetError(::GetLastError(), eErrorTypeWin32); 73962c76db4SStella Stamenova LLDB_LOG(log, 74062c76db4SStella Stamenova "VirtualQueryEx returned error {0} while getting memory " 741a385d2c1SPavel Labath "region info for address {1:x}", 742a385d2c1SPavel Labath error, vm_addr); 7434ad5def9SAdrian McCarthy return error; 7444ad5def9SAdrian McCarthy } 7454ad5def9SAdrian McCarthy } 7464ad5def9SAdrian McCarthy 7474ad5def9SAdrian McCarthy // Protect bits are only valid for MEM_COMMIT regions. 7484ad5def9SAdrian McCarthy if (mem_info.State == MEM_COMMIT) { 7494ad5def9SAdrian McCarthy const bool readable = IsPageReadable(mem_info.Protect); 7504ad5def9SAdrian McCarthy const bool executable = IsPageExecutable(mem_info.Protect); 7514ad5def9SAdrian McCarthy const bool writable = IsPageWritable(mem_info.Protect); 7524ad5def9SAdrian McCarthy info.SetReadable(readable ? MemoryRegionInfo::eYes : MemoryRegionInfo::eNo); 7534ad5def9SAdrian McCarthy info.SetExecutable(executable ? MemoryRegionInfo::eYes 7544ad5def9SAdrian McCarthy : MemoryRegionInfo::eNo); 7554ad5def9SAdrian McCarthy info.SetWritable(writable ? MemoryRegionInfo::eYes : MemoryRegionInfo::eNo); 7564ad5def9SAdrian McCarthy } else { 7574ad5def9SAdrian McCarthy info.SetReadable(MemoryRegionInfo::eNo); 7584ad5def9SAdrian McCarthy info.SetExecutable(MemoryRegionInfo::eNo); 7594ad5def9SAdrian McCarthy info.SetWritable(MemoryRegionInfo::eNo); 7604ad5def9SAdrian McCarthy } 7614ad5def9SAdrian McCarthy 7624ad5def9SAdrian McCarthy // AllocationBase is defined for MEM_COMMIT and MEM_RESERVE but not MEM_FREE. 7634ad5def9SAdrian McCarthy if (mem_info.State != MEM_FREE) { 7644ad5def9SAdrian McCarthy info.GetRange().SetRangeBase( 7654ad5def9SAdrian McCarthy reinterpret_cast<addr_t>(mem_info.AllocationBase)); 7664ad5def9SAdrian McCarthy info.GetRange().SetRangeEnd(reinterpret_cast<addr_t>(mem_info.BaseAddress) + 7674ad5def9SAdrian McCarthy mem_info.RegionSize); 7684ad5def9SAdrian McCarthy info.SetMapped(MemoryRegionInfo::eYes); 7694ad5def9SAdrian McCarthy } else { 7704ad5def9SAdrian McCarthy // In the unmapped case we need to return the distance to the next block of 77105097246SAdrian Prantl // memory. VirtualQueryEx nearly does that except that it gives the 77205097246SAdrian Prantl // distance from the start of the page containing vm_addr. 7734ad5def9SAdrian McCarthy SYSTEM_INFO data; 7744ad5def9SAdrian McCarthy GetSystemInfo(&data); 7754ad5def9SAdrian McCarthy DWORD page_offset = vm_addr % data.dwPageSize; 7764ad5def9SAdrian McCarthy info.GetRange().SetRangeBase(vm_addr); 7774ad5def9SAdrian McCarthy info.GetRange().SetByteSize(mem_info.RegionSize - page_offset); 7784ad5def9SAdrian McCarthy info.SetMapped(MemoryRegionInfo::eNo); 7794ad5def9SAdrian McCarthy } 7804ad5def9SAdrian McCarthy 7814ad5def9SAdrian McCarthy error.SetError(::GetLastError(), eErrorTypeWin32); 78262c76db4SStella Stamenova LLDB_LOGV(log, 78362c76db4SStella Stamenova "Memory region info for address {0}: readable={1}, " 784a385d2c1SPavel Labath "executable={2}, writable={3}", 785a385d2c1SPavel Labath vm_addr, info.GetReadable(), info.GetExecutable(), 786a385d2c1SPavel Labath info.GetWritable()); 7874ad5def9SAdrian McCarthy return error; 7884ad5def9SAdrian McCarthy } 7894ad5def9SAdrian McCarthy 790b9c1b51eSKate Stone lldb::addr_t ProcessWindows::GetImageInfoAddress() { 79118a9135dSAdrian McCarthy Target &target = GetTarget(); 79218a9135dSAdrian McCarthy ObjectFile *obj_file = target.GetExecutableModule()->GetObjectFile(); 79318a9135dSAdrian McCarthy Address addr = obj_file->GetImageInfoAddress(&target); 79418a9135dSAdrian McCarthy if (addr.IsValid()) 79518a9135dSAdrian McCarthy return addr.GetLoadAddress(&target); 79618a9135dSAdrian McCarthy else 79718a9135dSAdrian McCarthy return LLDB_INVALID_ADDRESS; 79818a9135dSAdrian McCarthy } 79918a9135dSAdrian McCarthy 8004ad5def9SAdrian McCarthy void ProcessWindows::OnExitProcess(uint32_t exit_code) { 8014ad5def9SAdrian McCarthy // No need to acquire the lock since m_session_data isn't accessed. 802a385d2c1SPavel Labath Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS); 803a385d2c1SPavel Labath LLDB_LOG(log, "Process {0} exited with code {1}", GetID(), exit_code); 8044ad5def9SAdrian McCarthy 805d7e126c4SJim Ingham TargetSP target = CalculateTarget(); 8064ad5def9SAdrian McCarthy if (target) { 8074ad5def9SAdrian McCarthy ModuleSP executable_module = target->GetExecutableModule(); 8084ad5def9SAdrian McCarthy ModuleList unloaded_modules; 8094ad5def9SAdrian McCarthy unloaded_modules.Append(executable_module); 8104ad5def9SAdrian McCarthy target->ModulesDidUnload(unloaded_modules, true); 8114ad5def9SAdrian McCarthy } 8124ad5def9SAdrian McCarthy 8134ad5def9SAdrian McCarthy SetProcessExitStatus(GetID(), true, 0, exit_code); 8144ad5def9SAdrian McCarthy SetPrivateState(eStateExited); 8154ad5def9SAdrian McCarthy } 8164ad5def9SAdrian McCarthy 8174ad5def9SAdrian McCarthy void ProcessWindows::OnDebuggerConnected(lldb::addr_t image_base) { 8184ad5def9SAdrian McCarthy DebuggerThreadSP debugger = m_session_data->m_debugger; 819a385d2c1SPavel Labath Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS); 820a385d2c1SPavel Labath LLDB_LOG(log, "Debugger connected to process {0}. Image base = {1:x}", 8214ad5def9SAdrian McCarthy debugger->GetProcess().GetProcessId(), image_base); 8224ad5def9SAdrian McCarthy 8234ad5def9SAdrian McCarthy ModuleSP module = GetTarget().GetExecutableModule(); 8244ad5def9SAdrian McCarthy if (!module) { 8254ad5def9SAdrian McCarthy // During attach, we won't have the executable module, so find it now. 8264ad5def9SAdrian McCarthy const DWORD pid = debugger->GetProcess().GetProcessId(); 8274ad5def9SAdrian McCarthy const std::string file_name = GetProcessExecutableName(pid); 8284ad5def9SAdrian McCarthy if (file_name.empty()) { 8294ad5def9SAdrian McCarthy return; 8304ad5def9SAdrian McCarthy } 8314ad5def9SAdrian McCarthy 8324ad5def9SAdrian McCarthy FileSpec executable_file(file_name, true); 8334ad5def9SAdrian McCarthy ModuleSpec module_spec(executable_file); 83497206d57SZachary Turner Status error; 8354ad5def9SAdrian McCarthy module = GetTarget().GetSharedModule(module_spec, &error); 8364ad5def9SAdrian McCarthy if (!module) { 8374ad5def9SAdrian McCarthy return; 8384ad5def9SAdrian McCarthy } 8394ad5def9SAdrian McCarthy 840*d54ee88aSTatyana Krasnukha GetTarget().SetExecutableModule(module, eLoadDependentsNo); 8414ad5def9SAdrian McCarthy } 8424ad5def9SAdrian McCarthy 8434ad5def9SAdrian McCarthy bool load_addr_changed; 8444ad5def9SAdrian McCarthy module->SetLoadAddress(GetTarget(), image_base, false, load_addr_changed); 8454ad5def9SAdrian McCarthy 8464ad5def9SAdrian McCarthy ModuleList loaded_modules; 8474ad5def9SAdrian McCarthy loaded_modules.Append(module); 8484ad5def9SAdrian McCarthy GetTarget().ModulesDidLoad(loaded_modules); 8494ad5def9SAdrian McCarthy 8504ad5def9SAdrian McCarthy // Add the main executable module to the list of pending module loads. We 85105097246SAdrian Prantl // can't call GetTarget().ModulesDidLoad() here because we still haven't 85205097246SAdrian Prantl // returned from DoLaunch() / DoAttach() yet so the target may not have set 85305097246SAdrian Prantl // the process instance to `this` yet. 8544ad5def9SAdrian McCarthy llvm::sys::ScopedLock lock(m_mutex); 8554ad5def9SAdrian McCarthy const HostThreadWindows &wmain_thread = 8564ad5def9SAdrian McCarthy debugger->GetMainThread().GetNativeThread(); 8574ad5def9SAdrian McCarthy m_session_data->m_new_threads[wmain_thread.GetThreadId()] = 8584ad5def9SAdrian McCarthy debugger->GetMainThread(); 8594ad5def9SAdrian McCarthy } 8604ad5def9SAdrian McCarthy 8614ad5def9SAdrian McCarthy ExceptionResult 8624ad5def9SAdrian McCarthy ProcessWindows::OnDebugException(bool first_chance, 8634ad5def9SAdrian McCarthy const ExceptionRecord &record) { 864a385d2c1SPavel Labath Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EXCEPTION); 8654ad5def9SAdrian McCarthy llvm::sys::ScopedLock lock(m_mutex); 8664ad5def9SAdrian McCarthy 8674ad5def9SAdrian McCarthy // FIXME: Without this check, occasionally when running the test suite there 8684ad5def9SAdrian McCarthy // is 8694ad5def9SAdrian McCarthy // an issue where m_session_data can be null. It's not clear how this could 87005097246SAdrian Prantl // happen but it only surfaces while running the test suite. In order to 87105097246SAdrian Prantl // properly diagnose this, we probably need to first figure allow the test 87205097246SAdrian Prantl // suite to print out full lldb logs, and then add logging to the process 87305097246SAdrian Prantl // plugin. 8744ad5def9SAdrian McCarthy if (!m_session_data) { 87562c76db4SStella Stamenova LLDB_LOG(log, 87662c76db4SStella Stamenova "Debugger thread reported exception {0:x} at address {1:x}, " 877a385d2c1SPavel Labath "but there is no session.", 8784ad5def9SAdrian McCarthy record.GetExceptionCode(), record.GetExceptionAddress()); 8794ad5def9SAdrian McCarthy return ExceptionResult::SendToApplication; 8804ad5def9SAdrian McCarthy } 8814ad5def9SAdrian McCarthy 8824ad5def9SAdrian McCarthy if (!first_chance) { 8834ad5def9SAdrian McCarthy // Any second chance exception is an application crash by definition. 8844ad5def9SAdrian McCarthy SetPrivateState(eStateCrashed); 8854ad5def9SAdrian McCarthy } 8864ad5def9SAdrian McCarthy 8874ad5def9SAdrian McCarthy ExceptionResult result = ExceptionResult::SendToApplication; 8884ad5def9SAdrian McCarthy switch (record.GetExceptionCode()) { 8894ad5def9SAdrian McCarthy case EXCEPTION_BREAKPOINT: 8904ad5def9SAdrian McCarthy // Handle breakpoints at the first chance. 8914ad5def9SAdrian McCarthy result = ExceptionResult::BreakInDebugger; 8924ad5def9SAdrian McCarthy 8934ad5def9SAdrian McCarthy if (!m_session_data->m_initial_stop_received) { 894a385d2c1SPavel Labath LLDB_LOG( 895a385d2c1SPavel Labath log, 896a385d2c1SPavel Labath "Hit loader breakpoint at address {0:x}, setting initial stop event.", 8974ad5def9SAdrian McCarthy record.GetExceptionAddress()); 8984ad5def9SAdrian McCarthy m_session_data->m_initial_stop_received = true; 8994ad5def9SAdrian McCarthy ::SetEvent(m_session_data->m_initial_stop_event); 9004ad5def9SAdrian McCarthy } else { 901a385d2c1SPavel Labath LLDB_LOG(log, "Hit non-loader breakpoint at address {0:x}.", 9024ad5def9SAdrian McCarthy record.GetExceptionAddress()); 9034ad5def9SAdrian McCarthy } 9044ad5def9SAdrian McCarthy SetPrivateState(eStateStopped); 9054ad5def9SAdrian McCarthy break; 9064ad5def9SAdrian McCarthy case EXCEPTION_SINGLE_STEP: 9074ad5def9SAdrian McCarthy result = ExceptionResult::BreakInDebugger; 9084ad5def9SAdrian McCarthy SetPrivateState(eStateStopped); 9094ad5def9SAdrian McCarthy break; 9104ad5def9SAdrian McCarthy default: 91162c76db4SStella Stamenova LLDB_LOG(log, 91262c76db4SStella Stamenova "Debugger thread reported exception {0:x} at address {1:x} " 913a385d2c1SPavel Labath "(first_chance={2})", 9144ad5def9SAdrian McCarthy record.GetExceptionCode(), record.GetExceptionAddress(), 915a385d2c1SPavel Labath first_chance); 9164ad5def9SAdrian McCarthy // For non-breakpoints, give the application a chance to handle the 9174ad5def9SAdrian McCarthy // exception first. 9184ad5def9SAdrian McCarthy if (first_chance) 9194ad5def9SAdrian McCarthy result = ExceptionResult::SendToApplication; 9204ad5def9SAdrian McCarthy else 9214ad5def9SAdrian McCarthy result = ExceptionResult::BreakInDebugger; 9224ad5def9SAdrian McCarthy } 9234ad5def9SAdrian McCarthy 9244ad5def9SAdrian McCarthy return result; 9254ad5def9SAdrian McCarthy } 9264ad5def9SAdrian McCarthy 9274ad5def9SAdrian McCarthy void ProcessWindows::OnCreateThread(const HostThread &new_thread) { 9284ad5def9SAdrian McCarthy llvm::sys::ScopedLock lock(m_mutex); 9294ad5def9SAdrian McCarthy const HostThreadWindows &wnew_thread = new_thread.GetNativeThread(); 9304ad5def9SAdrian McCarthy m_session_data->m_new_threads[wnew_thread.GetThreadId()] = new_thread; 9314ad5def9SAdrian McCarthy } 9324ad5def9SAdrian McCarthy 9334ad5def9SAdrian McCarthy void ProcessWindows::OnExitThread(lldb::tid_t thread_id, uint32_t exit_code) { 9344ad5def9SAdrian McCarthy llvm::sys::ScopedLock lock(m_mutex); 9354ad5def9SAdrian McCarthy 9364ad5def9SAdrian McCarthy // On a forced termination, we may get exit thread events after the session 9374ad5def9SAdrian McCarthy // data has been cleaned up. 9384ad5def9SAdrian McCarthy if (!m_session_data) 9394ad5def9SAdrian McCarthy return; 9404ad5def9SAdrian McCarthy 9414ad5def9SAdrian McCarthy // A thread may have started and exited before the debugger stopped allowing a 9424ad5def9SAdrian McCarthy // refresh. 9434ad5def9SAdrian McCarthy // Just remove it from the new threads list in that case. 9444ad5def9SAdrian McCarthy auto iter = m_session_data->m_new_threads.find(thread_id); 9454ad5def9SAdrian McCarthy if (iter != m_session_data->m_new_threads.end()) 9464ad5def9SAdrian McCarthy m_session_data->m_new_threads.erase(iter); 9474ad5def9SAdrian McCarthy else 9484ad5def9SAdrian McCarthy m_session_data->m_exited_threads.insert(thread_id); 9494ad5def9SAdrian McCarthy } 9504ad5def9SAdrian McCarthy 9514ad5def9SAdrian McCarthy void ProcessWindows::OnLoadDll(const ModuleSpec &module_spec, 9524ad5def9SAdrian McCarthy lldb::addr_t module_addr) { 9534ad5def9SAdrian McCarthy // Confusingly, there is no Target::AddSharedModule. Instead, calling 95405097246SAdrian Prantl // GetSharedModule() with a new module will add it to the module list and 95505097246SAdrian Prantl // return a corresponding ModuleSP. 95697206d57SZachary Turner Status error; 9574ad5def9SAdrian McCarthy ModuleSP module = GetTarget().GetSharedModule(module_spec, &error); 9584ad5def9SAdrian McCarthy bool load_addr_changed = false; 9594ad5def9SAdrian McCarthy module->SetLoadAddress(GetTarget(), module_addr, false, load_addr_changed); 9604ad5def9SAdrian McCarthy 9614ad5def9SAdrian McCarthy ModuleList loaded_modules; 9624ad5def9SAdrian McCarthy loaded_modules.Append(module); 9634ad5def9SAdrian McCarthy GetTarget().ModulesDidLoad(loaded_modules); 9644ad5def9SAdrian McCarthy } 9654ad5def9SAdrian McCarthy 9664ad5def9SAdrian McCarthy void ProcessWindows::OnUnloadDll(lldb::addr_t module_addr) { 9674ad5def9SAdrian McCarthy Address resolved_addr; 9684ad5def9SAdrian McCarthy if (GetTarget().ResolveLoadAddress(module_addr, resolved_addr)) { 9694ad5def9SAdrian McCarthy ModuleSP module = resolved_addr.GetModule(); 9704ad5def9SAdrian McCarthy if (module) { 9714ad5def9SAdrian McCarthy ModuleList unloaded_modules; 9724ad5def9SAdrian McCarthy unloaded_modules.Append(module); 9734ad5def9SAdrian McCarthy GetTarget().ModulesDidUnload(unloaded_modules, false); 9744ad5def9SAdrian McCarthy } 9754ad5def9SAdrian McCarthy } 9764ad5def9SAdrian McCarthy } 9774ad5def9SAdrian McCarthy 9784ad5def9SAdrian McCarthy void ProcessWindows::OnDebugString(const std::string &string) {} 9794ad5def9SAdrian McCarthy 98097206d57SZachary Turner void ProcessWindows::OnDebuggerError(const Status &error, uint32_t type) { 9814ad5def9SAdrian McCarthy llvm::sys::ScopedLock lock(m_mutex); 982a385d2c1SPavel Labath Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS); 9834ad5def9SAdrian McCarthy 9844ad5def9SAdrian McCarthy if (m_session_data->m_initial_stop_received) { 98505097246SAdrian Prantl // This happened while debugging. Do we shutdown the debugging session, 98605097246SAdrian Prantl // try to continue, or do something else? 98762c76db4SStella Stamenova LLDB_LOG(log, 98862c76db4SStella Stamenova "Error {0} occurred during debugging. Unexpected behavior " 989a385d2c1SPavel Labath "may result. {1}", 990a385d2c1SPavel Labath error.GetError(), error); 9914ad5def9SAdrian McCarthy } else { 9924ad5def9SAdrian McCarthy // If we haven't actually launched the process yet, this was an error 99305097246SAdrian Prantl // launching the process. Set the internal error and signal the initial 99405097246SAdrian Prantl // stop event so that the DoLaunch method wakes up and returns a failure. 9954ad5def9SAdrian McCarthy m_session_data->m_launch_error = error; 9964ad5def9SAdrian McCarthy ::SetEvent(m_session_data->m_initial_stop_event); 997a385d2c1SPavel Labath LLDB_LOG( 998a385d2c1SPavel Labath log, 999a385d2c1SPavel Labath "Error {0} occurred launching the process before the initial stop. {1}", 1000a385d2c1SPavel Labath error.GetError(), error); 10014ad5def9SAdrian McCarthy return; 10024ad5def9SAdrian McCarthy } 10034ad5def9SAdrian McCarthy } 10044ad5def9SAdrian McCarthy 100597206d57SZachary Turner Status ProcessWindows::WaitForDebuggerConnection(DebuggerThreadSP debugger, 10064ad5def9SAdrian McCarthy HostProcess &process) { 100797206d57SZachary Turner Status result; 1008a385d2c1SPavel Labath Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS | 1009a385d2c1SPavel Labath WINDOWS_LOG_BREAKPOINTS); 1010a385d2c1SPavel Labath LLDB_LOG(log, "Waiting for loader breakpoint."); 10114ad5def9SAdrian McCarthy 10124ad5def9SAdrian McCarthy // Block this function until we receive the initial stop from the process. 10134ad5def9SAdrian McCarthy if (::WaitForSingleObject(m_session_data->m_initial_stop_event, INFINITE) == 10144ad5def9SAdrian McCarthy WAIT_OBJECT_0) { 1015a385d2c1SPavel Labath LLDB_LOG(log, "hit loader breakpoint, returning."); 10164ad5def9SAdrian McCarthy 10174ad5def9SAdrian McCarthy process = debugger->GetProcess(); 10184ad5def9SAdrian McCarthy return m_session_data->m_launch_error; 10194ad5def9SAdrian McCarthy } else 102097206d57SZachary Turner return Status(::GetLastError(), eErrorTypeWin32); 10214ad5def9SAdrian McCarthy } 10224ad5def9SAdrian McCarthy 1023b9c1b51eSKate Stone // The Windows page protection bits are NOT independent masks that can be 102405097246SAdrian Prantl // bitwise-ORed together. For example, PAGE_EXECUTE_READ is not (PAGE_EXECUTE 102505097246SAdrian Prantl // | PAGE_READ). To test for an access type, it's necessary to test for any of 102605097246SAdrian Prantl // the bits that provide that access type. 1027b9c1b51eSKate Stone bool ProcessWindows::IsPageReadable(uint32_t protect) { 10280c35cde9SAdrian McCarthy return (protect & PAGE_NOACCESS) == 0; 10290c35cde9SAdrian McCarthy } 10300c35cde9SAdrian McCarthy 1031b9c1b51eSKate Stone bool ProcessWindows::IsPageWritable(uint32_t protect) { 1032b9c1b51eSKate Stone return (protect & (PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY | 1033b9c1b51eSKate Stone PAGE_READWRITE | PAGE_WRITECOPY)) != 0; 10340c35cde9SAdrian McCarthy } 10350c35cde9SAdrian McCarthy 1036b9c1b51eSKate Stone bool ProcessWindows::IsPageExecutable(uint32_t protect) { 1037b9c1b51eSKate Stone return (protect & (PAGE_EXECUTE | PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | 1038b9c1b51eSKate Stone PAGE_EXECUTE_WRITECOPY)) != 0; 10390c35cde9SAdrian McCarthy } 10404ad5def9SAdrian McCarthy 10414ad5def9SAdrian McCarthy } // namespace lldb_private 1042