jdk/test/java/lang/StackWalker/LocalsAndOperands.java
author alanb
Fri, 10 Feb 2017 09:04:39 +0000
changeset 43712 5dfd0950317c
parent 42338 a60f280f803c
child 43715 6d72cc84759f
permissions -rw-r--r--
8173393: Module system implementation refresh (2/2017) Reviewed-by: dfuchs, psandoz, mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com, claes.redestad@oracle.com, alex.buckley@oracle.com, mark.reinhold@oracle.com, john.r.rose@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     1
/*
40684
2e37c119dc2a 8164982: Fix legal notices in java/lang, java/net, java/util tests.
shurailine
parents: 38347
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
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40684
diff changeset
    28
 * @modules java.base/java.lang:open
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    29
 * @run testng LocalsAndOperands
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    30
 */
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    31
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    32
import org.testng.annotations.*;
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    33
import java.lang.StackWalker.StackFrame;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    34
import java.lang.reflect.*;
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    35
import java.util.*;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    36
import java.util.stream.*;
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    37
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    38
public class LocalsAndOperands {
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    39
    static final boolean debug = true;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    40
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    41
    static Class<?> liveStackFrameClass;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    42
    static Class<?> primitiveValueClass;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    43
    static StackWalker extendedWalker;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    44
    static Method getLocals;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    45
    static Method getOperands;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    46
    static Method getMonitors;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    47
    static Method primitiveType;
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    48
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    49
    static {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    50
        try {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    51
            liveStackFrameClass = Class.forName("java.lang.LiveStackFrame");
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    52
            primitiveValueClass = Class.forName("java.lang.LiveStackFrame$PrimitiveValue");
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    53
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    54
            getLocals = liveStackFrameClass.getDeclaredMethod("getLocals");
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    55
            getLocals.setAccessible(true);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    56
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    57
            getOperands = liveStackFrameClass.getDeclaredMethod("getStack");
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    58
            getOperands.setAccessible(true);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    59
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    60
            getMonitors = liveStackFrameClass.getDeclaredMethod("getMonitors");
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    61
            getMonitors.setAccessible(true);
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    62
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    63
            primitiveType = primitiveValueClass.getDeclaredMethod("type");
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    64
            primitiveType.setAccessible(true);
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    65
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    66
            Method method = liveStackFrameClass.getMethod("getStackWalker");
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    67
            method.setAccessible(true);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    68
            extendedWalker = (StackWalker) method.invoke(null);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    69
        } catch (Throwable t) { throw new RuntimeException(t); }
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
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    72
    /** Helper method to return a StackFrame's locals */
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    73
    static Object[] invokeGetLocals(StackFrame arg) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    74
        try {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    75
            return (Object[]) getLocals.invoke(arg);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    76
        } catch (Exception e) { throw new RuntimeException(e); }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    77
    }
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    78
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    79
    /*****************
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    80
     * DataProviders *
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    81
     *****************/
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    82
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    83
    /** Calls testLocals() and provides LiveStackFrames for testLocals* methods */
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    84
    @DataProvider
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    85
    public static StackFrame[][] provider() {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    86
        return new StackFrame[][] {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    87
            new Tester().testLocals()
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    88
        };
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    89
    }
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    90
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    91
    /**
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    92
     * Calls testLocalsKeepAlive() and provides LiveStackFrames for testLocals* methods.
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    93
     * Local variables in testLocalsKeepAlive() are ensured to not become dead.
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    94
     */
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    95
    @DataProvider
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    96
    public static StackFrame[][] keepAliveProvider() {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    97
        return new StackFrame[][] {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    98
            new Tester().testLocalsKeepAlive()
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
    99
        };
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   100
    }
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   101
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   102
    /**
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   103
     * Provides StackFrames from a StackWalker without the LOCALS_AND_OPERANDS
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   104
     * option.
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   105
     */
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   106
    @DataProvider
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   107
    public static StackFrame[][] noLocalsProvider() {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   108
        // Use default StackWalker
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   109
        return new StackFrame[][] {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   110
            new Tester(StackWalker.getInstance(), true).testLocals()
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   111
        };
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   112
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   113
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   114
    /**
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   115
     * Calls testLocals() and provides LiveStackFrames for *all* called methods,
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   116
     * including test infrastructure (jtreg, testng, etc)
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
     */
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   119
    @DataProvider
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   120
    public static StackFrame[][] unfilteredProvider() {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   121
        return new StackFrame[][] {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   122
            new Tester(extendedWalker, false).testLocals()
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
    /****************
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   127
     * Test methods *
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
    /**
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   131
     * Check for expected local values and types in the LiveStackFrame
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   132
     */
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   133
    @Test(dataProvider = "keepAliveProvider")
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   134
    public static void checkLocalValues(StackFrame... frames) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   135
        if (debug) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   136
            System.out.println("Running checkLocalValues");
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   137
            dumpStackWithLocals(frames);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   138
        }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   139
        Arrays.stream(frames).filter(f -> f.getMethodName()
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   140
                                           .equals("testLocalsKeepAlive"))
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   141
                                           .forEach(
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   142
            f -> {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   143
                Object[] locals = invokeGetLocals(f);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   144
                for (int i = 0; i < locals.length; i++) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   145
                    // Value
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   146
                    String expected = Tester.LOCAL_VALUES[i];
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   147
                    Object observed = locals[i];
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   148
                    if (expected != null /* skip nulls in golden values */ &&
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   149
                            !expected.equals(observed.toString())) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   150
                        System.err.println("Local value mismatch:");
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   151
                        if (!debug) { dumpStackWithLocals(frames); }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   152
                        throw new RuntimeException("local " + i + " value is " +
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   153
                                observed + ", expected " + expected);
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
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   156
                    // Type
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   157
                    expected = Tester.LOCAL_TYPES[i];
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   158
                    observed = type(locals[i]);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   159
                    if (expected != null /* skip nulls in golden values */ &&
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   160
                            !expected.equals(observed)) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   161
                        System.err.println("Local type mismatch:");
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   162
                        if (!debug) { dumpStackWithLocals(frames); }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   163
                        throw new RuntimeException("local " + i + " type is " +
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   164
                                observed + ", expected " + expected);
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
    /**
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   172
     * Basic sanity check for locals and operands
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   173
     */
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   174
    @Test(dataProvider = "provider")
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   175
    public static void sanityCheck(StackFrame... frames) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   176
        if (debug) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   177
            System.out.println("Running sanityCheck");
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   178
        }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   179
        try {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   180
            Stream<StackFrame> stream = Arrays.stream(frames);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   181
            if (debug) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   182
                stream.forEach(LocalsAndOperands::printLocals);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   183
            } else {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   184
                System.out.println(stream.count() + " frames");
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   185
            }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   186
        } catch (Throwable t) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   187
            dumpStackWithLocals(frames);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   188
            throw t;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   189
        }
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   190
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   191
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   192
    /**
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   193
     * Sanity check for locals and operands, including testng/jtreg frames
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   194
     */
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   195
    @Test(dataProvider = "unfilteredProvider")
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   196
    public static void unfilteredSanityCheck(StackFrame... frames) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   197
        if (debug) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   198
            System.out.println("Running unfilteredSanityCheck");
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   199
        }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   200
        try {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   201
            Stream<StackFrame> stream = Arrays.stream(frames);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   202
            if (debug) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   203
                stream.forEach(f -> { System.out.println(f + ": " +
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   204
                        invokeGetLocals(f).length + " locals"); } );
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   205
            } else {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   206
                System.out.println(stream.count() + " frames");
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   207
            }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   208
        } catch (Throwable t) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   209
            dumpStackWithLocals(frames);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   210
            throw t;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   211
        }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   212
    }
