-
Notifications
You must be signed in to change notification settings - Fork 288
/
debuggingManager.ts
273 lines (246 loc) · 11.2 KB
/
debuggingManager.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
'use strict';
import { inject, injectable } from 'inversify';
import {
debug,
DebugAdapterDescriptor,
DebugAdapterInlineImplementation,
DebugSession,
DebugSessionOptions,
NotebookCell,
NotebookDocument,
Uri
} from 'vscode';
import { IKernelProvider } from '../../kernels/types';
import { IExtensionSyncActivationService } from '../../platform/activation/types';
import {
IApplicationShell,
ICommandManager,
IDebugService,
IVSCodeNotebook
} from '../../platform/common/application/types';
import { EditorContexts } from '../../platform/common/constants';
import { ContextKey } from '../../platform/common/contextKey';
import { IPlatformService } from '../../platform/common/platform/types';
import { IConfigurationService } from '../../platform/common/types';
import { DataScience } from '../../platform/common/utils/localize';
import { noop } from '../../platform/common/utils/misc';
import { IServiceContainer } from '../../platform/ioc/types';
import { traceInfo } from '../../platform/logging';
import { ResourceSet } from '../../platform/vscode-path/map';
import * as path from '../../platform/vscode-path/path';
import { sendTelemetryEvent } from '../../telemetry';
import { IControllerLoader, IControllerRegistration } from '../controllers/types';
import { DebuggingTelemetry, pythonKernelDebugAdapter } from './constants';
import { DebugCellController } from './controllers/debugCellController';
import { RestartController } from './controllers/restartController';
import { RunByLineController } from './controllers/runByLineController';
import { Debugger } from './debugger';
import { DebuggingManagerBase } from './debuggingManagerBase';
import { INotebookDebugConfig, INotebookDebuggingManager, KernelDebugMode } from './debuggingTypes';
import { assertIsDebugConfig, IpykernelCheckResult } from './helper';
import { KernelDebugAdapter } from './kernelDebugAdapter';
/**
* The DebuggingManager maintains the mapping between notebook documents and debug sessions.
*/
@injectable()
export class DebuggingManager
extends DebuggingManagerBase
implements IExtensionSyncActivationService, INotebookDebuggingManager
{
private runByLineCells: ContextKey<Uri[]>;
private runByLineDocuments: ContextKey<Uri[]>;
private debugDocuments: ContextKey<Uri[]>;
private notebookToRunByLineController = new Map<NotebookDocument, RunByLineController>();
public constructor(
@inject(IKernelProvider) kernelProvider: IKernelProvider,
@inject(IControllerLoader) controllerLoader: IControllerLoader,
@inject(IControllerRegistration) controllerRegistration: IControllerRegistration,
@inject(ICommandManager) commandManager: ICommandManager,
@inject(IApplicationShell) appShell: IApplicationShell,
@inject(IVSCodeNotebook) vscNotebook: IVSCodeNotebook,
@inject(IConfigurationService) private readonly configurationService: IConfigurationService,
@inject(IPlatformService) private readonly platform: IPlatformService,
@inject(IDebugService) private readonly debugService: IDebugService,
@inject(IServiceContainer) serviceContainer: IServiceContainer
) {
super(
kernelProvider,
controllerLoader,
controllerRegistration,
commandManager,
appShell,
vscNotebook,
serviceContainer
);
this.runByLineCells = new ContextKey(EditorContexts.RunByLineCells, commandManager);
this.runByLineDocuments = new ContextKey(EditorContexts.RunByLineDocuments, commandManager);
this.debugDocuments = new ContextKey(EditorContexts.DebugDocuments, commandManager);
}
public override activate() {
super.activate();
this.disposables.push(
// factory for kernel debug adapters
debug.registerDebugAdapterDescriptorFactory(pythonKernelDebugAdapter, {
createDebugAdapterDescriptor: async (session) => this.createDebugAdapterDescriptor(session)
})
);
}
public getDebugMode(notebook: NotebookDocument): KernelDebugMode | undefined {
const controller = this.notebookToRunByLineController.get(notebook);
return controller?.getMode();
}
protected override onDidStopDebugging(notebook: NotebookDocument) {
super.onDidStopDebugging(notebook);
this.notebookToRunByLineController.delete(notebook);
this.updateRunByLineContextKeys();
this.updateDebugContextKey();
}
private updateRunByLineContextKeys() {
const rblCellUris: Uri[] = [];
const rblDocumentUris: Uri[] = [];
this.notebookToRunByLineController.forEach((controller) => {
rblCellUris.push(controller.debugCell.document.uri);
rblDocumentUris.push(controller.debugCell.notebook.uri);
});
this.runByLineCells.set(rblCellUris).ignoreErrors();
this.runByLineDocuments.set(rblDocumentUris).ignoreErrors();
}
private updateDebugContextKey() {
const debugDocumentUris = new ResourceSet();
this.notebookToDebugAdapter.forEach((_, notebook) => debugDocumentUris.add(notebook.uri));
this.notebookInProgress.forEach((notebook) => debugDocumentUris.add(notebook.uri));
this.debugDocuments.set(Array.from(debugDocumentUris.values())).ignoreErrors();
}
public async tryToStartDebugging(mode: KernelDebugMode, cell: NotebookCell, skipIpykernelCheck = false) {
traceInfo(`Starting debugging with mode ${mode}`);
if (!skipIpykernelCheck) {
const ipykernelResult = await this.checkIpykernelAndPrompt(cell);
if (ipykernelResult !== IpykernelCheckResult.Ok) {
traceInfo(`Ipykernel check failed: ${IpykernelCheckResult[ipykernelResult]}`);
return;
}
}
if (mode === KernelDebugMode.RunByLine || mode === KernelDebugMode.Cell) {
await this.startDebuggingCell(mode, cell!);
}
}
public runByLineNext(cell: NotebookCell) {
const controller = this.notebookToRunByLineController.get(cell.notebook);
if (controller && controller.debugCell.document.uri.toString() === cell.document.uri.toString()) {
controller.continue();
}
}
public runByLineStop(cell: NotebookCell) {
const controller = this.notebookToRunByLineController.get(cell.notebook);
if (controller) {
sendTelemetryEvent(DebuggingTelemetry.endedSession, undefined, {
reason: 'withKeybinding'
});
controller.stop();
}
}
private async startDebuggingCell(mode: KernelDebugMode.Cell | KernelDebugMode.RunByLine, cell: NotebookCell) {
const doc = cell.notebook;
const settings = this.configurationService.getSettings(doc.uri);
const config: INotebookDebugConfig = {
type: pythonKernelDebugAdapter,
name: path.basename(doc.uri.toString()),
request: 'attach',
justMyCode: mode === KernelDebugMode.Cell ? settings.debugJustMyCode : true,
// add a property to the config to know if the session is runByLine
__mode: mode,
__cellIndex: cell.index,
__notebookUri: doc.uri.toString()
};
const opts: DebugSessionOptions | undefined =
mode === KernelDebugMode.RunByLine
? {
suppressDebugStatusbar: true,
suppressDebugToolbar: true,
suppressDebugView: true,
suppressSaveBeforeStart: true
}
: { suppressSaveBeforeStart: true };
return this.startDebuggingConfig(config, opts);
}
protected async createDebugAdapterDescriptor(session: DebugSession): Promise<DebugAdapterDescriptor | undefined> {
const config = session.configuration;
assertIsDebugConfig(config);
const notebookUri = config.__notebookUri;
const notebook = this.vscNotebook.notebookDocuments.find((doc) => doc.uri.toString() === notebookUri);
if (!notebook) {
traceInfo(`Cannot start debugging. Notebook ${notebookUri} not found.`);
return;
}
if (this.notebookInProgress.has(notebook)) {
traceInfo(`Cannot start debugging. Already debugging this notebook`);
return;
}
if (this.isDebugging(notebook)) {
traceInfo(`Cannot start debugging. Already debugging this notebook document.`);
return;
}
this.notebookToDebugger.set(notebook, new Debugger(notebook, config, session));
try {
this.notebookInProgress.add(notebook);
this.updateDebugContextKey();
return await this.doCreateDebugAdapterDescriptor(config, session, notebook);
} finally {
this.notebookInProgress.delete(notebook);
this.updateDebugContextKey();
}
}
private async doCreateDebugAdapterDescriptor(
config: INotebookDebugConfig,
session: DebugSession,
notebook: NotebookDocument
): Promise<DebugAdapterDescriptor | undefined> {
const kernel = await this.ensureKernelIsRunning(notebook);
if (kernel?.session) {
const adapter = new KernelDebugAdapter(
session,
notebook,
kernel.session,
kernel,
this.platform,
this.debugService
);
if (config.__mode === KernelDebugMode.RunByLine && typeof config.__cellIndex === 'number') {
const cell = notebook.cellAt(config.__cellIndex);
const rblController = new RunByLineController(
adapter,
cell,
this.commandManager,
this.kernelProvider.getKernelExecution(kernel!),
this.configurationService
);
adapter.addDebuggingDelegates([
rblController,
new RestartController(KernelDebugMode.RunByLine, adapter, cell, this.serviceContainer)
]);
this.notebookToRunByLineController.set(notebook, rblController);
this.updateRunByLineContextKeys();
} else if (config.__mode === KernelDebugMode.Cell && typeof config.__cellIndex === 'number') {
const cell = notebook.cellAt(config.__cellIndex);
const controller = new DebugCellController(
adapter,
cell,
this.kernelProvider.getKernelExecution(kernel!),
this.commandManager
);
adapter.addDebuggingDelegates([
controller,
new RestartController(KernelDebugMode.Cell, adapter, cell, this.serviceContainer)
]);
}
this.trackDebugAdapter(notebook, adapter);
this.updateDebugContextKey();
return new DebugAdapterInlineImplementation(adapter);
} else {
this.appShell.showInformationMessage(DataScience.kernelWasNotStarted()).then(noop, noop);
}
return;
}
}