1/*
2 * Copyright (c) 2015, 2016, 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 * @test
26 * @bug 8136421
27 * @requires vm.jvmci
28 * @library /test/lib /
29 * @library ../common/patches
30 * @modules java.base/jdk.internal.misc
31 * @modules java.base/jdk.internal.org.objectweb.asm
32 *          java.base/jdk.internal.org.objectweb.asm.tree
33 *          jdk.internal.vm.ci/jdk.vm.ci.hotspot
34 *          jdk.internal.vm.ci/jdk.vm.ci.code
35 * @build jdk.internal.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper
36 * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
37 *                   -Djvmci.Compiler=null
38 *                   compiler.jvmci.compilerToVM.GetStackTraceElementTest
39 */
40
41package compiler.jvmci.compilerToVM;
42
43import compiler.jvmci.common.CTVMUtilities;
44import compiler.jvmci.common.testcases.TestCase;
45import jdk.test.lib.Asserts;
46import jdk.vm.ci.hotspot.CompilerToVMHelper;
47import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
48
49import java.lang.reflect.Executable;
50import java.lang.reflect.Method;
51import java.lang.reflect.Modifier;
52import java.util.HashMap;
53import java.util.Map;
54
55public class GetStackTraceElementTest {
56
57    public static void main(String[] args) {
58        Map<Executable, int[]> testCases = createTestCases();
59        testCases.forEach(GetStackTraceElementTest::runSanityTest);
60    }
61
62    private static void runSanityTest(Executable aMethod, int[] bcis) {
63        HotSpotResolvedJavaMethod method = CTVMUtilities
64                .getResolvedMethod(aMethod);
65        String className = aMethod.getDeclaringClass().getName();
66        String methodName = aMethod.getName().equals(className)
67                ? "<init>"
68                : aMethod.getName();
69        String fileName = getFileName(className);
70        Map<Integer, Integer> bciWithLineNumber = CTVMUtilities
71                .getBciToLineNumber(aMethod);
72        boolean isNative = Modifier.isNative(aMethod.getModifiers());
73        int lineNumber = -1;
74        for (int bci : bcis) {
75            StackTraceElement ste = CompilerToVMHelper
76                    .getStackTraceElement(method, bci);
77            Asserts.assertNotNull(ste, aMethod + " : got null StackTraceElement"
78                    + " at bci " + bci);
79            Asserts.assertEQ(className, ste.getClassName(), aMethod
80                    + " : unexpected class name");
81            Asserts.assertEQ(fileName, ste.getFileName(), aMethod
82                    + " : unexpected filename");
83            Asserts.assertEQ(methodName, ste.getMethodName(), aMethod
84                    + " : unexpected method name");
85            Asserts.assertEQ(isNative, ste.isNativeMethod(), aMethod
86                    + " : unexpected 'isNative' value");
87            if (bciWithLineNumber.size() > 0) {
88                if (bciWithLineNumber.containsKey(bci)) {
89                    lineNumber = bciWithLineNumber.get(bci);
90                }
91                Asserts.assertEQ(lineNumber, ste.getLineNumber(), aMethod
92                        + " : unexpected line number");
93            } else {
94                // native and abstract function
95                Asserts.assertGT(0, ste.getLineNumber(),
96                        aMethod + " : unexpected line number for abstract "
97                                + "or native method");
98            }
99        }
100
101    }
102
103    private static String getFileName(String className) {
104        int lastDot = className.lastIndexOf('.');
105        int firstDol = className.contains("$")
106                ? className.indexOf('$')
107                : className.length();
108        return className.substring(lastDot + 1, firstDol) + ".java";
109    }
110
111    private static Map<Executable, int[]> createTestCases() {
112        Map<Executable, int[]> testCases = new HashMap<>();
113
114        try {
115            Class<?> aClass = DummyClass.class;
116            Method aMethod = aClass.getDeclaredMethod("dummyInstanceFunction");
117            int[] bci = new int[] {0, 2, 3, 6, 7, 8, 11, 13, 15, 16, 17, 18};
118            testCases.put(aMethod, bci);
119
120            aMethod = aClass.getDeclaredMethod("dummyEmptyFunction");
121            bci = new int[] {0};
122            testCases.put(aMethod, bci);
123
124            aMethod = aClass.getDeclaredMethod("nativeFunction");
125            bci = new int[] {0};
126            testCases.put(aMethod, bci);
127
128            TestCase.getAllExecutables()
129                    .forEach(c -> testCases.put(c, new int[] {0}));
130        } catch (NoSuchMethodException e) {
131            throw new Error("TEST BUG : test method not found", e);
132        }
133        return testCases;
134    }
135
136    private class DummyClass {
137        public int dummyInstanceFunction() {
138            String str1 = "123123123";
139            double x = 3.14;
140            int y = Integer.parseInt(str1);
141
142            return y / (int)x;
143        }
144
145        public void dummyEmptyFunction() {}
146
147        public native void nativeFunction();
148    }
149}
150