comparison agent/src/share/classes/sun/jvm/hotspot/asm/Disassembler.java @ 6782:5a98bf7d847b

6879063: SA should use hsdis for disassembly Summary: We should in SA to use hsdis for it like the JVM does to replace the current java based disassembler. Reviewed-by: twisti, jrose, sla Contributed-by: yumin.qi@oracle.com
author minqi
date Mon, 24 Sep 2012 12:44:00 -0700
parents c18cbe5936b8
children 763705f0fec3
comparison
equal deleted inserted replaced
6780:8440414b0fd8 6782:5a98bf7d847b
1 /* 1 /*
2 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 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 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
22 * 22 *
23 */ 23 */
24 24
25 package sun.jvm.hotspot.asm; 25 package sun.jvm.hotspot.asm;
26 26
27 public abstract class Disassembler { 27 import java.io.PrintStream;
28 protected long startPc; 28 import java.util.Observer;
29 import java.util.Observable;
30 import sun.jvm.hotspot.code.CodeBlob;
31 import sun.jvm.hotspot.code.NMethod;
32 import sun.jvm.hotspot.debugger.Address;
33 import sun.jvm.hotspot.runtime.VM;
34
35 public class Disassembler {
36 private static String options = "";
37 private static long decode_function;
38
39 protected long startPc;
29 protected byte[] code; 40 protected byte[] code;
41 private CodeBlob blob;
42 private NMethod nmethod;
30 43
31 public Disassembler(long startPc, byte[] code) { 44 public static void decode(InstructionVisitor visitor, CodeBlob blob) {
45 decode(visitor, blob, blob.codeBegin(), blob.codeEnd());
46 }
47
48 public static void decode(InstructionVisitor visitor, CodeBlob blob, Address begin, Address end) {
49 int codeSize = (int)end.minus(begin);
50 long startPc = VM.getAddressValue(begin);
51 byte[] code = new byte[codeSize];
52 for (int i = 0; i < code.length; i++)
53 code[i] = begin.getJByteAt(i);
54 Disassembler dis = new Disassembler(startPc, code);
55 dis.decode(visitor);
56 }
57
58 private Disassembler(long startPc, byte[] code) {
32 this.startPc = startPc; 59 this.startPc = startPc;
33 this.code = code; 60 this.code = code;
61
62 // Lazily load hsdis
63 if (decode_function == 0) {
64 StringBuilder path = new StringBuilder(System.getProperty("java.home"));
65 String sep = System.getProperty("file.separator");
66 String os = System.getProperty("os.name");
67 String libname = "hsdis";
68 String arch = System.getProperty("os.arch");
69 if (os.lastIndexOf("Windows", 0) != -1) {
70 path.append(sep + "bin" + sep);
71 libname += ".dll";
72 } else if (os.lastIndexOf("SunOS", 0) != -1) {
73 if (arch.equals("x86") || arch.equals("i386")) {
74 path.append(sep + "lib" + sep + "i386" + sep);
75 libname += "-i386" + ".so";
76 } else if (arch.equals("amd64")) {
77 path.append(sep + "lib" + sep + "amd64" + sep);
78 libname += "-amd64" + ".so";
79 } else {
80 path.append(sep + "lib" + sep + arch + sep);
81 libname += "-" + arch + ".so";
82 }
83 } else if (os.lastIndexOf("Linux", 0) != -1) {
84 if (arch.equals("x86") || arch.equals("i386")) {
85 path.append(sep + "lib" + sep + "i386" + sep);
86 libname += "-i386.so";
87 } else if (arch.equals("amd64") || arch.equals("x86_64")) {
88 path.append(sep + "lib" + sep + "amd64" + sep);
89 libname += "-amd64.so";
90 } else {
91 path.append(sep + "lib" + sep + arch + sep);
92 libname += "-" + arch + ".so";
93 }
94 } else if (os.lastIndexOf("Mac OS X", 0) != -1) {
95 path.append(sep + "lib" + sep);
96 libname += "-amd64" + ".dylib"; // x86_64 => amd64
97 } else {
98 path.append(sep + "lib" + sep + "arch" + sep);
99 libname += "-" + arch + ".so";
100 }
101 decode_function = load_library(path.toString(), libname);
102 }
34 } 103 }
35 104
36 public long getStartPC() { 105 private static native long load_library(String installed_jrepath, String hsdis_library_name);
37 return startPc; 106
107 private native void decode(InstructionVisitor visitor, long pc, byte[] code,
108 String options, long decode_function);
109
110 private void decode(InstructionVisitor visitor) {
111 visitor.prologue();
112 decode(visitor, startPc, code, options, decode_function);
113 visitor.epilogue();
38 } 114 }
39 115
40 public byte[] getCode() { 116 private boolean match(String event, String tag) {
41 return code; 117 if (!event.startsWith(tag))
118 return false;
119 int taglen = tag.length();
120 if (taglen == event.length()) return true;
121 char delim = event.charAt(taglen);
122 return delim == ' ' || delim == '/' || delim == '=';
42 } 123 }
43 124
44 public abstract void decode(InstructionVisitor visitor); 125 // This is called from the native code to process various markers
126 // in the dissassembly.
127 private long handleEvent(InstructionVisitor visitor, String event, long arg) {
128 if (match(event, "insn")) {
129 try {
130 visitor.beginInstruction(arg);
131 } catch (Throwable e) {
132 e.printStackTrace();
133 }
134 } else if (match(event, "/insn")) {
135 try {
136 visitor.endInstruction(arg);
137 } catch (Throwable e) {
138 e.printStackTrace();
139 }
140 } else if (match(event, "addr")) {
141 if (arg != 0) {
142 visitor.printAddress(arg);
143 }
144 return arg;
145 } else if (match(event, "mach")) {
146 // output().printf("[Disassembling for mach='%s']\n", arg);
147 } else {
148 // ignore unrecognized markup
149 }
150 return 0;
151 }
152
153 // This called from the native code to perform printing
154 private void rawPrint(InstructionVisitor visitor, String s) {
155 visitor.print(s);
156 }
45 } 157 }