@@ -6,7 +6,6 @@ package kotlinx.coroutines.debug.internal
6
6
7
7
import kotlinx.atomicfu.*
8
8
import kotlinx.coroutines.*
9
- import kotlinx.coroutines.internal.*
10
9
import kotlinx.coroutines.internal.ScopeCoroutine
11
10
import java.io.*
12
11
import java.lang.StackTraceElement
@@ -17,10 +16,10 @@ import kotlin.concurrent.*
17
16
import kotlin.coroutines.*
18
17
import kotlin.coroutines.jvm.internal.CoroutineStackFrame
19
18
import kotlin.synchronized
20
- import kotlinx.coroutines.internal.artificialFrame as createArtificialFrame // IDEA bug workaround
19
+ import _COROUTINE.ArtificialStackFrames
21
20
22
21
internal object DebugProbesImpl {
23
- private const val ARTIFICIAL_FRAME_MESSAGE = " Coroutine creation stacktrace "
22
+ private val ARTIFICIAL_FRAME = ArtificialStackFrames ().coroutineCreation()
24
23
private val dateFormat = SimpleDateFormat (" yyyy/MM/dd HH:mm:ss" )
25
24
26
25
private var weakRefCleanerThread: Thread ? = null
@@ -298,7 +297,7 @@ internal object DebugProbesImpl {
298
297
info.state
299
298
out .print (" \n\n Coroutine ${owner.delegate} , state: $state " )
300
299
if (observedStackTrace.isEmpty()) {
301
- out .print (" \n\t at ${createArtificialFrame( ARTIFICIAL_FRAME_MESSAGE )} " )
300
+ out .print (" \n\t at $ARTIFICIAL_FRAME " )
302
301
printStackTrace(out , info.creationStackTrace)
303
302
} else {
304
303
printStackTrace(out , enhancedStackTrace)
@@ -500,15 +499,17 @@ internal object DebugProbesImpl {
500
499
return createOwner(completion, frame)
501
500
}
502
501
503
- private fun List<StackTraceElement>.toStackTraceFrame (): StackTraceFrame ? =
504
- foldRight<StackTraceElement , StackTraceFrame ?>(null ) { frame, acc ->
505
- StackTraceFrame (acc, frame)
506
- }
502
+ private fun List<StackTraceElement>.toStackTraceFrame (): StackTraceFrame =
503
+ StackTraceFrame (
504
+ foldRight<StackTraceElement , StackTraceFrame ?>(null ) { frame, acc ->
505
+ StackTraceFrame (acc, frame)
506
+ }, ARTIFICIAL_FRAME
507
+ )
507
508
508
509
private fun <T > createOwner (completion : Continuation <T >, frame : StackTraceFrame ? ): Continuation <T > {
509
510
if (! isInstalled) return completion
510
511
val info = DebugCoroutineInfoImpl (completion.context, frame, sequenceNumber.incrementAndGet())
511
- val owner = CoroutineOwner (completion, info, frame )
512
+ val owner = CoroutineOwner (completion, info)
512
513
capturedCoroutinesMap[owner] = true
513
514
if (! isInstalled) capturedCoroutinesMap.clear()
514
515
return owner
@@ -531,9 +532,9 @@ internal object DebugProbesImpl {
531
532
*/
532
533
private class CoroutineOwner <T >(
533
534
@JvmField val delegate : Continuation <T >,
534
- @JvmField val info : DebugCoroutineInfoImpl ,
535
- private val frame : CoroutineStackFrame ?
535
+ @JvmField val info : DebugCoroutineInfoImpl
536
536
) : Continuation<T> by delegate, CoroutineStackFrame {
537
+ private val frame get() = info.creationStackBottom
537
538
538
539
override val callerFrame: CoroutineStackFrame ?
539
540
get() = frame?.callerFrame
@@ -551,12 +552,10 @@ internal object DebugProbesImpl {
551
552
private fun <T : Throwable > sanitizeStackTrace (throwable : T ): List <StackTraceElement > {
552
553
val stackTrace = throwable.stackTrace
553
554
val size = stackTrace.size
554
- val probeIndex = stackTrace.indexOfLast { it.className == " kotlin.coroutines.jvm.internal.DebugProbesKt" }
555
+ val traceStart = 1 + stackTrace.indexOfLast { it.className == " kotlin.coroutines.jvm.internal.DebugProbesKt" }
555
556
556
557
if (! sanitizeStackTraces) {
557
- return List (size - probeIndex) {
558
- if (it == 0 ) createArtificialFrame(ARTIFICIAL_FRAME_MESSAGE ) else stackTrace[it + probeIndex]
559
- }
558
+ return List (size - traceStart) { stackTrace[it + traceStart] }
560
559
}
561
560
562
561
/*
@@ -567,9 +566,8 @@ internal object DebugProbesImpl {
567
566
* If an interval of internal methods ends in a synthetic method, the outermost non-synthetic method in that
568
567
* interval will also be included.
569
568
*/
570
- val result = ArrayList <StackTraceElement >(size - probeIndex + 1 )
571
- result + = createArtificialFrame(ARTIFICIAL_FRAME_MESSAGE )
572
- var i = probeIndex + 1
569
+ val result = ArrayList <StackTraceElement >(size - traceStart + 1 )
570
+ var i = traceStart
573
571
while (i < size) {
574
572
if (stackTrace[i].isInternalMethod) {
575
573
result + = stackTrace[i] // we include the boundary of the span in any case
0 commit comments