Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

debug: fix incomplete support of DAP invalidated event #200789

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
149 changes: 78 additions & 71 deletions src/vs/workbench/contrib/debug/browser/debugSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export class DebugSession implements IDebugSession, IDisposable {
private lastContinuedThreadId: number | undefined;
private repl: ReplModel;
private stoppedDetails: IRawStoppedDetails[] = [];
private readonly statusQueue = this.rawListeners.add(new ThreadStatusScheduler());

private readonly _onDidChangeState = new Emitter<void>();
private readonly _onDidEndAdapter = new Emitter<AdapterEndEvent | undefined>();
Expand Down Expand Up @@ -984,74 +985,8 @@ export class DebugSession implements IDebugSession, IDisposable {
}));


const statusQueue = this.rawListeners.add(new ThreadStatusScheduler());
this.rawListeners.add(this.raw.onDidStop(async event => {
this.passFocusScheduler.cancel();
this.stoppedDetails.push(event.body);

statusQueue.run(
this.fetchThreads(event.body).then(() => event.body.threadId === undefined ? this.threadIds : [event.body.threadId]),
async (threadId, token) => {
const hasLotsOfThreads = event.body.threadId === undefined && this.threadIds.length > 10;

// If the focus for the current session is on a non-existent thread, clear the focus.
const focusedThread = this.debugService.getViewModel().focusedThread;
const focusedThreadDoesNotExist = focusedThread !== undefined && focusedThread.session === this && !this.threads.has(focusedThread.threadId);
if (focusedThreadDoesNotExist) {
this.debugService.focusStackFrame(undefined, undefined);
}
const thread = typeof threadId === 'number' ? this.getThread(threadId) : undefined;
if (thread) {
// Call fetch call stack twice, the first only return the top stack frame.
// Second retrieves the rest of the call stack. For performance reasons #25605
// Second call is only done if there's few threads that stopped in this event.
const promises = this.model.refreshTopOfCallstack(<Thread>thread, /* fetchFullStack= */!hasLotsOfThreads);
const focus = async () => {
if (focusedThreadDoesNotExist || (!event.body.preserveFocusHint && thread.getCallStack().length)) {
const focusedStackFrame = this.debugService.getViewModel().focusedStackFrame;
if (!focusedStackFrame || focusedStackFrame.thread.session === this) {
// Only take focus if nothing is focused, or if the focus is already on the current session
const preserveFocus = !this.configurationService.getValue<IDebugConfiguration>('debug').focusEditorOnBreak;
await this.debugService.focusStackFrame(undefined, thread, undefined, { preserveFocus });
}

if (thread.stoppedDetails && !token.isCancellationRequested) {
if (thread.stoppedDetails.reason === 'breakpoint' && this.configurationService.getValue<IDebugConfiguration>('debug').openDebug === 'openOnDebugBreak' && !this.suppressDebugView) {
await this.paneCompositeService.openPaneComposite(VIEWLET_ID, ViewContainerLocation.Sidebar);
}

if (this.configurationService.getValue<IDebugConfiguration>('debug').focusWindowOnBreak && !this.workbenchEnvironmentService.extensionTestsLocationURI) {
const activeWindow = getActiveWindow();
if (!activeWindow.document.hasFocus()) {
await this.hostService.focus(mainWindow, { force: true /* Application may not be active */ });
}
}
}
}
};

await promises.topCallStack;
if (token.isCancellationRequested) {
return;
}

focus();

await promises.wholeCallStack;
if (token.isCancellationRequested) {
return;
}

const focusedStackFrame = this.debugService.getViewModel().focusedStackFrame;
if (!focusedStackFrame || !focusedStackFrame.source || focusedStackFrame.source.presentationHint === 'deemphasize' || focusedStackFrame.presentationHint === 'deemphasize') {
// The top stack frame can be deemphesized so try to focus again #68616
focus();
}
}
this._onDidChangeState.fire();
},
);
}));
const statusQueue = this.statusQueue;
this.rawListeners.add(this.raw.onDidStop(event => this.handleStop(event.body)));

