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

import nls = require('vs/nls');
import lifecycle = require('vs/base/common/lifecycle');
8
import { guessMimeTypes, MIME_TEXT } from 'vs/base/common/mime';
9
import Event, { Emitter } from 'vs/base/common/event';
E
Erich Gamma 已提交
10
import uri from 'vs/base/common/uri';
11
import { RunOnceScheduler } from 'vs/base/common/async';
12
import { Action } from 'vs/base/common/actions';
E
Erich Gamma 已提交
13
import arrays = require('vs/base/common/arrays');
14
import types = require('vs/base/common/types');
E
Erich Gamma 已提交
15 16
import errors = require('vs/base/common/errors');
import severity from 'vs/base/common/severity';
I
isidor 已提交
17
import { TPromise } from 'vs/base/common/winjs.base';
I
isidor 已提交
18
import aria = require('vs/base/browser/ui/aria/aria');
E
Erich Gamma 已提交
19
import editorbrowser = require('vs/editor/browser/editorBrowser');
20
import { IKeybindingService, IKeybindingContextKey } from 'vs/platform/keybinding/common/keybinding';
21 22
import {IMarkerService} from 'vs/platform/markers/common/markers';
import { ILifecycleService } from 'vs/platform/lifecycle/common/lifecycle';
A
Alex Dima 已提交
23
import { IExtensionService } from 'vs/platform/extensions/common/extensions';
24 25 26 27 28
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';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
29
import { TelemetryService } from 'vs/platform/telemetry/common/telemetryService';
30
import { TelemetryAppenderClient } from 'vs/platform/telemetry/common/telemetryIpc';
31
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
32
import { IEditorGroupService } from 'vs/workbench/services/group/common/groupService';
E
Erich Gamma 已提交
33 34 35 36
import wbeditorcommon = require('vs/workbench/common/editor');
import debug = require('vs/workbench/parts/debug/common/debug');
import session = require('vs/workbench/parts/debug/node/rawDebugSession');
import model = require('vs/workbench/parts/debug/common/debugModel');
37
import { DebugStringEditorInput } from 'vs/workbench/parts/debug/browser/debugEditorInputs';
E
Erich Gamma 已提交
38
import viewmodel = require('vs/workbench/parts/debug/common/debugViewModel');
39
import debugactions = require('vs/workbench/parts/debug/browser/debugActions');
40
import { ConfigurationManager } from 'vs/workbench/parts/debug/node/debugConfigurationManager';
I
isidor 已提交
41
import { Source } from 'vs/workbench/parts/debug/common/debugSource';
42 43
import { ITaskService, TaskEvent, TaskType, TaskServiceEvents, ITaskSummary} from 'vs/workbench/parts/tasks/common/taskService';
import { TaskError, TaskErrors } from 'vs/workbench/parts/tasks/common/taskSystem';
E
Erich Gamma 已提交
44
import { IViewletService } from 'vs/workbench/services/viewlet/common/viewletService';
I
isidor 已提交
45
import { IPanelService } from 'vs/workbench/services/panel/common/panelService';
E
Erich Gamma 已提交
46
import { IPartService } from 'vs/workbench/services/part/common/partService';
47
import { ITextFileService } from 'vs/workbench/parts/files/common/files';
48
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
E
Erich Gamma 已提交
49 50
import { IWorkspaceContextService } from 'vs/workbench/services/workspace/common/contextService';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
51
import { IWindowService, IBroadcast } from 'vs/workbench/services/window/electron-browser/windowService';
52
import { ILogEntry, EXTENSION_LOG_BROADCAST_CHANNEL, EXTENSION_ATTACH_BROADCAST_CHANNEL, EXTENSION_TERMINATE_BROADCAST_CHANNEL } from 'vs/workbench/services/thread/electron-browser/threadService';
53
import { ipcRenderer as ipc } from 'electron';
54
import { Client } from 'vs/base/parts/ipc/node/ipc.cp';
E
Erich Gamma 已提交
55

I
isidor 已提交
56 57 58 59 60 61
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 已提交
62

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

I
isidor 已提交
66
	private _state: debug.State;
67
	private _onDidChangeState: Emitter<debug.State>;
I
isidor 已提交
68
	private session: session.RawDebugSession;
E
Erich Gamma 已提交
69 70
	private model: model.Model;
	private viewModel: viewmodel.ViewModel;
71
	private configurationManager: ConfigurationManager;
72
	private customTelemetryService: ITelemetryService;
E
Erich Gamma 已提交
73
	private lastTaskEvent: TaskEvent;
74
	private toDispose: lifecycle.IDisposable[];
75
	private toDisposeOnSessionEnd: lifecycle.IDisposable[];
E
Erich Gamma 已提交
76
	private inDebugMode: IKeybindingContextKey<boolean>;
77
	private breakpointsToSendOnResourceSaved: { [uri: string]: boolean };
E
Erich Gamma 已提交
78 79 80 81

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

108
		if (!this.contextService.getWorkspace()) {
I
isidor 已提交
109
			this._state = debug.State.Disabled;
E
Erich Gamma 已提交
110
		}
111
		this.configurationManager = this.instantiationService.createInstance(ConfigurationManager, this.storageService.get(DEBUG_SELECTED_CONFIG_NAME_KEY, StorageScope.WORKSPACE, 'null'));
E
Erich Gamma 已提交
112 113
		this.inDebugMode = keybindingService.createKey(debug.CONTEXT_IN_DEBUG_MODE, false);

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

E
Erich Gamma 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
		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;
			}));
		}

139 140
		lifecycleService.onShutdown(this.store, this);
		lifecycleService.onShutdown(this.dispose, this);
