annotate agent/src/share/classes/sun/jvm/hotspot/tools/jcore/ClassDump.java @ 12065:10c59b8021ec

8022655: ClassDump ignored jarStream setting Reviewed-by: minqi, sla
author kevinw
date Mon, 19 Aug 2013 14:28:58 +0100
parents e0379d5ba5d2
children 7fe6ef09d242
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
11993
e0379d5ba5d2 8021444: SA: ClassDump.run() should not ignore existing ClassFilter.
kevinw
parents: 11054
diff changeset
2 * Copyright (c) 2002, 2013, 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.*;
11054
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 3939
diff changeset
28 import java.lang.reflect.Constructor;
3939
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
29 import java.util.jar.JarOutputStream;
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
30 import java.util.jar.JarEntry;
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
31 import java.util.jar.Manifest;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
32 import sun.jvm.hotspot.memory.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
33 import sun.jvm.hotspot.oops.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
34 import sun.jvm.hotspot.debugger.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
35 import sun.jvm.hotspot.runtime.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
36 import sun.jvm.hotspot.tools.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
37
a61af66fc99e Initial load
duke
parents:
diff changeset
38 public class ClassDump extends Tool {
a61af66fc99e Initial load
duke
parents:
diff changeset
39 private ClassFilter classFilter;
a61af66fc99e Initial load
duke
parents:
diff changeset
40 private String outputDirectory;
3939
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
41 private JarOutputStream jarStream;
11054
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 3939
diff changeset
42 private String pkgList;
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 3939
diff changeset
43
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 3939
diff changeset
44 public ClassDump() {
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 3939
diff changeset
45 super();
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 3939
diff changeset
46 }
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 3939
diff changeset
47
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 3939
diff changeset
48 public ClassDump(JVMDebugger d, String pkgList) {
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 3939
diff changeset
49 super(d);
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 3939
diff changeset
50 this.pkgList = pkgList;
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 3939
diff changeset
51 }
3939
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
52
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
53 public void setClassFilter(ClassFilter cf) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
54 classFilter = cf;
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
55 }
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 public void setOutputDirectory(String od) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
58 outputDirectory = od;
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
59 if (jarStream != null) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
60 try {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
61 jarStream.close();
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
62 } catch (IOException ioe) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
63 ioe.printStackTrace();
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
64 }
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
65 }
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
66 jarStream = null;
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
67 }
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
68
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
69 public void setJarOutput(String jarFileName) throws IOException {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
70 jarStream = new JarOutputStream(new FileOutputStream(jarFileName), new Manifest());
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
71 outputDirectory = null;
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
72 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
73
a61af66fc99e Initial load
duke
parents:
diff changeset
74 public void run() {
a61af66fc99e Initial load
duke
parents:
diff changeset
75 // Ready to go with the database...
a61af66fc99e Initial load
duke
parents:
diff changeset
76 try {
11993
e0379d5ba5d2 8021444: SA: ClassDump.run() should not ignore existing ClassFilter.
kevinw
parents: 11054
diff changeset
77 if (classFilter == null) {
e0379d5ba5d2 8021444: SA: ClassDump.run() should not ignore existing ClassFilter.
kevinw
parents: 11054
diff changeset
78 // If not already set, the name of the filter comes from a System property.
e0379d5ba5d2 8021444: SA: ClassDump.run() should not ignore existing ClassFilter.
kevinw
parents: 11054
diff changeset
79 // If we have a pkgList, pass it, otherwise let the filter read
e0379d5ba5d2 8021444: SA: ClassDump.run() should not ignore existing ClassFilter.
kevinw
parents: 11054
diff changeset
80 // its own System property for the list of classes.
e0379d5ba5d2 8021444: SA: ClassDump.run() should not ignore existing ClassFilter.
kevinw
parents: 11054
diff changeset
81 String filterClassName = System.getProperty("sun.jvm.hotspot.tools.jcore.filter",
e0379d5ba5d2 8021444: SA: ClassDump.run() should not ignore existing ClassFilter.
kevinw
parents: 11054
diff changeset
82 "sun.jvm.hotspot.tools.jcore.PackageNameFilter");
e0379d5ba5d2 8021444: SA: ClassDump.run() should not ignore existing ClassFilter.
kevinw
parents: 11054
diff changeset
83 try {
e0379d5ba5d2 8021444: SA: ClassDump.run() should not ignore existing ClassFilter.
kevinw
parents: 11054
diff changeset
84 Class filterClass = Class.forName(filterClassName);
e0379d5ba5d2 8021444: SA: ClassDump.run() should not ignore existing ClassFilter.
kevinw
parents: 11054
diff changeset
85 if (pkgList == null) {
e0379d5ba5d2 8021444: SA: ClassDump.run() should not ignore existing ClassFilter.
kevinw
parents: 11054
diff changeset
86 classFilter = (ClassFilter) filterClass.newInstance();
e0379d5ba5d2 8021444: SA: ClassDump.run() should not ignore existing ClassFilter.
kevinw
parents: 11054
diff changeset
87 } else {
e0379d5ba5d2 8021444: SA: ClassDump.run() should not ignore existing ClassFilter.
kevinw
parents: 11054
diff changeset
88 Constructor con = filterClass.getConstructor(String.class);
e0379d5ba5d2 8021444: SA: ClassDump.run() should not ignore existing ClassFilter.
kevinw
parents: 11054
diff changeset
89 classFilter = (ClassFilter) con.newInstance(pkgList);
e0379d5ba5d2 8021444: SA: ClassDump.run() should not ignore existing ClassFilter.
kevinw
parents: 11054
diff changeset
90 }
e0379d5ba5d2 8021444: SA: ClassDump.run() should not ignore existing ClassFilter.
kevinw
parents: 11054
diff changeset
91 } catch(Exception exp) {
e0379d5ba5d2 8021444: SA: ClassDump.run() should not ignore existing ClassFilter.
kevinw
parents: 11054
diff changeset
92 System.err.println("Warning: Can not create class filter!");
11054
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 3939
diff changeset
93 }
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 3939
diff changeset
94 }
12065
10c59b8021ec 8022655: ClassDump ignored jarStream setting
kevinw
parents: 11993
diff changeset
95
10c59b8021ec 8022655: ClassDump ignored jarStream setting
kevinw
parents: 11993
diff changeset
96 // outputDirectory and jarStream are alternatives: setting one closes the other.
10c59b8021ec 8022655: ClassDump ignored jarStream setting
kevinw
parents: 11993
diff changeset
97 // If neither is set, use outputDirectory from the System property:
10c59b8021ec 8022655: ClassDump ignored jarStream setting
kevinw
parents: 11993
diff changeset
98 if (outputDirectory == null && jarStream == null) {
10c59b8021ec 8022655: ClassDump ignored jarStream setting
kevinw
parents: 11993
diff changeset
99 String dirName = System.getProperty("sun.jvm.hotspot.tools.jcore.outputDir", ".");
10c59b8021ec 8022655: ClassDump ignored jarStream setting
kevinw
parents: 11993
diff changeset
100 setOutputDirectory(dirName);
10c59b8021ec 8022655: ClassDump ignored jarStream setting
kevinw
parents: 11993
diff changeset
101 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
102
a61af66fc99e Initial load
duke
parents:
diff changeset
103 // walk through the system dictionary
a61af66fc99e Initial load
duke
parents:
diff changeset
104 SystemDictionary dict = VM.getVM().getSystemDictionary();
a61af66fc99e Initial load
duke
parents:
diff changeset
105 dict.classesDo(new SystemDictionary.ClassVisitor() {
a61af66fc99e Initial load
duke
parents:
diff changeset
106 public void visit(Klass k) {
705
1f2abec69714 6826261: class file dumping from SA is broken
never
parents: 0
diff changeset
107 if (k instanceof InstanceKlass) {
1f2abec69714 6826261: class file dumping from SA is broken
never
parents: 0
diff changeset
108 try {
1f2abec69714 6826261: class file dumping from SA is broken
never
parents: 0
diff changeset
109 dumpKlass((InstanceKlass) k);
1f2abec69714 6826261: class file dumping from SA is broken
never
parents: 0
diff changeset
110 } catch (Exception e) {
1f2abec69714 6826261: class file dumping from SA is broken
never
parents: 0
diff changeset
111 System.out.println(k.getName().asString());
1f2abec69714 6826261: class file dumping from SA is broken
never
parents: 0
diff changeset
112 e.printStackTrace();
1f2abec69714 6826261: class file dumping from SA is broken
never
parents: 0
diff changeset
113 }
1f2abec69714 6826261: class file dumping from SA is broken
never
parents: 0
diff changeset
114 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
115 }
a61af66fc99e Initial load
duke
parents:
diff changeset
116 });
a61af66fc99e Initial load
duke
parents:
diff changeset
117 }
a61af66fc99e Initial load
duke
parents:
diff changeset
118 catch (AddressException e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
119 System.err.println("Error accessing address 0x"
a61af66fc99e Initial load
duke
parents:
diff changeset
120 + Long.toHexString(e.getAddress()));
a61af66fc99e Initial load
duke
parents:
diff changeset
121 e.printStackTrace();
a61af66fc99e Initial load
duke
parents:
diff changeset
122 }
3939
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
123 if (jarStream != null) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
124 try {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
125 jarStream.close();
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
126 } catch (IOException ioe) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
127 ioe.printStackTrace();
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
128 }
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
129 jarStream = null;
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
130 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
131 }
a61af66fc99e Initial load
duke
parents:
diff changeset
132
a61af66fc99e Initial load
duke
parents:
diff changeset
133 public String getName() {
a61af66fc99e Initial load
duke
parents:
diff changeset
134 return "jcore";
a61af66fc99e Initial load
duke
parents:
diff changeset
135 }
a61af66fc99e Initial load
duke
parents:
diff changeset
136
a61af66fc99e Initial load
duke
parents:
diff changeset
137 private void dumpKlass(InstanceKlass kls) {
a61af66fc99e Initial load
duke
parents:
diff changeset
138 if (classFilter != null && ! classFilter.canInclude(kls) ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
139 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
140 }
a61af66fc99e Initial load
duke
parents:
diff changeset
141
a61af66fc99e Initial load
duke
parents:
diff changeset
142 String klassName = kls.getName().asString();
a61af66fc99e Initial load
duke
parents:
diff changeset
143 klassName = klassName.replace('/', File.separatorChar);
3939
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
144 try {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
145 OutputStream os = null;
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
146 if (jarStream != null) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
147 jarStream.putNextEntry(new JarEntry(klassName + ".class"));
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
148 os = jarStream;
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
149 } else {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
150 int index = klassName.lastIndexOf(File.separatorChar);
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
151 File dir = null;
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
152 if (index != -1) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
153 String dirName = klassName.substring(0, index);
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
154 dir = new File(outputDirectory, dirName);
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
155 } else {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
156 dir = new File(outputDirectory);
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
157 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
158
3939
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
159 dir.mkdirs();
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
160 File f = new File(dir, klassName.substring(index + 1) + ".class");
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
161 f.createNewFile();
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
162 os = new BufferedOutputStream(new FileOutputStream(f));
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
163 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
164 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
165 ClassWriter cw = new ClassWriter(kls, os);
a61af66fc99e Initial load
duke
parents:
diff changeset
166 cw.write();
a61af66fc99e Initial load
duke
parents:
diff changeset
167 } finally {
3939
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
168 if (os != jarStream) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
169 os.close();
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
170 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
171 }
a61af66fc99e Initial load
duke
parents:
diff changeset
172 } catch(IOException exp) {
a61af66fc99e Initial load
duke
parents:
diff changeset
173 exp.printStackTrace();
a61af66fc99e Initial load
duke
parents:
diff changeset
174 }
a61af66fc99e Initial load
duke
parents:
diff changeset
175 }
a61af66fc99e Initial load
duke
parents:
diff changeset
176
a61af66fc99e Initial load
duke
parents:
diff changeset
177 public static void main(String[] args) {
3939
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 1552
diff changeset
178
0
a61af66fc99e Initial load
duke
parents:
diff changeset
179 ClassDump cd = new ClassDump();
a61af66fc99e Initial load
duke
parents:
diff changeset
180 cd.start(args);
a61af66fc99e Initial load
duke
parents:
diff changeset
181 cd.stop();
a61af66fc99e Initial load
duke
parents:
diff changeset
182 }
a61af66fc99e Initial load
duke
parents:
diff changeset
183 }