jdk/test/java/lang/StackWalker/LocalsAndOperands.java
author bchristi
Thu, 05 May 2016 11:44:01 -0700
changeset 38347 a3fdbd11148f
parent 34934 7fbff68dbc12
child 40684 2e37c119dc2a
permissions -rw-r--r--
8147039: Incorrect locals and operands in compiled frames Summary: Implement stack walking using javaVFrame instead of vframeStream Reviewed-by: mchung, vlivanov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     1
/*
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
     2
 * Copyright (c) 2015, 2016 Oracle and/or its affiliates. All rights reserved.
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     4
 *
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     8
 *
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    13
 * accompanied this code).
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    14
 *
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    18
 *
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    21
 * questions.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    22
 */
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    23
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    24
/*
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    25
 * @test
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    26
 * @bug 8020968 8147039
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    27
 * @summary Tests for locals and operands
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    28
 * @run testng LocalsAndOperands
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    29
 */
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    30
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    31
import org.testng.annotations.*;
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    32
import java.lang.StackWalker.StackFrame;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    33
import java.lang.reflect.*;
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    34
import java.util.*;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    35
import java.util.stream.*;
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    36
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    37
public class LocalsAndOperands {
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    38
    static final boolean debug = true;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    39
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    40
    static Class<?> liveStackFrameClass;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    41
    static Class<?> primitiveValueClass;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    42
    static StackWalker extendedWalker;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    43
    static Method getLocals;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    44
    static Method getOperands;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    45
    static Method getMonitors;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    46
    static Method primitiveType;
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    47
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    48
    static {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    49
        try {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    50
            liveStackFrameClass = Class.forName("java.lang.LiveStackFrame");
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    51
            primitiveValueClass = Class.forName("java.lang.LiveStackFrame$PrimitiveValue");
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    52
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    53
            getLocals = liveStackFrameClass.getDeclaredMethod("getLocals");
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    54
            getLocals.setAccessible(true);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    55
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    56
            getOperands = liveStackFrameClass.getDeclaredMethod("getStack");
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    57
            getOperands.setAccessible(true);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    58
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    59
            getMonitors = liveStackFrameClass.getDeclaredMethod("getMonitors");
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    60
            getMonitors.setAccessible(true);
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    61
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    62
            primitiveType = primitiveValueClass.getDeclaredMethod("type");
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    63
            primitiveType.setAccessible(true);
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    64
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    65
            Method method = liveStackFrameClass.getMethod("getStackWalker");
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    66
            method.setAccessible(true);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    67
            extendedWalker = (StackWalker) method.invoke(null);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    68
        } catch (Throwable t) { throw new RuntimeException(t); }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    69
    }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    70
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    71
    /** Helper method to return a StackFrame's locals */
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    72
    static Object[] invokeGetLocals(StackFrame arg) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    73
        try {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    74
            return (Object[]) getLocals.invoke(arg);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    75
        } catch (Exception e) { throw new RuntimeException(e); }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    76
    }
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    77
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    78
    /*****************
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    79
     * DataProviders *
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    80
     *****************/
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    81
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    82
    /** Calls testLocals() and provides LiveStackFrames for testLocals* methods */
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    83
    @DataProvider
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    84
    public static StackFrame[][] provider() {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    85
        return new StackFrame[][] {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    86
            new Tester().testLocals()
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    87
        };
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    88
    }
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    89
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    90
    /**
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    91
     * Calls testLocalsKeepAlive() and provides LiveStackFrames for testLocals* methods.
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    92
     * Local variables in testLocalsKeepAlive() are ensured to not become dead.
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    93
     */
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    94
    @DataProvider
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    95
    public static StackFrame[][] keepAliveProvider() {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    96
        return new StackFrame[][] {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    97
            new Tester().testLocalsKeepAlive()
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    98
        };
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    99
    }
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   100
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   101
    /**
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   102
     * Provides StackFrames from a StackWalker without the LOCALS_AND_OPERANDS
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   103
     * option.
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   104
     */
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   105
    @DataProvider
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   106
    public static StackFrame[][] noLocalsProvider() {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   107
        // Use default StackWalker
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   108
        return new StackFrame[][] {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   109
            new Tester(StackWalker.getInstance(), true).testLocals()
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   110
        };
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   111
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   112
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   113
    /**
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   114
     * Calls testLocals() and provides LiveStackFrames for *all* called methods,
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   115
     * including test infrastructure (jtreg, testng, etc)
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   116
     *
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   117
     */
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   118
    @DataProvider
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   119
    public static StackFrame[][] unfilteredProvider() {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   120
        return new StackFrame[][] {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   121
            new Tester(extendedWalker, false).testLocals()
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   122
        };
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   123
    }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   124
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   125
    /****************
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   126
     * Test methods *
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   127
     ****************/
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   128
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   129
    /**
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   130
     * Check for expected local values and types in the LiveStackFrame
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   131
     */
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   132
    @Test(dataProvider = "keepAliveProvider")
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   133
    public static void checkLocalValues(StackFrame... frames) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   134
        if (debug) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   135
            System.out.println("Running checkLocalValues");
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   136
            dumpStackWithLocals(frames);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   137
        }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   138
        Arrays.stream(frames).filter(f -> f.getMethodName()
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   139
                                           .equals("testLocalsKeepAlive"))
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   140
                                           .forEach(
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   141
            f -> {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   142
                Object[] locals = invokeGetLocals(f);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   143
                for (int i = 0; i < locals.length; i++) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   144
                    // Value
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   145
                    String expected = Tester.LOCAL_VALUES[i];
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   146
                    Object observed = locals[i];
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   147
                    if (expected != null /* skip nulls in golden values */ &&
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   148
                            !expected.equals(observed.toString())) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   149
                        System.err.println("Local value mismatch:");
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   150
                        if (!debug) { dumpStackWithLocals(frames); }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   151
                        throw new RuntimeException("local " + i + " value is " +
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   152
                                observed + ", expected " + expected);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   153
                    }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   154
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   155
                    // Type
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   156
                    expected = Tester.LOCAL_TYPES[i];
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   157
                    observed = type(locals[i]);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   158
                    if (expected != null /* skip nulls in golden values */ &&
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   159
                            !expected.equals(observed)) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   160
                        System.err.println("Local type mismatch:");
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   161
                        if (!debug) { dumpStackWithLocals(frames); }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   162
                        throw new RuntimeException("local " + i + " type is " +
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   163
                                observed + ", expected " + expected);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   164
                    }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   165
                }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   166
            }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   167
        );
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   168
    }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   169
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   170
    /**
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   171
     * Basic sanity check for locals and operands
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   172
     */
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   173
    @Test(dataProvider = "provider")
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   174
    public static void sanityCheck(StackFrame... frames) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   175
        if (debug) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   176
            System.out.println("Running sanityCheck");
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   177
        }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   178
        try {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   179
            Stream<StackFrame> stream = Arrays.stream(frames);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   180
            if (debug) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   181
                stream.forEach(LocalsAndOperands::printLocals);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   182
            } else {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   183
                System.out.println(stream.count() + " frames");
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   184
            }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   185
        } catch (Throwable t) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   186
            dumpStackWithLocals(frames);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   187
            throw t;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   188
        }
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   189
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   190
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   191
    /**
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   192
     * Sanity check for locals and operands, including testng/jtreg frames
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   193
     */
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   194
    @Test(dataProvider = "unfilteredProvider")
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   195
    public static void unfilteredSanityCheck(StackFrame... frames) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   196
        if (debug) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   197
            System.out.println("Running unfilteredSanityCheck");
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   198
        }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   199
        try {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   200
            Stream<StackFrame> stream = Arrays.stream(frames);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   201
            if (debug) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   202
                stream.forEach(f -> { System.out.println(f + ": " +
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   203
                        invokeGetLocals(f).length + " locals"); } );
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   204
            } else {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   205
                System.out.println(stream.count() + " frames");
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   206
            }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   207
        } catch (Throwable t) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   208
            dumpStackWithLocals(frames);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   209
            throw t;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   210
        }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   211
    }
