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

I
isidor 已提交
6 7
import * as nls from 'vs/nls';
import * as lifecycle from 'vs/base/common/lifecycle';
J
Johannes Rieken 已提交
8 9
import { guessMimeTypes } from 'vs/base/common/mime';
import Event, { Emitter } from 'vs/base/common/event';
10
import * as paths from 'vs/base/common/paths';
I
isidor 已提交
11
import { generateUuid } from 'vs/base/common/uuid';
E
Erich Gamma 已提交
12
import uri from 'vs/base/common/uri';
J
Johannes Rieken 已提交
13
import { Action } from 'vs/base/common/actions';
I
isidor 已提交
14 15 16
import { first, distinct } from 'vs/base/common/arrays';
import { isObject, isUndefinedOrNull } from 'vs/base/common/types';
import * as errors from 'vs/base/common/errors';
E
Erich Gamma 已提交
17
import severity from 'vs/base/common/severity';
J
Johannes Rieken 已提交
18
import { TPromise } from 'vs/base/common/winjs.base';
I
isidor 已提交
19
import * as aria from 'vs/base/browser/ui/aria/aria';
20
import { Client as TelemetryClient } from 'vs/base/parts/ipc/node/ipc.cp';
I
isidor 已提交
21
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
J
Johannes Rieken 已提交
22 23 24 25 26 27 28 29
import { IContextKeyService, IContextKey } from 'vs/platform/contextkey/common/contextkey';
import { IMarkerService } from 'vs/platform/markers/common/markers';
import { ILifecycleService } from 'vs/platform/lifecycle/common/lifecycle';
import { IExtensionService } from 'vs/platform/extensions/common/extensions';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IFileService, FileChangesEvent, FileChangeType, EventType } from 'vs/platform/files/common/files';
import { IEventService } from 'vs/platform/event/common/event';
import { IMessageService, CloseAction } from 'vs/platform/message/common/message';
30
import { IWindowsService } from 'vs/platform/windows/common/windows';
J
Johannes Rieken 已提交
31 32 33 34 35 36
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { TelemetryService } from 'vs/platform/telemetry/common/telemetryService';
import { TelemetryAppenderClient } from 'vs/platform/telemetry/common/telemetryIpc';
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
import { IEditorGroupService } from 'vs/workbench/services/group/common/groupService';
import { asFileEditorInput } from 'vs/workbench/common/editor';
I
isidor 已提交
37
import * as debug from 'vs/workbench/parts/debug/common/debug';
J
Johannes Rieken 已提交
38
import { RawDebugSession } from 'vs/workbench/parts/debug/electron-browser/rawDebugSession';
I
isidor 已提交
39
import { Model, ExceptionBreakpoint, FunctionBreakpoint, Breakpoint, Expression } from 'vs/workbench/parts/debug/common/debugModel';
J
Johannes Rieken 已提交
40
import { DebugStringEditorInput, DebugErrorEditorInput } from 'vs/workbench/parts/debug/browser/debugEditorInputs';
I
isidor 已提交
41 42
import { ViewModel } from 'vs/workbench/parts/debug/common/debugViewModel';
import * as debugactions from 'vs/workbench/parts/debug/browser/debugActions';
J
Johannes Rieken 已提交
43 44 45 46
import { ConfigurationManager } from 'vs/workbench/parts/debug/node/debugConfigurationManager';
import { Source } from 'vs/workbench/parts/debug/common/debugSource';
import { ITaskService, TaskEvent, TaskType, TaskServiceEvents, ITaskSummary } from 'vs/workbench/parts/tasks/common/taskService';
import { TaskError, TaskErrors } from 'vs/workbench/parts/tasks/common/taskSystem';
I
isidor 已提交
47
import { VIEWLET_ID as EXPLORER_VIEWLET_ID } from 'vs/workbench/parts/files/common/files';
J
Johannes Rieken 已提交
48 49 50 51 52 53 54
import { IViewletService } from 'vs/workbench/services/viewlet/common/viewletService';
import { IPanelService } from 'vs/workbench/services/panel/common/panelService';
import { IPartService } from 'vs/workbench/services/part/common/partService';
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
J
Joao Moreno 已提交
55
import { IWindowIPCService, IBroadcast } from 'vs/workbench/services/window/electron-browser/windowService';
S
Sandeep Somavarapu 已提交
56
import { ILogEntry, EXTENSION_LOG_BROADCAST_CHANNEL, EXTENSION_ATTACH_BROADCAST_CHANNEL, EXTENSION_TERMINATE_BROADCAST_CHANNEL } from 'vs/workbench/electron-browser/extensionHost';
E
Erich Gamma 已提交
57

I
isidor 已提交
58 59 60 61 62 63
const DEBUG_BREAKPOINTS_KEY = 'debug.breakpoint';
const DEBUG_BREAKPOINTS_ACTIVATED_KEY = 'debug.breakpointactivated';
const DEBUG_FUNCTION_BREAKPOINTS_KEY = 'debug.functionbreakpoint';
const DEBUG_EXCEPTION_BREAKPOINTS_KEY = 'debug.exceptionbreakpoint';
const DEBUG_WATCH_EXPRESSIONS_KEY = 'debug.watchexpressions';
const DEBUG_SELECTED_CONFIG_NAME_KEY = 'debug.selectedconfigname';
E
Erich Gamma 已提交
64

65
export class DebugService implements debug.IDebugService {
66
	public _serviceBrand: any;
E
Erich Gamma 已提交
67

I
isidor 已提交
68
	private sessionStates: { [id: string]: debug.State };
I
isidor 已提交
69
	private _onDidChangeState: Emitter<void>;
I
isidor 已提交
70 71
	private model: Model;
	private viewModel: ViewModel;
72
	private configurationManager: ConfigurationManager;
73
	private customTelemetryService: ITelemetryService;
E
Erich Gamma 已提交
74
	private lastTaskEvent: TaskEvent;
75
	private toDispose: lifecycle.IDisposable[];
76
	private toDisposeOnSessionEnd: { [id: string]: lifecycle.IDisposable[] };
A
Alex Dima 已提交
77
	private inDebugMode: IContextKey<boolean>;
78
	private breakpointsToSendOnResourceSaved: { [uri: string]: boolean };
E
Erich Gamma 已提交
79 80 81 82

