debugModel.ts 20.5 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
import { TPromise } from 'vs/base/common/winjs.base';
I
isidor 已提交
7
import strings = require('vs/base/common/strings');
E
Erich Gamma 已提交
8 9 10 11 12 13 14 15
import nls = require('vs/nls');
import lifecycle = require('vs/base/common/lifecycle');
import ee = require('vs/base/common/eventEmitter');
import uuid = require('vs/base/common/uuid');
import severity from 'vs/base/common/severity';
import types = require('vs/base/common/types');
import arrays = require('vs/base/common/arrays');
import debug = require('vs/workbench/parts/debug/common/debug');
I
isidor 已提交
16
import { Source } from 'vs/workbench/parts/debug/common/debugSource';
E
Erich Gamma 已提交
17 18

function resolveChildren(debugService: debug.IDebugService, parent: debug.IExpressionContainer): TPromise<Variable[]> {
I
isidor 已提交
19 20
	const session = debugService.getActiveSession();
	// only variables with reference > 0 have children.
E
Erich Gamma 已提交
21 22 23 24
	if (!session || parent.reference <= 0) {
		return TPromise.as([]);
	}

25
	return session.variables({ variablesReference: parent.reference }).then(response => {
E
Erich Gamma 已提交
26 27 28
		return arrays.distinct(response.body.variables, v => v.name).map(
			v => new Variable(parent, v.variablesReference, v.name, v.value)
		);
29
	}, (e: Error) => [new Variable(parent, 0, null, e.message, false)]);
E
Erich Gamma 已提交
30 31 32 33 34 35
}

function massageValue(value: string): string {
	return value ? value.replace(/\n/g, '\\n').replace(/\r/g, '\\r').replace(/\t/g, '\\t') : value;
}

I
isidor 已提交
36 37 38 39 40
export function evaluateExpression(session: debug.IRawDebugSession, stackFrame: debug.IStackFrame, expression: Expression, context: string): TPromise<Expression> {
	if (!session) {
		expression.value = context === 'repl' ? nls.localize('startDebugFirst', "Please start a debug session to evaluate") : Expression.DEFAULT_VALUE;
		expression.available = false;
		expression.reference = 0;
A
Alex Dima 已提交
41
		return TPromise.as(expression);
I
isidor 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
	}

	return session.evaluate({
		expression: expression.name,
		frameId: stackFrame ? stackFrame.frameId : undefined,
		context
	}).then(response => {
		expression.value = response.body.result;
		expression.available = true;
		expression.reference = response.body.variablesReference;

		return expression;
	}, err => {
		expression.value = err.message;
		expression.available = false;
		expression.reference = 0;

		return expression;
	});
}

I
isidor 已提交
63 64
const notPropertySyntax = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
const arrayElementSyntax = /\[.*\]$/;
I
isidor 已提交
65

66 67 68
export function getFullExpressionName(expression: debug.IExpression, sessionType: string): string {
	let names = [expression.name];
	if (expression instanceof Variable) {
I
isidor 已提交
69
		let v = (<Variable> expression).parent;
70 71 72 73 74 75 76 77 78 79 80
		while (v instanceof Variable || v instanceof Expression) {
			names.push((<Variable> v).name);
			v = (<Variable> v).parent;
		}
	}
	names = names.reverse();

	let result = null;
	names.forEach(name => {
		if (!result) {
			result = name;
I
isidor 已提交
81
		} else if (arrayElementSyntax.test(name) || (sessionType === 'node' && !notPropertySyntax.test(name))) {
I
isidor 已提交
82
			// use safe way to access node properties a['property_name']. Also handles array elements.
83 84 85 86 87 88 89 90 91
			result = name && name.indexOf('[') === 0 ? `${ result }${ name }` : `${ result }['${ name }']`;
		} else {
			result = `${ result }.${ name }`;
		}
	});

	return result;
}

E
Erich Gamma 已提交
92 93
export class Thread implements debug.IThread {

I
isidor 已提交
94
	public stoppedDetails: debug.IRawStoppedDetails;
E
Erich Gamma 已提交
95 96

