Vsevolod Tolstopyatov | 418ba80 | 2019-02-01 14:54:06 +0300 | [diff] [blame] | 1 | **Table of contents** |
| 2 | |
| 3 | <!--- TOC --> |
| 4 | |
| 5 | * [Debugging coroutines](#debugging-coroutines) |
| 6 | * [Debug mode](#debug-mode) |
| 7 | * [Stacktrace recovery](#stacktrace-recovery) |
| 8 | * [Stacktrace recovery machinery](#stacktrace-recovery-machinery) |
| 9 | * [Debug agent](#debug-agent) |
| 10 | |
| 11 | <!--- END_TOC --> |
| 12 | |
| 13 | |
| 14 | ## Debugging coroutines |
| 15 | Asynchronous programming is hard and debugging asynchronous programs is even harder. |
| 16 | To improve user experience, `kotlinx.coroutines` comes with additional features for debugging: debug mode, stacktrace recovery |
| 17 | and debug agent. |
| 18 | |
| 19 | ## Debug mode |
Vsevolod Tolstopyatov | a68376c | 2019-02-01 18:13:39 +0300 | [diff] [blame^] | 20 | |
Vsevolod Tolstopyatov | 418ba80 | 2019-02-01 14:54:06 +0300 | [diff] [blame] | 21 | The first debugging feature of `kotlinx.coroutines` is debug mode. |
| 22 | It can be enabled either by setting system property [DEBUG_PROPERTY_NAME] or by running Java with enabled assertions (`-ea` flag). |
| 23 | The latter is helpful to have debug mode enabled by default in unit tests. |
| 24 | |
| 25 | Debug mode attaches a unique [name][CoroutineName] to every launched coroutine, which then can be seen in a regular Java debugger, |
| 26 | a string representation of coroutine and thread name executing named coroutine. |
| 27 | Overhead of this feature is negligible and it can be safely turned on by default to simplify logging and diagnostic. |
| 28 | |
| 29 | ## Stacktrace recovery |
Vsevolod Tolstopyatov | a68376c | 2019-02-01 18:13:39 +0300 | [diff] [blame^] | 30 | |
Vsevolod Tolstopyatov | 418ba80 | 2019-02-01 14:54:06 +0300 | [diff] [blame] | 31 | Stacktrace recovery is another useful feature of debug mode. It is enabled by default in the debug mode, |
| 32 | but can be separately disabled by setting `kotlinx.coroutines.stacktrace.recovery` system property to `false`. |
| 33 | |
| 34 | Stacktrace recovery tries to knit asynchronous exception stacktrace with a stacktrace of the receiver by copying it, providing |
| 35 | not only information where an exception was thrown, but also where it was asynchronously rethrown or caught . |
| 36 | |
| 37 | It is easy to demonstrate with actual stacktraces of the same program that awaits asynchronous operation in `main` function: |
| 38 | |
| 39 | | Without recovery | With recovery | |
| 40 | | - | - | |
| 41 | |  |  | |
| 42 | |
| 43 | The only downside of this approach is losing referential transparency of the exception. |
| 44 | |
| 45 | ### Stacktrace recovery machinery |
Vsevolod Tolstopyatov | a68376c | 2019-02-01 18:13:39 +0300 | [diff] [blame^] | 46 | |
Vsevolod Tolstopyatov | 418ba80 | 2019-02-01 14:54:06 +0300 | [diff] [blame] | 47 | This section explains the inner mechanism of stacktrace recovery and can be skipped. |
| 48 | |
| 49 | When an exception is rethrown between coroutines (e.g. through `withContext` or `Deferred.await` boundary), stacktrace recovery |
| 50 | machinery tries to create a copy of the original exception (with the original exception as the cause), then rewrite stacktrace |
| 51 | of the copy with coroutine-related stack frames (using [Throwable.setStackTrace](https://docs.oracle.com/javase/9/docs/api/java/lang/Throwable.html#setStackTrace-java.lang.StackTraceElement:A-)) |
| 52 | and then throws resulting exception instead of the original one. |
| 53 | |
| 54 | Exception copy logic is straightforward: |
| 55 | 1) If exception class implements [CopyableThrowable], [CopyableThrowable.createCopy] is used. |
| 56 | 2) If exception class has class-specific fields not inherited from Throwable, the exception is not copied. |
| 57 | 3) Otherwise, one of the public exception's constructor is invoked reflectively with optional an `initCause` call. |
| 58 | |
| 59 | ## Debug agent |
Vsevolod Tolstopyatov | a68376c | 2019-02-01 18:13:39 +0300 | [diff] [blame^] | 60 | |
Vsevolod Tolstopyatov | 418ba80 | 2019-02-01 14:54:06 +0300 | [diff] [blame] | 61 | [kotlinx-coroutines-debug](../kotlinx-coroutines-debug) module provides one of the most powerful debug capabilities in `kotlinx.coroutines`. |
| 62 | |
| 63 | This is a separate module with a JVM agent that keeps track of all alive coroutines, introspect and dump them similar to thread dump command, |
| 64 | additionally enhancing stacktraces with information where coroutine was created. |
| 65 | |
| 66 | The full tutorial of how to use debug agent can be found in a corresponding [readme](../kotlinx-coroutines-debug/README.md). |
| 67 | |
Vsevolod Tolstopyatov | a68376c | 2019-02-01 18:13:39 +0300 | [diff] [blame^] | 68 | ### Debug agent and Android |
Vsevolod Tolstopyatov | 418ba80 | 2019-02-01 14:54:06 +0300 | [diff] [blame] | 69 | |
Vsevolod Tolstopyatov | a68376c | 2019-02-01 18:13:39 +0300 | [diff] [blame^] | 70 | Unfortunately, Android runtime does not support Instrument API necessary for `kotlinx-coroutines-debug` to function, triggering `java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/management/ManagementFactory;`. |
| 71 | |
| 72 | Nevertheless, it will be possible to support debug agent on Android as soon as [GradleAspectJ-Android](https://github.com/Archinamon/android-gradle-aspectj) will support androin-gradle 3.3 |
| 73 | |
| 74 | <!--- |
| 75 | Make an exception googlable |
| 76 | java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/management/ManagementFactory; |
| 77 | at kotlinx.coroutines.repackaged.net.bytebuddy.agent.ByteBuddyAgent$ProcessProvider$ForCurrentVm$ForLegacyVm.resolve(ByteBuddyAgent.java:1055) |
| 78 | at kotlinx.coroutines.repackaged.net.bytebuddy.agent.ByteBuddyAgent$ProcessProvider$ForCurrentVm.resolve(ByteBuddyAgent.java:1038) |
| 79 | at kotlinx.coroutines.repackaged.net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:374) |
| 80 | at kotlinx.coroutines.repackaged.net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:342) |
| 81 | at kotlinx.coroutines.repackaged.net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:328) |
| 82 | at kotlinx.coroutines.debug.internal.DebugProbesImpl.install(DebugProbesImpl.kt:39) |
| 83 | at kotlinx.coroutines.debug.DebugProbes.install(DebugProbes.kt:49) |
| 84 | --> |
Vsevolod Tolstopyatov | 418ba80 | 2019-02-01 14:54:06 +0300 | [diff] [blame] | 85 | |
| 86 | <!--- MODULE kotlinx-coroutines-core --> |
| 87 | <!--- INDEX kotlinx.coroutines --> |
| 88 | [DEBUG_PROPERTY_NAME]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-d-e-b-u-g_-p-r-o-p-e-r-t-y_-n-a-m-e.html |
| 89 | [CoroutineName]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-name/index.html |
| 90 | [CopyableThrowable]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-copyable-throwable/index.html |
| 91 | [CopyableThrowable.createCopy]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-copyable-throwable/create-copy.html |
| 92 | <!--- MODULE kotlinx-coroutines-debug --> |
| 93 | <!--- END --> |