Bug 65604 - Invalid stack frame exceptions
diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/AbstractDebugTest.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/AbstractDebugTest.java
index 1cde846..fbf8f22 100644
--- a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/AbstractDebugTest.java
+++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/AbstractDebugTest.java
Binary files differ
diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/core/InstanceVariableTests.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/core/InstanceVariableTests.java
index 7a8f177..9b696b3 100644
--- a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/core/InstanceVariableTests.java
+++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/core/InstanceVariableTests.java
@@ -200,10 +200,8 @@
 			assertFalse("i should not have changed", i.hasValueChanged());
 			
 			// after a step over "count++" it should have changed
-			thread = stepOver(frame);
-			// re-retrieve frame to force frames to update indicies
-			frame = (IJavaStackFrame)thread.getTopStackFrame();
-						
+			stepOver(frame);
+			
 			// count should have changed, and i should not
 			count.getValue();
 			i.getValue();			
@@ -211,8 +209,6 @@
 			assertFalse("i should still be the same", i.hasValueChanged());
 			
 			stepOver(frame);
-			// re-retrieve frame to force frames to update indicies
-			frame = (IJavaStackFrame)thread.getTopStackFrame();
 			
 			// now count should be the same, and i should have changed
 			count.getValue();
diff --git a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/JDIModelPresentation.java b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/JDIModelPresentation.java
index a3600fc..9a39230 100644
--- a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/JDIModelPresentation.java
+++ b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/JDIModelPresentation.java
@@ -849,7 +849,9 @@
 				}
 			}
 		} catch (DebugException e) {
-			JDIDebugUIPlugin.log(e);
+			if (!(e.getStatus().getCode() == IJavaThread.ERR_THREAD_NOT_SUSPENDED)) {
+				JDIDebugUIPlugin.log(e);
+			}
 		}
 		return 0;
 	}
diff --git a/org.eclipse.jdt.debug/model/org/eclipse/jdt/internal/debug/core/model/JDIStackFrame.java b/org.eclipse.jdt.debug/model/org/eclipse/jdt/internal/debug/core/model/JDIStackFrame.java
index 081d0be..ddc0b4e 100644
--- a/org.eclipse.jdt.debug/model/org/eclipse/jdt/internal/debug/core/model/JDIStackFrame.java
+++ b/org.eclipse.jdt.debug/model/org/eclipse/jdt/internal/debug/core/model/JDIStackFrame.java
@@ -956,9 +956,17 @@
 			if (fStackFrame == null) {
 				int depth= getDepth();
 				if (depth == -1) {
-					// Depth is set to -1 when the thread clears its handles
-					// to this object. See Bug 47198.
-					throw new DebugException(new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), IStatus.ERROR, JDIDebugModelMessages.getString("JDIStackFrame.25"), null)); //$NON-NLS-1$
+					if (fThread.isSuspended()) {
+						// re-index stack frames - See Bug 47198
+						fThread.computeStackFrames();
+						depth = getDepth();
+						if (depth == -1) {
+							// If depth still -1, then this is an invalid frame
+							throw new DebugException(new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), IStatus.ERROR, JDIDebugModelMessages.getString("JDIStackFrame.25"), null)); //$NON-NLS-1$
+						}
+					} else {
+						throw new DebugException(new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), IJavaThread.ERR_THREAD_NOT_SUSPENDED, JDIDebugModelMessages.getString("JDIStackFrame.25"), null)); //$NON-NLS-1$
+					}
 				}
 				setUnderlyingStackFrame(((JDIThread)getThread()).getUnderlyingFrame(depth));
 			}
diff --git a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/sourcelookup/containers/JavaSourceLookupParticipant.java b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/sourcelookup/containers/JavaSourceLookupParticipant.java
index 0e9d1aa..cedfa4c 100644
--- a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/sourcelookup/containers/JavaSourceLookupParticipant.java
+++ b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/sourcelookup/containers/JavaSourceLookupParticipant.java
@@ -20,6 +20,7 @@
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IAdaptable;
 import org.eclipse.core.runtime.IPath;
+import org.eclipse.debug.core.DebugException;
 import org.eclipse.debug.core.sourcelookup.AbstractSourceLookupParticipant;
 import org.eclipse.debug.core.sourcelookup.ISourceContainer;
 import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector;
@@ -29,6 +30,7 @@
 import org.eclipse.jdt.core.JavaCore;
 import org.eclipse.jdt.core.JavaModelException;
 import org.eclipse.jdt.debug.core.IJavaStackFrame;
+import org.eclipse.jdt.debug.core.IJavaThread;
 
 /**
  * A source lookup participant that searches for Java source code.
@@ -59,32 +61,40 @@
 	public String getSourceName(Object object) throws CoreException {
 		if (object instanceof IAdaptable) {
 			IJavaStackFrame frame = (IJavaStackFrame) ((IAdaptable)object).getAdapter(IJavaStackFrame.class);
-			if (frame != null) {
-				if (frame.isObsolete()) {
+			try {
+				if (frame != null) {
+					if (frame.isObsolete()) {
+						return null;
+					}
+					String sourceName = frame.getSourcePath();
+					// TODO: this may break fix to bug 21518
+					if (sourceName == null) {
+						// no debug attributes, guess at source name
+						sourceName = frame.getDeclaringTypeName();
+						int index = sourceName.lastIndexOf('.');
+						if (index < 0) {
+							index = 0;
+						}
+						sourceName = sourceName.replace('.', File.separatorChar);
+						index = sourceName.indexOf('$');
+						if (index >= 0) {
+							sourceName = sourceName.substring(0, index);
+						}
+						if (sourceName.length() == 0) {
+							// likely a proxy class (see bug 40815)
+							sourceName = null;
+						} else {
+							sourceName = sourceName + ".java"; //$NON-NLS-1$
+						}
+					}
+					return sourceName;	
+				}
+			} catch (DebugException e) {
+				if (e.getStatus().getCode() == IJavaThread.ERR_THREAD_NOT_SUSPENDED) {
 					return null;
+				} else {
+					throw e;
 				}
-				String sourceName = frame.getSourcePath();
-				// TODO: this may break fix to bug 21518
-				if (sourceName == null) {
-					// no debug attributes, guess at source name
-					sourceName = frame.getDeclaringTypeName();
-					int index = sourceName.lastIndexOf('.');
-					if (index < 0) {
-						index = 0;
-					}
-					sourceName = sourceName.replace('.', File.separatorChar);
-					index = sourceName.indexOf('$');
-					if (index >= 0) {
-						sourceName = sourceName.substring(0, index);
-					}
-					if (sourceName.length() == 0) {
-						// likely a proxy class (see bug 40815)
-						sourceName = null;
-					} else {
-						sourceName = sourceName + ".java"; //$NON-NLS-1$
-					}
-				}
-				return sourceName;	
 			}
 		}
 		if (object instanceof String) {