141

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

	private onBroadcast(broadcast: IBroadcast): void {
146

I
isidor 已提交
147
		// attach: PH is ready to be attached to
148
		if (broadcast.channel === EXTENSION_ATTACH_BROADCAST_CHANNEL) {
149
			this.rawAttach(broadcast.payload.port);
150 151
			return;
		}
152

153 154
		if (broadcast.channel === EXTENSION_TERMINATE_BROADCAST_CHANNEL) {
			this.onSessionEnd();
155 156 157
			return;
		}

I
isidor 已提交
158
		// from this point on we require an active session
159
		let session = this.getActiveSession();
I
isidor 已提交
160
		if (!session || session.configuration.type !== 'extensionHost') {
161 162 163
			return; // we are only intersted if we have an active debug session for extensionHost
		}

I
isidor 已提交
164
		// a plugin logged output, show it inside the REPL
165
		if (broadcast.channel === EXTENSION_LOG_BROADCAST_CHANNEL) {
166 167 168 169 170 171 172 173 174 175 176
			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 已提交
177
			// add output for each argument logged
178 179 180 181
			let simpleVals: any[] = [];
			for (let i = 0; i < args.length; i++) {
				let a = args[i];

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

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

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

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

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

I
isidor 已提交
205 206
				// string: watch out for % replacement directive
				// string substitution and formatting @ https://developer.chrome.com/devtools/docs/console
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
				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
							buf += !types.isUndefinedOrNull(args[i]) ? args[i] : ''; // replace
							j++; // read over directive
						} else {
							buf += a[j];
						}
					}

					simpleVals.push(buf);
				}

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

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

	private registerSessionListeners(): void {
I
isidor 已提交
237
		this.toDisposeOnSessionEnd.push(this.session);
238
		this.toDisposeOnSessionEnd.push(this.session.onDidInitialize(event => {
239
			aria.status(nls.localize('debuggingStarted', "Debugging started."));
I
isidor 已提交
240
			this.sendAllBreakpoints().then(() => {
I
isidor 已提交
241
				if (this.session.configuration.capabilities.supportsConfigurationDoneRequest) {
I
isidor 已提交
242 243
					this.session.configurationDone().done(null, errors.onUnexpectedError);
				}
I
isidor 已提交
244 245
			});
		}));
E
Erich Gamma 已提交
246

247
		this.toDisposeOnSessionEnd.push(this.session.onDidStop(event => {
248
			this.setStateAndEmit(debug.State.Stopped);
I
isidor 已提交
249
			const threadId = event.body.threadId;
E
Erich Gamma 已提交
250

I
isidor 已提交
251
			this.getThreadData().done(() => {
252
				this.model.rawUpdate({
I
isidor 已提交
253
					threadId,
254 255 256
					stoppedDetails: event.body,
					allThreadsStopped: event.body.allThreadsStopped
				});
257

258 259
				const thread = this.model.getThreads()[threadId];
				thread.getCallStack(this).then(callStack => {
E
Erich Gamma 已提交
260
					if (callStack.length > 0) {
261
						// focus first stack frame from top that has source location
I
isidor 已提交
262
						const stackFrameToFocus = arrays.first(callStack, sf => sf.source && sf.source.available, callStack[0]);
263
						this.setFocusedStackFrameAndEvaluate(stackFrameToFocus, thread).done(null, errors.onUnexpectedError);
I
isidor 已提交
264
						this.windowService.getWindow().focus();
265 266
						aria.alert(nls.localize('debuggingPaused', "Debugging paused, reason {0}, {1} {2}", event.body.reason, stackFrameToFocus.source ? stackFrameToFocus.source.name : '', stackFrameToFocus.lineNumber));

267
						return this.openOrRevealSource(stackFrameToFocus.source, stackFrameToFocus.lineNumber, false, false);
E
Erich Gamma 已提交
268
					} else {
269
						this.setFocusedStackFrameAndEvaluate(null, thread).done(null, errors.onUnexpectedError);
E
Erich Gamma 已提交
270 271 272 273 274
					}
				});
			}, errors.onUnexpectedError);
		}));

275
		this.toDisposeOnSessionEnd.push(this.session.onDidThread(event => {
E
Erich Gamma 已提交
276
			if (event.body.reason === 'started') {
I
isidor 已提交
277
				this.getThreadData().done(null, errors.onUnexpectedError);
E
Erich Gamma 已提交
278 279 280 281 282
			} else if (event.body.reason === 'exited') {
				this.model.clearThreads(true, event.body.threadId);
			}
		}));

283
		this.toDisposeOnSessionEnd.push(this.session.onDidTerminateDebugee(event => {
284
			aria.status(nls.localize('debuggingStopped', "Debugging stopped."));
285
			if (this.session && this.session.getId() === event.body.sessionId) {
286
				if (event.body && typeof event.body.restart === 'boolean' && event.body.restart) {
287
					this.restartSession().done(null, err => this.messageService.show(severity.Error, err.message));
288 289 290
				} else {
					this.session.disconnect().done(null, errors.onUnexpectedError);
				}
E
Erich Gamma 已提交
291 292 293
			}
		}));

294
		this.toDisposeOnSessionEnd.push(this.session.onDidOutput(event => {
295
			if (event.body && event.body.category === 'telemetry') {
296
				// only log telemetry events from debug adapter if the adapter provided the telemetry key
I
isidor 已提交
297
				// and the user opted in telemetry
298 299
				if (this.customTelemetryService && this.telemetryService.isOptedIn) {
					this.customTelemetryService.publicLog(event.body.output, event.body.data);
300
				}
301
			} else if (event.body && typeof event.body.output === 'string' && event.body.output.length > 0) {
E
Erich Gamma 已提交
302 303 304 305
				this.onOutput(event);
			}
		}));

306
		this.toDisposeOnSessionEnd.push(this.session.onDidBreakpoint(event => {
I
isidor 已提交
307
			const id = event.body && event.body.breakpoint ? event.body.breakpoint.id : undefined;
308
			const breakpoint = this.model.getBreakpoints().filter(bp => bp.idFromAdapter === id).pop();
I
isidor 已提交
309 310 311
			if (breakpoint) {
				this.model.updateBreakpoints({ [breakpoint.getId()]: event.body.breakpoint });
			} else {
312
				const functionBreakpoint = this.model.getFunctionBreakpoints().filter(bp => bp.idFromAdapter === id).pop();
I
isidor 已提交
313 314 315 316 317 318
				if (functionBreakpoint) {
					this.model.updateFunctionBreakpoints({ [functionBreakpoint.getId()]: event.body.breakpoint });
				}
			}
		}));

319
		this.toDisposeOnSessionEnd.push(this.session.onDidExitAdapter(event => {
320
			// 'Run without debugging' mode VSCode must terminate the extension host. More details: #3905
I
isidor 已提交
321
			if (this.session.configuration.type === 'extensionHost' && this._state === debug.State.RunningNoDebug) {
322 323
				ipc.send('vscode:closeExtensionHostWindow', this.contextService.getWorkspace().resource.fsPath);
			}
324
			if (this.session && this.session.getId() === event.body.sessionId) {
325 326
				this.onSessionEnd();
			}
E
Erich Gamma 已提交
327 328 329 330
		}));
	}

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

