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