This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathservice.ts
426 lines (371 loc) · 10.5 KB
/
service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { IClientSession } from '@jupyterlab/apputils';
import { ISignal, Signal } from '@phosphor/signaling';
import { DebugProtocol } from 'vscode-debugprotocol';
import { Debugger } from './debugger';
import { IDebugger } from './tokens';
import { Variables } from './variables';
import { Breakpoints } from './breakpoints';
import { Callstack } from './callstack';
/**
* A concrete implementation of IDebugger.
*/
export class DebugService implements IDebugger {
constructor() {
// Avoids setting session with invalid client
// session should be set only when a notebook or
// a console get the focus.
// TODO: also checks that the notebook or console
// runs a kernel with debugging ability
this._session = null;
// The model will be set by the UI which can be built
// after the service.
this._model = null;
}
/**
* Whether the debug service is disposed.
*/
get isDisposed(): boolean {
return this._isDisposed;
}
/**
* Returns the mode of the debugger UI.
*
* #### Notes
* There is only ever one debugger instance. If it is `expanded`, it exists
* as a `MainAreaWidget`, otherwise it is a sidebar.
*/
get mode(): IDebugger.Mode {
return this._model.mode;
}
/**
* Sets the mode of the debugger UI to the given parameter.
* @param mode - the new mode of the debugger UI.
*/
set mode(mode: IDebugger.Mode) {
this._model.mode = mode;
}
/**
* Returns the current debug session.
*/
get session(): IDebugger.ISession {
return this._session;
}
/**
* Sets the current debug session to the given parameter.
* @param session - the new debugger session.
*/
set session(session: IDebugger.ISession) {
if (this._session === session) {
return;
}
if (this._session) {
this._session.dispose();
}
this._session = session;
this._session.eventMessage.connect((_, event) => {
if (event.event === 'stopped') {
this._stoppedThreads.add(event.body.threadId);
void this.getAllFrames();
} else if (event.event === 'continued') {
this._stoppedThreads.delete(event.body.threadId);
this.clearModel();
}
this._eventMessage.emit(event);
});
this._sessionChanged.emit(session);
}
/**
* Returns the debugger model.
*/
get model(): Debugger.Model {
return this._model;
}
/**
* Sets the debugger model to the given parameter.
* @param model - The new debugger model.
*/
set model(model: Debugger.Model) {
this._model = model;
this._modelChanged.emit(model);
}
/**
* Signal emitted upon session changed.
*/
get sessionChanged(): ISignal<IDebugger, IDebugger.ISession> {
return this._sessionChanged;
}
/**
* Signal emitted upon model changed.
*/
get modelChanged(): ISignal<IDebugger, Debugger.Model> {
return this._modelChanged;
}
/**
* Signal emitted for debug event messages.
*/
get eventMessage(): ISignal<IDebugger, IDebugger.ISession.Event> {
return this._eventMessage;
}
/**
* Dispose the debug service.
*/
dispose(): void {
if (this.isDisposed) {
return;
}
this._isDisposed = true;
Signal.clearData(this);
}
/**
* Whether the current debugger is started.
*/
isStarted(): boolean {
return this._session !== null && this._session.isStarted;
}
/**
* Whether the current thread is stopped.
*/
isThreadStopped(): boolean {
return this._stoppedThreads.has(this.currentThread());
}
/**
* Starts a debugger.
* Precondition: canStart() && !isStarted()
*/
async start(): Promise<void> {
await this.session.start();
}
/**
* Stops the debugger.
* Precondition: isStarted()
*/
async stop(): Promise<void> {
await this.session.stop();
}
/**
* Restarts the debugger.
* Precondition: isStarted() and stopped.
*/
async restart(): Promise<void> {
const breakpoints = this.model.breakpointsModel.breakpoints;
await this.stop();
this.clearModel();
this._stoppedThreads.clear();
await this.start();
await this.updateBreakpoints(breakpoints);
}
/**
* Restore the state of a debug session.
* @param autoStart - when true, starts the debugger
* if it has not been started yet.
*/
async restoreState(autoStart: boolean): Promise<void> {
if (!this.model || !this.session) {
return;
}
await this.session.restoreState();
// TODO: restore breakpoints when the model is updated
if (!this.isStarted() && autoStart) {
await this.start();
}
}
/**
* Continues the execution of the current thread.
*/
async continue(): Promise<void> {
try {
await this.session.sendRequest('continue', {
threadId: this.currentThread()
});
this._stoppedThreads.delete(this.currentThread());
} catch (err) {
console.error('Error:', err.message);
}
}
/**
* Makes the current thread run again for one step.
*/
async next(): Promise<void> {
try {
await this.session.sendRequest('next', {
threadId: this.currentThread()
});
} catch (err) {
console.error('Error:', err.message);
}
}
/**
* Makes the current thread step in a function / method if possible.
*/
async stepIn(): Promise<void> {
try {
await this.session.sendRequest('stepIn', {
threadId: this.currentThread()
});
} catch (err) {
console.error('Error:', err.message);
}
}
/**
* Makes the current thread step out a function / method if possible.
*/
async stepOut(): Promise<void> {
try {
await this.session.sendRequest('stepOut', {
threadId: this.currentThread()
});
} catch (err) {
console.error('Error:', err.message);
}
}
/**
* Update all breakpoints at once.
*/
async updateBreakpoints(breakpoints: Breakpoints.IBreakpoint[]) {
if (!this.session.isStarted) {
return;
}
// Workaround: this should not be called before the session has started
await this.ensureSessionReady();
const code = this._model.codeValue.text;
const dumpedCell = await this.dumpCell(code);
const sourceBreakpoints = Private.toSourceBreakpoints(breakpoints);
const reply = await this.setBreakpoints(
sourceBreakpoints,
dumpedCell.sourcePath
);
let kernelBreakpoints = reply.body.breakpoints.map(breakpoint => {
return {
...breakpoint,
active: true,
source: { path: this.session.client.name }
};
});
// filter breakpoints with the same line number
kernelBreakpoints = kernelBreakpoints.filter(
(breakpoint, i, arr) =>
arr.findIndex(el => el.line === breakpoint.line) === i
);
this._model.breakpointsModel.breakpoints = kernelBreakpoints;
await this.session.sendRequest('configurationDone', {});
}
getAllFrames = async () => {
const stackFrames = await this.getFrames(this.currentThread());
stackFrames.forEach(async (frame, index) => {
const scopes = await this.getScopes(frame);
const variables = await this.getVariables(scopes);
const values = this.convertScope(scopes, variables);
this.frames.push({
id: frame.id,
scopes: values
});
if (index === 0) {
this._model.variablesModel.scopes = values;
}
});
if (stackFrames) {
this._model.callstackModel.frames = stackFrames;
}
this._model.callstackModel.currentFrameChanged.connect(this.onChangeFrame);
};
onChangeFrame = (_: Callstack.Model, update: Callstack.IFrame) => {
const frame = this.frames.find(ele => ele.id === update.id);
if (frame && frame.scopes) {
this._model.variablesModel.scopes = frame.scopes;
}
};
dumpCell = async (code: string) => {
const reply = await this.session.sendRequest('dumpCell', { code });
return reply.body;
};
getFrames = async (threadId: number) => {
const reply = await this.session.sendRequest('stackTrace', {
threadId
});
const stackFrames = reply.body.stackFrames;
return stackFrames;
};
getScopes = async (frame: DebugProtocol.StackFrame) => {
if (!frame) {
return;
}
const reply = await this.session.sendRequest('scopes', {
frameId: frame.id
});
return reply.body.scopes;
};
getVariables = async (scopes: DebugProtocol.Scope[]) => {
if (!scopes || scopes.length === 0) {
return;
}
const reply = await this.session.sendRequest('variables', {
variablesReference: scopes[0].variablesReference
});
return reply.body.variables;
};
setBreakpoints = async (
breakpoints: DebugProtocol.SourceBreakpoint[],
path: string
) => {
// Workaround: this should not be called before the session has started
await this.ensureSessionReady();
return await this.session.sendRequest('setBreakpoints', {
breakpoints: breakpoints,
source: { path },
sourceModified: false
});
};
protected convertScope = (
scopes: DebugProtocol.Scope[],
variables: DebugProtocol.Variable[]
): Variables.IScope[] => {
if (!variables || !scopes) {
return;
}
return scopes.map(scope => {
return {
name: scope.name,
variables: variables.map(variable => {
return { ...variable };
})
};
});
};
private async ensureSessionReady(): Promise<void> {
const client = this.session.client as IClientSession;
return client.ready;
}
private clearModel() {
this._model.callstackModel.frames = [];
this._model.variablesModel.scopes = [];
}
private currentThread(): number {
// TODO: ask the model for the current thread ID
return 1;
}
private _isDisposed: boolean = false;
private _session: IDebugger.ISession;
private _sessionChanged = new Signal<IDebugger, IDebugger.ISession>(this);
private _modelChanged = new Signal<IDebugger, Debugger.Model>(this);
private _eventMessage = new Signal<IDebugger, IDebugger.ISession.Event>(this);
private _model: Debugger.Model;
// TODO: remove frames from the service
private frames: Frame[] = [];
// TODO: move this in model
private _stoppedThreads = new Set();
}
export type Frame = {
id: number;
scopes: Variables.IScope[];
};
namespace Private {
export function toSourceBreakpoints(breakpoints: Breakpoints.IBreakpoint[]) {
return breakpoints.map(breakpoint => {
return {
line: breakpoint.line
};
});
}
}