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