	constructor(
		@IStorageService private storageService: IStorageService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
83 84
		@ITextFileService private textFileService: ITextFileService,
		@IViewletService private viewletService: IViewletService,
I
isidor 已提交
85
		@IPanelService private panelService: IPanelService,
E
Erich Gamma 已提交
86 87 88
		@IFileService private fileService: IFileService,
		@IMessageService private messageService: IMessageService,
		@IPartService private partService: IPartService,
89
		@IWindowsService private windowsService: IWindowsService,
J
Joao Moreno 已提交
90
		@IWindowIPCService private windowService: IWindowIPCService,
E
Erich Gamma 已提交
91 92
		@ITelemetryService private telemetryService: ITelemetryService,
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
93
		@IContextKeyService contextKeyService: IContextKeyService,
94
		@IEditorGroupService private editorGroupService: IEditorGroupService,
95
		@IEventService eventService: IEventService,
I
isidor 已提交
96
		@ILifecycleService lifecycleService: ILifecycleService,
I
isidor 已提交
97
		@IInstantiationService private instantiationService: IInstantiationService,
A
Alex Dima 已提交
98
		@IExtensionService private extensionService: IExtensionService,
99
		@IMarkerService private markerService: IMarkerService,
100 101
		@ITaskService private taskService: ITaskService,
		@IConfigurationService private configurationService: IConfigurationService
E
Erich Gamma 已提交
102 103
	) {
		this.toDispose = [];
104
		this.toDisposeOnSessionEnd = {};
105
		this.breakpointsToSendOnResourceSaved = {};
I
isidor 已提交
106
		this._onDidChangeState = new Emitter<void>();
I
isidor 已提交
107
		this.sessionStates = {};
E
Erich Gamma 已提交
108

109
		this.configurationManager = this.instantiationService.createInstance(ConfigurationManager);
110
		this.inDebugMode = debug.CONTEXT_IN_DEBUG_MODE.bindTo(contextKeyService);
E
Erich Gamma 已提交
111

I
isidor 已提交
112
		this.model = new Model(this.loadBreakpoints(), this.storageService.getBoolean(DEBUG_BREAKPOINTS_ACTIVATED_KEY, StorageScope.WORKSPACE, true), this.loadFunctionBreakpoints(),
E
Erich Gamma 已提交
113
			this.loadExceptionBreakpoints(), this.loadWatchExpressions());
I
isidor 已提交
114
		this.toDispose.push(this.model);
I
isidor 已提交
115
		this.viewModel = new ViewModel(this.storageService.get(DEBUG_SELECTED_CONFIG_NAME_KEY, StorageScope.WORKSPACE, null));
116

E
Erich Gamma 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
		this.registerListeners(eventService, lifecycleService);
	}

	private registerListeners(eventService: IEventService, lifecycleService: ILifecycleService): void {
		this.toDispose.push(eventService.addListener2(EventType.FILE_CHANGES, (e: FileChangesEvent) => this.onFileChanges(e)));

		if (this.taskService) {
			this.toDispose.push(this.taskService.addListener2(TaskServiceEvents.Active, (e: TaskEvent) => {
				this.lastTaskEvent = e;
			}));
			this.toDispose.push(this.taskService.addListener2(TaskServiceEvents.Inactive, (e: TaskEvent) => {
				if (e.type === TaskType.SingleRun) {
					this.lastTaskEvent = null;
				}
			}));
			this.toDispose.push(this.taskService.addListener2(TaskServiceEvents.Terminated, (e: TaskEvent) => {
				this.lastTaskEvent = null;
			}));
		}

137 138
		lifecycleService.onShutdown(this.store, this);
		lifecycleService.onShutdown(this.dispose, this);
139

I
isidor 已提交
140
		this.toDispose.push(this.windowService.onBroadcast(this.onBroadcast, this));
141 142 143
	}

