Skip to content

Commit

Permalink
reify the DebugAdapter; fixes #45129
Browse files Browse the repository at this point in the history
  • Loading branch information
weinand committed Apr 6, 2018
1 parent ced0b88 commit 4cd09ff
Show file tree
Hide file tree
Showing 12 changed files with 836 additions and 350 deletions.
75 changes: 74 additions & 1 deletion src/vs/workbench/api/electron-browser/mainThreadDebugService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,27 @@

import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import uri from 'vs/base/common/uri';
import { IDebugService, IConfig, IDebugConfigurationProvider, IBreakpoint, IFunctionBreakpoint, IBreakpointData } from 'vs/workbench/parts/debug/common/debug';
import { IDebugService, IConfig, IDebugConfigurationProvider, IBreakpoint, IFunctionBreakpoint, IBreakpointData, IAdapterExecutable } from 'vs/workbench/parts/debug/common/debug';
import { TPromise } from 'vs/base/common/winjs.base';
import {
ExtHostContext, ExtHostDebugServiceShape, MainThreadDebugServiceShape, DebugSessionUUID, MainContext,
IExtHostContext, IBreakpointsDeltaDto, ISourceMultiBreakpointDto, ISourceBreakpointDto, IFunctionBreakpointDto
} from '../node/extHost.protocol';
import { extHostNamedCustomer } from 'vs/workbench/api/electron-browser/extHostCustomers';
import severity from 'vs/base/common/severity';
import { AbstractDebugAdapter } from 'vs/workbench/parts/debug/node/v8Protocol';
import { convertToDAPaths, convertToVSCPaths } from 'vs/workbench/parts/debug/node/DapPathConverter';
import * as paths from 'vs/base/common/paths';


@extHostNamedCustomer(MainContext.MainThreadDebugService)
export class MainThreadDebugService implements MainThreadDebugServiceShape {

private _proxy: ExtHostDebugServiceShape;
private _toDispose: IDisposable[];
private _breakpointEventsActive: boolean;
private _debugAdapters: Map<number, ExtensionHostDebugAdapter>;
private _debugAdaptersHandleCounter = 1;

constructor(
extHostContext: IExtHostContext,
Expand All @@ -46,6 +52,18 @@ export class MainThreadDebugService implements MainThreadDebugServiceShape {
}
}
}));

this._debugAdapters = new Map<number, ExtensionHostDebugAdapter>();

// register a default DA provider
debugService.getConfigurationManager().registerDebugAdapterProvider('*', {
createDebugAdapter: (debugType, adapterInfo) => {
const handle = this._debugAdaptersHandleCounter++;
const da = new ExtensionHostDebugAdapter(handle, this._proxy, debugType, adapterInfo);
this._debugAdapters.set(handle, da);
return da;
}
});
}

public dispose(): void {
Expand Down Expand Up @@ -208,4 +226,59 @@ export class MainThreadDebugService implements MainThreadDebugServiceShape {
this.debugService.logToRepl(value, severity.Warning);
return TPromise.wrap<void>(undefined);
}

public $acceptDAMessage(handle: number, message: DebugProtocol.ProtocolMessage) {

convertToVSCPaths(message, source => {
if (typeof source.path === 'object') {
source.path = uri.revive(source.path).toString();
}
});

this._debugAdapters.get(handle).acceptMessage(message);
}

public $acceptDAError(handle: number, name: string, message: string, stack: string) {
this._debugAdapters.get(handle).fireError(handle, new Error(`${name}: ${message}\n${stack}`));
}

public $acceptDAExit(handle: number, code: number, signal: string) {
this._debugAdapters.get(handle).fireExit(handle, code, signal);
}
}

