Debugger: Gracefully handle debugger exceptions we can't do anything reasonable with (EA-128154, EA-139473)

This commit is contained in:
Yan Zhulanow
2019-07-24 17:48:58 +09:00
parent 70745f233f
commit 17c3406097
4 changed files with 44 additions and 26 deletions
@@ -25,8 +25,6 @@ class KotlinCoroutinesAsyncStackTraceProvider : KotlinCoroutinesAsyncStackTraceP
private companion object { private companion object {
const val DEBUG_METADATA_KT = "kotlin.coroutines.jvm.internal.DebugMetadataKt" const val DEBUG_METADATA_KT = "kotlin.coroutines.jvm.internal.DebugMetadataKt"
private val LOG = Logger.getInstance(this::class.java)
tailrec fun findBaseContinuationSuperSupertype(type: ClassType): ClassType? { tailrec fun findBaseContinuationSuperSupertype(type: ClassType): ClassType? {
if (type.name() == "kotlin.coroutines.jvm.internal.BaseContinuationImpl") { if (type.name() == "kotlin.coroutines.jvm.internal.BaseContinuationImpl") {
return type return type
@@ -37,25 +35,7 @@ class KotlinCoroutinesAsyncStackTraceProvider : KotlinCoroutinesAsyncStackTraceP
} }
override fun getAsyncStackTrace(stackFrame: JavaStackFrame, suspendContext: SuspendContextImpl): List<StackFrameItem>? { override fun getAsyncStackTrace(stackFrame: JavaStackFrame, suspendContext: SuspendContextImpl): List<StackFrameItem>? {
return try { return hopelessAware { getAsyncStackTraceSafe(stackFrame.stackFrameProxy, suspendContext) }
getAsyncStackTraceSafe(stackFrame.stackFrameProxy, suspendContext)
} catch (e: Exception) {
handleException(e)
null
}
}
private fun handleException(e: Exception) {
when (if (e is EvaluateException) e.cause ?: e else e) {
is ObjectCollectedException, is IncompatibleThreadStateException, is VMDisconnectedException -> {}
else -> {
if (e is EvaluateException) {
LOG.debug("Cannot evaluate async stack trace", e)
} else {
throw e
}
}
}
} }
fun getAsyncStackTraceSafe(frameProxy: StackFrameProxyImpl, suspendContext: SuspendContextImpl): List<StackFrameItem>? { fun getAsyncStackTraceSafe(frameProxy: StackFrameProxyImpl, suspendContext: SuspendContextImpl): List<StackFrameItem>? {
@@ -284,7 +284,7 @@ class KotlinPositionManager(private val myDebugProcess: DebugProcess) : MultiReq
val psiFile = sourcePosition.file val psiFile = sourcePosition.file
if (psiFile is KtFile) { if (psiFile is KtFile) {
if (!ProjectRootsUtil.isInProjectOrLibSource(psiFile)) return emptyList() if (!ProjectRootsUtil.isInProjectOrLibSource(psiFile)) return emptyList()
return DebuggerClassNameProvider(myDebugProcess).getClassesForPosition(sourcePosition) return hopelessAware { DebuggerClassNameProvider(myDebugProcess).getClassesForPosition(sourcePosition) } ?: emptyList()
} }
if (psiFile is ClsFileImpl) { if (psiFile is ClsFileImpl) {
@@ -40,6 +40,7 @@ import org.jetbrains.kotlin.idea.KotlinFileType
import org.jetbrains.kotlin.idea.core.util.getKotlinJvmRuntimeMarkerClass import org.jetbrains.kotlin.idea.core.util.getKotlinJvmRuntimeMarkerClass
import org.jetbrains.kotlin.idea.debugger.evaluate.compilation.DebugLabelPropertyDescriptorProvider import org.jetbrains.kotlin.idea.debugger.evaluate.compilation.DebugLabelPropertyDescriptorProvider
import org.jetbrains.kotlin.idea.debugger.getContextElement import org.jetbrains.kotlin.idea.debugger.getContextElement
import org.jetbrains.kotlin.idea.debugger.hopelessAware
import org.jetbrains.kotlin.idea.j2k.J2kPostProcessor import org.jetbrains.kotlin.idea.j2k.J2kPostProcessor
import org.jetbrains.kotlin.idea.j2k.convertToKotlin import org.jetbrains.kotlin.idea.j2k.convertToKotlin
import org.jetbrains.kotlin.idea.j2k.j2kText import org.jetbrains.kotlin.idea.j2k.j2kText
@@ -175,10 +176,12 @@ class KotlinCodeFragmentFactory : CodeFragmentFactory() {
val worker = object : DebuggerCommandImpl() { val worker = object : DebuggerCommandImpl() {
override fun action() { override fun action() {
try { try {
val frame = if (ApplicationManager.getApplication().isUnitTestMode) { val frame = hopelessAware {
contextElement?.getCopyableUserData(DEBUG_CONTEXT_FOR_TESTS)?.frameProxy?.stackFrame if (ApplicationManager.getApplication().isUnitTestMode) {
} else { contextElement?.getCopyableUserData(DEBUG_CONTEXT_FOR_TESTS)?.frameProxy?.stackFrame
debuggerContext.frameProxy?.stackFrame } else {
debuggerContext.frameProxy?.stackFrame
}
} }
frameInfo = FrameInfo.from(debuggerContext.project, frame) frameInfo = FrameInfo.from(debuggerContext.project, frame)
@@ -0,0 +1,35 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.debugger
import com.intellij.debugger.engine.evaluation.EvaluateException
import com.intellij.openapi.diagnostic.Logger
import com.sun.jdi.IncompatibleThreadStateException
import com.sun.jdi.VMDisconnectedException
private val LOG = Logger.getInstance("HopelessExceptionUtils")
inline fun <T : Any> hopelessAware(block: () -> T?): T? {
return try {
block()
} catch (e: Exception) {
handleHopelessException(e)
null
}
}
fun handleHopelessException(e: Exception) {
when (if (e is EvaluateException) e.cause ?: e else e) {
is IncompatibleThreadStateException, is VMDisconnectedException -> {}
else -> {
if (e is EvaluateException) {
LOG.debug("Cannot evaluate async stack trace", e)
} else {
throw e
}
}
}
}