I
isidor 已提交
335 336 337 338
	private getThreadData(): TPromise<void> {
		return this.session.threads().then(response => {
			response.body.threads.forEach(thread => this.model.rawUpdate({ threadId: thread.id, thread }));
		});
E
Erich Gamma 已提交
339 340 341
	}

	private loadBreakpoints(): debug.IBreakpoint[] {
342
		let result: debug.IBreakpoint[];
E
Erich Gamma 已提交
343
		try {
344
			result = JSON.parse(this.storageService.get(DEBUG_BREAKPOINTS_KEY, StorageScope.WORKSPACE, '[]')).map((breakpoint: any) => {
345
				return new model.Breakpoint(new Source(breakpoint.source.raw ? breakpoint.source.raw : { path: uri.parse(breakpoint.source.uri).fsPath, name: breakpoint.source.name }),
346
					breakpoint.desiredLineNumber || breakpoint.lineNumber, breakpoint.enabled, breakpoint.condition);
E
Erich Gamma 已提交
347
			});
348 349 350
		} catch (e) { }

		return result || [];
E
Erich Gamma 已提交
351 352
	}

I
isidor 已提交
353
	private loadFunctionBreakpoints(): debug.IFunctionBreakpoint[] {
354
		let result: debug.IFunctionBreakpoint[];
I
isidor 已提交
355
		try {
356
			result = JSON.parse(this.storageService.get(DEBUG_FUNCTION_BREAKPOINTS_KEY, StorageScope.WORKSPACE, '[]')).map((fb: any) => {
357
				return new model.FunctionBreakpoint(fb.name, fb.enabled);
I
isidor 已提交
358
			});
359 360 361
		} catch (e) { }

		return result || [];
I
isidor 已提交
362 363
	}

E
Erich Gamma 已提交
364
	private loadExceptionBreakpoints(): debug.IExceptionBreakpoint[] {
365
		let result: debug.IExceptionBreakpoint[];
E
Erich Gamma 已提交
366 367
		try {
			result = JSON.parse(this.storageService.get(DEBUG_EXCEPTION_BREAKPOINTS_KEY, StorageScope.WORKSPACE, '[]')).map((exBreakpoint: any) => {
368
				return new model.ExceptionBreakpoint(exBreakpoint.filter || exBreakpoint.name, exBreakpoint.label, exBreakpoint.enabled);
E
Erich Gamma 已提交
369
			});
370
		} catch (e) { }
E
Erich Gamma 已提交
371

372
		return result || [];
E
Erich Gamma 已提交
373 374 375
	}

	private loadWatchExpressions(): model.Expression[] {
376
		let result: model.Expression[];
E
Erich Gamma 已提交
377
		try {
378
			result = JSON.parse(this.storageService.get(DEBUG_WATCH_EXPRESSIONS_KEY, StorageScope.WORKSPACE, '[]')).map((watch: any) => {
E
Erich Gamma 已提交
379 380
				return new model.Expression(watch.name, false, watch.id);
			});
381 382 383
		} catch (e) { }

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

I
isidor 已提交
386 387
	public get state(): debug.State {
		return this._state;
E
Erich Gamma 已提交
388 389
	}

390 391 392 393
	public get onDidChangeState(): Event<debug.State> {
		return this._onDidChangeState.event;
	}

394
	private setStateAndEmit(newState: debug.State): void {
I
isidor 已提交
395
		this._state = newState;
396
		this._onDidChangeState.fire(newState);
E
Erich Gamma 已提交
397 398 399 400 401 402
	}

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

403 404 405 406 407 408
	public setFocusedStackFrameAndEvaluate(focusedStackFrame: debug.IStackFrame, thread?: debug.IThread): TPromise<void> {
		if (!thread && focusedStackFrame) {
			thread = this.model.getThreads()[focusedStackFrame.threadId];
		}

		this.viewModel.setFocusedStackFrame(focusedStackFrame, thread);
E
Erich Gamma 已提交
409
		if (focusedStackFrame) {
I
isidor 已提交
410
			return this.model.evaluateWatchExpressions(this.session, focusedStackFrame);
E
Erich Gamma 已提交
411 412
		} else {
			this.model.clearWatchExpressionValues();
I
isidor 已提交
413
			return TPromise.as(null);
E
Erich Gamma 已提交
414 415 416
		}
	}

I
isidor 已提交
417
	public enableOrDisableBreakpoints(enable: boolean, breakpoint?: debug.IEnablement): TPromise<void> {
418 419 420
		if (breakpoint) {
			this.model.setEnablement(breakpoint, enable);
			if (breakpoint instanceof model.Breakpoint) {
I
isidor 已提交
421
				return this.sendBreakpoints((<model.Breakpoint>breakpoint).source.uri);
422 423 424
			} else if (breakpoint instanceof model.FunctionBreakpoint) {
				return this.sendFunctionBreakpoints();
			}
E
Erich Gamma 已提交
425

426
			return this.sendExceptionBreakpoints();
E
Erich Gamma 已提交
427 428
		}

429 430
		this.model.enableOrDisableAllBreakpoints(enable);
		return this.sendAllBreakpoints();
E
Erich Gamma 已提交
431 432
	}

433 434 435
	public addBreakpoints(rawBreakpoints: debug.IRawBreakpoint[]): TPromise<void[]> {
		this.model.addBreakpoints(rawBreakpoints);
		const uris = arrays.distinct(rawBreakpoints, raw => raw.uri.toString()).map(raw => raw.uri);
436
		rawBreakpoints.forEach(rbp => aria.status(nls.localize('breakpointAdded', "Added breakpoint, line {0}, file {1}", rbp.lineNumber, rbp.uri.fsPath)));
437 438 439 440

		return TPromise.join(uris.map(uri => this.sendBreakpoints(uri)));
	}

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

446
		this.model.removeBreakpoints(toRemove);
I
isidor 已提交
447
		return TPromise.join(urisToClear.map(uri => this.sendBreakpoints(uri)));
E
Erich Gamma 已提交
448 449
	}