	constructor(public name: string, public threadId, public callStack: debug.IStackFrame[]) {
I
isidor 已提交
97
		this.stoppedDetails = undefined;
E
Erich Gamma 已提交
98 99 100 101 102 103 104 105 106
	}

	public getId(): string {
		return `thread:${ this.name }:${ this.threadId }`;
	}
}

export class OutputElement implements debug.ITreeElement {

I
isidor 已提交
107 108
	constructor(private id = uuid.generateUuid()) {
		// noop
E
Erich Gamma 已提交
109 110 111 112 113 114 115 116 117
	}

	public getId(): string {
		return this.id;
	}
}

export class ValueOutputElement extends OutputElement {

I
isidor 已提交
118 119
	constructor(public value: string, public severity: severity, public category?: string, public counter:number = 1) {
		super();
E
Erich Gamma 已提交
120 121 122 123 124 125 126 127 128 129
	}
}

export class KeyValueOutputElement extends OutputElement {

	private static MAX_CHILDREN = 1000; // upper bound of children per value

	private children: debug.ITreeElement[];
	private _valueName: string;

I
isidor 已提交
130 131
	constructor(public key: string, public valueObj: any, public annotation?: string) {
		super();
E
Erich Gamma 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

		this._valueName = null;
	}

	public get value(): string {
		if (this._valueName === null) {
			if (this.valueObj === null) {
				this._valueName = 'null';
			} else if (Array.isArray(this.valueObj)) {
				this._valueName = `Array[${this.valueObj.length}]`;
			} else if (types.isObject(this.valueObj)) {
				this._valueName = 'Object';
			} else if (types.isString(this.valueObj)) {
				this._valueName = `"${massageValue(this.valueObj)}"`;
			} else {
				this._valueName = String(this.valueObj);
			}

			if (!this._valueName) {
				this._valueName = '';
			}
		}

		return this._valueName;
	}

	public getChildren(): debug.ITreeElement[] {
		if (!this.children) {
			if (Array.isArray(this.valueObj)) {
I
isidor 已提交
161
				this.children = (<any[]>this.valueObj).slice(0, KeyValueOutputElement.MAX_CHILDREN).map((v, index) => new KeyValueOutputElement(String(index), v, null));
E
Erich Gamma 已提交
162
			} else if (types.isObject(this.valueObj)) {
I
isidor 已提交
163
				this.children = Object.getOwnPropertyNames(this.valueObj).slice(0, KeyValueOutputElement.MAX_CHILDREN).map(key => new KeyValueOutputElement(key, this.valueObj[key], null));
E
Erich Gamma 已提交
164 165 166 167 168 169 170 171 172
			} else {
				this.children = [];
			}
		}

		return this.children;
	}
}

173
export class ExpressionContainer implements debug.IExpressionContainer {
E
Erich Gamma 已提交
174 175

	private children: TPromise<debug.IExpression[]>;
176 177
	public valueChanged: boolean;
	public static allValues: { [id: string]: string } = {};
E
Erich Gamma 已提交
178

179
	constructor(public reference: number, private id: string, private cacheChildren: boolean) {
E
Erich Gamma 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192
		this.children = null;
	}

	public getChildren(debugService: debug.IDebugService): TPromise<debug.IExpression[]> {
		if (!this.cacheChildren) {
			return resolveChildren(debugService, this);
		}
		if (!this.children) {
			this.children = resolveChildren(debugService, this);
		}

		return this.children;
	}
193 194 195 196 197

	public getId(): string {
		return this.id;
	}

E
Erich Gamma 已提交
198 199
}

200 201
export class Expression extends ExpressionContainer implements debug.IExpression {
	static DEFAULT_VALUE = 'not available';
E
Erich Gamma 已提交
202

203 204
	public available: boolean;
	private _value: string;
E
Erich Gamma 已提交
205

206 207 208 209
	constructor(public name: string, cacheChildren: boolean, id = uuid.generateUuid()) {
		super(0, id, cacheChildren);
		this.value = Expression.DEFAULT_VALUE;
		this.available = false;
E
Erich Gamma 已提交
210 211
	}

212 213
	public get value(): string {
		return this._value;
E
Erich Gamma 已提交
214 215
	}

216 217 218 219 220 221 222
	public set value(value: string) {
		this._value = massageValue(value);
		this.valueChanged = ExpressionContainer.allValues[this.getId()] &&
			ExpressionContainer.allValues[this.getId()] !== Expression.DEFAULT_VALUE && ExpressionContainer.allValues[this.getId()] !== value;
		ExpressionContainer.allValues[this.getId()] = value;
	}
}
E
Erich Gamma 已提交
223

