| // Copyright 2019 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. | |
| #include "src/developer/debug/debug_agent/debugged_process.h" | |
| #include <lib/component/incoming/cpp/protocol.h> | |
| #include <lib/fit/defer.h> | |
| #include <lib/syslog/cpp/macros.h> | |
| #include <lib/zx/process.h> | |
| #include <lib/zx/thread.h> | |
| #include <zircon/syscalls/object.h> | |
| #include <memory> | |
| #include <optional> | |
| #include <set> | |
| #include <string> | |
| #include <vector> | |
| #include <gtest/gtest.h> | |
| #include "src/developer/debug/debug_agent/arch.h" | |
| #include "src/developer/debug/debug_agent/breakpoint.h" | |
| #include "src/developer/debug/debug_agent/debug_agent.h" | |
| #include "src/developer/debug/debug_agent/debugged_thread.h" | |
| #include "src/developer/debug/debug_agent/failing_write_stream.h" | |
| #include "src/developer/debug/debug_agent/hardware_breakpoint.h" | |
| #include "src/developer/debug/debug_agent/mock_debug_agent_harness.h" | |
| #include "src/developer/debug/debug_agent/mock_exception_handle.h" | |
| #include "src/developer/debug/debug_agent/mock_process.h" | |
| #include "src/developer/debug/debug_agent/mock_thread.h" | |
| #include "src/developer/debug/debug_agent/mock_thread_handle.h" | |
| #include "src/developer/debug/debug_agent/software_breakpoint.h" | |
| #include "src/developer/debug/debug_agent/test_utils.h" | |
| #include "src/developer/debug/debug_agent/zircon_binary_launcher.h" | |
| #include "src/developer/debug/ipc/protocol.h" | |
| #include "src/developer/debug/ipc/records.h" | |
| #include "src/developer/debug/shared/test_with_loop.h" | |
| namespace debug_agent { | |
| namespace { | |
| class MockProcessDelegate : public Breakpoint::ProcessDelegate { | |
| public: | |
| debug::Status RegisterBreakpoint(Breakpoint*, zx_koid_t, uint64_t) override { | |
| return debug::Status(); | |
| } | |
| void UnregisterBreakpoint(Breakpoint* bp, zx_koid_t process_koid, uint64_t address) override {} | |
| debug::Status RegisterWatchpoint(Breakpoint*, zx_koid_t, const debug::AddressRange&) override { | |
| return debug::Status(); | |
| } | |
| void UnregisterWatchpoint(Breakpoint*, zx_koid_t, const debug::AddressRange&) override {} | |
| }; | |
| // Fills a vector with the breakpoint instruction for the current architecture. The size will vary | |
| // by architecture. | |
| std::vector<uint8_t> GetBreakpointMemory() { | |
| std::vector<uint8_t> result; | |
| result.resize(arch::kBreakInstructionSize); | |
| memcpy(result.data(), &arch::kBreakInstruction, arch::kBreakInstructionSize); | |
| return result; | |
| } | |
| // If |thread| is null, it means a process-wide breakpoint. | |
| debug_ipc::ProcessBreakpointSettings CreateLocation(zx_koid_t process_koid, zx_koid_t thread_koid, | |
| uint64_t address) { | |
| debug_ipc::ProcessBreakpointSettings location = {}; | |
| location.id = {.process = process_koid, .thread = thread_koid}; | |
| location.address = address; | |
| return location; | |
| } | |
| debug_ipc::ProcessBreakpointSettings CreateLocation(zx_koid_t process_koid, zx_koid_t thread_koid, | |
| const debug::AddressRange& range) { | |
| debug_ipc::ProcessBreakpointSettings location = {}; | |
| location.id = {.process = process_koid, .thread = thread_koid}; | |
| location.address_range = range; | |
| return location; | |
| } | |
| debug::AddressRange SetLocation(Breakpoint* breakpoint, zx_koid_t koid, | |
| const debug::AddressRange& range) { | |
| debug_ipc::BreakpointSettings settings; | |
| settings.type = debug_ipc::BreakpointType::kWrite; | |
| settings.locations.push_back(CreateLocation(koid, 0, range)); | |
| breakpoint->SetSettings(settings); | |
| return range; | |
| } | |
| const MockThreadHandle& AsMockThreadHandle(const DebuggedThread* thread) { | |
| return static_cast<const MockThreadHandle&>(thread->thread_handle()); | |
| } | |
| void ExpectProcessSuspendEventOrder(const std::vector<std::string>& events, | |
| size_t expected_notify_count) { | |
| ASSERT_FALSE(events.empty()); | |
| EXPECT_EQ(events.front(), "process_suspend"); | |
| size_t suspend_count = 0; | |
| size_t resume_count = 0; | |
| size_t enum_count = 0; | |
| size_t notify_count = 0; | |
| std::optional<size_t> resume_index; | |
| for (size_t i = 0; i < events.size(); ++i) { | |
| if (events[i] == "process_suspend") { | |
| ++suspend_count; | |
| } else if (events[i] == "process_resume") { | |
| ++resume_count; | |
| resume_index = i; | |
| } else if (events[i] == "enum") { | |
| ++enum_count; | |
| EXPECT_FALSE(resume_index.has_value()) << "enum at index " << i << " after process_resume"; | |
| } else if (events[i] == "notify") { | |
| ++notify_count; | |
| EXPECT_TRUE(resume_index.has_value()) << "notify at index " << i << " before process_resume"; | |
| } else { | |
| ADD_FAILURE() << "Unexpected event: " << events[i]; | |
| } | |
| } | |
| EXPECT_EQ(suspend_count, 1u); | |
| EXPECT_EQ(resume_count, 1u); | |
| EXPECT_GT(enum_count, 0u); | |
| EXPECT_EQ(notify_count, expected_notify_count); | |
| } | |
| // Tests ------------------------------------------------------------------------------------------- | |
| constexpr zx_koid_t kProcessKoid = 0x1; | |
| // Owns a MockDebugAgentHarness with an injected MockProcess and clears any thread-starting | |
| // callback before the harness tears down. | |
| struct ProcessTestHarness { | |
| explicit ProcessTestHarness(zx_koid_t process_koid = kProcessKoid) | |
| : process(harness.AddProcess(process_koid)) {} | |
| ~ProcessTestHarness() { harness.stream_backend()->set_notify_thread_starting_callback({}); } | |
| MockDebugAgentHarness harness; | |
| MockProcess* process = nullptr; | |
| }; | |
| const std::string kProcessName = "process-name"; | |
| constexpr uint64_t kAddress1 = 0x1234; | |
| constexpr uint64_t kAddress2 = 0x5678; | |
| constexpr uint64_t kAddress3 = 0x9abc; | |
| constexpr uint64_t kAddress4 = 0xdef0; | |
| constexpr debug::AddressRange kAddressRange1 = {0x1, 0x2}; | |
| TEST(DebuggedProcess, RegisterBreakpoints) { | |
| MockProcessDelegate process_delegate; | |
| MockProcess process(nullptr, kProcessKoid, kProcessName); | |
| // There needs to be memory backing the breakpoints so set some memory here. When the breakpoint | |
| // is uninstalled, the breakpoint expects that it will be the debug break instruction. So we | |
| // handle both cases and always report a breakpoint at these addresses. | |
| process.mock_process_handle().mock_memory().AddMemory(kAddress1, GetBreakpointMemory()); | |
| process.mock_process_handle().mock_memory().AddMemory(kAddress2, GetBreakpointMemory()); | |
| process.mock_process_handle().mock_memory().AddMemory(kAddress3, GetBreakpointMemory()); | |
| process.mock_process_handle().mock_memory().AddMemory(kAddress4, GetBreakpointMemory()); | |
| debug_ipc::BreakpointSettings settings; | |
| settings.type = debug_ipc::BreakpointType::kSoftware; | |
| settings.locations.push_back(CreateLocation(kProcessKoid, 0, kAddress1)); | |
| settings.locations.push_back(CreateLocation(kProcessKoid, 0, kAddress2)); | |
| settings.locations.push_back(CreateLocation(kProcessKoid, 0, kAddress3)); | |
| Breakpoint breakpoint(&process_delegate); | |
| breakpoint.SetSettings(settings); | |
| ASSERT_TRUE(process.RegisterBreakpoint(&breakpoint, kAddress1).ok()); | |
| ASSERT_EQ(process.software_breakpoints().size(), 1u); | |
| auto it = process.software_breakpoints().begin(); | |
| EXPECT_EQ(it++->first, kAddress1); | |
| EXPECT_EQ(it, process.software_breakpoints().end()); | |
| // Add 2 other breakpoints. | |
| ASSERT_TRUE(process.RegisterBreakpoint(&breakpoint, kAddress2).ok()); | |
| ASSERT_TRUE(process.RegisterBreakpoint(&breakpoint, kAddress3).ok()); | |
| ASSERT_EQ(process.software_breakpoints().size(), 3u); | |
| it = process.software_breakpoints().begin(); | |
| EXPECT_EQ(it++->first, kAddress1); | |
| EXPECT_EQ(it++->first, kAddress2); | |
| EXPECT_EQ(it++->first, kAddress3); | |
| EXPECT_EQ(it, process.software_breakpoints().end()); | |
| // Unregister a breakpoint. | |
| process.UnregisterBreakpoint(&breakpoint, kAddress1); | |
| ASSERT_EQ(process.software_breakpoints().size(), 2u); | |
| it = process.software_breakpoints().begin(); | |
| EXPECT_EQ(it++->first, kAddress2); | |
| EXPECT_EQ(it++->first, kAddress3); | |
| EXPECT_EQ(it, process.software_breakpoints().end()); | |
| // Register a hardware breakpoint. | |
| Breakpoint hw_breakpoint(&process_delegate); | |
| debug_ipc::BreakpointSettings hw_settings; | |
| hw_settings.type = debug_ipc::BreakpointType::kHardware; | |
| hw_settings.locations.push_back(CreateLocation(kProcessKoid, 0, kAddress4)); | |
| hw_breakpoint.SetSettings(hw_settings); | |
| ASSERT_TRUE(process.RegisterBreakpoint(&hw_breakpoint, kAddress3).ok()); | |
| ASSERT_TRUE(process.RegisterBreakpoint(&hw_breakpoint, kAddress4).ok()); | |
| // Should've inserted 2 HW breakpoint. | |
| ASSERT_EQ(process.software_breakpoints().size(), 2u); | |
| ASSERT_EQ(process.hardware_breakpoints().size(), 2u); | |
| auto hw_it = process.hardware_breakpoints().begin(); | |
| EXPECT_EQ(hw_it++->first, kAddress3); | |
| EXPECT_EQ(hw_it++->first, kAddress4); | |
| EXPECT_EQ(hw_it, process.hardware_breakpoints().end()); | |
| // Remove a hardware breakpoint. | |
| process.UnregisterBreakpoint(&hw_breakpoint, kAddress3); | |
| ASSERT_EQ(process.software_breakpoints().size(), 2u); | |
| ASSERT_EQ(process.hardware_breakpoints().size(), 1u); | |
| hw_it = process.hardware_breakpoints().begin(); | |
| EXPECT_EQ(hw_it++->first, kAddress4); | |
| EXPECT_EQ(hw_it, process.hardware_breakpoints().end()); | |
| // Add a watchpoint. | |
| Breakpoint wp_breakpoint(&process_delegate); | |
| debug_ipc::BreakpointSettings wp_settings; | |
| wp_settings.type = debug_ipc::BreakpointType::kWrite; | |
| wp_settings.locations.push_back(CreateLocation(kProcessKoid, 0, kAddressRange1)); | |
| wp_breakpoint.SetSettings(wp_settings); | |
| ASSERT_TRUE(process.RegisterWatchpoint(&wp_breakpoint, kAddressRange1).ok()); | |
| ASSERT_EQ(process.software_breakpoints().size(), 2u); | |
| ASSERT_EQ(process.hardware_breakpoints().size(), 1u); | |
| ASSERT_EQ(process.watchpoints().size(), 1u); | |
| auto wp_it = process.watchpoints().begin(); | |
| EXPECT_EQ(wp_it++->first, kAddressRange1); | |
| EXPECT_EQ(wp_it, process.watchpoints().end()); | |
| } | |
| TEST(DebuggedProcess, WatchpointRegistration) { | |
| MockProcessDelegate process_delegate; | |
| Breakpoint breakpoint(&process_delegate); | |
| MockProcess process(nullptr, kProcessKoid, kProcessName); | |
| // 1 byte. | |
| for (uint32_t i = 0; i < 16; i++) { | |
| debug::AddressRange range = {0x10 + i, 0x10 + i + 1}; | |
| SetLocation(&breakpoint, process.koid(), range); | |
| ASSERT_TRUE(process.RegisterWatchpoint(&breakpoint, range).ok()); | |
| } | |
| // 2 bytes. | |
| for (uint32_t i = 0; i < 16; i++) { | |
| debug::AddressRange range = {0x10 + i, 0x10 + i + 2}; | |
| SetLocation(&breakpoint, process.koid(), range); | |
| // Only aligned values should work. | |
| bool expected_ok = (range.begin() & 0b1u) == 0; | |
| SCOPED_TRACE(range.ToString()); | |
| ASSERT_EQ(process.RegisterWatchpoint(&breakpoint, range).ok(), expected_ok); | |
| } | |
| // 3 bytes. | |
| for (uint32_t i = 0; i < 16; i++) { | |
| debug::AddressRange range = {0x10 + i, 0x10 + i + 3}; | |
| SetLocation(&breakpoint, process.koid(), range); | |
| ASSERT_TRUE(process.RegisterWatchpoint(&breakpoint, range).has_error()); | |
| } | |
| // 4 bytes. | |
| for (uint32_t i = 0; i < 16; i++) { | |
| debug::AddressRange range = {0x10 + i, 0x10 + i + 4}; | |
| SetLocation(&breakpoint, process.koid(), range); | |
| // Only aligned values should work. | |
| bool expected_ok = (range.begin() & 0b11u) == 0; | |
| SCOPED_TRACE(range.ToString()); | |
| ASSERT_EQ(process.RegisterWatchpoint(&breakpoint, range).ok(), expected_ok); | |
| } | |
| // 5 bytes. | |
| for (uint32_t i = 0; i < 16; i++) { | |
| debug::AddressRange range = {0x10 + i, 0x10 + i + 5}; | |
| SetLocation(&breakpoint, process.koid(), range); | |
| ASSERT_TRUE(process.RegisterWatchpoint(&breakpoint, range).has_error()); | |
| } | |
| // 6 bytes. | |
| for (uint32_t i = 0; i < 16; i++) { | |
| debug::AddressRange range = {0x10 + i, 0x10 + i + 6}; | |
| SetLocation(&breakpoint, process.koid(), range); | |
| ASSERT_TRUE(process.RegisterWatchpoint(&breakpoint, range).has_error()); | |
| } | |
| // 6 bytes. | |
| for (uint32_t i = 0; i < 16; i++) { | |
| debug::AddressRange range = {0x10 + i, 0x10 + i + 6}; | |
| SetLocation(&breakpoint, process.koid(), range); | |
| ASSERT_TRUE(process.RegisterWatchpoint(&breakpoint, range).has_error()); | |
| } | |
| // 7 bytes. | |
| for (uint32_t i = 0; i < 16; i++) { | |
| debug::AddressRange range = {0x10 + i, 0x10 + i + 7}; | |
| SetLocation(&breakpoint, process.koid(), range); | |
| ASSERT_TRUE(process.RegisterWatchpoint(&breakpoint, range).has_error()); | |
| } | |
| // 8 bytes. | |
| for (uint32_t i = 0; i < 16; i++) { | |
| debug::AddressRange range = {0x10 + i, 0x10 + i + 8}; | |
| SetLocation(&breakpoint, process.koid(), range); | |
| // Only aligned values should work. | |
| bool expected_ok = (range.begin() & 0b111u) == 0; | |
| SCOPED_TRACE(range.ToString()); | |
| ASSERT_EQ(process.RegisterWatchpoint(&breakpoint, range).ok(), expected_ok); | |
| } | |
| } | |
| TEST(DebuggedProcess, DetachFromProcess) { | |
| MockProcess process(nullptr, kProcessKoid, kProcessName); | |
| MockThread* thread = process.AddThread(2); | |
| ASSERT_TRUE(thread->running()); | |
| process.ClientSuspendAllThreads(); | |
| ASSERT_FALSE(thread->running()); | |
| process.DetachFromProcess(); | |
| ASSERT_TRUE(thread->running()); | |
| } | |
| TEST(DebuggedProcess, DetachFromProcessWithBreakpoints) { | |
| MockProcess process(nullptr, kProcessKoid, kProcessName); | |
| MockThread* thread = process.AddThread(1234); | |
| MockProcessDelegate delegate; | |
| Breakpoint bp1 = Breakpoint(&delegate); | |
| debug_ipc::BreakpointSettings settings; | |
| settings.type = debug_ipc::BreakpointType::kHardware; | |
| settings.locations.push_back(CreateLocation(process.koid(), thread->koid(), kAddress1)); | |
| bp1.SetSettings(settings); | |
| ASSERT_TRUE(process.RegisterBreakpoint(&bp1, kAddress1).ok()); | |
| Breakpoint bp2 = Breakpoint(&delegate); | |
| debug_ipc::BreakpointSettings settings2; | |
| settings2.type = debug_ipc::BreakpointType::kSoftware; | |
| settings2.locations.push_back(CreateLocation(process.koid(), thread->koid(), kAddress2)); | |
| bp2.SetSettings(settings2); | |
| // The software breakpoint needs some memory backing it. | |
| process.mock_process_handle().mock_memory().AddMemory(kAddress2, GetBreakpointMemory()); | |
| ASSERT_TRUE(process.RegisterBreakpoint(&bp2, kAddress2).ok()); | |
| process.DetachFromProcess(); | |
| // Now the thread should be running again and there shouldn't be any more breakpoints. | |
| EXPECT_TRUE(process.hardware_breakpoints().empty()); | |
| EXPECT_TRUE(process.software_breakpoints().empty()); | |
| EXPECT_TRUE(thread->running()); | |
| } | |
| TEST(DebuggedProcess, WeakAttachSkipsLoaderBreakpoint) { | |
| MockDebugAgentHarness harness; | |
| DebuggedProcessCreateInfo info(std::make_unique<MockProcessHandle>(kProcessKoid, kProcessName)); | |
| info.priority = debug_ipc::AttachConfig::Priority::kWeak; | |
| auto unique_process = std::make_unique<MockProcess>(harness.debug_agent(), std::move(info)); | |
| DebuggedProcess* process = unique_process.get(); | |
| harness.debug_agent()->InjectProcessForTest(std::move(unique_process)); | |
| process->HandleLoaderBreakpoint(MockProcessHandle::kLoaderBreakpointAddress); | |
| // Should not have sent modules. | |
| EXPECT_TRUE(harness.stream_backend()->modules().empty()); | |
| } | |
| TEST(DebuggedProcess, ExceptionEnsuresThreadsAreSynced) { | |
| MockDebugAgentHarness harness; | |
| auto mock_process = harness.AddProcess(kProcessKoid); | |
| // There should be no threads initially. | |
| ASSERT_TRUE(mock_process->GetThreads().empty()); | |
| constexpr zx_koid_t kThreadKoid = 2; | |
| // Inject a thread to the underlying process handle object, which will be the thread that receives | |
| // the exception. | |
| mock_process->mock_process_handle().set_threads({MockThreadHandle(kThreadKoid)}); | |
| // Dispatch the exception. | |
| mock_process->InjectException(std::make_unique<MockExceptionHandle>(kProcessKoid, kThreadKoid)); | |
| // Now the DebuggedProcess should see it's child thread even though it didn't get a ThreadStarting | |
| // notification. | |
| ASSERT_EQ(mock_process->GetThreads().size(), 1u); | |
| EXPECT_EQ(mock_process->GetThreads()[0]->koid(), kThreadKoid); | |
| // We should also receive an exception notification. | |
| ASSERT_EQ(harness.stream_backend()->exceptions().size(), 1u); | |
| EXPECT_EQ(harness.stream_backend()->exceptions()[0].thread.id.process, kProcessKoid); | |
| EXPECT_EQ(harness.stream_backend()->exceptions()[0].thread.id.thread, kThreadKoid); | |
| } | |
| TEST(DebuggedProcessTest, ClientResumeUnsetsSuspendNewThreads) { | |
| MockDebugAgentHarness harness; | |
| constexpr uint64_t kThread1Koid = 1234; | |
| constexpr uint64_t kThread2Koid = 1235; | |
| constexpr uint64_t kThread3Koid = 1236; | |
| auto* mock_process = harness.AddProcess(kProcessKoid); | |
| mock_process->AddThread(kThread1Koid); | |
| ASSERT_TRUE(mock_process); | |
| ASSERT_EQ(mock_process->GetThreads().size(), 1u); | |
| std::ignore = mock_process->ClientSuspendAllThreads(); | |
| auto* debugged_thread = mock_process->GetThread(kThread1Koid); | |
| ASSERT_TRUE(debugged_thread); | |
| EXPECT_TRUE(debugged_thread->is_client_suspended()); | |
| // Inject the thread starting notification. The thread should now be suspended. We have to inject | |
| // the new thread into the MockProcessHandle so DebuggedProcess can re-populate its threads to | |
| // find the new one. | |
| MockThreadHandle thread2_handle(kThread2Koid); | |
| mock_process->mock_process_handle().AddThreadAndSendEvent(std::move(thread2_handle)); | |
| auto* debugged_thread2 = mock_process->GetThread(kThread2Koid); | |
| ASSERT_TRUE(debugged_thread2); | |
| EXPECT_TRUE(debugged_thread2->is_client_suspended()); | |
| // Issue the resume as separate ProcessThreadIds, which simulates how zxdb does a "Process | |
| // Continue". | |
| debug_ipc::ResumeRequest resume_request; | |
| resume_request.how = debug_ipc::ResumeRequest::How::kResolveAndContinue; | |
| resume_request.ids = { | |
| {.process = kProcessKoid, .thread = kThread1Koid}, | |
| {.process = kProcessKoid, .thread = kThread2Koid}, | |
| }; | |
| mock_process->OnResume(resume_request); | |
| EXPECT_FALSE(debugged_thread->is_client_suspended()); | |
| EXPECT_FALSE(debugged_thread2->is_client_suspended()); | |
| MockThreadHandle thread3_handle(kThread3Koid); | |
| mock_process->mock_process_handle().AddThreadAndSendEvent(std::move(thread3_handle)); | |
| auto* thread3 = mock_process->GetThread(kThread3Koid); | |
| ASSERT_TRUE(thread3); | |
| EXPECT_FALSE(thread3->is_client_suspended()); | |
| } | |
| class ThreadStartingExceptionHandle : public MockExceptionHandle { | |
| public: | |
| ThreadStartingExceptionHandle(uint64_t process_koid, const MockThreadHandle* thread_handle, | |
| bool* exception_open_out, bool* was_suspended_at_close_out) | |
| : MockExceptionHandle(process_koid, | |
| thread_handle ? thread_handle->GetKoid() : ZX_KOID_INVALID), | |
| thread_handle_(thread_handle), | |
| exception_open_out_(exception_open_out), | |
| was_suspended_at_close_out_(was_suspended_at_close_out) { | |
| FX_CHECK(thread_handle_); | |
| if (exception_open_out_) { | |
| *exception_open_out_ = true; | |
| } | |
| } | |
| ~ThreadStartingExceptionHandle() override { | |
| if (was_suspended_at_close_out_) { | |
| *was_suspended_at_close_out_ = thread_handle_->is_suspended(); | |
| } | |
| if (exception_open_out_) { | |
| *exception_open_out_ = false; | |
| } | |
| } | |
| std::unique_ptr<ThreadHandle> GetThreadHandle() const override { | |
| return std::make_unique<MockThreadHandle>(*thread_handle_); | |
| } | |
| private: | |
| const MockThreadHandle* thread_handle_ = nullptr; | |
| bool* exception_open_out_ = nullptr; | |
| bool* was_suspended_at_close_out_ = nullptr; | |
| }; | |
| TEST(DebuggedProcessTest, OnThreadStartingSuspendsBeforeReleasingStartException) { | |
| bool exception_open = false; | |
| bool was_suspended_at_close = false; | |
| bool notified = false; | |
| constexpr zx_koid_t kThreadKoid = 43; | |
| MockThreadHandle thread_handle(kThreadKoid, "paused-worker"); | |
| MockDebugAgentHarness harness; | |
| auto clear_callback = | |
| fit::defer([&harness] { harness.stream_backend()->set_notify_thread_starting_callback({}); }); | |
| auto* mock_process = harness.AddProcess(kProcessKoid); | |
| mock_process->ClientSuspendAllThreads(); | |
| harness.stream_backend()->set_notify_thread_starting_callback( | |
| [&](const debug_ipc::NotifyThreadStarting& notify) { | |
| notified = true; | |
| EXPECT_FALSE(exception_open); | |
| EXPECT_NE(mock_process->GetThread(kThreadKoid), nullptr); | |
| EXPECT_EQ(kProcessKoid, notify.record.id.process); | |
| EXPECT_EQ(kThreadKoid, notify.record.id.thread); | |
| EXPECT_EQ("paused-worker", notify.record.name); | |
| }); | |
| static_cast<ProcessHandleObserver*>(mock_process) | |
| ->OnThreadStarting(std::make_unique<ThreadStartingExceptionHandle>( | |
| kProcessKoid, &thread_handle, &exception_open, &was_suspended_at_close)); | |
| EXPECT_TRUE(notified); | |
| EXPECT_TRUE(was_suspended_at_close); | |
| EXPECT_FALSE(exception_open); | |
| EXPECT_NE(mock_process->GetThread(kThreadKoid), nullptr); | |
| ASSERT_EQ(1u, harness.stream_backend()->thread_starts().size()); | |
| } | |
| TEST(DebuggedProcessTest, OnThreadStartingIgnoresDuplicateThreadKoid) { | |
| MockDebugAgentHarness harness; | |
| auto* mock_process = harness.AddProcess(kProcessKoid); | |
| constexpr zx_koid_t kThreadKoid = 44; | |
| MockThreadHandle thread_handle(kThreadKoid, "worker"); | |
| mock_process->mock_process_handle().set_threads({thread_handle}); | |
| mock_process->PopulateCurrentThreads(); | |
| ASSERT_NE(mock_process->GetThread(kThreadKoid), nullptr); | |
| ASSERT_EQ(1u, harness.stream_backend()->thread_starts().size()); | |
| static_cast<ProcessHandleObserver*>(mock_process) | |
| ->OnThreadStarting(std::make_unique<MockExceptionHandle>(kProcessKoid, kThreadKoid)); | |
| EXPECT_EQ(1u, harness.stream_backend()->thread_starts().size()); | |
| EXPECT_EQ(1u, mock_process->GetThreads().size()); | |
| } | |
| class DebuggedProcessZirconTest : public debug::TestWithLoop { | |
| protected: | |
| // Launches a zircon child process and runs the loop until its initial thread-start arrives. | |
| void RunThreadStartOnZirconProcess(bool suspend_before_start, | |
| debug_ipc::NotifyThreadStarting* out_notify, | |
| zx_thread_state_t* out_thread_state_at_notify) { | |
| auto svc_dir = component::OpenServiceRoot(); | |
| ASSERT_TRUE(svc_dir.is_ok()) << svc_dir.status_string(); | |
| ZirconBinaryLauncher launcher(*svc_dir); | |
| ASSERT_TRUE( | |
| launcher.Setup({"/pkg/test/debug_agent_unit_tests", "--gtest_filter=NonExistent"}).ok()); | |
| bool got_thread_start = false; | |
| auto timed_out = std::make_shared<bool>(false); | |
| DebuggedProcess* process = nullptr; | |
| MockDebugAgentHarness harness; | |
| auto clear_callback = fit::defer( | |
| [&harness] { harness.stream_backend()->set_notify_thread_starting_callback({}); }); | |
| std::unique_ptr<ProcessHandle> process_handle = launcher.GetProcess(); | |
| ASSERT_TRUE(process_handle); | |
| zx::process child_process; | |
| const zx_status_t dup_status = process_handle->GetNativeHandle().duplicate( | |
| ZX_RIGHT_SAME_RIGHTS, &child_process); // NOLINT(bugprone-signed-bitwise) | |
| ASSERT_ZX_EQ(dup_status, ZX_OK); | |
| auto kill_child = fit::defer([&child_process] { | |
| if (child_process.is_valid()) { | |
| child_process.kill(); | |
| } | |
| }); | |
| auto debugged_process = std::make_unique<DebuggedProcess>(harness.debug_agent()); | |
| process = debugged_process.get(); | |
| ASSERT_TRUE(debugged_process->Init(DebuggedProcessCreateInfo(std::move(process_handle))).ok()); | |
| harness.debug_agent()->InjectProcessForTest(std::move(debugged_process)); | |
| if (suspend_before_start) { | |
| process->ClientSuspendAllThreads(); | |
| } | |
| harness.stream_backend()->set_notify_thread_starting_callback( | |
| [&](const debug_ipc::NotifyThreadStarting& notify) { | |
| if (!got_thread_start) { | |
| got_thread_start = true; | |
| *out_notify = notify; | |
| DebuggedThread* thread = process->GetThread(notify.record.id.thread); | |
| EXPECT_NE(nullptr, thread); | |
| if (thread && out_thread_state_at_notify) { | |
| zx_info_thread_t thread_info = {}; | |
| EXPECT_ZX_EQ(thread->thread_handle().GetNativeHandle().get_info( | |
| ZX_INFO_THREAD, &thread_info, sizeof(thread_info), nullptr, nullptr), | |
| ZX_OK); | |
| *out_thread_state_at_notify = thread_info.state; | |
| } | |
| loop().QuitNow(); | |
| } | |
| }); | |
| loop().PostTimer(FROM_HERE, 10000, [timed_out, loop = &loop()] { | |
| *timed_out = true; | |
| loop->QuitNow(); | |
| }); | |
| ASSERT_TRUE(launcher.Start().ok()); | |
| loop().Run(); | |
| ASSERT_FALSE(*timed_out); | |
| ASSERT_TRUE(got_thread_start); | |
| ASSERT_EQ(1u, harness.stream_backend()->thread_starts().size()); | |
| } | |
| }; | |
| TEST_F(DebuggedProcessZirconTest, OnThreadStartingEmitsRunningWhenZirconProcessNotSuspended) { | |
| debug_ipc::NotifyThreadStarting notify; | |
| zx_thread_state_t thread_state = ZX_THREAD_STATE_NEW; | |
| ASSERT_NO_FATAL_FAILURE( | |
| RunThreadStartOnZirconProcess(/*suspend_before_start=*/false, ¬ify, &thread_state)); | |
| EXPECT_EQ(ZX_THREAD_STATE_BLOCKED_EXCEPTION, thread_state); | |
| EXPECT_EQ(debug_ipc::ThreadRecord::State::kRunning, notify.record.state); | |
| EXPECT_EQ(debug_ipc::ThreadRecord::BlockedReason::kNotBlocked, notify.record.blocked_reason); | |
| EXPECT_EQ(debug_ipc::ThreadRecord::StackAmount::kNone, notify.record.stack_amount); | |
| EXPECT_TRUE(notify.record.frames.empty()); | |
| } | |
| TEST_F(DebuggedProcessZirconTest, OnThreadStartingEmitsSuspendedWhenZirconProcessPaused) { | |
| debug_ipc::NotifyThreadStarting notify; | |
| zx_thread_state_t thread_state = ZX_THREAD_STATE_NEW; | |
| ASSERT_NO_FATAL_FAILURE( | |
| RunThreadStartOnZirconProcess(/*suspend_before_start=*/true, ¬ify, &thread_state)); | |
| EXPECT_EQ(ZX_THREAD_STATE_SUSPENDED, thread_state); | |
| EXPECT_EQ(debug_ipc::ThreadRecord::State::kSuspended, notify.record.state); | |
| EXPECT_EQ(debug_ipc::ThreadRecord::BlockedReason::kNotBlocked, notify.record.blocked_reason); | |
| EXPECT_EQ(debug_ipc::ThreadRecord::StackAmount::kMinimal, notify.record.stack_amount); | |
| EXPECT_FALSE(notify.record.frames.empty()); | |
| } | |
| TEST(DebuggedProcess, ClientSuspendAllThreadsCoalescesMidSuspendThreadsAndHandoffsToken) { | |
| int enum_calls = 0; | |
| std::vector<std::string> events; | |
| std::set<zx_koid_t> notified_koids; | |
| ProcessTestHarness test; | |
| MockProcess* process = test.process; | |
| constexpr zx_koid_t kThread1 = 101; | |
| constexpr zx_koid_t kThread2 = 102; | |
| constexpr zx_koid_t kThread3 = 103; | |
| process->mock_process_handle().set_threads({MockThreadHandle(kThread1, "t1")}); | |
| process->PopulateCurrentThreads(); | |
| process->mock_process_handle().set_on_suspend([&](zx_koid_t) { | |
| EXPECT_EQ(process->mock_process_handle().suspend_count(), 1); | |
| events.push_back("process_suspend"); | |
| }); | |
| process->mock_process_handle().set_on_resume( | |
| [&](zx_koid_t) { events.push_back("process_resume"); }); | |
| process->mock_process_handle().set_on_get_child_threads( | |
| [&](std::vector<MockThreadHandle>& threads) { | |
| EXPECT_EQ(process->mock_process_handle().suspend_count(), 1); | |
| events.push_back("enum"); | |
| ++enum_calls; | |
| if (enum_calls == 1) { | |
| threads.emplace_back(kThread2, "t2"); | |
| } else if (enum_calls == 2) { | |
| threads.emplace_back(kThread3, "t3"); | |
| } | |
| }); | |
| test.harness.stream_backend()->set_notify_thread_starting_callback( | |
| [&](const debug_ipc::NotifyThreadStarting& notify) { | |
| notified_koids.insert(notify.record.id.thread); | |
| events.push_back("notify"); | |
| EXPECT_EQ(process->mock_process_handle().suspend_count(), 0); | |
| DebuggedThread* thread = process->GetThread(notify.record.id.thread); | |
| ASSERT_NE(thread, nullptr); | |
| EXPECT_TRUE(thread->is_client_suspended()); | |
| EXPECT_TRUE(AsMockThreadHandle(thread).is_suspended()); | |
| EXPECT_EQ(AsMockThreadHandle(thread).wait_for_suspension_calls(), 1); | |
| }); | |
| std::vector<debug_ipc::ProcessThreadId> suspended = | |
| process->ClientSuspendAllThreads(ZX_KOID_INVALID, TickTimePoint::max()); | |
| EXPECT_EQ(suspended.size(), 3u); | |
| EXPECT_EQ(notified_koids, (std::set<zx_koid_t>{kThread2, kThread3})); | |
| ExpectProcessSuspendEventOrder(events, /*expected_notify_count=*/2); | |
| // The temporary process-level token must be released after handoff so individual threads can | |
| // still be resumed independently. | |
| EXPECT_EQ(process->mock_process_handle().suspend_count(), 0); | |
| ASSERT_NE(process->GetThread(kThread1), nullptr); | |
| ASSERT_NE(process->GetThread(kThread2), nullptr); | |
| ASSERT_NE(process->GetThread(kThread3), nullptr); | |
| EXPECT_TRUE(process->GetThread(kThread1)->is_client_suspended()); | |
| EXPECT_TRUE(process->GetThread(kThread2)->is_client_suspended()); | |
| EXPECT_TRUE(process->GetThread(kThread3)->is_client_suspended()); | |
| EXPECT_TRUE(AsMockThreadHandle(process->GetThread(kThread1)).is_suspended()); | |
| EXPECT_TRUE(AsMockThreadHandle(process->GetThread(kThread2)).is_suspended()); | |
| EXPECT_TRUE(AsMockThreadHandle(process->GetThread(kThread3)).is_suspended()); | |
| // Resuming one thread leaves the others suspended. | |
| debug_ipc::ResumeRequest resume; | |
| resume.ids = {{.process = kProcessKoid, .thread = kThread1}}; | |
| process->OnResume(resume); | |
| EXPECT_FALSE(process->GetThread(kThread1)->is_client_suspended()); | |
| EXPECT_FALSE(AsMockThreadHandle(process->GetThread(kThread1)).is_suspended()); | |
| EXPECT_TRUE(process->GetThread(kThread2)->is_client_suspended()); | |
| EXPECT_TRUE(AsMockThreadHandle(process->GetThread(kThread2)).is_suspended()); | |
| EXPECT_TRUE(process->GetThread(kThread3)->is_client_suspended()); | |
| EXPECT_TRUE(AsMockThreadHandle(process->GetThread(kThread3)).is_suspended()); | |
| } | |
| TEST(DebuggedProcess, ClientSuspendAllThreadsFallbackCoalescesAndSkipsUnsuspendable) { | |
| constexpr zx_koid_t kExceptThread = 301; | |
| constexpr zx_koid_t kLiveThread = 302; | |
| constexpr zx_koid_t kAlreadySuspended = 303; | |
| constexpr zx_koid_t kDyingThread = 304; | |
| constexpr zx_koid_t kSpawnedDying = 305; | |
| constexpr zx_koid_t kSpawnedLive = 306; | |
| MockThreadHandle except_thread(kExceptThread, "except"); | |
| MockThreadHandle live_thread(kLiveThread, "live"); | |
| MockThreadHandle already_suspended(kAlreadySuspended, "already-suspended"); | |
| MockThreadHandle dying_thread(kDyingThread, "dying"); | |
| dying_thread.set_suspend_fails(true); | |
| MockThreadHandle spawned_dying(kSpawnedDying, "spawned-dying"); | |
| spawned_dying.set_suspend_fails(true); | |
| MockThreadHandle spawned_live(kSpawnedLive, "spawned-live"); | |
| int enum_calls = 0; | |
| bool spawned_live_suspended_at_notify = false; | |
| int spawned_live_waits_at_notify = 0; | |
| ProcessTestHarness test; | |
| MockProcess* process = test.process; | |
| process->mock_process_handle().set_threads( | |
| {except_thread, live_thread, already_suspended, dying_thread}); | |
| process->PopulateCurrentThreads(); | |
| ASSERT_NE(process->GetThread(kAlreadySuspended), nullptr); | |
| process->GetThread(kAlreadySuspended)->ClientSuspend(true); | |
| test.harness.stream_backend()->set_notify_thread_starting_callback( | |
| [&](const debug_ipc::NotifyThreadStarting& notify) { | |
| if (notify.record.id.thread == kSpawnedLive) { | |
| DebuggedThread* thread = process->GetThread(kSpawnedLive); | |
| ASSERT_NE(thread, nullptr); | |
| spawned_live_suspended_at_notify = thread->is_client_suspended(); | |
| spawned_live_waits_at_notify = spawned_live.wait_for_suspension_calls(); | |
| } | |
| }); | |
| process->mock_process_handle().set_on_get_child_threads( | |
| [&](std::vector<MockThreadHandle>& threads) { | |
| ++enum_calls; | |
| if (enum_calls == 1) { | |
| threads.push_back(spawned_dying); | |
| threads.push_back(spawned_live); | |
| } | |
| }); | |
| std::vector<debug_ipc::ProcessThreadId> suspended = | |
| process->ClientSuspendAllThreads(kExceptThread, TickTimePoint::max()); | |
| // Pass 0 suspends spawned_live and observes spawned_dying fail; pass 1 runs and exits without | |
| // retrying either dying thread or spinning through all coalesce passes. | |
| EXPECT_EQ(enum_calls, 2); | |
| EXPECT_EQ(process->mock_process_handle().suspend_count(), 0); | |
| EXPECT_EQ(dying_thread.suspend_calls(), 1); | |
| EXPECT_EQ(dying_thread.wait_for_suspension_calls(), 0); | |
| EXPECT_EQ(spawned_dying.suspend_calls(), 1); | |
| EXPECT_EQ(spawned_dying.wait_for_suspension_calls(), 0); | |
| EXPECT_TRUE(spawned_live_suspended_at_notify); | |
| EXPECT_EQ(spawned_live_waits_at_notify, 1); | |
| ASSERT_NE(process->GetThread(kExceptThread), nullptr); | |
| EXPECT_FALSE(process->GetThread(kExceptThread)->is_client_suspended()); | |
| ASSERT_NE(process->GetThread(kDyingThread), nullptr); | |
| EXPECT_FALSE(process->GetThread(kDyingThread)->is_client_suspended()); | |
| ASSERT_NE(process->GetThread(kSpawnedDying), nullptr); | |
| EXPECT_FALSE(process->GetThread(kSpawnedDying)->is_client_suspended()); | |
| ASSERT_NE(process->GetThread(kLiveThread), nullptr); | |
| EXPECT_TRUE(process->GetThread(kLiveThread)->is_client_suspended()); | |
| ASSERT_NE(process->GetThread(kAlreadySuspended), nullptr); | |
| EXPECT_TRUE(process->GetThread(kAlreadySuspended)->is_client_suspended()); | |
| ASSERT_NE(process->GetThread(kSpawnedLive), nullptr); | |
| EXPECT_TRUE(process->GetThread(kSpawnedLive)->is_client_suspended()); | |
| EXPECT_EQ(live_thread.wait_for_suspension_calls(), 1); | |
| EXPECT_EQ(spawned_live.wait_for_suspension_calls(), 1); | |
| std::set<zx_koid_t> suspended_koids; | |
| for (const auto& id : suspended) { | |
| EXPECT_EQ(id.process, kProcessKoid); | |
| EXPECT_TRUE(suspended_koids.insert(id.thread).second) << "Duplicate thread KOID " << id.thread; | |
| } | |
| EXPECT_EQ(suspended_koids, (std::set<zx_koid_t>{kLiveThread, kSpawnedLive})); | |
| } | |
| TEST(DebuggedProcess, ClientSuspendAllThreadsFallsBackWhenProcessSuspendReturnsNull) { | |
| constexpr zx_koid_t kInitialThread = 351; | |
| constexpr zx_koid_t kSpawnedThread = 352; | |
| MockThreadHandle initial_thread(kInitialThread, "initial"); | |
| MockThreadHandle spawned_thread(kSpawnedThread, "spawned"); | |
| int enum_calls = 0; | |
| bool spawned_suspended_at_notify = false; | |
| ProcessTestHarness test; | |
| MockProcess* process = test.process; | |
| process->mock_process_handle().set_suspend_fails(true); | |
| process->mock_process_handle().set_threads({initial_thread}); | |
| process->PopulateCurrentThreads(); | |
| test.harness.stream_backend()->set_notify_thread_starting_callback( | |
| [&](const debug_ipc::NotifyThreadStarting& notify) { | |
| if (notify.record.id.thread == kSpawnedThread) { | |
| DebuggedThread* thread = process->GetThread(kSpawnedThread); | |
| ASSERT_NE(thread, nullptr); | |
| spawned_suspended_at_notify = thread->is_client_suspended(); | |
| } | |
| }); | |
| process->mock_process_handle().set_on_get_child_threads( | |
| [&](std::vector<MockThreadHandle>& threads) { | |
| ++enum_calls; | |
| if (enum_calls == 1) { | |
| threads.push_back(spawned_thread); | |
| } | |
| }); | |
| std::vector<debug_ipc::ProcessThreadId> suspended = | |
| process->ClientSuspendAllThreads(ZX_KOID_INVALID, TickTimePoint::max()); | |
| EXPECT_EQ(process->mock_process_handle().suspend_count(), 0); | |
| EXPECT_TRUE(spawned_suspended_at_notify); | |
| ASSERT_NE(process->GetThread(kInitialThread), nullptr); | |
| EXPECT_TRUE(process->GetThread(kInitialThread)->is_client_suspended()); | |
| ASSERT_NE(process->GetThread(kSpawnedThread), nullptr); | |
| EXPECT_TRUE(process->GetThread(kSpawnedThread)->is_client_suspended()); | |
| std::set<zx_koid_t> suspended_koids; | |
| for (const auto& id : suspended) { | |
| EXPECT_EQ(id.process, kProcessKoid); | |
| suspended_koids.insert(id.thread); | |
| } | |
| EXPECT_EQ(suspended_koids, (std::set<zx_koid_t>{kInitialThread, kSpawnedThread})); | |
| } | |
| TEST(DebuggedProcess, ClientSuspendAllThreadsSkipsUnsuspendableThreadInProcessHandoff) { | |
| constexpr zx_koid_t kLiveThread = 401; | |
| constexpr zx_koid_t kAlreadySuspended = 402; | |
| constexpr zx_koid_t kDyingThread = 403; | |
| MockThreadHandle live_thread(kLiveThread, "live"); | |
| MockThreadHandle already_suspended(kAlreadySuspended, "already-suspended"); | |
| MockThreadHandle dying_thread(kDyingThread, "dying"); | |
| dying_thread.set_state(ThreadHandle::State(debug_ipc::ThreadRecord::State::kDying)); | |
| dying_thread.set_suspend_fails(true); | |
| std::vector<std::string> events; | |
| std::set<zx_koid_t> notified_koids; | |
| ProcessTestHarness test; | |
| MockProcess* process = test.process; | |
| process->mock_process_handle().set_threads({live_thread, already_suspended, dying_thread}); | |
| process->PopulateCurrentThreads(); | |
| ASSERT_NE(process->GetThread(kAlreadySuspended), nullptr); | |
| process->GetThread(kAlreadySuspended)->ClientSuspend(true); | |
| process->mock_process_handle().set_on_suspend([&](zx_koid_t) { | |
| EXPECT_EQ(process->mock_process_handle().suspend_count(), 1); | |
| events.push_back("process_suspend"); | |
| }); | |
| process->mock_process_handle().set_on_resume( | |
| [&](zx_koid_t) { events.push_back("process_resume"); }); | |
| process->mock_process_handle().set_on_get_child_threads([&](std::vector<MockThreadHandle>&) { | |
| EXPECT_EQ(process->mock_process_handle().suspend_count(), 1); | |
| events.push_back("enum"); | |
| }); | |
| test.harness.stream_backend()->set_notify_thread_starting_callback( | |
| [&](const debug_ipc::NotifyThreadStarting& notify) { | |
| notified_koids.insert(notify.record.id.thread); | |
| events.push_back("notify"); | |
| }); | |
| std::vector<debug_ipc::ProcessThreadId> suspended = | |
| process->ClientSuspendAllThreads(ZX_KOID_INVALID, TickTimePoint::max()); | |
| EXPECT_EQ(process->mock_process_handle().suspend_count(), 0); | |
| EXPECT_TRUE(notified_koids.empty()); | |
| ExpectProcessSuspendEventOrder(events, /*expected_notify_count=*/0); | |
| EXPECT_EQ(dying_thread.suspend_calls(), 1); | |
| EXPECT_EQ(dying_thread.wait_for_suspension_calls(), 0); | |
| ASSERT_NE(process->GetThread(kDyingThread), nullptr); | |
| EXPECT_FALSE(process->GetThread(kDyingThread)->is_client_suspended()); | |
| EXPECT_FALSE(AsMockThreadHandle(process->GetThread(kDyingThread)).is_suspended()); | |
| ASSERT_NE(process->GetThread(kLiveThread), nullptr); | |
| EXPECT_TRUE(process->GetThread(kLiveThread)->is_client_suspended()); | |
| EXPECT_TRUE(AsMockThreadHandle(process->GetThread(kLiveThread)).is_suspended()); | |
| EXPECT_EQ(live_thread.wait_for_suspension_calls(), 1); | |
| ASSERT_NE(process->GetThread(kAlreadySuspended), nullptr); | |
| EXPECT_TRUE(process->GetThread(kAlreadySuspended)->is_client_suspended()); | |
| EXPECT_TRUE(AsMockThreadHandle(process->GetThread(kAlreadySuspended)).is_suspended()); | |
| ASSERT_EQ(suspended.size(), 1u); | |
| EXPECT_EQ(suspended[0].process, kProcessKoid); | |
| EXPECT_EQ(suspended[0].thread, kLiveThread); | |
| } | |
| TEST(DebuggedProcess, ClientSuspendAllThreadsSkipsUntrackedDyingAndDeadThreads) { | |
| for (bool fail_process_suspend : {false, true}) { | |
| constexpr zx_koid_t kLiveThread = 451; | |
| constexpr zx_koid_t kExitedThread = 452; | |
| constexpr zx_koid_t kDyingThread = 453; | |
| constexpr zx_koid_t kDeadThread = 454; | |
| MockThreadHandle live_thread(kLiveThread, "live"); | |
| MockThreadHandle exited_thread(kExitedThread, "exited"); | |
| MockThreadHandle dying_thread(kDyingThread, "dying"); | |
| dying_thread.set_state(ThreadHandle::State(debug_ipc::ThreadRecord::State::kDying)); | |
| MockThreadHandle dead_thread(kDeadThread, "dead"); | |
| dead_thread.set_state(ThreadHandle::State(debug_ipc::ThreadRecord::State::kDead)); | |
| std::set<zx_koid_t> notified_koids; | |
| ProcessTestHarness test; | |
| MockProcess* process = test.process; | |
| process->mock_process_handle().set_suspend_fails(fail_process_suspend); | |
| process->mock_process_handle().set_threads({live_thread, exited_thread}); | |
| process->PopulateCurrentThreads(); | |
| ASSERT_NE(process->GetThread(kExitedThread), nullptr); | |
| // Simulate OnThreadExiting erasing the thread from threads_ before the kernel removes it from | |
| // the process's child thread list. | |
| process->OnThreadExiting(std::make_unique<MockExceptionHandle>(kProcessKoid, exited_thread)); | |
| EXPECT_EQ(process->GetThread(kExitedThread), nullptr); | |
| exited_thread.set_state(ThreadHandle::State(debug_ipc::ThreadRecord::State::kDying)); | |
| process->mock_process_handle().set_threads( | |
| {live_thread, exited_thread, dying_thread, dead_thread}); | |
| test.harness.stream_backend()->set_notify_thread_starting_callback( | |
| [&](const debug_ipc::NotifyThreadStarting& notify) { | |
| notified_koids.insert(notify.record.id.thread); | |
| }); | |
| std::vector<debug_ipc::ProcessThreadId> suspended = | |
| process->ClientSuspendAllThreads(ZX_KOID_INVALID, TickTimePoint::max()); | |
| EXPECT_TRUE(notified_koids.empty()); | |
| EXPECT_EQ(process->GetThread(kExitedThread), nullptr); | |
| EXPECT_EQ(process->GetThread(kDyingThread), nullptr); | |
| EXPECT_EQ(process->GetThread(kDeadThread), nullptr); | |
| EXPECT_EQ(exited_thread.suspend_calls(), 0); | |
| EXPECT_EQ(dying_thread.suspend_calls(), 0); | |
| EXPECT_EQ(dead_thread.suspend_calls(), 0); | |
| EXPECT_EQ(exited_thread.wait_for_suspension_calls(), 0); | |
| EXPECT_EQ(dying_thread.wait_for_suspension_calls(), 0); | |
| EXPECT_EQ(dead_thread.wait_for_suspension_calls(), 0); | |
| ASSERT_NE(process->GetThread(kLiveThread), nullptr); | |
| EXPECT_TRUE(process->GetThread(kLiveThread)->is_client_suspended()); | |
| ASSERT_EQ(suspended.size(), 1u); | |
| EXPECT_EQ(suspended[0].process, kProcessKoid); | |
| EXPECT_EQ(suspended[0].thread, kLiveThread); | |
| } | |
| } | |
| TEST(ProcessHandle, WaitForThreadsSuspendedCapsCoalescePasses) { | |
| MockProcessHandle process(kProcessKoid, "churn-process"); | |
| zx_koid_t next_tid = 500; | |
| size_t enum_calls = 0; | |
| process.set_on_get_child_threads([&](std::vector<MockThreadHandle>& threads) { | |
| ++enum_calls; | |
| threads.emplace_back(next_tid++, "churn"); | |
| }); | |
| ProcessHandle::WaitForThreadsSuspendedResult result = | |
| process.WaitForThreadsSuspended(TickTimePoint::max()); | |
| EXPECT_EQ(enum_calls, ProcessHandle::kMaxThreadCoalescePasses); | |
| EXPECT_EQ(result.waited_koids.size(), ProcessHandle::kMaxThreadCoalescePasses); | |
| EXPECT_FALSE(result.settled); | |
| } | |
| TEST(ProcessHandle, WaitForThreadsSuspendedExitsImmediatelyOnExpiredDeadline) { | |
| MockProcessHandle process(kProcessKoid, "expired-deadline-process"); | |
| zx_koid_t next_tid = 550; | |
| size_t enum_calls = 0; | |
| std::vector<MockThreadHandle> spawned; | |
| process.set_on_get_child_threads([&](std::vector<MockThreadHandle>& threads) { | |
| ++enum_calls; | |
| spawned.emplace_back(next_tid++, "churn"); | |
| threads.push_back(spawned.back()); | |
| }); | |
| ProcessHandle::WaitForThreadsSuspendedResult result = | |
| process.WaitForThreadsSuspended(TickTimePoint{}); | |
| EXPECT_EQ(enum_calls, 1u); | |
| EXPECT_EQ(result.waited_koids, (std::set<zx_koid_t>{550})); | |
| EXPECT_FALSE(result.settled); | |
| ASSERT_EQ(spawned.size(), 1u); | |
| EXPECT_EQ(spawned[0].wait_for_suspension_calls(), 0); | |
| } | |
| TEST(ProcessHandle, WaitForThreadsSuspendedSkipsWaitingOnDyingAndDeadThreads) { | |
| MockProcessHandle process(kProcessKoid, "dying-thread-process"); | |
| MockThreadHandle live(570, "live"); | |
| MockThreadHandle dying(571, "dying"); | |
| dying.set_state(ThreadHandle::State(debug_ipc::ThreadRecord::State::kDying)); | |
| MockThreadHandle dead(572, "dead"); | |
| dead.set_state(ThreadHandle::State(debug_ipc::ThreadRecord::State::kDead)); | |
| process.set_threads({live, dying, dead}); | |
| ProcessHandle::WaitForThreadsSuspendedResult result = | |
| process.WaitForThreadsSuspended(TickTimePoint::max()); | |
| EXPECT_EQ(result.waited_koids, (std::set<zx_koid_t>{570, 571, 572})); | |
| EXPECT_TRUE(result.settled); | |
| EXPECT_EQ(live.wait_for_suspension_calls(), 1); | |
| EXPECT_EQ(dying.wait_for_suspension_calls(), 0); | |
| EXPECT_EQ(dead.wait_for_suspension_calls(), 0); | |
| } | |
| TEST(ProcessHandle, WaitForThreadsSuspendedSettlesAfterConvergence) { | |
| MockProcessHandle process(kProcessKoid, "converging-process"); | |
| MockThreadHandle initial(575, "initial"); | |
| MockThreadHandle late(576, "late"); | |
| size_t enum_calls = 0; | |
| process.set_threads({initial}); | |
| process.set_on_get_child_threads([&](std::vector<MockThreadHandle>& threads) { | |
| ++enum_calls; | |
| if (enum_calls == 2) { | |
| threads.push_back(late); | |
| } | |
| }); | |
| ProcessHandle::WaitForThreadsSuspendedResult result = | |
| process.WaitForThreadsSuspended(TickTimePoint::max()); | |
| EXPECT_EQ(enum_calls, 3u); | |
| EXPECT_EQ(result.waited_koids, (std::set<zx_koid_t>{575, 576})); | |
| EXPECT_TRUE(result.settled); | |
| EXPECT_EQ(initial.wait_for_suspension_calls(), 1); | |
| EXPECT_EQ(late.wait_for_suspension_calls(), 1); | |
| } | |
| TEST(ProcessHandle, WaitForThreadsSuspendedReportsUnsettledWhenLiveWaitFails) { | |
| MockProcessHandle process(kProcessKoid, "wait-fails-process"); | |
| MockThreadHandle live(580, "live"); | |
| live.set_on_wait_for_suspension([] { return false; }); | |
| process.set_threads({live}); | |
| ProcessHandle::WaitForThreadsSuspendedResult result = | |
| process.WaitForThreadsSuspended(TickTimePoint::max()); | |
| EXPECT_EQ(result.waited_koids, (std::set<zx_koid_t>{580})); | |
| EXPECT_FALSE(result.settled); | |
| EXPECT_EQ(live.wait_for_suspension_calls(), 1); | |
| } | |
| TEST(ProcessHandle, WaitForThreadsSuspendedSettlesWhenThreadExitsMidWait) { | |
| for (debug_ipc::ThreadRecord::State exit_state : | |
| {debug_ipc::ThreadRecord::State::kDying, debug_ipc::ThreadRecord::State::kDead}) { | |
| MockThreadHandle direct_thread(590, "direct-exiting"); | |
| direct_thread.set_on_wait_for_suspension([&] { | |
| direct_thread.set_state(ThreadHandle::State(exit_state)); | |
| return false; | |
| }); | |
| EXPECT_TRUE(WaitForThreadSuspensionBeforeDeadline(direct_thread, TickTimePoint::max())); | |
| EXPECT_EQ(direct_thread.wait_for_suspension_calls(), 1); | |
| MockProcessHandle process(kProcessKoid, "exiting-mid-wait-process"); | |
| MockThreadHandle thread(591, "exiting"); | |
| thread.set_on_wait_for_suspension([&] { | |
| thread.set_state(ThreadHandle::State(exit_state)); | |
| return false; | |
| }); | |
| process.set_threads({thread}); | |
| ProcessHandle::WaitForThreadsSuspendedResult result = | |
| process.WaitForThreadsSuspended(TickTimePoint::max()); | |
| EXPECT_EQ(result.waited_koids, (std::set<zx_koid_t>{591})); | |
| EXPECT_TRUE(result.settled); | |
| EXPECT_EQ(thread.wait_for_suspension_calls(), 1); | |
| } | |
| } | |
| TEST(DebuggedProcess, ClientSuspendAllThreadsCapsCoalescePasses) { | |
| // Verify the fallback coalesce loop and the process-suspend handoff path both stop after the | |
| // bounded pass limit even when every enumeration reports a newly spawned thread. | |
| { | |
| zx_koid_t next_tid = 600; | |
| size_t enum_calls = 0; | |
| MockDebugAgentHarness harness; | |
| MockProcess* process = harness.AddProcess(kProcessKoid); | |
| process->mock_process_handle().set_suspend_fails(true); | |
| process->mock_process_handle().set_on_get_child_threads( | |
| [&](std::vector<MockThreadHandle>& threads) { | |
| ++enum_calls; | |
| threads.emplace_back(next_tid++, "churn"); | |
| }); | |
| std::vector<debug_ipc::ProcessThreadId> suspended = | |
| process->ClientSuspendAllThreads(ZX_KOID_INVALID, TickTimePoint::max()); | |
| EXPECT_EQ(enum_calls, ProcessHandle::kMaxThreadCoalescePasses); | |
| EXPECT_EQ(suspended.size(), ProcessHandle::kMaxThreadCoalescePasses); | |
| } | |
| { | |
| constexpr zx_koid_t kBaseTid = 700; | |
| zx_koid_t next_tid = kBaseTid; | |
| size_t enum_calls = 0; | |
| std::set<zx_koid_t> notified_koids; | |
| ProcessTestHarness test; | |
| MockProcess* process = test.process; | |
| process->mock_process_handle().set_suspend_fails(false); | |
| process->mock_process_handle().set_on_get_child_threads( | |
| [&](std::vector<MockThreadHandle>& threads) { | |
| EXPECT_EQ(process->mock_process_handle().suspend_count(), 1); | |
| ++enum_calls; | |
| threads.emplace_back(next_tid++, "churn"); | |
| }); | |
| test.harness.stream_backend()->set_notify_thread_starting_callback( | |
| [&](const debug_ipc::NotifyThreadStarting& notify) { | |
| notified_koids.insert(notify.record.id.thread); | |
| EXPECT_EQ(process->mock_process_handle().suspend_count(), 0); | |
| DebuggedThread* thread = process->GetThread(notify.record.id.thread); | |
| ASSERT_NE(thread, nullptr); | |
| EXPECT_TRUE(thread->is_client_suspended()); | |
| EXPECT_TRUE(AsMockThreadHandle(thread).is_suspended()); | |
| EXPECT_EQ(AsMockThreadHandle(thread).wait_for_suspension_calls(), 1); | |
| }); | |
| std::vector<debug_ipc::ProcessThreadId> suspended = | |
| process->ClientSuspendAllThreads(ZX_KOID_INVALID, TickTimePoint::max()); | |
| EXPECT_EQ(process->mock_process_handle().suspend_count(), 0); | |
| // ProcessHandle::WaitForThreadsSuspended runs kMaxThreadCoalescePasses passes, then post-cap | |
| // discovery enumerates once more and waits/suspends the extra thread before notifying. | |
| EXPECT_EQ(enum_calls, ProcessHandle::kMaxThreadCoalescePasses + 1); | |
| EXPECT_EQ(suspended.size(), ProcessHandle::kMaxThreadCoalescePasses + 1); | |
| EXPECT_EQ(notified_koids.size(), ProcessHandle::kMaxThreadCoalescePasses + 1); | |
| const zx_koid_t post_cap_tid = | |
| kBaseTid + static_cast<zx_koid_t>(ProcessHandle::kMaxThreadCoalescePasses); | |
| DebuggedThread* post_cap_thread = process->GetThread(post_cap_tid); | |
| ASSERT_NE(post_cap_thread, nullptr); | |
| EXPECT_TRUE(post_cap_thread->is_client_suspended()); | |
| EXPECT_TRUE(AsMockThreadHandle(post_cap_thread).is_suspended()); | |
| EXPECT_EQ(AsMockThreadHandle(post_cap_thread).wait_for_suspension_calls(), 1); | |
| } | |
| } | |
| TEST(DebuggedProcess, ClientSuspendAllThreadsDefersThreadBlockedInStartException) { | |
| for (bool fail_process_suspend : {false, true}) { | |
| constexpr zx_koid_t kLiveThread = 750; | |
| constexpr zx_koid_t kStartingThread = 751; | |
| constexpr zx_koid_t kNonStartExceptionThread = 752; | |
| MockThreadHandle live_thread(kLiveThread, "live"); | |
| MockThreadHandle starting_thread(kStartingThread, "starting"); | |
| starting_thread.set_state( | |
| ThreadHandle::State(debug_ipc::ThreadRecord::State::kBlocked, | |
| debug_ipc::ThreadRecord::BlockedReason::kException)); | |
| starting_thread.set_in_thread_starting_exception(true); | |
| MockThreadHandle non_start_exception_thread(kNonStartExceptionThread, "faulting"); | |
| non_start_exception_thread.set_state( | |
| ThreadHandle::State(debug_ipc::ThreadRecord::State::kBlocked, | |
| debug_ipc::ThreadRecord::BlockedReason::kException)); | |
| bool injected = false; | |
| std::vector<debug_ipc::ThreadRecord> notifications; | |
| ProcessTestHarness test; | |
| MockProcess* process = test.process; | |
| process->mock_process_handle().set_suspend_fails(fail_process_suspend); | |
| process->mock_process_handle().set_threads({live_thread}); | |
| process->PopulateCurrentThreads(); | |
| ASSERT_TRUE(process->IsAttached()); | |
| process->mock_process_handle().set_on_get_child_threads( | |
| [&](std::vector<MockThreadHandle>& threads) { | |
| if (!injected) { | |
| injected = true; | |
| threads.push_back(starting_thread); | |
| threads.push_back(non_start_exception_thread); | |
| } | |
| }); | |
| test.harness.stream_backend()->set_notify_thread_starting_callback( | |
| [&](const debug_ipc::NotifyThreadStarting& notify) { | |
| notifications.push_back(notify.record); | |
| }); | |
| std::vector<debug_ipc::ProcessThreadId> suspended = | |
| process->ClientSuspendAllThreads(ZX_KOID_INVALID, TickTimePoint::max()); | |
| ASSERT_EQ(suspended.size(), 2u); | |
| EXPECT_EQ(process->GetThread(kStartingThread), nullptr); | |
| ASSERT_NE(process->GetThread(kNonStartExceptionThread), nullptr); | |
| EXPECT_TRUE(process->GetThread(kNonStartExceptionThread)->is_client_suspended()); | |
| ASSERT_EQ(notifications.size(), 1u); | |
| EXPECT_EQ(notifications[0].id.thread, kNonStartExceptionThread); | |
| EXPECT_EQ(notifications[0].state, debug_ipc::ThreadRecord::State::kBlocked); | |
| // Delivering OnThreadStarting afterwards suspends the thread and notifies once. | |
| EXPECT_EQ(starting_thread.suspend_count(), 0); | |
| starting_thread.set_in_thread_starting_exception(false); | |
| starting_thread.set_state(ThreadHandle::State(debug_ipc::ThreadRecord::State::kRunning)); | |
| process->mock_process_handle().set_threads( | |
| {live_thread, starting_thread, non_start_exception_thread}); | |
| process->OnThreadStarting(std::make_unique<MockExceptionHandle>(kProcessKoid, starting_thread)); | |
| DebuggedThread* deferred_thread = process->GetThread(kStartingThread); | |
| ASSERT_NE(deferred_thread, nullptr); | |
| EXPECT_TRUE(deferred_thread->is_client_suspended()); | |
| EXPECT_EQ(starting_thread.suspend_count(), 1); | |
| EXPECT_EQ(AsMockThreadHandle(deferred_thread).suspend_count(), 1); | |
| EXPECT_TRUE(AsMockThreadHandle(deferred_thread).is_suspended()); | |
| ASSERT_EQ(notifications.size(), 2u); | |
| EXPECT_EQ(notifications[1].id.thread, kStartingThread); | |
| EXPECT_EQ(notifications[1].state, debug_ipc::ThreadRecord::State::kSuspended); | |
| } | |
| } | |
| TEST(DebuggedProcess, ClientSuspendAllThreadsDoesNotDeferStartExceptionWhenUnattached) { | |
| constexpr zx_koid_t kStartingThread = 761; | |
| MockThreadHandle starting_thread(kStartingThread, "starting"); | |
| starting_thread.set_state( | |
| ThreadHandle::State(debug_ipc::ThreadRecord::State::kBlocked, | |
| debug_ipc::ThreadRecord::BlockedReason::kException)); | |
| starting_thread.set_in_thread_starting_exception(true); | |
| std::vector<debug_ipc::ThreadRecord> notifications; | |
| ProcessTestHarness test; | |
| MockProcess* process = test.process; | |
| process->mock_process_handle().Detach(); | |
| ASSERT_FALSE(process->IsAttached()); | |
| process->mock_process_handle().set_threads({starting_thread}); | |
| test.harness.stream_backend()->set_notify_thread_starting_callback( | |
| [&](const debug_ipc::NotifyThreadStarting& notify) { | |
| notifications.push_back(notify.record); | |
| }); | |
| std::vector<debug_ipc::ProcessThreadId> suspended = | |
| process->ClientSuspendAllThreads(ZX_KOID_INVALID, TickTimePoint::max()); | |
| ASSERT_EQ(suspended.size(), 1u); | |
| EXPECT_EQ(suspended[0].thread, kStartingThread); | |
| EXPECT_NE(process->GetThread(kStartingThread), nullptr); | |
| ASSERT_EQ(notifications.size(), 1u); | |
| EXPECT_EQ(notifications[0].id.thread, kStartingThread); | |
| } | |
| TEST(DebuggedProcess, ClientSuspendAllThreadsSkipsDiscoveryWhenUnsupported) { | |
| for (bool fail_process_suspend : {true, false}) { | |
| constexpr zx_koid_t kTrackedThread = 780; | |
| constexpr zx_koid_t kUntrackedThread = 781; | |
| MockThreadHandle tracked_thread(kTrackedThread, "tracked"); | |
| MockThreadHandle untracked_thread(kUntrackedThread, "untracked"); | |
| size_t enum_calls = 0; | |
| size_t notify_calls = 0; | |
| ProcessTestHarness test; | |
| MockProcess* process = test.process; | |
| process->mock_process_handle().set_suspend_fails(fail_process_suspend); | |
| process->mock_process_handle().set_threads({tracked_thread}); | |
| process->PopulateCurrentThreads(); | |
| process->mock_process_handle().set_supports_suspend_time_thread_discovery(false); | |
| process->mock_process_handle().set_threads({tracked_thread, untracked_thread}); | |
| process->mock_process_handle().set_on_get_child_threads( | |
| [&](std::vector<MockThreadHandle>&) { ++enum_calls; }); | |
| test.harness.stream_backend()->set_notify_thread_starting_callback( | |
| [&](const debug_ipc::NotifyThreadStarting&) { ++notify_calls; }); | |
| std::vector<debug_ipc::ProcessThreadId> suspended = | |
| process->ClientSuspendAllThreads(ZX_KOID_INVALID, TickTimePoint::max()); | |
| EXPECT_EQ(enum_calls, 0u); | |
| EXPECT_EQ(notify_calls, 0u); | |
| EXPECT_EQ(process->GetThread(kUntrackedThread), nullptr); | |
| ASSERT_NE(process->GetThread(kTrackedThread), nullptr); | |
| EXPECT_TRUE(process->GetThread(kTrackedThread)->is_client_suspended()); | |
| EXPECT_TRUE(AsMockThreadHandle(process->GetThread(kTrackedThread)).is_suspended()); | |
| ASSERT_EQ(suspended.size(), 1u); | |
| EXPECT_EQ(suspended[0].process, kProcessKoid); | |
| EXPECT_EQ(suspended[0].thread, kTrackedThread); | |
| // Cloned threads delivered through OnThreadStarting afterwards are still adopted and suspended | |
| // because suspend_new_threads_ is active. | |
| process->OnThreadStarting( | |
| std::make_unique<MockExceptionHandle>(kProcessKoid, untracked_thread)); | |
| DebuggedThread* cloned_thread = process->GetThread(kUntrackedThread); | |
| ASSERT_NE(cloned_thread, nullptr); | |
| EXPECT_TRUE(cloned_thread->is_client_suspended()); | |
| EXPECT_EQ(untracked_thread.suspend_count(), 1); | |
| EXPECT_EQ(notify_calls, 1u); | |
| } | |
| } | |
| using DebuggedProcessLoopTest = debug::TestWithLoop; | |
| TEST_F(DebuggedProcessLoopTest, BreakpointHitDefersClearStateOnWriteError) { | |
| constexpr zx_koid_t kHittingThread = 821; | |
| constexpr zx_koid_t kOtherThread = 822; | |
| constexpr uint64_t kBreakpointAddr = 0x10000; | |
| FailingWriteAgentSetup setup; | |
| ASSERT_TRUE(setup.agent->is_connected()); | |
| MockProcess* process = setup.AddProcess(kProcessKoid); | |
| process->mock_process_handle().mock_memory().AddMemory(kBreakpointAddr, GetBreakpointMemory()); | |
| MockThread* hitting_thread = process->AddThread(kHittingThread); | |
| GeneralRegisters regs; | |
| regs.set_ip(kBreakpointAddr + arch::kExceptionOffsetForSoftwareBreakpoint); | |
| hitting_thread->mock_thread_handle().SetGeneralRegisters(regs); | |
| process->mock_process_handle().set_threads( | |
| {MockThreadHandle(kHittingThread, "hit"), MockThreadHandle(kOtherThread, "other")}); | |
| debug_ipc::AddOrChangeBreakpointRequest bp_request; | |
| bp_request.breakpoint.id = 1; | |
| bp_request.breakpoint.type = debug_ipc::BreakpointType::kSoftware; | |
| bp_request.breakpoint.stop = debug_ipc::Stop::kProcess; | |
| bp_request.breakpoint.one_shot = true; | |
| bp_request.breakpoint.locations.push_back(CreateLocation(kProcessKoid, 0, kBreakpointAddr)); | |
| debug_ipc::AddOrChangeBreakpointReply bp_reply; | |
| setup.agent->OnAddOrChangeBreakpoint(bp_request, &bp_reply); | |
| ASSERT_TRUE(bp_reply.status.ok()); | |
| process->InjectException(std::make_unique<MockExceptionHandle>( | |
| kProcessKoid, kHittingThread, debug_ipc::ExceptionType::kSoftwareBreakpoint)); | |
| // NotifyThreadStarting triggers the write error and disconnects immediately, while the | |
| // remaining calls on the current stack complete without crashing and ClearState() runs on the | |
| // loop. | |
| EXPECT_FALSE(setup.agent->is_connected()); | |
| EXPECT_EQ(setup.stream->write_calls(), 1u); | |
| EXPECT_EQ(setup.agent->GetBreakpoint(1), nullptr); | |
| DebuggedThread* other_thread = process->GetThread(kOtherThread); | |
| ASSERT_NE(other_thread, nullptr); | |
| EXPECT_TRUE(other_thread->is_client_suspended()); | |
| EXPECT_TRUE(AsMockThreadHandle(other_thread).is_suspended()); | |
| EXPECT_NE(setup.agent->GetDebuggedProcess(kProcessKoid), nullptr); | |
| ExpectDeferredQuitNow(loop()); | |
| EXPECT_EQ(setup.agent->GetDebuggedProcess(kProcessKoid), nullptr); | |
| } | |
| } // namespace | |
| } // namespace debug_agent |