	private onBroadcast(broadcast: IBroadcast): void {
144

I
isidor 已提交
145
		// attach: PH is ready to be attached to
146 147 148 149
		// TODO@Isidor this is a hack to just get any 'extensionHost' session.
		// Optimally the broadcast would contain the id of the session
		// We are only intersted if we have an active debug session for extensionHost
		const session = <RawDebugSession>this.model.getProcesses().map(p => p.session).filter(s => s.configuration.type === 'extensionHost').pop();
150
		if (broadcast.channel === EXTENSION_ATTACH_BROADCAST_CHANNEL) {
151
			this.rawAttach(session, broadcast.payload.port);
152 153
			return;
		}
154

155
		if (broadcast.channel === EXTENSION_TERMINATE_BROADCAST_CHANNEL) {
156
			this.onSessionEnd(session);
157 158 159
			return;
		}

I
isidor 已提交
160
		// from this point on we require an active session
161 162
		if (!session) {
			return;
163 164
		}

I
isidor 已提交
165
		// a plugin logged output, show it inside the REPL
166
		if (broadcast.channel === EXTENSION_LOG_BROADCAST_CHANNEL) {
167 168 169 170 171 172 173 174 175 176 177
			let extensionOutput: ILogEntry = broadcast.payload;
			let sev = extensionOutput.severity === 'warn' ? severity.Warning : extensionOutput.severity === 'error' ? severity.Error : severity.Info;

			let args: any[] = [];
			try {
				let parsed = JSON.parse(extensionOutput.arguments);
				args.push(...Object.getOwnPropertyNames(parsed).map(o => parsed[o]));
			} catch (error) {
				args.push(extensionOutput.arguments);
			}

I
isidor 已提交
178
			// add output for each argument logged
179 180 181 182
			let simpleVals: any[] = [];
			for (let i = 0; i < args.length; i++) {
				let a = args[i];

I
isidor 已提交
183
				// undefined gets printed as 'undefined'
184 185 186 187
				if (typeof a === 'undefined') {
					simpleVals.push('undefined');
				}

I
isidor 已提交
188
				// null gets printed as 'null'
189 190 191 192
				else if (a === null) {
					simpleVals.push('null');
				}

I
isidor 已提交
193
				// objects & arrays are special because we want to inspect them in the REPL
I
isidor 已提交
194
				else if (isObject(a) || Array.isArray(a)) {
195

I
isidor 已提交
196
					// flush any existing simple values logged
197 198 199 200 201
					if (simpleVals.length) {
						this.logToRepl(simpleVals.join(' '), sev);
						simpleVals = [];
					}

I
isidor 已提交
202
					// show object
203 204 205
					this.logToRepl(a, sev);
				}

I
isidor 已提交
206 207
				// string: watch out for % replacement directive
				// string substitution and formatting @ https://developer.chrome.com/devtools/docs/console
208 209 210 211 212 213
				else if (typeof a === 'string') {
					let buf = '';

					for (let j = 0, len = a.length; j < len; j++) {
						if (a[j] === '%' && (a[j + 1] === 's' || a[j + 1] === 'i' || a[j + 1] === 'd')) {
							i++; // read over substitution
I
isidor 已提交
214
							buf += !isUndefinedOrNull(args[i]) ? args[i] : ''; // replace
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
							j++; // read over directive
						} else {
							buf += a[j];
						}
					}

					simpleVals.push(buf);
				}

				// number or boolean is joined together
				else {
					simpleVals.push(a);
				}
			}

I
isidor 已提交
230
			// flush simple values
231 232 233 234
			if (simpleVals.length) {
				this.logToRepl(simpleVals.join(' '), sev);
			}
		}
E
Erich Gamma 已提交
235 236
	}

237
	private registerSessionListeners(session: RawDebugSession): void {
238 239
		this.toDisposeOnSessionEnd[session.getId()].push(session);
		this.toDisposeOnSessionEnd[session.getId()].push(session.onDidInitialize(event => {
240
			aria.status(nls.localize('debuggingStarted', "Debugging started."));
241
			const sendConfigurationDone = () => {
242
				if (session && session.configuration.capabilities.supportsConfigurationDoneRequest) {
243
					return session.configurationDone().done(null, e => {
244
						// Disconnect the debug session on configuration done error #10596
245 246
						if (session) {
							session.disconnect().done(null, errors.onUnexpectedError);
247 248 249
						}
						this.messageService.show(severity.Error, e.message);
					});
I
isidor 已提交
250
				}
251
			};
252

253
			const process = this.model.getProcesses().filter(p => p.getId() === session.getId()).pop();
254 255
			this.sendAllBreakpoints(process).then(sendConfigurationDone, sendConfigurationDone)
				.done(() => this.fetchThreads(session), errors.onUnexpectedError);
I
isidor 已提交
256
		}));
E
Erich Gamma 已提交
257

258
		this.toDisposeOnSessionEnd[session.getId()].push(session.onDidStop(event => {
I
isidor 已提交
259
			this.setStateAndEmit(session.getId(), debug.State.Stopped);
I
isidor 已提交
260
			const threadId = event.body.threadId;
E
Erich Gamma 已提交
261

262 263 264 265 266
			session.threads().then(response => {
				if (!response || !response.body || !response.body.threads) {
					return;
				}

I
isidor 已提交
267
				const rawThread = response.body.threads.filter(t => t.id === threadId).pop();
268
				this.model.rawUpdate({
269
					sessionId: session.getId(),
I
isidor 已提交
270
					thread: rawThread,
I
isidor 已提交
271
					threadId,
272 273 274
					stoppedDetails: event.body,
					allThreadsStopped: event.body.allThreadsStopped
				});
275

I
isidor 已提交
276
				const process = this.model.getProcesses().filter(p => p.getId() === session.getId()).pop();
I
isidor 已提交
277 278 279 280 281
				const thread = process && process.getThread(threadId);
				if (thread) {
					thread.getCallStack().then(callStack => {
						if (callStack.length > 0) {
							// focus first stack frame from top that has source location
I
isidor 已提交
282
							const stackFrameToFocus = first(callStack, sf => sf.source && sf.source.available, callStack[0]);
283
							this.focusStackFrameAndEvaluate(stackFrameToFocus).done(null, errors.onUnexpectedError);
I
isidor 已提交
284 285 286 287 288
							this.windowService.getWindow().focus();
							aria.alert(nls.localize('debuggingPaused', "Debugging paused, reason {0}, {1} {2}", event.body.reason, stackFrameToFocus.source ? stackFrameToFocus.source.name : '', stackFrameToFocus.lineNumber));

							return this.openOrRevealSource(stackFrameToFocus.source, stackFrameToFocus.lineNumber, false, false);
						} else {
289
							this.focusStackFrameAndEvaluate(null).done(null, errors.onUnexpectedError);
I
isidor 已提交
290 291 292
						}
					});
				}
E
Erich Gamma 已提交
293 294 295
			}, errors.onUnexpectedError);
		}));

296
		this.toDisposeOnSessionEnd[session.getId()].push(session.onDidThread(event => {
E
Erich Gamma 已提交
297
			if (event.body.reason === 'started') {
298
				this.fetchThreads(session).done(undefined, errors.onUnexpectedError);
E
Erich Gamma 已提交
299
			} else if (event.body.reason === 'exited') {
300
				this.model.clearThreads(session.getId(), true, event.body.threadId);
E
Erich Gamma 已提交
301 302 303
			}
		}));

304
		this.toDisposeOnSessionEnd[session.getId()].push(session.onDidTerminateDebugee(event => {
305
			aria.status(nls.localize('debuggingStopped', "Debugging stopped."));
306
			if (session && session.getId() === event.body.sessionId) {
307
				if (event.body && typeof event.body.restart === 'boolean' && event.body.restart) {
I
isidor 已提交
308 309
					const process = this.model.getProcesses().filter(p => p.getId() === session.getId()).pop();
					this.restartProcess(process).done(null, err => this.messageService.show(severity.Error, err.message));
310
				} else {
311
					session.disconnect().done(null, errors.onUnexpectedError);
312
				}
E
Erich Gamma 已提交
313 314 315
			}
		}));

316
		this.toDisposeOnSessionEnd[session.getId()].push(session.onDidContinued(event => {
317
			this.transitionToRunningState(session, event.body.allThreadsContinued ? undefined : event.body.threadId);
I
isidor 已提交
318 319
		}));

320
		this.toDisposeOnSessionEnd[session.getId()].push(session.onDidOutput(event => {
321
			if (event.body && event.body.category === 'telemetry') {
322
				// only log telemetry events from debug adapter if the adapter provided the telemetry key
I
isidor 已提交
323
				// and the user opted in telemetry
324 325
				if (this.customTelemetryService && this.telemetryService.isOptedIn) {
					this.customTelemetryService.publicLog(event.body.output, event.body.data);
326
				}
327
			} else if (event.body && typeof event.body.output === 'string' && event.body.output.length > 0) {
E
Erich Gamma 已提交
328 329 330 331
				this.onOutput(event);
			}
		}));

332
		this.toDisposeOnSessionEnd[session.getId()].push(session.onDidBreakpoint(event => {
I
isidor 已提交
333
			const id = event.body && event.body.breakpoint ? event.body.breakpoint.id : undefined;
334
			const breakpoint = this.model.getBreakpoints().filter(bp => bp.idFromAdapter === id).pop();
I
isidor 已提交
335 336 337
			if (breakpoint) {
				this.model.updateBreakpoints({ [breakpoint.getId()]: event.body.breakpoint });
			} else {
338
				const functionBreakpoint = this.model.getFunctionBreakpoints().filter(bp => bp.idFromAdapter === id).pop();
I
isidor 已提交
339 340 341 342 343 344
				if (functionBreakpoint) {
					this.model.updateFunctionBreakpoints({ [functionBreakpoint.getId()]: event.body.breakpoint });
				}
			}
		}));

345
		this.toDisposeOnSessionEnd[session.getId()].push(session.onDidExitAdapter(event => {
346
			// 'Run without debugging' mode VSCode must terminate the extension host. More details: #3905
I
isidor 已提交
347
			if (session && session.configuration.type === 'extensionHost' && this.sessionStates[session.getId()] === debug.State.RunningNoDebug) {
348
				this.windowsService.closeExtensionHostWindow(this.contextService.getWorkspace().resource.fsPath);
349
			}
350
			if (session && session.getId() === event.body.sessionId) {
351
				this.onSessionEnd(session);
352
			}
E
Erich Gamma 已提交
353 354 355
		}));
	}

356 357 358 359 360 361 362 363 364 365 366 367 368
	private fetchThreads(session: RawDebugSession): TPromise<any> {
		return session.threads().then(response => {
			if (response && response.body && response.body.threads) {
				response.body.threads.forEach(thread =>
					this.model.rawUpdate({
						sessionId: session.getId(),
						threadId: thread.id,
						thread
					}));
			}
		});
	}

E
Erich Gamma 已提交
369
	private onOutput(event: DebugProtocol.OutputEvent): void {
370
		const outputSeverity = event.body.category === 'stderr' ? severity.Error : event.body.category === 'console' ? severity.Warning : severity.Info;
E
Erich Gamma 已提交
371 372 373
		this.appendReplOutput(event.body.output, outputSeverity);
	}

374 375
	private loadBreakpoints(): Breakpoint[] {
		let result: Breakpoint[];
E
Erich Gamma 已提交
376
		try {
377
			result = JSON.parse(this.storageService.get(DEBUG_BREAKPOINTS_KEY, StorageScope.WORKSPACE, '[]')).map((breakpoint: any) => {
378
				return new Breakpoint(uri.parse(breakpoint.uri.external || breakpoint.source.uri.external), breakpoint.desiredLineNumber || breakpoint.lineNumber, breakpoint.enabled, breakpoint.condition, breakpoint.hitCondition);
E
Erich Gamma 已提交
379
			});
380 381 382
		} catch (e) { }

		return result || [];
E
Erich Gamma 已提交
383 384
	}

385 386
	private loadFunctionBreakpoints(): FunctionBreakpoint[] {
		let result: FunctionBreakpoint[];
I
isidor 已提交
387
		try {
388
			result = JSON.parse(this.storageService.get(DEBUG_FUNCTION_BREAKPOINTS_KEY, StorageScope.WORKSPACE, '[]')).map((fb: any) => {
I
isidor 已提交
389
				return new FunctionBreakpoint(fb.name, fb.enabled, fb.hitCondition);
I
isidor 已提交
390
			});
391 392 393
		} catch (e) { }

		return result || [];
I
isidor 已提交
394 395
	}

396 397
	private loadExceptionBreakpoints(): ExceptionBreakpoint[] {
		let result: ExceptionBreakpoint[];
E
Erich Gamma 已提交
398 399
		try {
			result = JSON.parse(this.storageService.get(DEBUG_EXCEPTION_BREAKPOINTS_KEY, StorageScope.WORKSPACE, '[]')).map((exBreakpoint: any) => {
I
isidor 已提交
400
				return new ExceptionBreakpoint(exBreakpoint.filter || exBreakpoint.name, exBreakpoint.label, exBreakpoint.enabled);
E
Erich Gamma 已提交
401
			});
402
		} catch (e) { }
E
Erich Gamma 已提交
403

404
		return result || [];
E
Erich Gamma 已提交
405 406
	}

I
isidor 已提交
407 408
	private loadWatchExpressions(): Expression[] {
		let result: Expression[];
E
Erich Gamma 已提交
409
		try {
J
Johannes Rieken 已提交
410
			result = JSON.parse(this.storageService.get(DEBUG_WATCH_EXPRESSIONS_KEY, StorageScope.WORKSPACE, '[]')).map((watchStoredData: { name: string, id: string }) => {
411
				return new Expression(watchStoredData.name, watchStoredData.id);
E
Erich Gamma 已提交
412
			});
413 414 415
		} catch (e) { }

		return result || [];
E
Erich Gamma 已提交
416 417
	}

I
isidor 已提交
418
	public get state(): debug.State {
I
isidor 已提交
419 420 421 422 423 424 425 426 427 428 429 430 431 432
		if (!this.contextService.getWorkspace()) {
			return debug.State.Disabled;
		}

		const focusedProcess = this.viewModel.focusedProcess;
		if (focusedProcess) {
			return this.sessionStates[focusedProcess.getId()];
		}
		const processes = this.model.getProcesses();
		if (processes.length > 0) {
			return this.sessionStates[processes[0].getId()];
		}

		return debug.State.Inactive;
E
Erich Gamma 已提交
433 434
	}

I
isidor 已提交
435
	public get onDidChangeState(): Event<void> {
436 437 438
		return this._onDidChangeState.event;
	}

I
isidor 已提交
439 440
	private setStateAndEmit(sessionId: string, newState: debug.State): void {
		this.sessionStates[sessionId] = newState;
I
isidor 已提交
441
		this._onDidChangeState.fire();
E
Erich Gamma 已提交
442 443 444 445 446 447
	}