class ExtensionHostDebugAdapter extends AbstractDebugAdapter {

constructor(private _handle: number, private _proxy: ExtHostDebugServiceShape, private _debugType: string, private _adapterExecutable: IAdapterExecutable | null) {
super();
}

public fireError(handle: number, err: Error) {
this._onError.fire(err);
}

public fireExit(handle: number, code: number, signal: string) {
this._onExit.fire(code);
}

public startSession(): TPromise<void> {
return this._proxy.$startDASession(this._handle, this._debugType, this._adapterExecutable);
}

public sendMessage(message: DebugProtocol.ProtocolMessage): void {

convertToDAPaths(message, source => {
if (paths.isAbsolute(source.path)) {
(<any>source).path = uri.file(source.path);
} else {
(<any>source).path = uri.parse(source.path);
}
});

this._proxy.$sendDAMessage(this._handle, message);
}

public stopSession(): TPromise<void> {
return this._proxy.$stopDASession(this._handle);
}
}
2 changes: 1 addition & 1 deletion src/vs/workbench/api/node/extHost.api.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export function createApiFactory(
const extHostCommands = rpcProtocol.set(ExtHostContext.ExtHostCommands, new ExtHostCommands(rpcProtocol, extHostHeapService, extHostLogService));
const extHostTreeViews = rpcProtocol.set(ExtHostContext.ExtHostTreeViews, new ExtHostTreeViews(rpcProtocol.getProxy(MainContext.MainThreadTreeViews), extHostCommands));
rpcProtocol.set(ExtHostContext.ExtHostWorkspace, extHostWorkspace);
const extHostDebugService = rpcProtocol.set(ExtHostContext.ExtHostDebugService, new ExtHostDebugService(rpcProtocol, extHostWorkspace));
const extHostDebugService = rpcProtocol.set(ExtHostContext.ExtHostDebugService, new ExtHostDebugService(rpcProtocol, extHostWorkspace, extensionService));
rpcProtocol.set(ExtHostContext.ExtHostConfiguration, extHostConfiguration);
const extHostDiagnostics = rpcProtocol.set(ExtHostContext.ExtHostDiagnostics, new ExtHostDiagnostics(rpcProtocol));
const extHostLanguageFeatures = rpcProtocol.set(ExtHostContext.ExtHostLanguageFeatures, new ExtHostLanguageFeatures(rpcProtocol, extHostDocuments, extHostCommands, extHostHeapService, extHostDiagnostics));
Expand Down
6 changes: 6 additions & 0 deletions src/vs/workbench/api/node/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,9 @@ export interface MainThreadSCMShape extends IDisposable {
export type DebugSessionUUID = string;

export interface MainThreadDebugServiceShape extends IDisposable {
$acceptDAMessage(handle: number, message: DebugProtocol.ProtocolMessage);
$acceptDAError(handle: number, name: string, message: string, stack: string);
$acceptDAExit(handle: number, code: number, signal: string);
$registerDebugConfigurationProvider(type: string, hasProvideMethod: boolean, hasResolveMethod: boolean, hasDebugAdapterExecutable: boolean, handle: number): TPromise<any>;
$unregisterDebugConfigurationProvider(handle: number): TPromise<any>;
$startDebugging(folder: UriComponents | undefined, nameOrConfig: string | vscode.DebugConfiguration): TPromise<boolean>;
Expand Down Expand Up @@ -783,6 +786,9 @@ export interface ISourceMultiBreakpointDto {
}

export interface ExtHostDebugServiceShape {
$startDASession(handle: number, debugType: string, adapterExecutableInfo: IAdapterExecutable | null): TPromise<void>;
$stopDASession(handle: number): TPromise<void>;
$sendDAMessage(handle: number, message: DebugProtocol.ProtocolMessage): TPromise<void>;
$resolveDebugConfiguration(handle: number, folder: UriComponents | undefined, debugConfiguration: IConfig): TPromise<IConfig>;
$provideDebugConfigurations(handle: number, folder: UriComponents | undefined): TPromise<IConfig[]>;
$debugAdapterExecutable(handle: number, folder: UriComponents | undefined): TPromise<IAdapterExecutable>;
Expand Down
57 changes: 52 additions & 5 deletions src/vs/workbench/api/node/extHostDebugService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ import {
IMainContext, IBreakpointsDeltaDto, ISourceMultiBreakpointDto, IFunctionBreakpointDto
} from 'vs/workbench/api/node/extHost.protocol';
import { ExtHostWorkspace } from 'vs/workbench/api/node/extHostWorkspace';

import * as vscode from 'vscode';
import URI, { UriComponents } from 'vs/base/common/uri';
import { Disposable, Position, Location, SourceBreakpoint, FunctionBreakpoint } from 'vs/workbench/api/node/extHostTypes';
import { generateUuid } from 'vs/base/common/uuid';
import { DebugAdapter } from 'vs/workbench/parts/debug/node/v8Protocol';
import { convertToVSCPaths, convertToDAPaths } from 'vs/workbench/parts/debug/node/DapPathConverter';
import * as paths from 'vs/base/common/paths';
import { ExtHostExtensionService } from 'vs/workbench/api/node/extHostExtensionService';
import { IAdapterExecutable } from 'vs/workbench/parts/debug/common/debug';


export class ExtHostDebugService implements ExtHostDebugServiceShape {

private _workspace: ExtHostWorkspace;

private _handleCounter: number;
private _handlers: Map<number, vscode.DebugConfigurationProvider>;

Expand Down Expand Up @@ -52,10 +54,10 @@ export class ExtHostDebugService implements ExtHostDebugServiceShape {

private readonly _onDidChangeBreakpoints: Emitter<vscode.BreakpointsChangeEvent>;

private _debugAdapters: Map<number, DebugAdapter>;

constructor(mainContext: IMainContext, workspace: ExtHostWorkspace) {

this._workspace = workspace;
constructor(mainContext: IMainContext, private _workspace: ExtHostWorkspace, private _extensionService: ExtHostExtensionService) {

this._handleCounter = 0;
this._handlers = new Map<number, vscode.DebugConfigurationProvider>();
Expand All @@ -77,6 +79,51 @@ export class ExtHostDebugService implements ExtHostDebugServiceShape {

this._breakpoints = new Map<string, vscode.Breakpoint>();
this._breakpointEventsActive = false;

this._debugAdapters = new Map<number, DebugAdapter>();
}

public $startDASession(handle: number, debugType: string, adpaterExecutable: IAdapterExecutable | null): TPromise<void> {
const mythis = this;

const da = new class extends DebugAdapter {

// DA -> VS Code
public acceptMessage(message: DebugProtocol.ProtocolMessage) {
convertToVSCPaths(message, source => {
if (paths.isAbsolute(source.path)) {
(<any>source).path = URI.file(source.path);
}
});
mythis._debugServiceProxy.$acceptDAMessage(handle, message);
}

}(debugType, adpaterExecutable, this._extensionService.getAllExtensionDescriptions());

this._debugAdapters.set(handle, da);
da.onError(err => this._debugServiceProxy.$acceptDAError(handle, err.name, err.message, err.stack));
da.onExit(code => this._debugServiceProxy.$acceptDAExit(handle, code, null));
return da.startSession();
}

public $sendDAMessage(handle: number, message: DebugProtocol.ProtocolMessage): TPromise<void> {
// VS Code -> DA
convertToDAPaths(message, source => {
if (typeof source.path === 'object') {
source.path = URI.revive(source.path).fsPath;
}
});
const da = this._debugAdapters.get(handle);
if (da) {
da.sendMessage(message);
}
return void 0;
}

public $stopDASession(handle: number): TPromise<void> {
const da = this._debugAdapters.get(handle);
this._debugAdapters.delete(handle);
return da ? da.stopSession() : void 0;
}

private startBreakpoints() {
Expand Down
21 changes: 21 additions & 0 deletions src/vs/workbench/parts/debug/common/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Range, IRange } from 'vs/editor/common/core/range';
import { RawContextKey, ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
import { IWorkspaceFolder } from 'vs/platform/workspace/common/workspace';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IDisposable } from 'vs/base/common/lifecycle';

export const VIEWLET_ID = 'workbench.view.debug';
export const VARIABLES_VIEW_ID = 'workbench.debug.variablesView';
Expand Down Expand Up @@ -347,6 +348,7 @@ export interface IDebugConfiguration {
hideActionBar: boolean;
showInStatusBar: 'never' | 'always' | 'onFirstSessionStart';
internalConsoleOptions: 'neverOpen' | 'openOnSessionStart' | 'openOnFirstSessionStart';
extensionHostDebugAdapter: boolean;
}

export interface IGlobalConfig {
Expand Down Expand Up @@ -380,6 +382,22 @@ export interface ICompound {
configurations: (string | { name: string, folder: string })[];
}

export interface IDebugAdapter extends IDisposable {
readonly onError: Event<Error>;
readonly onExit: Event<number>;
onRequest(callback: (request: DebugProtocol.Request) => void);
onEvent(callback: (event: DebugProtocol.Event) => void);
startSession(): TPromise<void>;
sendMessage(message: DebugProtocol.ProtocolMessage): void;
sendResponse(response: DebugProtocol.Response): void;
sendRequest(command: string, args: any, clb: (result: DebugProtocol.Response) => void): void;
stopSession(): TPromise<void>;
}

export interface IDebugAdapterProvider {
createDebugAdapter(debugType: string, adapterInfo: IAdapterExecutable | null): IDebugAdapter;
}

export interface IAdapterExecutable {
command?: string;
args?: string[];
Expand Down Expand Up @@ -448,6 +466,9 @@ export interface IConfigurationManager {

resolveConfigurationByProviders(folderUri: uri | undefined, type: string | undefined, debugConfiguration: any): TPromise<any>;
debugAdapterExecutable(folderUri: uri | undefined, type: string): TPromise<IAdapterExecutable | undefined>;

registerDebugAdapterProvider(debugType: string, debugAdapterLauncher: IDebugAdapterProvider);
createDebugAdapter(debugType: string, adapterExecutable: IAdapterExecutable | null): IDebugAdapter;
}

export interface ILaunch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ configurationRegistry.registerConfiguration({
description: nls.localize({ comment: ['This is the description for a setting'], key: 'launch' }, "Global debug launch configuration. Should be used as an alternative to 'launch.json' that is shared across workspaces"),
default: { configurations: [], compounds: [] },
$ref: launchSchemaId
},
'debug.extensionHostDebugAdapter': {
type: 'boolean',
description: nls.localize({ comment: ['This is the description for a setting'], key: 'extensionHostDebugAdapter' }, "Run debug adapter in extension host"),
default: false
}
}
});
Expand Down
Loading

0 comments on commit 4cd09ff

Please sign in to comment.