Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/interpreter/BytecodeDisassembler.java
41161 views
/*1* Copyright (c) 2002, 2020, 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*22*/2324package sun.jvm.hotspot.interpreter;2526import java.util.*;27import java.lang.reflect.Constructor;28import sun.jvm.hotspot.oops.*;29import sun.jvm.hotspot.utilities.*;3031public class BytecodeDisassembler {32private Method method;3334private static Map<Integer, Class> bytecode2Class = new HashMap<>();3536private static void addBytecodeClass(int bytecode, Class clazz) {37bytecode2Class.put(bytecode, clazz);38}3940private static Class getBytecodeClass(int bytecode) {41return (Class) bytecode2Class.get(bytecode);42}4344static {45addBytecodeClass(Bytecodes._anewarray, BytecodeANewArray.class);46addBytecodeClass(Bytecodes._bipush, BytecodeBipush.class);47addBytecodeClass(Bytecodes._checkcast, BytecodeCheckCast.class);48addBytecodeClass(Bytecodes._getfield, BytecodeGetField.class);49addBytecodeClass(Bytecodes._getstatic, BytecodeGetStatic.class);50addBytecodeClass(Bytecodes._goto, BytecodeGoto.class);51addBytecodeClass(Bytecodes._goto_w, BytecodeGotoW.class);52addBytecodeClass(Bytecodes._ifeq, BytecodeIf.class);53addBytecodeClass(Bytecodes._ifne, BytecodeIf.class);54addBytecodeClass(Bytecodes._iflt, BytecodeIf.class);55addBytecodeClass(Bytecodes._ifge, BytecodeIf.class);56addBytecodeClass(Bytecodes._ifgt, BytecodeIf.class);57addBytecodeClass(Bytecodes._ifle, BytecodeIf.class);58addBytecodeClass(Bytecodes._if_icmpeq, BytecodeIf.class);59addBytecodeClass(Bytecodes._if_icmpne, BytecodeIf.class);60addBytecodeClass(Bytecodes._if_icmplt, BytecodeIf.class);61addBytecodeClass(Bytecodes._if_icmpge, BytecodeIf.class);62addBytecodeClass(Bytecodes._if_icmpgt, BytecodeIf.class);63addBytecodeClass(Bytecodes._if_icmple, BytecodeIf.class);64addBytecodeClass(Bytecodes._if_acmpeq, BytecodeIf.class);65addBytecodeClass(Bytecodes._if_acmpne, BytecodeIf.class);66addBytecodeClass(Bytecodes._ifnull, BytecodeIf.class);67addBytecodeClass(Bytecodes._ifnonnull, BytecodeIf.class);68addBytecodeClass(Bytecodes._iinc, BytecodeIinc.class);69addBytecodeClass(Bytecodes._instanceof, BytecodeInstanceOf.class);70addBytecodeClass(Bytecodes._invokevirtual, BytecodeInvoke.class);71addBytecodeClass(Bytecodes._invokestatic, BytecodeInvoke.class);72addBytecodeClass(Bytecodes._invokespecial, BytecodeInvoke.class);73addBytecodeClass(Bytecodes._invokeinterface, BytecodeInvoke.class);74addBytecodeClass(Bytecodes._invokedynamic, BytecodeInvoke.class);75addBytecodeClass(Bytecodes._jsr, BytecodeJsr.class);76addBytecodeClass(Bytecodes._jsr_w, BytecodeJsrW.class);77addBytecodeClass(Bytecodes._iload, BytecodeLoad.class);78addBytecodeClass(Bytecodes._lload, BytecodeLoad.class);79addBytecodeClass(Bytecodes._fload, BytecodeLoad.class);80addBytecodeClass(Bytecodes._dload, BytecodeLoad.class);81addBytecodeClass(Bytecodes._aload, BytecodeLoad.class);82addBytecodeClass(Bytecodes._ldc, BytecodeLoadConstant.class);83addBytecodeClass(Bytecodes._ldc_w, BytecodeLoadConstant.class);84addBytecodeClass(Bytecodes._ldc2_w, BytecodeLoadConstant.class);85addBytecodeClass(Bytecodes._lookupswitch, BytecodeLookupswitch.class);86addBytecodeClass(Bytecodes._multianewarray, BytecodeMultiANewArray.class);87addBytecodeClass(Bytecodes._new, BytecodeNew.class);88addBytecodeClass(Bytecodes._newarray, BytecodeNewArray.class);89addBytecodeClass(Bytecodes._putfield, BytecodePutField.class);90addBytecodeClass(Bytecodes._putstatic, BytecodePutStatic.class);91addBytecodeClass(Bytecodes._ret, BytecodeRet.class);92addBytecodeClass(Bytecodes._sipush, BytecodeSipush.class);93addBytecodeClass(Bytecodes._istore, BytecodeStore.class);94addBytecodeClass(Bytecodes._lstore, BytecodeStore.class);95addBytecodeClass(Bytecodes._fstore, BytecodeStore.class);96addBytecodeClass(Bytecodes._dstore, BytecodeStore.class);97addBytecodeClass(Bytecodes._astore, BytecodeStore.class);98addBytecodeClass(Bytecodes._tableswitch, BytecodeTableswitch.class);99}100101public BytecodeDisassembler(Method method) {102this.method = method;103}104105public Method getMethod() {106return method;107}108109public void decode(BytecodeVisitor visitor) {110visitor.prologue(method);111112BytecodeStream stream = new BytecodeStream(method);113int javacode = Bytecodes._illegal;114while ( (javacode = stream.next()) != Bytecodes._illegal) {115// look for special Bytecode class116int bci = stream.bci();117int hotspotcode = method.getBytecodeOrBPAt(bci);118Class<?> clazz = getBytecodeClass(javacode);119if (clazz == null) {120// check for fast_(i|a)_access_0121clazz = getBytecodeClass(hotspotcode);122if (clazz == null) {123// use generic bytecode class124clazz = Bytecode.class;125}126}127128// All bytecode classes must have a constructor with signature129// (Lsun/jvm/hotspot/oops/Method;I)V130131Constructor cstr = null;132try {133cstr = clazz.getDeclaredConstructor(new Class[] { Method.class, Integer.TYPE });134} catch(NoSuchMethodException nomethod) {135if (Assert.ASSERTS_ENABLED) {136Assert.that(false, "Bytecode class without proper constructor!");137}138}139140Bytecode bytecodeObj = null;141try {142bytecodeObj = (Bytecode)cstr.newInstance(new Object[] { method, bci});143} catch (Exception exp) {144if (Assert.ASSERTS_ENABLED) {145Assert.that(false, "Bytecode instance of class "146+ clazz.getName() + " can not be created!");147}148}149150if (stream.isWide()) {151visitor.visit(new Bytecode(method, bci - 1));152}153154try {155visitor.visit(bytecodeObj);156} catch(ClassCastException castfail) {157castfail.printStackTrace();158System.err.println(method.getAddress() + " " + bci);159}160}161162visitor.epilogue();163}164}165166167