224 225 226 227 228 229 230 231 232
export class Variable extends ExpressionContainer implements debug.IExpression {

	public value: string;

	constructor(public parent: debug.IExpressionContainer, reference: number, public name: string, value: string, public available = true) {
		super(reference, `variable:${ parent.getId() }:${ name }`, true);
		this.value = massageValue(value);
		this.valueChanged = ExpressionContainer.allValues[this.getId()] && ExpressionContainer.allValues[this.getId()] !== value;
		ExpressionContainer.allValues[this.getId()] = value;
E
Erich Gamma 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
	}
}

export class Scope implements debug.IScope {

	private children: TPromise<Variable[]>;

	constructor(private threadId: number, public name: string, public reference: number, public expensive: boolean) {
		this.children = null;
	}

	public getId(): string {
		return `scope:${ this.threadId }:${ this.name }:${ this.reference }`;
	}

	public getChildren(debugService: debug.IDebugService): TPromise<Variable[]> {
		if (!this.children) {
			this.children = resolveChildren(debugService, this);
		}

		return this.children;
	}
}

export class StackFrame implements debug.IStackFrame {

	private internalId: string;
	private scopes: TPromise<Scope[]>;

I
isidor 已提交
262
	constructor(public threadId: number, public frameId: number, public source: Source, public name: string, public lineNumber: number, public column: number) {
E
Erich Gamma 已提交
263 264 265 266 267 268 269 270 271 272 273 274
		this.internalId = uuid.generateUuid();
		this.scopes = null;
	}

	public getId(): string {
		return this.internalId;
	}

	public getScopes(debugService: debug.IDebugService): TPromise<debug.IScope[]> {
		if (!this.scopes) {
			this.scopes = debugService.getActiveSession().scopes({ frameId: this.frameId }).then(response => {
				return response.body.scopes.map(rs => new Scope(this.threadId, rs.name, rs.variablesReference, rs.expensive));
275
			}, err => []);
E
Erich Gamma 已提交
276 277 278 279 280 281 282 283 284
		}

		return this.scopes;
	}
}

export class Breakpoint implements debug.IBreakpoint {

	public lineNumber: number;
285
	public verified: boolean;
286
	public idFromAdapter: number;
I
isidor 已提交
287
	public message: string;
E
Erich Gamma 已提交
288 289
	private id: string;

I
isidor 已提交
290
	constructor(public source: Source, public desiredLineNumber: number, public enabled: boolean, public condition: string) {
291 292 293
		if (enabled === undefined) {
			this.enabled = true;
		}
E
Erich Gamma 已提交
294
		this.lineNumber = this.desiredLineNumber;
295
		this.verified = false;
E
Erich Gamma 已提交
296 297 298 299 300 301 302 303
		this.id = uuid.generateUuid();
	}

	public getId(): string {
		return this.id;
	}
}

I
isidor 已提交
304 305 306
export class FunctionBreakpoint implements debug.IFunctionBreakpoint {

	private id: string;
I
isidor 已提交
307
	public verified: boolean;
308
	public idFromAdapter: number;
I
isidor 已提交
309

310
	constructor(public name: string, public enabled: boolean) {
I
isidor 已提交
311
		this.verified = false;
I
isidor 已提交
312 313 314 315 316 317 318 319
		this.id = uuid.generateUuid();
	}

	public getId(): string {
		return this.id;
	}
}

E
Erich Gamma 已提交
320 321 322 323
export class ExceptionBreakpoint implements debug.IExceptionBreakpoint {

	private id: string;

324
	constructor(public filter: string, public label: string, public enabled: boolean) {
E
Erich Gamma 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338
		this.id = uuid.generateUuid();
	}

