1 12 package org.eclipse.jdt.internal.debug.core.model; 13 14 15 import java.util.ArrayList ; 16 import java.util.Collections ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Vector ; 20 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.IProgressMonitor; 23 import org.eclipse.core.runtime.IStatus; 24 import org.eclipse.core.runtime.MultiStatus; 25 import org.eclipse.core.runtime.Status; 26 import org.eclipse.core.runtime.jobs.ISchedulingRule; 27 import org.eclipse.core.runtime.jobs.Job; 28 import org.eclipse.debug.core.DebugEvent; 29 import org.eclipse.debug.core.DebugException; 30 import org.eclipse.debug.core.DebugPlugin; 31 import org.eclipse.debug.core.IStatusHandler; 32 import org.eclipse.debug.core.model.IBreakpoint; 33 import org.eclipse.debug.core.model.IStackFrame; 34 import org.eclipse.debug.core.model.ITerminate; 35 import org.eclipse.jdt.core.Signature; 36 import org.eclipse.jdt.debug.core.IEvaluationRunnable; 37 import org.eclipse.jdt.debug.core.IJavaBreakpoint; 38 import org.eclipse.jdt.debug.core.IJavaObject; 39 import org.eclipse.jdt.debug.core.IJavaStackFrame; 40 import org.eclipse.jdt.debug.core.IJavaThread; 41 import org.eclipse.jdt.debug.core.IJavaThreadGroup; 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.internal.debug.core.IJDIEventListener; 46 import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin; 47 import org.eclipse.jdt.internal.debug.core.breakpoints.JavaBreakpoint; 48 49 import com.ibm.icu.text.MessageFormat; 50 import com.sun.jdi.BooleanValue; 51 import com.sun.jdi.ClassNotLoadedException; 52 import com.sun.jdi.ClassType; 53 import com.sun.jdi.Field; 54 import com.sun.jdi.IncompatibleThreadStateException; 55 import com.sun.jdi.IntegerValue; 56 import com.sun.jdi.InvalidStackFrameException; 57 import com.sun.jdi.InvalidTypeException; 58 import com.sun.jdi.InvocationException; 59 import com.sun.jdi.Location; 60 import com.sun.jdi.Method; 61 import com.sun.jdi.ObjectCollectedException; 62 import com.sun.jdi.ObjectReference; 63 import com.sun.jdi.ReferenceType; 64 import com.sun.jdi.StackFrame; 65 import com.sun.jdi.ThreadGroupReference; 66 import com.sun.jdi.ThreadReference; 67 import com.sun.jdi.VMDisconnectedException; 68 import com.sun.jdi.Value; 69 import com.sun.jdi.event.Event; 70 import com.sun.jdi.event.StepEvent; 71 import com.sun.jdi.request.EventRequest; 72 import com.sun.jdi.request.EventRequestManager; 73 import com.sun.jdi.request.StepRequest; 74 75 79 public class JDIThread extends JDIDebugElement implements IJavaThread { 80 81 84 private static final String JAVA_STRATUM_CONSTANT = "Java"; 86 89 private static final String MAIN_THREAD_GROUP = "main"; 93 public static final int SUSPEND_TIMEOUT= 161; 94 97 private ThreadReference fThread; 98 101 private String fPreviousName; 102 105 private List fStackFrames; 106 109 private ThreadGroupReference fThreadGroup; 110 113 private String fThreadGroupName; 114 119 private boolean fRefreshChildren = true; 120 124 private StepHandler fStepHandler= null; 125 128 private boolean fRunning; 129 132 private boolean fTerminated; 133 136 private boolean fSuspendedQuiet; 137 140 private boolean fIsSystemThread; 141 142 146 private boolean fIsDaemon = false; 147 148 153 private List fCurrentBreakpoints = new ArrayList (2); 154 159 private boolean fIsPerformingEvaluation= false; 160 private IEvaluationRunnable fEvaluationRunnable; 161 162 166 private boolean fEvaluationInterrupted = false; 167 168 172 private boolean fIsInvokingMethod = false; 173 178 private boolean fHonorBreakpoints= true; 179 185 private int fOriginalStepKind; 186 189 private Location fOriginalStepLocation; 190 197 private int fOriginalStepStackDepth; 198 199 202 private boolean fIsSuspending= false; 203 206 private boolean fIsEvaluatingConditionalBreakpoint= false; 207 208 private ThreadJob fAsyncJob; 209 210 private ThreadJob fRunningAsyncJob; 211 212 221 public JDIThread(JDIDebugTarget target, ThreadReference thread) throws ObjectCollectedException { 222 super(target); 223 setUnderlyingThread(thread); 224 initialize(); 225 } 226 227 237 protected void initialize() throws ObjectCollectedException { 238 fStackFrames= new ArrayList (); 239 try { 241 determineIfSystemThread(); 242 } catch (DebugException e) { 243 Throwable underlyingException= e.getStatus().getException(); 244 if (underlyingException instanceof VMDisconnectedException) { 245 disconnected(); 249 return; 250 } 251 if (underlyingException instanceof ObjectCollectedException) { 252 throw (ObjectCollectedException)underlyingException; 253 } 254 logError(e); 255 } 256 257 try { 258 determineIfDaemonThread(); 259 } catch (DebugException e) { 260 Throwable underlyingException= e.getStatus().getException(); 261 if (underlyingException instanceof VMDisconnectedException) { 262 disconnected(); 266 return; 267 } 268 logError(e); 269 } 270 271 try { 272 ThreadGroupReference group = getUnderlyingThreadGroup(); 273 if (group != null) { 275 getJavaDebugTarget().addThreadGroup(group); 276 } 277 } catch (DebugException e1) { 278 } 279 280 setTerminated(false); 282 setRunning(false); 283 try { 284 if (fThread.status() == ThreadReference.THREAD_STATUS_UNKNOWN) { 286 setRunning(true); 287 return; 288 } 289 } catch (VMDisconnectedException e) { 290 disconnected(); 291 return; 292 } catch (ObjectCollectedException e){ 293 throw e; 294 } catch (RuntimeException e) { 295 logError(e); 296 } 297 298 try { 299 boolean suspended = fThread.isSuspended(); 300 if (suspended) { 301 suspended = getBreakpoints().length > 0; 307 } 308 setRunning(!suspended); 309 } catch (VMDisconnectedException e) { 310 disconnected(); 311 return; 312 } catch (ObjectCollectedException e){ 313 throw e; 314 } catch (RuntimeException e) { 315 logError(e); 316 } 317 } 318 319 323 protected void addCurrentBreakpoint(IBreakpoint bp) { 324 fCurrentBreakpoints.add(bp); 325 } 326 327 332 protected void removeCurrentBreakpoint(IBreakpoint bp) { 333 fCurrentBreakpoints.remove(bp); 334 } 335 336 339 public synchronized IBreakpoint[] getBreakpoints() { 340 return (IBreakpoint[])fCurrentBreakpoints.toArray(new IBreakpoint[fCurrentBreakpoints.size()]); 341 } 342 343 346 public boolean canResume() { 347 return isSuspended() && !isSuspendedQuiet() && (!isPerformingEvaluation() || isInvokingMethod()); 348 } 349 350 353 public boolean canSuspend() { 354 return !isSuspended() || isSuspendedQuiet() || (isPerformingEvaluation() && !isInvokingMethod()); 355 } 356 357 360 public boolean canTerminate() { 361 return getDebugTarget().canTerminate(); 362 } 363 364 367 public boolean canStepInto() { 368 return canStep(); 369 } 370 371 374 public boolean canStepOver() { 375 return canStep(); 376 } 377 378 381 public boolean canStepReturn() { 382 return canStep(); 383 } 384 385 392 protected boolean canStep() { 393 try { 394 return isSuspended() 395 && !isSuspendedQuiet() 396 && (!isPerformingEvaluation() || isInvokingMethod()) 397 && !isStepping() 398 && getTopStackFrame() != null 399 && !getJavaDebugTarget().isPerformingHotCodeReplace(); 400 } catch (DebugException e) { 401 return false; 402 } 403 } 404 405 415 protected void determineIfSystemThread() throws DebugException { 416 fIsSystemThread= false; 417 ThreadGroupReference tgr= getUnderlyingThreadGroup(); 418 fIsSystemThread = tgr != null; 419 while (tgr != null) { 420 String tgn= null; 421 try { 422 tgn= tgr.name(); 423 tgr= tgr.parent(); 424 } catch (UnsupportedOperationException e) { 425 fIsSystemThread = false; 426 break; 427 } catch (RuntimeException e) { 428 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIThread_exception_determining_if_system_thread, new String [] {e.toString()}), e); 429 return; 432 } 433 if (tgn != null && tgn.equals(MAIN_THREAD_GROUP)) { 434 fIsSystemThread= false; 435 break; 436 } 437 } 438 } 439 440 445 protected void determineIfDaemonThread() throws DebugException { 446 fIsDaemon = false; 447 try { 448 ReferenceType referenceType = getUnderlyingThread().referenceType(); 449 Field field = referenceType.fieldByName("daemon"); if (field == null) { 451 field = referenceType.fieldByName("isDaemon"); } 453 if (field != null) { 454 if (field.signature().equals(Signature.SIG_BOOLEAN)) { 455 Value value = getUnderlyingThread().getValue(field); 456 if (value instanceof BooleanValue) { 457 fIsDaemon = ((BooleanValue)value).booleanValue(); 458 } 459 } 460 } 461 } 462 catch(ObjectCollectedException oce) {} 463 catch (RuntimeException e) { 464 targetRequestFailed(JDIDebugModelMessages.JDIThread_47, e); 465 } 466 } 467 468 473 public synchronized IStackFrame[] getStackFrames() throws DebugException { 474 if (isSuspendedQuiet()) { 475 return new IStackFrame[0]; 476 } 477 List list = computeStackFrames(); 478 return (IStackFrame[])list.toArray(new IStackFrame[list.size()]); 479 } 480 481 487 protected synchronized List computeStackFrames(boolean refreshChildren) throws DebugException { 488 if (isSuspended()) { 489 if (isTerminated()) { 490 fStackFrames.clear(); 491 } else if (refreshChildren) { 492 List frames = getUnderlyingFrames(); 493 int oldSize = fStackFrames.size(); 494 int newSize = frames.size(); 495 int discard = oldSize - newSize; for (int i = 0; i < discard; i++) { 497 JDIStackFrame invalid = (JDIStackFrame) fStackFrames.remove(0); 498 invalid.bind(null, -1); 499 } 500 int newFrames = newSize - oldSize; int depth = oldSize; 502 for (int i = newFrames - 1; i >= 0; i--) { 503 fStackFrames.add(0, new JDIStackFrame(this, (StackFrame) frames.get(i), depth)); 504 depth++; 505 } 506 int numToRebind = Math.min(newSize, oldSize); int offset = newSize - 1; 508 for (depth = 0; depth < numToRebind; depth++) { 509 JDIStackFrame oldFrame = (JDIStackFrame) fStackFrames.get(offset); 510 StackFrame frame = (StackFrame) frames.get(offset); 511 JDIStackFrame newFrame = oldFrame.bind(frame, depth); 512 if (newFrame != oldFrame) { 513 fStackFrames.set(offset, newFrame); 514 } 515 offset--; 516 } 517 518 519 } 520 fRefreshChildren = false; 521 } else { 522 return Collections.EMPTY_LIST; 523 } 524 return fStackFrames; 525 } 526 527 555 public synchronized List computeStackFrames() throws DebugException { 556 return computeStackFrames(fRefreshChildren); 557 } 558 559 568 public List computeNewStackFrames() throws DebugException { 569 return computeStackFrames(true); 570 } 571 572 private List getUnderlyingFrames() throws DebugException { 573 if (!isSuspended()) { 574 requestFailed(JDIDebugModelMessages.JDIThread_Unable_to_retrieve_stack_frame___thread_not_suspended__1, null, IJavaThread.ERR_THREAD_NOT_SUSPENDED); 578 } 579 try { 580 return fThread.frames(); 581 } catch (IncompatibleThreadStateException e) { 582 requestFailed(JDIDebugModelMessages.JDIThread_Unable_to_retrieve_stack_frame___thread_not_suspended__1, e, IJavaThread.ERR_THREAD_NOT_SUSPENDED); 583 } catch (RuntimeException e) { 584 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIThread_exception_retrieving_stack_frames_2, new String [] {e.toString()}), e); 585 } catch (InternalError e) { 586 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIThread_exception_retrieving_stack_frames_2, new String [] {e.toString()}), e); 587 } 588 return null; 591 } 592 593 606 protected int getUnderlyingFrameCount() throws DebugException { 607 try { 608 return fThread.frameCount(); 609 } catch (RuntimeException e) { 610 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIThread_exception_retrieving_frame_count, new String [] {e.toString()}), e); 611 } catch (IncompatibleThreadStateException e) { 612 requestFailed(MessageFormat.format(JDIDebugModelMessages.JDIThread_exception_retrieving_frame_count, new String [] {e.toString()}), e, IJavaThread.ERR_THREAD_NOT_SUSPENDED); 613 } 614 return -1; 617 } 618 619 622 public void runEvaluation(IEvaluationRunnable evaluation, IProgressMonitor monitor, int evaluationDetail, boolean hitBreakpoints) throws DebugException { 623 if (isPerformingEvaluation()) { 624 requestFailed(JDIDebugModelMessages.JDIThread_Cannot_perform_nested_evaluations, null, IJavaThread.ERR_NESTED_METHOD_INVOCATION); } 626 627 if (!canRunEvaluation()) { 628 requestFailed(JDIDebugModelMessages.JDIThread_Evaluation_failed___thread_not_suspended, null, IJavaThread.ERR_THREAD_NOT_SUSPENDED); 629 } 630 631 fIsPerformingEvaluation = true; 632 fEvaluationRunnable= evaluation; 633 fHonorBreakpoints= hitBreakpoints; 634 fireResumeEvent(evaluationDetail); 635 IBreakpoint[] breakpoints = getBreakpoints(); 637 ISchedulingRule rule = null; 638 if (evaluationDetail == DebugEvent.EVALUATION_IMPLICIT) { 639 rule = getThreadRule(); 640 } 641 try { 642 if (rule != null) { 643 Job.getJobManager().beginRule(rule, monitor); 644 } 645 if (monitor == null || !monitor.isCanceled()) { 646 evaluation.run(this, monitor); 647 } 648 } catch (DebugException e) { 649 throw e; 650 } finally { 651 if (rule != null) { 652 Job.getJobManager().endRule(rule); 653 } 654 fIsPerformingEvaluation = false; 655 fEvaluationRunnable= null; 656 fHonorBreakpoints= true; 657 if (getBreakpoints().length == 0 && breakpoints.length > 0) { 658 for (int i = 0; i < breakpoints.length; i++) { 659 addCurrentBreakpoint(breakpoints[i]); 660 } 661 } 662 fireSuspendEvent(evaluationDetail); 663 if (fEvaluationInterrupted && (fAsyncJob == null || fAsyncJob.isEmpty()) && (fRunningAsyncJob == null || fRunningAsyncJob.isEmpty())) { 664 fEvaluationInterrupted = false; 670 fireChangeEvent(DebugEvent.CONTENT); 671 } 672 } 673 } 674 675 682 protected boolean canRunEvaluation() { 683 try { 685 return isSuspendedQuiet() || (isSuspended() 686 && !(isPerformingEvaluation() || isInvokingMethod()) 687 && !isStepping() 688 && getTopStackFrame() != null 689 && !getJavaDebugTarget().isPerformingHotCodeReplace()); 690 } catch (DebugException e) { 691 return false; 692 } 693 } 694 695 698 public void queueRunnable(Runnable evaluation) { 699 if (fAsyncJob == null) { 700 fAsyncJob= new ThreadJob(this); 701 } 702 fAsyncJob.addRunnable(evaluation); 703 } 704 705 708 public void terminateEvaluation() throws DebugException { 709 if (canTerminateEvaluation()) { 710 ((ITerminate) fEvaluationRunnable).terminate(); 711 } 712 } 713 714 717 public boolean canTerminateEvaluation() { 718 return fEvaluationRunnable instanceof ITerminate; 719 } 720 721 771 protected Value invokeMethod(ClassType receiverClass, ObjectReference receiverObject, Method method, List args, boolean invokeNonvirtual) throws DebugException { 772 if (receiverClass != null && receiverObject != null) { 773 throw new IllegalArgumentException (JDIDebugModelMessages.JDIThread_can_only_specify_one_receiver_for_a_method_invocation); 774 } 775 Value result= null; 776 int timeout= getRequestTimeout(); 777 try { 778 synchronized (this) { 783 if (!isSuspended()) { 784 requestFailed(JDIDebugModelMessages.JDIThread_Evaluation_failed___thread_not_suspended, null, IJavaThread.ERR_THREAD_NOT_SUSPENDED); 785 } 786 if (isInvokingMethod()) { 787 requestFailed(JDIDebugModelMessages.JDIThread_Cannot_perform_nested_evaluations, null, IJavaThread.ERR_NESTED_METHOD_INVOCATION); 788 } 789 setRequestTimeout(Integer.MAX_VALUE); 791 setRunning(true); 792 setInvokingMethod(true); 793 } 794 preserveStackFrames(); 795 int flags= ClassType.INVOKE_SINGLE_THREADED; 796 if (invokeNonvirtual) { 797 flags |= ObjectReference.INVOKE_NONVIRTUAL; 799 } 800 if (receiverClass == null) { 801 result= receiverObject.invokeMethod(fThread, method, args, flags); 802 } else { 803 result= receiverClass.invokeMethod(fThread, method, args, flags); 804 } 805 } catch (InvalidTypeException e) { 806 invokeFailed(e, timeout); 807 } catch (ClassNotLoadedException e) { 808 invokeFailed(e, timeout); 809 } catch (IncompatibleThreadStateException e) { 810 invokeFailed(JDIDebugModelMessages.JDIThread_Thread_must_be_suspended_by_step_or_breakpoint_to_perform_method_invocation_1, IJavaThread.ERR_INCOMPATIBLE_THREAD_STATE, e, timeout); 811 } catch (InvocationException e) { 812 invokeFailed(e, timeout); 813 } catch (RuntimeException e) { 814 invokeFailed(e, timeout); 815 } 816 817 invokeComplete(timeout); 818 return result; 819 } 820 821 861 protected ObjectReference newInstance(ClassType receiverClass, Method constructor, List args) throws DebugException { 862 if (isInvokingMethod()) { 863 requestFailed(JDIDebugModelMessages.JDIThread_Cannot_perform_nested_evaluations_2, null); 864 } 865 ObjectReference result= null; 866 int timeout= getRequestTimeout(); 867 try { 868 setRequestTimeout(Integer.MAX_VALUE); 870 setRunning(true); 871 setInvokingMethod(true); 872 preserveStackFrames(); 873 result= receiverClass.newInstance(fThread, constructor, args, ClassType.INVOKE_SINGLE_THREADED); 874 } catch (InvalidTypeException e) { 875 invokeFailed(e, timeout); 876 } catch (ClassNotLoadedException e) { 877 invokeFailed(e, timeout); 878 } catch (IncompatibleThreadStateException e) { 879 invokeFailed(e, timeout); 880 } catch (InvocationException e) { 881 invokeFailed(e, timeout); 882 } catch (RuntimeException e) { 883 invokeFailed(e, timeout); 884 } 885 886 invokeComplete(timeout); 887 return result; 888 } 889 890 905 protected void invokeFailed(Throwable e, int restoreTimeout) throws DebugException { 906 invokeFailed(MessageFormat.format(JDIDebugModelMessages.JDIThread_exception_invoking_method, new String [] {e.toString()}), DebugException.TARGET_REQUEST_FAILED, e, restoreTimeout); 907 } 908 909 926 protected void invokeFailed(String message, int code, Throwable e, int restoreTimeout) throws DebugException { 927 invokeComplete(restoreTimeout); 928 requestFailed(message, e, code); 929 } 930 931 944 protected synchronized void invokeComplete(int restoreTimeout) { 945 if (!fIsEvaluatingConditionalBreakpoint) { 946 abortStep(); 947 } 948 setInvokingMethod(false); 949 setRunning(false); 950 setRequestTimeout(restoreTimeout); 951 try { 953 computeStackFrames(); 954 } catch (DebugException e) { 955 logError(e); 956 } 957 } 958 959 966 public void setEvaluatingConditionalBreakpoint(boolean evaluating) { 967 fIsEvaluatingConditionalBreakpoint= evaluating; 968 } 969 970 973 public String getName() throws DebugException { 974 try { 975 fPreviousName = fThread.name(); 976 } catch (RuntimeException e) { 977 if (e instanceof ObjectCollectedException) { 979 if (fPreviousName == null) { 980 fPreviousName= JDIDebugModelMessages.JDIThread_garbage_collected_1; 981 } 982 } else if (e instanceof VMDisconnectedException) { 983 if (fPreviousName == null) { 984 fPreviousName= JDIDebugModelMessages.JDIThread_42; 985 } 986 } else { 987 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIThread_exception_retrieving_thread_name, new String [] {e.toString()}), e); 988 } 989 } 990 return fPreviousName; 991 } 992 993 996 public int getPriority() throws DebugException { 997 Field p= null; 999 try { 1000 p= fThread.referenceType().fieldByName("priority"); if (p == null) { 1002 requestFailed(JDIDebugModelMessages.JDIThread_no_priority_field, null); 1003 } 1004 Value v= fThread.getValue(p); 1005 if (v instanceof IntegerValue) { 1006 return ((IntegerValue)v).value(); 1007 } 1008 requestFailed(JDIDebugModelMessages.JDIThread_priority_not_an_integer, null); 1009 } catch (RuntimeException e) { 1010 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIThread_exception_retrieving_thread_priority, new String [] {e.toString()}), e); 1011 } 1012 return -1; 1016 } 1017 1018 1021 public synchronized IStackFrame getTopStackFrame() throws DebugException { 1022 List c= computeStackFrames(); 1023 if (c.isEmpty()) { 1024 return null; 1025 } 1026 return (IStackFrame) c.get(0); 1027 } 1028 1029 1037 public synchronized boolean handleSuspendForBreakpoint(JavaBreakpoint breakpoint, boolean queueEvent) { 1038 addCurrentBreakpoint(breakpoint); 1039 setSuspendedQuiet(false); 1040 try { 1041 if (breakpoint.getSuspendPolicy() == IJavaBreakpoint.SUSPEND_VM) { 1044 ((JDIDebugTarget)getDebugTarget()).prepareToSuspendByBreakpoint(breakpoint); 1045 } else { 1046 setRunning(false); 1047 } 1048 1049 boolean suspend = JDIDebugPlugin.getDefault().fireBreakpointHit(this, breakpoint); 1051 1052 if (suspend) { 1054 if (breakpoint.getSuspendPolicy() == IJavaBreakpoint.SUSPEND_VM) { 1055 ((JDIDebugTarget)getDebugTarget()).suspendedByBreakpoint(breakpoint, queueEvent); 1056 } 1057 abortStep(); 1058 if (queueEvent) { 1059 queueSuspendEvent(DebugEvent.BREAKPOINT); 1060 } else { 1061 fireSuspendEvent(DebugEvent.BREAKPOINT); 1062 } 1063 } else { 1064 if (breakpoint.getSuspendPolicy() == IJavaBreakpoint.SUSPEND_VM) { 1065 ((JDIDebugTarget)getDebugTarget()).cancelSuspendByBreakpoint(breakpoint); 1066 } else { 1067 setRunning(true); 1068 preserveStackFrames(); 1070 } 1071 } 1072 return suspend; 1073 } catch (CoreException e) { 1074 logError(e); 1075 setRunning(true); 1076 return false; 1077 } 1078 } 1079 1080 public void wonSuspendVote(JavaBreakpoint breakpoint) { 1081 setSuspendedQuiet(false); 1082 try { 1083 setRunning(false); 1084 if (breakpoint.getSuspendPolicy() == IJavaBreakpoint.SUSPEND_VM) { 1085 ((JDIDebugTarget)getDebugTarget()).suspendedByBreakpoint(breakpoint, false); 1086 } 1087 } catch (CoreException e) { 1088 logError(e); 1089 } 1090 } 1091 1092 1098 public synchronized boolean handleSuspendForBreakpointQuiet(JavaBreakpoint breakpoint) { 1099 addCurrentBreakpoint(breakpoint); 1100 setSuspendedQuiet(true); 1101 setRunning(false); 1102 return true; 1103 } 1104 1105 1108 public boolean isStepping() { 1109 return getPendingStepHandler() != null; 1110 } 1111 1112 1115 public boolean isSuspended() { 1116 return !fRunning && !fTerminated; 1117 } 1118 1119 1122 public boolean isSuspendedQuiet() { 1123 return fSuspendedQuiet; 1124 } 1125 1126 1129 public boolean isSystemThread() { 1130 return fIsSystemThread; 1131 } 1132 1133 1136 public boolean isDaemon() throws DebugException { 1137 return fIsDaemon; 1138 } 1139 1140 1143 public String getThreadGroupName() throws DebugException { 1144 if (fThreadGroupName == null) { 1145 ThreadGroupReference tgr= getUnderlyingThreadGroup(); 1146 1147 if (tgr == null) { 1149 return null; 1150 } 1151 1152 try { 1153 fThreadGroupName = tgr.name(); 1154 } catch (RuntimeException e) { 1155 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIThread_exception_retrieving_thread_group_name, new String [] {e.toString()}), e); 1156 return null; 1159 } 1160 } 1161 return fThreadGroupName; 1162 } 1163 1164 1167 public boolean isTerminated() { 1168 return fTerminated; 1169 } 1170 1171 public synchronized boolean isOutOfSynch() throws DebugException { 1172 if (isSuspended() && ((JDIDebugTarget)getDebugTarget()).hasHCRFailed()) { 1173 List frames= computeStackFrames(); 1174 Iterator iter= frames.iterator(); 1175 while (iter.hasNext()) { 1176 if (((JDIStackFrame) iter.next()).isOutOfSynch()) { 1177 return true; 1178 } 1179 } 1180 return false; 1181 } 1182 return false; 1185 } 1186 1187 public boolean mayBeOutOfSynch() { 1188 if (!isSuspended()) { 1189 return ((JDIDebugTarget)getDebugTarget()).hasHCRFailed(); 1190 } 1191 return false; 1192 } 1193 1194 1199 protected void setTerminated(boolean terminated) { 1200 fTerminated= terminated; 1201 } 1202 1203 1206 public synchronized void resume() throws DebugException { 1207 if (getDebugTarget().isSuspended()) { 1208 getDebugTarget().resume(); 1209 } else { 1210 resumeThread(true); 1211 } 1212 } 1213 1214 1220 public synchronized void resumeQuiet() throws DebugException { 1221 if (isSuspendedQuiet()) { 1222 resumeThread(false); 1223 } 1224 } 1225 1226 1233 private synchronized void resumeThread(boolean fireNotification) throws DebugException { 1234 if (!isSuspended() || (isPerformingEvaluation() && !isInvokingMethod())) { 1235 return; 1236 } 1237 try { 1238 setRunning(true); 1239 setSuspendedQuiet(false); 1240 if (fireNotification) { 1241 fireResumeEvent(DebugEvent.CLIENT_REQUEST); 1242 } 1243 preserveStackFrames(); 1244 fThread.resume(); 1245 } catch (VMDisconnectedException e) { 1246 disconnected(); 1247 } catch (RuntimeException e) { 1248 setRunning(false); 1249 fireSuspendEvent(DebugEvent.CLIENT_REQUEST); 1250 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIThread_exception_resuming, new String [] {e.toString()}), e); 1251 } 1252 } 1253 1254 1261 protected void setRunning(boolean running) { 1262 fRunning = running; 1263 if (running) { 1264 fCurrentBreakpoints.clear(); 1265 } 1266 } 1267 1268 protected void setSuspendedQuiet(boolean suspendedQuiet) { 1269 fSuspendedQuiet= suspendedQuiet; 1270 } 1271 1272 1281 protected synchronized void preserveStackFrames() { 1282 fRefreshChildren = true; 1283 Iterator frames = fStackFrames.iterator(); 1284 while (frames.hasNext()) { 1285 ((JDIStackFrame)frames.next()).setUnderlyingStackFrame(null); 1286 } 1287 } 1288 1289 1297 protected synchronized void disposeStackFrames() { 1298 fStackFrames.clear(); 1299 fRefreshChildren = true; 1300 } 1301 1302 1308 public synchronized void stepInto() throws DebugException { 1309 if (!canStepInto()) { 1310 return; 1311 } 1312 StepHandler handler = new StepIntoHandler(); 1313 handler.step(); 1314 } 1315 1316 1322 public synchronized void stepOver() throws DebugException { 1323 if (!canStepOver()) { 1324 return; 1325 } 1326 StepHandler handler = new StepOverHandler(); 1327 handler.step(); 1328 } 1329 1330 1336 public synchronized void stepReturn() throws DebugException { 1337 if (!canStepReturn()) { 1338 return; 1339 } 1340 StepHandler handler = new StepReturnHandler(); 1341 handler.step(); 1342 } 1343 1344 protected void setOriginalStepKind(int stepKind) { 1345 fOriginalStepKind = stepKind; 1346 } 1347 1348 protected int getOriginalStepKind() { 1349 return fOriginalStepKind; 1350 } 1351 1352 protected void setOriginalStepLocation(Location location) { 1353 fOriginalStepLocation = location; 1354 } 1355 1356 protected Location getOriginalStepLocation() { 1357 return fOriginalStepLocation; 1358 } 1359 1360 protected void setOriginalStepStackDepth(int depth) { 1361 fOriginalStepStackDepth = depth; 1362 } 1363 1364 protected int getOriginalStepStackDepth() { 1365 return fOriginalStepStackDepth; 1366 } 1367 1368 1378 protected boolean shouldDoExtraStepInto(Location location) throws DebugException { 1379 if (getOriginalStepKind() != StepRequest.STEP_INTO) { 1380 return false; 1381 } 1382 if (getOriginalStepStackDepth() != getUnderlyingFrameCount()) { 1383 return false; 1384 } 1385 Location origLocation = getOriginalStepLocation(); 1386 if (origLocation == null) { 1387 return false; 1388 } 1389 Method origMethod = origLocation.method(); 1394 Method currMethod = location.method(); 1395 if (!origMethod.equals(currMethod)) { 1396 return false; 1397 } 1398 if (origLocation.lineNumber() != location.lineNumber()) { 1399 return false; 1400 } 1401 return true; 1402 } 1403 1404 1409 protected boolean shouldDoStepReturn() throws DebugException { 1410 if (getOriginalStepKind() == StepRequest.STEP_INTO) { 1411 if ((getOriginalStepStackDepth() + 1) < getUnderlyingFrameCount()) { 1412 return true; 1413 } 1414 } 1415 return false; 1416 } 1417 1418 1421 public synchronized void suspend() throws DebugException { 1422 try { 1423 abortStep(); 1425 setSuspendedQuiet(false); 1426 fEvaluationInterrupted = isPerformingEvaluation(); 1427 suspendUnderlyingThread(); 1428 } catch (RuntimeException e) { 1429 setRunning(true); 1430 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIThread_exception_suspending, new String [] {e.toString()}), e); 1431 } 1432 } 1433 1434 1438 protected synchronized void suspendUnderlyingThread() { 1439 if (fIsSuspending) { 1440 return; 1441 } 1442 if (isSuspended()) { 1443 fireSuspendEvent(DebugEvent.CLIENT_REQUEST); 1444 return; 1445 } 1446 fIsSuspending= true; 1447 Thread thread= new Thread (new Runnable () { 1448 public void run() { 1449 try { 1450 fThread.suspend(); 1451 int timeout= JDIDebugModel.getPreferences().getInt(JDIDebugModel.PREF_REQUEST_TIMEOUT); 1452 long stop= System.currentTimeMillis() + timeout; 1453 boolean suspended= isUnderlyingThreadSuspended(); 1454 while (System.currentTimeMillis() < stop && !suspended) { 1455 try { 1456 Thread.sleep(50); 1457 } catch (InterruptedException e) { 1458 } 1459 suspended= isUnderlyingThreadSuspended(); 1460 if (suspended) { 1461 break; 1462 } 1463 } 1464 if (!suspended) { 1465 IStatus status= new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), SUSPEND_TIMEOUT, MessageFormat.format(JDIDebugModelMessages.JDIThread_suspend_timeout, new String [] {new Integer (timeout).toString()}), null); 1466 IStatusHandler handler= DebugPlugin.getDefault().getStatusHandler(status); 1467 if (handler != null) { 1468 try { 1469 handler.handleStatus(status, JDIThread.this); 1470 } catch (CoreException e) { 1471 } 1472 } 1473 } 1474 setRunning(false); 1475 fireSuspendEvent(DebugEvent.CLIENT_REQUEST); 1476 } catch (RuntimeException exception) { 1477 } finally { 1478 fIsSuspending= false; 1479 } 1480 } 1481 }); 1482 thread.setDaemon(true); 1483 thread.start(); 1484 } 1485 1486 public boolean isUnderlyingThreadSuspended() { 1487 return fThread.isSuspended(); 1488 } 1489 1490 1494 protected synchronized void suspendedByVM() { 1495 setRunning(false); 1496 setSuspendedQuiet(false); 1497 } 1498 1499 1503 protected synchronized void resumedByVM() throws DebugException { 1504 setRunning(true); 1505 preserveStackFrames(); 1506 ThreadReference thread= fThread; 1511 while (thread.suspendCount() > 1) { 1512 try { 1513 thread.resume(); 1514 } catch (ObjectCollectedException e) { 1515 } catch (VMDisconnectedException e) { 1516 disconnected(); 1517 }catch (RuntimeException e) { 1518 setRunning(false); 1519 fireSuspendEvent(DebugEvent.CLIENT_REQUEST); 1520 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIThread_exception_resuming, new String [] {e.toString()}), e); } 1522 } 1523 } 1524 1525 1528 public void terminate() throws DebugException { 1529 terminateEvaluation(); 1530 getDebugTarget().terminate(); 1531 } 1532 1533 1543 protected void dropToFrame(IStackFrame frame) throws DebugException { 1544 JDIDebugTarget target= (JDIDebugTarget) getDebugTarget(); 1545 if (target.canPopFrames()) { 1546 try { 1548 popFrame(frame); 1550 stepInto(); 1551 } catch (RuntimeException exception) { 1552 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIThread_exception_dropping_to_frame, new String [] {exception.toString()}),exception); 1553 } 1554 } else { 1555 synchronized (this) { 1559 StepHandler handler = new DropToFrameHandler(frame); 1560 handler.step(); 1561 } 1562 } 1563 } 1564 1565 protected void popFrame(IStackFrame frame) throws DebugException { 1566 JDIDebugTarget target= (JDIDebugTarget)getDebugTarget(); 1567 if (target.canPopFrames()) { 1568 try { 1570 StackFrame jdiFrame= null; 1572 int desiredSize= fStackFrames.size() - fStackFrames.indexOf(frame) - 1; 1573 int lastSize= fStackFrames.size() + 1; int size= fStackFrames.size(); 1575 while (size < lastSize && size > desiredSize) { 1576 jdiFrame = ((JDIStackFrame) frame).getUnderlyingStackFrame(); 1580 preserveStackFrames(); 1581 fThread.popFrames(jdiFrame); 1582 lastSize= size; 1583 size= computeStackFrames().size(); 1584 } 1585 } catch (IncompatibleThreadStateException exception) { 1586 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIThread_exception_popping, new String [] {exception.toString()}),exception); 1587 } catch (InvalidStackFrameException exception) { 1588 fireChangeEvent(DebugEvent.CONTENT); 1592 targetRequestFailed(exception.toString(),exception); 1593 } catch (RuntimeException exception) { 1594 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIThread_exception_popping, new String [] {exception.toString()}),exception); 1595 } 1596 } 1597 } 1598 1599 1612 protected synchronized void stepToFrame(IStackFrame frame) throws DebugException { 1613 if (!canStepReturn()) { 1614 return; 1615 } 1616 StepHandler handler = new StepToFrameHandler(frame); 1617 handler.step(); 1618 } 1619 1620 1623 protected void abortStep() { 1624 StepHandler handler = getPendingStepHandler(); 1625 if (handler != null) { 1626 handler.abort(); 1627 } 1628 } 1629 1630 1633 public IJavaVariable findVariable(String varName) throws DebugException { 1634 if (isSuspended()) { 1635 try { 1636 IStackFrame[] stackFrames= getStackFrames(); 1637 for (int i = 0; i < stackFrames.length; i++) { 1638 IJavaStackFrame sf= (IJavaStackFrame)stackFrames[i]; 1639 IJavaVariable var= sf.findVariable(varName); 1640 if (var != null) { 1641 return var; 1642 } 1643 } 1644 } catch (DebugException e) { 1645 if (e.getStatus().getCode() != IJavaThread.ERR_THREAD_NOT_SUSPENDED) { 1647 throw e; 1648 } 1649 } 1650 } 1651 return null; 1652 } 1653 1654 1658 protected void terminated() { 1659 setTerminated(true); 1660 setRunning(false); 1661 fireTerminateEvent(); 1662 } 1663 1664 1670 public ThreadReference getUnderlyingThread() { 1671 return fThread; 1672 } 1673 1674 1680 protected void setUnderlyingThread(ThreadReference thread) { 1681 fThread = thread; 1682 } 1683 1684 1697 protected ThreadGroupReference getUnderlyingThreadGroup() throws DebugException { 1698 if (fThreadGroup == null) { 1699 try { 1700 fThreadGroup = fThread.threadGroup(); 1701 } catch (UnsupportedOperationException e) { 1702 requestFailed(MessageFormat.format(JDIDebugModelMessages.JDIThread_exception_retrieving_thread_group, new String [] {e.toString()}), e); 1703 return null; 1706 } catch (VMDisconnectedException e) { 1707 return null; 1709 } catch (ObjectCollectedException e) { 1710 return null; 1712 } catch (RuntimeException e) { 1713 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIThread_exception_retrieving_thread_group, new String [] {e.toString()}), e); 1714 return null; 1717 } 1718 } 1719 return fThreadGroup; 1720 } 1721 1722 1725 public boolean isPerformingEvaluation() { 1726 return fIsPerformingEvaluation; 1727 } 1728 1729 1733 public boolean isInvokingMethod() { 1734 return fIsInvokingMethod; 1735 } 1736 1737 1741 public boolean isIgnoringBreakpoints() { 1742 return !fHonorBreakpoints; 1743 } 1744 1745 1751 protected void setInvokingMethod(boolean invoking) { 1752 fIsInvokingMethod= invoking; 1753 } 1754 1755 1762 protected void setPendingStepHandler(StepHandler handler) { 1763 fStepHandler = handler; 1764 } 1765 1766 1772 protected StepHandler getPendingStepHandler() { 1773 return fStepHandler; 1774 } 1775 1776 1777 1780 abstract class StepHandler implements IJDIEventListener { 1781 1784 private StepRequest fStepRequest; 1785 1786 1808 protected void step() throws DebugException { 1809 JDIStackFrame top = (JDIStackFrame)getTopStackFrame(); 1810 if (top == null) { 1811 return; 1812 } 1813 setOriginalStepKind(getStepKind()); 1814 Location location = top.getUnderlyingStackFrame().location(); 1815 setOriginalStepLocation(location); 1816 setOriginalStepStackDepth(computeStackFrames().size()); 1817 setStepRequest(createStepRequest()); 1818 setPendingStepHandler(this); 1819 addJDIEventListener(this, getStepRequest()); 1820 setRunning(true); 1821 preserveStackFrames(); 1822 fireResumeEvent(getStepDetail()); 1823 invokeThread(); 1824 } 1825 1826 1838 protected void invokeThread() throws DebugException { 1839 try { 1840 fThread.resume(); 1841 } catch (RuntimeException e) { 1842 stepEnd(); 1843 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIThread_exception_stepping, new String [] {e.toString()}), e); 1844 } 1845 } 1846 1847 1860 protected StepRequest createStepRequest() throws DebugException { 1861 return createStepRequest(getStepKind()); 1862 } 1863 1864 1877 protected StepRequest createStepRequest(int kind) throws DebugException { 1878 EventRequestManager manager = getEventRequestManager(); 1879 if (manager == null) { 1880 requestFailed(JDIDebugModelMessages.JDIThread_Unable_to_create_step_request___VM_disconnected__1, null); 1881 } 1882 try { 1883 StepRequest request = manager.createStepRequest(fThread, StepRequest.STEP_LINE, kind); 1884 request.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD); 1885 request.addCountFilter(1); 1886 attachFiltersToStepRequest(request); 1887 request.enable(); 1888 return request; 1889 } catch (RuntimeException e) { 1890 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIThread_exception_creating_step_request, new String [] {e.toString()}), e); 1891 } 1892 return null; 1896 1897 } 1898 1899 1905 protected abstract int getStepKind(); 1906 1907 1913 protected abstract int getStepDetail(); 1914 1915 1922 protected void setStepRequest(StepRequest request) { 1923 fStepRequest = request; 1924 } 1925 1926 1932 protected StepRequest getStepRequest() { 1933 return fStepRequest; 1934 } 1935 1936 1940 protected void deleteStepRequest() { 1941 removeJDIEventListener(this, getStepRequest()); 1942 try { 1943 EventRequestManager manager = getEventRequestManager(); 1944 if (manager != null) { 1945 manager.deleteEventRequest(getStepRequest()); 1946 } 1947 setStepRequest(null); 1948 } catch (RuntimeException e) { 1949 logError(e); 1950 } 1951 } 1952 1953 1957 protected void attachFiltersToStepRequest(StepRequest request) { 1958 1959 if (applyStepFilters() && isStepFiltersEnabled()) { 1960 Location currentLocation= getOriginalStepLocation(); 1961 if (currentLocation == null || !JAVA_STRATUM_CONSTANT.equals(currentLocation.declaringType().defaultStratum())) { 1962 return; 1963 } 1964 String [] activeFilters = getJavaDebugTarget().getStepFilters(); 1970 if (activeFilters != null) { 1977 for (int i = 0; i < activeFilters.length; i++) { 1978 request.addClassExclusionFilter(activeFilters[i]); 1979 } 1980 } 1981 } 1982 } 1983 1984 1993 protected boolean applyStepFilters() { 1994 return true; 1995 } 1996 1997 2005 public boolean handleEvent(Event event, JDIDebugTarget target) { 2006 try { 2007 StepEvent stepEvent = (StepEvent) event; 2008 Location currentLocation = stepEvent.location(); 2009 2010 2011 if (!getJavaDebugTarget().isStepThruFilters()) { 2012 if (shouldDoStepReturn()) { 2013 deleteStepRequest(); 2014 createSecondaryStepRequest(StepRequest.STEP_OUT); 2015 return true; 2016 } 2017 } 2018 if (locationShouldBeFiltered(currentLocation) || shouldDoExtraStepInto(currentLocation)) { 2022 setRunning(true); 2023 deleteStepRequest(); 2024 createSecondaryStepRequest(); 2025 return true; 2026 } 2028 stepEnd(); 2029 return false; 2030 } catch (DebugException e) { 2031 logError(e); 2032 stepEnd(); 2033 return false; 2034 } 2035 } 2036 2037 2038 2039 2042 public void wonSuspendVote(Event event, JDIDebugTarget target) { 2043 } 2045 2046 2052 protected boolean locationShouldBeFiltered(Location location) throws DebugException { 2053 if (applyStepFilters()) { 2054 Location origLocation= getOriginalStepLocation(); 2055 if (origLocation != null) { 2056 return !locationIsFiltered(origLocation.method()) && locationIsFiltered(location.method()); 2057 } 2058 } 2059 return false; 2060 } 2061 2066 protected boolean locationIsFiltered(Method method) { 2067 if (isStepFiltersEnabled()) { 2068 boolean filterStatics = getJavaDebugTarget().isFilterStaticInitializers(); 2069 boolean filterSynthetics = getJavaDebugTarget().isFilterSynthetics(); 2070 boolean filterConstructors = getJavaDebugTarget().isFilterConstructors(); 2071 if (!(filterStatics || filterSynthetics || filterConstructors)) { 2072 return false; 2073 } 2074 2075 if ((filterStatics && method.isStaticInitializer()) || 2076 (filterSynthetics && method.isSynthetic()) || 2077 (filterConstructors && method.isConstructor()) ) { 2078 return true; 2079 } 2080 } 2081 2082 return false; 2083 } 2084 2085 2095 protected void stepEnd() { 2096 setRunning(false); 2097 deleteStepRequest(); 2098 setPendingStepHandler(null); 2099 queueSuspendEvent(DebugEvent.STEP_END); 2100 } 2101 2102 2118 protected void createSecondaryStepRequest() throws DebugException { 2119 createSecondaryStepRequest(getStepKind()); 2120 } 2121 2122 2140 protected void createSecondaryStepRequest(int kind) throws DebugException { 2141 setStepRequest(createStepRequest(kind)); 2142 setPendingStepHandler(this); 2143 addJDIEventListener(this, getStepRequest()); 2144 } 2145 2146 2150 protected void abort() { 2151 if (getStepRequest() != null) { 2152 deleteStepRequest(); 2153 setPendingStepHandler(null); 2154 } 2155 } 2156} 2157 2158 2161 class StepOverHandler extends StepHandler { 2162 2165 protected int getStepKind() { 2166 return StepRequest.STEP_OVER; 2167 } 2168 2169 2172 protected int getStepDetail() { 2173 return DebugEvent.STEP_OVER; 2174 } 2175 } 2176 2177 2180 class StepIntoHandler extends StepHandler { 2181 2184 protected int getStepKind() { 2185 return StepRequest.STEP_INTO; 2186 } 2187 2188 2191 protected int getStepDetail() { 2192 return DebugEvent.STEP_INTO; 2193 } 2194 2195 } 2196 2197 2200 class StepReturnHandler extends StepHandler { 2201 2204 protected boolean locationShouldBeFiltered(Location location) throws DebugException { 2205 if (getOriginalStepStackDepth() == getUnderlyingFrameCount()) { 2207 return true; 2208 } 2209 return super.locationShouldBeFiltered(location); 2210 } 2211 2212 2215 protected int getStepKind() { 2216 return StepRequest.STEP_OUT; 2217 } 2218 2219 2222 protected int getStepDetail() { 2223 return DebugEvent.STEP_RETURN; 2224 } 2225 } 2226 2227 2234 class StepToFrameHandler extends StepReturnHandler { 2235 2236 2239 private int fRemainingFrames; 2240 2241 2253 protected StepToFrameHandler(IStackFrame frame) throws DebugException { 2254 List frames = computeStackFrames(); 2255 setRemainingFrames(frames.size() - frames.indexOf(frame)); 2256 } 2257 2258 2264 protected void setRemainingFrames(int num) { 2265 fRemainingFrames = num; 2266 } 2267 2268 2274 protected int getRemainingFrames() { 2275 return fRemainingFrames; 2276 } 2277 2278 2287 public boolean handleEvent(Event event, JDIDebugTarget target) { 2288 try { 2289 int numFrames = getUnderlyingFrameCount(); 2290 if (numFrames <= getRemainingFrames()) { 2292 stepEnd(); 2293 return false; 2294 } 2295 setRunning(true); 2297 deleteStepRequest(); 2298 createSecondaryStepRequest(); 2299 return true; 2300 } catch (DebugException e) { 2301 logError(e); 2302 stepEnd(); 2303 return false; 2304 } 2305 } 2306 } 2307 2308 2311 class DropToFrameHandler extends StepReturnHandler { 2312 2313 2317 private int fFramesToDrop; 2318 2319 2331 protected DropToFrameHandler(IStackFrame frame) throws DebugException { 2332 List frames = computeStackFrames(); 2333 setFramesToDrop(frames.indexOf(frame)); 2334 } 2335 2336 2341 protected void setFramesToDrop(int num) { 2342 fFramesToDrop = num; 2343 } 2344 2345 2350 protected int getFramesToDrop() { 2351 return fFramesToDrop; 2352 } 2353 2354 2362 protected void invokeThread() throws DebugException { 2363 if (getFramesToDrop() < 0) { 2364 super.invokeThread(); 2365 } else { 2366 try { 2367 org.eclipse.jdi.hcr.ThreadReference hcrThread= (org.eclipse.jdi.hcr.ThreadReference) fThread; 2368 hcrThread.doReturn(null, true); 2369 } catch (RuntimeException e) { 2370 stepEnd(); 2371 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIThread_exception_while_popping_stack_frame, new String [] {e.toString()}), e); 2372 } 2373 } 2374 } 2375 2376 2385 public boolean handleEvent(Event event, JDIDebugTarget target) { 2386 setFramesToDrop(getFramesToDrop() - 1); 2388 try { 2389 if (getFramesToDrop() >= -1) { 2390 deleteStepRequest(); 2391 doSecondaryStep(); 2392 } else { 2393 stepEnd(); 2394 } 2395 } catch (DebugException e) { 2396 stepEnd(); 2397 logError(e); 2398 } 2399 return false; 2400 } 2401 2402 2413 protected void doSecondaryStep() throws DebugException { 2414 setStepRequest(createStepRequest()); 2415 setPendingStepHandler(this); 2416 addJDIEventListener(this, getStepRequest()); 2417 invokeThread(); 2418 } 2419 2420 2434 protected StepRequest createStepRequest() throws DebugException { 2435 EventRequestManager manager = getEventRequestManager(); 2436 if (manager == null) { 2437 requestFailed(JDIDebugModelMessages.JDIThread_Unable_to_create_step_request___VM_disconnected__2, null); 2438 } 2439 int num = getFramesToDrop(); 2440 if (num > 0) { 2441 return super.createStepRequest(); 2442 } else if (num == 0) { 2443 try { 2444 StepRequest request = ((org.eclipse.jdi.hcr.EventRequestManager) manager).createReenterStepRequest(fThread); 2445 request.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD); 2446 request.addCountFilter(1); 2447 request.enable(); 2448 return request; 2449 } catch (RuntimeException e) { 2450 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIThread_exception_creating_step_request, new String [] {e.toString()}), e); 2451 } 2452 } else if (num == -1) { 2453 try { 2454 StepRequest request = manager.createStepRequest(fThread, StepRequest.STEP_LINE, StepRequest.STEP_INTO); 2455 request.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD); 2456 request.addCountFilter(1); 2457 request.enable(); 2458 return request; 2459 } catch (RuntimeException e) { 2460 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIThread_exception_creating_step_request, new String [] {e.toString()}), e); 2461 } 2462 } 2463 return null; 2467 } 2468 } 2469 2470 2471 2474 public boolean hasStackFrames() throws DebugException { 2475 return isSuspended(); 2476 } 2477 2478 2481 public Object getAdapter(Class adapter) { 2482 if (adapter == IJavaThread.class) { 2483 return this; 2484 } 2485 if (adapter == IJavaStackFrame.class) { 2486 try { 2487 return getTopStackFrame(); 2488 } catch (DebugException e) { 2489 } 2491 } 2492 return super.getAdapter(adapter); 2493 } 2494 2495 2498 public boolean hasOwnedMonitors() throws DebugException { 2499 return isSuspended() && getOwnedMonitors().length > 0; 2500 } 2501 2502 2503 2506 public IJavaObject[] getOwnedMonitors() throws DebugException { 2507 try { 2508 JDIDebugTarget target= (JDIDebugTarget)getDebugTarget(); 2509 List ownedMonitors= fThread.ownedMonitors(); 2510 IJavaObject[] javaOwnedMonitors= new IJavaObject[ownedMonitors.size()]; 2511 Iterator itr= ownedMonitors.iterator(); 2512 int i= 0; 2513 while (itr.hasNext()) { 2514 ObjectReference element = (ObjectReference) itr.next(); 2515 javaOwnedMonitors[i]= new JDIObjectValue(target, element); 2516 i++; 2517 } 2518 return javaOwnedMonitors; 2519 } catch (IncompatibleThreadStateException e) { 2520 targetRequestFailed(JDIDebugModelMessages.JDIThread_43, e); 2521 } catch (RuntimeException e) { 2522 targetRequestFailed(JDIDebugModelMessages.JDIThread_44, e); 2523 } 2524 return null; 2525 } 2526 2527 2530 public IJavaObject getContendedMonitor() throws DebugException { 2531 try { 2532 ObjectReference monitor= fThread.currentContendedMonitor(); 2533 if (monitor != null) { 2534 return new JDIObjectValue((JDIDebugTarget)getDebugTarget(), monitor); 2535 } 2536 } catch (IncompatibleThreadStateException e) { 2537 targetRequestFailed(JDIDebugModelMessages.JDIThread_45, e); 2538 } catch (RuntimeException e) { 2539 targetRequestFailed(JDIDebugModelMessages.JDIThread_46, e); 2540 } 2541 2542 return null; 2543 } 2544 2547 public boolean canStepWithFilters() { 2548 if (canStepInto()) { 2549 String [] filters = getJavaDebugTarget().getStepFilters(); 2550 return filters != null && filters.length > 0; 2551 } 2552 return false; 2553 } 2554 2555 2558 public void stepWithFilters() throws DebugException { 2559 if (!canStepWithFilters()) { 2560 return; 2561 } 2562 stepInto(); 2563 } 2564 2565 2568 static class ThreadJob extends Job { 2569 2570 private Vector fRunnables; 2571 2572 private JDIThread fJDIThread; 2573 2574 public ThreadJob(JDIThread thread) { 2575 super(JDIDebugModelMessages.JDIThread_39); 2576 fJDIThread= thread; 2577 fRunnables= new Vector (5); 2578 setSystem(true); 2579 } 2580 2581 public void addRunnable(Runnable runnable) { 2582 synchronized (fRunnables) { 2583 fRunnables.add(runnable); 2584 } 2585 schedule(); 2586 } 2587 2588 public boolean isEmpty() { 2589 return fRunnables.isEmpty(); 2590 } 2591 2592 2595 public IStatus run(IProgressMonitor monitor) { 2596 fJDIThread.fRunningAsyncJob= this; 2597 Object [] runnables; 2598 synchronized (fRunnables) { 2599 runnables= fRunnables.toArray(); 2600 fRunnables.clear(); 2601 } 2602 2603 MultiStatus failed = null; 2604 monitor.beginTask(this.getName(), runnables.length); 2605 int i = 0; 2606 while (i < runnables.length && !fJDIThread.isTerminated() && !monitor.isCanceled()) { 2607 try { 2608 ((Runnable ) runnables[i]).run(); 2609 } catch (Exception e) { 2610 if (failed == null) { 2611 failed = new MultiStatus(JDIDebugPlugin.getUniqueIdentifier(), JDIDebugPlugin.INTERNAL_ERROR, JDIDebugModelMessages.JDIThread_0, null); 2612 } 2613 failed.add(new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), JDIDebugPlugin.INTERNAL_ERROR, JDIDebugModelMessages.JDIThread_0, e)); 2614 } 2615 i++; 2616 monitor.worked(1); 2617 } 2618 fJDIThread.fRunningAsyncJob= null; 2619 monitor.done(); 2620 if (failed == null) { 2621 return Status.OK_STATUS; 2622 } 2623 return failed; 2624 } 2625 2626 2629 public boolean shouldRun() { 2630 return !fJDIThread.isTerminated() && !fRunnables.isEmpty(); 2631 } 2632 2633 } 2634 2635 2636 2639 public void stop(IJavaObject exception) throws DebugException { 2640 try { 2641 fThread.stop(((JDIObjectValue)exception).getUnderlyingObject()); 2642 } catch (InvalidTypeException e) { 2643 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIThread_exception_stoping_thread, new String [] {e.toString()}), e); 2644 } 2645 } 2646 2647 2650 public IJavaThreadGroup getThreadGroup() throws DebugException { 2651 ThreadGroupReference group = getUnderlyingThreadGroup(); 2652 if (group != null) { 2653 return getJavaDebugTarget().findThreadGroup(group); 2654 } 2655 return null; 2656 } 2657 2658 2661 public int getFrameCount() throws DebugException { 2662 return getUnderlyingFrameCount(); 2663 } 2664 2665 protected void forceReturn(IJavaValue value) throws DebugException { 2666 if (!isSuspended()) { 2667 return; 2668 } 2669 try { 2670 fThread.forceEarlyReturn(((JDIValue)value).getUnderlyingValue()); 2671 stepReturn(); 2672 } catch (VMDisconnectedException e) { 2673 disconnected(); 2674 } catch (InvalidTypeException e) { 2675 targetRequestFailed(JDIDebugModelMessages.JDIThread_48, e); 2676 } catch (ClassNotLoadedException e) { 2677 targetRequestFailed(JDIDebugModelMessages.JDIThread_48, e); 2678 } catch (IncompatibleThreadStateException e) { 2679 targetRequestFailed(JDIDebugModelMessages.JDIThread_48, e); 2680 } catch (UnsupportedOperationException e) { 2681 requestFailed(JDIDebugModelMessages.JDIThread_48, e); 2682 } catch (RuntimeException e) { 2683 targetRequestFailed(JDIDebugModelMessages.JDIThread_48, e); 2684 } 2685 } 2686 2687 2688 2694 class SerialPerObjectRule implements ISchedulingRule { 2695 2696 private Object fObject = null; 2697 2698 public SerialPerObjectRule(Object lock) { 2699 fObject = lock; 2700 } 2701 2702 2705 public boolean contains(ISchedulingRule rule) { 2706 return rule == this; 2707 } 2708 2709 2712 public boolean isConflicting(ISchedulingRule rule) { 2713 if (rule instanceof SerialPerObjectRule) { 2714 SerialPerObjectRule vup = (SerialPerObjectRule) rule; 2715 return fObject == vup.fObject; 2716 } 2717 return false; 2718 } 2719 2720 } 2721 2722 2728 public ISchedulingRule getThreadRule() { 2729 return new SerialPerObjectRule(this); 2730 } 2731} 2732 | Popular Tags |