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.simpleArch == "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 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethodImpl;
  45 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  46 import jdk.test.lib.Asserts;
  47 
  48 public class GetStackTraceElementTest {
  49 
  50     public static void main(String[] args) {
  51         Map<Executable, int[]> testCases = createTestCases();
  52         testCases.forEach(GetStackTraceElementTest::runSanityTest);
  53     }
  54 
  55     private static void runSanityTest(Executable aMethod, int[] bcis) {
  56         HotSpotResolvedJavaMethodImpl method = CTVMUtilities
  57                 .getResolvedMethod(aMethod);
  58         String className = aMethod.getDeclaringClass().getName();
  59         int lastDot = className.lastIndexOf('.');
  60         int firstDol = className.contains("$")
  61                 ? className.indexOf('$')
  62                 : className.length();
  63         String fileName = className.substring(lastDot + 1, firstDol) + ".java";
  64         for (int bci : bcis) {
  65             StackTraceElement ste = CompilerToVMHelper
  66                     .getStackTraceElement(method, bci);
  67             Asserts.assertNotNull(ste);
  68             Asserts.assertEQ(ste.getClassName(), className);
  69             Asserts.assertEQ(ste.getFileName(), fileName);
  70             Asserts.assertEQ(ste.getMethodName(), aMethod.getName());
  71             Asserts.assertEQ(ste.isNativeMethod(), Modifier
  72                     .isNative(aMethod.getModifiers()));
  73         }
  74 
  75     }
  76 
  77     private static Map<Executable, int[]> createTestCases() {
  78         Map<Executable, int[]> testCases = new HashMap<>();
  79 
  80         try {
  81             Class<?> aClass = DummyClass.class;
  82             Method aMethod = aClass.getDeclaredMethod("dummyInstanceFunction");
  83             int[] bci = new int[] {0, 2, 3, 6, 7, 8, 11, 13, 15, 16, 17, 18};
  84             testCases.put(aMethod, bci);
  85 
  86             aMethod = aClass.getDeclaredMethod("dummyEmptyFunction");
  87             bci = new int[] {0};
  88             testCases.put(aMethod, bci);
  89         } catch (NoSuchMethodException e) {
  90             throw new Error("TEST BUG : test method not found", e);
  91         }
  92         return testCases;
  93     }
  94 
  95     private class DummyClass {
  96         public int dummyInstanceFunction() {
  97             String str1 = "123123123";
  98             double x = 3.14;
  99             int y = Integer.parseInt(str1);
 100 
 101             return y / (int)x;
 102         }
 103 
 104         public void dummyEmptyFunction() {}
 105     }
 106 }