src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.debug.test/src/org/graalvm/compiler/debug/test/DebugContextTest.java
author iveresov
Fri, 02 Feb 2018 17:28:17 -0800
changeset 48861 47f19ff9903c
parent 47216 71c04702a3d5
child 49873 26ebfe8ce852
permissions -rw-r--r--
8194819: Update Graal Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46640
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
     1
/*
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
     2
 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
     4
 *
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
     8
 *
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    13
 * accompanied this code).
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    14
 *
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    18
 *
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    21
 * questions.
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    22
 */
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    23
package org.graalvm.compiler.debug.test;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    24
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    25
import static org.graalvm.compiler.debug.DebugContext.NO_DESCRIPTION;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    26
import static org.graalvm.compiler.debug.DebugContext.NO_GLOBAL_METRIC_VALUES;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    27
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    28
import java.io.ByteArrayOutputStream;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    29
import java.io.DataInputStream;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    30
import java.io.IOException;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    31
import java.io.PrintStream;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    32
import java.util.Arrays;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    33
import java.util.Collections;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    34
import java.util.Formatter;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    35
import java.util.List;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    36
48861
47f19ff9903c 8194819: Update Graal
iveresov
parents: 47216
diff changeset
    37
import org.graalvm.collections.EconomicMap;
46640
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    38
import org.graalvm.compiler.debug.Assertions;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    39
import org.graalvm.compiler.debug.CounterKey;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    40
import org.graalvm.compiler.debug.DebugCloseable;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    41
import org.graalvm.compiler.debug.DebugContext;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    42
import org.graalvm.compiler.debug.DebugContext.Scope;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    43
import org.graalvm.compiler.debug.DebugDumpHandler;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    44
import org.graalvm.compiler.debug.DebugHandler;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    45
import org.graalvm.compiler.debug.DebugHandlersFactory;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    46
import org.graalvm.compiler.debug.DebugOptions;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    47
import org.graalvm.compiler.debug.DebugVerifyHandler;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    48
import org.graalvm.compiler.options.OptionKey;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    49
import org.graalvm.compiler.options.OptionValues;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    50
import org.junit.Assert;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    51
import org.junit.Assume;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    52
import org.junit.Test;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    53
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    54
@SuppressWarnings("try")
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    55
public class DebugContextTest {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    56
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    57
    static class DebugContextSetup {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    58
        final Formatter dumpOutput = new Formatter();
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    59
        final Formatter verifyOutput = new Formatter();
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    60
        final ByteArrayOutputStream logOutput = new ByteArrayOutputStream();
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    61
        DebugHandlersFactory handlers = new DebugHandlersFactory() {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    62
            @Override
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    63
            public List<DebugHandler> createHandlers(OptionValues options) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    64
                return Arrays.asList(new DebugDumpHandler() {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    65
                    @Override
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    66
                    public void dump(DebugContext ignore, Object object, String format, Object... arguments) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    67
                        dumpOutput.format("Dumping %s with label \"%s\"%n", object, String.format(format, arguments));
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    68
                    }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    69
                }, new DebugVerifyHandler() {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    70
                    @Override
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    71
                    public void verify(DebugContext ignore, Object object, String format, Object... args) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    72
                        verifyOutput.format("Verifying %s with label \"%s\"%n", object, String.format(format, args));
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    73
                    }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    74
                });
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    75
            }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    76
        };
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    77
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    78
        DebugContext openDebugContext(OptionValues options) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    79
            return DebugContext.create(options, NO_DESCRIPTION, NO_GLOBAL_METRIC_VALUES, new PrintStream(logOutput), Collections.singletonList(handlers));
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    80
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    81
        }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    82
    }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    83
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    84
    @Test
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    85
    public void testDisabledScopes() {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    86
        OptionValues options = new OptionValues(EconomicMap.create());
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    87
        DebugContextSetup setup = new DebugContextSetup();
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    88
        try (DebugContext debug = setup.openDebugContext(options);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    89
                        DebugContext.Scope d = debug.scope("TestDisabledScoping")) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    90
            for (int level = DebugContext.BASIC_LEVEL; level <= DebugContext.VERY_DETAILED_LEVEL; level++) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    91
                debug.dump(level, "an object", "at level %d", level);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    92
                debug.verify("an object", "at level %d", level);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    93
                debug.log(level, "log statement at level %d", level);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    94
            }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    95
        }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    96
        String log = setup.logOutput.toString();
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    97
        String dumpOutput = setup.dumpOutput.toString();
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    98
        String verifyOutput = setup.verifyOutput.toString();
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
    99
        Assert.assertTrue(log, log.isEmpty());
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   100
        Assert.assertTrue(dumpOutput, dumpOutput.isEmpty());
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   101
        Assert.assertTrue(verifyOutput, verifyOutput.isEmpty());
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   102
    }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   103
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   104
    @Test
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   105
    public void testDumping() {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   106
        for (int level = DebugContext.BASIC_LEVEL; level <= DebugContext.VERY_DETAILED_LEVEL; level++) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   107
            OptionValues options = new OptionValues(EconomicMap.create());
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   108
            options = new OptionValues(options, DebugOptions.Dump, "Scope" + level + ":" + level);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   109
            DebugContextSetup setup = new DebugContextSetup();
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   110
            try (DebugContext debug = setup.openDebugContext(options);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   111
                            DebugContext.Scope s0 = debug.scope("TestDumping")) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   112
                try (DebugContext.Scope s1 = debug.scope("Scope1")) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   113
                    try (DebugContext.Scope s2 = debug.scope("Scope2")) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   114
                        try (DebugContext.Scope s3 = debug.scope("Scope3")) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   115
                            try (DebugContext.Scope s4 = debug.scope("Scope4")) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   116
                                try (DebugContext.Scope s5 = debug.scope("Scope5")) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   117
                                    debug.dump(level, "an object", "at level %d", level);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   118
                                }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   119
                            }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   120
                        }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   121
                    }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   122
                }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   123
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   124
            }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   125
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   126
            String expect = String.format("Dumping an object with label \"at level %d\"%n", level);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   127
            String dump = setup.dumpOutput.toString();
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   128
            Assert.assertEquals(expect, dump);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   129
        }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   130
    }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   131
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   132
    @Test
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   133
    public void testLogging() throws IOException {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   134
        OptionValues options = new OptionValues(EconomicMap.create());
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   135
        options = new OptionValues(options, DebugOptions.Log, ":5");
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   136
        DebugContextSetup setup = new DebugContextSetup();
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   137
        try (DebugContext debug = setup.openDebugContext(options)) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   138
            for (int level = DebugContext.BASIC_LEVEL; level <= DebugContext.VERY_DETAILED_LEVEL; level++) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   139
                try (DebugContext.Scope s0 = debug.scope("TestLogging")) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   140
                    debug.log(level, "log statement at level %d", level);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   141
                    try (DebugContext.Scope s1 = debug.scope("Level1")) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   142
                        debug.log(level, "log statement at level %d", level);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   143
                        try (DebugContext.Scope s2 = debug.scope("Level2")) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   144
                            debug.log(level, "log statement at level %d", level);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   145
                            try (DebugContext.Scope s3 = debug.scope("Level3")) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   146
                                debug.log(level, "log statement at level %d", level);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   147
                                try (DebugContext.Scope s4 = debug.scope("Level4")) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   148
                                    debug.log(level, "log statement at level %d", level);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   149
                                    try (DebugContext.Scope s5 = debug.scope("Level5")) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   150
                                        debug.log(level, "log statement at level %d", level);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   151
                                    }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   152
                                }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   153
                            }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   154
                        }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   155
                    }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   156
                }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   157
            }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   158
        }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   159
        DataInputStream in = new DataInputStream(getClass().getResourceAsStream(getClass().getSimpleName() + ".testLogging.input"));
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   160
        byte[] buf = new byte[in.available()];
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   161
        in.readFully(buf);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   162
        String threadLabel = "[thread:" + Thread.currentThread().getId() + "]";
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   163
        String expect = new String(buf).replace("[thread:1]", threadLabel);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   164
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   165
        String log = setup.logOutput.toString();
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   166
        Assert.assertEquals(expect, log);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   167
    }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   168
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   169
    @Test
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   170
    public void testEnabledSandbox() {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   171
        EconomicMap<OptionKey<?>, Object> map = EconomicMap.create();
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   172
        // Configure with an option that enables scopes
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   173
        map.put(DebugOptions.DumpOnError, true);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   174
        OptionValues options = new OptionValues(map);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   175
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   176
        DebugContext debug = DebugContext.create(options, NO_DESCRIPTION, NO_GLOBAL_METRIC_VALUES, new PrintStream(baos), DebugHandlersFactory.LOADER);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   177
        Exception e = new Exception("testEnabledSandbox");
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   178
        String scopeName = "";
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   179
        try {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   180
            try (DebugContext.Scope d = debug.sandbox("TestExceptionHandling", debug.getConfig())) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   181
                scopeName = d.getQualifiedName();
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   182
                throw e;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   183
            } catch (Throwable t) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   184
                assert e == t;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   185
                debug.handle(t);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   186
            }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   187
        } catch (Throwable t) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   188
            // The exception object should propagate all the way out through
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   189
            // a enabled sandbox scope
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   190
            Assert.assertEquals(e, t);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   191
        }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   192
        String logged = baos.toString();
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   193
        String expected = String.format("Exception raised in scope %s: %s", scopeName, e);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   194
        String line = "-------------------------------------------------------";
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   195
        Assert.assertTrue(String.format("Could not find \"%s\" in content between lines below:%n%s%n%s%s", expected, line, logged, line), logged.contains(expected));
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   196
    }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   197
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   198
    @Test
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   199
    public void testDisabledSandbox() {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   200
        EconomicMap<OptionKey<?>, Object> map = EconomicMap.create();
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   201
        // Configure with an option that enables scopes
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   202
        map.put(DebugOptions.DumpOnError, true);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   203
        OptionValues options = new OptionValues(map);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   204
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   205
        DebugContext debug = DebugContext.create(options, NO_DESCRIPTION, NO_GLOBAL_METRIC_VALUES, new PrintStream(baos), DebugHandlersFactory.LOADER);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   206
        Exception e = new Exception("testDisabledSandbox");
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   207
        try {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   208
            // Test a disabled sandbox scope
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   209
            try (DebugContext.Scope d = debug.sandbox("TestExceptionHandling", null)) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   210
                throw e;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   211
            } catch (Throwable t) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   212
                assert e == t;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   213
                debug.handle(t);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   214
            }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   215
        } catch (Throwable t) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   216
            // The exception object should propagate all the way out through
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   217
            // a disabled sandbox scope
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   218
            Assert.assertEquals(e, t);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   219
        }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   220
        String logged = baos.toString();
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   221
        Assert.assertTrue(logged, logged.isEmpty());
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   222
    }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   223
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   224
    /**
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   225
     * Tests that using a {@link DebugContext} on a thread other than the one on which it was
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   226
     * created causes an assertion failure.
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   227
     */
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   228
    @Test
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   229
    public void testInvariantChecking() throws InterruptedException {
46762
f7defa99f173 8185829: Update Graal
dlong
parents: 46640
diff changeset
   230
        Assume.assumeTrue(Assertions.assertionsEnabled());
46640
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   231
        EconomicMap<OptionKey<?>, Object> map = EconomicMap.create();
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   232
        // Configure with an option that enables counters
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   233
        map.put(DebugOptions.Counters, "");
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   234
        OptionValues options = new OptionValues(map);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   235
        DebugContext debug = DebugContext.create(options, DebugHandlersFactory.LOADER);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   236
        CounterKey counter = DebugContext.counter("DebugContextTestCounter");
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   237
        AssertionError[] result = {null};
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   238
        Thread thread = new Thread() {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   239
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   240
            @Override
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   241
            public void run() {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   242
                try {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   243
                    counter.add(debug, 1);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   244
                } catch (AssertionError e) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   245
                    result[0] = e;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   246
                }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   247
            }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   248
        };
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   249
        thread.start();
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   250
        thread.join();
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   251
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   252
        Assert.assertNotNull("Expected thread to throw AssertionError", result[0]);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   253
    }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   254
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   255
    @Test
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   256
    public void testDisableIntercept() {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   257
        EconomicMap<OptionKey<?>, Object> map = EconomicMap.create();
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   258
        // Configure with an option that enables scopes
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   259
        map.put(DebugOptions.DumpOnError, true);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   260
        OptionValues options = new OptionValues(map);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   261
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   262
        DebugContext debug = DebugContext.create(options, NO_DESCRIPTION, NO_GLOBAL_METRIC_VALUES, new PrintStream(baos), DebugHandlersFactory.LOADER);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   263
        Exception e = new Exception();
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   264
        try {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   265
            try (DebugCloseable disabled = debug.disableIntercept(); Scope s1 = debug.scope("ScopeWithDisabledIntercept")) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   266
                try (Scope s2 = debug.scope("InnerScopeInheritsDisabledIntercept")) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   267
                    throw e;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   268
                }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   269
            } catch (Throwable t) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   270
                assert e == t;
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   271
                debug.handle(t);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   272
            }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   273
        } catch (Throwable t) {
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   274
            // The exception object should propagate all the way out through
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   275
            // an intercept disabled scope
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   276
            Assert.assertEquals(e, t);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   277
        }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   278
        String logged = baos.toString();
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   279
        Assert.assertEquals("Exception should not have been intercepted", "", logged);
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   280
    }
70bdce04c59b 8183991: Update Graal
iveresov
parents:
diff changeset
   281
}