Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/debug/common/debugViewModel.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { Emitter, Event } from '../../../../base/common/event.js';
7
import { IContextKey, IContextKeyService } from '../../../../platform/contextkey/common/contextkey.js';
8
import { CONTEXT_DISASSEMBLE_REQUEST_SUPPORTED, CONTEXT_EXPRESSION_SELECTED, CONTEXT_FOCUSED_SESSION_IS_ATTACH, CONTEXT_FOCUSED_SESSION_IS_NO_DEBUG, CONTEXT_FOCUSED_STACK_FRAME_HAS_INSTRUCTION_POINTER_REFERENCE, CONTEXT_JUMP_TO_CURSOR_SUPPORTED, CONTEXT_LOADED_SCRIPTS_SUPPORTED, CONTEXT_MULTI_SESSION_DEBUG, CONTEXT_RESTART_FRAME_SUPPORTED, CONTEXT_SET_DATA_BREAKPOINT_BYTES_SUPPORTED, CONTEXT_SET_EXPRESSION_SUPPORTED, CONTEXT_SET_VARIABLE_SUPPORTED, CONTEXT_STEP_BACK_SUPPORTED, CONTEXT_STEP_INTO_TARGETS_SUPPORTED, CONTEXT_SUSPEND_DEBUGGEE_SUPPORTED, CONTEXT_TERMINATE_DEBUGGEE_SUPPORTED, CONTEXT_TERMINATE_THREADS_SUPPORTED, IDebugSession, IExpression, IExpressionContainer, IStackFrame, IThread, IViewModel } from './debug.js';
9
import { isSessionAttach } from './debugUtils.js';
10
11
export class ViewModel implements IViewModel {
12
13
firstSessionStart = true;
14
15
private _focusedStackFrame: IStackFrame | undefined;
16
private _focusedSession: IDebugSession | undefined;
17
private _focusedThread: IThread | undefined;
18
private selectedExpression: { expression: IExpression; settingWatch: boolean } | undefined;
19
private readonly _onDidFocusSession = new Emitter<IDebugSession | undefined>();
20
private readonly _onDidFocusThread = new Emitter<{ thread: IThread | undefined; explicit: boolean; session: IDebugSession | undefined }>();
21
private readonly _onDidFocusStackFrame = new Emitter<{ stackFrame: IStackFrame | undefined; explicit: boolean; session: IDebugSession | undefined }>();
22
private readonly _onDidSelectExpression = new Emitter<{ expression: IExpression; settingWatch: boolean } | undefined>();
23
private readonly _onDidEvaluateLazyExpression = new Emitter<IExpressionContainer>();
24
private readonly _onWillUpdateViews = new Emitter<void>();
25
private readonly _onDidChangeVisualization = new Emitter<{ original: IExpression; replacement: IExpression }>();
26
private readonly visualized = new WeakMap<IExpression, IExpression>();
27
private readonly preferredVisualizers = new Map</** cache key */ string, /* tree ID */ string>();
28
private expressionSelectedContextKey!: IContextKey<boolean>;
29
private loadedScriptsSupportedContextKey!: IContextKey<boolean>;
30
private stepBackSupportedContextKey!: IContextKey<boolean>;
31
private focusedSessionIsAttach!: IContextKey<boolean>;
32
private focusedSessionIsNoDebug!: IContextKey<boolean>;
33
private restartFrameSupportedContextKey!: IContextKey<boolean>;
34
private stepIntoTargetsSupported!: IContextKey<boolean>;
35
private jumpToCursorSupported!: IContextKey<boolean>;
36
private setVariableSupported!: IContextKey<boolean>;
37
private setDataBreakpointAtByteSupported!: IContextKey<boolean>;
38
private setExpressionSupported!: IContextKey<boolean>;
39
private multiSessionDebug!: IContextKey<boolean>;
40
private terminateDebuggeeSupported!: IContextKey<boolean>;
41
private suspendDebuggeeSupported!: IContextKey<boolean>;
42
private terminateThreadsSupported!: IContextKey<boolean>;
43
private disassembleRequestSupported!: IContextKey<boolean>;
44
private focusedStackFrameHasInstructionPointerReference!: IContextKey<boolean>;
45
46
constructor(private contextKeyService: IContextKeyService) {
47
contextKeyService.bufferChangeEvents(() => {
48
this.expressionSelectedContextKey = CONTEXT_EXPRESSION_SELECTED.bindTo(contextKeyService);
49
this.loadedScriptsSupportedContextKey = CONTEXT_LOADED_SCRIPTS_SUPPORTED.bindTo(contextKeyService);
50
this.stepBackSupportedContextKey = CONTEXT_STEP_BACK_SUPPORTED.bindTo(contextKeyService);
51
this.focusedSessionIsAttach = CONTEXT_FOCUSED_SESSION_IS_ATTACH.bindTo(contextKeyService);
52
this.focusedSessionIsNoDebug = CONTEXT_FOCUSED_SESSION_IS_NO_DEBUG.bindTo(contextKeyService);
53
this.restartFrameSupportedContextKey = CONTEXT_RESTART_FRAME_SUPPORTED.bindTo(contextKeyService);
54
this.stepIntoTargetsSupported = CONTEXT_STEP_INTO_TARGETS_SUPPORTED.bindTo(contextKeyService);
55
this.jumpToCursorSupported = CONTEXT_JUMP_TO_CURSOR_SUPPORTED.bindTo(contextKeyService);
56
this.setVariableSupported = CONTEXT_SET_VARIABLE_SUPPORTED.bindTo(contextKeyService);
57
this.setDataBreakpointAtByteSupported = CONTEXT_SET_DATA_BREAKPOINT_BYTES_SUPPORTED.bindTo(contextKeyService);
58
this.setExpressionSupported = CONTEXT_SET_EXPRESSION_SUPPORTED.bindTo(contextKeyService);
59
this.multiSessionDebug = CONTEXT_MULTI_SESSION_DEBUG.bindTo(contextKeyService);
60
this.terminateDebuggeeSupported = CONTEXT_TERMINATE_DEBUGGEE_SUPPORTED.bindTo(contextKeyService);
61
this.suspendDebuggeeSupported = CONTEXT_SUSPEND_DEBUGGEE_SUPPORTED.bindTo(contextKeyService);
62
this.terminateThreadsSupported = CONTEXT_TERMINATE_THREADS_SUPPORTED.bindTo(contextKeyService);
63
this.disassembleRequestSupported = CONTEXT_DISASSEMBLE_REQUEST_SUPPORTED.bindTo(contextKeyService);
64
this.focusedStackFrameHasInstructionPointerReference = CONTEXT_FOCUSED_STACK_FRAME_HAS_INSTRUCTION_POINTER_REFERENCE.bindTo(contextKeyService);
65
});
66
}
67
68
getId(): string {
69
return 'root';
70
}
71
72
get focusedSession(): IDebugSession | undefined {
73
return this._focusedSession;
74
}
75
76
get focusedThread(): IThread | undefined {
77
return this._focusedThread;
78
}
79
80
get focusedStackFrame(): IStackFrame | undefined {
81
return this._focusedStackFrame;
82
}
83
84
setFocus(stackFrame: IStackFrame | undefined, thread: IThread | undefined, session: IDebugSession | undefined, explicit: boolean): void {
85
const shouldEmitForStackFrame = this._focusedStackFrame !== stackFrame;
86
const shouldEmitForSession = this._focusedSession !== session;
87
const shouldEmitForThread = this._focusedThread !== thread;
88
89
90
this._focusedStackFrame = stackFrame;
91
this._focusedThread = thread;
92
this._focusedSession = session;
93
94
this.contextKeyService.bufferChangeEvents(() => {
95
this.loadedScriptsSupportedContextKey.set(!!session?.capabilities.supportsLoadedSourcesRequest);
96
this.stepBackSupportedContextKey.set(!!session?.capabilities.supportsStepBack);
97
this.restartFrameSupportedContextKey.set(!!session?.capabilities.supportsRestartFrame);
98
this.stepIntoTargetsSupported.set(!!session?.capabilities.supportsStepInTargetsRequest);
99
this.jumpToCursorSupported.set(!!session?.capabilities.supportsGotoTargetsRequest);
100
this.setVariableSupported.set(!!session?.capabilities.supportsSetVariable);
101
this.setDataBreakpointAtByteSupported.set(!!session?.capabilities.supportsDataBreakpointBytes);
102
this.setExpressionSupported.set(!!session?.capabilities.supportsSetExpression);
103
this.terminateDebuggeeSupported.set(!!session?.capabilities.supportTerminateDebuggee);
104
this.suspendDebuggeeSupported.set(!!session?.capabilities.supportSuspendDebuggee);
105
this.terminateThreadsSupported.set(!!session?.capabilities.supportsTerminateThreadsRequest);
106
this.disassembleRequestSupported.set(!!session?.capabilities.supportsDisassembleRequest);
107
this.focusedStackFrameHasInstructionPointerReference.set(!!stackFrame?.instructionPointerReference);
108
const attach = !!session && isSessionAttach(session);
109
this.focusedSessionIsAttach.set(attach);
110
this.focusedSessionIsNoDebug.set(!!session && !!session.configuration.noDebug);
111
});
112
113
if (shouldEmitForSession) {
114
this._onDidFocusSession.fire(session);
115
}
116
117
// should not call onDidFocusThread if onDidFocusStackFrame is called.
118
if (shouldEmitForStackFrame) {
119
this._onDidFocusStackFrame.fire({ stackFrame, explicit, session });
120
} else if (shouldEmitForThread) {
121
this._onDidFocusThread.fire({ thread, explicit, session });
122
}
123
}
124
125
get onDidFocusSession(): Event<IDebugSession | undefined> {
126
return this._onDidFocusSession.event;
127
}
128
129
get onDidFocusThread(): Event<{ thread: IThread | undefined; explicit: boolean; session: IDebugSession | undefined }> {
130
return this._onDidFocusThread.event;
131
}
132
133
get onDidFocusStackFrame(): Event<{ stackFrame: IStackFrame | undefined; explicit: boolean; session: IDebugSession | undefined }> {
134
return this._onDidFocusStackFrame.event;
135
}
136
137
get onDidChangeVisualization() {
138
return this._onDidChangeVisualization.event;
139
}
140
141
getSelectedExpression(): { expression: IExpression; settingWatch: boolean } | undefined {
142
return this.selectedExpression;
143
}
144
145
setSelectedExpression(expression: IExpression | undefined, settingWatch: boolean) {
146
this.selectedExpression = expression ? { expression, settingWatch: settingWatch } : undefined;
147
this.expressionSelectedContextKey.set(!!expression);
148
this._onDidSelectExpression.fire(this.selectedExpression);
149
}
150
151
get onDidSelectExpression(): Event<{ expression: IExpression; settingWatch: boolean } | undefined> {
152
return this._onDidSelectExpression.event;
153
}
154
155
get onDidEvaluateLazyExpression(): Event<IExpressionContainer> {
156
return this._onDidEvaluateLazyExpression.event;
157
}
158
159
updateViews(): void {
160
this._onWillUpdateViews.fire();
161
}
162
163
get onWillUpdateViews(): Event<void> {
164
return this._onWillUpdateViews.event;
165
}
166
167
isMultiSessionView(): boolean {
168
return !!this.multiSessionDebug.get();
169
}
170
171
setMultiSessionView(isMultiSessionView: boolean): void {
172
this.multiSessionDebug.set(isMultiSessionView);
173
}
174
175
setVisualizedExpression(original: IExpression, visualized: IExpression & { treeId: string } | undefined): void {
176
const current = this.visualized.get(original) || original;
177
const key = this.getPreferredVisualizedKey(original);
178
if (visualized) {
179
this.visualized.set(original, visualized);
180
this.preferredVisualizers.set(key, visualized.treeId);
181
} else {
182
this.visualized.delete(original);
183
this.preferredVisualizers.delete(key);
184
}
185
this._onDidChangeVisualization.fire({ original: current, replacement: visualized || original });
186
}
187
188
getVisualizedExpression(expression: IExpression): IExpression | string | undefined {
189
return this.visualized.get(expression) || this.preferredVisualizers.get(this.getPreferredVisualizedKey(expression));
190
}
191
192
async evaluateLazyExpression(expression: IExpressionContainer): Promise<void> {
193
await expression.evaluateLazy();
194
this._onDidEvaluateLazyExpression.fire(expression);
195
}
196
197
private getPreferredVisualizedKey(expr: IExpression) {
198
return JSON.stringify([
199
expr.name,
200
expr.type,
201
!!expr.memoryReference,
202
].join('\0'));
203
}
204
}
205
206