	public get enabled(): boolean {
		return !!this.contextService.getWorkspace();
	}

448
	public focusStackFrameAndEvaluate(focusedStackFrame: debug.IStackFrame, process?: debug.IProcess): TPromise<void> {
I
isidor 已提交
449
		const processes = this.model.getProcesses();
I
isidor 已提交
450 451 452
		if (!process) {
			process = focusedStackFrame ? focusedStackFrame.thread.process : processes.length ? processes[0] : null;
		}
I
isidor 已提交
453

454
		this.viewModel.setFocusedStackFrame(focusedStackFrame, process);
I
isidor 已提交
455
		this._onDidChangeState.fire();
456

457
		return this.model.evaluateWatchExpressions(this.viewModel.focusedProcess, this.viewModel.focusedStackFrame);
E
Erich Gamma 已提交
458 459
	}

I
isidor 已提交
460
	public enableOrDisableBreakpoints(enable: boolean, breakpoint?: debug.IEnablement): TPromise<void> {
461 462
		if (breakpoint) {
			this.model.setEnablement(breakpoint, enable);
I
isidor 已提交
463
			if (breakpoint instanceof Breakpoint) {
464
				return this.sendBreakpoints(breakpoint.uri);
I
isidor 已提交
465
			} else if (breakpoint instanceof FunctionBreakpoint) {
466 467
				return this.sendFunctionBreakpoints();
			}
E
Erich Gamma 已提交
468

469
			return this.sendExceptionBreakpoints();
E
Erich Gamma 已提交
470 471
		}

472 473
		this.model.enableOrDisableAllBreakpoints(enable);
		return this.sendAllBreakpoints();
E
Erich Gamma 已提交
474 475
	}

476 477 478
	public addBreakpoints(uri: uri, rawBreakpoints: debug.IRawBreakpoint[]): TPromise<void> {
		this.model.addBreakpoints(uri, rawBreakpoints);
		rawBreakpoints.forEach(rbp => aria.status(nls.localize('breakpointAdded', "Added breakpoint, line {0}, file {1}", rbp.lineNumber, uri.fsPath)));
479

480
		return this.sendBreakpoints(uri);
481 482
	}

483 484
	public removeBreakpoints(id?: string): TPromise<any> {
		const toRemove = this.model.getBreakpoints().filter(bp => !id || bp.getId() === id);
485 486
		toRemove.forEach(bp => aria.status(nls.localize('breakpointRemoved', "Removed breakpoint, line {0}, file {1}", bp.lineNumber, bp.uri.fsPath)));
		const urisToClear = distinct(toRemove, bp => bp.uri.toString()).map(bp => bp.uri);
E
Erich Gamma 已提交
487

488
		this.model.removeBreakpoints(toRemove);
I
isidor 已提交
489
		return TPromise.join(urisToClear.map(uri => this.sendBreakpoints(uri)));
E
Erich Gamma 已提交
490 491
	}

492 493
	public setBreakpointsActivated(activated: boolean): TPromise<void> {
		this.model.setBreakpointsActivated(activated);
E
Erich Gamma 已提交
494 495 496
		return this.sendAllBreakpoints();
	}

I
isidor 已提交
497 498
	public addFunctionBreakpoint(): void {
		this.model.addFunctionBreakpoint('');
499 500
	}

I
isidor 已提交
501
	public renameFunctionBreakpoint(id: string, newFunctionName: string): TPromise<void> {
I
isidor 已提交
502
		this.model.updateFunctionBreakpoints({ [id]: { name: newFunctionName } });
I
isidor 已提交
503
		return this.sendFunctionBreakpoints();
I
isidor 已提交
504 505
	}

I
isidor 已提交
506
	public removeFunctionBreakpoints(id?: string): TPromise<void> {
507
		this.model.removeFunctionBreakpoints(id);
I
isidor 已提交
508
		return this.sendFunctionBreakpoints();
I
isidor 已提交
509 510
	}

I
isidor 已提交
511
	public addReplExpression(name: string): TPromise<void> {
P
Pierson Lee 已提交
512
		this.telemetryService.publicLog('debugService/addReplExpression');
513
		return this.model.addReplExpression(this.viewModel.focusedProcess, this.viewModel.focusedStackFrame, name)
514
			// Evaluate all watch expressions again since repl evaluation might have changed some.
515
			.then(() => this.focusStackFrameAndEvaluate(this.viewModel.focusedStackFrame));
E
Erich Gamma 已提交
516 517
	}

518
	public logToRepl(value: string | { [key: string]: any }, severity?: severity): void {
E
Erich Gamma 已提交
519 520 521 522 523 524 525
		this.model.logToRepl(value, severity);
	}

