diff --git a/crates/ide-dap/src/lib.rs b/crates/ide-dap/src/lib.rs index 4b3c8623f..1f29f130a 100644 --- a/crates/ide-dap/src/lib.rs +++ b/crates/ide-dap/src/lib.rs @@ -1713,7 +1713,7 @@ impl DebugManager { // A step runs its thread; other threads stopped by their own events stay stopped. session.suspended.resumed(ticket, thread_id, false); stream_trace::release_done(&session); - session.emit(ide_core::event::DebugEvent::Continued { thread_id, all_threads_continued: true }); + session.emit(ide_core::event::DebugEvent::Continued { thread_id, all_threads_continued: false }); Ok(()) } @@ -1825,7 +1825,7 @@ impl DebugManager { step_settled(&session, sent)?; session.suspended.resumed(ticket, thread_id, false); stream_trace::release_done(&session); - session.emit(ide_core::event::DebugEvent::Continued { thread_id, all_threads_continued: true }); + session.emit(ide_core::event::DebugEvent::Continued { thread_id, all_threads_continued: false }); Ok(()) } diff --git a/crates/ide-dap/src/tests.rs b/crates/ide-dap/src/tests.rs index a7f912e39..9d6610af6 100644 --- a/crates/ide-dap/src/tests.rs +++ b/crates/ide-dap/src/tests.rs @@ -2763,6 +2763,12 @@ async fn step_of_one_thread_keeps_the_others_suspended() { assert_eq!(session.suspended.ticket(), ticket + 1); h.manager.step_in_target(id, 7, 1).await.unwrap(); assert!(!session.suspended.get()); + // The UI stays on a thread that is still stopped only when told the step ran one thread. + for thread in [7, 8] { + let continued = + h.wait_event(|e| matches!(e, DebugEvent::Continued { thread_id, .. } if *thread_id == thread)).await; + assert!(matches!(continued, DebugEvent::Continued { all_threads_continued: false, .. }), "{continued:?}"); + } } #[tokio::test] diff --git a/src/features/debugger/__tests__/executionState.test.ts b/src/features/debugger/__tests__/executionState.test.ts index 0969cbe0a..30ec8d13e 100644 --- a/src/features/debugger/__tests__/executionState.test.ts +++ b/src/features/debugger/__tests__/executionState.test.ts @@ -26,7 +26,15 @@ vi.mock("@/workbench/contributions", async (orig) => ({ ...(await orig()), maybeEditorService: () => ({ openFile: vi.fn(async () => {}) }), })); -import { adoptSession, handleDebugEvent, selectThread, resumeExecution, useDebug, useExecutionPoint } from "../state"; +import { + adoptSession, + handleDebugEvent, + isThreadSuspended, + selectThread, + resumeExecution, + useDebug, + useExecutionPoint, +} from "../state"; const path = "/proj/MsMemberUtils.java"; const frame = (line: number, id = 100): StackFrame => ({ @@ -126,6 +134,56 @@ describe("execution point state", () => { expect(useExecutionPoint.getState().path).toBeNull(); }); + it("moves to another thread still stopped by its own breakpoint when the current one continues", async () => { + stopped(5); + await settle(); + ipc.debugStackTrace.mockResolvedValueOnce(ipc.ok([frame(9, 200)])); + stopped(1); + await settle(); + ipc.debugStackTrace.mockResolvedValueOnce(ipc.ok([frame(6841)])); + await resumeExecution(1, async () => { + handleDebugEvent(1, { kind: "continued", threadId: 1, allThreadsContinued: false }); + }); + await settle(); + expect(useDebug.getState().sessions[1]).toMatchObject({ + status: "paused", + stoppedThread: 5, + selectedThread: 5, + stopReason: "step", + selectedFrame: 100, + }); + expect(useExecutionPoint.getState()).toMatchObject({ path, line: 6841 }); + expect(isThreadSuspended(useDebug.getState().sessions[1]!, 5)).toBe(true); + expect(isThreadSuspended(useDebug.getState().sessions[1]!, 1)).toBe(false); + handleDebugEvent(1, { kind: "continued", threadId: 5, allThreadsContinued: false }); + expect(useDebug.getState().sessions[1]?.status).toBe("running"); + }); + + it("forgets a stopped thread that continues while another one is shown", async () => { + stopped(5); + await settle(); + stopped(1); + await settle(); + handleDebugEvent(1, { kind: "continued", threadId: 5, allThreadsContinued: false }); + expect(useDebug.getState().sessions[1]).toMatchObject({ status: "paused", stoppedThread: 1 }); + handleDebugEvent(1, { kind: "continued", threadId: 1, allThreadsContinued: false }); + expect(useDebug.getState().sessions[1]?.status).toBe("running"); + }); + + it.each([ + ["every thread continues", false, true], + ["the stop suspended every thread", true, false], + ])("runs when %s", async (_, allStopped, allContinued) => { + stopped(5, allStopped); + await settle(); + stopped(1, allStopped); + await settle(); + expect(isThreadSuspended(useDebug.getState().sessions[1]!, 5)).toBe(true); + handleDebugEvent(1, { kind: "continued", threadId: 1, allThreadsContinued: allContinued }); + expect(useDebug.getState().sessions[1]?.status).toBe("running"); + expect(isThreadSuspended(useDebug.getState().sessions[1]!, 5)).toBe(false); + }); + it.each(["continued", "terminated"] as const)("clears the execution point on %s", async (kind) => { stopped(); await settle(); diff --git a/src/features/debugger/state.ts b/src/features/debugger/state.ts index 6136b01e0..3025f1d38 100644 --- a/src/features/debugger/state.ts +++ b/src/features/debugger/state.ts @@ -84,6 +84,13 @@ export interface DebugSession { stoppedThread: number | null; stopReason: string | null; stopDescription: string | null; + /** + * Threads stopped alone (a `stopped` event without `allThreadsStopped`), oldest first. Adapters + * such as java-debug keep them suspended while another thread runs. + */ + suspendedThreads?: SuspendedThread[]; + /** The last stop suspended every thread. */ + allThreadsStopped?: boolean; threads: ThreadInfo[]; selectedThread: number | null; frames: StackFrame[]; @@ -107,6 +114,12 @@ export interface DebugSession { flutterAppStarted?: boolean; } +export interface SuspendedThread { + id: number; + reason: string; + description: string | null; +} + export interface ConsoleLine { id: number; category: string; @@ -1051,6 +1064,11 @@ export function pausedThread(s: DebugSession): number | null { return s.selectedThread ?? s.stoppedThread ?? s.threads[0]?.id ?? null; } +export function isThreadSuspended(s: DebugSession, threadId: number): boolean { + if (s.status !== "paused") return false; + return !!s.allThreadsStopped || s.stoppedThread === threadId || !!s.suspendedThreads?.some((t) => t.id === threadId); +} + /** The selected frame of the paused session (top frame when none is selected). */ export function selectedFrame(s: DebugSession): StackFrame | null { return s.frames.find((f) => f.id === s.selectedFrame) ?? s.frames[0] ?? null; @@ -1116,7 +1134,8 @@ export function selectFrame(id: DebugSessionId, frameId: number, reveal = true) void loadLocals(id, frameId); } -async function loadFrames(id: DebugSessionId, threadId: number, reveal: boolean) { +/** `newStop`: false when returning to an earlier stop, whose breakpoint hit was handled when it happened. */ +async function loadFrames(id: DebugSessionId, threadId: number, reveal: boolean, newStop = true) { const before = useDebug.getState().sessions[id]; let frames: StackFrame[] = []; try { @@ -1128,7 +1147,7 @@ async function loadFrames(id: DebugSessionId, threadId: number, reveal: boolean) if (!s || s.status !== "paused" || s.selectedThread !== threadId || s.frames !== before?.frames) return; patch(id, { frames }); const top = topFrame(frames); - if (top?.path && (s.stopReason === "breakpoint" || s.stopReason === "function breakpoint")) { + if (newStop && top?.path && (s.stopReason === "breakpoint" || s.stopReason === "function breakpoint")) { void logSuspendingHit(id, top); consumeTemporary(top.path, top.line); } @@ -1377,8 +1396,13 @@ export function handleDebugEvent(id: DebugSessionId, event: DebugEvent) { break; case "stopped": { const isBp = event.reason === "breakpoint" || event.reason === "function breakpoint"; + const alone = (s.suspendedThreads ?? []).filter((t) => t.id !== event.threadId); + if (!event.allThreadsStopped) + alone.push({ id: event.threadId, reason: event.reason, description: event.description }); patch(id, { status: "paused", + suspendedThreads: event.allThreadsStopped ? [] : alone, + allThreadsStopped: event.allThreadsStopped, stoppedThread: event.threadId, selectedThread: event.threadId, stopReason: event.reason, @@ -1410,13 +1434,39 @@ export function handleDebugEvent(id: DebugSessionId, event: DebugEvent) { case "continued": { const cur = useDebug.getState().sessions[id]!; if (cur.status === "terminated") break; - if (!event.allThreadsContinued && event.threadId !== (cur.selectedThread ?? cur.stoppedThread)) break; + const rest = event.allThreadsContinued ? [] : (cur.suspendedThreads ?? []).filter((t) => t.id !== event.threadId); + if (!event.allThreadsContinued && event.threadId !== (cur.selectedThread ?? cur.stoppedThread)) { + patch(id, { suspendedThreads: rest }); + break; + } stopTaskbarFlash(); + const next = rest.at(-1); + if (next) { + // Like IDEA, the session stays suspended on a thread its own breakpoint still holds. + patch(id, { + status: "paused", + suspendedThreads: rest, + stoppedThread: next.id, + selectedThread: next.id, + stopReason: next.reason, + stopDescription: next.description, + frames: [], + selectedFrame: null, + }); + useDebug.setState((st) => ({ stopSeq: st.stopSeq + 1 })); + publishContext(); + updateExecutionPoint(); + void loadThreads(id); + void loadFrames(id, next.id, true, false); + break; + } patch(id, { status: "running", frames: [], selectedFrame: null, stoppedThread: null, + suspendedThreads: [], + allThreadsStopped: false, }); updateExecutionPoint(); break; diff --git a/src/features/debugger/ui/DebugToolWindow.tsx b/src/features/debugger/ui/DebugToolWindow.tsx index 14cc65e24..e491c436b 100644 --- a/src/features/debugger/ui/DebugToolWindow.tsx +++ b/src/features/debugger/ui/DebugToolWindow.tsx @@ -32,6 +32,7 @@ import { closeSessionTab, evaluate, isLive, + isThreadSuspended, selectFrame, selectSession, selectThread, @@ -406,7 +407,7 @@ function FramesPane({ session }: { session: DebugSession }) { value: String(t.id), label: t.name, verbatim: true, - detail: t.id === session.stoppedThread ? "suspended" : undefined, + detail: isThreadSuspended(session, t.id) ? "suspended" : undefined, }))} onChange={(v) => void selectThread(session.id, +v)} aria-label="Thread"