--- old/agent/src/share/classes/sun/jvm/hotspot/asm/Disassembler.java Thu Sep 17 16:09:23 2009 +++ new/agent/src/share/classes/sun/jvm/hotspot/asm/Disassembler.java Thu Sep 17 16:09:23 2009 @@ -24,22 +24,123 @@ package sun.jvm.hotspot.asm; -public abstract class Disassembler { +import java.io.PrintStream; +import java.util.Observer; +import java.util.Observable; +import sun.jvm.hotspot.code.CodeBlob; +import sun.jvm.hotspot.code.NMethod; +import sun.jvm.hotspot.debugger.Address; +import sun.jvm.hotspot.runtime.VM; + +public class Disassembler { + static final int COMMENT_COLUMN = 40; + static final String BYTES_COMMENT = ";..."; /* funky byte display comment */ + + private static String libname; + private static String options; + private static long decode_function; + + static { + VM.registerVMInitializedObserver(new Observer() { + public void update(Observable o, Object data) { + options = ""; + libname = null; + String cpu = VM.getVM().getCPU(); + if (cpu.equals("sparc")) { + libname = "hsdis-sparc"; + if (VM.getVM().isLP64()) { + options = "v9only"; + } + } else if (cpu.equals("x86")) { + libname = "hsdis-i386"; + } else if (cpu.equals("amd64")) { + libname = "hsdis-amd64"; + } else if (cpu.equals("ia64")) { + libname = "hsdis-ia64"; + } + } + }); + } + protected long startPc; protected byte[] code; + private CodeBlob blob; + private NMethod nmethod; - public Disassembler(long startPc, byte[] code) { + public static void decode(InstructionVisitor visitor, CodeBlob blob) { + decode(visitor, blob, blob.instructionsBegin(), blob.instructionsEnd()); + } + + public static void decode(InstructionVisitor visitor, CodeBlob blob, Address begin, Address end) { + int codeSize = (int)end.minus(begin); + long startPc = VM.getAddressValue(begin); + byte[] code = new byte[codeSize]; + for (int i = 0; i < code.length; i++) + code[i] = begin.getJByteAt(i); + Disassembler dis = new Disassembler(startPc, code); + dis.decode(visitor); + } + + private Disassembler(long startPc, byte[] code) { this.startPc = startPc; this.code = code; + + // Lazily load hsdis + if (decode_function == 0) { + decode_function = load_library(libname); + } } - public long getStartPC() { - return startPc; + private static native long load_library(String hsdis_library_name); + + private native void decode(InstructionVisitor visitor, long pc, byte[] code, + String options, long decode_function); + + private void decode(InstructionVisitor visitor) { + visitor.prologue(); + decode(visitor, startPc, code, options, decode_function); + visitor.epilogue(); } - public byte[] getCode() { - return code; + private boolean match(String event, String tag) { + if (!event.startsWith(tag)) + return false; + int taglen = tag.length(); + if (taglen == event.length()) return true; + char delim = event.charAt(taglen); + return delim == ' ' || delim == '/' || delim == '='; } - public abstract void decode(InstructionVisitor visitor); + // This is called from the native code to process various markers + // in the dissassembly. + private long handleEvent(InstructionVisitor visitor, String event, long arg) { + if (match(event, "insn")) { + try { + visitor.beginInstruction(arg); + } catch (Throwable e) { + e.printStackTrace(); + } + } else if (match(event, "/insn")) { + try { + visitor.endInstruction(arg); + } catch (Throwable e) { + e.printStackTrace(); + } + } else if (match(event, "addr")) { + if (arg != 0) { + visitor.printAddress(arg); + } + return arg; + } else if (match(event, "mach")) { + // output().printf("[Disassembling for mach='%s']\n", arg); + } else { + // ignore unrecognized markup + } + return 0; + } + + // This called from the native code to perform printing + private void rawPrint(InstructionVisitor visitor, String s) { + visitor.print(s); + } }