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) |
Vsevolod Tolstopyatov | 4764e98 | 2019-02-04 12:39:34 +0300 | [diff] [blame] | 10 | * [Debug agent and Android](#debug-agent-and-android) |
Roman Elizarov | bf9509d | 2020-02-14 15:52:10 +0300 | [diff] [blame] | 11 | * [Android optimization](#android-optimization) |
Vsevolod Tolstopyatov | 418ba80 | 2019-02-01 14:54:06 +0300 | [diff] [blame] | 12 | |
Roman Elizarov | 660c2d7 | 2020-02-14 13:18:37 +0300 | [diff] [blame] | 13 | <!--- END --> |
Vsevolod Tolstopyatov | 418ba80 | 2019-02-01 14:54:06 +0300 | [diff] [blame] | 14 | |
| 15 | ## Debugging coroutines |
Roman Elizarov | bf9509d | 2020-02-14 15:52:10 +0300 | [diff] [blame] | 16 | |
Vsevolod Tolstopyatov | f528898 | 2019-02-18 14:46:58 +0300 | [diff] [blame] | 17 | Debugging asynchronous programs is challenging, because multiple concurrent coroutines are typically working at the same time. |
| 18 | To help with that, `kotlinx.coroutines` comes with additional features for debugging: debug mode, stacktrace recovery |
Vsevolod Tolstopyatov | 418ba80 | 2019-02-01 14:54:06 +0300 | [diff] [blame] | 19 | and debug agent. |
| 20 | |
| 21 | ## Debug mode |
Vsevolod Tolstopyatov | a68376c | 2019-02-01 18:13:39 +0300 | [diff] [blame] | 22 | |
Vsevolod Tolstopyatov | 418ba80 | 2019-02-01 14:54:06 +0300 | [diff] [blame] | 23 | The first debugging feature of `kotlinx.coroutines` is debug mode. |
| 24 | It can be enabled either by setting system property [DEBUG_PROPERTY_NAME] or by running Java with enabled assertions (`-ea` flag). |
| 25 | The latter is helpful to have debug mode enabled by default in unit tests. |
| 26 | |
Vsevolod Tolstopyatov | f528898 | 2019-02-18 14:46:58 +0300 | [diff] [blame] | 27 | Debug mode attaches a unique [name][CoroutineName] to every launched coroutine. |
| 28 | Coroutine name can be seen in a regular Java debugger, |
| 29 | in a string representation of the coroutine or in the thread name executing named coroutine. |
Vsevolod Tolstopyatov | 418ba80 | 2019-02-01 14:54:06 +0300 | [diff] [blame] | 30 | Overhead of this feature is negligible and it can be safely turned on by default to simplify logging and diagnostic. |
| 31 | |
| 32 | ## Stacktrace recovery |
Vsevolod Tolstopyatov | a68376c | 2019-02-01 18:13:39 +0300 | [diff] [blame] | 33 | |
Vsevolod Tolstopyatov | 418ba80 | 2019-02-01 14:54:06 +0300 | [diff] [blame] | 34 | Stacktrace recovery is another useful feature of debug mode. It is enabled by default in the debug mode, |
| 35 | but can be separately disabled by setting `kotlinx.coroutines.stacktrace.recovery` system property to `false`. |
| 36 | |
Vsevolod Tolstopyatov | f528898 | 2019-02-18 14:46:58 +0300 | [diff] [blame] | 37 | Stacktrace recovery tries to stitch asynchronous exception stacktrace with a stacktrace of the receiver by copying it, providing |
Vsevolod Tolstopyatov | 4764e98 | 2019-02-04 12:39:34 +0300 | [diff] [blame] | 38 | not only information where an exception was thrown, but also where it was asynchronously rethrown or caught. |
Vsevolod Tolstopyatov | 418ba80 | 2019-02-01 14:54:06 +0300 | [diff] [blame] | 39 | |
Vsevolod Tolstopyatov | f528898 | 2019-02-18 14:46:58 +0300 | [diff] [blame] | 40 | It is easy to demonstrate with actual stacktraces of the same program that awaits asynchronous operation in `main` function |
| 41 | (runnable code is [here](../kotlinx-coroutines-debug/test/RecoveryExample.kt)): |
Vsevolod Tolstopyatov | 418ba80 | 2019-02-01 14:54:06 +0300 | [diff] [blame] | 42 | |
| 43 | | Without recovery | With recovery | |
| 44 | | - | - | |
| 45 | |  |  | |
| 46 | |
| 47 | The only downside of this approach is losing referential transparency of the exception. |
| 48 | |
Vsevolod Tolstopyatov | 897f02e | 2019-08-09 17:32:34 +0300 | [diff] [blame] | 49 | ### Stacktrace recovery machinery |
Vsevolod Tolstopyatov | a68376c | 2019-02-01 18:13:39 +0300 | [diff] [blame] | 50 | |
Vsevolod Tolstopyatov | 418ba80 | 2019-02-01 14:54:06 +0300 | [diff] [blame] | 51 | This section explains the inner mechanism of stacktrace recovery and can be skipped. |
| 52 | |
| 53 | When an exception is rethrown between coroutines (e.g. through `withContext` or `Deferred.await` boundary), stacktrace recovery |
| 54 | machinery tries to create a copy of the original exception (with the original exception as the cause), then rewrite stacktrace |
| 55 | 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-)) |
Vsevolod Tolstopyatov | f528898 | 2019-02-18 14:46:58 +0300 | [diff] [blame] | 56 | and then throws the resulting exception instead of the original one. |
Vsevolod Tolstopyatov | 418ba80 | 2019-02-01 14:54:06 +0300 | [diff] [blame] | 57 | |
| 58 | Exception copy logic is straightforward: |
Vsevolod Tolstopyatov | f528898 | 2019-02-18 14:46:58 +0300 | [diff] [blame] | 59 | 1) If the exception class implements [CopyableThrowable], [CopyableThrowable.createCopy] is used. |
Vsevolod Tolstopyatov | 897f02e | 2019-08-09 17:32:34 +0300 | [diff] [blame] | 60 | `null` can be returned from `createCopy` to opt-out specific exception from being recovered. |
Vsevolod Tolstopyatov | f528898 | 2019-02-18 14:46:58 +0300 | [diff] [blame] | 61 | 2) If the exception class has class-specific fields not inherited from Throwable, the exception is not copied. |
| 62 | 3) Otherwise, one of the public exception's constructor is invoked reflectively with an optional `initCause` call. |
Vsevolod Tolstopyatov | 418ba80 | 2019-02-01 14:54:06 +0300 | [diff] [blame] | 63 | |
| 64 | ## Debug agent |
Vsevolod Tolstopyatov | a68376c | 2019-02-01 18:13:39 +0300 | [diff] [blame] | 65 | |
Vsevolod Tolstopyatov | 418ba80 | 2019-02-01 14:54:06 +0300 | [diff] [blame] | 66 | [kotlinx-coroutines-debug](../kotlinx-coroutines-debug) module provides one of the most powerful debug capabilities in `kotlinx.coroutines`. |
| 67 | |
Vsevolod Tolstopyatov | f528898 | 2019-02-18 14:46:58 +0300 | [diff] [blame] | 68 | This is a separate module with a JVM agent that keeps track of all alive coroutines, introspects and dumps them similar to thread dump command, |
Vsevolod Tolstopyatov | 418ba80 | 2019-02-01 14:54:06 +0300 | [diff] [blame] | 69 | additionally enhancing stacktraces with information where coroutine was created. |
| 70 | |
Vsevolod Tolstopyatov | f528898 | 2019-02-18 14:46:58 +0300 | [diff] [blame] | 71 | The full tutorial of how to use debug agent can be found in the corresponding [readme](../kotlinx-coroutines-debug/README.md). |
Vsevolod Tolstopyatov | 418ba80 | 2019-02-01 14:54:06 +0300 | [diff] [blame] | 72 | |
Vsevolod Tolstopyatov | a68376c | 2019-02-01 18:13:39 +0300 | [diff] [blame] | 73 | ### Debug agent and Android |
Vsevolod Tolstopyatov | 418ba80 | 2019-02-01 14:54:06 +0300 | [diff] [blame] | 74 | |
Vsevolod Tolstopyatov | a68376c | 2019-02-01 18:13:39 +0300 | [diff] [blame] | 75 | 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;`. |
| 76 | |
Mike Nakhimovich | da2cf23 | 2019-05-22 09:04:22 -0400 | [diff] [blame] | 77 | 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 android-gradle 3.3 |
Vsevolod Tolstopyatov | a68376c | 2019-02-01 18:13:39 +0300 | [diff] [blame] | 78 | |
| 79 | <!--- |
| 80 | Make an exception googlable |
| 81 | java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/management/ManagementFactory; |
| 82 | at kotlinx.coroutines.repackaged.net.bytebuddy.agent.ByteBuddyAgent$ProcessProvider$ForCurrentVm$ForLegacyVm.resolve(ByteBuddyAgent.java:1055) |
| 83 | at kotlinx.coroutines.repackaged.net.bytebuddy.agent.ByteBuddyAgent$ProcessProvider$ForCurrentVm.resolve(ByteBuddyAgent.java:1038) |
| 84 | at kotlinx.coroutines.repackaged.net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:374) |
| 85 | at kotlinx.coroutines.repackaged.net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:342) |
| 86 | at kotlinx.coroutines.repackaged.net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:328) |
| 87 | at kotlinx.coroutines.debug.internal.DebugProbesImpl.install(DebugProbesImpl.kt:39) |
| 88 | at kotlinx.coroutines.debug.DebugProbes.install(DebugProbes.kt:49) |
| 89 | --> |
Vsevolod Tolstopyatov | 418ba80 | 2019-02-01 14:54:06 +0300 | [diff] [blame] | 90 | |
Roman Elizarov | bf9509d | 2020-02-14 15:52:10 +0300 | [diff] [blame] | 91 | ## Android optimization |
| 92 | |
| 93 | In optimized (release) builds with R8 version 1.6.0 or later both |
| 94 | [Debugging mode](../../docs/debugging.md#debug-mode) and |
| 95 | [Stacktrace recovery](../../docs/debugging.md#stacktrace-recovery) |
| 96 | are permanently turned off. |
| 97 | For more details see ["Optimization" section for Android](../ui/kotlinx-coroutines-android/README.md#optimization). |
| 98 | |
Vsevolod Tolstopyatov | 418ba80 | 2019-02-01 14:54:06 +0300 | [diff] [blame] | 99 | <!--- MODULE kotlinx-coroutines-core --> |
| 100 | <!--- INDEX kotlinx.coroutines --> |
| 101 | [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 |
| 102 | [CoroutineName]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-name/index.html |
| 103 | [CopyableThrowable]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-copyable-throwable/index.html |
| 104 | [CopyableThrowable.createCopy]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-copyable-throwable/create-copy.html |
| 105 | <!--- MODULE kotlinx-coroutines-debug --> |
| 106 | <!--- END --> |