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:. 34 * compiler.jvmci.compilerToVM.GetLineNumberTableTest 35 */ 36 37 package compiler.jvmci.compilerToVM; 38 39 import compiler.jvmci.common.CTVMUtilities; 40 import compiler.jvmci.common.testcases.TestCase; 41 import jdk.vm.ci.hotspot.CompilerToVMHelper; 42 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod; 43 import jdk.internal.org.objectweb.asm.ClassReader; 44 import jdk.internal.org.objectweb.asm.ClassVisitor; 45 import jdk.internal.org.objectweb.asm.ClassWriter; 46 import jdk.internal.org.objectweb.asm.Label; 47 import jdk.internal.org.objectweb.asm.MethodVisitor; 48 import jdk.internal.org.objectweb.asm.Opcodes; 49 import jdk.internal.org.objectweb.asm.tree.ClassNode; 50 import jdk.test.lib.Asserts; 51 import jdk.test.lib.Utils; 52 53 import java.io.IOException; 54 import java.lang.reflect.Constructor; 55 import java.lang.reflect.Executable; 56 import java.lang.reflect.Method; 57 import java.lang.reflect.Parameter; 58 import java.util.Arrays; 59 import java.util.HashMap; 60 import java.util.Map; 61 import java.util.TreeMap; 62 63 public class GetLineNumberTableTest { 64 public static void main(String[] args) { 65 TestCase.getAllExecutables() 66 .forEach(GetLineNumberTableTest::runSanityTest); 67 } 68 69 public static void runSanityTest(Executable aMethod) { 70 HotSpotResolvedJavaMethod method = CTVMUtilities 71 .getResolvedMethod(aMethod); 72 long[] lineNumbers = CompilerToVMHelper.getLineNumberTable(method); 73 long[] expectedLineNumbers = getExpectedLineNumbers(aMethod); 74 75 Asserts.assertTrue(Arrays.equals(lineNumbers, expectedLineNumbers), 76 String.format("%s : unequal table values : %n%s%n%s%n", 77 aMethod, 78 Arrays.toString(lineNumbers), 79 Arrays.toString(expectedLineNumbers))); 80 } 81 82 public static long[] getExpectedLineNumbers(Executable aMethod) { 83 try { 84 ClassReader cr = new ClassReader(aMethod.getDeclaringClass() 85 .getName()); 86 ClassNode cn = new ClassNode(); 87 cr.accept(cn, ClassReader.EXPAND_FRAMES); 88 89 Map<Label, Integer> lineNumbers = new HashMap<>(); 90 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); 91 ClassVisitor cv = new ClassVisitorForLabels(cw, lineNumbers, 92 aMethod); 93 cr.accept(cv, ClassReader.EXPAND_FRAMES); 94 95 long[] result = null; 96 if (!lineNumbers.isEmpty()) { 97 Map<Integer, Integer> labels = new TreeMap<>(); 98 lineNumbers.forEach((k, v) -> labels.put(k.getOffset(), v)); 99 100 result = new long[2 * labels.size()]; 101 int i = 0; 102 for (Integer key : labels.keySet()) { 103 result[i++] = key.longValue(); 104 result[i++] = labels.get(key).longValue(); 105 } 106 } 107 // compilerToVM::getLineNumberTable returns null in case empty table 108 return result; 109 } catch (IOException e) { 110 throw new Error("TEST BUG " + e, e); 111 } 112 } 113 114 private static class ClassVisitorForLabels extends ClassVisitor { 115 private final Map<Label, Integer> lineNumbers; 116 private final String targetName; 117 private final String targetDesc; 118 119 public ClassVisitorForLabels(ClassWriter cw, Map<Label, Integer> lines, 120 Executable target) { 121 super(Opcodes.ASM5, cw); 122 this.lineNumbers = lines; 123 124 StringBuilder builder = new StringBuilder("("); 125 for (Parameter parameter : target.getParameters()) { 126 builder.append(Utils.toJVMTypeSignature(parameter.getType())); 127 } 128 builder.append(")"); 129 if (target instanceof Constructor) { 130 targetName = "<init>"; 131 builder.append("V"); 132 } else { 133 targetName = target.getName(); 134 builder.append(Utils.toJVMTypeSignature( 135 ((Method) target).getReturnType())); 136 } 137 targetDesc = builder.toString(); 138 } 139 140 @Override 141 public final MethodVisitor visitMethod(int access, String name, 142 String desc, String signature, 143 String[] exceptions) { 144 MethodVisitor mv = cv.visitMethod(access, name, desc, signature, 145 exceptions); 146 if (targetDesc.equals(desc) && targetName.equals(name)) { 147 return new MethodVisitor(Opcodes.ASM5, mv) { 148 @Override 149 public void visitLineNumber(int i, Label label) { 150 super.visitLineNumber(i, label); 151 lineNumbers.put(label, i); 152 } 153 }; 154 } 155 return mv; 156 } 157 } 158 }