comparison agent/src/share/classes/sun/jvm/hotspot/jdi/StackFrameImpl.java @ 0:a61af66fc99e jdk7-b24

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children c18cbe5936b8
comparison
equal deleted inserted replaced
-1:000000000000 0:a61af66fc99e
1 /*
2 * Copyright 2002-2005 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25 package sun.jvm.hotspot.jdi;
26
27 import com.sun.jdi.*;
28 import sun.jvm.hotspot.oops.ObjectHeap;
29 import sun.jvm.hotspot.debugger.OopHandle;
30 import sun.jvm.hotspot.oops.Array;
31 import sun.jvm.hotspot.oops.ObjArray;
32 import sun.jvm.hotspot.oops.TypeArray;
33 import sun.jvm.hotspot.oops.Instance;
34 import sun.jvm.hotspot.runtime.BasicType;
35 import sun.jvm.hotspot.runtime.JavaVFrame;
36 import sun.jvm.hotspot.runtime.StackValue;
37 import sun.jvm.hotspot.runtime.StackValueCollection;
38 import sun.jvm.hotspot.utilities.Assert;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.ArrayList;
42 import java.util.HashMap;
43 import java.util.Iterator;
44 import java.util.Collections;
45
46 public class StackFrameImpl extends MirrorImpl
47 implements StackFrame
48 {
49 /* Once false, frame should not be used.
50 * access synchronized on (vm.state())
51 */
52 private boolean isValid = true;
53
54 private final ThreadReferenceImpl thread;
55 private final JavaVFrame saFrame;
56 private final Location location;
57 private Map visibleVariables = null;
58 private ObjectReference thisObject = null;
59
60 StackFrameImpl(VirtualMachine vm, ThreadReferenceImpl thread,
61 JavaVFrame jvf) {
62 super(vm);
63 this.thread = thread;
64 this.saFrame = jvf;
65
66 sun.jvm.hotspot.oops.Method SAMethod = jvf.getMethod();
67
68 ReferenceType rt = ((VirtualMachineImpl)vm).referenceType(SAMethod.getMethodHolder());
69
70 this.location = new LocationImpl(vm, rt, SAMethod, (long)jvf.getBCI());
71 }
72
73 private void validateStackFrame() {
74 if (!isValid) {
75 throw new InvalidStackFrameException("Thread has been resumed");
76 }
77 }
78
79 JavaVFrame getJavaVFrame() {
80 return saFrame;
81 }
82
83 /**
84 * Return the frame location.
85 * Need not be synchronized since it cannot be provably stale.
86 */
87 public Location location() {
88 validateStackFrame();
89 return location;
90 }
91
92 /**
93 * Return the thread holding the frame.
94 * Need not be synchronized since it cannot be provably stale.
95 */
96 public ThreadReference thread() {
97 validateStackFrame();
98 return thread;
99 }
100
101 public boolean equals(Object obj) {
102 if ((obj != null) && (obj instanceof StackFrameImpl)) {
103 StackFrameImpl other = (StackFrameImpl)obj;
104 return (saFrame.equals(other.saFrame));
105 } else {
106 return false;
107 }
108 }
109
110 public int hashCode() {
111 return saFrame.hashCode();
112 }
113
114 public ObjectReference thisObject() {
115 validateStackFrame();
116 MethodImpl currentMethod = (MethodImpl)location.method();
117 if (currentMethod.isStatic() || currentMethod.isNative()) {
118 return null;
119 }
120 if (thisObject == null) {
121 StackValueCollection values = saFrame.getLocals();
122 if (Assert.ASSERTS_ENABLED) {
123 Assert.that(values.size() > 0, "this is missing");
124 }
125 // 'this' at index 0.
126 OopHandle handle = values.oopHandleAt(0);
127 ObjectHeap heap = vm.saObjectHeap();
128 thisObject = vm.objectMirror(heap.newOop(handle));
129 }
130 return thisObject;
131 }
132
133 /**
134 * Build the visible variable map.
135 * Need not be synchronized since it cannot be provably stale.
136 */
137 private void createVisibleVariables() throws AbsentInformationException {
138 if (visibleVariables == null) {
139 List allVariables = location.method().variables();
140 Map map = new HashMap(allVariables.size());
141
142 Iterator iter = allVariables.iterator();
143 while (iter.hasNext()) {
144 LocalVariableImpl variable = (LocalVariableImpl)iter.next();
145 String name = variable.name();
146 if (variable.isVisible(this)) {
147 LocalVariable existing = (LocalVariable)map.get(name);
148 if ((existing == null) ||
149 variable.hides(existing)) {
150 map.put(name, variable);
151 }
152 }
153 }
154 visibleVariables = map;
155 }
156 }
157
158 /**
159 * Return the list of visible variable in the frame.
160 * Need not be synchronized since it cannot be provably stale.
161 */
162 public List visibleVariables() throws AbsentInformationException {
163 validateStackFrame();
164 createVisibleVariables();
165 List mapAsList = new ArrayList(visibleVariables.values());
166 Collections.sort(mapAsList);
167 return mapAsList;
168 }
169
170 /**
171 * Return a particular variable in the frame.
172 * Need not be synchronized since it cannot be provably stale.
173 */
174 public LocalVariable visibleVariableByName(String name) throws AbsentInformationException {
175 validateStackFrame();
176 createVisibleVariables();
177 return (LocalVariable)visibleVariables.get(name);
178 }
179
180 public Value getValue(LocalVariable variable) {
181 List list = new ArrayList(1);
182 list.add(variable);
183 Map map = getValues(list);
184 return (Value)map.get(variable);
185 }
186
187 public Map getValues(List variables) {
188 validateStackFrame();
189 StackValueCollection values = saFrame.getLocals();
190
191 int count = variables.size();
192 Map map = new HashMap(count);
193 for (int ii=0; ii<count; ++ii) {
194 LocalVariableImpl variable = (LocalVariableImpl)variables.get(ii);
195 if (!variable.isVisible(this)) {
196 throw new IllegalArgumentException(variable.name() +
197 " is not valid at this frame location");
198 }
199 ValueImpl valueImpl;
200 int ss = variable.slot();
201 char c = variable.signature().charAt(0);
202 BasicType variableType = BasicType.charToBasicType(c);
203 valueImpl = getSlotValue(values, variableType, ss);
204 map.put(variable, valueImpl);
205 }
206 return map;
207 }
208
209 public List getArgumentValues() {
210 validateStackFrame();
211 StackValueCollection values = saFrame.getLocals();
212 MethodImpl mmm = (MethodImpl)location.method();
213 List argSigs = mmm.argumentSignatures();
214 int count = argSigs.size();
215 List res = new ArrayList(0);
216
217 int slot = mmm.isStatic()? 0 : 1;
218 for (int ii = 0; ii < count; ++slot, ++ii) {
219 char sigChar = ((String)argSigs.get(ii)).charAt(0);
220 BasicType variableType = BasicType.charToBasicType(sigChar);
221 res.add(getSlotValue(values, variableType, slot));
222 if (sigChar == 'J' || sigChar == 'D') {
223 slot++;
224 }
225 }
226 return res;
227 }
228
229 private ValueImpl getSlotValue(StackValueCollection values,
230 BasicType variableType, int ss) {
231 ValueImpl valueImpl = null;
232 OopHandle handle = null;
233 ObjectHeap heap = vm.saObjectHeap();
234 if (variableType == BasicType.T_BOOLEAN) {
235 valueImpl = (BooleanValueImpl) vm.mirrorOf(values.booleanAt(ss));
236 } else if (variableType == BasicType.T_CHAR) {
237 valueImpl = (CharValueImpl) vm.mirrorOf(values.charAt(ss));
238 } else if (variableType == BasicType.T_FLOAT) {
239 valueImpl = (FloatValueImpl) vm.mirrorOf(values.floatAt(ss));
240 } else if (variableType == BasicType.T_DOUBLE) {
241 valueImpl = (DoubleValueImpl) vm.mirrorOf(values.doubleAt(ss));
242 } else if (variableType == BasicType.T_BYTE) {
243 valueImpl = (ByteValueImpl) vm.mirrorOf(values.byteAt(ss));
244 } else if (variableType == BasicType.T_SHORT) {
245 valueImpl = (ShortValueImpl) vm.mirrorOf(values.shortAt(ss));
246 } else if (variableType == BasicType.T_INT) {
247 valueImpl = (IntegerValueImpl) vm.mirrorOf(values.intAt(ss));
248 } else if (variableType == BasicType.T_LONG) {
249 valueImpl = (LongValueImpl) vm.mirrorOf(values.longAt(ss));
250 } else if (variableType == BasicType.T_OBJECT) {
251 // we may have an [Ljava/lang/Object; - i.e., Object[] with the
252 // elements themselves may be arrays because every array is an Object.
253 handle = values.oopHandleAt(ss);
254 valueImpl = (ObjectReferenceImpl) vm.objectMirror(heap.newOop(handle));
255 } else if (variableType == BasicType.T_ARRAY) {
256 handle = values.oopHandleAt(ss);
257 valueImpl = vm.arrayMirror((Array)heap.newOop(handle));
258 } else if (variableType == BasicType.T_VOID) {
259 valueImpl = new VoidValueImpl(vm);
260 } else {
261 throw new RuntimeException("Should not read here");
262 }
263
264 return valueImpl;
265 }
266
267 public void setValue(LocalVariable variableIntf, Value valueIntf)
268 throws InvalidTypeException, ClassNotLoadedException {
269
270 vm.throwNotReadOnlyException("StackFrame.setValue()");
271 }
272
273 public String toString() {
274 return location.toString() + " in thread " + thread.toString();
275 }
276 }