-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
debug-ext.ts
466 lines (407 loc) · 20.2 KB
/
debug-ext.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
// *****************************************************************************
// Copyright (C) 2018 Red Hat, Inc. and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
// *****************************************************************************
import { Emitter } from '@theia/core/lib/common/event';
import { Path } from '@theia/core/lib/common/path';
import * as theia from '@theia/plugin';
import { URI } from '@theia/core/shared/vscode-uri';
import { Breakpoint } from '../../common/plugin-api-rpc-model';
import { DebugConfigurationProviderTriggerKind, DebugExt, DebugMain, PLUGIN_RPC_CONTEXT as Ext, TerminalOptionsExt } from '../../common/plugin-api-rpc';
import { PluginPackageDebuggersContribution } from '../../common/plugin-protocol';
import { RPCProtocol } from '../../common/rpc-protocol';
import { CommandRegistryImpl } from '../command-registry';
import { ConnectionImpl } from '../../common/connection';
import { DEBUG_SCHEME, SCHEME_PATTERN } from '@theia/debug/lib/common/debug-uri-utils';
import { Disposable, Breakpoint as BreakpointExt, SourceBreakpoint, FunctionBreakpoint, Location, Range, URI as URIImpl } from '../types-impl';
import { PluginDebugAdapterSession } from './plugin-debug-adapter-session';
import { PluginDebugAdapterTracker } from './plugin-debug-adapter-tracker';
import uuid = require('uuid');
import { DebugAdapter } from '@theia/debug/lib/common/debug-model';
import { PluginDebugAdapterCreator } from './plugin-debug-adapter-creator';
import { NodeDebugAdapterCreator } from '../node/debug/plugin-node-debug-adapter-creator';
import { DebugProtocol } from '@vscode/debugprotocol';
interface ConfigurationProviderRecord {
handle: number;
type: string;
trigger: DebugConfigurationProviderTriggerKind,
provider: theia.DebugConfigurationProvider;
}
/* eslint-disable @typescript-eslint/no-explicit-any */
export class DebugExtImpl implements DebugExt {
// debug sessions by sessionId
private sessions = new Map<string, PluginDebugAdapterSession>();
private configurationProviderHandleGenerator: number;
private configurationProviders: ConfigurationProviderRecord[];
/**
* Only use internally, don't send it to the frontend. It's expensive!
* It's already there as a part of the plugin metadata.
*/
private debuggersContributions = new Map<string, PluginPackageDebuggersContribution>();
private descriptorFactories = new Map<string, theia.DebugAdapterDescriptorFactory>();
private trackerFactories: [string, theia.DebugAdapterTrackerFactory][] = [];
private contributionPaths = new Map<string, string>();
private contributionTypes = new Map<string, theia.PluginType>();
private connectionExt: ConnectionImpl;
private commandRegistryExt: CommandRegistryImpl;
private proxy: DebugMain;
private readonly onDidChangeBreakpointsEmitter = new Emitter<theia.BreakpointsChangeEvent>();
private readonly onDidChangeActiveDebugSessionEmitter = new Emitter<theia.DebugSession | undefined>();
private readonly onDidTerminateDebugSessionEmitter = new Emitter<theia.DebugSession>();
private readonly onDidStartDebugSessionEmitter = new Emitter<theia.DebugSession>();
private readonly onDidReceiveDebugSessionCustomEmitter = new Emitter<theia.DebugSessionCustomEvent>();
activeDebugSession: theia.DebugSession | undefined;
activeDebugConsole: theia.DebugConsole;
private readonly _breakpoints = new Map<string, theia.Breakpoint>();
private frontendAdapterCreator = new PluginDebugAdapterCreator();
private backendAdapterCreator = new NodeDebugAdapterCreator();
get breakpoints(): theia.Breakpoint[] {
return [...this._breakpoints.values()];
}
constructor(rpc: RPCProtocol) {
this.proxy = rpc.getProxy(Ext.DEBUG_MAIN);
this.activeDebugConsole = {
append: (value: string) => this.proxy.$appendToDebugConsole(value),
appendLine: (value: string) => this.proxy.$appendLineToDebugConsole(value)
};
this.configurationProviderHandleGenerator = 0;
this.configurationProviders = [];
}
/**
* Sets dependencies.
*/
assistedInject(connectionExt: ConnectionImpl, commandRegistryExt: CommandRegistryImpl): void {
this.connectionExt = connectionExt;
this.commandRegistryExt = commandRegistryExt;
}
/**
* Registers contributions.
* @param pluginFolder plugin folder path
* @param pluginType plugin type
* @param contributions available debuggers contributions
*/
registerDebuggersContributions(pluginFolder: string, pluginType: theia.PluginType, contributions: PluginPackageDebuggersContribution[]): void {
contributions.forEach(contribution => {
this.contributionPaths.set(contribution.type, pluginFolder);
this.contributionTypes.set(contribution.type, pluginType);
this.debuggersContributions.set(contribution.type, contribution);
this.proxy.$registerDebuggerContribution({
type: contribution.type,
label: contribution.label || contribution.type
});
console.log(`Debugger contribution has been registered: ${contribution.type}`);
});
}
get onDidReceiveDebugSessionCustomEvent(): theia.Event<theia.DebugSessionCustomEvent> {
return this.onDidReceiveDebugSessionCustomEmitter.event;
}
get onDidChangeActiveDebugSession(): theia.Event<theia.DebugSession | undefined> {
return this.onDidChangeActiveDebugSessionEmitter.event;
}
get onDidTerminateDebugSession(): theia.Event<theia.DebugSession> {
return this.onDidTerminateDebugSessionEmitter.event;
}
get onDidStartDebugSession(): theia.Event<theia.DebugSession> {
return this.onDidStartDebugSessionEmitter.event;
}
get onDidChangeBreakpoints(): theia.Event<theia.BreakpointsChangeEvent> {
return this.onDidChangeBreakpointsEmitter.event;
}
addBreakpoints(breakpoints: readonly theia.Breakpoint[]): void {
const added: theia.Breakpoint[] = [];
for (const b of breakpoints) {
if (this._breakpoints.has(b.id)) {
continue;
}
this._breakpoints.set(b.id, b);
added.push(b);
}
if (added.length) {
this.onDidChangeBreakpointsEmitter.fire({ added, removed: [], changed: [] });
this.proxy.$addBreakpoints(added);
}
}
removeBreakpoints(breakpoints: readonly theia.Breakpoint[]): void {
const removed: theia.Breakpoint[] = [];
const removedIds: string[] = [];
for (const b of breakpoints) {
if (!this._breakpoints.has(b.id)) {
continue;
}
this._breakpoints.delete(b.id);
removed.push(b);
removedIds.push(b.id);
}
if (removed.length) {
this.onDidChangeBreakpointsEmitter.fire({ added: [], removed, changed: [] });
this.proxy.$removeBreakpoints(removedIds);
}
}
startDebugging(folder: theia.WorkspaceFolder | undefined, nameOrConfiguration: string | theia.DebugConfiguration, options: theia.DebugSessionOptions): PromiseLike<boolean> {
return this.proxy.$startDebugging(folder, nameOrConfiguration, options);
}
stopDebugging(session?: theia.DebugSession): PromiseLike<void> {
return this.proxy.$stopDebugging(session?.id);
}
asDebugSourceUri(source: theia.DebugProtocolSource, session?: theia.DebugSession): theia.Uri {
return this.getDebugSourceUri(source, session?.id);
}
private getDebugSourceUri(raw: DebugProtocol.Source, sessionId?: string): theia.Uri {
if (raw.sourceReference && raw.sourceReference > 0) {
let query = 'ref=' + String(raw.sourceReference);
if (sessionId) {
query += `&session=${sessionId}`;
}
return URIImpl.from({ scheme: DEBUG_SCHEME, path: raw.path ?? '', query });
}
if (!raw.path) {
throw new Error('Unrecognized source type: ' + JSON.stringify(raw));
}
if (raw.path.match(SCHEME_PATTERN)) {
return URIImpl.parse(raw.path);
}
return URIImpl.file(raw.path);
}
registerDebugAdapterDescriptorFactory(debugType: string, factory: theia.DebugAdapterDescriptorFactory): Disposable {
if (this.descriptorFactories.has(debugType)) {
throw new Error(`Descriptor factory for ${debugType} has been already registered`);
}
this.descriptorFactories.set(debugType, factory);
return Disposable.create(() => this.descriptorFactories.delete(debugType));
}
registerDebugAdapterTrackerFactory(debugType: string, factory: theia.DebugAdapterTrackerFactory): Disposable {
if (!factory) {
return Disposable.create(() => { });
}
this.trackerFactories.push([debugType, factory]);
return Disposable.create(() => {
this.trackerFactories = this.trackerFactories.filter(tuple => tuple[1] !== factory);
});
}
registerDebugConfigurationProvider(debugType: string, provider: theia.DebugConfigurationProvider, trigger: DebugConfigurationProviderTriggerKind): Disposable {
console.log(`Debug configuration provider has been registered: ${debugType}, trigger: ${trigger}`);
const handle = this.configurationProviderHandleGenerator++;
this.configurationProviders.push({ handle, type: debugType, trigger, provider });
const descriptor = {
handle,
type: debugType,
trigger,
provideDebugConfiguration: !!provider.provideDebugConfigurations,
resolveDebugConfigurations: !!provider.resolveDebugConfiguration,
resolveDebugConfigurationWithSubstitutedVariables: !!provider.resolveDebugConfigurationWithSubstitutedVariables
};
this.proxy.$registerDebugConfigurationProvider(descriptor);
return Disposable.create(() => {
this.configurationProviders = this.configurationProviders.filter(p => (p.handle !== handle));
this.proxy.$unregisterDebugConfigurationProvider(handle);
});
}
async $onSessionCustomEvent(sessionId: string, event: string, body?: any): Promise<void> {
const session = this.sessions.get(sessionId);
if (session) {
this.onDidReceiveDebugSessionCustomEmitter.fire({ event, body, session });
}
}
async $sessionDidCreate(sessionId: string): Promise<void> {
const session = this.sessions.get(sessionId);
if (session) {
this.onDidStartDebugSessionEmitter.fire(session);
}
}
async $sessionDidDestroy(sessionId: string): Promise<void> {
const session = this.sessions.get(sessionId);
if (session) {
this.onDidTerminateDebugSessionEmitter.fire(session);
this.sessions.delete(sessionId);
}
}
async $sessionDidChange(sessionId: string | undefined): Promise<void> {
this.activeDebugSession = sessionId ? this.sessions.get(sessionId) : undefined;
this.onDidChangeActiveDebugSessionEmitter.fire(this.activeDebugSession);
}
async $breakpointsDidChange(added: Breakpoint[], removed: string[], changed: Breakpoint[]): Promise<void> {
const a: theia.Breakpoint[] = [];
const r: theia.Breakpoint[] = [];
const c: theia.Breakpoint[] = [];
for (const b of added) {
if (this._breakpoints.has(b.id)) {
continue;
}
const bExt = this.toBreakpointExt(b);
if (bExt) {
this._breakpoints.set(bExt.id, bExt);
a.push(bExt);
}
}
for (const id of removed) {
const bExt = this._breakpoints.get(id);
if (bExt) {
this._breakpoints.delete(id);
r.push(bExt);
}
}
for (const b of changed) {
const bExt = this._breakpoints.get(b.id);
if (bExt) {
const { functionName, location, enabled, condition, hitCondition, logMessage } = b;
if (bExt instanceof FunctionBreakpoint && functionName) {
Object.assign(bExt, { enabled, condition, hitCondition, logMessage, functionName });
} else if (bExt instanceof SourceBreakpoint && location) {
const range = new Range(location.range.startLineNumber, location.range.startColumn, location.range.endLineNumber, location.range.endColumn);
Object.assign(bExt, { enabled, condition, hitCondition, logMessage, location: new Location(URI.revive(location.uri), range) });
}
c.push(bExt);
}
}
this.onDidChangeBreakpointsEmitter.fire({ added: a, removed: r, changed: c });
}
protected toBreakpointExt({ functionName, location, enabled, condition, hitCondition, logMessage, id }: Breakpoint): BreakpointExt | undefined {
if (location) {
const range = new Range(location.range.startLineNumber, location.range.startColumn, location.range.endLineNumber, location.range.endColumn);
return new SourceBreakpoint(new Location(URI.revive(location.uri), range), enabled, condition, hitCondition, logMessage, id);
}
if (functionName) {
return new FunctionBreakpoint(functionName!, enabled, condition, hitCondition, logMessage, id);
}
return undefined;
}
async $createDebugSession(debugConfiguration: theia.DebugConfiguration, workspaceFolderUri: string | undefined): Promise<string> {
const sessionId = uuid.v4();
const theiaSession: theia.DebugSession = {
id: sessionId,
type: debugConfiguration.type,
name: debugConfiguration.name,
workspaceFolder: this.toWorkspaceFolder(workspaceFolderUri),
configuration: debugConfiguration,
customRequest: async (command: string, args?: any) => {
const response = await this.proxy.$customRequest(sessionId, command, args);
if (response && response.success) {
return response.body;
}
return Promise.reject(new Error(response.message ?? 'custom request failed'));
},
getDebugProtocolBreakpoint: async (breakpoint: Breakpoint) =>
this.proxy.$getDebugProtocolBreakpoint(sessionId, breakpoint.id)
};
const tracker = await this.createDebugAdapterTracker(theiaSession);
const communicationProvider = await this.createDebugAdapter(theiaSession, debugConfiguration);
const debugAdapterSession = new PluginDebugAdapterSession(communicationProvider, tracker, theiaSession);
this.sessions.set(sessionId, debugAdapterSession);
const connection = await this.connectionExt!.ensureConnection(sessionId);
debugAdapterSession.start(connection);
return sessionId;
}
async $terminateDebugSession(sessionId: string): Promise<void> {
const debugAdapterSession = this.sessions.get(sessionId);
if (debugAdapterSession) {
await debugAdapterSession.stop();
}
}
async $getTerminalCreationOptions(debugType: string): Promise<TerminalOptionsExt | undefined> {
return this.doGetTerminalCreationOptions(debugType);
}
async doGetTerminalCreationOptions(debugType: string): Promise<TerminalOptionsExt | undefined> {
return undefined;
}
private getConfigurationProviderRecord(handle: number): {
provider: theia.DebugConfigurationProvider
type: string
} {
const record = this.configurationProviders.find(p => p.handle === handle);
if (!record) {
throw new Error('No Debug configuration provider found with given handle number: ' + handle);
}
const { provider, type } = record;
return { provider, type };
}
async $provideDebugConfigurationsByHandle(
handle: number,
workspaceFolderUri: string | undefined
): Promise<theia.DebugConfiguration[]> {
const { provider, type } = this.getConfigurationProviderRecord(handle);
const configurations = await provider.provideDebugConfigurations?.(
this.toWorkspaceFolder(workspaceFolderUri)
);
if (!configurations) {
throw new Error(
'nothing returned from DebugConfigurationProvider.provideDebugConfigurations, type: ' + type
);
}
return configurations;
}
async $resolveDebugConfigurationByHandle(
handle: number,
workspaceFolderUri: string | undefined,
debugConfiguration: theia.DebugConfiguration
): Promise<theia.DebugConfiguration | undefined | null> {
const { provider } = this.getConfigurationProviderRecord(handle);
return provider.resolveDebugConfiguration?.(
this.toWorkspaceFolder(workspaceFolderUri),
debugConfiguration
);
}
async $resolveDebugConfigurationWithSubstitutedVariablesByHandle(
handle: number,
workspaceFolderUri: string | undefined,
debugConfiguration: theia.DebugConfiguration
): Promise<theia.DebugConfiguration | undefined | null> {
const { provider } = this.getConfigurationProviderRecord(handle);
return provider.resolveDebugConfigurationWithSubstitutedVariables?.(
this.toWorkspaceFolder(workspaceFolderUri),
debugConfiguration
);
}
protected async createDebugAdapterTracker(session: theia.DebugSession): Promise<theia.DebugAdapterTracker> {
return PluginDebugAdapterTracker.create(session, this.trackerFactories);
}
protected async createDebugAdapter(session: theia.DebugSession, debugConfiguration: theia.DebugConfiguration): Promise<DebugAdapter> {
const executable = await this.resolveDebugAdapterExecutable(debugConfiguration);
const descriptorFactory = this.descriptorFactories.get(session.type);
return this.getAdapterCreator(debugConfiguration).createDebugAdapter(session, debugConfiguration, executable, descriptorFactory);
}
protected async resolveDebugAdapterExecutable(debugConfiguration: theia.DebugConfiguration): Promise<theia.DebugAdapterExecutable | undefined> {
const { type } = debugConfiguration;
const contribution = this.debuggersContributions.get(type);
if (contribution) {
if (contribution.adapterExecutableCommand) {
const executable = await this.commandRegistryExt.executeCommand<theia.DebugAdapterExecutable>(contribution.adapterExecutableCommand);
if (executable) {
return executable;
}
} else {
const contributionPath = this.contributionPaths.get(type);
if (contributionPath) {
return this.getAdapterCreator(debugConfiguration).resolveDebugAdapterExecutable(contributionPath, contribution);
}
}
}
throw new Error(`It is not possible to provide debug adapter executable for '${debugConfiguration.type}'.`);
}
private toWorkspaceFolder(folder: string | undefined): theia.WorkspaceFolder | undefined {
if (!folder) {
return undefined;
}
const uri = URI.parse(folder);
const path = new Path(uri.path);
return {
uri: uri,
name: path.base,
index: 0
};
}
private getAdapterCreator(debugConfiguration: theia.DebugConfiguration): PluginDebugAdapterCreator {
const pluginType = this.contributionTypes.get(debugConfiguration.type);
return pluginType === 'frontend' ? this.frontendAdapterCreator : this.backendAdapterCreator;
}
}