annotate agent/src/share/classes/sun/jvm/hotspot/tools/jcore/ClassDump.java @ 6641:a9fed06c01d2

7154641: Servicability agent should work on platforms other than x86, sparc Summary: Added capability to load support classes for other cpus Reviewed-by: coleenp, bobv, sla Contributed-by: Bill Pittore <bill.pittore@oracle.com>
author bpittore
date Thu, 30 Aug 2012 11:20:01 -0400
parents f6f3bb0ee072
children 38ea2efa32a7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
3939
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
2 * Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 844
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 844
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 844
diff changeset
21 * questions.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 package sun.jvm.hotspot.tools.jcore;
a61af66fc99e Initial load
duke
parents:
diff changeset
26
a61af66fc99e Initial load
duke
parents:
diff changeset
27 import java.io.*;
3939
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
28 import java.util.jar.JarOutputStream;
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
29 import java.util.jar.JarEntry;
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
30 import java.util.jar.Manifest;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
31 import sun.jvm.hotspot.memory.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
32 import sun.jvm.hotspot.oops.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
33 import sun.jvm.hotspot.debugger.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
34 import sun.jvm.hotspot.runtime.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
35 import sun.jvm.hotspot.tools.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
36
a61af66fc99e Initial load
duke
parents:
diff changeset
37 public class ClassDump extends Tool {
a61af66fc99e Initial load
duke
parents:
diff changeset
38 private ClassFilter classFilter;
a61af66fc99e Initial load
duke
parents:
diff changeset
39 private String outputDirectory;
3939
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
40 private JarOutputStream jarStream;
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
41
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
42 public void setClassFilter(ClassFilter cf) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
43 classFilter = cf;
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
44 }
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
45
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
46 public void setOutputDirectory(String od) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
47 outputDirectory = od;
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
48 if (jarStream != null) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
49 try {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
50 jarStream.close();
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
51 } catch (IOException ioe) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
52 ioe.printStackTrace();
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
53 }
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
54 }
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
55 jarStream = null;
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
56 }
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
57
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
58 public void setJarOutput(String jarFileName) throws IOException {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
59 jarStream = new JarOutputStream(new FileOutputStream(jarFileName), new Manifest());
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
60 outputDirectory = null;
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
61 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
62
a61af66fc99e Initial load
duke
parents:
diff changeset
63 public void run() {
a61af66fc99e Initial load
duke
parents:
diff changeset
64 // Ready to go with the database...
a61af66fc99e Initial load
duke
parents:
diff changeset
65 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
66
a61af66fc99e Initial load
duke
parents:
diff changeset
67 // walk through the system dictionary
a61af66fc99e Initial load
duke
parents:
diff changeset
68 SystemDictionary dict = VM.getVM().getSystemDictionary();
a61af66fc99e Initial load
duke
parents:
diff changeset
69 dict.classesDo(new SystemDictionary.ClassVisitor() {
a61af66fc99e Initial load
duke
parents:
diff changeset
70 public void visit(Klass k) {
705
1f2abec69714 6826261: class file dumping from SA is broken
never
parents: 0
diff changeset
71 if (k instanceof InstanceKlass) {
1f2abec69714 6826261: class file dumping from SA is broken
never
parents: 0
diff changeset
72 try {
1f2abec69714 6826261: class file dumping from SA is broken
never
parents: 0
diff changeset
73 dumpKlass((InstanceKlass) k);
1f2abec69714 6826261: class file dumping from SA is broken
never
parents: 0
diff changeset
74 } catch (Exception e) {
1f2abec69714 6826261: class file dumping from SA is broken
never
parents: 0
diff changeset
75 System.out.println(k.getName().asString());
1f2abec69714 6826261: class file dumping from SA is broken
never
parents: 0
diff changeset
76 e.printStackTrace();
1f2abec69714 6826261: class file dumping from SA is broken
never
parents: 0
diff changeset
77 }
1f2abec69714 6826261: class file dumping from SA is broken
never
parents: 0
diff changeset
78 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
79 }
a61af66fc99e Initial load
duke
parents:
diff changeset
80 });
a61af66fc99e Initial load
duke
parents:
diff changeset
81 }
a61af66fc99e Initial load
duke
parents:
diff changeset
82 catch (AddressException e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
83 System.err.println("Error accessing address 0x"
a61af66fc99e Initial load
duke
parents:
diff changeset
84 + Long.toHexString(e.getAddress()));
a61af66fc99e Initial load
duke
parents:
diff changeset
85 e.printStackTrace();
a61af66fc99e Initial load
duke
parents:
diff changeset
86 }
3939
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
87 if (jarStream != null) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
88 try {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
89 jarStream.close();
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
90 } catch (IOException ioe) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
91 ioe.printStackTrace();
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
92 }
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
93 jarStream = null;
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
94 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
95 }
a61af66fc99e Initial load
duke
parents:
diff changeset
96
a61af66fc99e Initial load
duke
parents:
diff changeset
97 public String getName() {
a61af66fc99e Initial load
duke
parents:
diff changeset
98 return "jcore";
a61af66fc99e Initial load
duke
parents:
diff changeset
99 }
a61af66fc99e Initial load
duke
parents:
diff changeset
100
a61af66fc99e Initial load
duke
parents:
diff changeset
101 private void dumpKlass(InstanceKlass kls) {
a61af66fc99e Initial load
duke
parents:
diff changeset
102 if (classFilter != null && ! classFilter.canInclude(kls) ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
103 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
104 }
a61af66fc99e Initial load
duke
parents:
diff changeset
105
a61af66fc99e Initial load
duke
parents:
diff changeset
106 String klassName = kls.getName().asString();
a61af66fc99e Initial load
duke
parents:
diff changeset
107 klassName = klassName.replace('/', File.separatorChar);
3939
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
108 try {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
109 OutputStream os = null;
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
110 if (jarStream != null) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
111 jarStream.putNextEntry(new JarEntry(klassName + ".class"));
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
112 os = jarStream;
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
113 } else {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
114 int index = klassName.lastIndexOf(File.separatorChar);
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
115 File dir = null;
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
116 if (index != -1) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
117 String dirName = klassName.substring(0, index);
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
118 dir = new File(outputDirectory, dirName);
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
119 } else {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
120 dir = new File(outputDirectory);
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
121 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
122
3939
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
123 dir.mkdirs();
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
124 File f = new File(dir, klassName.substring(index + 1) + ".class");
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
125 f.createNewFile();
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
126 os = new BufferedOutputStream(new FileOutputStream(f));
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
127 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
128 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
129 ClassWriter cw = new ClassWriter(kls, os);
a61af66fc99e Initial load
duke
parents:
diff changeset
130 cw.write();
a61af66fc99e Initial load
duke
parents:
diff changeset
131 } finally {
3939
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
132 if (os != jarStream) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
133 os.close();
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
134 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
135 }
a61af66fc99e Initial load
duke
parents:
diff changeset
136 } catch(IOException exp) {
a61af66fc99e Initial load
duke
parents:
diff changeset
137 exp.printStackTrace();
a61af66fc99e Initial load
duke
parents:
diff changeset
138 }
a61af66fc99e Initial load
duke
parents:
diff changeset
139 }
a61af66fc99e Initial load
duke
parents:
diff changeset
140
a61af66fc99e Initial load
duke
parents:
diff changeset
141 public static void main(String[] args) {
3939
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
142 // load class filters
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
143 ClassFilter classFilter = null;
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
144 String filterClassName = System.getProperty("sun.jvm.hotspot.tools.jcore.filter");
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
145 if (filterClassName != null) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
146 try {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
147 Class filterClass = Class.forName(filterClassName);
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
148 classFilter = (ClassFilter) filterClass.newInstance();
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
149 } catch(Exception exp) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
150 System.err.println("Warning: Can not create class filter!");
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
151 }
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
152 }
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
153
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
154 String outputDirectory = System.getProperty("sun.jvm.hotspot.tools.jcore.outputDir");
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
155 if (outputDirectory == null)
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
156 outputDirectory = ".";
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
157
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
158
0
a61af66fc99e Initial load
duke
parents:
diff changeset
159 ClassDump cd = new ClassDump();
3939
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
160 cd.setClassFilter(classFilter);
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
161 cd.setOutputDirectory(outputDirectory);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
162 cd.start(args);
a61af66fc99e Initial load
duke
parents:
diff changeset
163 cd.stop();
a61af66fc99e Initial load
duke
parents:
diff changeset
164 }
a61af66fc99e Initial load
duke
parents:
diff changeset
165 }