1 //===-- ProcessWindows.cpp ------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "ProcessWindows.h" 10 11 // Windows includes 12 #include "lldb/Host/windows/windows.h" 13 #include <psapi.h> 14 15 #include "lldb/Breakpoint/Watchpoint.h" 16 #include "lldb/Core/Module.h" 17 #include "lldb/Core/ModuleSpec.h" 18 #include "lldb/Core/PluginManager.h" 19 #include "lldb/Core/Section.h" 20 #include "lldb/Host/FileSystem.h" 21 #include "lldb/Host/HostNativeProcessBase.h" 22 #include "lldb/Host/HostProcess.h" 23 #include "lldb/Host/windows/HostThreadWindows.h" 24 #include "lldb/Host/windows/windows.h" 25 #include "lldb/Symbol/ObjectFile.h" 26 #include "lldb/Target/DynamicLoader.h" 27 #include "lldb/Target/MemoryRegionInfo.h" 28 #include "lldb/Target/StopInfo.h" 29 #include "lldb/Target/Target.h" 30 #include "lldb/Utility/State.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 LLDB_PLUGIN_DEFINE_ADV(ProcessWindows, ProcessWindowsCommon) 48 49 namespace { 50 std::string GetProcessExecutableName(HANDLE process_handle) { 51 std::vector<wchar_t> file_name; 52 DWORD file_name_size = MAX_PATH; // first guess, not an absolute limit 53 DWORD copied = 0; 54 do { 55 file_name_size *= 2; 56 file_name.resize(file_name_size); 57 copied = ::GetModuleFileNameExW(process_handle, NULL, file_name.data(), 58 file_name_size); 59 } while (copied >= file_name_size); 60 file_name.resize(copied); 61 std::string result; 62 llvm::convertWideToUTF8(file_name.data(), result); 63 return result; 64 } 65 66 std::string GetProcessExecutableName(DWORD pid) { 67 std::string file_name; 68 HANDLE process_handle = 69 ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid); 70 if (process_handle != NULL) { 71 file_name = GetProcessExecutableName(process_handle); 72 ::CloseHandle(process_handle); 73 } 74 return file_name; 75 } 76 } // anonymous namespace 77 78 namespace lldb_private { 79 80 ProcessSP ProcessWindows::CreateInstance(lldb::TargetSP target_sp, 81 lldb::ListenerSP listener_sp, 82 const FileSpec *) { 83 return ProcessSP(new ProcessWindows(target_sp, listener_sp)); 84 } 85 86 static bool ShouldUseLLDBServer() { 87 llvm::StringRef use_lldb_server = ::getenv("LLDB_USE_LLDB_SERVER"); 88 return use_lldb_server.equals_lower("on") || 89 use_lldb_server.equals_lower("yes") || 90 use_lldb_server.equals_lower("1") || 91 use_lldb_server.equals_lower("true"); 92 } 93 94 void ProcessWindows::Initialize() { 95 if (!ShouldUseLLDBServer()) { 96 static llvm::once_flag g_once_flag; 97 98 llvm::call_once(g_once_flag, []() { 99 PluginManager::RegisterPlugin(GetPluginNameStatic(), 100 GetPluginDescriptionStatic(), 101 CreateInstance); 102 }); 103 } 104 } 105 106 void ProcessWindows::Terminate() {} 107 108 lldb_private::ConstString ProcessWindows::GetPluginNameStatic() { 109 static ConstString g_name("windows"); 110 return g_name; 111 } 112 113 const char *ProcessWindows::GetPluginDescriptionStatic() { 114 return "Process plugin for Windows"; 115 } 116 117 // Constructors and destructors. 118 119 ProcessWindows::ProcessWindows(lldb::TargetSP target_sp, 120 lldb::ListenerSP listener_sp) 121 : lldb_private::Process(target_sp, listener_sp), 122 m_watchpoint_ids( 123 RegisterContextWindows::GetNumHardwareBreakpointSlots(), 124 LLDB_INVALID_BREAK_ID) {} 125 126 ProcessWindows::~ProcessWindows() {} 127 128 size_t ProcessWindows::GetSTDOUT(char *buf, size_t buf_size, Status &error) { 129 error.SetErrorString("GetSTDOUT unsupported on Windows"); 130 return 0; 131 } 132 133 size_t ProcessWindows::GetSTDERR(char *buf, size_t buf_size, Status &error) { 134 error.SetErrorString("GetSTDERR unsupported on Windows"); 135 return 0; 136 } 137 138 size_t ProcessWindows::PutSTDIN(const char *buf, size_t buf_size, 139 Status &error) { 140 error.SetErrorString("PutSTDIN unsupported on Windows"); 141 return 0; 142 } 143 144 // ProcessInterface protocol. 145 146 lldb_private::ConstString ProcessWindows::GetPluginName() { 147 return GetPluginNameStatic(); 148 } 149 150 uint32_t ProcessWindows::GetPluginVersion() { return 1; } 151 152 Status ProcessWindows::EnableBreakpointSite(BreakpointSite *bp_site) { 153 if (bp_site->HardwareRequired()) 154 return Status("Hardware breakpoints are not supported."); 155 156 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_BREAKPOINTS); 157 LLDB_LOG(log, "bp_site = {0:x}, id={1}, addr={2:x}", bp_site, 158 bp_site->GetID(), bp_site->GetLoadAddress()); 159 160 Status error = EnableSoftwareBreakpoint(bp_site); 161 if (!error.Success()) 162 LLDB_LOG(log, "error: {0}", error); 163 return error; 164 } 165 166 Status ProcessWindows::DisableBreakpointSite(BreakpointSite *bp_site) { 167 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_BREAKPOINTS); 168 LLDB_LOG(log, "bp_site = {0:x}, id={1}, addr={2:x}", bp_site, 169 bp_site->GetID(), bp_site->GetLoadAddress()); 170 171 Status error = DisableSoftwareBreakpoint(bp_site); 172 173 if (!error.Success()) 174 LLDB_LOG(log, "error: {0}", error); 175 return error; 176 } 177 178 Status ProcessWindows::DoDetach(bool keep_stopped) { 179 Status error; 180 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS); 181 StateType private_state = GetPrivateState(); 182 if (private_state != eStateExited && private_state != eStateDetached) { 183 error = DetachProcess(); 184 if (error.Success()) 185 SetPrivateState(eStateDetached); 186 else 187 LLDB_LOG(log, "Detaching process error: {0}", error); 188 } else { 189 error.SetErrorStringWithFormatv("error: process {0} in state = {1}, but " 190 "cannot detach it in this state.", 191 GetID(), private_state); 192 LLDB_LOG(log, "error: {0}", error); 193 } 194 return error; 195 } 196 197 Status ProcessWindows::DoLaunch(Module *exe_module, 198 ProcessLaunchInfo &launch_info) { 199 Status error; 200 DebugDelegateSP delegate(new LocalDebugDelegate(shared_from_this())); 201 error = LaunchProcess(launch_info, delegate); 202 if (error.Success()) 203 SetID(launch_info.GetProcessID()); 204 return error; 205 } 206 207 Status 208 ProcessWindows::DoAttachToProcessWithID(lldb::pid_t pid, 209 const ProcessAttachInfo &attach_info) { 210 DebugDelegateSP delegate(new LocalDebugDelegate(shared_from_this())); 211 Status error = AttachProcess(pid, attach_info, delegate); 212 if (error.Success()) 213 SetID(GetDebuggedProcessId()); 214 return error; 215 } 216 217 Status ProcessWindows::DoResume() { 218 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS); 219 llvm::sys::ScopedLock lock(m_mutex); 220 Status error; 221 222 StateType private_state = GetPrivateState(); 223 if (private_state == eStateStopped || private_state == eStateCrashed) { 224 LLDB_LOG(log, "process {0} is in state {1}. Resuming...", 225 m_session_data->m_debugger->GetProcess().GetProcessId(), 226 GetPrivateState()); 227 228 LLDB_LOG(log, "resuming {0} threads.", m_thread_list.GetSize()); 229 230 bool failed = false; 231 for (uint32_t i = 0; i < m_thread_list.GetSize(); ++i) { 232 auto thread = std::static_pointer_cast<TargetThreadWindows>( 233 m_thread_list.GetThreadAtIndex(i)); 234 Status result = thread->DoResume(); 235 if (result.Fail()) { 236 failed = true; 237 LLDB_LOG( 238 log, 239 "Trying to resume thread at index {0}, but failed with error {1}.", 240 i, result); 241 } 242 } 243 244 if (failed) { 245 error.SetErrorString("ProcessWindows::DoResume failed"); 246 } else { 247 SetPrivateState(eStateRunning); 248 } 249 250 ExceptionRecordSP active_exception = 251 m_session_data->m_debugger->GetActiveException().lock(); 252 if (active_exception) { 253 // Resume the process and continue processing debug events. Mask the 254 // exception so that from the process's view, there is no indication that 255 // anything happened. 256 m_session_data->m_debugger->ContinueAsyncException( 257 ExceptionResult::MaskException); 258 } 259 } else { 260 LLDB_LOG(log, "error: process {0} is in state {1}. Returning...", 261 m_session_data->m_debugger->GetProcess().GetProcessId(), 262 GetPrivateState()); 263 } 264 return error; 265 } 266 267 Status ProcessWindows::DoDestroy() { 268 StateType private_state = GetPrivateState(); 269 return DestroyProcess(private_state); 270 } 271 272 Status ProcessWindows::DoHalt(bool &caused_stop) { 273 StateType state = GetPrivateState(); 274 if (state != eStateStopped) 275 return HaltProcess(caused_stop); 276 caused_stop = false; 277 return Status(); 278 } 279 280 void ProcessWindows::DidLaunch() { 281 ArchSpec arch_spec; 282 DidAttach(arch_spec); 283 } 284 285 void ProcessWindows::DidAttach(ArchSpec &arch_spec) { 286 llvm::sys::ScopedLock lock(m_mutex); 287 288 // The initial stop won't broadcast the state change event, so account for 289 // that here. 290 if (m_session_data && GetPrivateState() == eStateStopped && 291 m_session_data->m_stop_at_entry) 292 RefreshStateAfterStop(); 293 } 294 295 static void 296 DumpAdditionalExceptionInformation(llvm::raw_ostream &stream, 297 const ExceptionRecordSP &exception) { 298 // Decode additional exception information for specific exception types based 299 // on 300 // https://docs.microsoft.com/en-us/windows/desktop/api/winnt/ns-winnt-_exception_record 301 302 const int addr_min_width = 2 + 8; // "0x" + 4 address bytes 303 304 const std::vector<ULONG_PTR> &args = exception->GetExceptionArguments(); 305 switch (exception->GetExceptionCode()) { 306 case EXCEPTION_ACCESS_VIOLATION: { 307 if (args.size() < 2) 308 break; 309 310 stream << ": "; 311 const int access_violation_code = args[0]; 312 const lldb::addr_t access_violation_address = args[1]; 313 switch (access_violation_code) { 314 case 0: 315 stream << "Access violation reading"; 316 break; 317 case 1: 318 stream << "Access violation writing"; 319 break; 320 case 8: 321 stream << "User-mode data execution prevention (DEP) violation at"; 322 break; 323 default: 324 stream << "Unknown access violation (code " << access_violation_code 325 << ") at"; 326 break; 327 } 328 stream << " location " 329 << llvm::format_hex(access_violation_address, addr_min_width); 330 break; 331 } 332 case EXCEPTION_IN_PAGE_ERROR: { 333 if (args.size() < 3) 334 break; 335 336 stream << ": "; 337 const int page_load_error_code = args[0]; 338 const lldb::addr_t page_load_error_address = args[1]; 339 const DWORD underlying_code = args[2]; 340 switch (page_load_error_code) { 341 case 0: 342 stream << "In page error reading"; 343 break; 344 case 1: 345 stream << "In page error writing"; 346 break; 347 case 8: 348 stream << "User-mode data execution prevention (DEP) violation at"; 349 break; 350 default: 351 stream << "Unknown page loading error (code " << page_load_error_code 352 << ") at"; 353 break; 354 } 355 stream << " location " 356 << llvm::format_hex(page_load_error_address, addr_min_width) 357 << " (status code " << llvm::format_hex(underlying_code, 8) << ")"; 358 break; 359 } 360 } 361 } 362 363 void ProcessWindows::RefreshStateAfterStop() { 364 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EXCEPTION); 365 llvm::sys::ScopedLock lock(m_mutex); 366 367 if (!m_session_data) { 368 LLDB_LOG(log, "no active session. Returning..."); 369 return; 370 } 371 372 m_thread_list.RefreshStateAfterStop(); 373 374 std::weak_ptr<ExceptionRecord> exception_record = 375 m_session_data->m_debugger->GetActiveException(); 376 ExceptionRecordSP active_exception = exception_record.lock(); 377 if (!active_exception) { 378 LLDB_LOG(log, 379 "there is no active exception in process {0}. Why is the " 380 "process stopped?", 381 m_session_data->m_debugger->GetProcess().GetProcessId()); 382 return; 383 } 384 385 StopInfoSP stop_info; 386 m_thread_list.SetSelectedThreadByID(active_exception->GetThreadID()); 387 ThreadSP stop_thread = m_thread_list.GetSelectedThread(); 388 if (!stop_thread) 389 return; 390 391 switch (active_exception->GetExceptionCode()) { 392 case EXCEPTION_SINGLE_STEP: { 393 RegisterContextSP register_context = stop_thread->GetRegisterContext(); 394 const uint64_t pc = register_context->GetPC(); 395 BreakpointSiteSP site(GetBreakpointSiteList().FindByAddress(pc)); 396 if (site && site->ValidForThisThread(stop_thread.get())) { 397 LLDB_LOG(log, 398 "Single-stepped onto a breakpoint in process {0} at " 399 "address {1:x} with breakpoint site {2}", 400 m_session_data->m_debugger->GetProcess().GetProcessId(), pc, 401 site->GetID()); 402 stop_info = StopInfo::CreateStopReasonWithBreakpointSiteID(*stop_thread, 403 site->GetID()); 404 stop_thread->SetStopInfo(stop_info); 405 406 return; 407 } 408 409 auto *reg_ctx = static_cast<RegisterContextWindows *>( 410 stop_thread->GetRegisterContext().get()); 411 uint32_t slot_id = reg_ctx->GetTriggeredHardwareBreakpointSlotId(); 412 if (slot_id != LLDB_INVALID_INDEX32) { 413 int id = m_watchpoint_ids[slot_id]; 414 LLDB_LOG(log, 415 "Single-stepped onto a watchpoint in process {0} at address " 416 "{1:x} with watchpoint {2}", 417 m_session_data->m_debugger->GetProcess().GetProcessId(), pc, id); 418 419 if (lldb::WatchpointSP wp_sp = 420 GetTarget().GetWatchpointList().FindByID(id)) 421 wp_sp->SetHardwareIndex(slot_id); 422 423 stop_info = StopInfo::CreateStopReasonWithWatchpointID( 424 *stop_thread, id, m_watchpoints[id].address); 425 stop_thread->SetStopInfo(stop_info); 426 427 return; 428 } 429 430 LLDB_LOG(log, "single stepping thread {0}", stop_thread->GetID()); 431 stop_info = StopInfo::CreateStopReasonToTrace(*stop_thread); 432 stop_thread->SetStopInfo(stop_info); 433 434 return; 435 } 436 437 case EXCEPTION_BREAKPOINT: { 438 RegisterContextSP register_context = stop_thread->GetRegisterContext(); 439 440 // The current EIP is AFTER the BP opcode, which is one byte. 441 uint64_t pc = register_context->GetPC() - 1; 442 443 BreakpointSiteSP site(GetBreakpointSiteList().FindByAddress(pc)); 444 if (site) { 445 LLDB_LOG(log, 446 "detected breakpoint in process {0} at address {1:x} with " 447 "breakpoint site {2}", 448 m_session_data->m_debugger->GetProcess().GetProcessId(), pc, 449 site->GetID()); 450 451 if (site->ValidForThisThread(stop_thread.get())) { 452 LLDB_LOG(log, 453 "Breakpoint site {0} is valid for this thread ({1:x}), " 454 "creating stop info.", 455 site->GetID(), stop_thread->GetID()); 456 457 stop_info = StopInfo::CreateStopReasonWithBreakpointSiteID( 458 *stop_thread, site->GetID()); 459 register_context->SetPC(pc); 460 } else { 461 LLDB_LOG(log, 462 "Breakpoint site {0} is not valid for this thread, " 463 "creating empty stop info.", 464 site->GetID()); 465 } 466 stop_thread->SetStopInfo(stop_info); 467 return; 468 } else { 469 // The thread hit a hard-coded breakpoint like an `int 3` or 470 // `__debugbreak()`. 471 LLDB_LOG(log, 472 "No breakpoint site matches for this thread. __debugbreak()? " 473 "Creating stop info with the exception."); 474 // FALLTHROUGH: We'll treat this as a generic exception record in the 475 // default case. 476 LLVM_FALLTHROUGH; 477 } 478 } 479 480 default: { 481 std::string desc; 482 llvm::raw_string_ostream desc_stream(desc); 483 desc_stream << "Exception " 484 << llvm::format_hex(active_exception->GetExceptionCode(), 8) 485 << " encountered at address " 486 << llvm::format_hex(active_exception->GetExceptionAddress(), 8); 487 DumpAdditionalExceptionInformation(desc_stream, active_exception); 488 489 stop_info = StopInfo::CreateStopReasonWithException( 490 *stop_thread, desc_stream.str().c_str()); 491 stop_thread->SetStopInfo(stop_info); 492 LLDB_LOG(log, "{0}", desc_stream.str()); 493 return; 494 } 495 } 496 } 497 498 bool ProcessWindows::CanDebug(lldb::TargetSP target_sp, 499 bool plugin_specified_by_name) { 500 if (plugin_specified_by_name) 501 return true; 502 503 // For now we are just making sure the file exists for a given module 504 ModuleSP exe_module_sp(target_sp->GetExecutableModule()); 505 if (exe_module_sp.get()) 506 return FileSystem::Instance().Exists(exe_module_sp->GetFileSpec()); 507 // However, if there is no executable module, we return true since we might 508 // be preparing to attach. 509 return true; 510 } 511 512 bool ProcessWindows::UpdateThreadList(ThreadList &old_thread_list, 513 ThreadList &new_thread_list) { 514 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_THREAD); 515 // Add all the threads that were previously running and for which we did not 516 // detect a thread exited event. 517 int new_size = 0; 518 int continued_threads = 0; 519 int exited_threads = 0; 520 int new_threads = 0; 521 522 for (ThreadSP old_thread : old_thread_list.Threads()) { 523 lldb::tid_t old_thread_id = old_thread->GetID(); 524 auto exited_thread_iter = 525 m_session_data->m_exited_threads.find(old_thread_id); 526 if (exited_thread_iter == m_session_data->m_exited_threads.end()) { 527 new_thread_list.AddThread(old_thread); 528 ++new_size; 529 ++continued_threads; 530 LLDB_LOGV(log, "Thread {0} was running and is still running.", 531 old_thread_id); 532 } else { 533 LLDB_LOGV(log, "Thread {0} was running and has exited.", old_thread_id); 534 ++exited_threads; 535 } 536 } 537 538 // Also add all the threads that are new since the last time we broke into 539 // the debugger. 540 for (const auto &thread_info : m_session_data->m_new_threads) { 541 new_thread_list.AddThread(thread_info.second); 542 ++new_size; 543 ++new_threads; 544 LLDB_LOGV(log, "Thread {0} is new since last update.", thread_info.first); 545 } 546 547 LLDB_LOG(log, "{0} new threads, {1} old threads, {2} exited threads.", 548 new_threads, continued_threads, exited_threads); 549 550 m_session_data->m_new_threads.clear(); 551 m_session_data->m_exited_threads.clear(); 552 553 return new_size > 0; 554 } 555 556 bool ProcessWindows::IsAlive() { 557 StateType state = GetPrivateState(); 558 switch (state) { 559 case eStateCrashed: 560 case eStateDetached: 561 case eStateUnloaded: 562 case eStateExited: 563 case eStateInvalid: 564 return false; 565 default: 566 return true; 567 } 568 } 569 570 size_t ProcessWindows::DoReadMemory(lldb::addr_t vm_addr, void *buf, 571 size_t size, Status &error) { 572 size_t bytes_read = 0; 573 error = ProcessDebugger::ReadMemory(vm_addr, buf, size, bytes_read); 574 return bytes_read; 575 } 576 577 size_t ProcessWindows::DoWriteMemory(lldb::addr_t vm_addr, const void *buf, 578 size_t size, Status &error) { 579 size_t bytes_written = 0; 580 error = ProcessDebugger::WriteMemory(vm_addr, buf, size, bytes_written); 581 return bytes_written; 582 } 583 584 lldb::addr_t ProcessWindows::DoAllocateMemory(size_t size, uint32_t permissions, 585 Status &error) { 586 lldb::addr_t vm_addr = LLDB_INVALID_ADDRESS; 587 error = ProcessDebugger::AllocateMemory(size, permissions, vm_addr); 588 return vm_addr; 589 } 590 591 Status ProcessWindows::DoDeallocateMemory(lldb::addr_t ptr) { 592 return ProcessDebugger::DeallocateMemory(ptr); 593 } 594 595 Status ProcessWindows::GetMemoryRegionInfo(lldb::addr_t vm_addr, 596 MemoryRegionInfo &info) { 597 return ProcessDebugger::GetMemoryRegionInfo(vm_addr, info); 598 } 599 600 lldb::addr_t ProcessWindows::GetImageInfoAddress() { 601 Target &target = GetTarget(); 602 ObjectFile *obj_file = target.GetExecutableModule()->GetObjectFile(); 603 Address addr = obj_file->GetImageInfoAddress(&target); 604 if (addr.IsValid()) 605 return addr.GetLoadAddress(&target); 606 else 607 return LLDB_INVALID_ADDRESS; 608 } 609 610 DynamicLoaderWindowsDYLD *ProcessWindows::GetDynamicLoader() { 611 if (m_dyld_up.get() == NULL) 612 m_dyld_up.reset(DynamicLoader::FindPlugin( 613 this, DynamicLoaderWindowsDYLD::GetPluginNameStatic().GetCString())); 614 return static_cast<DynamicLoaderWindowsDYLD *>(m_dyld_up.get()); 615 } 616 617 void ProcessWindows::OnExitProcess(uint32_t exit_code) { 618 // No need to acquire the lock since m_session_data isn't accessed. 619 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS); 620 LLDB_LOG(log, "Process {0} exited with code {1}", GetID(), exit_code); 621 622 TargetSP target = CalculateTarget(); 623 if (target) { 624 ModuleSP executable_module = target->GetExecutableModule(); 625 ModuleList unloaded_modules; 626 unloaded_modules.Append(executable_module); 627 target->ModulesDidUnload(unloaded_modules, true); 628 } 629 630 SetProcessExitStatus(GetID(), true, 0, exit_code); 631 SetPrivateState(eStateExited); 632 633 ProcessDebugger::OnExitProcess(exit_code); 634 } 635 636 void ProcessWindows::OnDebuggerConnected(lldb::addr_t image_base) { 637 DebuggerThreadSP debugger = m_session_data->m_debugger; 638 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS); 639 LLDB_LOG(log, "Debugger connected to process {0}. Image base = {1:x}", 640 debugger->GetProcess().GetProcessId(), image_base); 641 642 ModuleSP module = GetTarget().GetExecutableModule(); 643 if (!module) { 644 // During attach, we won't have the executable module, so find it now. 645 const DWORD pid = debugger->GetProcess().GetProcessId(); 646 const std::string file_name = GetProcessExecutableName(pid); 647 if (file_name.empty()) { 648 return; 649 } 650 651 FileSpec executable_file(file_name); 652 FileSystem::Instance().Resolve(executable_file); 653 ModuleSpec module_spec(executable_file); 654 Status error; 655 module = 656 GetTarget().GetOrCreateModule(module_spec, true /* notify */, &error); 657 if (!module) { 658 return; 659 } 660 661 GetTarget().SetExecutableModule(module, eLoadDependentsNo); 662 } 663 664 if (auto dyld = GetDynamicLoader()) 665 dyld->OnLoadModule(module, ModuleSpec(), image_base); 666 667 // Add the main executable module to the list of pending module loads. We 668 // can't call GetTarget().ModulesDidLoad() here because we still haven't 669 // returned from DoLaunch() / DoAttach() yet so the target may not have set 670 // the process instance to `this` yet. 671 llvm::sys::ScopedLock lock(m_mutex); 672 673 const HostThread &host_main_thread = debugger->GetMainThread(); 674 ThreadSP main_thread = 675 std::make_shared<TargetThreadWindows>(*this, host_main_thread); 676 677 tid_t id = host_main_thread.GetNativeThread().GetThreadId(); 678 main_thread->SetID(id); 679 680 m_session_data->m_new_threads[id] = main_thread; 681 } 682 683 ExceptionResult 684 ProcessWindows::OnDebugException(bool first_chance, 685 const ExceptionRecord &record) { 686 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_EXCEPTION); 687 llvm::sys::ScopedLock lock(m_mutex); 688 689 // FIXME: Without this check, occasionally when running the test suite there 690 // is 691 // an issue where m_session_data can be null. It's not clear how this could 692 // happen but it only surfaces while running the test suite. In order to 693 // properly diagnose this, we probably need to first figure allow the test 694 // suite to print out full lldb logs, and then add logging to the process 695 // plugin. 696 if (!m_session_data) { 697 LLDB_LOG(log, 698 "Debugger thread reported exception {0:x} at address {1:x}, " 699 "but there is no session.", 700 record.GetExceptionCode(), record.GetExceptionAddress()); 701 return ExceptionResult::SendToApplication; 702 } 703 704 if (!first_chance) { 705 // Not any second chance exception is an application crash by definition. 706 // It may be an expression evaluation crash. 707 SetPrivateState(eStateStopped); 708 } 709 710 ExceptionResult result = ExceptionResult::SendToApplication; 711 switch (record.GetExceptionCode()) { 712 case EXCEPTION_BREAKPOINT: 713 // Handle breakpoints at the first chance. 714 result = ExceptionResult::BreakInDebugger; 715 716 if (!m_session_data->m_initial_stop_received) { 717 LLDB_LOG( 718 log, 719 "Hit loader breakpoint at address {0:x}, setting initial stop event.", 720 record.GetExceptionAddress()); 721 m_session_data->m_initial_stop_received = true; 722 ::SetEvent(m_session_data->m_initial_stop_event); 723 } else { 724 LLDB_LOG(log, "Hit non-loader breakpoint at address {0:x}.", 725 record.GetExceptionAddress()); 726 } 727 SetPrivateState(eStateStopped); 728 break; 729 case EXCEPTION_SINGLE_STEP: 730 result = ExceptionResult::BreakInDebugger; 731 SetPrivateState(eStateStopped); 732 break; 733 default: 734 LLDB_LOG(log, 735 "Debugger thread reported exception {0:x} at address {1:x} " 736 "(first_chance={2})", 737 record.GetExceptionCode(), record.GetExceptionAddress(), 738 first_chance); 739 // For non-breakpoints, give the application a chance to handle the 740 // exception first. 741 if (first_chance) 742 result = ExceptionResult::SendToApplication; 743 else 744 result = ExceptionResult::BreakInDebugger; 745 } 746 747 return result; 748 } 749 750 void ProcessWindows::OnCreateThread(const HostThread &new_thread) { 751 llvm::sys::ScopedLock lock(m_mutex); 752 753 ThreadSP thread = std::make_shared<TargetThreadWindows>(*this, new_thread); 754 755 const HostNativeThread &native_new_thread = new_thread.GetNativeThread(); 756 tid_t id = native_new_thread.GetThreadId(); 757 thread->SetID(id); 758 759 m_session_data->m_new_threads[id] = thread; 760 761 for (const std::map<int, WatchpointInfo>::value_type &p : m_watchpoints) { 762 auto *reg_ctx = static_cast<RegisterContextWindows *>( 763 thread->GetRegisterContext().get()); 764 reg_ctx->AddHardwareBreakpoint(p.second.slot_id, p.second.address, 765 p.second.size, p.second.read, 766 p.second.write); 767 } 768 } 769 770 void ProcessWindows::OnExitThread(lldb::tid_t thread_id, uint32_t exit_code) { 771 llvm::sys::ScopedLock lock(m_mutex); 772 773 // On a forced termination, we may get exit thread events after the session 774 // data has been cleaned up. 775 if (!m_session_data) 776 return; 777 778 // A thread may have started and exited before the debugger stopped allowing a 779 // refresh. 780 // Just remove it from the new threads list in that case. 781 auto iter = m_session_data->m_new_threads.find(thread_id); 782 if (iter != m_session_data->m_new_threads.end()) 783 m_session_data->m_new_threads.erase(iter); 784 else 785 m_session_data->m_exited_threads.insert(thread_id); 786 } 787 788 void ProcessWindows::OnLoadDll(const ModuleSpec &module_spec, 789 lldb::addr_t module_addr) { 790 if (auto dyld = GetDynamicLoader()) 791 dyld->OnLoadModule(nullptr, module_spec, module_addr); 792 } 793 794 void ProcessWindows::OnUnloadDll(lldb::addr_t module_addr) { 795 if (auto dyld = GetDynamicLoader()) 796 dyld->OnUnloadModule(module_addr); 797 } 798 799 void ProcessWindows::OnDebugString(const std::string &string) {} 800 801 void ProcessWindows::OnDebuggerError(const Status &error, uint32_t type) { 802 llvm::sys::ScopedLock lock(m_mutex); 803 Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS); 804 805 if (m_session_data->m_initial_stop_received) { 806 // This happened while debugging. Do we shutdown the debugging session, 807 // try to continue, or do something else? 808 LLDB_LOG(log, 809 "Error {0} occurred during debugging. Unexpected behavior " 810 "may result. {1}", 811 error.GetError(), error); 812 } else { 813 // If we haven't actually launched the process yet, this was an error 814 // launching the process. Set the internal error and signal the initial 815 // stop event so that the DoLaunch method wakes up and returns a failure. 816 m_session_data->m_launch_error = error; 817 ::SetEvent(m_session_data->m_initial_stop_event); 818 LLDB_LOG( 819 log, 820 "Error {0} occurred launching the process before the initial stop. {1}", 821 error.GetError(), error); 822 return; 823 } 824 } 825 826 Status ProcessWindows::GetWatchpointSupportInfo(uint32_t &num) { 827 num = RegisterContextWindows::GetNumHardwareBreakpointSlots(); 828 return {}; 829 } 830 831 Status ProcessWindows::GetWatchpointSupportInfo(uint32_t &num, bool &after) { 832 num = RegisterContextWindows::GetNumHardwareBreakpointSlots(); 833 after = RegisterContextWindows::DoHardwareBreakpointsTriggerAfter(); 834 return {}; 835 } 836 837 Status ProcessWindows::EnableWatchpoint(Watchpoint *wp, bool notify) { 838 Status error; 839 840 if (wp->IsEnabled()) { 841 wp->SetEnabled(true, notify); 842 return error; 843 } 844 845 WatchpointInfo info; 846 for (info.slot_id = 0; 847 info.slot_id < RegisterContextWindows::GetNumHardwareBreakpointSlots(); 848 info.slot_id++) 849 if (m_watchpoint_ids[info.slot_id] == LLDB_INVALID_BREAK_ID) 850 break; 851 if (info.slot_id == RegisterContextWindows::GetNumHardwareBreakpointSlots()) { 852 error.SetErrorStringWithFormat("Can't find free slot for watchpoint %i", 853 wp->GetID()); 854 return error; 855 } 856 info.address = wp->GetLoadAddress(); 857 info.size = wp->GetByteSize(); 858 info.read = wp->WatchpointRead(); 859 info.write = wp->WatchpointWrite(); 860 861 for (unsigned i = 0U; i < m_thread_list.GetSize(); i++) { 862 Thread *thread = m_thread_list.GetThreadAtIndex(i).get(); 863 auto *reg_ctx = static_cast<RegisterContextWindows *>( 864 thread->GetRegisterContext().get()); 865 if (!reg_ctx->AddHardwareBreakpoint(info.slot_id, info.address, info.size, 866 info.read, info.write)) { 867 error.SetErrorStringWithFormat( 868 "Can't enable watchpoint %i on thread 0x%llx", wp->GetID(), 869 thread->GetID()); 870 break; 871 } 872 } 873 if (error.Fail()) { 874 for (unsigned i = 0U; i < m_thread_list.GetSize(); i++) { 875 Thread *thread = m_thread_list.GetThreadAtIndex(i).get(); 876 auto *reg_ctx = static_cast<RegisterContextWindows *>( 877 thread->GetRegisterContext().get()); 878 reg_ctx->RemoveHardwareBreakpoint(info.slot_id); 879 } 880 return error; 881 } 882 883 m_watchpoints[wp->GetID()] = info; 884 m_watchpoint_ids[info.slot_id] = wp->GetID(); 885 886 wp->SetEnabled(true, notify); 887 888 return error; 889 } 890 891 Status ProcessWindows::DisableWatchpoint(Watchpoint *wp, bool notify) { 892 Status error; 893 894 if (!wp->IsEnabled()) { 895 wp->SetEnabled(false, notify); 896 return error; 897 } 898 899 auto it = m_watchpoints.find(wp->GetID()); 900 if (it == m_watchpoints.end()) { 901 error.SetErrorStringWithFormat("Info about watchpoint %i is not found", 902 wp->GetID()); 903 return error; 904 } 905 906 for (unsigned i = 0U; i < m_thread_list.GetSize(); i++) { 907 Thread *thread = m_thread_list.GetThreadAtIndex(i).get(); 908 auto *reg_ctx = static_cast<RegisterContextWindows *>( 909 thread->GetRegisterContext().get()); 910 if (!reg_ctx->RemoveHardwareBreakpoint(it->second.slot_id)) { 911 error.SetErrorStringWithFormat( 912 "Can't disable watchpoint %i on thread 0x%llx", wp->GetID(), 913 thread->GetID()); 914 break; 915 } 916 } 917 if (error.Fail()) 918 return error; 919 920 m_watchpoint_ids[it->second.slot_id] = LLDB_INVALID_BREAK_ID; 921 m_watchpoints.erase(it); 922 923 wp->SetEnabled(false, notify); 924 925 return error; 926 } 927 } // namespace lldb_private 928