34934
7fbff68dbc12 8144552: java/lang/StackWalker/LocalsAndOperands.java fails with java.lang.NullPointerException
bchristi
parents: 34372
diff changeset
   213
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   214
    /**
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   215
     * Test that LiveStackFrames are not provided with the default StackWalker
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   216
     * options.
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   217
     */
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   218
    @Test(dataProvider = "noLocalsProvider")
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   219
    public static void withoutLocalsAndOperands(StackFrame... frames) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   220
        for (StackFrame frame : frames) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   221
            if (liveStackFrameClass.isInstance(frame)) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   222
                throw new RuntimeException("should not be LiveStackFrame");
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
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   227
    static class Tester {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   228
        private StackWalker walker;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   229
        private boolean filter = true; // Filter out testng/jtreg/etc frames?
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   230
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   231
        Tester() {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   232
            this.walker = extendedWalker;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   233
        }
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   234
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   235
        Tester(StackWalker walker, boolean filter) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   236
            this.walker = walker;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   237
            this.filter = filter;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   238
        }
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   239
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   240
        /**
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   241
         * Perform stackwalk without keeping local variables alive and return an
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   242
         * array of the collected StackFrames
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   243
         */
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   244
        private synchronized StackFrame[] testLocals() {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   245
            // Unused local variables will become dead
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   246
            int x = 10;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   247
            char c = 'z';
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   248
            String hi = "himom";
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   249
            long l = 1000000L;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   250
            double d =  3.1415926;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   251
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   252
            if (filter) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   253
                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
   254
                        .getMethodName())).collect(Collectors.toList()))
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   255
                        .toArray(new StackFrame[0]);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   256
            } else {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   257
                return walker.walk(s -> s.collect(Collectors.toList()))
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   258
                        .toArray(new StackFrame[0]);
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   259
            }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   260
        }
