Skip to content

Add method that allows IDEA debugger to retrieve enhanced stack trace in a JSON format #2933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions kotlinx-coroutines-core/jvm/src/debug/internal/DebugProbesImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ internal object DebugProbesImpl {
*/
@OptIn(ExperimentalStdlibApi::class)
public fun dumpCoroutinesInfoAsJsonAndReferences(): Array<Any> {
fun Any.toStringWithQuotes() = "\"$this\""
val coroutinesInfo = dumpCoroutinesInfo()
val size = coroutinesInfo.size
val lastObservedThreads = ArrayList<Thread?>(size)
Expand All @@ -196,11 +195,11 @@ internal object DebugProbesImpl {
coroutinesInfoAsJson.add(
"""
{
"name": $name,
"id": ${context[CoroutineId.Key]?.id},
"dispatcher": $dispatcher,
"sequenceNumber": ${info.sequenceNumber},
"state": "${info.state}"
"name": $name,
"id": ${context[CoroutineId.Key]?.id},
"dispatcher": $dispatcher,
"sequenceNumber": ${info.sequenceNumber},
"state": "${info.state}"
}
""".trimIndent()
)
Expand All @@ -216,6 +215,30 @@ internal object DebugProbesImpl {
)
}

/*
* Internal (JVM-public) method used by IDEA debugger as of 1.6.0-RC.
*/
public fun enhanceStackTraceWithThreadDumpAsJson(info: DebugCoroutineInfo): String {
val stackTraceElements = enhanceStackTraceWithThreadDump(info, info.lastObservedStackTrace)
val stackTraceElementsInfoAsJson = mutableListOf<String>()
for (element in stackTraceElements) {
stackTraceElementsInfoAsJson.add(
"""
{
"declaringClass": "${element.className}",
"methodName": "${element.methodName}",
"fileName": ${element.fileName?.toStringWithQuotes()},
"lineNumber": ${element.lineNumber}
}
""".trimIndent()
)
}

return "[${stackTraceElementsInfoAsJson.joinToString()}]"
}

private fun Any.toStringWithQuotes() = "\"$this\""

/*
* Internal (JVM-public) method used by IDEA debugger as of 1.4-M3.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
package kotlinx.coroutines.debug

import com.google.gson.*
import kotlinx.coroutines.*
import kotlinx.coroutines.debug.internal.*
import org.junit.Test
import kotlin.test.*

class EnhanceStackTraceWithTreadDumpAsJsonTest : DebugTestBase() {
private data class StackTraceElementInfoFromJson(
val declaringClass: String,
val methodName: String,
val fileName: String?,
val lineNumber: Int
)

@Test
fun testEnhancedStackTraceFormatWithDeferred() = runTest {
val deferred = async {
suspendingMethod()
assertTrue(true)
}
yield()

val coroutineInfo = DebugProbesImpl.dumpCoroutinesInfo()
assertEquals(coroutineInfo.size, 2)
val info = coroutineInfo[1]
val enhancedStackTraceAsJsonString = DebugProbesImpl.enhanceStackTraceWithThreadDumpAsJson(info)
val enhancedStackTraceFromJson = Gson().fromJson(enhancedStackTraceAsJsonString, Array<StackTraceElementInfoFromJson>::class.java)
val enhancedStackTrace = DebugProbesImpl.enhanceStackTraceWithThreadDump(info, info.lastObservedStackTrace)
assertEquals(enhancedStackTrace.size, enhancedStackTraceFromJson.size)
for ((frame, frameFromJson) in enhancedStackTrace.zip(enhancedStackTraceFromJson)) {
assertEquals(frame.className, frameFromJson.declaringClass)
assertEquals(frame.methodName, frameFromJson.methodName)
assertEquals(frame.fileName, frameFromJson.fileName)
assertEquals(frame.lineNumber, frameFromJson.lineNumber)
}

deferred.cancelAndJoin()
}

private suspend fun suspendingMethod() {
delay(Long.MAX_VALUE)
}
}