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.*;
|
|
30 |
|
|
31 |
import java.util.ArrayList;
|
|
32 |
import java.util.List;
|
|
33 |
import java.util.Iterator;
|
|
34 |
|
|
35 |
class BreakpointSpec extends EventRequestSpec {
|
|
36 |
String methodId;
|
51
|
37 |
List<String> methodArgs;
|
2
|
38 |
int lineNumber;
|
|
39 |
|
|
40 |
BreakpointSpec(ReferenceTypeSpec refSpec, int lineNumber) {
|
|
41 |
super(refSpec);
|
|
42 |
this.methodId = null;
|
|
43 |
this.methodArgs = null;
|
|
44 |
this.lineNumber = lineNumber;
|
|
45 |
}
|
|
46 |
|
|
47 |
BreakpointSpec(ReferenceTypeSpec refSpec, String methodId,
|
51
|
48 |
List<String> methodArgs) throws MalformedMemberNameException {
|
2
|
49 |
super(refSpec);
|
|
50 |
this.methodId = methodId;
|
|
51 |
this.methodArgs = methodArgs;
|
|
52 |
this.lineNumber = 0;
|
|
53 |
if (!isValidMethodName(methodId)) {
|
|
54 |
throw new MalformedMemberNameException(methodId);
|
|
55 |
}
|
|
56 |
}
|
|
57 |
|
|
58 |
/**
|
|
59 |
* The 'refType' is known to match, return the EventRequest.
|
|
60 |
*/
|
|
61 |
EventRequest resolveEventRequest(ReferenceType refType)
|
|
62 |
throws AmbiguousMethodException,
|
|
63 |
AbsentInformationException,
|
|
64 |
InvalidTypeException,
|
|
65 |
NoSuchMethodException,
|
|
66 |
LineNotFoundException {
|
|
67 |
Location location = location(refType);
|
|
68 |
if (location == null) {
|
|
69 |
throw new InvalidTypeException();
|
|
70 |
}
|
|
71 |
EventRequestManager em = refType.virtualMachine().eventRequestManager();
|
|
72 |
EventRequest bp = em.createBreakpointRequest(location);
|
|
73 |
bp.setSuspendPolicy(suspendPolicy);
|
|
74 |
bp.enable();
|
|
75 |
return bp;
|
|
76 |
}
|
|
77 |
|
|
78 |
String methodName() {
|
|
79 |
return methodId;
|
|
80 |
}
|
|
81 |
|
|
82 |
int lineNumber() {
|
|
83 |
return lineNumber;
|
|
84 |
}
|
|
85 |
|
51
|
86 |
List<String> methodArgs() {
|
2
|
87 |
return methodArgs;
|
|
88 |
}
|
|
89 |
|
|
90 |
boolean isMethodBreakpoint() {
|
|
91 |
return (methodId != null);
|
|
92 |
}
|
|
93 |
|
|
94 |
public int hashCode() {
|
|
95 |
return refSpec.hashCode() + lineNumber +
|
|
96 |
((methodId != null) ? methodId.hashCode() : 0) +
|
|
97 |
((methodArgs != null) ? methodArgs.hashCode() : 0);
|
|
98 |
}
|
|
99 |
|
|
100 |
public boolean equals(Object obj) {
|
|
101 |
if (obj instanceof BreakpointSpec) {
|
|
102 |
BreakpointSpec breakpoint = (BreakpointSpec)obj;
|
|
103 |
|
|
104 |
return ((methodId != null) ?
|
|
105 |
methodId.equals(breakpoint.methodId)
|
|
106 |
: methodId == breakpoint.methodId) &&
|
|
107 |
((methodArgs != null) ?
|
|
108 |
methodArgs.equals(breakpoint.methodArgs)
|
|
109 |
: methodArgs == breakpoint.methodArgs) &&
|
|
110 |
refSpec.equals(breakpoint.refSpec) &&
|
|
111 |
(lineNumber == breakpoint.lineNumber);
|
|
112 |
} else {
|
|
113 |
return false;
|
|
114 |
}
|
|
115 |
}
|
|
116 |
|
|
117 |
String errorMessageFor(Exception e) {
|
|
118 |
if (e instanceof AmbiguousMethodException) {
|
|
119 |
return (MessageOutput.format("Method is overloaded; specify arguments",
|
|
120 |
methodName()));
|
|
121 |
/*
|
|
122 |
* TO DO: list the methods here
|
|
123 |
*/
|
|
124 |
} else if (e instanceof NoSuchMethodException) {
|
|
125 |
return (MessageOutput.format("No method in",
|
|
126 |
new Object [] {methodName(),
|
|
127 |
refSpec.toString()}));
|
|
128 |
} else if (e instanceof AbsentInformationException) {
|
|
129 |
return (MessageOutput.format("No linenumber information for",
|
|
130 |
refSpec.toString()));
|
|
131 |
} else if (e instanceof LineNotFoundException) {
|
|
132 |
return (MessageOutput.format("No code at line",
|
|
133 |
new Object [] {new Long (lineNumber()),
|
|
134 |
refSpec.toString()}));
|
|
135 |
} else if (e instanceof InvalidTypeException) {
|
|
136 |
return (MessageOutput.format("Breakpoints can be located only in classes.",
|
|
137 |
refSpec.toString()));
|
|
138 |
} else {
|
|
139 |
return super.errorMessageFor( e);
|
|
140 |
}
|
|
141 |
}
|
|
142 |
|
|
143 |
public String toString() {
|
|
144 |
StringBuffer buffer = new StringBuffer(refSpec.toString());
|
|
145 |
if (isMethodBreakpoint()) {
|
|
146 |
buffer.append('.');
|
|
147 |
buffer.append(methodId);
|
|
148 |
if (methodArgs != null) {
|
|
149 |
boolean first = true;
|
|
150 |
buffer.append('(');
|
51
|
151 |
for (String arg : methodArgs) {
|
2
|
152 |
if (!first) {
|
|
153 |
buffer.append(',');
|
|
154 |
}
|
51
|
155 |
buffer.append(arg);
|
2
|
156 |
first = false;
|
|
157 |
}
|
|
158 |
buffer.append(")");
|
|
159 |
}
|
|
160 |
} else {
|
|
161 |
buffer.append(':');
|
|
162 |
buffer.append(lineNumber);
|
|
163 |
}
|
|
164 |
return MessageOutput.format("breakpoint", buffer.toString());
|
|
165 |
}
|
|
166 |
|
|
167 |
private Location location(ReferenceType refType) throws
|
|
168 |
AmbiguousMethodException,
|
|
169 |
AbsentInformationException,
|
|
170 |
NoSuchMethodException,
|
|
171 |
LineNotFoundException {
|
|
172 |
Location location = null;
|
|
173 |
if (isMethodBreakpoint()) {
|
|
174 |
Method method = findMatchingMethod(refType);
|
|
175 |
location = method.location();
|
|
176 |
} else {
|
|
177 |
// let AbsentInformationException be thrown
|
51
|
178 |
List<Location> locs = refType.locationsOfLine(lineNumber());
|
2
|
179 |
if (locs.size() == 0) {
|
|
180 |
throw new LineNotFoundException();
|
|
181 |
}
|
|
182 |
// TO DO: handle multiple locations
|
51
|
183 |
location = locs.get(0);
|
2
|
184 |
if (location.method() == null) {
|
|
185 |
throw new LineNotFoundException();
|
|
186 |
}
|
|
187 |
}
|
|
188 |
return location;
|
|
189 |
}
|
|
190 |
|
|
191 |
private boolean isValidMethodName(String s) {
|
|
192 |
return isJavaIdentifier(s) ||
|
|
193 |
s.equals("<init>") ||
|
|
194 |
s.equals("<clinit>");
|
|
195 |
}
|
|
196 |
|
|
197 |
/*
|
|
198 |
* Compare a method's argument types with a Vector of type names.
|
|
199 |
* Return true if each argument type has a name identical to the
|
|
200 |
* corresponding string in the vector (allowing for varars)
|
|
201 |
* and if the number of arguments in the method matches the
|
|
202 |
* number of names passed
|
|
203 |
*/
|
51
|
204 |
private boolean compareArgTypes(Method method, List<String> nameList) {
|
|
205 |
List<String> argTypeNames = method.argumentTypeNames();
|
2
|
206 |
|
|
207 |
// If argument counts differ, we can stop here
|
|
208 |
if (argTypeNames.size() != nameList.size()) {
|
|
209 |
return false;
|
|
210 |
}
|
|
211 |
|
|
212 |
// Compare each argument type's name
|
|
213 |
int nTypes = argTypeNames.size();
|
|
214 |
for (int i = 0; i < nTypes; ++i) {
|
51
|
215 |
String comp1 = argTypeNames.get(i);
|
|
216 |
String comp2 = nameList.get(i);
|
2
|
217 |
if (! comp1.equals(comp2)) {
|
|
218 |
/*
|
|
219 |
* We have to handle varargs. EG, the
|
|
220 |
* method's last arg type is xxx[]
|
|
221 |
* while the nameList contains xxx...
|
|
222 |
* Note that the nameList can also contain
|
|
223 |
* xxx[] in which case we don't get here.
|
|
224 |
*/
|
|
225 |
if (i != nTypes - 1 ||
|
|
226 |
!method.isVarArgs() ||
|
|
227 |
!comp2.endsWith("...")) {
|
|
228 |
return false;
|
|
229 |
}
|
|
230 |
/*
|
|
231 |
* The last types differ, it is a varargs
|
|
232 |
* method and the nameList item is varargs.
|
|
233 |
* We just have to compare the type names, eg,
|
|
234 |
* make sure we don't have xxx[] for the method
|
|
235 |
* arg type and yyy... for the nameList item.
|
|
236 |
*/
|
|
237 |
int comp1Length = comp1.length();
|
|
238 |
if (comp1Length + 1 != comp2.length()) {
|
|
239 |
// The type names are different lengths
|
|
240 |
return false;
|
|
241 |
}
|
|
242 |
// We know the two type names are the same length
|
|
243 |
if (!comp1.regionMatches(0, comp2, 0, comp1Length - 2)) {
|
|
244 |
return false;
|
|
245 |
}
|
|
246 |
// We do have xxx[] and xxx... as the last param type
|
|
247 |
return true;
|
|
248 |
}
|
|
249 |
}
|
|
250 |
|
|
251 |
return true;
|
|
252 |
}
|
|
253 |
|
|
254 |
|
|
255 |
/*
|
|
256 |
* Remove unneeded spaces and expand class names to fully
|
|
257 |
* qualified names, if necessary and possible.
|
|
258 |
*/
|
|
259 |
private String normalizeArgTypeName(String name) {
|
|
260 |
/*
|
|
261 |
* Separate the type name from any array modifiers,
|
|
262 |
* stripping whitespace after the name ends
|
|
263 |
*/
|
|
264 |
int i = 0;
|
|
265 |
StringBuffer typePart = new StringBuffer();
|
|
266 |
StringBuffer arrayPart = new StringBuffer();
|
|
267 |
name = name.trim();
|
|
268 |
int nameLength = name.length();
|
|
269 |
/*
|
|
270 |
* For varargs, there can be spaces before the ... but not
|
|
271 |
* within the ... So, we will just ignore the ...
|
|
272 |
* while stripping blanks.
|
|
273 |
*/
|
|
274 |
boolean isVarArgs = name.endsWith("...");
|
|
275 |
if (isVarArgs) {
|
|
276 |
nameLength -= 3;
|
|
277 |
}
|
|
278 |
while (i < nameLength) {
|
|
279 |
char c = name.charAt(i);
|
|
280 |
if (Character.isWhitespace(c) || c == '[') {
|
|
281 |
break; // name is complete
|
|
282 |
}
|
|
283 |
typePart.append(c);
|
|
284 |
i++;
|
|
285 |
}
|
|
286 |
while (i < nameLength) {
|
|
287 |
char c = name.charAt(i);
|
|
288 |
if ( (c == '[') || (c == ']')) {
|
|
289 |
arrayPart.append(c);
|
|
290 |
} else if (!Character.isWhitespace(c)) {
|
|
291 |
throw new IllegalArgumentException
|
|
292 |
(MessageOutput.format("Invalid argument type name"));
|
|
293 |
}
|
|
294 |
i++;
|
|
295 |
}
|
|
296 |
name = typePart.toString();
|
|
297 |
|
|
298 |
/*
|
|
299 |
* When there's no sign of a package name already, try to expand the
|
|
300 |
* the name to a fully qualified class name
|
|
301 |
*/
|
|
302 |
if ((name.indexOf('.') == -1) || name.startsWith("*.")) {
|
|
303 |
try {
|
|
304 |
ReferenceType argClass = Env.getReferenceTypeFromToken(name);
|
|
305 |
if (argClass != null) {
|
|
306 |
name = argClass.name();
|
|
307 |
}
|
|
308 |
} catch (IllegalArgumentException e) {
|
|
309 |
// We'll try the name as is
|
|
310 |
}
|
|
311 |
}
|
|
312 |
name += arrayPart.toString();
|
|
313 |
if (isVarArgs) {
|
|
314 |
name += "...";
|
|
315 |
}
|
|
316 |
return name;
|
|
317 |
}
|
|
318 |
|
|
319 |
/*
|
|
320 |
* Attempt an unambiguous match of the method name and
|
|
321 |
* argument specification to a method. If no arguments
|
|
322 |
* are specified, the method must not be overloaded.
|
|
323 |
* Otherwise, the argument types much match exactly
|
|
324 |
*/
|
|
325 |
private Method findMatchingMethod(ReferenceType refType)
|
|
326 |
throws AmbiguousMethodException,
|
|
327 |
NoSuchMethodException {
|
|
328 |
|
|
329 |
// Normalize the argument string once before looping below.
|
|
330 |
List<String> argTypeNames = null;
|
|
331 |
if (methodArgs() != null) {
|
|
332 |
argTypeNames = new ArrayList<String>(methodArgs().size());
|
51
|
333 |
for (String name : methodArgs()) {
|
2
|
334 |
name = normalizeArgTypeName(name);
|
|
335 |
argTypeNames.add(name);
|
|
336 |
}
|
|
337 |
}
|
|
338 |
|
|
339 |
// Check each method in the class for matches
|
|
340 |
Method firstMatch = null; // first method with matching name
|
|
341 |
Method exactMatch = null; // (only) method with same name & sig
|
|
342 |
int matchCount = 0; // > 1 implies overload
|
51
|
343 |
for (Method candidate : refType.methods()) {
|
2
|
344 |
if (candidate.name().equals(methodName())) {
|
|
345 |
matchCount++;
|
|
346 |
|
|
347 |
// Remember the first match in case it is the only one
|
|
348 |
if (matchCount == 1) {
|
|
349 |
firstMatch = candidate;
|
|
350 |
}
|
|
351 |
|
|
352 |
// If argument types were specified, check against candidate
|
|
353 |
if ((argTypeNames != null)
|
|
354 |
&& compareArgTypes(candidate, argTypeNames) == true) {
|
|
355 |
exactMatch = candidate;
|
|
356 |
break;
|
|
357 |
}
|
|
358 |
}
|
|
359 |
}
|
|
360 |
|
|
361 |
// Determine method for breakpoint
|
|
362 |
Method method = null;
|
|
363 |
if (exactMatch != null) {
|
|
364 |
// Name and signature match
|
|
365 |
method = exactMatch;
|
|
366 |
} else if ((argTypeNames == null) && (matchCount > 0)) {
|
|
367 |
// At least one name matched and no arg types were specified
|
|
368 |
if (matchCount == 1) {
|
|
369 |
method = firstMatch; // Only one match; safe to use it
|
|
370 |
} else {
|
|
371 |
throw new AmbiguousMethodException();
|
|
372 |
}
|
|
373 |
} else {
|
|
374 |
throw new NoSuchMethodException(methodName());
|
|
375 |
}
|
|
376 |
return method;
|
|
377 |
}
|
|
378 |
}
|