-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support 'ExternalStaticDebugProbes' (in collaboration with the kotlin…
…-stdlib)
- Loading branch information
Showing
6 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...on-testing/jpmsTest/src/externalStaticDebugProbesTest/kotlin/ExternalStaticDebugProbes.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package kotlinx.coroutines.external | ||
|
||
import kotlinx.coroutines.debug.internal.AbstractStaticDebugProbes | ||
import kotlin.coroutines.* | ||
|
||
object ExternalStaticDebugProbes: AbstractStaticDebugProbes() { | ||
override fun <T> probeCoroutineCreated(completion: Continuation<T>): Continuation<T> { | ||
return super.probeCoroutineCreated(completion) | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...esting/jpmsTest/src/externalStaticDebugProbesTest/kotlin/ExternalStaticDebugProbesTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import org.junit.* | ||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.external.ExternalStaticDebugProbes | ||
import org.junit.Test | ||
import java.io.* | ||
|
||
class ExternalStaticDebugProbesTest { | ||
|
||
@Test | ||
fun testDumpCoroutines() { | ||
runBlocking { | ||
val baos = ByteArrayOutputStream() | ||
ExternalStaticDebugProbes.dumpCoroutines(PrintStream(baos)) | ||
// if the agent works, then dumps should contain something, | ||
// at least the fact that this test is running. | ||
val dump = baos.toString() | ||
Assert.assertTrue(dump, dump.contains("testDumpCoroutines")) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
kotlinx-coroutines-core/jvm/src/debug/internal/AbstractStaticDebugProbes.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package kotlinx.coroutines.debug.internal | ||
|
||
import kotlinx.coroutines.* | ||
import java.io.* | ||
import kotlin.coroutines.* | ||
|
||
/** | ||
* Allows to statically install 'Debug Probes' at the known location | ||
* (kotlinx.coroutines.external.ExternalStaticDebugProbes). | ||
* | ||
* **Discussion** | ||
* | ||
* There are three methods of installing/engaging coroutines 'Debug Probes' | ||
* | ||
* 1) Dynamic Attach (using the 'kotlinx-coroutines-debug' module) | ||
* This uses runtime byte-code alteration to replace the 'Debug Probes' straight from the kotlin-stdlib | ||
* | ||
* 2) Static Attach using an Agent | ||
* This uses a java agent to replace the 'Debug Probes' from the kotlin-stdlib statically | ||
* | ||
* 3) ExternalStaticDebugProbes | ||
* The kotlin-stdlib compiled against a class at | ||
* `kotlinx.coroutines.external.ExternalStaticDebugProbes` which is not available at runtime, by default. | ||
* If a class at this location is present, then the kotlin-stdlib will call into it. | ||
* | ||
* ```kotlin | ||
* package kotlinx.coroutines.external | ||
* object ExternalStaticDebugProbes: AbstractStaticDebugProbes() { | ||
* override fun <T> probeCoroutineCreated(completion: Continuation<T>): Continuation<T> { | ||
* // intercept | ||
* // ... | ||
* | ||
* // forward to debugger machinery | ||
* return super.probeCoroutineCreated(completion) | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
@Suppress("unused") | ||
@DelicateCoroutinesApi | ||
@ExperimentalCoroutinesApi | ||
abstract class AbstractStaticDebugProbes { | ||
init { | ||
require(javaClass.name == "kotlinx.coroutines.external.ExternalStaticDebugProbes") | ||
AgentInstallationType.isInstalledStatically = true | ||
DebugProbesImpl.install() | ||
} | ||
|
||
open fun probeCoroutineResumed(frame: Continuation<*>) = DebugProbesImpl.probeCoroutineResumed(frame) | ||
|
||
open fun probeCoroutineSuspended(frame: Continuation<*>) = DebugProbesImpl.probeCoroutineSuspended(frame) | ||
|
||
open fun <T> probeCoroutineCreated(completion: Continuation<T>): Continuation<T> = | ||
DebugProbesImpl.probeCoroutineCreated(completion) | ||
|
||
fun dumpCoroutines(out: PrintStream) { | ||
DebugProbesImpl.dumpCoroutines(out) | ||
} | ||
} |