-
Notifications
You must be signed in to change notification settings - Fork 315
/
Copy pathkernelDebugAdapterBase.ts
366 lines (333 loc) · 14.2 KB
/
kernelDebugAdapterBase.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { KernelMessage } from '@jupyterlab/services';
import * as path from '../../platform/vscode-path/path';
import {
debug,
DebugAdapter,
DebugProtocolMessage,
DebugSession,
Disposable,
Event,
EventEmitter,
NotebookCell,
NotebookCellExecutionState,
NotebookCellExecutionStateChangeEvent,
NotebookCellKind,
NotebookDocument,
notebooks,
Uri,
workspace
} from 'vscode';
import { DebugProtocol } from 'vscode-debugprotocol';
import { IKernelConnectionSession, IKernel } from '../../kernels/types';
import { IPlatformService } from '../../platform/common/platform/types';
import { DebuggingTelemetry } from './constants';
import {
IKernelDebugAdapter,
IKernelDebugAdapterConfig,
IDebuggingDelegate,
KernelDebugMode,
IDebugInfoResponse
} from './debuggingTypes';
import { sendTelemetryEvent } from '../../telemetry';
import { traceError, traceInfo, traceInfoIfCI, traceVerbose, traceWarning } from '../../platform/logging';
import { assertIsDebugConfig, isShortNamePath, shortNameMatchesLongName, getMessageSourceAndHookIt } from './helper';
import { IDisposable } from '../../platform/common/types';
import { executeSilently } from '../../kernels/helpers';
import { noop } from '../../platform/common/utils/misc';
import { IDebugService } from '../../platform/common/application/types';
/**
* For info on the custom requests implemented by jupyter see:
* https://jupyter-client.readthedocs.io/en/stable/messaging.html#debug-request
* https://jupyter-client.readthedocs.io/en/stable/messaging.html#additions-to-the-dap
*/
/**
* Base class for a debug adapter for connecting to a jupyter kernel. The DebugAdapter is responsible for translating DAP requests for a kernel.
*/
export abstract class KernelDebugAdapterBase implements DebugAdapter, IKernelDebugAdapter, IDisposable {
protected readonly fileToCell = new Map<string, Uri>();
private readonly sendMessage = new EventEmitter<DebugProtocolMessage>();
private readonly endSession = new EventEmitter<DebugSession>();
private readonly configuration: IKernelDebugAdapterConfig;
protected readonly disposables: IDisposable[] = [];
private delegate: IDebuggingDelegate | undefined;
onDidSendMessage: Event<DebugProtocolMessage> = this.sendMessage.event;
onDidEndSession: Event<DebugSession> = this.endSession.event;
public readonly debugCell: NotebookCell | undefined;
private disconnected: boolean = false;
private kernelEventHook = (_event: 'willRestart' | 'willInterrupt') => this.disconnect();
constructor(
protected session: DebugSession,
protected notebookDocument: NotebookDocument,
protected readonly jupyterSession: IKernelConnectionSession,
private readonly kernel: IKernel | undefined,
private readonly platformService: IPlatformService,
private readonly debugService: IDebugService
) {
traceInfoIfCI(`Creating kernel debug adapter for debugging notebooks`);
const configuration = this.session.configuration;
assertIsDebugConfig(configuration);
this.configuration = configuration;
if (
configuration.__mode === KernelDebugMode.InteractiveWindow ||
configuration.__mode === KernelDebugMode.Cell ||
configuration.__mode === KernelDebugMode.RunByLine
) {
this.debugCell = notebookDocument.cellAt(configuration.__cellIndex!);
}
this.jupyterSession.kernel?.iopubMessage.connect(this.onIOPubMessage, this);
this.disposables.push(
new Disposable(() => this.jupyterSession.kernel?.iopubMessage.disconnect(this.onIOPubMessage, this))
);
if (this.kernel) {
this.kernel.addEventHook(this.kernelEventHook);
this.disposables.push(
this.kernel.onDisposed(() => {
if (!this.disconnected) {
debug.stopDebugging(this.session).then(noop, noop);
this.disconnect().ignoreErrors();
sendTelemetryEvent(DebuggingTelemetry.endedSession, undefined, { reason: 'onKernelDisposed' });
}
})
);
}
this.disposables.push(
notebooks.onDidChangeNotebookCellExecutionState(
(cellStateChange: NotebookCellExecutionStateChangeEvent) => {
// If a cell has moved to idle, stop the debug session
if (
this.configuration.__cellIndex === cellStateChange.cell.index &&
cellStateChange.state === NotebookCellExecutionState.Idle &&
!this.disconnected
) {
sendTelemetryEvent(DebuggingTelemetry.endedSession, undefined, { reason: 'normally' });
this.disconnect().ignoreErrors();
}
},
this,
this.disposables
)
);
this.disposables.push(
workspace.onDidChangeNotebookDocument(
(e) => {
e.contentChanges.forEach((change) => {
change.removedCells.forEach((cell: NotebookCell) => {
if (cell === this.debugCell) {
this.disconnect().ignoreErrors();
}
});
});
},
this,
this.disposables
)
);
this.disposables.push(
this.debugService.onDidTerminateDebugSession((e) => {
if (e === this.session) {
this.disconnect().ignoreErrors();
}
})
);
}
public setDebuggingDelegate(delegate: IDebuggingDelegate) {
this.delegate = delegate;
}
private trace(tag: string, msg: string) {
traceVerbose(`[Debug] ${tag}: ${msg}`);
}
async onIOPubMessage(_: unknown, msg: KernelMessage.IIOPubMessage) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const anyMsg = msg as any;
traceInfoIfCI(`Debug IO Pub message: ${JSON.stringify(msg)}`);
if (anyMsg.header.msg_type === 'debug_event') {
this.trace('event', JSON.stringify(msg));
if (!(await this.delegate?.willSendEvent(anyMsg))) {
this.sendMessage.fire(msg.content);
}
}
}
async handleMessage(message: DebugProtocol.ProtocolMessage) {
try {
traceInfoIfCI(`KernelDebugAdapter::handleMessage ${JSON.stringify(message, undefined, ' ')}`);
// intercept 'setBreakpoints' request
if (message.type === 'request' && (message as DebugProtocol.Request).command === 'setBreakpoints') {
const args = (message as DebugProtocol.Request).arguments;
if (args.source && args.source.path && args.source.path.indexOf('vscode-notebook-cell:') === 0) {
const cell = this.notebookDocument
.getCells()
.find((c) => c.document.uri.toString() === args.source.path);
if (cell) {
await this.dumpCell(cell.index);
}
}
}
// after attaching, send a 'debugInfo' request
// reset breakpoints and continue stopped threads if there are any
// we do this in case the kernel is stopped when we attach
// This might happen if VS Code or the extension host crashes
if (message.type === 'request' && (message as DebugProtocol.Request).command === 'attach') {
await this.debugInfo();
}
if (message.type === 'request') {
await this.delegate?.willSendRequest(message as DebugProtocol.Request);
}
return this.sendRequestToJupyterSession(message);
} catch (e) {
traceError(`KernelDebugAdapter::handleMessage failure: ${e}`);
}
}
public getConfiguration(): IKernelDebugAdapterConfig {
return this.configuration;
}
public stepIn(threadId: number): Thenable<DebugProtocol.StepInResponse['body']> {
return this.session.customRequest('stepIn', { threadId });
}
public async disconnect() {
if (!this.disconnected) {
this.disconnected = true;
if (this.debugService.activeDebugSession === this.session) {
await this.session.customRequest('disconnect', { restart: false });
}
this.endSession.fire(this.session);
this.kernel?.removeEventHook(this.kernelEventHook);
}
}
dispose() {
this.deleteDumpedFiles().catch((ex) => traceWarning('Error deleting temporary debug files.', ex));
this.disposables.forEach((d) => d.dispose());
}
public stackTrace(args: DebugProtocol.StackTraceArguments): Thenable<DebugProtocol.StackTraceResponse['body']> {
return this.session.customRequest('stackTrace', args);
}
public setBreakpoints(
args: DebugProtocol.SetBreakpointsArguments
): Thenable<DebugProtocol.SetBreakpointsResponse['body']> {
return this.session.customRequest('setBreakpoints', args);
}
public async dumpAllCells() {
await Promise.all(
this.notebookDocument.getCells().map(async (cell) => {
if (cell.kind === NotebookCellKind.Code) {
await this.dumpCell(cell.index);
}
})
);
}
protected abstract dumpCell(index: number): Promise<void>;
private async debugInfo(): Promise<void> {
const response = await this.session.customRequest('debugInfo');
// If there's stopped threads at this point, continue them all
(response as IDebugInfoResponse).stoppedThreads.forEach((thread: number) => {
this.jupyterSession.requestDebug({
seq: 0,
type: 'request',
command: 'continue',
arguments: {
threadId: thread
}
});
});
}
private lookupCellByLongName(sourcePath: string) {
if (!this.platformService.isWindows) {
return undefined;
}
sourcePath = path.normalize(sourcePath);
for (let [file, cell] of this.fileToCell.entries()) {
if (isShortNamePath(file) && shortNameMatchesLongName(file, sourcePath)) {
return cell;
}
}
return undefined;
}
protected async sendRequestToJupyterSession(message: DebugProtocol.ProtocolMessage) {
if (this.jupyterSession.disposed || this.jupyterSession.status === 'dead') {
traceInfo(`Skipping sending message ${message.type} because session is disposed`);
return;
}
// map Source paths from VS Code to Ipykernel temp files
getMessageSourceAndHookIt(message, this.translateRealFileToDebuggerFile.bind(this));
this.trace('to kernel', JSON.stringify(message));
if (message.type === 'request') {
const request = message as DebugProtocol.Request;
const control = this.jupyterSession.requestDebug(
{
seq: request.seq,
type: 'request',
command: request.command,
arguments: request.arguments
},
true
);
control.onReply = (msg) => {
const message = msg.content as DebugProtocol.ProtocolMessage;
getMessageSourceAndHookIt(message, this.translateDebuggerFileToRealFile.bind(this));
this.trace('response', JSON.stringify(message));
this.sendMessage.fire(message);
};
return control.done;
} else if (message.type === 'response') {
// responses of reverse requests
const response = message as DebugProtocol.Response;
const control = this.jupyterSession.requestDebug(
{
seq: response.seq,
type: 'request',
command: response.command
},
true
);
return control.done;
} else {
// cannot send via iopub, no way to handle events even if they existed
traceError(`Unknown message type to send ${message.type}`);
}
}
protected translateDebuggerFileToRealFile(
source: DebugProtocol.Source | undefined,
_lines?: { line?: number; endLine?: number; lines?: number[] }
) {
if (source && source.path) {
const mapping = this.fileToCell.get(source.path) ?? this.lookupCellByLongName(source.path);
if (mapping) {
source.name = path.basename(mapping.path);
source.path = mapping.toString();
}
}
}
protected abstract translateRealFileToDebuggerFile(
source: DebugProtocol.Source | undefined,
_lines?: { line?: number; endLine?: number; lines?: number[] }
): void;
protected abstract getDumpFilesForDeletion(): string[];
private async deleteDumpedFiles() {
const fileValues = this.getDumpFilesForDeletion();
// Need to have our Jupyter Session and some dumpCell files to delete
if (this.jupyterSession && fileValues.length) {
// Create our python string of file names
const fileListString = fileValues
.map((filePath) => {
// escape Windows path separators again for python
return '"' + filePath.replace(/\\/g, '\\\\') + '"';
})
.join(',');
// Insert into our delete snippet
const deleteFilesCode = `import os
_VSCODE_fileList = [${fileListString}]
for file in _VSCODE_fileList:
try:
os.remove(file)
except:
pass
del _VSCODE_fileList`;
return executeSilently(this.jupyterSession, deleteFilesCode, {
traceErrors: true,
traceErrorsMessage: 'Error deleting temporary debugging files'
});
}
}
}