Mercurial > hg > truffle
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 |
rev | line source |
---|---|
0 | 1 /* |
3939 | 2 * Copyright (c) 2002, 2011, 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.*; | |
3939 | 28 import java.util.jar.JarOutputStream; |
29 import java.util.jar.JarEntry; | |
30 import java.util.jar.Manifest; | |
0 | 31 import sun.jvm.hotspot.memory.*; |
32 import sun.jvm.hotspot.oops.*; | |
33 import sun.jvm.hotspot.debugger.*; | |
34 import sun.jvm.hotspot.runtime.*; | |
35 import sun.jvm.hotspot.tools.*; | |
36 | |
37 public class ClassDump extends Tool { | |
38 private ClassFilter classFilter; | |
39 private String outputDirectory; | |
3939 | 40 private JarOutputStream jarStream; |
41 | |
42 public void setClassFilter(ClassFilter cf) { | |
43 classFilter = cf; | |
44 } | |
45 | |
46 public void setOutputDirectory(String od) { | |
47 outputDirectory = od; | |
48 if (jarStream != null) { | |
49 try { | |
50 jarStream.close(); | |
51 } catch (IOException ioe) { | |
52 ioe.printStackTrace(); | |
53 } | |
54 } | |
55 jarStream = null; | |
56 } | |
57 | |
58 public void setJarOutput(String jarFileName) throws IOException { | |
59 jarStream = new JarOutputStream(new FileOutputStream(jarFileName), new Manifest()); | |
60 outputDirectory = null; | |
61 } | |
0 | 62 |
63 public void run() { | |
64 // Ready to go with the database... | |
65 try { | |
66 | |
67 // walk through the system dictionary | |
68 SystemDictionary dict = VM.getVM().getSystemDictionary(); | |
69 dict.classesDo(new SystemDictionary.ClassVisitor() { | |
70 public void visit(Klass k) { | |
705 | 71 if (k instanceof InstanceKlass) { |
72 try { | |
73 dumpKlass((InstanceKlass) k); | |
74 } catch (Exception e) { | |
75 System.out.println(k.getName().asString()); | |
76 e.printStackTrace(); | |
77 } | |
78 } | |
0 | 79 } |
80 }); | |
81 } | |
82 catch (AddressException e) { | |
83 System.err.println("Error accessing address 0x" | |
84 + Long.toHexString(e.getAddress())); | |
85 e.printStackTrace(); | |
86 } | |
3939 | 87 if (jarStream != null) { |
88 try { | |
89 jarStream.close(); | |
90 } catch (IOException ioe) { | |
91 ioe.printStackTrace(); | |
92 } | |
93 jarStream = null; | |
94 } | |
0 | 95 } |
96 | |
97 public String getName() { | |
98 return "jcore"; | |
99 } | |
100 | |
101 private void dumpKlass(InstanceKlass kls) { | |
102 if (classFilter != null && ! classFilter.canInclude(kls) ) { | |
103 return; | |
104 } | |
105 | |
106 String klassName = kls.getName().asString(); | |
107 klassName = klassName.replace('/', File.separatorChar); | |
3939 | 108 try { |
109 OutputStream os = null; | |
110 if (jarStream != null) { | |
111 jarStream.putNextEntry(new JarEntry(klassName + ".class")); | |
112 os = jarStream; | |
113 } else { | |
114 int index = klassName.lastIndexOf(File.separatorChar); | |
115 File dir = null; | |
116 if (index != -1) { | |
117 String dirName = klassName.substring(0, index); | |
118 dir = new File(outputDirectory, dirName); | |
119 } else { | |
120 dir = new File(outputDirectory); | |
121 } | |
0 | 122 |
3939 | 123 dir.mkdirs(); |
124 File f = new File(dir, klassName.substring(index + 1) + ".class"); | |
125 f.createNewFile(); | |
126 os = new BufferedOutputStream(new FileOutputStream(f)); | |
127 } | |
0 | 128 try { |
129 ClassWriter cw = new ClassWriter(kls, os); | |
130 cw.write(); | |
131 } finally { | |
3939 | 132 if (os != jarStream) { |
133 os.close(); | |
134 } | |
0 | 135 } |
136 } catch(IOException exp) { | |
137 exp.printStackTrace(); | |
138 } | |
139 } | |
140 | |
141 public static void main(String[] args) { | |
3939 | 142 // load class filters |
143 ClassFilter classFilter = null; | |
144 String filterClassName = System.getProperty("sun.jvm.hotspot.tools.jcore.filter"); | |
145 if (filterClassName != null) { | |
146 try { | |
147 Class filterClass = Class.forName(filterClassName); | |
148 classFilter = (ClassFilter) filterClass.newInstance(); | |
149 } catch(Exception exp) { | |
150 System.err.println("Warning: Can not create class filter!"); | |
151 } | |
152 } | |
153 | |
154 String outputDirectory = System.getProperty("sun.jvm.hotspot.tools.jcore.outputDir"); | |
155 if (outputDirectory == null) | |
156 outputDirectory = "."; | |
157 | |
158 | |
0 | 159 ClassDump cd = new ClassDump(); |
3939 | 160 cd.setClassFilter(classFilter); |
161 cd.setOutputDirectory(outputDirectory); | |
0 | 162 cd.start(args); |
163 cd.stop(); | |
164 } | |
165 } |