@@ -7,7 +7,6 @@ package kotlinx.coroutines.debug.internal
7
7
import kotlinx.atomicfu.*
8
8
import kotlinx.coroutines.*
9
9
import kotlinx.coroutines.debug.*
10
- import kotlinx.coroutines.internal.*
11
10
import kotlinx.coroutines.internal.ScopeCoroutine
12
11
import java.io.*
13
12
import java.lang.StackTraceElement
@@ -18,10 +17,10 @@ import kotlin.concurrent.*
18
17
import kotlin.coroutines.*
19
18
import kotlin.coroutines.jvm.internal.CoroutineStackFrame
20
19
import kotlin.synchronized
21
- import kotlinx.coroutines.internal.artificialFrame as createArtificialFrame // IDEA bug workaround
20
+ import _COROUTINE.ArtificialStackFrames
22
21
23
22
internal object DebugProbesImpl {
24
- private const val ARTIFICIAL_FRAME_MESSAGE = " Coroutine creation stacktrace "
23
+ private val ARTIFICIAL_FRAME = ArtificialStackFrames ().coroutineCreation()
25
24
private val dateFormat = SimpleDateFormat (" yyyy/MM/dd HH:mm:ss" )
26
25
27
26
private var weakRefCleanerThread: Thread ? = null
@@ -219,7 +218,7 @@ internal object DebugProbesImpl {
219
218
info.state
220
219
out .print (" \n\n Coroutine ${owner.delegate} , state: $state " )
221
220
if (observedStackTrace.isEmpty()) {
222
- out .print (" \n\t at ${createArtificialFrame( ARTIFICIAL_FRAME_MESSAGE )} " )
221
+ out .print (" \n\t at $ARTIFICIAL_FRAME " )
223
222
printStackTrace(out , info.creationStackTrace)
224
223
} else {
225
224
printStackTrace(out , enhancedStackTrace)
@@ -416,15 +415,17 @@ internal object DebugProbesImpl {
416
415
return createOwner(completion, frame)
417
416
}
418
417
419
- private fun List<StackTraceElement>.toStackTraceFrame (): StackTraceFrame ? =
420
- foldRight<StackTraceElement , StackTraceFrame ?>(null ) { frame, acc ->
421
- StackTraceFrame (acc, frame)
422
- }
418
+ private fun List<StackTraceElement>.toStackTraceFrame (): StackTraceFrame =
419
+ StackTraceFrame (
420
+ foldRight<StackTraceElement , StackTraceFrame ?>(null ) { frame, acc ->
421
+ StackTraceFrame (acc, frame)
422
+ }, ARTIFICIAL_FRAME
423
+ )
423
424
424
425
private fun <T > createOwner (completion : Continuation <T >, frame : StackTraceFrame ? ): Continuation <T > {
425
426
if (! isInstalled) return completion
426
427
val info = DebugCoroutineInfoImpl (completion.context, frame, sequenceNumber.incrementAndGet())
427
- val owner = CoroutineOwner (completion, info, frame )
428
+ val owner = CoroutineOwner (completion, info)
428
429
capturedCoroutinesMap[owner] = true
429
430
if (! isInstalled) capturedCoroutinesMap.clear()
430
431
return owner
@@ -447,9 +448,9 @@ internal object DebugProbesImpl {
447
448
*/
448
449
private class CoroutineOwner <T >(
449
450
@JvmField val delegate : Continuation <T >,
450
- @JvmField val info : DebugCoroutineInfoImpl ,
451
- private val frame : CoroutineStackFrame ?
451
+ @JvmField val info : DebugCoroutineInfoImpl
452
452
) : Continuation<T> by delegate, CoroutineStackFrame {
453
+ private val frame get() = info.creationStackBottom
453
454
454
455
override val callerFrame: CoroutineStackFrame ?
455
456
get() = frame?.callerFrame
@@ -467,12 +468,10 @@ internal object DebugProbesImpl {
467
468
private fun <T : Throwable > sanitizeStackTrace (throwable : T ): List <StackTraceElement > {
468
469
val stackTrace = throwable.stackTrace
469
470
val size = stackTrace.size
470
- val probeIndex = stackTrace.indexOfLast { it.className == " kotlin.coroutines.jvm.internal.DebugProbesKt" }
471
+ val traceStart = 1 + stackTrace.indexOfLast { it.className == " kotlin.coroutines.jvm.internal.DebugProbesKt" }
471
472
472
473
if (! sanitizeStackTraces) {
473
- return List (size - probeIndex) {
474
- if (it == 0 ) createArtificialFrame(ARTIFICIAL_FRAME_MESSAGE ) else stackTrace[it + probeIndex]
475
- }
474
+ return List (size - traceStart) { stackTrace[it + traceStart] }
476
475
}
477
476
478
477
/*
@@ -483,9 +482,8 @@ internal object DebugProbesImpl {
483
482
* If an interval of internal methods ends in a synthetic method, the outermost non-synthetic method in that
484
483
* interval will also be included.
485
484
*/
486
- val result = ArrayList <StackTraceElement >(size - probeIndex + 1 )
487
- result + = createArtificialFrame(ARTIFICIAL_FRAME_MESSAGE )
488
- var i = probeIndex + 1
485
+ val result = ArrayList <StackTraceElement >(size - traceStart + 1 )
486
+ var i = traceStart
489
487
while (i < size) {
490
488
if (stackTrace[i].isInternalMethod) {
491
489
result + = stackTrace[i] // we include the boundary of the span in any case
0 commit comments