debug.ts 12.7 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import uri from 'vs/base/common/uri';
I
isidor 已提交
7
import { TPromise } from 'vs/base/common/winjs.base';
8
import { IActionRunner } from 'vs/base/common/actions';
9
import Event from 'vs/base/common/event';
E
Erich Gamma 已提交
10
import severity from 'vs/base/common/severity';
I
isidor 已提交
11
import { IViewletView } from 'vs/workbench/browser/viewlet';
E
Erich Gamma 已提交
12 13
import { createDecorator, ServiceIdentifier } from 'vs/platform/instantiation/common/instantiation';
import editor = require('vs/editor/common/editorCommon');
I
isidor 已提交
14
import { Source } from 'vs/workbench/parts/debug/common/debugSource';
15
import { Range } from 'vs/editor/common/core/range';
E
Erich Gamma 已提交
16

I
isidor 已提交
17 18 19 20
export const VIEWLET_ID = 'workbench.view.debug';
export const REPL_ID = 'workbench.panel.repl';
export const DEBUG_SERVICE_ID = 'debugService';
export const CONTEXT_IN_DEBUG_MODE = 'inDebugMode';
I
isidor 已提交
21
export const EDITOR_CONTRIBUTION_ID = 'editor.contrib.debug';
E
Erich Gamma 已提交
22

I
isidor 已提交
23
// raw
E
Erich Gamma 已提交
24 25 26 27 28

export interface IRawModelUpdate {
	threadId: number;
	thread?: DebugProtocol.Thread;
	callStack?: DebugProtocol.StackFrame[];
I
isidor 已提交
29
	stoppedDetails?: IRawStoppedDetails;
30
	allThreadsStopped?: boolean;
I
isidor 已提交
31 32 33 34 35 36
}

export interface IRawStoppedDetails {
	reason: string;
	threadId?: number;
	text?: string;
I
isidor 已提交
37
	totalFrames?: number;
38
	framesErrorMessage?: string;
E
Erich Gamma 已提交
39 40
}

I
isidor 已提交
41
// model
E
Erich Gamma 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54

export interface ITreeElement {
	getId(): string;
}

export interface IExpressionContainer extends ITreeElement {
	reference: number;
	getChildren(debugService: IDebugService): TPromise<IExpression[]>;
}

export interface IExpression extends ITreeElement, IExpressionContainer {
	name: string;
	value: string;
55
	valueChanged: boolean;
E
Erich Gamma 已提交
56 57 58
}

export interface IThread extends ITreeElement {
I
isidor 已提交
59 60 61
	/**
	 * Id of the thread generated by the debug adapter backend.
	 */
E
Erich Gamma 已提交
62
	threadId: number;
I
isidor 已提交
63 64 65 66

	/**
	 * Name of the thread.
	 */
E
Erich Gamma 已提交
67
	name: string;
I
isidor 已提交
68 69 70 71

	/**
	 * Information about the current thread stop event. Null if thread is not stopped.
	 */
I
isidor 已提交
72
	stoppedDetails: IRawStoppedDetails;
73 74 75 76 77

	/**
	 * Queries the debug adapter for the callstack and returns a promise with
	 * the stack frames of the callstack.
	 * If the thread is not stopped, it returns a promise to an empty array.
I
isidor 已提交
78 79
	 * Only gets the first 20 stack frames. Calling this method consecutive times
	 * with getAdditionalStackFrames = true gets the remainder of the call stack.
80
	 */
I
isidor 已提交
81
	getCallStack(debugService: IDebugService, getAdditionalStackFrames?: boolean): TPromise<IStackFrame[]>;
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

	/**
	 * Gets the callstack if it has already been received from the debug
	 * adapter, otherwise it returns undefined.
	 */
	getCachedCallStack(): IStackFrame[];

	/**
	 * Invalidates the callstack cache
	 */
	clearCallStack(): void;

