--- old/src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.graph.test/src/org/graalvm/compiler/graph/test/GraphTest.java 2017-07-07 09:29:55.000000000 -0700 +++ new/src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.graph.test/src/org/graalvm/compiler/graph/test/GraphTest.java 2017-07-07 09:29:55.000000000 -0700 @@ -23,11 +23,41 @@ package org.graalvm.compiler.graph.test; import org.graalvm.compiler.api.test.Graal; +import org.graalvm.compiler.debug.DebugHandlersFactory; +import org.graalvm.compiler.debug.DebugContext; import org.graalvm.compiler.options.OptionValues; +import org.junit.After; public abstract class GraphTest { static OptionValues getOptions() { return Graal.getRequiredCapability(OptionValues.class); } + + private final ThreadLocal cachedDebug = new ThreadLocal<>(); + + protected DebugContext getDebug(OptionValues options) { + DebugContext cached = cachedDebug.get(); + if (cached != null) { + if (cached.getOptions() == options) { + return cached; + } + throw new AssertionError("At most one " + DebugContext.class.getName() + " object should be created per test"); + } + DebugContext debug = DebugContext.create(options, DebugHandlersFactory.LOADER); + cachedDebug.set(debug); + return debug; + } + + protected DebugContext getDebug() { + return getDebug(getOptions()); + } + + @After + public void afterTest() { + DebugContext cached = cachedDebug.get(); + if (cached != null) { + cached.closeDumpHandlers(true); + } + } }