450 451
	public setBreakpointsActivated(activated: boolean): TPromise<void> {
		this.model.setBreakpointsActivated(activated);
E
Erich Gamma 已提交
452 453 454
		return this.sendAllBreakpoints();
	}

I
isidor 已提交
455 456
	public addFunctionBreakpoint(): void {
		this.model.addFunctionBreakpoint('');
457 458
	}

I
isidor 已提交
459
	public renameFunctionBreakpoint(id: string, newFunctionName: string): TPromise<void> {
I
isidor 已提交
460
		this.model.updateFunctionBreakpoints({ [id]: { name: newFunctionName } });
I
isidor 已提交
461
		return this.sendFunctionBreakpoints();
I
isidor 已提交
462 463
	}

I
isidor 已提交
464
	public removeFunctionBreakpoints(id?: string): TPromise<void> {
465
		this.model.removeFunctionBreakpoints(id);
I
isidor 已提交
466
		return this.sendFunctionBreakpoints();
I
isidor 已提交
467 468
	}

I
isidor 已提交
469
	public addReplExpression(name: string): TPromise<void> {
P
Pierson Lee 已提交
470
		this.telemetryService.publicLog('debugService/addReplExpression');
471 472 473 474
		const focussedStackFrame = this.viewModel.getFocusedStackFrame();
		return this.model.addReplExpression(this.session, focussedStackFrame, name)
			// Evaluate all watch expressions again since repl evaluation might have changed some.
			.then(() => this.setFocusedStackFrameAndEvaluate(focussedStackFrame));
E
Erich Gamma 已提交
475 476
	}

477
	public logToRepl(value: string | { [key: string]: any }, severity?: severity): void {
E
Erich Gamma 已提交
478 479 480 481 482 483 484
		this.model.logToRepl(value, severity);
	}

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

485 486
	public removeReplExpressions(): void {
		this.model.removeReplExpressions();
E
Erich Gamma 已提交
487 488
	}

I
isidor 已提交
489 490 491 492 493 494 495 496 497
	public setVariable(variable: debug.IExpression, value: string): TPromise<any> {
		if (!this.session || !(variable instanceof model.Variable)) {
			return TPromise.as(null);
		}

		return this.session.setVarialbe({
			name: variable.name,
			value,
			variablesReference: (<model.Variable>variable).parent.reference
498
		}).then(response => variable.value = response.body.value, err => (<model.Variable>variable).errorMessage = err.message);
I
isidor 已提交
499 500
	}

I
isidor 已提交
501
	public addWatchExpression(name: string): TPromise<void> {
E
Erich Gamma 已提交
502 503 504
		return this.model.addWatchExpression(this.session, this.viewModel.getFocusedStackFrame(), name);
	}

I
isidor 已提交
505
	public renameWatchExpression(id: string, newName: string): TPromise<void> {
E
Erich Gamma 已提交
506 507 508
		return this.model.renameWatchExpression(this.session, this.viewModel.getFocusedStackFrame(), id, newName);
	}

509 510
	public removeWatchExpressions(id?: string): void {
		this.model.removeWatchExpressions(id);
E
Erich Gamma 已提交
511 512
	}

I
isidor 已提交
513
	public createSession(noDebug: boolean, configuration?: debug.IConfig): TPromise<any> {
514
		this.removeReplExpressions();
E
Erich Gamma 已提交
515

516
		return this.textFileService.saveAll()							// make sure all dirty files are saved
I
isidor 已提交
517
			.then(() => this.configurationService.loadConfiguration()	// make sure configuration is up to date
518
			.then(() => this.extensionService.onReady()
519
			.then(() => this.configurationManager.setConfiguration(configuration || this.configurationManager.configurationName)
520 521 522
			.then(() => this.configurationManager.resolveInteractiveVariables())
			.then(resolvedConfiguration => {
				configuration = resolvedConfiguration;
523 524 525 526 527 528 529
				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."));
						}
					});
				}
530 531 532
				if (configuration.silentlyAbort) {
					return;
				}
E
Erich Gamma 已提交
533

534 535 536
				configuration.noDebug = noDebug;
				if (!this.configurationManager.adapter) {
					return configuration.type ? TPromise.wrapError(new Error(nls.localize('debugTypeNotSupported', "Configured debug type '{0}' is not supported.", configuration.type)))
I
isidor 已提交
537
						: TPromise.wrapError(errors.create(nls.localize('debugTypeMissing', "Missing property 'type' for the chosen launch configuration."),
538
							{ actions: [CloseAction, this.instantiationService.createInstance(debugactions.ConfigureAction, debugactions.ConfigureAction.ID, debugactions.ConfigureAction.LABEL)] }));
539
				}
I
isidor 已提交
540

541 542 543 544 545
				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 已提交
546
						return this.doCreateSession(configuration);
547 548 549 550 551 552 553 554
					}

					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: [CloseAction, new Action('debug.continue', nls.localize('debugAnyway', "Debug Anyway"), null, true, () => {
							this.messageService.hideAll();
I
isidor 已提交
555
							return this.doCreateSession(configuration);
556 557 558 559 560 561
						})]
					});
				}, (err: TaskError) => {
					if (err.code !== TaskErrors.NotConfigured) {
						throw err;
					}
562

563 564 565 566
					this.messageService.show(err.severity, {
						message: err.message,
						actions: [CloseAction, this.taskService.configureAction()]
					});
567
				});