	/**
	 * Indicates whether this thread is stopped. The callstack for stopped
	 * threads can be retrieved from the debug adapter.
	 */
	stopped: boolean;
E
Erich Gamma 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
}

export interface IScope extends IExpressionContainer {
	name: string;
	expensive: boolean;
}

export interface IStackFrame extends ITreeElement {
	threadId: number;
	name: string;
	lineNumber: number;
	column: number;
	frameId: number;
	source: Source;
	getScopes(debugService: IDebugService): TPromise<IScope[]>;
}

export interface IEnablement extends ITreeElement {
	enabled: boolean;
}

120 121 122
export interface IRawBreakpoint {
	uri: uri;
	lineNumber: number;
123
	enabled?: boolean;
124 125 126
	condition?: string;
}

E
Erich Gamma 已提交
127 128 129 130
export interface IBreakpoint extends IEnablement {
	source: Source;
	lineNumber: number;
	desiredLineNumber: number;
I
isidor 已提交
131
	condition: string;
132
	verified: boolean;
133
	idFromAdapter: number;
I
isidor 已提交
134
	message: string;
E
Erich Gamma 已提交
135 136
}

I
isidor 已提交
137
export interface IFunctionBreakpoint extends IEnablement {
138
	name: string;
I
isidor 已提交
139
	verified: boolean;
140
	idFromAdapter: number;
I
isidor 已提交
141 142
}

E
Erich Gamma 已提交
143
export interface IExceptionBreakpoint extends IEnablement {
144 145
	filter: string;
	label: string;
E
Erich Gamma 已提交
146 147
}

I
isidor 已提交
148
// model interfaces
E
Erich Gamma 已提交
149

150
export interface IViewModel extends ITreeElement {
E
Erich Gamma 已提交
151 152 153 154
	getFocusedStackFrame(): IStackFrame;
	getSelectedExpression(): IExpression;
	getFocusedThreadId(): number;
	setSelectedExpression(expression: IExpression);
I
isidor 已提交
155 156
	getSelectedFunctionBreakpoint(): IFunctionBreakpoint;
	setSelectedFunctionBreakpoint(functionBreakpoint: IFunctionBreakpoint): void;
157 158 159 160

	onDidFocusStackFrame: Event<IStackFrame>;
	onDidSelectExpression: Event<IExpression>;
	onDidSelectFunctionBreakpoint: Event<IFunctionBreakpoint>;
E
Erich Gamma 已提交
161 162
}

163
export interface IModel extends ITreeElement {
164
	getThreads(): { [threadId: number]: IThread; };
E
Erich Gamma 已提交
165 166
	getBreakpoints(): IBreakpoint[];
	areBreakpointsActivated(): boolean;
I
isidor 已提交
167
	getFunctionBreakpoints(): IFunctionBreakpoint[];
E
Erich Gamma 已提交
168 169 170
	getExceptionBreakpoints(): IExceptionBreakpoint[];
	getWatchExpressions(): IExpression[];
	getReplElements(): ITreeElement[];
171 172 173 174

	onDidChangeBreakpoints: Event<void>;
	onDidChangeCallStack: Event<void>;
	onDidChangeWatchExpressions: Event<IExpression>;
175
	onDidChangeReplElements: Event<void>;
176
};
E
Erich Gamma 已提交
177

I
isidor 已提交
178
// service enums
E
Erich Gamma 已提交
179 180 181 182 183 184

export enum State {
	Disabled,
	Inactive,
	Initializing,
	Stopped,
I
isidor 已提交
185 186
	Running,
	RunningNoDebug
E
Erich Gamma 已提交
187 188
}

I
isidor 已提交
189
// service interfaces
E
Erich Gamma 已提交
190 191 192

export interface IGlobalConfig {
	version: string;
193
	debugServer?: number;
E
Erich Gamma 已提交
194 195 196
	configurations: IConfig[];
}

