15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package org.graalvm.compiler.debug;
24
25 import static java.util.FormattableFlags.LEFT_JUSTIFY;
26 import static java.util.FormattableFlags.UPPERCASE;
27 import static org.graalvm.compiler.debug.DebugOptions.Count;
28 import static org.graalvm.compiler.debug.DebugOptions.Counters;
29 import static org.graalvm.compiler.debug.DebugOptions.Dump;
30 import static org.graalvm.compiler.debug.DebugOptions.DumpOnError;
31 import static org.graalvm.compiler.debug.DebugOptions.DumpOnPhaseChange;
32 import static org.graalvm.compiler.debug.DebugOptions.ListMetrics;
33 import static org.graalvm.compiler.debug.DebugOptions.Log;
34 import static org.graalvm.compiler.debug.DebugOptions.MemUseTrackers;
35 import static org.graalvm.compiler.debug.DebugOptions.MethodFilter;
36 import static org.graalvm.compiler.debug.DebugOptions.Time;
37 import static org.graalvm.compiler.debug.DebugOptions.Timers;
38 import static org.graalvm.compiler.debug.DebugOptions.TrackMemUse;
39 import static org.graalvm.compiler.debug.DebugOptions.Verify;
40
41 import java.io.ByteArrayOutputStream;
42 import java.io.File;
43 import java.io.IOException;
44 import java.io.PrintStream;
45 import java.nio.file.Files;
46 import java.nio.file.Path;
47 import java.nio.file.Paths;
48 import java.nio.file.StandardOpenOption;
49 import java.util.ArrayList;
50 import java.util.Arrays;
51 import java.util.Collection;
52 import java.util.Collections;
53 import java.util.Formatter;
54 import java.util.List;
55 import java.util.Map;
56 import java.util.SortedMap;
57 import java.util.TreeMap;
58
59 import org.graalvm.compiler.options.OptionKey;
357 }
358
359 private DebugContext(Description description, GlobalMetrics globalMetrics, PrintStream logStream, Immutable immutable, Iterable<DebugHandlersFactory> factories) {
360 this.immutable = immutable;
361 this.description = description;
362 this.globalMetrics = globalMetrics;
363 if (immutable.scopesEnabled) {
364 OptionValues options = immutable.options;
365 List<DebugDumpHandler> dumpHandlers = new ArrayList<>();
366 List<DebugVerifyHandler> verifyHandlers = new ArrayList<>();
367 for (DebugHandlersFactory factory : factories) {
368 for (DebugHandler handler : factory.createHandlers(options)) {
369 if (handler instanceof DebugDumpHandler) {
370 dumpHandlers.add((DebugDumpHandler) handler);
371 } else {
372 assert handler instanceof DebugVerifyHandler;
373 verifyHandlers.add((DebugVerifyHandler) handler);
374 }
375 }
376 }
377 currentConfig = new DebugConfigImpl(
378 options,
379 Log.getValue(options),
380 Count.getValue(options),
381 TrackMemUse.getValue(options),
382 Time.getValue(options),
383 Dump.getValue(options),
384 Verify.getValue(options),
385 MethodFilter.getValue(options),
386 logStream, dumpHandlers, verifyHandlers);
387 currentScope = new ScopeImpl(this, Thread.currentThread());
388 currentScope.updateFlags(currentConfig);
389 metricsEnabled = true;
390 } else {
391 metricsEnabled = immutable.hasUnscopedMetrics() || immutable.listMetrics;
392 }
393 }
394
395 /**
396 * A special dump level that indicates the dumping machinery is enabled but no dumps will be
397 * produced except through other options.
398 */
399 public static final int ENABLED_LEVEL = 0;
400
401 /**
402 * Basic debug level.
403 *
404 * For HIR dumping, only ~5 graphs per method: after parsing, after inlining, after high tier,
405 * after mid tier, after low tier.
406 *
558 return enterScope(convertFormatArg(name).toString(), null, contextObjects);
559 } else {
560 return null;
561 }
562 }
563
564 /**
565 * Similar to {@link #scope(Object, Object[])} but without context objects. Therefore the catch
566 * block can be omitted.
567 *
568 * @see #scope(Object, Object[])
569 */
570 public DebugContext.Scope scope(Object name) {
571 if (currentScope != null) {
572 return enterScope(convertFormatArg(name).toString(), null);
573 } else {
574 return null;
575 }
576 }
577
578 private final Invariants invariants = Assertions.ENABLED ? new Invariants() : null;
579
580 static StackTraceElement[] getStackTrace(Thread thread) {
581 return thread.getStackTrace();
582 }
583
584 /**
585 * Utility for enforcing {@link DebugContext} invariants via assertions.
586 */
587 static class Invariants {
588 private final Thread thread;
589 private final StackTraceElement[] origin;
590
591 Invariants() {
592 thread = Thread.currentThread();
593 origin = getStackTrace(thread);
594 }
595
596 boolean checkNoConcurrentAccess() {
597 Thread currentThread = Thread.currentThread();
598 if (currentThread != thread) {
|
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package org.graalvm.compiler.debug;
24
25 import static java.util.FormattableFlags.LEFT_JUSTIFY;
26 import static java.util.FormattableFlags.UPPERCASE;
27 import static org.graalvm.compiler.debug.DebugOptions.Count;
28 import static org.graalvm.compiler.debug.DebugOptions.Counters;
29 import static org.graalvm.compiler.debug.DebugOptions.Dump;
30 import static org.graalvm.compiler.debug.DebugOptions.DumpOnError;
31 import static org.graalvm.compiler.debug.DebugOptions.DumpOnPhaseChange;
32 import static org.graalvm.compiler.debug.DebugOptions.ListMetrics;
33 import static org.graalvm.compiler.debug.DebugOptions.Log;
34 import static org.graalvm.compiler.debug.DebugOptions.MemUseTrackers;
35 import static org.graalvm.compiler.debug.DebugOptions.Time;
36 import static org.graalvm.compiler.debug.DebugOptions.Timers;
37 import static org.graalvm.compiler.debug.DebugOptions.TrackMemUse;
38
39 import java.io.ByteArrayOutputStream;
40 import java.io.File;
41 import java.io.IOException;
42 import java.io.PrintStream;
43 import java.nio.file.Files;
44 import java.nio.file.Path;
45 import java.nio.file.Paths;
46 import java.nio.file.StandardOpenOption;
47 import java.util.ArrayList;
48 import java.util.Arrays;
49 import java.util.Collection;
50 import java.util.Collections;
51 import java.util.Formatter;
52 import java.util.List;
53 import java.util.Map;
54 import java.util.SortedMap;
55 import java.util.TreeMap;
56
57 import org.graalvm.compiler.options.OptionKey;
355 }
356
357 private DebugContext(Description description, GlobalMetrics globalMetrics, PrintStream logStream, Immutable immutable, Iterable<DebugHandlersFactory> factories) {
358 this.immutable = immutable;
359 this.description = description;
360 this.globalMetrics = globalMetrics;
361 if (immutable.scopesEnabled) {
362 OptionValues options = immutable.options;
363 List<DebugDumpHandler> dumpHandlers = new ArrayList<>();
364 List<DebugVerifyHandler> verifyHandlers = new ArrayList<>();
365 for (DebugHandlersFactory factory : factories) {
366 for (DebugHandler handler : factory.createHandlers(options)) {
367 if (handler instanceof DebugDumpHandler) {
368 dumpHandlers.add((DebugDumpHandler) handler);
369 } else {
370 assert handler instanceof DebugVerifyHandler;
371 verifyHandlers.add((DebugVerifyHandler) handler);
372 }
373 }
374 }
375 currentConfig = new DebugConfigImpl(options, logStream, dumpHandlers, verifyHandlers);
376 currentScope = new ScopeImpl(this, Thread.currentThread());
377 currentScope.updateFlags(currentConfig);
378 metricsEnabled = true;
379 } else {
380 metricsEnabled = immutable.hasUnscopedMetrics() || immutable.listMetrics;
381 }
382 }
383
384 /**
385 * A special dump level that indicates the dumping machinery is enabled but no dumps will be
386 * produced except through other options.
387 */
388 public static final int ENABLED_LEVEL = 0;
389
390 /**
391 * Basic debug level.
392 *
393 * For HIR dumping, only ~5 graphs per method: after parsing, after inlining, after high tier,
394 * after mid tier, after low tier.
395 *
547 return enterScope(convertFormatArg(name).toString(), null, contextObjects);
548 } else {
549 return null;
550 }
551 }
552
553 /**
554 * Similar to {@link #scope(Object, Object[])} but without context objects. Therefore the catch
555 * block can be omitted.
556 *
557 * @see #scope(Object, Object[])
558 */
559 public DebugContext.Scope scope(Object name) {
560 if (currentScope != null) {
561 return enterScope(convertFormatArg(name).toString(), null);
562 } else {
563 return null;
564 }
565 }
566
567 private final Invariants invariants = Assertions.assertionsEnabled() ? new Invariants() : null;
568
569 static StackTraceElement[] getStackTrace(Thread thread) {
570 return thread.getStackTrace();
571 }
572
573 /**
574 * Utility for enforcing {@link DebugContext} invariants via assertions.
575 */
576 static class Invariants {
577 private final Thread thread;
578 private final StackTraceElement[] origin;
579
580 Invariants() {
581 thread = Thread.currentThread();
582 origin = getStackTrace(thread);
583 }
584
585 boolean checkNoConcurrentAccess() {
586 Thread currentThread = Thread.currentThread();
587 if (currentThread != thread) {
|