Mercurial > hg > truffle
annotate agent/src/share/classes/sun/jvm/hotspot/tools/jcore/ClassDump.java @ 12932:7fe6ef09d242
8025638: jmap returns 0 instead of 1 when it fails.
Summary: Re-factored some code handling return values and fails/errors during tool execution.
Reviewed-by: sla, kevinw
Contributed-by: fredrik.arvidsson@oracle.com
author | farvidsson |
---|---|
date | Wed, 16 Oct 2013 09:20:23 +0200 |
parents | 10c59b8021ec |
children |
rev | line source |
---|---|
0 | 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 | 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * | |
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 | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
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 | 22 * |
23 */ | |
24 | |
25 package sun.jvm.hotspot.tools.jcore; | |
26 | |
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 | 29 import java.util.jar.JarOutputStream; |
30 import java.util.jar.JarEntry; | |
31 import java.util.jar.Manifest; | |
0 | 32 import sun.jvm.hotspot.memory.*; |
33 import sun.jvm.hotspot.oops.*; | |
34 import sun.jvm.hotspot.debugger.*; | |
35 import sun.jvm.hotspot.runtime.*; | |
36 import sun.jvm.hotspot.tools.*; | |
37 | |
38 public class ClassDump extends Tool { | |
39 private ClassFilter classFilter; | |
40 private String outputDirectory; | |
3939 | 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 | 52 |
53 public void setClassFilter(ClassFilter cf) { | |
54 classFilter = cf; | |
55 } | |
56 | |
57 public void setOutputDirectory(String od) { | |
58 outputDirectory = od; | |
59 if (jarStream != null) { | |
60 try { | |
61 jarStream.close(); | |
62 } catch (IOException ioe) { | |
63 ioe.printStackTrace(); | |
64 } | |
65 } | |
66 jarStream = null; | |
67 } | |
68 | |
69 public void setJarOutput(String jarFileName) throws IOException { | |
70 jarStream = new JarOutputStream(new FileOutputStream(jarFileName), new Manifest()); | |
71 outputDirectory = null; | |
72 } | |
0 | 73 |
74 public void run() { | |
75 // Ready to go with the database... | |
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 | 95 |
96 // outputDirectory and jarStream are alternatives: setting one closes the other. | |
97 // If neither is set, use outputDirectory from the System property: | |
98 if (outputDirectory == null && jarStream == null) { | |
99 String dirName = System.getProperty("sun.jvm.hotspot.tools.jcore.outputDir", "."); | |
100 setOutputDirectory(dirName); | |
101 } | |
0 | 102 |
103 // walk through the system dictionary | |
104 SystemDictionary dict = VM.getVM().getSystemDictionary(); | |
105 dict.classesDo(new SystemDictionary.ClassVisitor() { | |
106 public void visit(Klass k) { | |
705 | 107 if (k instanceof InstanceKlass) { |
108 try { | |
109 dumpKlass((InstanceKlass) k); | |
110 } catch (Exception e) { | |
111 System.out.println(k.getName().asString()); | |
112 e.printStackTrace(); | |
113 } | |
114 } | |
0 | 115 } |
116 }); | |
117 } | |
118 catch (AddressException e) { | |
119 System.err.println("Error accessing address 0x" | |
120 + Long.toHexString(e.getAddress())); | |
121 e.printStackTrace(); | |
122 } | |
3939 | 123 if (jarStream != null) { |
124 try { | |
125 jarStream.close(); | |
126 } catch (IOException ioe) { | |
127 ioe.printStackTrace(); | |
128 } | |
129 jarStream = null; | |
130 } | |
0 | 131 } |
132 | |
133 public String getName() { | |
134 return "jcore"; | |
135 } | |
136 | |
137 private void dumpKlass(InstanceKlass kls) { | |
138 if (classFilter != null && ! classFilter.canInclude(kls) ) { | |
139 return; | |
140 } | |
141 | |
142 String klassName = kls.getName().asString(); | |
143 klassName = klassName.replace('/', File.separatorChar); | |
3939 | 144 try { |
145 OutputStream os = null; | |
146 if (jarStream != null) { | |
147 jarStream.putNextEntry(new JarEntry(klassName + ".class")); | |
148 os = jarStream; | |
149 } else { | |
150 int index = klassName.lastIndexOf(File.separatorChar); | |
151 File dir = null; | |
152 if (index != -1) { | |
153 String dirName = klassName.substring(0, index); | |
154 dir = new File(outputDirectory, dirName); | |
155 } else { | |
156 dir = new File(outputDirectory); | |
157 } | |
0 | 158 |
3939 | 159 dir.mkdirs(); |
160 File f = new File(dir, klassName.substring(index + 1) + ".class"); | |
161 f.createNewFile(); | |
162 os = new BufferedOutputStream(new FileOutputStream(f)); | |
163 } | |
0 | 164 try { |
165 ClassWriter cw = new ClassWriter(kls, os); | |
166 cw.write(); | |
167 } finally { | |
3939 | 168 if (os != jarStream) { |
169 os.close(); | |
170 } | |
0 | 171 } |
172 } catch(IOException exp) { | |
173 exp.printStackTrace(); | |
174 } | |
175 } | |
176 | |
177 public static void main(String[] args) { | |
3939 | 178 |
0 | 179 ClassDump cd = new ClassDump(); |
12932
7fe6ef09d242
8025638: jmap returns 0 instead of 1 when it fails.
farvidsson
parents:
12065
diff
changeset
|
180 cd.execute(args); |
0 | 181 } |
182 } |