Skip to content

Commit f6d78aa

Browse files
authored
Merge pull request #109 from gentoo90/var-obj-fixes
Var obj fixes
2 parents aa1ea10 + 90c7b76 commit f6d78aa

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

src/backend/backend.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,38 @@ export class VariableObject {
123123
return res;
124124
}
125125
}
126+
127+
// from https://gist.github.com/justmoon/15511f92e5216fa2624b#gistcomment-1928632
128+
export interface MIError extends Error {
129+
readonly name: string;
130+
readonly message: string;
131+
readonly source: string;
132+
};
133+
export interface MIErrorConstructor {
134+
new (message: string, source: string): MIError;
135+
readonly prototype: MIError;
136+
}
137+
138+
export const MIError: MIErrorConstructor = <any>class MIError {
139+
readonly name: string;
140+
readonly message: string;
141+
readonly source: string;
142+
public constructor(message: string, source: string) {
143+
Object.defineProperty(this, 'name', {
144+
get: () => (this.constructor as any).name,
145+
});
146+
Object.defineProperty(this, 'message', {
147+
get: () => message,
148+
});
149+
Object.defineProperty(this, 'source', {
150+
get: () => source,
151+
});
152+
Error.captureStackTrace(this, this.constructor);
153+
}
154+
155+
public toString() {
156+
return `${this.message} (from ${this.source})`;
157+
}
158+
};
159+
Object.setPrototypeOf(MIError as any, Object.create(Error.prototype));
160+
MIError.prototype.constructor = MIError;

src/backend/mi2/mi2.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Breakpoint, IBackend, Stack, SSHArguments, Variable, VariableObject } from "../backend"
1+
import { Breakpoint, IBackend, Stack, SSHArguments, Variable, VariableObject, MIError } from "../backend"
22
import * as ChildProcess from "child_process"
33
import { EventEmitter } from "events"
44
import { parseMI, MINode } from '../mi_parse';
@@ -679,7 +679,7 @@ export class MI2 extends EventEmitter implements IBackend {
679679
this.log("stderr", "varListChildren");
680680
//TODO: add `from` and `to` arguments
681681
const res = await this.sendCommand(`var-list-children --all-values ${name}`);
682-
const children = res.result("children");
682+
const children = res.result("children") || [];
683683
let omg: VariableObject[] = children.map(child => new VariableObject(child[1]));
684684
return omg;
685685
}
@@ -729,11 +729,11 @@ export class MI2 extends EventEmitter implements IBackend {
729729
this.handlers[sel] = (node: MINode) => {
730730
if (node && node.resultRecords && node.resultRecords.resultClass === "error") {
731731
if (suppressFailure) {
732-
this.log("stderr", "WARNING: Error executing command '" + command + "'");
732+
this.log("stderr", `WARNING: Error executing command '${command}'`);
733733
resolve(node);
734734
}
735735
else
736-
reject((node.result("msg") || "Internal error") + " (from " + command + ")");
736+
reject(new MIError(node.result("msg") || "Internal error", command));
737737
}
738738
else
739739
resolve(node);

src/mibase.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DebugSession, InitializedEvent, TerminatedEvent, StoppedEvent, OutputEvent, Thread, StackFrame, Scope, Source, Handles } from 'vscode-debugadapter';
22
import { DebugProtocol } from 'vscode-debugprotocol';
3-
import { Breakpoint, IBackend, Variable, VariableObject, ValuesFormattingMode } from './backend/backend';
3+
import { Breakpoint, IBackend, Variable, VariableObject, ValuesFormattingMode, MIError } from './backend/backend';
44
import { MINode } from './backend/mi_parse';
55
import { expandValue, isExpandable } from './backend/gdb_expansion';
66
import { MI2 } from './backend/mi2/mi2';
@@ -335,24 +335,30 @@ export class MI2DebugSession extends DebugSession {
335335
for (const variable of stack) {
336336
if (this.useVarObjects) {
337337
try {
338+
let varObjName = `var_${variable.name}`;
338339
let varObj: VariableObject;
339340
try {
340-
const changes = await this.miDebugger.varUpdate(variable.name);
341+
const changes = await this.miDebugger.varUpdate(varObjName);
341342
const changelist = changes.result("changelist");
342343
changelist.forEach((change) => {
343344
const name = MINode.valueOf(change, "name");
344-
const vId = this.variableHandlesReverse[variable.name];
345+
const vId = this.variableHandlesReverse[varObjName];
345346
const v = this.variableHandles.get(vId) as any;
346347
v.applyChanges(change);
347348
});
348-
const varId = this.variableHandlesReverse[variable.name];
349+
const varId = this.variableHandlesReverse[varObjName];
349350
varObj = this.variableHandles.get(varId) as any;
350351
}
351352
catch (err) {
352-
varObj = await this.miDebugger.varCreate(variable.name, variable.name);
353-
const varId = findOrCreateVariable(varObj);
354-
varObj.exp = variable.name;
355-
varObj.id = varId;
353+
if (err instanceof MIError && err.message == "Variable object not found") {
354+
varObj = await this.miDebugger.varCreate(variable.name, varObjName);
355+
const varId = findOrCreateVariable(varObj);
356+
varObj.exp = variable.name;
357+
varObj.id = varId;
358+
}
359+
else {
360+
throw err;
361+
}
356362
}
357363
variables.push(varObj.toProtocolVariable());
358364
}

0 commit comments

Comments
 (0)