diff --git a/java/java-runtime/src/com/intellij/rt/debugger/coroutines/CoroutinesDebugHelper.java b/java/java-runtime/src/com/intellij/rt/debugger/coroutines/CoroutinesDebugHelper.java
index 4d17db3204db..19c137725134 100644
--- a/java/java-runtime/src/com/intellij/rt/debugger/coroutines/CoroutinesDebugHelper.java
+++ b/java/java-runtime/src/com/intellij/rt/debugger/coroutines/CoroutinesDebugHelper.java
@@ -8,6 +8,7 @@ import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
+import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
@@ -276,6 +277,64 @@ public final class CoroutinesDebugHelper {
return jobToCapturedParent;
}
+ /**
+ * Returns an Object array containing coroutine job hierarchy information, or {@code null} if something went wrong
+ * or job corresponding to some coroutine does not exist.
+ *
+ *
{@code String[]} - string representations of the {@code kotlinx.coroutines.Job Job} extracted
+ * from each coroutine's context, in the same order as the input array.
+ *
{@code Object[]} - the actual {@code kotlinx.coroutines.Job Job} objects corresponding to each coroutine.
+ *
{@code int[]} - for each job, the index of its nearest ancestor job that is also present
+ * in this dump, or -1 if no such parent exists.
+ *
+ */
+ public static Object[] getCoroutineJobHierarchyInfo(Object ... debugCoroutineInfos) throws ReflectiveOperationException {
+ if (debugCoroutineInfos.length == 0) return new String[]{};
+ ClassLoader loader = debugCoroutineInfos[0].getClass().getClassLoader();
+ Class> debugCoroutineInfoClass = Class.forName(DEBUG_COROUTINE_INFO_FQN, false, loader);
+ Class> coroutineContext = Class.forName(COROUTINE_CONTEXT_FQN, false, loader);
+ Class> coroutineContextKey = Class.forName(COROUTINE_CONTEXT_KEY_FQN, false, loader);
+ Class> coroutineJobClass = Class.forName(COROUTINE_JOB_FQN, false, loader);
+ Object coroutineJobKey = coroutineJobClass.getField("Key").get(null); // Job.Key
+ Method coroutineContextGet = coroutineContext.getMethod("get", coroutineContextKey);
+ Method getParentJob = coroutineJobClass.getMethod("getParent");
+ Method getContext = debugCoroutineInfoClass.getMethod("getContext");
+
+ String[] jobNames = new String[debugCoroutineInfos.length];
+ Object[] jobObjects = new Object[debugCoroutineInfos.length];
+ int[] parentIndexes = new int[debugCoroutineInfos.length];
+ Arrays.fill(parentIndexes, -1);
+
+ for (int i = 0; i < debugCoroutineInfos.length; i++) {
+ Object info = debugCoroutineInfos[i];
+ Object context = invoke(info, getContext);
+ Object job = invoke(context, coroutineContextGet, coroutineJobKey);
+ // job corresponding to the coroutine should not be null, coroutines should be dumped again, fast return
+ if (job == null) return null;
+ jobNames[i] = job.toString();
+ jobObjects[i] = job;
+ }
+ // we use this set of coroutine jobs to only save parent jobs which correspond to some coroutine in the dump
+ Map