34934
7fbff68dbc12 8144552: java/lang/StackWalker/LocalsAndOperands.java fails with java.lang.NullPointerException
bchristi
parents: 34372
diff changeset
   212
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   213
    /**
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   214
     * Test that LiveStackFrames are not provided with the default StackWalker
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   215
     * options.
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   216
     */
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   217
    @Test(dataProvider = "noLocalsProvider")
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   218
    public static void withoutLocalsAndOperands(StackFrame... frames) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   219
        for (StackFrame frame : frames) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   220
            if (liveStackFrameClass.isInstance(frame)) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   221
                throw new RuntimeException("should not be LiveStackFrame");
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   222
            }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   223
        }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   224
    }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   225
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   226
    static class Tester {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   227
        private StackWalker walker;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   228
        private boolean filter = true; // Filter out testng/jtreg/etc frames?
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   229
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   230
        Tester() {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   231
            this.walker = extendedWalker;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   232
        }
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   233
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   234
        Tester(StackWalker walker, boolean filter) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   235
            this.walker = walker;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   236
            this.filter = filter;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   237
        }
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   238
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   239
        /**
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   240
         * Perform stackwalk without keeping local variables alive and return an
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   241
         * array of the collected StackFrames
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   242
         */
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   243
        private synchronized StackFrame[] testLocals() {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   244
            // Unused local variables will become dead
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   245
            int x = 10;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   246
            char c = 'z';
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   247
            String hi = "himom";
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   248
            long l = 1000000L;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   249
            double d =  3.1415926;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   250
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   251
            if (filter) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   252
                return walker.walk(s -> s.filter(f -> TEST_METHODS.contains(f
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   253
                        .getMethodName())).collect(Collectors.toList()))
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   254
                        .toArray(new StackFrame[0]);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   255
            } else {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   256
                return walker.walk(s -> s.collect(Collectors.toList()))
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   257
                        .toArray(new StackFrame[0]);
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   258
            }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   259
        }
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   260
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   261
        /**
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   262
         * Perform stackwalk, keeping local variables alive, and return a list of
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   263
         * the collected StackFrames
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   264
         */
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   265
        private synchronized StackFrame[] testLocalsKeepAlive() {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   266
            int x = 10;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   267
            char c = 'z';
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   268
            String hi = "himom";
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   269
            long l = 1000000L;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   270
            double d =  3.1415926;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   271
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   272
            List<StackWalker.StackFrame> frames;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   273
            if (filter) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   274
                frames = walker.walk(s -> s.filter(f -> TEST_METHODS.contains(f
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   275
                        .getMethodName())).collect(Collectors.toList()));
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   276
            } else {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   277
                frames = walker.walk(s -> s.collect(Collectors.toList()));
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   278
            }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   279
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   280
            // Use local variables so they stay alive
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   281
            System.out.println("Stayin' alive: "+x+" "+c+" "+hi+" "+l+" "+d);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   282
            return frames.toArray(new StackFrame[0]); // FIXME: convert to Array here
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   283
        }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   284
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   285
        // Expected values for locals in testLocals() & testLocalsKeepAlive()
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   286
        // TODO: use real values instead of Strings, rebuild doubles & floats, etc
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   287
        private final static String[] LOCAL_VALUES = new String[] {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   288
            null, // skip, LocalsAndOperands$Tester@XXX identity is different each run
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   289
            "10",
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   290
            "122",
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   291
            "himom",
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   292
            "0",
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   293
            null, // skip, fix in 8156073
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   294
            null, // skip, fix in 8156073
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   295
            null, // skip, fix in 8156073
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   296
            "0"
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   297
        };
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   298
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   299
        // Expected types for locals in testLocals() & testLocalsKeepAlive()
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   300
        // TODO: use real types
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   301
        private final static String[] LOCAL_TYPES = new String[] {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   302
            null, // skip
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   303
            "I",
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   304
            "I",
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   305
            "java.lang.String",
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   306
            "I",
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   307
            "I",
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   308
            "I",
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   309
            "I",
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   310
            "I"
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   311
        };
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   312
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   313
        final static Map NUM_LOCALS = Map.of("testLocals", 8,
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   314
                                             "testLocalsKeepAlive",
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   315
                                             LOCAL_VALUES.length);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   316
        private final static Collection<String> TEST_METHODS = NUM_LOCALS.keySet();
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   317
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   318
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   319
    /**
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   320
     * Print stack trace with locals
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   321
     */
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   322
    public static void dumpStackWithLocals(StackFrame...frames) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   323
        Arrays.stream(frames).forEach(LocalsAndOperands::printLocals);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   324
    }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   325
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   326
    /**
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   327
     * Print the StackFrame and an indexed list of its locals
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   328
     */
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   329
    public static void printLocals(StackWalker.StackFrame frame) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   330
        try {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   331
            System.out.println(frame);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   332
            Object[] locals = (Object[]) getLocals.invoke(frame);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   333
            for (int i = 0; i < locals.length; i++) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   334
                System.out.format("  local %d: %s type %s\n", i, locals[i], type(locals[i]));
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   335
            }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   336
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   337
            Object[] operands = (Object[]) getOperands.invoke(frame);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   338
            for (int i = 0; i < operands.length; i++) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   339
                System.out.format("  operand %d: %s type %s%n", i, operands[i],
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   340
                                  type(operands[i]));
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   341
            }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   342
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   343
            Object[] monitors = (Object[]) getMonitors.invoke(frame);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   344
            for (int i = 0; i < monitors.length; i++) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   345
                System.out.format("  monitor %d: %s%n", i, monitors[i]);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   346
            }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   347
        } catch (Exception e) { throw new RuntimeException(e); }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   348
    }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   349
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   350
    private static String type(Object o) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   351
        try {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   352
            if (o == null) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   353
                return "null";
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   354
            } else if (primitiveValueClass.isInstance(o)) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   355
                char c = (char)primitiveType.invoke(o);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   356
                return String.valueOf(c);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   357
            } else {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   358
                return o.getClass().getName();
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   359
            }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   360
        } catch(Exception e) { throw new RuntimeException(e); }
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   361
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   362
}