	public getId(): string {
		return this.id;
	}
}

export class Model extends ee.EventEmitter implements debug.IModel {

	private threads: { [reference: number]: debug.IThread; };
	private toDispose: lifecycle.IDisposable[];
	private replElements: debug.ITreeElement[];

I
isidor 已提交
339
	constructor(private breakpoints: debug.IBreakpoint[], private breakpointsActivated: boolean, private functionBreakpoints: debug.IFunctionBreakpoint[],
E
Erich Gamma 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
		private exceptionBreakpoints: debug.IExceptionBreakpoint[], private watchExpressions: Expression[]) {

		super();
		this.threads = {};
		this.replElements = [];
		this.toDispose = [];
	}

	public getId(): string {
		return 'root';
	}

	public getThreads(): { [reference: number]: debug.IThread; } {
		return this.threads;
	}

	public clearThreads(removeThreads: boolean, reference: number = undefined): void {
		if (reference) {
			if (removeThreads) {
				delete this.threads[reference];
			} else {
				this.threads[reference].callStack = [];
I
isidor 已提交
362
				this.threads[reference].stoppedDetails = undefined;
E
Erich Gamma 已提交
363 364 365 366
			}
		} else {
			if (removeThreads) {
				this.threads = {};
367
				ExpressionContainer.allValues = {};
E
Erich Gamma 已提交
368
			} else {
I
isidor 已提交
369
				for (let ref in this.threads) {
E
Erich Gamma 已提交
370 371
					if (this.threads.hasOwnProperty(ref)) {
						this.threads[ref].callStack = [];
I
isidor 已提交
372
						this.threads[ref].stoppedDetails = undefined;
E
Erich Gamma 已提交
373 374 375 376 377 378 379 380 381 382 383 384
					}
				}
			}
		}

		this.emit(debug.ModelEvents.CALLSTACK_UPDATED);
	}

	public getBreakpoints(): debug.IBreakpoint[] {
		return this.breakpoints;
	}

I
isidor 已提交
385 386 387 388
	public getFunctionBreakpoints(): debug.IFunctionBreakpoint[] {
		return this.functionBreakpoints;
	}

E
Erich Gamma 已提交
389 390 391 392
	public getExceptionBreakpoints(): debug.IExceptionBreakpoint[] {
		return this.exceptionBreakpoints;
	}

393 394 395 396 397 398 399
	public setExceptionBreakpoints(data: [{ filter: string, label: string }]): void {
		if (data) {
			this.exceptionBreakpoints = data.map(d =>
				new ExceptionBreakpoint(d.filter, d.label, this.exceptionBreakpoints.some(ebp => ebp.filter === d.filter && ebp.enabled)));
		}
	}

E
Erich Gamma 已提交
400 401 402 403 404 405 406 407 408
	public areBreakpointsActivated(): boolean {
		return this.breakpointsActivated;
	}

	public toggleBreakpointsActivated(): void {
		this.breakpointsActivated = !this.breakpointsActivated;
		this.emit(debug.ModelEvents.BREAKPOINTS_UPDATED);
	}

409
	public addBreakpoints(rawData: debug.IRawBreakpoint[]): void {
410 411
		this.breakpoints = this.breakpoints.concat(rawData.map(rawBp =>
			new Breakpoint(new Source(Source.toRawSource(rawBp.uri, this)), rawBp.lineNumber, rawBp.enabled, rawBp.condition)));
412 413 414
		this.breakpointsActivated = true;
		this.emit(debug.ModelEvents.BREAKPOINTS_UPDATED);
	}
E
Erich Gamma 已提交
415

416 417
	public removeBreakpoints(toRemove: debug.IBreakpoint[]): void {
		this.breakpoints = this.breakpoints.filter(bp => !toRemove.some(toRemove => toRemove.getId() === bp.getId()));
E
Erich Gamma 已提交
418 419 420
		this.emit(debug.ModelEvents.BREAKPOINTS_UPDATED);
	}

I
isidor 已提交
421
	public updateBreakpoints(data: { [id: string]: DebugProtocol.Breakpoint }): void {
422 423 424
		this.breakpoints.forEach(bp => {
			const bpData = data[bp.getId()];
			if (bpData) {
425
				bp.lineNumber = bpData.line ? bpData.line : bp.lineNumber;
426
				bp.verified = bpData.verified;
427
				bp.idFromAdapter = bpData.id;
I
isidor 已提交
428
				bp.message = bpData.message;
429 430 431
			}
		});
		this.emit(debug.ModelEvents.BREAKPOINTS_UPDATED);
432 433
	}

E
Erich Gamma 已提交
434 435 436 437 438
	public toggleEnablement(element: debug.IEnablement): void {
		element.enabled = !element.enabled;
		if (element instanceof Breakpoint && !element.enabled) {
			var breakpoint = <Breakpoint> element;
			breakpoint.lineNumber = breakpoint.desiredLineNumber;
439
			breakpoint.verified = false;
E
Erich Gamma 已提交
440 441 442 443 444 445 446 447 448 449
		}

		this.emit(debug.ModelEvents.BREAKPOINTS_UPDATED);
	}