197
export interface IEnvConfig {
198
	name?: string;
E
Erich Gamma 已提交
199 200
	type: string;
	request: string;
201 202 203 204 205 206 207 208 209 210
	program?: string;
	stopOnEntry?: boolean;
	args?: string[];
	cwd?: string;
	runtimeExecutable?: string;
	runtimeArgs?: string[];
	env?: { [key: string]: string; };
	sourceMaps?: boolean;
	outDir?: string;
	address?: string;
211
	internalConsoleOptions?: string;
212 213 214 215
	port?: number;
	preLaunchTask?: string;
	externalConsole?: boolean;
	debugServer?: number;
I
isidor 已提交
216
	noDebug?: boolean;
217
	silentlyAbort?: boolean;
E
Erich Gamma 已提交
218 219
}

220 221 222 223 224 225
export interface IConfig extends IEnvConfig {
	windows?: IEnvConfig;
	osx?: IEnvConfig;
	linux?: IEnvConfig;
}

E
Erich Gamma 已提交
226
export interface IRawEnvAdapter {
I
isidor 已提交
227 228 229 230 231 232
	type?: string;
	label?: string;
	program?: string;
	args?: string[];
	runtime?: string;
	runtimeArgs?: string[];
E
Erich Gamma 已提交
233 234 235
}

export interface IRawAdapter extends IRawEnvAdapter {
I
isidor 已提交
236 237 238
	enableBreakpointsFor?: { languageIds: string[] };
	configurationAttributes?: any;
	initialConfigurations?: any[];
239
	variables: { [key: string]: string };
I
isidor 已提交
240
	aiKey?: string;
I
isidor 已提交
241
	win?: IRawEnvAdapter;
I
isidor 已提交
242
	winx86?: IRawEnvAdapter;
243
	windows?: IRawEnvAdapter;
I
isidor 已提交
244 245
	osx?: IRawEnvAdapter;
	linux?: IRawEnvAdapter;
E
Erich Gamma 已提交
246 247
}

248
export interface IRawDebugSession {
I
isidor 已提交
249 250
	configuration: { type: string, isAttach: boolean, capabilities: DebugProtocol.Capabilites };

251
	disconnect(restart?: boolean, force?: boolean): TPromise<DebugProtocol.DisconnectResponse>;
E
Erich Gamma 已提交
252

253
	stackTrace(args: DebugProtocol.StackTraceArguments): TPromise<DebugProtocol.StackTraceResponse>;
E
Erich Gamma 已提交
254
	scopes(args: DebugProtocol.ScopesArguments): TPromise<DebugProtocol.ScopesResponse>;
255
	variables(args: DebugProtocol.VariablesArguments): TPromise<DebugProtocol.VariablesResponse>;
E
Erich Gamma 已提交
256
	evaluate(args: DebugProtocol.EvaluateArguments): TPromise<DebugProtocol.EvaluateResponse>;
257

258 259
	custom(request: string, args: any): TPromise<DebugProtocol.Response>;

260 261 262 263
	/**
	 * Allows to register on each debug session stop event.
	 */
	onDidStop: Event<DebugProtocol.StoppedEvent>;
264 265

	onDidEvent: Event<DebugProtocol.Event>;
E
Erich Gamma 已提交
266 267
}

268 269 270 271 272 273 274 275 276 277 278 279 280
export interface IConfigurationManager {
	configurationName: string;
	setConfiguration(name: string): TPromise<void>;
	openConfigFile(sideBySide: boolean): TPromise<boolean>;
	loadLaunchConfig(): TPromise<IGlobalConfig>;
	canSetBreakpointsIn(model: editor.IModel): boolean;

	/**
	 * Allows to register on change of debug configuration.
	 */
	onDidConfigurationChange: Event<string>;
}

E
Erich Gamma 已提交
281 282
export var IDebugService = createDecorator<IDebugService>(DEBUG_SERVICE_ID);