	public appendReplOutput(value: string, severity?: severity): void {
		this.model.appendReplOutput(value, severity);
	}

526 527
	public removeReplExpressions(): void {
		this.model.removeReplExpressions();
E
Erich Gamma 已提交
528 529
	}

I
isidor 已提交
530
	public addWatchExpression(name: string): TPromise<void> {
531
		return this.model.addWatchExpression(this.viewModel.focusedProcess, this.viewModel.focusedStackFrame, name);
E
Erich Gamma 已提交
532 533
	}

I
isidor 已提交
534
	public renameWatchExpression(id: string, newName: string): TPromise<void> {
535
		return this.model.renameWatchExpression(this.viewModel.focusedProcess, this.viewModel.focusedStackFrame, id, newName);
E
Erich Gamma 已提交
536 537
	}

I
isidor 已提交
538 539 540 541
	public moveWatchExpression(id: string, position: number): void {
		this.model.moveWatchExpression(id, position);
	}

542 543
	public removeWatchExpressions(id?: string): void {
		this.model.removeWatchExpressions(id);
E
Erich Gamma 已提交
544 545
	}

I
isidor 已提交
546
	public createProcess(configurationOrName: debug.IConfig | string): TPromise<any> {
547
		this.removeReplExpressions();
I
isidor 已提交
548
		const sessionId = generateUuid();
I
isidor 已提交
549
		this.setStateAndEmit(sessionId, debug.State.Initializing);
E
Erich Gamma 已提交
550

551
		return this.textFileService.saveAll()							// make sure all dirty files are saved
552
			.then(() => this.configurationService.reloadConfiguration()	// make sure configuration is up to date
J
Johannes Rieken 已提交
553
				.then(() => this.extensionService.onReady()
I
isidor 已提交
554 555
					.then(() => this.configurationManager.getConfiguration(configurationOrName)
						.then(configuration => {
J
Johannes Rieken 已提交
556 557 558 559 560 561 562 563
							if (!configuration) {
								return this.configurationManager.openConfigFile(false).then(openend => {
									if (openend) {
										this.messageService.show(severity.Info, nls.localize('NewLaunchConfig', "Please set up the launch configuration file for your application."));
									}
								});
							}

564
							if (!this.configurationManager.getAdapter(configuration.type)) {
J
Johannes Rieken 已提交
565 566 567 568 569 570 571 572 573 574
								return configuration.type ? TPromise.wrapError(new Error(nls.localize('debugTypeNotSupported', "Configured debug type '{0}' is not supported.", configuration.type)))
									: TPromise.wrapError(errors.create(nls.localize('debugTypeMissing', "Missing property 'type' for the chosen launch configuration."),
										{ actions: [this.instantiationService.createInstance(debugactions.ConfigureAction, debugactions.ConfigureAction.ID, debugactions.ConfigureAction.LABEL), CloseAction] }));
							}

							return this.runPreLaunchTask(configuration.preLaunchTask).then((taskSummary: ITaskSummary) => {
								const errorCount = configuration.preLaunchTask ? this.markerService.getStatistics().errors : 0;
								const successExitCode = taskSummary && taskSummary.exitCode === 0;
								const failureExitCode = taskSummary && taskSummary.exitCode !== undefined && taskSummary.exitCode !== 0;
								if (successExitCode || (errorCount === 0 && !failureExitCode)) {
I
isidor 已提交
575
									return this.doCreateProcess(sessionId, configuration);
J
Johannes Rieken 已提交
576 577 578 579 580 581 582 583
								}

								this.messageService.show(severity.Error, {
									message: errorCount > 1 ? nls.localize('preLaunchTaskErrors', "Build errors have been detected during preLaunchTask '{0}'.", configuration.preLaunchTask) :
										errorCount === 1 ? nls.localize('preLaunchTaskError', "Build error has been detected during preLaunchTask '{0}'.", configuration.preLaunchTask) :
											nls.localize('preLaunchTaskExitCode', "The preLaunchTask '{0}' terminated with exit code {1}.", configuration.preLaunchTask, taskSummary.exitCode),
									actions: [new Action('debug.continue', nls.localize('debugAnyway', "Debug Anyway"), null, true, () => {
										this.messageService.hideAll();
I
isidor 已提交
584
										return this.doCreateProcess(sessionId, configuration);
J
Johannes Rieken 已提交
585 586 587 588 589 590 591 592 593 594 595 596 597
									}), CloseAction]
								});
							}, (err: TaskError) => {
								if (err.code !== TaskErrors.NotConfigured) {
									throw err;
								}

								this.messageService.show(err.severity, {
									message: err.message,
									actions: [this.taskService.configureAction(), CloseAction]
								});
							});
						}))));
I
isidor 已提交
598
	}
E
Erich Gamma 已提交
599

I
isidor 已提交
600
	private doCreateProcess(sessionId: string, configuration: debug.IConfig): TPromise<any> {
601

J
Joao Moreno 已提交
602
		return this.telemetryService.getTelemetryInfo().then(info => {
603
			const telemetryInfo: { [key: string]: string } = Object.create(null);
604 605
			telemetryInfo['common.vscodemachineid'] = info.machineId;
			telemetryInfo['common.vscodesessionid'] = info.sessionId;
606 607
			return telemetryInfo;
		}).then(data => {
608 609 610
			const adapter = this.configurationManager.getAdapter(configuration.type);
			const { aiKey, type } = adapter;
			const publisher = adapter.extensionDescription.publisher;
611
			this.customTelemetryService = null;
612
			let client: TelemetryClient;
613 614

			if (aiKey) {
615
				client = new TelemetryClient(
616 617 618 619
					uri.parse(require.toUrl('bootstrap')).fsPath,
					{
						serverName: 'Debug Telemetry',
						timeout: 1000 * 60 * 5,
J
Johannes Rieken 已提交
620
						args: [`${publisher}.${type}`, JSON.stringify(data), aiKey],
621
						env: {
B
Benjamin Pasero 已提交
622
							ELECTRON_RUN_AS_NODE: 1,
623 624 625
							PIPE_LOGGING: 'true',
							AMD_ENTRYPOINT: 'vs/workbench/parts/debug/node/telemetryApp'
						}
626
					}
627
				);
628

629 630
				const channel = client.getChannel('telemetryAppender');
				const appender = new TelemetryAppenderClient(channel);
631

632
				this.customTelemetryService = new TelemetryService({ appender }, this.configurationService);
633 634
			}

635
			const session = this.instantiationService.createInstance(RawDebugSession, sessionId, configuration.debugServer, adapter, this.customTelemetryService);
636 637
			const process = this.model.addProcess(configuration.name, session);

638 639 640
			if (this.model.getProcesses().length > 1) {
				this.viewModel.setMultiProcessView(true);
			}
641
			if (!this.viewModel.focusedProcess) {
642
				this.viewModel.setFocusedStackFrame(null, process);
643
				this._onDidChangeState.fire();
644
			}
645 646 647 648
			this.toDisposeOnSessionEnd[session.getId()] = [];
			if (client) {
				this.toDisposeOnSessionEnd[session.getId()].push(client);
			}
649
			this.registerSessionListeners(session);
J
Joao Moreno 已提交
650

651
			return session.initialize({
J
Joao Moreno 已提交
652 653 654
				adapterID: configuration.type,
				pathFormat: 'path',
				linesStartAt1: true,
655
				columnsStartAt1: true,
I
isidor 已提交
656
				supportsVariableType: true, // #8858
657 658
				supportsVariablePaging: true, // #9537
				supportsRunInTerminalRequest: true // #10574
J
Joao Moreno 已提交
659
			}).then((result: DebugProtocol.InitializeResponse) => {
660
				if (session.disconnected) {
J
Joao Moreno 已提交
661 662
					return TPromise.wrapError(new Error(nls.localize('debugAdapterCrash', "Debug adapter process has terminated unexpectedly")));
				}
663

664 665
				this.model.setExceptionBreakpoints(session.configuration.capabilities.exceptionBreakpointFilters);
				return configuration.request === 'attach' ? session.attach(configuration) : session.launch(configuration);
J
Joao Moreno 已提交
666
			}).then((result: DebugProtocol.Response) => {
667
				if (session.disconnected) {
668 669 670
					return TPromise.as(null);
				}

J
Joao Moreno 已提交
671 672 673
				if (configuration.internalConsoleOptions === 'openOnSessionStart' || (!this.viewModel.changedWorkbenchViewState && configuration.internalConsoleOptions !== 'neverOpen')) {
					this.panelService.openPanel(debug.REPL_ID, false).done(undefined, errors.onUnexpectedError);
				}
674

J
Joao Moreno 已提交
675 676 677 678 679
				if (!this.viewModel.changedWorkbenchViewState && !this.partService.isSideBarHidden()) {
					// We only want to change the workbench view state on the first debug session #5738 and if the side bar is not hidden
					this.viewModel.changedWorkbenchViewState = true;
					this.viewletService.openViewlet(debug.VIEWLET_ID);
				}
680

J
Joao Moreno 已提交
681 682 683 684 685 686
				// Do not change status bar to orange if we are just running without debug.
				if (!configuration.noDebug) {
					this.partService.addClass('debugging');
				}
				this.extensionService.activateByEvent(`onDebug:${configuration.type}`).done(null, errors.onUnexpectedError);
				this.inDebugMode.set(true);
687
				this.transitionToRunningState(session);
J
Joao Moreno 已提交
688 689 690 691 692 693

				this.telemetryService.publicLog('debugSessionStart', {
					type: configuration.type,
					breakpointCount: this.model.getBreakpoints().length,
					exceptionBreakpoints: this.model.getExceptionBreakpoints(),
					watchExpressionsCount: this.model.getWatchExpressions().length,
694 695
					extensionName: `${adapter.extensionDescription.publisher}.${adapter.extensionDescription.name}`,
					isBuiltin: adapter.extensionDescription.isBuiltin
J
Joao Moreno 已提交
696 697
				});
			}).then(undefined, (error: any) => {
698 699 700 701 702
				if (error instanceof Error && error.message === 'Canceled') {
					// Do not show 'canceled' error messages to the user #7906
					return TPromise.as(null);
				}

J
Joao Moreno 已提交
703
				this.telemetryService.publicLog('debugMisconfiguration', { type: configuration ? configuration.type : undefined });
I
isidor 已提交
704
				this.setStateAndEmit(session.getId(), debug.State.Inactive);
705 706
				if (!session.disconnected) {
					session.disconnect().done(null, errors.onUnexpectedError);
J
Joao Moreno 已提交
707 708 709 710 711
				}
				// Show the repl if some error got logged there #5870
				if (this.model.getReplElements().length > 0) {
					this.panelService.openPanel(debug.REPL_ID, false).done(undefined, errors.onUnexpectedError);
				}
I
isidor 已提交
712

J
Joao Moreno 已提交
713 714 715 716
				const configureAction = this.instantiationService.createInstance(debugactions.ConfigureAction, debugactions.ConfigureAction.ID, debugactions.ConfigureAction.LABEL);
				const actions = (error.actions && error.actions.length) ? error.actions.concat([configureAction]) : [CloseAction, configureAction];
				return TPromise.wrapError(errors.create(error.message, { actions }));
			});
E
Erich Gamma 已提交
717 718 719
		});
	}

I
isidor 已提交
720
	private runPreLaunchTask(taskName: string): TPromise<ITaskSummary> {
721
		if (!taskName) {
I
isidor 已提交
722
			return TPromise.as(null);
E
Erich Gamma 已提交
723 724
		}

725
		// run a task before starting a debug session
E
Erich Gamma 已提交
726
		return this.taskService.tasks().then(descriptions => {
727
			const filteredTasks = descriptions.filter(task => task.name === taskName);
E
Erich Gamma 已提交
728
			if (filteredTasks.length !== 1) {
729 730
				return TPromise.wrapError(errors.create(nls.localize('DebugTaskNotFound', "Could not find the preLaunchTask \'{0}\'.", taskName), {
					actions: [
731
						this.instantiationService.createInstance(debugactions.ConfigureAction, debugactions.ConfigureAction.ID, debugactions.ConfigureAction.LABEL),
732
						this.taskService.configureAction(),
733
						CloseAction
734 735
					]
				}));
E
Erich Gamma 已提交
736 737
			}

I
isidor 已提交
738
			// task is already running - nothing to do.
739
			if (this.lastTaskEvent && this.lastTaskEvent.taskName === taskName) {
I
isidor 已提交
740
				return TPromise.as(null);
E
Erich Gamma 已提交
741 742 743
			}

			if (this.lastTaskEvent) {
I
isidor 已提交
744
				// there is a different task running currently.
I
isidor 已提交
745
				return TPromise.wrapError(errors.create(nls.localize('differentTaskRunning', "There is a task {0} running. Can not run pre launch task {1}.", this.lastTaskEvent.taskName, taskName)));
E
Erich Gamma 已提交
746 747
			}

I
isidor 已提交
748
			// no task running, execute the preLaunchTask.
749
			const taskPromise = this.taskService.run(filteredTasks[0].id).then(result => {
E
Erich Gamma 已提交
750
				this.lastTaskEvent = null;
I
isidor 已提交
751
				return result;
E
Erich Gamma 已提交
752 753 754
			}, err => {
				this.lastTaskEvent = null;
			});
755

756
			if (filteredTasks[0].isWatching) {
A
Alex Dima 已提交
757
				return new TPromise((c, e) => this.taskService.addOneTimeDisposableListener(TaskServiceEvents.Inactive, () => c(null)));
758 759 760
			}

			return taskPromise;
E
Erich Gamma 已提交
761 762 763
		});
	}

764 765 766
	private rawAttach(session: RawDebugSession, port: number): TPromise<any> {
		if (session) {
			return session.attach({ port });
I
isidor 已提交
767 768
		}

I
isidor 已提交
769
		const sessionId = generateUuid();
I
isidor 已提交
770
		this.setStateAndEmit(sessionId, debug.State.Initializing);
I
isidor 已提交
771 772
		return this.configurationManager.getConfiguration(this.viewModel.selectedConfigurationName).then(config =>
			this.doCreateProcess(sessionId, config)
773
		);
I
isidor 已提交
774 775
	}

I
isidor 已提交
776 777
	public restartProcess(process: debug.IProcess): TPromise<any> {
		return process ? process.session.disconnect(true).then(() =>
778
			new TPromise<void>((c, e) => {
E
Erich Gamma 已提交
779
				setTimeout(() => {
I
isidor 已提交
780
					this.createProcess(process.name).then(() => c(null), err => e(err));
E
Erich Gamma 已提交
781
				}, 300);
I
isidor 已提交
782
			})
I
isidor 已提交
783
		) : this.createProcess(this.viewModel.selectedConfigurationName);
E
Erich Gamma 已提交
784 785
	}

I
isidor 已提交
786
	private onSessionEnd(session: RawDebugSession): void {
787
		if (session) {
I
isidor 已提交
788
			const bpsExist = this.model.getBreakpoints().length > 0;
I
isidor 已提交
789
			this.telemetryService.publicLog('debugSessionStop', {
790 791 792
				type: session.configuration.type,
				success: session.emittedStopped || !bpsExist,
				sessionLengthInSeconds: session.getLengthInSeconds(),
I
isidor 已提交
793 794 795
				breakpointCount: this.model.getBreakpoints().length,
				watchExpressionsCount: this.model.getWatchExpressions().length
			});
E
Erich Gamma 已提交
796
		}
797

I
isidor 已提交
798
		try {
799
			this.toDisposeOnSessionEnd[session.getId()] = lifecycle.dispose(this.toDisposeOnSessionEnd[session.getId()]);
I
isidor 已提交
800 801 802 803
		} catch (e) {
			// an internal module might be open so the dispose can throw -> ignore and continue with stop session.
		}

804
		this.model.removeProcess(session.getId());
805
		this.focusStackFrameAndEvaluate(null).done(null, errors.onUnexpectedError);
I
isidor 已提交
806
		this.setStateAndEmit(session.getId(), debug.State.Inactive);
807

808 809 810
		if (this.model.getProcesses().length === 0) {
			this.partService.removeClass('debugging');
			// set breakpoints back to unverified since the session ended.
811
			const data: { [id: string]: { line: number, verified: boolean } } = {};
812
			this.model.getBreakpoints().forEach(bp => {
813
				data[bp.getId()] = { line: bp.lineNumber, verified: false };
814 815
			});
			this.model.updateBreakpoints(data);
816

817
			this.inDebugMode.reset();
818
			this.viewModel.setMultiProcessView(false);
I
isidor 已提交
819

820 821 822
			if (!this.partService.isSideBarHidden() && this.configurationService.getConfiguration<debug.IDebugConfiguration>('debug').openExplorerOnEnd) {
				this.viewletService.openViewlet(EXPLORER_VIEWLET_ID).done(null, errors.onUnexpectedError);
			}
I
isidor 已提交
823
		}
E
Erich Gamma 已提交
824 825 826 827 828 829 830 831 832 833
	}

