Skip to content

Commit

Permalink
Merge pull request #22 from Anamorphosee/dev
Browse files Browse the repository at this point in the history
release 2.3.6
  • Loading branch information
Anamorphosee committed Feb 6, 2023
2 parents 14e21b3 + aa97c4d commit c26f498
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 27 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ Thus, if the coroutine throws an exception, they mimic the real call stack of th

### JVM
There are three ways to enable Stacktrace-decoroutinator for JVM.
1. (recommended) Add dependency `dev.reformator.stacktracedecoroutinator:stacktrace-decoroutinator-jvm:2.3.5` and call method `DecoroutinatorRuntime.load()`.
2. Add `-javaagent:stacktrace-decoroutinator-jvm-agent-2.3.5.jar` to your JVM start arguments. Corresponding dependency is `dev.reformator.stacktracedecoroutinator:stacktrace-decoroutinator-jvm-agent:2.3.5`.
3. (less recommended) Add dependency `dev.reformator.stacktracedecoroutinator:stacktrace-decoroutinator-jvm-legacy:2.3.5` and call method `DecoroutinatorRuntime.load()`. This way doesn't use Java instrumentation API unlike the way number 1.
1. (recommended) Add dependency `dev.reformator.stacktracedecoroutinator:stacktrace-decoroutinator-jvm:2.3.6` and call method `DecoroutinatorRuntime.load()`.
2. Add `-javaagent:stacktrace-decoroutinator-jvm-agent-2.3.6.jar` to your JVM start arguments. Corresponding dependency is `dev.reformator.stacktracedecoroutinator:stacktrace-decoroutinator-jvm-agent:2.3.6`.
3. (less recommended) Add dependency `dev.reformator.stacktracedecoroutinator:stacktrace-decoroutinator-jvm-legacy:2.3.6` and call method `DecoroutinatorRuntime.load()`. This way doesn't use Java instrumentation API unlike the way number 1.

Usage example:
```kotlin
Expand Down Expand Up @@ -152,7 +152,7 @@ java.lang.Exception: exception at 1653565535416
```

### Android
To enable Stacktrace-decoroutinator for Android you should add dependency `dev.reformator.stacktracedecoroutinator:stacktrace-decoroutinator-android:2.3.5` in your Android application and call method `DecoroutinatorRuntime.load()` before creating any coroutines.
To enable Stacktrace-decoroutinator for Android you should add dependency `dev.reformator.stacktracedecoroutinator:stacktrace-decoroutinator-android:2.3.6` in your Android application and call method `DecoroutinatorRuntime.load()` before creating any coroutines.