568
			}))));
I
isidor 已提交
569
	}
E
Erich Gamma 已提交
570

I
isidor 已提交
571
	private doCreateSession(configuration: debug.IExtHostConfig): TPromise<any> {
572
		this.setStateAndEmit(debug.State.Initializing);
573

J
Joao Moreno 已提交
574
		return this.telemetryService.getTelemetryInfo().then(info => {
575
			const telemetryInfo: { [key: string]: string } = Object.create(null);
576 577
			telemetryInfo['common.vscodemachineid'] = info.machineId;
			telemetryInfo['common.vscodesessionid'] = info.sessionId;
578 579
			return telemetryInfo;
		}).then(data => {
580
			const { aiKey, type } = this.configurationManager.adapter;
581
			const publisher = this.configurationManager.adapter.extensionDescription.publisher;
582 583 584 585 586 587 588 589
			this.customTelemetryService = null;

			if (aiKey) {
				const client = new Client(
					uri.parse(require.toUrl('bootstrap')).fsPath,
					{
						serverName: 'Debug Telemetry',
						timeout: 1000 * 60 * 5,
590
						args: [`${ publisher }.${ type }`, JSON.stringify(data), aiKey],
591
						env: {
592
							ELECTRON_RUN_AS_NODE: 1,
593 594 595
							PIPE_LOGGING: 'true',
							AMD_ENTRYPOINT: 'vs/workbench/parts/debug/node/telemetryApp'
						}
596
					}
597
				);
598

599 600
				const channel = client.getChannel('telemetryAppender');
				const appender = new TelemetryAppenderClient(channel);
601

602 603
				this.toDisposeOnSessionEnd.push(client);
				this.customTelemetryService = new TelemetryService({ appender }, this.configurationService);
604 605
			}

606
			this.session = this.instantiationService.createInstance(session.RawDebugSession, configuration.debugServer, this.configurationManager.adapter, this.customTelemetryService);
J
Joao Moreno 已提交
607 608 609 610 611 612 613 614 615 616 617
			this.registerSessionListeners();

			return this.session.initialize({
				adapterID: configuration.type,
				pathFormat: 'path',
				linesStartAt1: true,
				columnsStartAt1: true
			}).then((result: DebugProtocol.InitializeResponse) => {
				if (!this.session) {
					return TPromise.wrapError(new Error(nls.localize('debugAdapterCrash', "Debug adapter process has terminated unexpectedly")));
				}
618

J
Joao Moreno 已提交
619 620 621 622 623 624
				this.model.setExceptionBreakpoints(this.session.configuration.capabilities.exceptionBreakpointFilters);
				return configuration.request === 'attach' ? this.session.attach(configuration) : this.session.launch(configuration);
			}).then((result: DebugProtocol.Response) => {
				if (configuration.internalConsoleOptions === 'openOnSessionStart' || (!this.viewModel.changedWorkbenchViewState && configuration.internalConsoleOptions !== 'neverOpen')) {
					this.panelService.openPanel(debug.REPL_ID, false).done(undefined, errors.onUnexpectedError);
				}
625

J
Joao Moreno 已提交
626 627 628 629 630
				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);
				}
631

J
Joao Moreno 已提交
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
				// 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.contextService.updateOptions('editor', {
					glyphMargin: true
				});
				this.inDebugMode.set(true);
				this.lazyTransitionToRunningState();

				this.telemetryService.publicLog('debugSessionStart', {
					type: configuration.type,
					breakpointCount: this.model.getBreakpoints().length,
					exceptionBreakpoints: this.model.getExceptionBreakpoints(),
					watchExpressionsCount: this.model.getWatchExpressions().length,
					extensionName: `${ this.configurationManager.adapter.extensionDescription.publisher }.${ this.configurationManager.adapter.extensionDescription.name }`,
					isBuiltin: this.configurationManager.adapter.extensionDescription.isBuiltin
				});
			}).then(undefined, (error: any) => {
652 653 654 655 656
				if (error instanceof Error && error.message === 'Canceled') {
					// Do not show 'canceled' error messages to the user #7906
					return TPromise.as(null);
				}

J
Joao Moreno 已提交
657 658 659 660 661 662 663 664 665
				this.telemetryService.publicLog('debugMisconfiguration', { type: configuration ? configuration.type : undefined });
				this.setStateAndEmit(debug.State.Inactive);
				if (this.session) {
					this.session.disconnect();
				}
				// 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 已提交
666

J
Joao Moreno 已提交
667 668 669 670
				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 已提交
671 672 673
		});
	}