38347
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
        /**
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   263
         * 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
   264
         * the collected StackFrames
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   265
         */
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   266
        private synchronized StackFrame[] testLocalsKeepAlive() {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   267
            int x = 10;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   268
            char c = 'z';
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   269
            String hi = "himom";
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   270
            long l = 1000000L;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   271
            double d =  3.1415926;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   272
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   273
            List<StackWalker.StackFrame> frames;
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   274
            if (filter) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   275
                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
   276
                        .getMethodName())).collect(Collectors.toList()));
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   277
            } else {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   278
                frames = walker.walk(s -> s.collect(Collectors.toList()));
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
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   281
            // Use local variables so they stay alive
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   282
            System.out.println("Stayin' alive: "+x+" "+c+" "+hi+" "+l+" "+d);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   283
            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
   284
        }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   285
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   286
        // Expected values for locals in testLocals() & testLocalsKeepAlive()
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   287
        // 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
   288
        private final static String[] LOCAL_VALUES = new String[] {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   289
            null, // skip, LocalsAndOperands$Tester@XXX identity is different each run
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   290
            "10",
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   291
            "122",
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   292
            "himom",
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   293
            "0",
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
            null, // skip, fix in 8156073
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   297
            "0"
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
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   300
        // Expected types for locals in testLocals() & testLocalsKeepAlive()
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   301
        // TODO: use real types
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   302
        private final static String[] LOCAL_TYPES = new String[] {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   303
            null, // skip
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
            "I",
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   306
            "java.lang.String",
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
            "I"
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
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   314
        final static Map NUM_LOCALS = Map.of("testLocals", 8,
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   315
                                             "testLocalsKeepAlive",
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   316
                                             LOCAL_VALUES.length);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   317
        private final static Collection<String> TEST_METHODS = NUM_LOCALS.keySet();
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   318
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   319
38347
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   320
    /**
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   321
     * Print stack trace with locals
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   322
     */
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   323
    public static void dumpStackWithLocals(StackFrame...frames) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   324
        Arrays.stream(frames).forEach(LocalsAndOperands::printLocals);
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
    /**
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   328
     * Print the StackFrame and an indexed list of its locals
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   329
     */
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   330
    public static void printLocals(StackWalker.StackFrame frame) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   331
        try {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   332
            System.out.println(frame);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   333
            Object[] locals = (Object[]) getLocals.invoke(frame);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   334
            for (int i = 0; i < locals.length; i++) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   335
                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
   336
            }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   337
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   338
            Object[] operands = (Object[]) getOperands.invoke(frame);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   339
            for (int i = 0; i < operands.length; i++) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   340
                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
   341
                                  type(operands[i]));
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
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   344
            Object[] monitors = (Object[]) getMonitors.invoke(frame);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   345
            for (int i = 0; i < monitors.length; i++) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   346
                System.out.format("  monitor %d: %s%n", i, monitors[i]);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   347
            }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   348
        } catch (Exception e) { throw new RuntimeException(e); }
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
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   351
    private static String type(Object o) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   352
        try {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   353
            if (o == null) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   354
                return "null";
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   355
            } else if (primitiveValueClass.isInstance(o)) {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   356
                char c = (char)primitiveType.invoke(o);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   357
                return String.valueOf(c);
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   358
            } else {
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   359
                return o.getClass().getName();
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   360
            }
a3fdbd11148f 8147039: Incorrect locals and operands in compiled frames
bchristi
parents: 34934
diff changeset
   361
        } catch(Exception e) { throw new RuntimeException(e); }
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   362
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   363
}