From 77b689443fa8c339ae7d4440cdbefe4d900e7f7a Mon Sep 17 00:00:00 2001 From: gentoo90 Date: Thu, 25 May 2017 23:02:26 +0300 Subject: [PATCH] Reuse ids of stask frames, add some type inference Use GDB's stask frames number as its id in vscode --- src/mibase.ts | 99 +++++++++++++++++++++++++++------------------------ 1 file changed, 53 insertions(+), 46 deletions(-) diff --git a/src/mibase.ts b/src/mibase.ts index 98b4e08..e9500fb 100644 --- a/src/mibase.ts +++ b/src/mibase.ts @@ -18,8 +18,11 @@ class ExtendedVariable { } } +const STACK_HANDLES_START = 1000; +const VAR_HANDLES_START = 2000; + export class MI2DebugSession extends DebugSession { - protected variableHandles = new Handles(); + protected variableHandles = new Handles(VAR_HANDLES_START); protected quit: boolean; protected attached: boolean; protected needContinue: boolean; @@ -253,7 +256,7 @@ export class MI2DebugSession extends DebugSession { protected scopesRequest(response: DebugProtocol.ScopesResponse, args: DebugProtocol.ScopesArguments): void { const scopes = new Array(); - scopes.push(new Scope("Local", this.variableHandles.create("@frame:" + (args.frameId || 0)), false)); + scopes.push(new Scope("Local", STACK_HANDLES_START + (parseInt(args.frameId as any) || 0), false)); response.body = { scopes: scopes @@ -263,7 +266,13 @@ export class MI2DebugSession extends DebugSession { protected async variablesRequest(response: DebugProtocol.VariablesResponse, args: DebugProtocol.VariablesArguments): Promise { const variables: DebugProtocol.Variable[] = []; - const id = this.variableHandles.get(args.variablesReference); + let id: number | string | ExtendedVariable; + if (args.variablesReference < VAR_HANDLES_START) { + id = args.variablesReference - STACK_HANDLES_START; + } + else { + id = this.variableHandles.get(args.variablesReference); + } let createVariable = (arg, options?) => { if (options) @@ -296,58 +305,56 @@ export class MI2DebugSession extends DebugSession { return res; }; - if (typeof id == "string") { - if (id.startsWith("@frame:")) { - let stack: Variable[]; - try { - stack = await this.miDebugger.getStackVariables(this.threadID, parseInt(id.substr("@frame:".length))); - for (const variable of stack) { - try { - const varObj = await this.miDebugger.varCreate(variable.name); - let v = miVarObjToVariable(varObj.resultRecords.results); - v.name = variable.name; - variables.push(v); - } - catch (err) { - variables.push({ - name: variable.name, - value: err, - variablesReference: 0 - }); - } + if (typeof id == "number") { + let stack: Variable[]; + try { + stack = await this.miDebugger.getStackVariables(this.threadID, id); + for (const variable of stack) { + try { + const varObj = await this.miDebugger.varCreate(variable.name); + let v = miVarObjToVariable(varObj.resultRecords.results); + v.name = variable.name; + variables.push(v); + } + catch (err) { + variables.push({ + name: variable.name, + value: `<${err}>`, + variablesReference: 0 + }); } - response.body = { - variables: variables - }; - this.sendResponse(response); - } - catch (err) { - this.sendErrorResponse(response, 1, `Could not expand variable: ${err}`); } + response.body = { + variables: variables + }; + this.sendResponse(response); } - else { - // Variable members - let listChildren; - try { - listChildren = await this.miDebugger.varListChildren(id); - const children: any[] = listChildren.result("children"); - // TODO: use hasMore when it's > 0 - // const hasMore = parseInt(listChildren.result("has_more")); - const vars = children.map(child => miVarObjToVariable(child[1])); + catch (err) { + this.sendErrorResponse(response, 1, `Could not expand variable: ${err}`); + } + } + else if (typeof id == "string") { + // Variable members + let listChildren; + try { + listChildren = await this.miDebugger.varListChildren(id); + const children: any[] = listChildren.result("children"); + // TODO: use hasMore when it's > 0 + // const hasMore = parseInt(listChildren.result("has_more")); + const vars = children.map(child => miVarObjToVariable(child[1])); - response.body = { - variables: vars - } - this.sendResponse(response); - } - catch (err) { - this.sendErrorResponse(response, 1, `Could not expand variable: ${err}`); + response.body = { + variables: vars } + this.sendResponse(response); + } + catch (err) { + this.sendErrorResponse(response, 1, `Could not expand variable: ${err}`); } } else if (typeof id == "object") { if (id instanceof ExtendedVariable) { - let varReq = id; + let varReq = id; if (varReq.options.arg) { let strArr = []; let argsPart = true;