I
isidor 已提交
674
	private runPreLaunchTask(taskName: string): TPromise<ITaskSummary> {
675
		if (!taskName) {
I
isidor 已提交
676
			return TPromise.as(null);
E
Erich Gamma 已提交
677 678
		}

679
		// run a task before starting a debug session
E
Erich Gamma 已提交
680
		return this.taskService.tasks().then(descriptions => {
681
			const filteredTasks = descriptions.filter(task => task.name === taskName);
E
Erich Gamma 已提交
682
			if (filteredTasks.length !== 1) {
683 684 685 686 687 688 689
				return TPromise.wrapError(errors.create(nls.localize('DebugTaskNotFound', "Could not find the preLaunchTask \'{0}\'.", taskName), {
					actions: [
						CloseAction,
						this.taskService.configureAction(),
						this.instantiationService.createInstance(debugactions.ConfigureAction, debugactions.ConfigureAction.ID, debugactions.ConfigureAction.LABEL)
					]
				}));
E
Erich Gamma 已提交
690 691
			}

I
isidor 已提交
692
			// task is already running - nothing to do.
693
			if (this.lastTaskEvent && this.lastTaskEvent.taskName === taskName) {
I
isidor 已提交
694
				return TPromise.as(null);
E
Erich Gamma 已提交
695 696 697
			}

			if (this.lastTaskEvent) {
I
isidor 已提交
698
				// there is a different task running currently.
I
isidor 已提交
699
				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 已提交
700 701
			}

I
isidor 已提交
702
			// no task running, execute the preLaunchTask.
703
			const taskPromise = this.taskService.run(filteredTasks[0].id).then(result => {
E
Erich Gamma 已提交
704
				this.lastTaskEvent = null;
I
isidor 已提交
705
				return result;
E
Erich Gamma 已提交
706 707 708
			}, err => {
				this.lastTaskEvent = null;
			});
709

710
			if (filteredTasks[0].isWatching) {
A
Alex Dima 已提交
711
				return new TPromise((c, e) => this.taskService.addOneTimeDisposableListener(TaskServiceEvents.Inactive, () => c(null)));
712 713 714
			}

			return taskPromise;
E
Erich Gamma 已提交
715 716 717
		});
	}

I
isidor 已提交
718
	private rawAttach(port: number): TPromise<any> {
I
isidor 已提交
719
		if (this.session) {
I
isidor 已提交
720
			if (!this.session.configuration.isAttach) {
721 722 723 724
				return this.session.attach({ port });
			}

			this.session.disconnect().done(null, errors.onUnexpectedError);
I
isidor 已提交
725 726
		}

I
isidor 已提交
727
		this.setStateAndEmit(debug.State.Initializing);
I
isidor 已提交
728
		const configuration = <debug.IExtHostConfig>this.configurationManager.configuration;
I
isidor 已提交
729
		return this.doCreateSession({
730
			type: configuration.type,
I
isidor 已提交
731
			request: 'attach',
732 733
			port,
			sourceMaps: configuration.sourceMaps,
734 735
			outDir: configuration.outDir,
			debugServer: configuration.debugServer
I
isidor 已提交
736
		});
I
isidor 已提交
737 738
	}

I
isidor 已提交
739
	public restartSession(): TPromise<any> {
I
isidor 已提交
740
		return this.session ? this.session.disconnect(true).then(() =>
741
			new TPromise<void>((c, e) => {
E
Erich Gamma 已提交
742
				setTimeout(() => {
I
isidor 已提交
743
					this.createSession(false, null).then(() => c(null), err => e(err));
E
Erich Gamma 已提交
744
				}, 300);
I
isidor 已提交
745
			})
I
isidor 已提交
746
		) : this.createSession(false, null);
E
Erich Gamma 已提交
747 748 749 750 751 752 753 754
	}

	public getActiveSession(): debug.IRawDebugSession {
		return this.session;
	}

	private onSessionEnd(): void {
		if (this.session) {
I
isidor 已提交
755
			const bpsExist = this.model.getBreakpoints().length > 0;
I
isidor 已提交
756
			this.telemetryService.publicLog('debugSessionStop', {
I
isidor 已提交
757
				type: this.session.configuration.type,
I
isidor 已提交
758 759 760 761 762
				success: this.session.emittedStopped || !bpsExist,
				sessionLengthInSeconds: this.session.getLengthInSeconds(),
				breakpointCount: this.model.getBreakpoints().length,
				watchExpressionsCount: this.model.getWatchExpressions().length
			});
E
Erich Gamma 已提交
763
		}
764

E
Erich Gamma 已提交
765
		this.session = null;
I
isidor 已提交
766 767 768 769 770 771
		try {
			this.toDisposeOnSessionEnd = lifecycle.dispose(this.toDisposeOnSessionEnd);
		} catch (e) {
			// an internal module might be open so the dispose can throw -> ignore and continue with stop session.
		}

E
Erich Gamma 已提交
772
		this.partService.removeClass('debugging');
773

774
		this.model.clearThreads(true);
I
isidor 已提交
775
		this.setFocusedStackFrameAndEvaluate(null).done(null, errors.onUnexpectedError);
776 777
		this.setStateAndEmit(debug.State.Inactive);

I
isidor 已提交
778
		// set breakpoints back to unverified since the session ended.
779
		// source reference changes across sessions, so we do not use it to persist the source.
I
isidor 已提交
780
		const data: { [id: string]: { line: number, verified: boolean } } = {};
781 782 783 784
		this.model.getBreakpoints().forEach(bp => {
			delete bp.source.raw.sourceReference;
			data[bp.getId()] = { line: bp.lineNumber, verified: false };
		});
785 786
		this.model.updateBreakpoints(data);

E
Erich Gamma 已提交
787 788 789 790 791 792 793 794 795 796 797
		this.inDebugMode.reset();
	}

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

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