	public enableOrDisableAllBreakpoints(enabled: boolean): void {
		this.breakpoints.forEach(bp => {
			bp.enabled = enabled;
			if (!enabled) {
				bp.lineNumber = bp.desiredLineNumber;
450
				bp.verified = false;
E
Erich Gamma 已提交
451 452
			}
		});
I
isidor 已提交
453 454
		this.exceptionBreakpoints.forEach(ebp => ebp.enabled = enabled);
		this.functionBreakpoints.forEach(fbp => fbp.enabled = enabled);
E
Erich Gamma 已提交
455 456 457 458

		this.emit(debug.ModelEvents.BREAKPOINTS_UPDATED);
	}

I
isidor 已提交
459 460 461 462 463
	public addFunctionBreakpoint(functionName: string): void {
		this.functionBreakpoints.push(new FunctionBreakpoint(functionName, true));
		this.emit(debug.ModelEvents.BREAKPOINTS_UPDATED);
	}

464
	public updateFunctionBreakpoints(data: { [id: string]: { name?: string, verified?: boolean; id?: number } }): void {
I
isidor 已提交
465 466 467 468 469
		this.functionBreakpoints.forEach(fbp => {
			const fbpData = data[fbp.getId()];
			if (fbpData) {
				fbp.name = fbpData.name || fbp.name;
				fbp.verified = fbpData.verified;
470
				fbp.idFromAdapter = fbpData.id;
I
isidor 已提交
471 472 473 474
			}
		});

		this.emit(debug.ModelEvents.BREAKPOINTS_UPDATED);
I
isidor 已提交
475 476
	}

477
	public removeFunctionBreakpoints(id?: string): void {
I
isidor 已提交
478
		this.functionBreakpoints = id ? this.functionBreakpoints.filter(fbp => fbp.getId() !== id) : [];
479 480 481
		this.emit(debug.ModelEvents.BREAKPOINTS_UPDATED);
	}

E
Erich Gamma 已提交
482 483 484 485
	public getReplElements(): debug.ITreeElement[] {
		return this.replElements;
	}

I
isidor 已提交
486
	public addReplExpression(session: debug.IRawDebugSession, stackFrame: debug.IStackFrame, name: string): TPromise<void> {
I
isidor 已提交
487
		const expression = new Expression(name, true);
E
Erich Gamma 已提交
488
		this.replElements.push(expression);
I
isidor 已提交
489
		return evaluateExpression(session, stackFrame, expression, 'repl').then(() =>
E
Erich Gamma 已提交
490 491 492 493 494 495 496 497 498 499
			this.emit(debug.ModelEvents.REPL_ELEMENTS_UPDATED, expression)
		);
	}