283
export interface IDebugService {
E
Erich Gamma 已提交
284
	serviceId: ServiceIdentifier<any>;
285 286 287 288

	/**
	 * Gets the current debug state.
	 */
I
isidor 已提交
289
	state: State;
E
Erich Gamma 已提交
290

291 292 293 294 295
	/**
	 * Allows to register on debug state changes.
	 */
	onDidChangeState: Event<State>;

296 297 298 299
	/**
	 * Gets the current configuration manager.
	 */
	getConfigurationManager(): IConfigurationManager;
E
Erich Gamma 已提交
300

I
isidor 已提交
301 302 303 304
	/**
	 * Sets the focused stack frame and evaluates all expresions against the newly focused stack frame,
	 */
	setFocusedStackFrameAndEvaluate(focusedStackFrame: IStackFrame): TPromise<void>;
E
Erich Gamma 已提交
305

306
	/**
307
	 * Adds new breakpoints to the model. Notifies debug adapter of breakpoint changes.
308
	 */
309
	addBreakpoints(rawBreakpoints: IRawBreakpoint[]): TPromise<void[]>;
310 311 312 313 314 315 316 317 318 319 320

	/**
	 * Enables or disables all breakpoints. If breakpoint is passed only enables or disables the passed breakpoint.
	 * Notifies debug adapter of breakpoint changes.
	 */
	enableOrDisableBreakpoints(enable: boolean, breakpoint?: IEnablement): TPromise<void>;

	/**
	 * Sets the global activated property for all breakpoints.
	 * Notifies debug adapter of breakpoint changes.
	 */
321
	setBreakpointsActivated(activated: boolean): TPromise<void>;
322 323 324 325 326

	/**
	 * Removes all breakpoints. If id is passed only removes the breakpoint associated with that id.
	 * Notifies debug adapter of breakpoint changes.
	 */
327
	removeBreakpoints(id?: string): TPromise<any>;
I
isidor 已提交
328

329
	/**
330
	 * Adds a new no name function breakpoint. The function breakpoint should be renamed once user enters the name.
331
	 */
I
isidor 已提交
332
	addFunctionBreakpoint(): void;
333 334 335 336 337

	/**
	 * Renames an already existing function breakpoint.
	 * Notifies debug adapter of breakpoint changes.
	 */
I
isidor 已提交
338
	renameFunctionBreakpoint(id: string, newFunctionName: string): TPromise<void>;
339 340 341 342 343

	/**
	 * Removes all function breakpoints. If id is passed only removes the function breakpoint with the passed id.
	 * Notifies debug adapter of breakpoint changes.
	 */
I
isidor 已提交
344
	removeFunctionBreakpoints(id?: string): TPromise<void>;
E
Erich Gamma 已提交
345

346
	/**
347
	 * Adds a new expression to the repl.
348
	 */
I
isidor 已提交
349
	addReplExpression(name: string): TPromise<void>;
350 351 352 353

	/**
	 * Removes all repl expressions.
	 */
354
	removeReplExpressions(): void;
355 356 357 358

	/**
	 * Adds a new log to the repl. Either a string value or a dictionary (used to inspect complex objects printed to the repl).
	 */
359
	logToRepl(value: string | { [key: string]: any }, severity?: severity): void;
360 361 362 363

	/**
	 * Appends new output to the repl.
	 */
E
Erich Gamma 已提交
364 365
	appendReplOutput(value: string, severity?: severity): void;

366
	/**
367
	 * Adds a new watch expression and evaluates it against the debug adapter.
368
	 */
I
isidor 已提交
369
	addWatchExpression(name?: string): TPromise<void>;
370 371 372 373

	/**
	 * Renames a watch expression and evaluates it against the debug adapter.
	 */
I
isidor 已提交
374
	renameWatchExpression(id: string, newName: string): TPromise<void>;
375 376 377 378

