2
|
1 |
/*
|
715
|
2 |
* Copyright 1998-2008 Sun Microsystems, Inc. All Rights Reserved.
|
2
|
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. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.tools.example.debug.tty;
|
|
27 |
|
|
28 |
import com.sun.jdi.*;
|
|
29 |
import com.sun.jdi.request.StepRequest;
|
|
30 |
import com.sun.jdi.request.MethodEntryRequest;
|
|
31 |
import com.sun.jdi.request.MethodExitRequest;
|
|
32 |
import com.sun.tools.jdi.*;
|
|
33 |
import java.util.*;
|
|
34 |
import java.io.*;
|
|
35 |
|
|
36 |
|
|
37 |
class Env {
|
|
38 |
|
|
39 |
static EventRequestSpecList specList = new EventRequestSpecList();
|
|
40 |
|
|
41 |
private static VMConnection connection;
|
|
42 |
|
|
43 |
private static SourceMapper sourceMapper = new SourceMapper("");
|
|
44 |
private static List<String> excludes;
|
|
45 |
|
|
46 |
private static final int SOURCE_CACHE_SIZE = 5;
|
|
47 |
private static List<SourceCode> sourceCache = new LinkedList<SourceCode>();
|
|
48 |
|
|
49 |
private static HashMap<String, Value> savedValues = new HashMap<String, Value>();
|
|
50 |
private static Method atExitMethod;
|
|
51 |
|
|
52 |
static void init(String connectSpec, boolean openNow, int flags) {
|
|
53 |
connection = new VMConnection(connectSpec, flags);
|
|
54 |
if (!connection.isLaunch() || openNow) {
|
|
55 |
connection.open();
|
|
56 |
}
|
|
57 |
}
|
|
58 |
|
|
59 |
static VMConnection connection() {
|
|
60 |
return connection;
|
|
61 |
}
|
|
62 |
|
|
63 |
static VirtualMachine vm() {
|
|
64 |
return connection.vm();
|
|
65 |
}
|
|
66 |
|
|
67 |
static void shutdown() {
|
|
68 |
shutdown(null);
|
|
69 |
}
|
|
70 |
|
|
71 |
static void shutdown(String message) {
|
|
72 |
if (connection != null) {
|
|
73 |
try {
|
|
74 |
connection.disposeVM();
|
|
75 |
} catch (VMDisconnectedException e) {
|
|
76 |
// Shutting down after the VM has gone away. This is
|
|
77 |
// not an error, and we just ignore it.
|
|
78 |
}
|
|
79 |
}
|
|
80 |
if (message != null) {
|
|
81 |
MessageOutput.lnprint(message);
|
|
82 |
MessageOutput.println();
|
|
83 |
}
|
|
84 |
System.exit(0);
|
|
85 |
}
|
|
86 |
|
|
87 |
static void setSourcePath(String srcPath) {
|
|
88 |
sourceMapper = new SourceMapper(srcPath);
|
|
89 |
sourceCache.clear();
|
|
90 |
}
|
|
91 |
|
51
|
92 |
static void setSourcePath(List<String> srcList) {
|
2
|
93 |
sourceMapper = new SourceMapper(srcList);
|
|
94 |
sourceCache.clear();
|
|
95 |
}
|
|
96 |
|
|
97 |
static String getSourcePath() {
|
|
98 |
return sourceMapper.getSourcePath();
|
|
99 |
}
|
|
100 |
|
|
101 |
static private List<String> excludes() {
|
|
102 |
if (excludes == null) {
|
|
103 |
setExcludes("java.*, javax.*, sun.*, com.sun.*");
|
|
104 |
}
|
|
105 |
return excludes;
|
|
106 |
}
|
|
107 |
|
|
108 |
static String excludesString() {
|
|
109 |
StringBuffer buffer = new StringBuffer();
|
51
|
110 |
for (String pattern : excludes()) {
|
2
|
111 |
buffer.append(pattern);
|
|
112 |
buffer.append(",");
|
|
113 |
}
|
|
114 |
return buffer.toString();
|
|
115 |
}
|
|
116 |
|
|
117 |
static void addExcludes(StepRequest request) {
|
51
|
118 |
for (String pattern : excludes()) {
|
2
|
119 |
request.addClassExclusionFilter(pattern);
|
|
120 |
}
|
|
121 |
}
|
|
122 |
|
|
123 |
static void addExcludes(MethodEntryRequest request) {
|
51
|
124 |
for (String pattern : excludes()) {
|
2
|
125 |
request.addClassExclusionFilter(pattern);
|
|
126 |
}
|
|
127 |
}
|
|
128 |
|
|
129 |
static void addExcludes(MethodExitRequest request) {
|
51
|
130 |
for (String pattern : excludes()) {
|
2
|
131 |
request.addClassExclusionFilter(pattern);
|
|
132 |
}
|
|
133 |
}
|
|
134 |
|
|
135 |
static void setExcludes(String excludeString) {
|
|
136 |
StringTokenizer t = new StringTokenizer(excludeString, " ,;");
|
|
137 |
List<String> list = new ArrayList<String>();
|
|
138 |
while (t.hasMoreTokens()) {
|
|
139 |
list.add(t.nextToken());
|
|
140 |
}
|
|
141 |
excludes = list;
|
|
142 |
}
|
|
143 |
|
|
144 |
static Method atExitMethod() {
|
|
145 |
return atExitMethod;
|
|
146 |
}
|
|
147 |
|
|
148 |
static void setAtExitMethod(Method mmm) {
|
|
149 |
atExitMethod = mmm;
|
|
150 |
}
|
|
151 |
|
|
152 |
/**
|
|
153 |
* Return a Reader cooresponding to the source of this location.
|
|
154 |
* Return null if not available.
|
|
155 |
* Note: returned reader must be closed.
|
|
156 |
*/
|
|
157 |
static BufferedReader sourceReader(Location location) {
|
|
158 |
return sourceMapper.sourceReader(location);
|
|
159 |
}
|
|
160 |
|
|
161 |
static synchronized String sourceLine(Location location, int lineNumber)
|
|
162 |
throws IOException {
|
|
163 |
if (lineNumber == -1) {
|
|
164 |
throw new IllegalArgumentException();
|
|
165 |
}
|
|
166 |
|
|
167 |
try {
|
|
168 |
String fileName = location.sourceName();
|
|
169 |
|
51
|
170 |
Iterator<SourceCode> iter = sourceCache.iterator();
|
2
|
171 |
SourceCode code = null;
|
|
172 |
while (iter.hasNext()) {
|
51
|
173 |
SourceCode candidate = iter.next();
|
2
|
174 |
if (candidate.fileName().equals(fileName)) {
|
|
175 |
code = candidate;
|
|
176 |
iter.remove();
|
|
177 |
break;
|
|
178 |
}
|
|
179 |
}
|
|
180 |
if (code == null) {
|
|
181 |
BufferedReader reader = sourceReader(location);
|
|
182 |
if (reader == null) {
|
|
183 |
throw new FileNotFoundException(fileName);
|
|
184 |
}
|
|
185 |
code = new SourceCode(fileName, reader);
|
|
186 |
if (sourceCache.size() == SOURCE_CACHE_SIZE) {
|
|
187 |
sourceCache.remove(sourceCache.size() - 1);
|
|
188 |
}
|
|
189 |
}
|
|
190 |
sourceCache.add(0, code);
|
|
191 |
return code.sourceLine(lineNumber);
|
|
192 |
} catch (AbsentInformationException e) {
|
|
193 |
throw new IllegalArgumentException();
|
|
194 |
}
|
|
195 |
}
|
|
196 |
|
|
197 |
/** Return a description of an object. */
|
|
198 |
static String description(ObjectReference ref) {
|
|
199 |
ReferenceType clazz = ref.referenceType();
|
|
200 |
long id = ref.uniqueID();
|
|
201 |
if (clazz == null) {
|
|
202 |
return toHex(id);
|
|
203 |
} else {
|
|
204 |
return MessageOutput.format("object description and hex id",
|
|
205 |
new Object [] {clazz.name(),
|
|
206 |
toHex(id)});
|
|
207 |
}
|
|
208 |
}
|
|
209 |
|
|
210 |
/** Convert a long to a hexadecimal string. */
|
|
211 |
static String toHex(long n) {
|
|
212 |
char s1[] = new char[16];
|
|
213 |
char s2[] = new char[18];
|
|
214 |
|
|
215 |
/* Store digits in reverse order. */
|
|
216 |
int i = 0;
|
|
217 |
do {
|
|
218 |
long d = n & 0xf;
|
|
219 |
s1[i++] = (char)((d < 10) ? ('0' + d) : ('a' + d - 10));
|
|
220 |
} while ((n >>>= 4) > 0);
|
|
221 |
|
|
222 |
/* Now reverse the array. */
|
|
223 |
s2[0] = '0';
|
|
224 |
s2[1] = 'x';
|
|
225 |
int j = 2;
|
|
226 |
while (--i >= 0) {
|
|
227 |
s2[j++] = s1[i];
|
|
228 |
}
|
|
229 |
return new String(s2, 0, j);
|
|
230 |
}
|
|
231 |
|
|
232 |
/** Convert hexadecimal strings to longs. */
|
|
233 |
static long fromHex(String hexStr) {
|
|
234 |
String str = hexStr.startsWith("0x") ?
|
|
235 |
hexStr.substring(2).toLowerCase() : hexStr.toLowerCase();
|
|
236 |
if (hexStr.length() == 0) {
|
|
237 |
throw new NumberFormatException();
|
|
238 |
}
|
|
239 |
|
|
240 |
long ret = 0;
|
|
241 |
for (int i = 0; i < str.length(); i++) {
|
|
242 |
int c = str.charAt(i);
|
|
243 |
if (c >= '0' && c <= '9') {
|
|
244 |
ret = (ret * 16) + (c - '0');
|
|
245 |
} else if (c >= 'a' && c <= 'f') {
|
|
246 |
ret = (ret * 16) + (c - 'a' + 10);
|
|
247 |
} else {
|
|
248 |
throw new NumberFormatException();
|
|
249 |
}
|
|
250 |
}
|
|
251 |
return ret;
|
|
252 |
}
|
|
253 |
|
|
254 |
static ReferenceType getReferenceTypeFromToken(String idToken) {
|
|
255 |
ReferenceType cls = null;
|
|
256 |
if (Character.isDigit(idToken.charAt(0))) {
|
|
257 |
cls = null;
|
|
258 |
} else if (idToken.startsWith("*.")) {
|
|
259 |
// This notation saves typing by letting the user omit leading
|
|
260 |
// package names. The first
|
|
261 |
// loaded class whose name matches this limited regular
|
|
262 |
// expression is selected.
|
|
263 |
idToken = idToken.substring(1);
|
51
|
264 |
for (ReferenceType type : Env.vm().allClasses()) {
|
2
|
265 |
if (type.name().endsWith(idToken)) {
|
|
266 |
cls = type;
|
|
267 |
break;
|
|
268 |
}
|
|
269 |
}
|
|
270 |
} else {
|
|
271 |
// It's a class name
|
51
|
272 |
List<ReferenceType> classes = Env.vm().classesByName(idToken);
|
2
|
273 |
if (classes.size() > 0) {
|
|
274 |
// TO DO: handle multiples
|
51
|
275 |
cls = classes.get(0);
|
2
|
276 |
}
|
|
277 |
}
|
|
278 |
return cls;
|
|
279 |
}
|
|
280 |
|
51
|
281 |
static Set<String> getSaveKeys() {
|
2
|
282 |
return savedValues.keySet();
|
|
283 |
}
|
|
284 |
|
|
285 |
static Value getSavedValue(String key) {
|
51
|
286 |
return savedValues.get(key);
|
2
|
287 |
}
|
|
288 |
|
|
289 |
static void setSavedValue(String key, Value value) {
|
|
290 |
savedValues.put(key, value);
|
|
291 |
}
|
|
292 |
|
|
293 |
static class SourceCode {
|
|
294 |
private String fileName;
|
|
295 |
private List<String> sourceLines = new ArrayList<String>();
|
|
296 |
|
|
297 |
SourceCode(String fileName, BufferedReader reader) throws IOException {
|
|
298 |
this.fileName = fileName;
|
|
299 |
try {
|
|
300 |
String line = reader.readLine();
|
|
301 |
while (line != null) {
|
|
302 |
sourceLines.add(line);
|
|
303 |
line = reader.readLine();
|
|
304 |
}
|
|
305 |
} finally {
|
|
306 |
reader.close();
|
|
307 |
}
|
|
308 |
}
|
|
309 |
|
|
310 |
String fileName() {
|
|
311 |
return fileName;
|
|
312 |
}
|
|
313 |
|
|
314 |
String sourceLine(int number) {
|
|
315 |
int index = number - 1; // list is 0-indexed
|
|
316 |
if (index >= sourceLines.size()) {
|
|
317 |
return null;
|
|
318 |
} else {
|
51
|
319 |
return sourceLines.get(index);
|
2
|
320 |
}
|
|
321 |
}
|
|
322 |
}
|
|
323 |
}
|