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.EventRequest;
|
|
30 |
import com.sun.jdi.request.ExceptionRequest;
|
|
31 |
import com.sun.jdi.request.ClassPrepareRequest;
|
|
32 |
import com.sun.jdi.event.ClassPrepareEvent;
|
|
33 |
import java.util.List;
|
|
34 |
import java.util.ArrayList;
|
|
35 |
import java.util.Iterator;
|
|
36 |
|
|
37 |
abstract class EventRequestSpec {
|
|
38 |
|
|
39 |
final ReferenceTypeSpec refSpec;
|
|
40 |
|
|
41 |
int suspendPolicy = EventRequest.SUSPEND_ALL;
|
|
42 |
|
|
43 |
EventRequest resolved = null;
|
|
44 |
ClassPrepareRequest prepareRequest = null;
|
|
45 |
|
|
46 |
EventRequestSpec(ReferenceTypeSpec refSpec) {
|
|
47 |
this.refSpec = refSpec;
|
|
48 |
}
|
|
49 |
|
|
50 |
/**
|
|
51 |
* The 'refType' is known to match, return the EventRequest.
|
|
52 |
*/
|
|
53 |
abstract EventRequest resolveEventRequest(ReferenceType refType)
|
|
54 |
throws Exception;
|
|
55 |
|
|
56 |
/**
|
|
57 |
* @return If this EventRequestSpec matches the 'refType'
|
|
58 |
* return the cooresponding EventRequest. Otherwise
|
|
59 |
* return null.
|
|
60 |
*/
|
|
61 |
synchronized EventRequest resolve(ClassPrepareEvent event) throws Exception {
|
|
62 |
if ((resolved == null) &&
|
|
63 |
(prepareRequest != null) &&
|
|
64 |
prepareRequest.equals(event.request())) {
|
|
65 |
|
|
66 |
resolved = resolveEventRequest(event.referenceType());
|
|
67 |
prepareRequest.disable();
|
|
68 |
Env.vm().eventRequestManager().deleteEventRequest(prepareRequest);
|
|
69 |
prepareRequest = null;
|
|
70 |
|
|
71 |
if (refSpec instanceof PatternReferenceTypeSpec) {
|
|
72 |
PatternReferenceTypeSpec prs = (PatternReferenceTypeSpec)refSpec;
|
|
73 |
if (! prs.isUnique()){
|
|
74 |
/*
|
|
75 |
* Class pattern event requests are never
|
|
76 |
* considered "resolved", since future class loads
|
|
77 |
* might also match.
|
|
78 |
* Create and enable a new ClassPrepareRequest to
|
|
79 |
* keep trying to resolve.
|
|
80 |
*/
|
|
81 |
resolved = null;
|
|
82 |
prepareRequest = refSpec.createPrepareRequest();
|
|
83 |
prepareRequest.enable();
|
|
84 |
}
|
|
85 |
}
|
|
86 |
}
|
|
87 |
return resolved;
|
|
88 |
}
|
|
89 |
|
|
90 |
synchronized void remove() {
|
|
91 |
if (isResolved()) {
|
|
92 |
Env.vm().eventRequestManager().deleteEventRequest(resolved());
|
|
93 |
}
|
|
94 |
if (refSpec instanceof PatternReferenceTypeSpec) {
|
|
95 |
PatternReferenceTypeSpec prs = (PatternReferenceTypeSpec)refSpec;
|
|
96 |
if (! prs.isUnique()){
|
|
97 |
/*
|
|
98 |
* This is a class pattern. Track down and delete
|
|
99 |
* all EventRequests matching this spec.
|
|
100 |
* Note: Class patterns apply only to ExceptionRequests,
|
|
101 |
* so that is all we need to examine.
|
|
102 |
*/
|
|
103 |
ArrayList<ExceptionRequest> deleteList = new ArrayList<ExceptionRequest>();
|
51
|
104 |
for (ExceptionRequest er :
|
|
105 |
Env.vm().eventRequestManager().exceptionRequests()) {
|
2
|
106 |
if (prs.matches(er.exception())) {
|
|
107 |
deleteList.add (er);
|
|
108 |
}
|
|
109 |
}
|
|
110 |
Env.vm().eventRequestManager().deleteEventRequests(deleteList);
|
|
111 |
}
|
|
112 |
}
|
|
113 |
}
|
|
114 |
|
|
115 |
private EventRequest resolveAgainstPreparedClasses() throws Exception {
|
51
|
116 |
for (ReferenceType refType : Env.vm().allClasses()) {
|
2
|
117 |
if (refType.isPrepared() && refSpec.matches(refType)) {
|
|
118 |
resolved = resolveEventRequest(refType);
|
|
119 |
}
|
|
120 |
}
|
|
121 |
return resolved;
|
|
122 |
}
|
|
123 |
|
|
124 |
synchronized EventRequest resolveEagerly() throws Exception {
|
|
125 |
try {
|
|
126 |
if (resolved == null) {
|
|
127 |
/*
|
|
128 |
* Not resolved. Schedule a prepare request so we
|
|
129 |
* can resolve later.
|
|
130 |
*/
|
|
131 |
prepareRequest = refSpec.createPrepareRequest();
|
|
132 |
prepareRequest.enable();
|
|
133 |
|
|
134 |
// Try to resolve in case the class is already loaded.
|
|
135 |
resolveAgainstPreparedClasses();
|
|
136 |
if (resolved != null) {
|
|
137 |
prepareRequest.disable();
|
|
138 |
Env.vm().eventRequestManager().deleteEventRequest(prepareRequest);
|
|
139 |
prepareRequest = null;
|
|
140 |
}
|
|
141 |
}
|
|
142 |
if (refSpec instanceof PatternReferenceTypeSpec) {
|
|
143 |
PatternReferenceTypeSpec prs = (PatternReferenceTypeSpec)refSpec;
|
|
144 |
if (! prs.isUnique()){
|
|
145 |
/*
|
|
146 |
* Class pattern event requests are never
|
|
147 |
* considered "resolved", since future class loads
|
|
148 |
* might also match. Create a new
|
|
149 |
* ClassPrepareRequest if necessary and keep
|
|
150 |
* trying to resolve.
|
|
151 |
*/
|
|
152 |
resolved = null;
|
|
153 |
if (prepareRequest == null) {
|
|
154 |
prepareRequest = refSpec.createPrepareRequest();
|
|
155 |
prepareRequest.enable();
|
|
156 |
}
|
|
157 |
}
|
|
158 |
}
|
|
159 |
} catch (VMNotConnectedException e) {
|
|
160 |
// Do nothing. Another resolve will be attempted when the
|
|
161 |
// VM is started.
|
|
162 |
}
|
|
163 |
return resolved;
|
|
164 |
}
|
|
165 |
|
|
166 |
/**
|
|
167 |
* @return the eventRequest this spec has been resolved to,
|
|
168 |
* null if so far unresolved.
|
|
169 |
*/
|
|
170 |
EventRequest resolved() {
|
|
171 |
return resolved;
|
|
172 |
}
|
|
173 |
|
|
174 |
/**
|
|
175 |
* @return true if this spec has been resolved.
|
|
176 |
*/
|
|
177 |
boolean isResolved() {
|
|
178 |
return resolved != null;
|
|
179 |
}
|
|
180 |
|
|
181 |
protected boolean isJavaIdentifier(String s) {
|
|
182 |
if (s.length() == 0) {
|
|
183 |
return false;
|
|
184 |
}
|
|
185 |
|
|
186 |
int cp = s.codePointAt(0);
|
|
187 |
if (! Character.isJavaIdentifierStart(cp)) {
|
|
188 |
return false;
|
|
189 |
}
|
|
190 |
|
|
191 |
for (int i = Character.charCount(cp); i < s.length(); i += Character.charCount(cp)) {
|
|
192 |
cp = s.codePointAt(i);
|
|
193 |
if (! Character.isJavaIdentifierPart(cp)) {
|
|
194 |
return false;
|
|
195 |
}
|
|
196 |
}
|
|
197 |
|
|
198 |
return true;
|
|
199 |
}
|
|
200 |
|
|
201 |
String errorMessageFor(Exception e) {
|
|
202 |
if (e instanceof IllegalArgumentException) {
|
|
203 |
return (MessageOutput.format("Invalid command syntax"));
|
|
204 |
} else if (e instanceof RuntimeException) {
|
|
205 |
// A runtime exception that we were not expecting
|
|
206 |
throw (RuntimeException)e;
|
|
207 |
} else {
|
|
208 |
return (MessageOutput.format("Internal error; unable to set",
|
|
209 |
this.refSpec.toString()));
|
|
210 |
}
|
|
211 |
}
|
|
212 |
}
|