	public logToRepl(value: string, severity?: severity): void;
	public logToRepl(value: { [key: string]: any }, severity?: severity): void;
	public logToRepl(value: any, severity?: severity): void {
		let elements:OutputElement[] = [];
		let previousOutput = this.replElements.length && (<ValueOutputElement>this.replElements[this.replElements.length - 1]);

I
isidor 已提交
500
		// string message
E
Erich Gamma 已提交
501
		if (typeof value === 'string') {
I
isidor 已提交
502
			value = strings.removeAnsiEscapeCodes(value);
E
Erich Gamma 已提交
503 504 505
			if (value && value.trim() && previousOutput && previousOutput.value === value && previousOutput.severity === severity) {
				previousOutput.counter++; // we got the same output (but not an empty string when trimmed) so we just increment the counter
			} else {
I
isidor 已提交
506
				let lines = value.trim().split('\n');
E
Erich Gamma 已提交
507
				lines.forEach((line, index) => {
I
isidor 已提交
508
					elements.push(new ValueOutputElement(line, severity));
E
Erich Gamma 已提交
509 510 511 512
				});
			}
		}

I
isidor 已提交
513
		// key-value output
E
Erich Gamma 已提交
514
		else {
I
isidor 已提交
515
			elements.push(new KeyValueOutputElement(value.prototype, value, nls.localize('snapshotObj', "Only primitive values are shown for this object.")));
E
Erich Gamma 已提交
516 517 518 519 520 521 522 523 524
		}

		if (elements.length) {
			this.replElements.push(...elements);
			this.emit(debug.ModelEvents.REPL_ELEMENTS_UPDATED, elements);
		}
	}

	public appendReplOutput(value: string, severity?: severity): void {
I
isidor 已提交
525
		value = strings.removeAnsiEscapeCodes(value);
I
isidor 已提交
526
		const elements: OutputElement[] = [];
E
Erich Gamma 已提交
527
		let previousOutput = this.replElements.length && (<ValueOutputElement>this.replElements[this.replElements.length - 1]);
528 529
		let lines = value.split('\n');
		let groupTogether = !!previousOutput && (previousOutput.category === 'output' && severity === previousOutput.severity);
E
Erich Gamma 已提交
530 531

		if (groupTogether) {
532 533 534 535 536
			// append to previous line if same group
			previousOutput.value += lines.shift();
		} else if (previousOutput && previousOutput.value === '') {
			// remove potential empty lines between different output types
			this.replElements.pop();
E
Erich Gamma 已提交
537 538 539 540
		}

		// fill in lines as output value elements
		lines.forEach((line, index) => {
I
isidor 已提交
541
			elements.push(new ValueOutputElement(line, severity, 'output'));
E
Erich Gamma 已提交
542 543 544 545 546 547 548
		});

		this.replElements.push(...elements);
		this.emit(debug.ModelEvents.REPL_ELEMENTS_UPDATED, elements);
	}

	public clearReplExpressions(): void {
549 550 551 552
		if (this.replElements.length > 0) {
			this.replElements = [];
			this.emit(debug.ModelEvents.REPL_ELEMENTS_UPDATED);
		}
E
Erich Gamma 已提交
553 554 555 556 557 558
	}