this.rawListeners.add(this.raw.onDidThread(event => {
statusQueue.cancel([event.body.threadId]);
Expand Down Expand Up @@ -1268,11 +1203,15 @@ export class DebugSession implements IDebugSession, IDisposable {
this._onDidInvalidMemory.fire(event);
}));
this.rawListeners.add(this.raw.onDidInvalidated(async event => {
if (!(event.body.areas && event.body.areas.length === 1 && (event.body.areas[0] === 'variables' || event.body.areas[0] === 'watch'))) {
// If invalidated event only requires to update variables or watch, do that, otherwise refatch threads https://github.com/microsoft/vscode/issues/106745
const areas = event.body.areas || ['all'];
// If invalidated event only requires to update variables or watch, do that, otherwise refetch threads https://github.com/microsoft/vscode/issues/106745
if (areas.includes('threads') || areas.includes('stacks') || areas.includes('all')) {
this.cancelAllRequests();
this.model.clearThreads(this.getId(), true);
await this.fetchThreads(this.getStoppedDetails());

const details = this.stoppedDetails;
this.stoppedDetails.length = 1;
await Promise.all(details.map(d => this.handleStop(d)));
}

const viewModel = this.debugService.getViewModel();
Expand All @@ -1284,6 +1223,74 @@ export class DebugSession implements IDebugSession, IDisposable {
this.rawListeners.add(this.raw.onDidExitAdapter(event => this.onDidExitAdapter(event)));
}

private async handleStop(event: IRawStoppedDetails) {
this.passFocusScheduler.cancel();
this.stoppedDetails.push(event);

this.statusQueue.run(
this.fetchThreads(event).then(() => event.threadId === undefined ? this.threadIds : [event.threadId]),
async (threadId, token) => {
const hasLotsOfThreads = event.threadId === undefined && this.threadIds.length > 10;

// If the focus for the current session is on a non-existent thread, clear the focus.
const focusedThread = this.debugService.getViewModel().focusedThread;
const focusedThreadDoesNotExist = focusedThread !== undefined && focusedThread.session === this && !this.threads.has(focusedThread.threadId);
if (focusedThreadDoesNotExist) {
this.debugService.focusStackFrame(undefined, undefined);
}
const thread = typeof threadId === 'number' ? this.getThread(threadId) : undefined;
if (thread) {
// Call fetch call stack twice, the first only return the top stack frame.
// Second retrieves the rest of the call stack. For performance reasons #25605
// Second call is only done if there's few threads that stopped in this event.
const promises = this.model.refreshTopOfCallstack(<Thread>thread, /* fetchFullStack= */!hasLotsOfThreads);
const focus = async () => {
if (focusedThreadDoesNotExist || (!event.preserveFocusHint && thread.getCallStack().length)) {
const focusedStackFrame = this.debugService.getViewModel().focusedStackFrame;
if (!focusedStackFrame || focusedStackFrame.thread.session === this) {
// Only take focus if nothing is focused, or if the focus is already on the current session
const preserveFocus = !this.configurationService.getValue<IDebugConfiguration>('debug').focusEditorOnBreak;
await this.debugService.focusStackFrame(undefined, thread, undefined, { preserveFocus });
}

if (thread.stoppedDetails && !token.isCancellationRequested) {
if (thread.stoppedDetails.reason === 'breakpoint' && this.configurationService.getValue<IDebugConfiguration>('debug').openDebug === 'openOnDebugBreak' && !this.suppressDebugView) {
await this.paneCompositeService.openPaneComposite(VIEWLET_ID, ViewContainerLocation.Sidebar);
}

if (this.configurationService.getValue<IDebugConfiguration>('debug').focusWindowOnBreak && !this.workbenchEnvironmentService.extensionTestsLocationURI) {
const activeWindow = getActiveWindow();
if (!activeWindow.document.hasFocus()) {
await this.hostService.focus(mainWindow, { force: true /* Application may not be active */ });
}
}
}
}
};

await promises.topCallStack;
if (token.isCancellationRequested) {
return;
}

focus();

await promises.wholeCallStack;
if (token.isCancellationRequested) {
return;
}

const focusedStackFrame = this.debugService.getViewModel().focusedStackFrame;
if (!focusedStackFrame || !focusedStackFrame.source || focusedStackFrame.source.presentationHint === 'deemphasize' || focusedStackFrame.presentationHint === 'deemphasize') {
// The top stack frame can be deemphesized so try to focus again #68616
focus();
}
}
this._onDidChangeState.fire();
},
);
}

private onDidExitAdapter(event?: AdapterEndEvent): void {
this.initialized = true;
this.model.setBreakpointSessionData(this.getId(), this.capabilities, undefined);
Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/contrib/debug/common/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export interface IRawStoppedDetails {
text?: string;
totalFrames?: number;
allThreadsStopped?: boolean;
preserveFocusHint?: boolean;
framesErrorMessage?: string;
hitBreakpointIds?: number[];
}
Expand Down