0
|
1 /*
|
|
2 * Copyright 2004-2007 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.utilities.soql;
|
|
26
|
|
27 import java.util.*;
|
|
28 import sun.jvm.hotspot.debugger.*;
|
|
29 import sun.jvm.hotspot.oops.*;
|
|
30 import sun.jvm.hotspot.runtime.*;
|
|
31
|
|
32 public class JSJavaFrame extends DefaultScriptObject {
|
|
33 private static final int FIELD_METHOD = 0;
|
|
34 private static final int FIELD_BCI = 1;
|
|
35 private static final int FIELD_LINE_NUMBER = 2;
|
|
36 private static final int FIELD_LOCALS = 3;
|
|
37 private static final int FIELD_THIS_OBJECT = 4;
|
|
38 private static final int FIELD_THREAD = 5;
|
|
39 private static final int FIELD_UNDEFINED = -1;
|
|
40
|
|
41 public JSJavaFrame(JavaVFrame jvf, JSJavaFactory fac) {
|
|
42 this.jvf = jvf;
|
|
43 this.factory = fac;
|
|
44 }
|
|
45
|
|
46 public Object get(String name) {
|
|
47 int fieldID = getFieldID(name);
|
|
48 switch (fieldID) {
|
|
49 case FIELD_METHOD:
|
|
50 return getMethod();
|
|
51 case FIELD_BCI:
|
|
52 return new Integer(getBCI());
|
|
53 case FIELD_LINE_NUMBER:
|
|
54 return new Integer(getLineNumber());
|
|
55 case FIELD_LOCALS:
|
|
56 return getLocals();
|
|
57 case FIELD_THIS_OBJECT:
|
|
58 return getThisObject();
|
|
59 case FIELD_THREAD:
|
|
60 return getThread();
|
|
61 case FIELD_UNDEFINED:
|
|
62 default:
|
|
63 return super.get(name);
|
|
64 }
|
|
65 }
|
|
66
|
|
67 public Object[] getIds() {
|
|
68 Object[] fieldNames = fields.keySet().toArray();
|
|
69 Object[] superFields = super.getIds();
|
|
70 Object[] res = new Object[fieldNames.length + superFields.length];
|
|
71 System.arraycopy(fieldNames, 0, res, 0, fieldNames.length);
|
|
72 System.arraycopy(superFields, 0, res, fieldNames.length, superFields.length);
|
|
73 return res;
|
|
74 }
|
|
75
|
|
76 public boolean has(String name) {
|
|
77 if (getFieldID(name) != FIELD_UNDEFINED) {
|
|
78 return true;
|
|
79 } else {
|
|
80 return super.has(name);
|
|
81 }
|
|
82 }
|
|
83
|
|
84 public void put(String name, Object value) {
|
|
85 if (getFieldID(name) == FIELD_UNDEFINED) {
|
|
86 super.put(name, value);
|
|
87 }
|
|
88 }
|
|
89
|
|
90 public String toString() {
|
|
91 StringBuffer buf = new StringBuffer();
|
|
92 buf.append("Frame (method=");
|
|
93 buf.append(jvf.getMethod().externalNameAndSignature());
|
|
94 buf.append(", bci=");
|
|
95 buf.append(getBCI());
|
|
96 buf.append(", line=");
|
|
97 buf.append(getLineNumber());
|
|
98 buf.append(')');
|
|
99 return buf.toString();
|
|
100 }
|
|
101
|
|
102 //-- Internals only below this point
|
|
103 private static Map fields = new HashMap();
|
|
104 private static void addField(String name, int fieldId) {
|
|
105 fields.put(name, new Integer(fieldId));
|
|
106 }
|
|
107
|
|
108 private static int getFieldID(String name) {
|
|
109 Integer res = (Integer) fields.get(name);
|
|
110 return (res != null)? res.intValue() : FIELD_UNDEFINED;
|
|
111 }
|
|
112
|
|
113 static {
|
|
114 addField("method", FIELD_METHOD);
|
|
115 addField("bci", FIELD_BCI);
|
|
116 addField("line", FIELD_LINE_NUMBER);
|
|
117 addField("locals", FIELD_LOCALS);
|
|
118 addField("thisObject", FIELD_THIS_OBJECT);
|
|
119 addField("thread", FIELD_THREAD);
|
|
120 }
|
|
121
|
|
122 private JSJavaObject getMethod() {
|
|
123 return factory.newJSJavaObject(jvf.getMethod());
|
|
124 }
|
|
125
|
|
126 private int getBCI() {
|
|
127 return jvf.getBCI();
|
|
128 }
|
|
129
|
|
130 private int getLineNumber() {
|
|
131 int bci = jvf.getBCI();
|
|
132 if (bci == -1) {
|
|
133 return 0;
|
|
134 } else {
|
|
135 int lineNum = jvf.getMethod().getLineNumberFromBCI(bci);
|
|
136 return (lineNum <= 0)? 0 : lineNum;
|
|
137 }
|
|
138 }
|
|
139
|
|
140 private synchronized JSMap getLocals() {
|
|
141 if (localsCache == null) {
|
|
142 Map map = new HashMap();
|
|
143 localsCache = factory.newJSMap(map);
|
|
144 StackValueCollection values = jvf.getLocals();
|
|
145 Method method = jvf.getMethod();
|
|
146 if (method.isNative() || ! method.hasLocalVariableTable() ||
|
|
147 values == null) {
|
|
148 return localsCache;
|
|
149 }
|
|
150
|
|
151 LocalVariableTableElement[] localVars = method.getLocalVariableTable();
|
|
152 int bci = getBCI();
|
|
153 List visibleVars = new ArrayList(0);
|
|
154 for (int i = 0; i < localVars.length; i++) {
|
|
155 LocalVariableTableElement cur = localVars[i];
|
|
156 if (cur.getStartBCI() >= bci && cur.getLength() > 0) {
|
|
157 visibleVars.add(cur);
|
|
158 }
|
|
159 }
|
|
160
|
|
161 OopHandle handle = null;
|
|
162 ObjectHeap heap = VM.getVM().getObjectHeap();
|
|
163 for (Iterator varItr = visibleVars.iterator(); varItr.hasNext();) {
|
|
164 LocalVariableTableElement cur = (LocalVariableTableElement) varItr.next();
|
|
165 String name = method.getConstants().getSymbolAt(cur.getNameCPIndex()).asString();
|
|
166 int slot = cur.getSlot();
|
|
167
|
|
168 String signature = method.getConstants().getSymbolAt(cur.getDescriptorCPIndex()).asString();
|
|
169 BasicType variableType = BasicType.charToBasicType(signature.charAt(0));
|
|
170 Object value = null;
|
|
171 if (variableType == BasicType.T_BOOLEAN) {
|
|
172 value = Boolean.valueOf(values.booleanAt(slot));
|
|
173 } else if (variableType == BasicType.T_CHAR) {
|
|
174 value = new Character(values.charAt(slot));
|
|
175 } else if (variableType == BasicType.T_FLOAT) {
|
|
176 value = new Float(values.floatAt(slot));
|
|
177 } else if (variableType == BasicType.T_DOUBLE) {
|
|
178 value = new Double(values.doubleAt(slot));
|
|
179 } else if (variableType == BasicType.T_BYTE) {
|
|
180 value = new Byte(values.byteAt(slot));
|
|
181 } else if (variableType == BasicType.T_SHORT) {
|
|
182 value = new Short(values.shortAt(slot));
|
|
183 } else if (variableType == BasicType.T_INT) {
|
|
184 value = new Integer(values.intAt(slot));
|
|
185 } else if (variableType == BasicType.T_LONG) {
|
|
186 value = new Long(values.longAt(slot));
|
|
187 } else if (variableType == BasicType.T_OBJECT ||
|
|
188 variableType == BasicType.T_ARRAY) {
|
|
189 handle = values.oopHandleAt(slot);
|
|
190 value = factory.newJSJavaObject(heap.newOop(handle));
|
|
191 } else {
|
|
192 // ignore
|
|
193 }
|
|
194 map.put(name, value);
|
|
195 }
|
|
196 }
|
|
197 return localsCache;
|
|
198 }
|
|
199
|
|
200 private JSJavaObject getThisObject() {
|
|
201 Method method = jvf.getMethod();
|
|
202 if (method.isStatic()) {
|
|
203 return null;
|
|
204 }
|
|
205 StackValueCollection values = jvf.getLocals();
|
|
206 if (values != null) {
|
|
207 // 'this' at index 0.
|
|
208 OopHandle handle = values.oopHandleAt(0);
|
|
209 ObjectHeap heap = VM.getVM().getObjectHeap();
|
|
210 return factory.newJSJavaObject(heap.newOop(handle));
|
|
211 } else {
|
|
212 // can't get locals, return null.
|
|
213 return null;
|
|
214 }
|
|
215 }
|
|
216
|
|
217 private JSJavaThread getThread() {
|
|
218 return factory.newJSJavaThread(jvf.getThread());
|
|
219 }
|
|
220
|
|
221 private final JavaVFrame jvf;
|
|
222 private final JSJavaFactory factory;
|
|
223 private JSMap localsCache;
|
|
224 }
|