	public getModel(): debug.IModel {
		return this.model;
	}

	public getViewModel(): debug.IViewModel {
		return this.viewModel;
	}

834
	public openOrRevealSource(sourceOrUri: Source | uri, lineNumber: number, preserveFocus: boolean, sideBySide: boolean): TPromise<any> {
E
Erich Gamma 已提交
835
		const visibleEditors = this.editorService.getVisibleEditors();
836 837
		const uri = sourceOrUri instanceof Source ? sourceOrUri.uri : sourceOrUri;
		const source = sourceOrUri instanceof Source ? sourceOrUri : null;
I
isidor 已提交
838
		for (let i = 0; i < visibleEditors.length; i++) {
I
isidor 已提交
839
			const fileInput = asFileEditorInput(visibleEditors[i].input);
840 841
			if ((fileInput && fileInput.getResource().toString() === uri.toString()) ||
				(visibleEditors[i].input instanceof DebugStringEditorInput && (<DebugStringEditorInput>visibleEditors[i].input).getResource().toString() === uri.toString())) {
842

I
isidor 已提交
843
				const control = <ICodeEditor>visibleEditors[i].getControl();
844 845 846
				if (control) {
					control.revealLineInCenterIfOutsideViewport(lineNumber);
					control.setSelection({ startLineNumber: lineNumber, startColumn: 1, endLineNumber: lineNumber, endColumn: 1 });
847
					this.editorGroupService.activateGroup(i);
I
isidor 已提交
848
					if (!preserveFocus) {
849
						this.editorGroupService.focusGroup(i);
I
isidor 已提交
850
					}
E
Erich Gamma 已提交
851
				}
852

A
Alex Dima 已提交
853
				return TPromise.as(null);
E
Erich Gamma 已提交
854 855 856
			}
		}

857
		const process = this.viewModel.focusedProcess;
858
		if (process && source && source.inMemory) {
I
isidor 已提交
859
			// internal module
860
			if (source.reference !== 0 && source.available) {
861
				return process.session.source({ sourceReference: source.reference }).then(response => {
862 863
					const mime = response && response.body && response.body.mimeType ? response.body.mimeType : guessMimeTypes(source.name)[0];
					const inputValue = response && response.body ? response.body.content : '';
864
					return this.getDebugStringEditorInput(process, source, inputValue, mime);
865 866
				}, (err: DebugProtocol.ErrorResponse) => {
					// Display the error from debug adapter using a temporary editor #8836
867
					return this.getDebugErrorEditorInput(process, source, err.message);
868
				}).then(editorInput => {
B
Benjamin Pasero 已提交
869
					return this.editorService.openEditor(editorInput, { preserveFocus, selection: { startLineNumber: lineNumber, startColumn: 1, endLineNumber: lineNumber, endColumn: 1 } }, sideBySide);
E
Erich Gamma 已提交
870 871 872
				});
			}

873
			return this.sourceIsUnavailable(process, source, sideBySide);
E
Erich Gamma 已提交
874
		}
875 876 877
		if (Source.isInMemory(uri)) {
			return TPromise.as(null);
		}
E
Erich Gamma 已提交
878

879
		return this.fileService.resolveFile(uri).then(() =>
E
Erich Gamma 已提交
880
			this.editorService.openEditor({
881
				resource: uri,
E
Erich Gamma 已提交
882 883 884 885 886 887 888 889 890
				options: {
					selection: {
						startLineNumber: lineNumber,
						startColumn: 1,
						endLineNumber: lineNumber,
						endColumn: 1
					},
					preserveFocus: preserveFocus
				}
891
			}, sideBySide), err => this.sourceIsUnavailable(process, source, sideBySide)
E
Erich Gamma 已提交
892 893 894
		);
	}

895
	private sourceIsUnavailable(process: debug.IProcess, source: Source, sideBySide: boolean): TPromise<any> {
E
Erich Gamma 已提交
896
		this.model.sourceIsUnavailable(source);
897
		const editorInput = this.getDebugErrorEditorInput(process, source, nls.localize('debugSourceNotAvailable', "Source {0} is not available.", source.name));
E
Erich Gamma 已提交
898

899
		return this.editorService.openEditor(editorInput, { preserveFocus: true }, sideBySide);
E
Erich Gamma 已提交
900 901
	}

902 903
	public getConfigurationManager(): debug.IConfigurationManager {
		return this.configurationManager;
904 905
	}

906
	private transitionToRunningState(session: RawDebugSession, threadId?: number): void {
907
		this.model.clearThreads(session.getId(), false, threadId);
908
		this.setStateAndEmit(session.getId(), session.requestType === debug.SessionRequestType.LAUNCH_NO_DEBUG ? debug.State.RunningNoDebug : debug.State.Running);
909
		this.focusStackFrameAndEvaluate(null).done(null, errors.onUnexpectedError);
910 911
	}

912
	private getDebugStringEditorInput(process: debug.IProcess, source: Source, value: string, mtype: string): DebugStringEditorInput {
913
		const result = this.instantiationService.createInstance(DebugStringEditorInput, source.name, source.uri, source.origin, value, mtype, void 0);
914
		this.toDisposeOnSessionEnd[process.getId()].push(result);
I
isidor 已提交
915 916 917 918

		return result;
	}

919
	private getDebugErrorEditorInput(process: debug.IProcess, source: Source, value: string): DebugErrorEditorInput {
I
isidor 已提交
920
		const result = this.instantiationService.createInstance(DebugErrorEditorInput, source.name, value);
921
		this.toDisposeOnSessionEnd[process.getId()].push(result);
I
isidor 已提交
922

923
		return result;
E
Erich Gamma 已提交
924 925
	}

926 927 928
	private sendAllBreakpoints(process?: debug.IProcess): TPromise<any> {
		return TPromise.join(distinct(this.model.getBreakpoints(), bp => bp.uri.toString()).map(bp => this.sendBreakpoints(bp.uri, false, process)))
			.then(() => this.sendFunctionBreakpoints(process))
I
isidor 已提交
929
			// send exception breakpoints at the end since some debug adapters rely on the order
930
			.then(() => this.sendExceptionBreakpoints(process));
E
Erich Gamma 已提交
931 932
	}

933 934 935 936
	private sendBreakpoints(modelUri: uri, sourceModified = false, targetProcess?: debug.IProcess): TPromise<void> {

		const sendBreakpointsToProcess = (process: debug.IProcess): TPromise<void> => {
			const session = <RawDebugSession>process.session;
I
isidor 已提交
937 938 939 940 941 942 943 944
			if (!session.readyForBreakpoints) {
				return TPromise.as(null);
			}
			if (this.textFileService.isDirty(modelUri)) {
				// Only send breakpoints for a file once it is not dirty #8077
				this.breakpointsToSendOnResourceSaved[modelUri.toString()] = true;
				return TPromise.as(null);
			}
945

I
isidor 已提交
946
			const breakpointsToSend = distinct(
947
				this.model.getBreakpoints().filter(bp => this.model.areBreakpointsActivated() && bp.enabled && bp.uri.toString() === modelUri.toString()),
I
isidor 已提交
948 949
				bp => `${bp.desiredLineNumber}`
			);
950 951 952 953 954 955 956 957 958 959 960

			let rawSource: DebugProtocol.Source;
			for (let t of process.getAllThreads()) {
				for (let sf of t.getCachedCallStack()) {
					if (sf.source.uri.toString() === modelUri.toString()) {
						rawSource = sf.source.raw;
						break;
					}
				}
			}
			rawSource = rawSource || { path: paths.normalize(modelUri.fsPath, true), name: paths.basename(modelUri.fsPath) };
I
isidor 已提交
961 962 963 964 965 966 967 968 969 970

			return session.setBreakpoints({
				source: rawSource,
				lines: breakpointsToSend.map(bp => bp.desiredLineNumber),
				breakpoints: breakpointsToSend.map(bp => ({ line: bp.desiredLineNumber, condition: bp.condition, hitCondition: bp.hitCondition })),
				sourceModified
			}).then(response => {
				if (!response || !response.body) {
					return;
				}
971

I
isidor 已提交
972 973 974 975
				const data: { [id: string]: { line?: number, verified: boolean } } = {};
				for (let i = 0; i < breakpointsToSend.length; i++) {
					data[breakpointsToSend[i].getId()] = response.body.breakpoints[i];
				}
976

I
isidor 已提交
977 978 979
				this.model.updateBreakpoints(data);
			});
		};
980

981
		return this.sendToOneOrAllProcesses(targetProcess, sendBreakpointsToProcess);
E
Erich Gamma 已提交
982 983
	}

984 985 986
	private sendFunctionBreakpoints(targetProcess?: debug.IProcess): TPromise<void> {
		const sendFunctionBreakpointsToProcess = (process: debug.IProcess): TPromise<void> => {
			const session = <RawDebugSession>process.session;
I
isidor 已提交
987 988
			if (!session.readyForBreakpoints || !session.configuration.capabilities.supportsFunctionBreakpoints) {
				return TPromise.as(null);
989 990
			}

I
isidor 已提交
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
			const breakpointsToSend = this.model.getFunctionBreakpoints().filter(fbp => fbp.enabled && this.model.areBreakpointsActivated());
			return session.setFunctionBreakpoints({ breakpoints: breakpointsToSend }).then(response => {
				if (!response || !response.body) {
					return;
				}

				const data: { [id: string]: { name?: string, verified?: boolean } } = {};
				for (let i = 0; i < breakpointsToSend.length; i++) {
					data[breakpointsToSend[i].getId()] = response.body.breakpoints[i];
				}

				this.model.updateFunctionBreakpoints(data);
			});
		};

1006
		return this.sendToOneOrAllProcesses(targetProcess, sendFunctionBreakpointsToProcess);
I
isidor 已提交
1007 1008
	}

1009 1010 1011 1012
	private sendExceptionBreakpoints(targetProcess?: debug.IProcess): TPromise<void> {
		const sendExceptionBreakpointsToProcess = (process: debug.IProcess): TPromise<any> => {
			const session = <RawDebugSession>process.session;
			if (!session.readyForBreakpoints || this.model.getExceptionBreakpoints().length === 0) {
I
isidor 已提交
1013
				return TPromise.as(null);
I
isidor 已提交
1014 1015
			}

I
isidor 已提交
1016 1017 1018 1019
			const enabledExceptionBps = this.model.getExceptionBreakpoints().filter(exb => exb.enabled);
			return session.setExceptionBreakpoints({ filters: enabledExceptionBps.map(exb => exb.filter) });
		};

1020
		return this.sendToOneOrAllProcesses(targetProcess, sendExceptionBreakpointsToProcess);
I
isidor 已提交
1021 1022
	}

1023 1024 1025
	private sendToOneOrAllProcesses(process: debug.IProcess, send: (process: debug.IProcess) => TPromise<void>): TPromise<void> {
		if (process) {
			return send(process);
I
isidor 已提交
1026
		}
I
isidor 已提交
1027

1028
		return TPromise.join(this.model.getProcesses().map(p => send(p))).then(() => void 0);
E
Erich Gamma 已提交
1029 1030 1031
	}