798
	public openOrRevealSource(source: Source, lineNumber: number, preserveFocus: boolean, sideBySide: boolean): TPromise<any> {
E
Erich Gamma 已提交
799
		const visibleEditors = this.editorService.getVisibleEditors();
I
isidor 已提交
800
		for (let i = 0; i < visibleEditors.length; i++) {
801
			const fileInput = wbeditorcommon.asFileEditorInput(visibleEditors[i].input);
802 803 804
			if ((fileInput && fileInput.getResource().toString() === source.uri.toString()) ||
				(visibleEditors[i].input instanceof DebugStringEditorInput && (<DebugStringEditorInput>visibleEditors[i].input).getResource().toString() === source.uri.toString())) {

805 806 807 808
				const control = <editorbrowser.ICodeEditor>visibleEditors[i].getControl();
				if (control) {
					control.revealLineInCenterIfOutsideViewport(lineNumber);
					control.setSelection({ startLineNumber: lineNumber, startColumn: 1, endLineNumber: lineNumber, endColumn: 1 });
809
					this.editorGroupService.activateGroup(i);
I
isidor 已提交
810
					if (!preserveFocus) {
811
						this.editorGroupService.focusGroup(i);
I
isidor 已提交
812
					}
E
Erich Gamma 已提交
813
				}
814

A
Alex Dima 已提交
815
				return TPromise.as(null);
E
Erich Gamma 已提交
816 817 818 819
			}
		}

		if (source.inMemory) {
I
isidor 已提交
820
			// internal module
E
Erich Gamma 已提交
821
			if (source.reference !== 0 && this.session) {
822
				return this.session.source({ sourceReference: source.reference }).then(response => {
823
					const mime = response.body.mimeType ? response.body.mimeType : guessMimeTypes(source.name)[0];
824 825 826 827 828
					return this.getDebugStringEditorInput(source, response.body.content, mime);
				}, (err: DebugProtocol.ErrorResponse) => {
					// Display the error from debug adapter using a temporary editor #8836
					return this.getDebugStringEditorInput(source, err.message, MIME_TEXT);
				}).then(editorInput => {
B
Benjamin Pasero 已提交
829
					return this.editorService.openEditor(editorInput, { preserveFocus, selection: { startLineNumber: lineNumber, startColumn: 1, endLineNumber: lineNumber, endColumn: 1 } }, sideBySide);
E
Erich Gamma 已提交
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
				});
			}

			return this.sourceIsUnavailable(source, sideBySide);
		}

		return this.fileService.resolveFile(source.uri).then(() =>
			this.editorService.openEditor({
				resource: source.uri,
				options: {
					selection: {
						startLineNumber: lineNumber,
						startColumn: 1,
						endLineNumber: lineNumber,
						endColumn: 1
					},
					preserveFocus: preserveFocus
				}
			}, sideBySide), err => this.sourceIsUnavailable(source, sideBySide)
		);
	}

I
isidor 已提交
852
	private sourceIsUnavailable(source: Source, sideBySide: boolean): TPromise<any> {
E
Erich Gamma 已提交
853
		this.model.sourceIsUnavailable(source);
854
		const editorInput = this.getDebugStringEditorInput(source, nls.localize('debugSourceNotAvailable', "Source {0} is not available.", source.uri.fsPath), 'text/plain');
E
Erich Gamma 已提交
855

856
		return this.editorService.openEditor(editorInput, { preserveFocus: true }, sideBySide);
E
Erich Gamma 已提交
857 858
	}

859 860
	public getConfigurationManager(): debug.IConfigurationManager {
		return this.configurationManager;
861 862
	}

863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
	public next(threadId: number): TPromise<void> {
		if (!this.session) {
			return TPromise.as(null);
		}

		return this.session.next({ threadId }).then(() => {
			this.lazyTransitionToRunningState(threadId);
		});
	}

	public stepIn(threadId: number): TPromise<void> {
		if (!this.session) {
			return TPromise.as(null);
		}

		return this.session.stepIn({ threadId }).then(() => {
			this.lazyTransitionToRunningState(threadId);
		});
	}

	public stepOut(threadId: number): TPromise<void> {
		if (!this.session) {
			return TPromise.as(null);
		}

		return this.session.stepOut({ threadId }).then(() => {
889 890 891 892 893 894 895 896 897 898
			this.lazyTransitionToRunningState(threadId);
		});
	}

	public stepBack(threadId: number): TPromise<void> {
		if (!this.session) {
			return TPromise.as(null);
		}

		return this.session.stepBack({ threadId }).then(() => {
899 900 901 902 903 904 905 906 907
			this.lazyTransitionToRunningState(threadId);
		});
	}

	public continue(threadId: number): TPromise<void> {
		if (!this.session) {
			return TPromise.as(null);
		}

I
isidor 已提交
908
		return this.session.continue({ threadId }).then(response => {
909
			const allThreadsContinued = response.body ? response.body.allThreadsContinued !== false : true;
I
isidor 已提交
910
			this.lazyTransitionToRunningState(allThreadsContinued ? undefined : threadId);
911 912 913 914 915 916 917 918
		});
	}

	public pause(threadId: number): TPromise<any> {
		if (!this.session) {
			return TPromise.as(null);
		}

I
isidor 已提交
919
		return this.session.pause({ threadId });
920 921
	}

922
	private lazyTransitionToRunningState(threadId?: number): void {
923
		let setNewFocusedStackFrameScheduler: RunOnceScheduler;
924
		const toDispose = this.session.onDidStop(e => {
925
			if (e.body.threadId === threadId || e.body.allThreadsStopped || !threadId) {
926
				setNewFocusedStackFrameScheduler.cancel();
927 928 929
			}
		});

I
isidor 已提交
930
		this.model.clearThreads(false, threadId);
931

932 933 934 935 936 937
		// Get a top stack frame of a stopped thread if there is any.
		const threads = this.model.getThreads();
		const stoppedReference = Object.keys(threads).filter(ref => threads[ref].stopped).pop();
		const stoppedThread = stoppedReference ? threads[parseInt(stoppedReference)] : null;
		const callStack = stoppedThread ? stoppedThread.getCachedCallStack() : null;
		const stackFrameToFocus = callStack && callStack.length > 0 ? callStack[0] : null;
938

939 940 941 942 943 944 945 946 947 948 949
		if (!stoppedThread) {
			this.setStateAndEmit(this.configurationManager.configuration.noDebug ? debug.State.RunningNoDebug : debug.State.Running);
		}

		// Do not immediatly set a new focused stack frame since that might cause unnecessery flickering
		// of the tree in the debug viewlet. Only set focused stack frame if no stopped event has arrived in 500ms.
		setNewFocusedStackFrameScheduler = new RunOnceScheduler(() => {
			toDispose.dispose();
			aria.status(nls.localize('debuggingContinued', "Debugging continued."));

			this.setFocusedStackFrameAndEvaluate(stackFrameToFocus).done(null, errors.onUnexpectedError);
950
		}, 500);
951
		setNewFocusedStackFrameScheduler.schedule();
952 953
	}