It's recomended to add `DecoroutinatorRuntime.load()` call in your `Application.onCreate()` method. Example:
```kotlin
Expand Down
7 changes: 3 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ buildscript {
}
}
dependencies {
classpath("com.android.tools.build:gradle:7.0.3")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.0")
classpath("com.android.tools.build:gradle:7.0.4")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.22")
classpath("gradle.plugin.com.github.johnrengelman:shadow:7.1.2")
classpath("net.vrallev.gradle:jarjar-gradle:1.1.0")
}
}

subprojects {
group = "dev.reformator.stacktracedecoroutinator"
version = "2.3.5"
version = "2.3.6"
}
7 changes: 2 additions & 5 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
rootProject.name = "stacktrace-decoroutinator"
include(
"stacktrace-decoroutinator-common",

"stacktrace-decoroutinator-jvm-common",
"stacktrace-decoroutinator-jvm-agent-common",
"stacktrace-decoroutinator-jvm-legacy-common",
"stacktrace-decoroutinator-jvm",
"stacktrace-decoroutinator-jvm-agent",
"stacktrace-decoroutinator-jvm-legacy",

"stacktrace-decoroutinator-android",

"stacktrace-decoroutinator-noop",

"jvm-agent-tests",
"stdlib",

"jvm-agent-tests"
)
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package dev.reformator.stacktracedecoroutinator.jvmagentcommon

import dev.reformator.stacktracedecoroutinator.common.getFileClass
import dev.reformator.stacktracedecoroutinator.jvmcommon.loadResource
import org.objectweb.asm.ClassReader
import org.objectweb.asm.Opcodes
import org.objectweb.asm.Type
import org.objectweb.asm.tree.ClassNode
import java.lang.invoke.MethodHandles
import java.nio.file.FileSystems
import java.util.concurrent.ConcurrentHashMap

enum class DecoroutinatorJvmAgentDebugMetadataInfoResolveStrategy: DecoroutinatorDebugMetadataInfoResolver {
SYSTEM_RESOURCE {
override fun getDebugMetadataInfo(className: String): DebugMetadataInfo? {
val path = className.replace('.', '/') + ".class"
return ClassLoader.getSystemResourceAsStream(path)?.use { classBodyStream ->
val classBody = classBodyStream.readBytes()
return loadResource(path)?.let { classBody ->
val classReader = ClassReader(classBody)
val classNode = ClassNode(Opcodes.ASM9)
classReader.accept(classNode, ClassReader.SKIP_CODE)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package dev.reformator.stacktracedecoroutinator.jvmagentcommon

import dev.reformator.stacktracedecoroutinator.common.BASE_CONTINUATION_CLASS_NAME
import dev.reformator.stacktracedecoroutinator.jvmcommon.loadResource
import org.objectweb.asm.Type
import org.objectweb.asm.tree.ClassNode
import java.lang.instrument.Instrumentation
import kotlin.coroutines.suspendCoroutine

val BASE_CONTINUATION_INTERNAL_CLASS_NAME = BASE_CONTINUATION_CLASS_NAME.replace('.', '/')
val REGISTER_LOOKUP_METHOD_NAME = "\$decoroutinatorRegisterLookup"
const val REGISTER_LOOKUP_METHOD_NAME = "\$decoroutinatorRegisterLookup"
private val debugMetadataAnnotationClassDescriptor = Type.getDescriptor(JavaUtilsImpl.metadataAnnotationClass)

@Target(AnnotationTarget.CLASS)
Expand Down Expand Up @@ -42,9 +43,7 @@ fun addDecoroutinatorClassFileTransformers(inst: Instrumentation) {
Class.forName(BASE_CONTINUATION_CLASS_NAME)
val stubClassName = _preloadStub::class.java.name
val stubClassPath = stubClassName.replace('.', '/') + ".class"
val stubClassBody = ClassLoader.getSystemResourceAsStream(stubClassPath).use { classBodyStream ->
classBodyStream.readBytes()
}
val stubClassBody = loadResource(stubClassPath)!!
val stubClassInternalName = stubClassName.replace('.', '/')
DecoroutinatorClassFileTransformer.transform(
loader = null,
Expand All @@ -68,7 +67,7 @@ internal fun ClassNode.getDebugMetadataInfo(): DebugMetadataInfo? {
val internalClassName = (parameters["c"] as String).replace('.', '/')
val methodName = parameters["m"] as String
val fileName = (parameters["f"] as String).ifEmpty { null }
val lineNumbers = (parameters["l"] as List<Int>).toSet()
@Suppress("UNCHECKED_CAST") val lineNumbers = (parameters["l"] as List<Int>).toSet()
if (lineNumbers.isEmpty()) {
return null
}
Expand All @@ -83,9 +82,9 @@ internal fun ClassNode.getDebugMetadataInfo(): DebugMetadataInfo? {
return null
}


@Suppress("ClassName")
private class _preloadStub {
@Suppress("Unused")
suspend fun suspendFun() {
suspendCoroutine<Unit> { }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
package dev.reformator.stacktracedecoroutinator.jvmcommon

import dev.reformator.stacktracedecoroutinator.common.DecoroutinatorMarker
import org.objectweb.asm.Type

private val decoroutinatorMarkerClassDescriptor = Type.getDescriptor(DecoroutinatorMarker::class.java)
@Suppress("ClassName")
private class _jvmcommonStub

fun loadDecoroutinatorBaseContinuationClassBody(): ByteArray =
ClassLoader.getSystemResourceAsStream("decoroutinatorBaseContinuation.class").use {
loadResource("decoroutinatorBaseContinuation.class")!!

fun loadResource(name: String): ByteArray? {
val classLoader = try {
Thread.currentThread().contextClassLoader
} catch (_: Throwable) {
null
} ?: try {
_jvmcommonStub::class.java.classLoader
} catch (_: Throwable) {
null
} ?: try {
ClassLoader.getSystemClassLoader()
} catch (_: Throwable) {
null
}
val stream = if (classLoader != null) {
classLoader.getResourceAsStream(name)
} else {
ClassLoader.getSystemResourceAsStream(name)
}
return stream?.use {
it.readBytes()
}
}
2 changes: 2 additions & 0 deletions stdlib/src/main/kotlin/stdlib.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("PackageDirectoryMismatch")

package dev.reformator.stacktracedecoroutinator.stdlib

import dev.reformator.stacktracedecoroutinator.common.*
Expand Down

0 comments on commit c26f498

Please sign in to comment.