Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2009 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* IBM Corporation - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.jdt.internal.debug.eval.engine; |
12 |
|
13 |
import java.util.HashSet; |
14 |
import java.util.Set; |
15 |
import java.util.StringTokenizer; |
16 |
|
17 |
import org.eclipse.core.runtime.CoreException; |
18 |
import org.eclipse.core.runtime.IProgressMonitor; |
19 |
import org.eclipse.core.runtime.IStatus; |
20 |
import org.eclipse.core.runtime.NullProgressMonitor; |
21 |
import org.eclipse.core.runtime.Status; |
22 |
import org.eclipse.core.runtime.jobs.Job; |
23 |
import org.eclipse.debug.core.DebugEvent; |
24 |
import org.eclipse.debug.core.DebugException; |
25 |
import org.eclipse.debug.core.DebugPlugin; |
26 |
import org.eclipse.debug.core.IDebugEventFilter; |
27 |
import org.eclipse.debug.core.model.ITerminate; |
28 |
import org.eclipse.jdt.core.Flags; |
29 |
import org.eclipse.jdt.core.IJavaProject; |
30 |
import org.eclipse.jdt.core.IType; |
31 |
import org.eclipse.jdt.core.JavaModelException; |
32 |
import org.eclipse.jdt.core.Signature; |
33 |
import org.eclipse.jdt.core.eval.IEvaluationContext; |
34 |
import org.eclipse.jdt.debug.core.IEvaluationRunnable; |
35 |
import org.eclipse.jdt.debug.core.IJavaClassType; |
36 |
import org.eclipse.jdt.debug.core.IJavaDebugTarget; |
37 |
import org.eclipse.jdt.debug.core.IJavaInterfaceType; |
38 |
import org.eclipse.jdt.debug.core.IJavaObject; |
39 |
import org.eclipse.jdt.debug.core.IJavaReferenceType; |
40 |
import org.eclipse.jdt.debug.core.IJavaStackFrame; |
41 |
import org.eclipse.jdt.debug.core.IJavaThread; |
42 |
import org.eclipse.jdt.debug.core.IJavaValue; |
43 |
import org.eclipse.jdt.debug.core.IJavaVariable; |
44 |
import org.eclipse.jdt.debug.core.JDIDebugModel; |
45 |
import org.eclipse.jdt.debug.eval.IAstEvaluationEngine; |
46 |
import org.eclipse.jdt.debug.eval.ICompiledExpression; |
47 |
import org.eclipse.jdt.debug.eval.IEvaluationListener; |
48 |
import org.eclipse.jdt.debug.eval.IEvaluationResult; |
49 |
import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin; |
50 |
import org.eclipse.jdt.internal.debug.core.JavaDebugUtils; |
51 |
import org.eclipse.jdt.internal.debug.core.model.JDIDebugTarget; |
52 |
import org.eclipse.jdt.internal.debug.core.model.JDIThread; |
53 |
import org.eclipse.jdt.internal.debug.core.model.JDIValue; |
54 |
import org.eclipse.jdt.internal.debug.eval.EvaluationResult; |
55 |
import org.eclipse.jdt.internal.debug.eval.ast.engine.EvaluationEngineMessages; |
56 |
import org.eclipse.jdt.internal.debug.eval.ast.engine.IRuntimeContext; |
57 |
import org.eclipse.jdt.internal.debug.eval.ast.engine.JavaObjectRuntimeContext; |
58 |
import org.eclipse.jdt.internal.debug.eval.ast.engine.RuntimeContext; |
59 |
import org.eclipse.jdt.internal.debug.eval.ast.engine.TypeRuntimeContext; |
60 |
|
61 |
import com.sun.jdi.InvocationException; |
62 |
import com.sun.jdi.ObjectReference; |
63 |
|
64 |
/** |
65 |
* |
66 |
*/ |
67 |
public class RemoteEvaluationEngine implements IAstEvaluationEngine { |
68 |
|
69 |
/** |
70 |
* Associated Java project |
71 |
*/ |
72 |
private IJavaProject fProject; |
73 |
|
74 |
/** |
75 |
* Associated debug target |
76 |
*/ |
77 |
private IJavaDebugTarget fTarget; |
78 |
|
79 |
/** |
80 |
* Runnable serialized on a thread to perform submit an evaluation |
81 |
*/ |
82 |
class EvalRunnable implements Runnable { |
83 |
|
84 |
private CompiledExpression fExpression; |
85 |
|
86 |
private int fEvaluationDetail; |
87 |
|
88 |
private boolean fHitBreakpoints; |
89 |
|
90 |
private IRuntimeContext fContext; |
91 |
|
92 |
private IEvaluationListener fListener; |
93 |
|
94 |
public EvalRunnable(CompiledExpression expression, IRuntimeContext context, IEvaluationListener listener, int evaluationDetail, boolean hitBreakpoints) { |
95 |
fExpression= expression; |
96 |
fContext= context; |
97 |
fListener= listener; |
98 |
fEvaluationDetail= evaluationDetail; |
99 |
fHitBreakpoints= hitBreakpoints; |
100 |
} |
101 |
|
102 |
public void run() { |
103 |
EvaluationResult result = new EvaluationResult(RemoteEvaluationEngine.this, fExpression.getSnippet(), fContext.getThread()); |
104 |
if (fExpression.hasErrors()) { |
105 |
String[] errors = fExpression.getErrorMessages(); |
106 |
for (int i = 0, numErrors = errors.length; i < numErrors; i++) { |
107 |
result.addError(errors[i]); |
108 |
} |
109 |
evaluationFinished(result); |
110 |
return; |
111 |
} |
112 |
|
113 |
class EvaluationRunnable implements IEvaluationRunnable, ITerminate { |
114 |
|
115 |
CoreException fException; |
116 |
boolean fTerminated = false; |
117 |
|
118 |
public void run(IJavaThread jt, IProgressMonitor pm) { |
119 |
EventFilter filter = new EventFilter(); |
120 |
try { |
121 |
DebugPlugin.getDefault().addDebugEventFilter(filter); |
122 |
fExpression.execute(fContext); |
123 |
} catch (CoreException exception) { |
124 |
fException = exception; |
125 |
if (fEvaluationDetail == DebugEvent.EVALUATION && exception.getStatus().getException() instanceof InvocationException) { |
126 |
// print the stack trace for the exception if an *explicit* evaluation |
127 |
InvocationException invocationException = (InvocationException)exception.getStatus().getException(); |
128 |
ObjectReference exObject = invocationException.exception(); |
129 |
IJavaObject modelObject = (IJavaObject)JDIValue.createValue((JDIDebugTarget)getDebugTarget(), exObject); |
130 |
try { |
131 |
modelObject.sendMessage("printStackTrace", "()V", null, jt, false); //$NON-NLS-1$ //$NON-NLS-2$ |
132 |
} catch (DebugException e) { |
133 |
// unable to print stack trace |
134 |
} |
135 |
} |
136 |
} finally { |
137 |
DebugPlugin.getDefault().removeDebugEventFilter(filter); |
138 |
fTerminated = true; |
139 |
} |
140 |
} |
141 |
public void terminate() { |
142 |
fTerminated = true; |
143 |
// TODO: how to support terminate? |
144 |
} |
145 |
public boolean canTerminate() { |
146 |
return !fTerminated; |
147 |
} |
148 |
public boolean isTerminated() { |
149 |
return fTerminated; |
150 |
} |
151 |
|
152 |
public CoreException getException() { |
153 |
return fException; |
154 |
} |
155 |
} |
156 |
|
157 |
EvaluationRunnable er = new EvaluationRunnable(); |
158 |
CoreException exception = null; |
159 |
try { |
160 |
fContext.getThread().runEvaluation(er, null, fEvaluationDetail, fHitBreakpoints); |
161 |
} catch (DebugException e) { |
162 |
exception = e; |
163 |
} |
164 |
|
165 |
IJavaValue value = fExpression.getResult(); |
166 |
|
167 |
if (exception == null) { |
168 |
exception = er.getException(); |
169 |
} |
170 |
|
171 |
result.setTerminated(er.fTerminated); |
172 |
if (exception != null) { |
173 |
if (exception instanceof DebugException) { |
174 |
result.setException((DebugException)exception); |
175 |
} else { |
176 |
result.setException(new DebugException(exception.getStatus())); |
177 |
} |
178 |
} else { |
179 |
if (value != null) { |
180 |
result.setValue(value); |
181 |
} else { |
182 |
result.addError(EvaluationEngineMessages.ASTEvaluationEngine_An_unknown_error_occurred_during_evaluation); |
183 |
} |
184 |
} |
185 |
|
186 |
|
187 |
evaluationFinished(result); |
188 |
} |
189 |
private void evaluationFinished(IEvaluationResult result) { |
190 |
// only notify if plug-in not yet shutdown - bug# 8693 |
191 |
if(JDIDebugPlugin.getDefault() != null) { |
192 |
fListener.evaluationComplete(result); |
193 |
} |
194 |
} |
195 |
|
196 |
} |
197 |
|
198 |
/** |
199 |
* Filters variable change events during an evaluation to avoid refreshing the variables |
200 |
* view until done. |
201 |
*/ |
202 |
class EventFilter implements IDebugEventFilter { |
203 |
|
204 |
/* (non-Javadoc) |
205 |
* @see org.eclipse.debug.core.IDebugEventFilter#filterDebugEvents(org.eclipse.debug.core.DebugEvent[]) |
206 |
*/ |
207 |
public DebugEvent[] filterDebugEvents(DebugEvent[] events) { |
208 |
if (events.length == 1) { |
209 |
DebugEvent event = events[0]; |
210 |
if (event.getSource() instanceof IJavaVariable && event.getKind() == DebugEvent.CHANGE) { |
211 |
if (((IJavaVariable)event.getSource()).getDebugTarget().equals(getDebugTarget())) { |
212 |
return null; |
213 |
} |
214 |
} |
215 |
} |
216 |
return events; |
217 |
} |
218 |
|
219 |
} |
220 |
|
221 |
/** |
222 |
* Constructs a new engine on the given target and project. |
223 |
* |
224 |
* @param target debug target |
225 |
* @param project associated project for compilation purposes |
226 |
*/ |
227 |
public RemoteEvaluationEngine(IJavaDebugTarget target, IJavaProject project) { |
228 |
fProject = project; |
229 |
fTarget = target; |
230 |
} |
231 |
|
232 |
/* (non-Javadoc) |
233 |
* @see org.eclipse.jdt.debug.eval.IAstEvaluationEngine#evaluateExpression(org.eclipse.jdt.debug.eval.ICompiledExpression, org.eclipse.jdt.debug.core.IJavaStackFrame, org.eclipse.jdt.debug.eval.IEvaluationListener, int, boolean) |
234 |
*/ |
235 |
public void evaluateExpression(ICompiledExpression expression, IJavaStackFrame frame, final IEvaluationListener listener, int evaluationDetail, boolean hitBreakpoints) throws DebugException { |
236 |
IRuntimeContext debugContext = new RuntimeContext(getJavaProject(), frame); |
237 |
evaluateExpression(expression, debugContext, listener, evaluationDetail, hitBreakpoints); |
238 |
} |
239 |
|
240 |
/** |
241 |
* Evaluates the given expression in the specified thread and the context. |
242 |
* |
243 |
* @param expression expression to evaluate |
244 |
* @param context context to evaluate in |
245 |
* @param listener listener to report results to |
246 |
* @param evaluationDetail detail event to fire with evaluation resume/suspend |
247 |
* @param hitBreakpoints whether to honor breakpoints during the evaluation |
248 |
*/ |
249 |
private void evaluateExpression(ICompiledExpression expression, IRuntimeContext context, IEvaluationListener listener, int evaluationDetail, boolean hitBreakpoints) throws DebugException { |
250 |
if (expression instanceof CompiledExpression) { |
251 |
IJavaThread thread = context.getThread(); |
252 |
// don't queue explicit evaluation if the thread is all ready performing an evaluation. |
253 |
if (thread.isSuspended() && ((JDIThread)thread).isInvokingMethod() || thread.isPerformingEvaluation() && evaluationDetail == DebugEvent.EVALUATION) { |
254 |
EvaluationResult result= new EvaluationResult(this, expression.getSnippet(), thread); |
255 |
result.addError(EvaluationEngineMessages.ASTEvaluationEngine_Cannot_perform_nested_evaluations); |
256 |
listener.evaluationComplete(result); |
257 |
return; |
258 |
} |
259 |
thread.queueRunnable(new EvalRunnable((CompiledExpression)expression, context, listener, evaluationDetail, hitBreakpoints)); |
260 |
} else { |
261 |
throw new DebugException(new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), IStatus.OK, EvaluationEngineMessages.ASTEvaluationEngine_AST_evaluation_engine_cannot_evaluate_expression, null)); |
262 |
} |
263 |
} |
264 |
|
265 |
/* (non-Javadoc) |
266 |
* @see org.eclipse.jdt.debug.eval.IAstEvaluationEngine#evaluateExpression(org.eclipse.jdt.debug.eval.ICompiledExpression, org.eclipse.jdt.debug.core.IJavaObject, org.eclipse.jdt.debug.core.IJavaThread, org.eclipse.jdt.debug.eval.IEvaluationListener, int, boolean) |
267 |
*/ |
268 |
public void evaluateExpression(ICompiledExpression expression, |
269 |
IJavaObject object, IJavaThread thread, |
270 |
IEvaluationListener listener, int evaluationDetail, |
271 |
boolean hitBreakpoints) throws DebugException { |
272 |
IRuntimeContext debugContext = new JavaObjectRuntimeContext(object, getJavaProject(), thread); |
273 |
evaluateExpression(expression, debugContext, listener, evaluationDetail, hitBreakpoints); |
274 |
} |
275 |
|
276 |
/* (non-Javadoc) |
277 |
* @see org.eclipse.jdt.debug.eval.IAstEvaluationEngine#getCompiledExpression(java.lang.String, org.eclipse.jdt.debug.core.IJavaStackFrame) |
278 |
*/ |
279 |
public ICompiledExpression getCompiledExpression(String expression, IJavaStackFrame frame) throws DebugException { |
280 |
return getCompiledExpression(expression, new RuntimeContext(getJavaProject(), frame)); |
281 |
} |
282 |
|
283 |
/** |
284 |
* Returns a compiled expression for the given snippet in the given debugger context. |
285 |
* |
286 |
* @param snippet expression to compile |
287 |
* @param debugContext current execution context to compile in |
288 |
* @return compiled expression |
289 |
* @throws DebugException on failure |
290 |
*/ |
291 |
private ICompiledExpression getCompiledExpression(final String snippet, final IRuntimeContext debugContext) throws DebugException { |
292 |
IJavaProject javaProject = getJavaProject(); |
293 |
final IEvaluationContext evalContext = javaProject.newEvaluationContext(); |
294 |
|
295 |
try { |
296 |
IJavaVariable[] localsVar = debugContext.getLocals(); |
297 |
int numLocalsVar= localsVar.length; |
298 |
Set names = new HashSet(); |
299 |
// ****** |
300 |
// to hide problems with local variable declare as instance of Local Types |
301 |
// and to remove locals with duplicate names |
302 |
IJavaVariable[] locals= new IJavaVariable[numLocalsVar]; |
303 |
int numLocals= 0; |
304 |
for (int i = 0; i < numLocalsVar; i++) { |
305 |
if (!isLocalType(localsVar[i].getSignature()) && !names.contains(localsVar[i].getName())) { |
306 |
locals[numLocals++]= localsVar[i]; |
307 |
names.add(localsVar[i].getName()); |
308 |
} |
309 |
} |
310 |
// to solve and remove |
311 |
// ****** |
312 |
final String[] localTypesNames= new String[numLocals]; |
313 |
final String[] localVariables= new String[numLocals]; |
314 |
final int[] localModifiers = new int[numLocals]; |
315 |
for (int i = 0; i < numLocals; i++) { |
316 |
localVariables[i] = locals[i].getName(); |
317 |
localTypesNames[i] = Signature.toString(locals[i].getGenericSignature()).replace('/', '.'); |
318 |
localModifiers[i] = Flags.AccDefault; |
319 |
if (locals[i].isFinal()) { |
320 |
localModifiers[i] = Flags.AccFinal; |
321 |
} |
322 |
} |
323 |
// Compile in context of declaring type to get proper visibility of locals and members. |
324 |
// Compiling in context of receiving type potentially provides access to more members, |
325 |
// but does not allow access to privates members in declaring type |
326 |
IJavaReferenceType receivingType = debugContext.getReceivingType(); |
327 |
IType iType = JavaDebugUtils.resolveType(receivingType); |
328 |
while (iType.isAnonymous() && receivingType instanceof IJavaClassType) { |
329 |
// cannot compile in context of anonymous classes, so compile in context of their superclass |
330 |
receivingType = ((IJavaClassType)receivingType).getSuperclass(); |
331 |
iType = JavaDebugUtils.resolveType(receivingType); |
332 |
} |
333 |
|
334 |
final IType declaringType = iType; |
335 |
final CompiledExpression compiledExpression = new CompiledExpression(snippet, localVariables); |
336 |
Job job = new Job("Debugger Evaluation") { |
337 |
protected IStatus run(IProgressMonitor monitor) { |
338 |
try { |
339 |
evalContext.evaluateCodeSnippet( |
340 |
snippet, |
341 |
localTypesNames, |
342 |
localVariables, |
343 |
localModifiers, |
344 |
declaringType, |
345 |
debugContext.isStatic(), |
346 |
debugContext.isConstructor(), |
347 |
compiledExpression, |
348 |
new NullProgressMonitor()); |
349 |
} catch (JavaModelException e) { |
350 |
return e.getStatus(); |
351 |
} catch (CoreException e) { |
352 |
return e.getStatus(); |
353 |
} |
354 |
return Status.OK_STATUS; |
355 |
} |
356 |
}; |
357 |
job.setSystem(true); |
358 |
job.schedule(); |
359 |
try { |
360 |
job.join(); |
361 |
} catch (InterruptedException e) { |
362 |
throw new DebugException(new Status(IStatus.ERROR, JDIDebugModel.getPluginIdentifier(), "Evaluation interrupted", e)); |
363 |
} |
364 |
|
365 |
return compiledExpression; |
366 |
} catch (CoreException e) { |
367 |
throw new DebugException(e.getStatus()); |
368 |
} |
369 |
} |
370 |
|
371 |
//TODO: local types might be OK with this new engine |
372 |
|
373 |
// ****** |
374 |
// to hide problems with local variable declare as instance of Local Types |
375 |
private boolean isLocalType(String typeName) { |
376 |
StringTokenizer strTok= new StringTokenizer(typeName,"$"); //$NON-NLS-1$ |
377 |
strTok.nextToken(); |
378 |
while (strTok.hasMoreTokens()) { |
379 |
char char0= strTok.nextToken().charAt(0); |
380 |
if ('0' <= char0 && char0 <= '9') { |
381 |
return true; |
382 |
} |
383 |
} |
384 |
return false; |
385 |
} |
386 |
// ****** |
387 |
|
388 |
/* (non-Javadoc) |
389 |
* @see org.eclipse.jdt.debug.eval.IAstEvaluationEngine#getCompiledExpression(java.lang.String, org.eclipse.jdt.debug.core.IJavaObject) |
390 |
*/ |
391 |
public ICompiledExpression getCompiledExpression(String expression, IJavaObject object) throws DebugException { |
392 |
return getCompiledExpression(expression, new JavaObjectRuntimeContext(object, getJavaProject(), null)); |
393 |
} |
394 |
|
395 |
/* (non-Javadoc) |
396 |
* @see org.eclipse.jdt.debug.eval.IAstEvaluationEngine#getCompiledExpression(java.lang.String, org.eclipse.jdt.debug.core.IJavaReferenceType) |
397 |
*/ |
398 |
public ICompiledExpression getCompiledExpression(String expression, IJavaReferenceType type) throws DebugException { |
399 |
return getCompiledExpression(expression, new TypeRuntimeContext(getJavaProject(), null, type)); |
400 |
} |
401 |
|
402 |
/* (non-Javadoc) |
403 |
* @see org.eclipse.jdt.debug.eval.IEvaluationEngine#dispose() |
404 |
*/ |
405 |
public void dispose() { |
406 |
// TODO Auto-generated method stub |
407 |
|
408 |
} |
409 |
|
410 |
/** |
411 |
* Checks if the stack frame is declared in an interface an aborts |
412 |
* if so. |
413 |
* |
414 |
* @param frame stack frame |
415 |
* @throws DebugException if declaring type is an interface |
416 |
*/ |
417 |
private void checkInterface(IJavaStackFrame frame) throws DebugException { |
418 |
if (frame.getReferenceType() instanceof IJavaInterfaceType) { |
419 |
IStatus status = new Status(IStatus.ERROR, JDIDebugModel.getPluginIdentifier(), DebugException.REQUEST_FAILED, |
420 |
EvaluationEngineMessages.ASTEvaluationEngine_0, null); |
421 |
throw new DebugException(status); |
422 |
} |
423 |
} |
424 |
|
425 |
/* (non-Javadoc) |
426 |
* @see org.eclipse.jdt.debug.eval.IEvaluationEngine#evaluate(java.lang.String, org.eclipse.jdt.debug.core.IJavaStackFrame, org.eclipse.jdt.debug.eval.IEvaluationListener, int, boolean) |
427 |
*/ |
428 |
public void evaluate(String snippet, IJavaStackFrame frame, IEvaluationListener listener, int evaluationDetail, boolean hitBreakpoints) throws DebugException { |
429 |
checkInterface(frame); |
430 |
IRuntimeContext context = new RuntimeContext(getJavaProject(), frame); |
431 |
ICompiledExpression expression= getCompiledExpression(snippet, context); |
432 |
evaluateExpression(expression, context, listener, evaluationDetail, hitBreakpoints); |
433 |
} |
434 |
|
435 |
/* (non-Javadoc) |
436 |
* @see org.eclipse.jdt.debug.eval.IEvaluationEngine#evaluate(java.lang.String, org.eclipse.jdt.debug.core.IJavaObject, org.eclipse.jdt.debug.core.IJavaThread, org.eclipse.jdt.debug.eval.IEvaluationListener, int, boolean) |
437 |
*/ |
438 |
public void evaluate(String snippet, IJavaObject thisContext, |
439 |
IJavaThread thread, IEvaluationListener listener, |
440 |
int evaluationDetail, boolean hitBreakpoints) throws DebugException { |
441 |
IRuntimeContext context = new JavaObjectRuntimeContext(thisContext, getJavaProject(), thread); |
442 |
ICompiledExpression expression= getCompiledExpression(snippet, context); |
443 |
evaluateExpression(expression, context, listener, evaluationDetail, hitBreakpoints); |
444 |
} |
445 |
|
446 |
/* (non-Javadoc) |
447 |
* @see org.eclipse.jdt.debug.eval.IEvaluationEngine#getDebugTarget() |
448 |
*/ |
449 |
public IJavaDebugTarget getDebugTarget() { |
450 |
return fTarget; |
451 |
} |
452 |
|
453 |
/* (non-Javadoc) |
454 |
* @see org.eclipse.jdt.debug.eval.IEvaluationEngine#getJavaProject() |
455 |
*/ |
456 |
public IJavaProject getJavaProject() { |
457 |
return fProject; |
458 |
} |
459 |
|
460 |
} |