Path: blob/master/test/hotspot/jtreg/compiler/jvmci/compilerToVM/GetStackTraceElementTest.java
41153 views
/*1* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 813642126* @requires vm.jvmci27* @library /test/lib /28* @library ../common/patches29* @modules java.base/jdk.internal.misc30* @modules java.base/jdk.internal.org.objectweb.asm31* java.base/jdk.internal.org.objectweb.asm.tree32* jdk.internal.vm.ci/jdk.vm.ci.hotspot33* jdk.internal.vm.ci/jdk.vm.ci.code34* jdk.internal.vm.ci/jdk.vm.ci.meta35* jdk.internal.vm.ci/jdk.vm.ci.runtime36*37* @build jdk.internal.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper38* @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI39* -XX:-UseJVMCICompiler40* compiler.jvmci.compilerToVM.GetStackTraceElementTest41*/4243package compiler.jvmci.compilerToVM;4445import compiler.jvmci.common.CTVMUtilities;46import compiler.jvmci.common.testcases.TestCase;47import jdk.test.lib.Asserts;48import jdk.vm.ci.hotspot.CompilerToVMHelper;49import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;5051import java.lang.reflect.Executable;52import java.lang.reflect.Method;53import java.lang.reflect.Modifier;54import java.util.HashMap;55import java.util.Map;5657public class GetStackTraceElementTest {5859public static void main(String[] args) {60Map<Executable, int[]> testCases = createTestCases();61testCases.forEach(GetStackTraceElementTest::runSanityTest);62}6364private static void runSanityTest(Executable aMethod, int[] bcis) {65HotSpotResolvedJavaMethod method = CTVMUtilities66.getResolvedMethod(aMethod);67String className = aMethod.getDeclaringClass().getName();68String methodName = aMethod.getName().equals(className)69? "<init>"70: aMethod.getName();71String fileName = getFileName(className);72Map<Integer, Integer> bciWithLineNumber = CTVMUtilities73.getBciToLineNumber(aMethod);74boolean isNative = Modifier.isNative(aMethod.getModifiers());75int lineNumber = -1;76for (int bci : bcis) {77StackTraceElement ste = CompilerToVMHelper78.getStackTraceElement(method, bci);79Asserts.assertNotNull(ste, aMethod + " : got null StackTraceElement"80+ " at bci " + bci);81Asserts.assertEQ(className, ste.getClassName(), aMethod82+ " : unexpected class name");83Asserts.assertEQ(fileName, ste.getFileName(), aMethod84+ " : unexpected filename");85Asserts.assertEQ(methodName, ste.getMethodName(), aMethod86+ " : unexpected method name");87Asserts.assertEQ(isNative, ste.isNativeMethod(), aMethod88+ " : unexpected 'isNative' value");89if (bciWithLineNumber.size() > 0) {90if (bciWithLineNumber.containsKey(bci)) {91lineNumber = bciWithLineNumber.get(bci);92}93Asserts.assertEQ(lineNumber, ste.getLineNumber(), aMethod94+ " : unexpected line number");95} else {96// native and abstract function97Asserts.assertGT(0, ste.getLineNumber(),98aMethod + " : unexpected line number for abstract "99+ "or native method");100}101}102103}104105private static String getFileName(String className) {106int lastDot = className.lastIndexOf('.');107int firstDol = className.contains("$")108? className.indexOf('$')109: className.length();110return className.substring(lastDot + 1, firstDol) + ".java";111}112113private static Map<Executable, int[]> createTestCases() {114Map<Executable, int[]> testCases = new HashMap<>();115116try {117Class<?> aClass = DummyClass.class;118Method aMethod = aClass.getDeclaredMethod("dummyInstanceFunction");119int[] bci = new int[] {0, 2, 3, 6, 7, 8, 11, 13, 15, 16, 17, 18};120testCases.put(aMethod, bci);121122aMethod = aClass.getDeclaredMethod("dummyEmptyFunction");123bci = new int[] {0};124testCases.put(aMethod, bci);125126aMethod = aClass.getDeclaredMethod("nativeFunction");127bci = new int[] {0};128testCases.put(aMethod, bci);129130TestCase.getAllExecutables()131.forEach(c -> testCases.put(c, new int[] {0}));132} catch (NoSuchMethodException e) {133throw new Error("TEST BUG : test method not found", e);134}135return testCases;136}137138private class DummyClass {139public int dummyInstanceFunction() {140String str1 = "123123123";141double x = 3.14;142int y = Integer.parseInt(str1);143144return y / (int)x;145}146147public void dummyEmptyFunction() {}148149public native void nativeFunction();150}151}152153154