IntelliJ IDEA 2020.2 Help

Debug Kotlin coroutines

While coroutines are particularly well suited for asynchronous programming, there is still room for mistakes, which may be hard to figure out due to the challenges that the asynchronous flow poses.

When debugging Kotlin code, IntelliJ IDEA allows you to suspend the execution and diagnose problems that the code in coroutines may have. The debug information is available even if the coroutine is not running at the moment.

Coroutine debugger provides you with the information on:

  • The list of coroutines and their states grouped by dispatcher. To get the list, go to the Coroutines tab. The top-level nodes are dispatchers, then go coroutines. For each coroutine, you get information on its current state (RUNNING, SUSPENDED) and the state of its thread.

    kotlin debug coroutines list png
  • Coroutine context: the values of local variables and fields available in a coroutine at a certain execution point. When debugging coroutines, you can use all the standard functionality of the Variables tab. For more information on using the Variables tab, see the Examine/update variables topic.

    kotlin debug coroutines context png
  • The coroutine creation stack and the call stack inside the coroutine.

    kotlin debug coroutines frames png

    If you are not interested in calls in Kotlin classes, you can hide them by clicking Hide Frames from Libraries in the top-right corner of the Frames tab.

    kotlin debug coroutines frames f png

Get coroutines dump

If you need to get a report containing the state of each coroutine and its stack, use the coroutine dump option. This is useful when you need to share the information about the coroutines in text format.

  1. Right-click anywhere in the Coroutines tab and select Get Coroutines Dump.

    kotlin debug coroutines dump1 png
  2. A tab with the dump opens where you can choose what to export:

    • If you want to copy the information related to a specific coroutine, select it and click Copy.

      kotlin debug coroutines dump2 png
    • To export the dump of all coroutines, click Export to Text File, then click Copy (to copy the dump to the clipboard), or specify the file and click Save.

      kotlin debug coroutines dump3 png
Last modified: 30 June 2020