954
	private getDebugStringEditorInput(source: Source, value: string, mtype: string): DebugStringEditorInput {
955 956 957
		const result = this.instantiationService.createInstance(DebugStringEditorInput, source.name, source.uri, source.origin, value, mtype, void 0);
		this.toDisposeOnSessionEnd.push(result);
		return result;
E
Erich Gamma 已提交
958 959
	}

960
	private sendAllBreakpoints(): TPromise<any> {
I
isidor 已提交
961 962 963 964
		return TPromise.join(arrays.distinct(this.model.getBreakpoints(), bp => bp.source.uri.toString()).map(bp => this.sendBreakpoints(bp.source.uri)))
			.then(() => this.sendFunctionBreakpoints())
			// send exception breakpoints at the end since some debug adapters rely on the order
			.then(() => this.sendExceptionBreakpoints());
E
Erich Gamma 已提交
965 966
	}

967
	private sendBreakpoints(modelUri: uri, sourceModified = false): TPromise<void> {
968
		if (!this.session || !this.session.readyForBreakpoints) {
I
isidor 已提交
969
			return TPromise.as(null);
970
		}
971 972 973 974 975
		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);
		}
976

977 978
		const breakpointsToSend = arrays.distinct(
			this.model.getBreakpoints().filter(bp => this.model.areBreakpointsActivated() && bp.enabled && bp.source.uri.toString() === modelUri.toString()),
I
isidor 已提交
979
			bp => `${bp.desiredLineNumber}`
980
		);
I
isidor 已提交
981
		const rawSource = breakpointsToSend.length > 0 ? breakpointsToSend[0].source.raw : Source.toRawSource(modelUri, this.model);
982

I
isidor 已提交
983 984 985
		return this.session.setBreakpoints({
			source: rawSource,
			lines: breakpointsToSend.map(bp => bp.desiredLineNumber),
986 987 988
			breakpoints: breakpointsToSend.map(bp => ({ line: bp.desiredLineNumber, condition: bp.condition })),
			sourceModified
		}).then(response => {
I
isidor 已提交
989
			const data: { [id: string]: { line?: number, verified: boolean } } = {};
990 991 992 993 994
			for (let i = 0; i < breakpointsToSend.length; i++) {
				data[breakpointsToSend[i].getId()] = response.body.breakpoints[i];
			}

			this.model.updateBreakpoints(data);
E
Erich Gamma 已提交
995 996 997
		});
	}

I
isidor 已提交
998
	private sendFunctionBreakpoints(): TPromise<void> {
I
isidor 已提交
999
		if (!this.session || !this.session.readyForBreakpoints || !this.session.configuration.capabilities.supportsFunctionBreakpoints) {
I
isidor 已提交
1000 1001 1002
			return TPromise.as(null);
		}

I
isidor 已提交
1003
		const breakpointsToSend = this.model.getFunctionBreakpoints().filter(fbp => fbp.enabled && this.model.areBreakpointsActivated());
I
isidor 已提交
1004
		return this.session.setFunctionBreakpoints({ breakpoints: breakpointsToSend }).then(response => {
I
isidor 已提交
1005
			const data: { [id: string]: { name?: string, verified?: boolean } } = {};
I
isidor 已提交
1006 1007 1008 1009 1010
			for (let i = 0; i < breakpointsToSend.length; i++) {
				data[breakpointsToSend[i].getId()] = response.body.breakpoints[i];
			}

			this.model.updateFunctionBreakpoints(data);
I
isidor 已提交
1011 1012 1013
		});
	}

I
isidor 已提交
1014
	private sendExceptionBreakpoints(): TPromise<any> {
1015
		if (!this.session || !this.session.readyForBreakpoints || this.model.getExceptionBreakpoints().length === 0) {
I
isidor 已提交
1016
			return TPromise.as(null);
I
isidor 已提交
1017
		}
I
isidor 已提交
1018

I
isidor 已提交
1019
		const enabledExceptionBps = this.model.getExceptionBreakpoints().filter(exb => exb.enabled);
1020
		return this.session.setExceptionBreakpoints({ filters: enabledExceptionBps.map(exb => exb.filter) });
E
Erich Gamma 已提交
1021 1022 1023
	}

	private onFileChanges(fileChangesEvent: FileChangesEvent): void {
1024 1025
		this.model.removeBreakpoints(this.model.getBreakpoints().filter(bp =>
			fileChangesEvent.contains(bp.source.uri, FileChangeType.DELETED)));
1026 1027 1028 1029 1030 1031 1032 1033

		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 已提交
1034 1035 1036 1037 1038
	}

	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);
1039
		this.storageService.store(DEBUG_FUNCTION_BREAKPOINTS_KEY, JSON.stringify(this.model.getFunctionBreakpoints()), StorageScope.WORKSPACE);
E
Erich Gamma 已提交
1040
		this.storageService.store(DEBUG_EXCEPTION_BREAKPOINTS_KEY, JSON.stringify(this.model.getExceptionBreakpoints()), StorageScope.WORKSPACE);
1041
		this.storageService.store(DEBUG_SELECTED_CONFIG_NAME_KEY, this.configurationManager.configurationName, StorageScope.WORKSPACE);
E
Erich Gamma 已提交
1042 1043 1044 1045
		this.storageService.store(DEBUG_WATCH_EXPRESSIONS_KEY, JSON.stringify(this.model.getWatchExpressions()), StorageScope.WORKSPACE);
	}

	public dispose(): void {
J
Joao Moreno 已提交
1046
		this.toDisposeOnSessionEnd = lifecycle.dispose(this.toDisposeOnSessionEnd);
I
isidor 已提交
1047
		this.toDispose = lifecycle.dispose(this.toDispose);
E
Erich Gamma 已提交
1048 1049
	}
}