	public getWatchExpressions(): Expression[] {
		return this.watchExpressions;
	}

I
isidor 已提交
559
	public addWatchExpression(session: debug.IRawDebugSession, stackFrame: debug.IStackFrame, name: string): TPromise<void> {
I
isidor 已提交
560
		const we = new Expression(name, false);
E
Erich Gamma 已提交
561 562 563
		this.watchExpressions.push(we);
		if (!name) {
			this.emit(debug.ModelEvents.WATCH_EXPRESSIONS_UPDATED, we);
A
Alex Dima 已提交
564
			return TPromise.as(null);
E
Erich Gamma 已提交
565 566 567 568 569
		}

		return this.evaluateWatchExpressions(session, stackFrame, we.getId());
	}

I
isidor 已提交
570
	public renameWatchExpression(session: debug.IRawDebugSession, stackFrame: debug.IStackFrame, id: string, newName: string): TPromise<void> {
I
isidor 已提交
571
		const filtered = this.watchExpressions.filter(we => we.getId() === id);
E
Erich Gamma 已提交
572 573
		if (filtered.length === 1) {
			filtered[0].name = newName;
I
isidor 已提交
574
			return evaluateExpression(session, stackFrame, filtered[0], 'watch').then(() => {
E
Erich Gamma 已提交
575 576 577 578
				this.emit(debug.ModelEvents.WATCH_EXPRESSIONS_UPDATED, filtered[0]);
			});
		}

A
Alex Dima 已提交
579
		return TPromise.as(null);
E
Erich Gamma 已提交
580 581
	}

I
isidor 已提交
582
	public evaluateWatchExpressions(session: debug.IRawDebugSession, stackFrame: debug.IStackFrame, id: string = null): TPromise<void> {
E
Erich Gamma 已提交
583
		if (id) {
I
isidor 已提交
584
			const filtered = this.watchExpressions.filter(we => we.getId() === id);
E
Erich Gamma 已提交
585
			if (filtered.length !== 1) {
A
Alex Dima 已提交
586
				return TPromise.as(null);
E
Erich Gamma 已提交
587 588
			}

I
isidor 已提交
589
			return evaluateExpression(session, stackFrame, filtered[0], 'watch').then(() => {
E
Erich Gamma 已提交
590 591 592 593
				this.emit(debug.ModelEvents.WATCH_EXPRESSIONS_UPDATED, filtered[0]);
			});
		}

I
isidor 已提交
594
		return TPromise.join(this.watchExpressions.map(we => evaluateExpression(session, stackFrame, we, 'watch'))).then(() => {
E
Erich Gamma 已提交
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
			this.emit(debug.ModelEvents.WATCH_EXPRESSIONS_UPDATED);
		});
	}

	public clearWatchExpressionValues(): void {
		this.watchExpressions.forEach(we => {
			we.value = Expression.DEFAULT_VALUE;
			we.available = false;
			we.reference = 0;
		});

		this.emit(debug.ModelEvents.WATCH_EXPRESSIONS_UPDATED);
	}

	public clearWatchExpressions(id: string = null): void {
610
		this.watchExpressions = id ? this.watchExpressions.filter(we => we.getId() !== id) : [];
E
Erich Gamma 已提交
611 612 613
		this.emit(debug.ModelEvents.WATCH_EXPRESSIONS_UPDATED);
	}

I
isidor 已提交
614
	public sourceIsUnavailable(source: Source): void {
615 616 617 618 619 620 621 622
		Object.keys(this.threads).forEach(key => {
			this.threads[key].callStack.forEach(stackFrame => {
				if (stackFrame.source.uri.toString() === source.uri.toString()) {
					stackFrame.source.available = false;
				}
			});
		});

E
Erich Gamma 已提交
623 624 625 626 627 628 629 630 631
		this.emit(debug.ModelEvents.CALLSTACK_UPDATED);
	}

	public rawUpdate(data: debug.IRawModelUpdate): void {
		if (data.thread) {
			this.threads[data.threadId] = new Thread(data.thread.name, data.thread.id, []);
		}

		if (data.callStack) {
I
isidor 已提交
632
			// convert raw call stack into proper modelled call stack
E
Erich Gamma 已提交
633 634 635
			this.threads[data.threadId].callStack = data.callStack.map(
				(rsf, level) => {
					if (!rsf) {
636
						return new StackFrame(data.threadId, 0, new Source({ name: 'unknown' }), nls.localize('unknownStack', "Unknown stack location"), undefined, undefined);
E
Erich Gamma 已提交
637
					}
638

639
					return new StackFrame(data.threadId, rsf.id, rsf.source ? new Source(rsf.source) : new Source({ name: 'unknown' }), rsf.name, rsf.line, rsf.column);
E
Erich Gamma 已提交
640 641
				});

I
isidor 已提交
642
			this.threads[data.threadId].stoppedDetails = data.stoppedDetails;
E
Erich Gamma 已提交
643 644 645 646 647 648 649 650 651 652
		}

		this.emit(debug.ModelEvents.CALLSTACK_UPDATED);
	}

	public dispose(): void {
		super.dispose();
		this.threads = null;
		this.breakpoints = null;
		this.exceptionBreakpoints = null;
I
isidor 已提交
653
		this.functionBreakpoints = null;
E
Erich Gamma 已提交
654 655 656 657 658
		this.watchExpressions = null;
		this.replElements = null;
		this.toDispose = lifecycle.disposeAll(this.toDispose);
	}
}