From 2a6c0b8c168d7a8ba4c6d1bdec585c34146e05da Mon Sep 17 00:00:00 2001 From: Alain Magloire Date: Sat, 30 Oct 2004 03:32:49 +0000 Subject: [PATCH] Fix expression manager --- .../debug/mi/core/cdi/ExpressionManager.java | 83 +++++++++++++++---- .../cdt/debug/mi/core/cdi/SourceManager.java | 1 - .../debug/mi/core/cdi/event/ChangedEvent.java | 4 +- .../debug/mi/core/cdi/event/CreatedEvent.java | 2 +- .../mi/core/cdi/event/DestroyedEvent.java | 13 +-- .../debug/mi/core/cdi/model/Expression.java | 79 +----------------- 6 files changed, 80 insertions(+), 102 deletions(-) diff --git a/debug/org.eclipse.cdt.debug.mi.core/cdi/org/eclipse/cdt/debug/mi/core/cdi/ExpressionManager.java b/debug/org.eclipse.cdt.debug.mi.core/cdi/org/eclipse/cdt/debug/mi/core/cdi/ExpressionManager.java index 7f53f743920..4e1a491bda3 100644 --- a/debug/org.eclipse.cdt.debug.mi.core/cdi/org/eclipse/cdt/debug/mi/core/cdi/ExpressionManager.java +++ b/debug/org.eclipse.cdt.debug.mi.core/cdi/org/eclipse/cdt/debug/mi/core/cdi/ExpressionManager.java @@ -18,17 +18,24 @@ import java.util.Map; import org.eclipse.cdt.debug.core.cdi.CDIException; import org.eclipse.cdt.debug.core.cdi.model.ICDIExpression; +import org.eclipse.cdt.debug.core.cdi.model.ICDIStackFrame; +import org.eclipse.cdt.debug.core.cdi.model.ICDIThread; import org.eclipse.cdt.debug.mi.core.MIException; import org.eclipse.cdt.debug.mi.core.MISession; import org.eclipse.cdt.debug.mi.core.cdi.model.Expression; import org.eclipse.cdt.debug.mi.core.cdi.model.Target; import org.eclipse.cdt.debug.mi.core.cdi.model.Variable; +import org.eclipse.cdt.debug.mi.core.cdi.model.VariableObject; import org.eclipse.cdt.debug.mi.core.command.CommandFactory; +import org.eclipse.cdt.debug.mi.core.command.MIVarCreate; +import org.eclipse.cdt.debug.mi.core.command.MIVarDelete; import org.eclipse.cdt.debug.mi.core.command.MIVarUpdate; import org.eclipse.cdt.debug.mi.core.event.MIEvent; import org.eclipse.cdt.debug.mi.core.event.MIVarChangedEvent; import org.eclipse.cdt.debug.mi.core.event.MIVarDeletedEvent; +import org.eclipse.cdt.debug.mi.core.output.MIVar; import org.eclipse.cdt.debug.mi.core.output.MIVarChange; +import org.eclipse.cdt.debug.mi.core.output.MIVarCreateInfo; import org.eclipse.cdt.debug.mi.core.output.MIVarUpdateInfo; /** @@ -37,11 +44,13 @@ public class ExpressionManager extends Manager { final static ICDIExpression[] EMPTY_EXPRESSIONS = {}; Map expMap; + List variableList; MIVarChange[] noChanges = new MIVarChange[0]; public ExpressionManager(Session session) { super(session, true); expMap = new Hashtable(); + variableList = Collections.synchronizedList(new ArrayList()); } synchronized List getExpressionList(Target target) { @@ -79,7 +88,6 @@ public class ExpressionManager extends Manager { for (int i = 0; i < expressions.length; ++i) { if (expressions[i] instanceof Expression) { expList.remove(expressions[i]); - ((Expression)expressions[i]).deleteVariable(); } } } @@ -96,10 +104,9 @@ public class ExpressionManager extends Manager { List eventList = new ArrayList(); MISession mi = target.getMISession(); CommandFactory factory = mi.getCommandFactory(); - List expList = getExpressionList(target); - Expression[] exps = (Expression[])expList.toArray(new Expression[0]); - for (int i = 0; i < exps.length; i++) { - Variable variable = exps[i].getVariable(); + Variable[] vars = (Variable[])variableList.toArray(new Variable[0]); + for (int i = 0; i < vars.length; i++) { + Variable variable = vars[i]; if (variable != null) { String varName = variable.getMIVar().getVarName(); MIVarChange[] changes = noChanges; @@ -132,20 +139,68 @@ public class ExpressionManager extends Manager { * @param varName * @return */ - public Expression getExpression(MISession miSession, String varName) { + public Variable getVariable(MISession miSession, String varName) { Session session = (Session)getSession(); Target target = session.getTarget(miSession); - List expList = getExpressionList(target); - Expression[] exps = (Expression[])expList.toArray(new Expression[0]); - for (int i = 0; i < exps.length; i++) { - Variable variable = exps[i].getVariable(); - if (variable != null) { - if (variable.getMIVar().getVarName().equals(varName)) { - return exps[i]; - } + Variable[] vars = (Variable[])variableList.toArray(new Variable[0]); + for (int i = 0; i < vars.length; i++) { + if (vars[i].getMIVar().getVarName().equals(varName)) { + return vars[i]; } } return null; } + public Variable createVariable(ICDIStackFrame frame, String code) throws CDIException { + Target target = (Target)frame.getTarget(); + ICDIThread currentThread = target.getCurrentThread(); + ICDIStackFrame currentFrame = currentThread.getCurrentStackFrame(); + target.setCurrentThread(frame.getThread(), false); + frame.getThread().setCurrentStackFrame(frame, false); + try { + MISession mi = target.getMISession(); + CommandFactory factory = mi.getCommandFactory(); + MIVarCreate var = factory.createMIVarCreate(code); + mi.postCommand(var); + MIVarCreateInfo info = var.getMIVarCreateInfo(); + if (info == null) { + throw new CDIException(CdiResources.getString("cdi.Common.No_answer")); //$NON-NLS-1$ + } + VariableObject varObj = new VariableObject(target, code, frame, 0, 0); + Variable variable = new Variable(varObj, info.getMIVar()); + variableList.add(variable); + return variable; + } catch (MIException e) { + throw new MI2CDIException(e); + } finally { + target.setCurrentThread(currentThread, false); + currentThread.setCurrentStackFrame(currentFrame, false); + } + } + + /** + * Get rid of the underlying variable. + * + */ + public void deleteVariable(Variable variable) throws CDIException { + Target target = (Target)variable.getTarget(); + MISession miSession = target.getMISession(); + MIVar miVar = variable.getMIVar(); + removeMIVar(miSession, variable.getMIVar()); + } + + /** + * Tell gdb to remove the underlying var-object also. + */ + public void removeMIVar(MISession miSession, MIVar miVar) throws CDIException { + CommandFactory factory = miSession.getCommandFactory(); + MIVarDelete var = factory.createMIVarDelete(miVar.getVarName()); + try { + miSession.postCommand(var); + var.getMIInfo(); + } catch (MIException e) { + throw new MI2CDIException(e); + } + } + } diff --git a/debug/org.eclipse.cdt.debug.mi.core/cdi/org/eclipse/cdt/debug/mi/core/cdi/SourceManager.java b/debug/org.eclipse.cdt.debug.mi.core/cdi/org/eclipse/cdt/debug/mi/core/cdi/SourceManager.java index da6a2a5fbfb..6e98e10e0b5 100644 --- a/debug/org.eclipse.cdt.debug.mi.core/cdi/org/eclipse/cdt/debug/mi/core/cdi/SourceManager.java +++ b/debug/org.eclipse.cdt.debug.mi.core/cdi/org/eclipse/cdt/debug/mi/core/cdi/SourceManager.java @@ -26,7 +26,6 @@ import org.eclipse.cdt.debug.mi.core.GDBTypeParser.GDBType; import org.eclipse.cdt.debug.mi.core.cdi.model.Instruction; import org.eclipse.cdt.debug.mi.core.cdi.model.MixedInstruction; import org.eclipse.cdt.debug.mi.core.cdi.model.Target; -import org.eclipse.cdt.debug.mi.core.cdi.model.VariableObject; import org.eclipse.cdt.debug.mi.core.cdi.model.type.ArrayType; import org.eclipse.cdt.debug.mi.core.cdi.model.type.BoolType; import org.eclipse.cdt.debug.mi.core.cdi.model.type.CharType; diff --git a/debug/org.eclipse.cdt.debug.mi.core/cdi/org/eclipse/cdt/debug/mi/core/cdi/event/ChangedEvent.java b/debug/org.eclipse.cdt.debug.mi.core/cdi/org/eclipse/cdt/debug/mi/core/cdi/event/ChangedEvent.java index 8aa17e3b995..9375d5ab690 100644 --- a/debug/org.eclipse.cdt.debug.mi.core/cdi/org/eclipse/cdt/debug/mi/core/cdi/event/ChangedEvent.java +++ b/debug/org.eclipse.cdt.debug.mi.core/cdi/org/eclipse/cdt/debug/mi/core/cdi/event/ChangedEvent.java @@ -50,7 +50,7 @@ public class ChangedEvent implements ICDIChangedEvent { // Try the Expression manager if (source == null) { ExpressionManager expMgr = session.getExpressionManager(); - source = expMgr.getExpression(miSession, varName); + source = expMgr.getVariable(miSession, varName); } // Try the Register manager @@ -94,7 +94,7 @@ public class ChangedEvent implements ICDIChangedEvent { public ChangedEvent(Session s, MISharedLibChangedEvent slib) { session = s; - SharedLibraryManager mgr = (SharedLibraryManager)session.getSharedLibraryManager(); + SharedLibraryManager mgr = session.getSharedLibraryManager(); MISession miSession = slib.getMISession(); String name = slib.getName(); ICDISharedLibrary lib = mgr.getSharedLibrary(miSession, name); diff --git a/debug/org.eclipse.cdt.debug.mi.core/cdi/org/eclipse/cdt/debug/mi/core/cdi/event/CreatedEvent.java b/debug/org.eclipse.cdt.debug.mi.core/cdi/org/eclipse/cdt/debug/mi/core/cdi/event/CreatedEvent.java index eb80c1e81cc..ca3016ca21b 100644 --- a/debug/org.eclipse.cdt.debug.mi.core/cdi/org/eclipse/cdt/debug/mi/core/cdi/event/CreatedEvent.java +++ b/debug/org.eclipse.cdt.debug.mi.core/cdi/org/eclipse/cdt/debug/mi/core/cdi/event/CreatedEvent.java @@ -109,7 +109,7 @@ public class CreatedEvent implements ICDICreatedEvent { public CreatedEvent(Session s, MISharedLibCreatedEvent slib) { session = s; - SharedLibraryManager mgr = (SharedLibraryManager)session.getSharedLibraryManager(); + SharedLibraryManager mgr = session.getSharedLibraryManager(); MISession miSession = slib.getMISession(); String name = slib.getName(); source = mgr.getSharedLibrary(miSession, name); diff --git a/debug/org.eclipse.cdt.debug.mi.core/cdi/org/eclipse/cdt/debug/mi/core/cdi/event/DestroyedEvent.java b/debug/org.eclipse.cdt.debug.mi.core/cdi/org/eclipse/cdt/debug/mi/core/cdi/event/DestroyedEvent.java index 8f231fcef74..cf0400c2cdc 100644 --- a/debug/org.eclipse.cdt.debug.mi.core/cdi/org/eclipse/cdt/debug/mi/core/cdi/event/DestroyedEvent.java +++ b/debug/org.eclipse.cdt.debug.mi.core/cdi/org/eclipse/cdt/debug/mi/core/cdi/event/DestroyedEvent.java @@ -22,7 +22,6 @@ import org.eclipse.cdt.debug.mi.core.cdi.SharedLibraryManager; import org.eclipse.cdt.debug.mi.core.cdi.VariableManager; import org.eclipse.cdt.debug.mi.core.cdi.model.Breakpoint; import org.eclipse.cdt.debug.mi.core.cdi.model.CObject; -import org.eclipse.cdt.debug.mi.core.cdi.model.Expression; import org.eclipse.cdt.debug.mi.core.cdi.model.Target; import org.eclipse.cdt.debug.mi.core.cdi.model.Thread; import org.eclipse.cdt.debug.mi.core.cdi.model.Variable; @@ -58,13 +57,9 @@ public class DestroyedEvent implements ICDIDestroyedEvent { } } else { ExpressionManager expMgr = session.getExpressionManager(); - Expression expression = expMgr.getExpression(miSession, varName); - if (expression != null) { - source = expression; - try { - expression.deleteVariable(); - } catch (CDIException e) { - } + variable = expMgr.getVariable(miSession, varName); + if (variable != null) { + source = variable; } else { Target target = session.getTarget(miSession); source = new CObject(target); @@ -89,7 +84,7 @@ public class DestroyedEvent implements ICDIDestroyedEvent { public DestroyedEvent(Session s, MISharedLibUnloadedEvent slib) { session = s; - SharedLibraryManager mgr = (SharedLibraryManager)session.getSharedLibraryManager(); + SharedLibraryManager mgr = session.getSharedLibraryManager(); MISession miSession = slib.getMISession(); String name = slib.getName(); ICDISharedLibrary lib = mgr.getSharedLibrary(miSession, name); diff --git a/debug/org.eclipse.cdt.debug.mi.core/cdi/org/eclipse/cdt/debug/mi/core/cdi/model/Expression.java b/debug/org.eclipse.cdt.debug.mi.core/cdi/org/eclipse/cdt/debug/mi/core/cdi/model/Expression.java index 6cbf83e5a3e..c5ba8d001f8 100644 --- a/debug/org.eclipse.cdt.debug.mi.core/cdi/org/eclipse/cdt/debug/mi/core/cdi/model/Expression.java +++ b/debug/org.eclipse.cdt.debug.mi.core/cdi/org/eclipse/cdt/debug/mi/core/cdi/model/Expression.java @@ -13,22 +13,13 @@ package org.eclipse.cdt.debug.mi.core.cdi.model; import org.eclipse.cdt.debug.core.cdi.CDIException; import org.eclipse.cdt.debug.core.cdi.model.ICDIExpression; import org.eclipse.cdt.debug.core.cdi.model.ICDIStackFrame; -import org.eclipse.cdt.debug.core.cdi.model.ICDIThread; import org.eclipse.cdt.debug.core.cdi.model.ICDIValue; import org.eclipse.cdt.debug.core.cdi.model.type.ICDIType; -import org.eclipse.cdt.debug.mi.core.MIException; -import org.eclipse.cdt.debug.mi.core.MISession; -import org.eclipse.cdt.debug.mi.core.cdi.CdiResources; -import org.eclipse.cdt.debug.mi.core.cdi.MI2CDIException; +import org.eclipse.cdt.debug.mi.core.cdi.ExpressionManager; import org.eclipse.cdt.debug.mi.core.cdi.Session; import org.eclipse.cdt.debug.mi.core.cdi.SourceManager; import org.eclipse.cdt.debug.mi.core.cdi.model.type.IncompleteType; import org.eclipse.cdt.debug.mi.core.cdi.model.type.Type; -import org.eclipse.cdt.debug.mi.core.command.CommandFactory; -import org.eclipse.cdt.debug.mi.core.command.MIVarCreate; -import org.eclipse.cdt.debug.mi.core.command.MIVarDelete; -import org.eclipse.cdt.debug.mi.core.output.MIVar; -import org.eclipse.cdt.debug.mi.core.output.MIVarCreateInfo; /** @@ -38,7 +29,6 @@ public class Expression extends CObject implements ICDIExpression { private static int ID_COUNT = 0; private int id; String fExpression; - Variable fVariable; Type fType; public Expression(Target target, String ex) { @@ -104,71 +94,10 @@ public class Expression extends CObject implements ICDIExpression { * @see org.eclipse.cdt.debug.core.cdi.model.ICDIExpression#getValue(org.eclipse.cdt.debug.core.cdi.model.ICDIStackFrame) */ public ICDIValue getValue(ICDIStackFrame context) throws CDIException { - if (fVariable != null) { - Target target = (Target)getTarget(); - MISession miSession = target.getMISession(); - removeMIVar(miSession, fVariable.getMIVar()); - } - fVariable = createVariable(context); - return fVariable.getValue(); - } - - public Variable getVariable() { - return fVariable; - } - - protected Variable createVariable(ICDIStackFrame frame) throws CDIException { Session session = (Session)getTarget().getSession(); - Target target = (Target)frame.getTarget(); - ICDIThread currentThread = target.getCurrentThread(); - ICDIStackFrame currentFrame = currentThread.getCurrentStackFrame(); - target.setCurrentThread(frame.getThread(), false); - frame.getThread().setCurrentStackFrame(frame, false); - try { - MISession mi = target.getMISession(); - CommandFactory factory = mi.getCommandFactory(); - MIVarCreate var = factory.createMIVarCreate(fExpression); - mi.postCommand(var); - MIVarCreateInfo info = var.getMIVarCreateInfo(); - if (info == null) { - throw new CDIException(CdiResources.getString("cdi.Common.No_answer")); //$NON-NLS-1$ - } - VariableObject varObj = new VariableObject(target, fExpression, frame, 0, 0); - return new Variable(varObj, info.getMIVar()); - } catch (MIException e) { - throw new MI2CDIException(e); - } finally { - target.setCurrentThread(currentThread, false); - currentThread.setCurrentStackFrame(currentFrame, false); - } - } - - /** - * Get rid of the underlying variable. - * - */ - public void deleteVariable() throws CDIException { - if (fVariable != null) { - Target target = (Target)getTarget(); - MISession miSession = target.getMISession(); - MIVar miVar = fVariable.getMIVar(); - removeMIVar(miSession, fVariable.getMIVar()); - fVariable = null; - } - } - - /** - * Tell gdb to remove the underlying var-object also. - */ - public void removeMIVar(MISession miSession, MIVar miVar) throws CDIException { - CommandFactory factory = miSession.getCommandFactory(); - MIVarDelete var = factory.createMIVarDelete(miVar.getVarName()); - try { - miSession.postCommand(var); - var.getMIInfo(); - } catch (MIException e) { - throw new MI2CDIException(e); - } + ExpressionManager mgr = session.getExpressionManager(); + Variable var = mgr.createVariable(context, getExpressionText()); + return var.getValue(); } }