GetStackTraceElementTest.java revision 11707:ad7af1afda7a
1/*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25/**
26 * @test
27 * @bug 8136421
28 * @requires (vm.simpleArch == "x64" | vm.simpleArch == "sparcv9" | vm.simpleArch == "aarch64")
29 * @library /testlibrary /test/lib /
30 * @library ../common/patches
31 * @modules java.base/jdk.internal.misc
32 * @modules java.base/jdk.internal.org.objectweb.asm
33 *          java.base/jdk.internal.org.objectweb.asm.tree
34 *          jdk.vm.ci/jdk.vm.ci.hotspot
35 *          jdk.vm.ci/jdk.vm.ci.code
36 * @build jdk.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper
37 * @build compiler.jvmci.compilerToVM.GetStackTraceElementTest
38 * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
39 *                   compiler.jvmci.compilerToVM.GetStackTraceElementTest
40 */
41
42package compiler.jvmci.compilerToVM;
43
44import compiler.jvmci.common.CTVMUtilities;
45import compiler.jvmci.common.testcases.TestCase;
46import jdk.test.lib.Asserts;
47import jdk.vm.ci.hotspot.CompilerToVMHelper;
48import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
49
50import java.lang.reflect.Executable;
51import java.lang.reflect.Method;
52import java.lang.reflect.Modifier;
53import java.util.HashMap;
54import java.util.Map;
55
56public class GetStackTraceElementTest {
57
58    public static void main(String[] args) {
59        Map<Executable, int[]> testCases = createTestCases();
60        testCases.forEach(GetStackTraceElementTest::runSanityTest);
61    }
62
63    private static void runSanityTest(Executable aMethod, int[] bcis) {
64        HotSpotResolvedJavaMethod method = CTVMUtilities
65                .getResolvedMethod(aMethod);
66        String className = aMethod.getDeclaringClass().getName();
67        String methodName = aMethod.getName().equals(className)
68                ? "<init>"
69                : aMethod.getName();
70        String fileName = getFileName(className);
71        Map<Integer, Integer> bciWithLineNumber = CTVMUtilities
72                .getBciToLineNumber(aMethod);
73        boolean isNative = Modifier.isNative(aMethod.getModifiers());
74        int lineNumber = -1;
75        for (int bci : bcis) {
76            StackTraceElement ste = CompilerToVMHelper
77                    .getStackTraceElement(method, bci);
78            Asserts.assertNotNull(ste, aMethod + " : got null StackTraceElement"
79                    + " at bci " + bci);
80            Asserts.assertEQ(className, ste.getClassName(), aMethod
81                    + " : unexpected class name");
82            Asserts.assertEQ(fileName, ste.getFileName(), aMethod
83                    + " : unexpected filename");
84            Asserts.assertEQ(methodName, ste.getMethodName(), aMethod
85                    + " : unexpected method name");
86            Asserts.assertEQ(isNative, ste.isNativeMethod(), aMethod
87                    + " : unexpected 'isNative' value");
88            if (bciWithLineNumber.size() > 0) {
89                if (bciWithLineNumber.containsKey(bci)) {
90                    lineNumber = bciWithLineNumber.get(bci);
91                }
92                Asserts.assertEQ(lineNumber, ste.getLineNumber(), aMethod
93                        + " : unexpected line number");
94            } else {
95                // native and abstract function
96                Asserts.assertGT(0, ste.getLineNumber(),
97                        aMethod + " : unexpected line number for abstract "
98                                + "or native method");
99            }
100        }
101
102    }
103
104    private static String getFileName(String className) {
105        int lastDot = className.lastIndexOf('.');
106        int firstDol = className.contains("$")
107                ? className.indexOf('$')
108                : className.length();
109        return className.substring(lastDot + 1, firstDol) + ".java";
110    }
111
112    private static Map<Executable, int[]> createTestCases() {
113        Map<Executable, int[]> testCases = new HashMap<>();
114
115        try {
116            Class<?> aClass = DummyClass.class;
117            Method aMethod = aClass.getDeclaredMethod("dummyInstanceFunction");
118            int[] bci = new int[] {0, 2, 3, 6, 7, 8, 11, 13, 15, 16, 17, 18};
119            testCases.put(aMethod, bci);
120
121            aMethod = aClass.getDeclaredMethod("dummyEmptyFunction");
122            bci = new int[] {0};
123            testCases.put(aMethod, bci);
124
125            aMethod = aClass.getDeclaredMethod("nativeFunction");
126            bci = new int[] {0};
127            testCases.put(aMethod, bci);
128
129            TestCase.getAllExecutables()
130                    .forEach(c -> testCases.put(c, new int[] {0}));
131        } catch (NoSuchMethodException e) {
132            throw new Error("TEST BUG : test method not found", e);
133        }
134        return testCases;
135    }
136
137    private class DummyClass {
138        public int dummyInstanceFunction() {
139            String str1 = "123123123";
140            double x = 3.14;
141            int y = Integer.parseInt(str1);
142
143            return y / (int)x;
144        }
145
146        public void dummyEmptyFunction() {}
147
148        public native void nativeFunction();
149    }
150}
151