1 11 package org.eclipse.jdi.internal; 12 13 14 import java.io.ByteArrayOutputStream ; 15 import java.io.DataInputStream ; 16 import java.io.DataOutputStream ; 17 import java.io.IOException ; 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import org.eclipse.jdi.internal.jdwp.JdwpCommandPacket; 25 import org.eclipse.jdi.internal.jdwp.JdwpFrameID; 26 import org.eclipse.jdi.internal.jdwp.JdwpID; 27 import org.eclipse.jdi.internal.jdwp.JdwpReplyPacket; 28 import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin; 29 30 import com.sun.jdi.AbsentInformationException; 31 import com.sun.jdi.ClassNotLoadedException; 32 import com.sun.jdi.InvalidStackFrameException; 33 import com.sun.jdi.InvalidTypeException; 34 import com.sun.jdi.LocalVariable; 35 import com.sun.jdi.Locatable; 36 import com.sun.jdi.Location; 37 import com.sun.jdi.ObjectReference; 38 import com.sun.jdi.StackFrame; 39 import com.sun.jdi.ThreadReference; 40 import com.sun.jdi.VMMismatchException; 41 import com.sun.jdi.Value; 42 43 49 public class StackFrameImpl extends MirrorImpl implements StackFrame, Locatable { 50 51 private JdwpFrameID fFrameID; 52 53 private ThreadReferenceImpl fThread; 54 55 private LocationImpl fLocation; 56 57 60 public StackFrameImpl(VirtualMachineImpl vmImpl, JdwpFrameID ID, ThreadReferenceImpl thread, LocationImpl location) { 61 super("StackFrame", vmImpl); fFrameID = ID; 63 fThread = thread; 64 fLocation = location; 65 } 66 67 70 public Value getValue(LocalVariable variable) throws IllegalArgumentException , InvalidStackFrameException, VMMismatchException { 71 ArrayList list = new ArrayList (1); 72 list.add(variable); 73 return (ValueImpl)getValues(list).get(variable); 74 } 75 76 79 public Map getValues(List variables) throws IllegalArgumentException , InvalidStackFrameException, VMMismatchException { 80 Map map = new HashMap (variables.size()); 82 if (variables.isEmpty()) { 84 return map; 85 } 86 90 int sizeAll = variables.size(); 91 int sizeThis = 0; 92 boolean[] isThisValue = new boolean[sizeAll]; 93 for (int i = 0; i < sizeAll; i++) { 94 LocalVariableImpl var = (LocalVariableImpl)variables.get(i); 95 isThisValue[i] = var.isThis(); 96 if (isThisValue[i]) { 97 sizeThis++; 98 } 99 } 100 int sizeNotThis = sizeAll - sizeThis; 101 102 if (sizeThis > 0) { 103 Value thisValue = thisObject(); 104 for (int i = 0; i < sizeAll; i++) { 105 if (isThisValue[i]) { 106 map.put(variables.get(i), thisValue); 107 } 108 } 109 } 110 111 if (sizeNotThis == 0) { 113 return map; 114 } 115 116 initJdwpRequest(); 118 try { 119 ByteArrayOutputStream outBytes = new ByteArrayOutputStream (); 120 DataOutputStream outData = new DataOutputStream (outBytes); 121 writeWithThread(this, outData); 122 writeInt(sizeNotThis, "size", outData); for (int i = 0; i < sizeAll; i++) { 124 if (!isThisValue[i]) { 125 LocalVariableImpl var = (LocalVariableImpl)variables.get(i); 126 checkVM(var); 127 writeInt(var.slot(), "slot", outData); writeByte(var.tag(), "tag", JdwpID.tagMap(), outData); } 130 } 131 JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.SF_GET_VALUES, outBytes); 132 defaultReplyErrorHandler(replyPacket.errorCode()); 133 134 DataInputStream replyData = replyPacket.dataInStream(); 135 int nrOfElements = readInt("elements", replyData); if (nrOfElements != sizeNotThis) 137 throw new InternalError (JDIMessages.StackFrameImpl_Retrieved_a_different_number_of_values_from_the_VM_than_requested_1); 138 139 for (int i = 0, j = 0; i < sizeAll; i++) { 140 if (!isThisValue[i]) 141 map.put(variables.get(j++), ValueImpl.readWithTag(this, replyData)); 142 } 143 return map; 144 } catch (IOException e) { 145 defaultIOExceptionHandler(e); 146 return null; 147 } finally { 148 handledJdwpRequest(); 149 } 150 } 151 152 156 public List getArgumentValues() throws InvalidStackFrameException { 157 if(!thread().isSuspended()) { 158 throw new InvalidStackFrameException(JDIMessages.StackFrameImpl_no_argument_values_available); 159 } 160 try { 161 List list = location().method().variables(); 162 ArrayList ret = new ArrayList (); 163 LocalVariable var = null; 164 for(Iterator iter = list.iterator(); iter.hasNext();){ 165 var = (LocalVariable) iter.next(); 166 if(var.isArgument()) { 167 ret.add(getValue(var)); 168 } 169 } 170 return ret; 171 } 172 catch (AbsentInformationException e) { 173 JDIDebugPlugin.log(e); 174 return null; 175 } 176 } 177 178 181 public Location location() { 182 return fLocation; 183 } 184 185 188 public void setValue(LocalVariable var, Value value) throws InvalidTypeException, ClassNotLoadedException { 189 initJdwpRequest(); 191 try { 192 ByteArrayOutputStream outBytes = new ByteArrayOutputStream (); 193 DataOutputStream outData = new DataOutputStream (outBytes); 194 ((ThreadReferenceImpl)thread()).write(this, outData); 195 write(this, outData); 196 writeInt(1, "size", outData); checkVM(var); 198 writeInt(((LocalVariableImpl)var).slot(), "slot", outData); 200 ValueImpl checkedValue= ValueImpl.checkValue(value, var.type(), virtualMachineImpl()); 202 203 if (checkedValue != null) { 204 checkedValue.writeWithTag(this, outData); 205 } else { 206 ValueImpl.writeNullWithTag(this, outData); 207 } 208 209 JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.SF_SET_VALUES, outBytes); 210 switch (replyPacket.errorCode()) { 211 case JdwpReplyPacket.INVALID_CLASS: 212 throw new ClassNotLoadedException(var.typeName()); 213 } 214 defaultReplyErrorHandler(replyPacket.errorCode()); 215 } catch (IOException e) { 216 defaultIOExceptionHandler(e); 217 } finally { 218 handledJdwpRequest(); 219 } 220 } 221 222 225 public ObjectReference thisObject() throws InvalidStackFrameException { 226 initJdwpRequest(); 228 try { 229 ByteArrayOutputStream outBytes = new ByteArrayOutputStream (); 230 DataOutputStream outData = new DataOutputStream (outBytes); 231 writeWithThread(this, outData); 232 233 JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.SF_THIS_OBJECT, outBytes); 234 defaultReplyErrorHandler(replyPacket.errorCode()); 235 236 DataInputStream replyData = replyPacket.dataInStream(); 237 ObjectReference result = ObjectReferenceImpl.readObjectRefWithTag(this, replyData); 238 return result; 239 } catch (IOException e) { 240 defaultIOExceptionHandler(e); 241 return null; 242 } finally { 243 handledJdwpRequest(); 244 } 245 } 246 247 250 public ThreadReference thread() { 251 return fThread; 252 } 253 254 257 public LocalVariable visibleVariableByName(String name) throws AbsentInformationException { 258 Iterator iter = visibleVariables().iterator(); 259 while (iter.hasNext()) { 260 LocalVariableImpl var = (LocalVariableImpl)iter.next(); 261 if (var.name().equals(name)) { 262 return var; 263 } 264 } 265 266 return null; 267 } 268 269 272 public List visibleVariables() throws AbsentInformationException { 273 List variables= fLocation.method().variables(); 274 Iterator iter = variables.iterator(); 275 List visibleVars = new ArrayList (variables.size()); 276 while (iter.hasNext()) { 277 LocalVariableImpl var = (LocalVariableImpl)iter.next(); 278 if (var.isVisible(this) && !var.isThis()) { 280 visibleVars.add(var); 281 } 282 } 283 return visibleVars; 284 } 285 286 289 public int hashCode() { 290 return fThread.hashCode() + fFrameID.hashCode(); 291 } 292 293 297 public boolean equals(Object object) { 298 return object != null && object.getClass().equals(this.getClass()) && fThread.equals(((StackFrameImpl)object).fThread) && fFrameID.equals(((StackFrameImpl)object).fFrameID); 299 } 300 301 304 public void write(MirrorImpl target, DataOutputStream out) throws IOException { 305 fFrameID.write(out); 306 if (target.fVerboseWriter != null) { 307 target.fVerboseWriter.println("stackFrame", fFrameID.value()); } 309 } 310 311 314 public void writeWithThread(MirrorImpl target, DataOutputStream out) throws IOException { 315 fThread.write(target, out); 316 write(target, out); 317 } 318 319 322 public static StackFrameImpl readWithLocation(MirrorImpl target, ThreadReferenceImpl thread, DataInputStream in) throws IOException { 323 VirtualMachineImpl vmImpl = target.virtualMachineImpl(); 324 JdwpFrameID ID = new JdwpFrameID(vmImpl); 325 ID.read(in); 326 if (target.fVerboseWriter != null) { 327 target.fVerboseWriter.println("stackFrame", ID.value()); } 329 330 if (ID.isNull()) { 331 return null; 332 } 333 LocationImpl location = LocationImpl.read(target, in); 334 if (location == null) { 335 return null; 336 } 337 338 return new StackFrameImpl(vmImpl, ID, thread, location); 339 } 340 } 341 | Popular Tags |