	/**
	 * Removes all watch expressions. If id is passed only removes the watch expression with the passed id.
	 */
379
	removeWatchExpressions(id?: string): void;
E
Erich Gamma 已提交
380

I
isidor 已提交
381 382 383
	/**
	 * Creates a new debug session. Depending on the configuration will either 'launch' or 'attach'.
	 */
384
	createSession(noDebug: boolean, configuration?: IConfig): TPromise<any>;
I
isidor 已提交
385 386 387 388

	/**
	 * Restarts an active debug session or creates a new one if there is no active session.
	 */
I
isidor 已提交
389
	restartSession(): TPromise<any>;
I
isidor 已提交
390 391 392 393

	/**
	 * Returns the active debug session or null if debug is inactive.
	 */
E
Erich Gamma 已提交
394 395
	getActiveSession(): IRawDebugSession;

I
isidor 已提交
396 397 398
	/**
	 * Gets the current debug model.
	 */
E
Erich Gamma 已提交
399
	getModel(): IModel;
I
isidor 已提交
400 401 402 403

	/**
	 * Gets the current view model.
	 */
E
Erich Gamma 已提交
404 405
	getViewModel(): IViewModel;

I
isidor 已提交
406 407 408
	/**
	 * Opens a new or reveals an already visible editor showing the source.
	 */
409
	openOrRevealSource(source: Source, lineNumber: number, preserveFocus: boolean, sideBySide: boolean): TPromise<any>;
410 411 412 413 414 415

	next(threadId: number): TPromise<void>;
	stepIn(threadId: number): TPromise<void>;
	stepOut(threadId: number): TPromise<void>;
	continue(threadId: number): TPromise<void>;
	pause(threadId: number): TPromise<any>;
E
Erich Gamma 已提交
416 417
}

I
isidor 已提交
418 419
// Editor interfaces
export interface IDebugEditorContribution extends editor.IEditorContribution {
420
	showHover(range: Range, hoveringOver: string, focus: boolean): TPromise<void>;
I
isidor 已提交
421 422
}

423
// Debug view registration
424

425
export interface IDebugViewConstructorSignature {
I
isidor 已提交
426
	new (actionRunner: IActionRunner, viewletSetings: any, ...services: { serviceId: ServiceIdentifier<any>; }[]): IViewletView;
427 428 429
}

export interface IDebugViewRegistry {
430 431
	registerDebugView(view: IDebugViewConstructorSignature, order: number): void;
	getDebugViews(): IDebugViewConstructorSignature[];
432 433 434
}

class DebugViewRegistryImpl implements IDebugViewRegistry {
435
	private debugViews: { view: IDebugViewConstructorSignature, order: number }[];
436 437 438 439 440

	constructor() {
		this.debugViews = [];
	}

441 442
	public registerDebugView(view: IDebugViewConstructorSignature, order: number): void {
		this.debugViews.push({ view, order });
443 444
	}

445 446 447
	public getDebugViews(): IDebugViewConstructorSignature[] {
		return this.debugViews.sort((first, second) => first.order - second.order)
			.map(viewWithOrder => viewWithOrder.view);
448 449 450 451 452
	}
}

export var DebugViewRegistry = <IDebugViewRegistry>new DebugViewRegistryImpl();

I
isidor 已提交
453
// utils
E
Erich Gamma 已提交
454

I
isidor 已提交
455
const _formatPIIRegexp = /{([^}]+)}/g;
E
Erich Gamma 已提交
456

I
isidor 已提交
457 458
export function formatPII(value: string, excludePII: boolean, args: { [key: string]: string }): string {
	return value.replace(_formatPIIRegexp, function (match, group) {
E
Erich Gamma 已提交
459 460 461 462
		if (excludePII && group.length > 0 && group[0] !== '_') {
			return match;
		}

I
isidor 已提交
463
		return args && args.hasOwnProperty(group) ?
E
Erich Gamma 已提交
464 465
			args[group] :
			match;
I
isidor 已提交
466
	});
E
Erich Gamma 已提交
467
}