	private onFileChanges(fileChangesEvent: FileChangesEvent): void {
1032
		this.model.removeBreakpoints(this.model.getBreakpoints().filter(bp =>
1033
			fileChangesEvent.contains(bp.uri, FileChangeType.DELETED)));
1034 1035 1036 1037 1038 1039 1040 1041

		fileChangesEvent.getUpdated().forEach(event => {
			if (this.breakpointsToSendOnResourceSaved[event.resource.toString()]) {
				this.breakpointsToSendOnResourceSaved[event.resource.toString()] = false;
				this.sendBreakpoints(event.resource, true).done(null, errors.onUnexpectedError);
			}
		});

E
Erich Gamma 已提交
1042 1043 1044 1045 1046
	}

	private store(): void {
		this.storageService.store(DEBUG_BREAKPOINTS_KEY, JSON.stringify(this.model.getBreakpoints()), StorageScope.WORKSPACE);
		this.storageService.store(DEBUG_BREAKPOINTS_ACTIVATED_KEY, this.model.areBreakpointsActivated() ? 'true' : 'false', StorageScope.WORKSPACE);
1047
		this.storageService.store(DEBUG_FUNCTION_BREAKPOINTS_KEY, JSON.stringify(this.model.getFunctionBreakpoints()), StorageScope.WORKSPACE);
E
Erich Gamma 已提交
1048
		this.storageService.store(DEBUG_EXCEPTION_BREAKPOINTS_KEY, JSON.stringify(this.model.getExceptionBreakpoints()), StorageScope.WORKSPACE);
1049
		this.storageService.store(DEBUG_SELECTED_CONFIG_NAME_KEY, this.viewModel.selectedConfigurationName, StorageScope.WORKSPACE);
1050
		this.storageService.store(DEBUG_WATCH_EXPRESSIONS_KEY, JSON.stringify(this.model.getWatchExpressions().map(we => ({ name: we.name, id: we.getId() }))), StorageScope.WORKSPACE);
E
Erich Gamma 已提交
1051 1052 1053
	}

	public dispose(): void {
1054
		Object.keys(this.toDisposeOnSessionEnd).forEach(key => lifecycle.dispose(this.toDisposeOnSessionEnd[key]));
I
isidor 已提交
1055
		this.toDispose = lifecycle.dispose(this.toDispose);
E
Erich Gamma 已提交
1056 1057
	}
}