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

6996563: 6984311 changes forgot to update vmStructs.cpp for new field _operands Summary: Add missing line to vmStructs. Also fix bug with class dumper. Reviewed-by: twisti, kvn
author jrose
date Fri, 05 Nov 2010 12:18:30 -0700
parents c18cbe5936b8
children f6f3bb0ee072
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 844
diff changeset
2 * Copyright (c) 2002, 2009, 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.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
28 import sun.jvm.hotspot.memory.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
29 import sun.jvm.hotspot.oops.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
30 import sun.jvm.hotspot.debugger.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
31 import sun.jvm.hotspot.runtime.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
32 import sun.jvm.hotspot.tools.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
33
a61af66fc99e Initial load
duke
parents:
diff changeset
34 public class ClassDump extends Tool {
a61af66fc99e Initial load
duke
parents:
diff changeset
35 private ClassFilter classFilter;
a61af66fc99e Initial load
duke
parents:
diff changeset
36 private String outputDirectory;
a61af66fc99e Initial load
duke
parents:
diff changeset
37
a61af66fc99e Initial load
duke
parents:
diff changeset
38 public void run() {
a61af66fc99e Initial load
duke
parents:
diff changeset
39 // Ready to go with the database...
a61af66fc99e Initial load
duke
parents:
diff changeset
40 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
41
a61af66fc99e Initial load
duke
parents:
diff changeset
42 // load class filters
a61af66fc99e Initial load
duke
parents:
diff changeset
43
a61af66fc99e Initial load
duke
parents:
diff changeset
44 String filterClassName = System.getProperty("sun.jvm.hotspot.tools.jcore.filter");
a61af66fc99e Initial load
duke
parents:
diff changeset
45 if (filterClassName != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
46 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
47 Class filterClass = Class.forName(filterClassName);
a61af66fc99e Initial load
duke
parents:
diff changeset
48 classFilter = (ClassFilter) filterClass.newInstance();
a61af66fc99e Initial load
duke
parents:
diff changeset
49 } catch(Exception exp) {
a61af66fc99e Initial load
duke
parents:
diff changeset
50 System.err.println("Warning: Can not create class filter!");
a61af66fc99e Initial load
duke
parents:
diff changeset
51 }
a61af66fc99e Initial load
duke
parents:
diff changeset
52 }
a61af66fc99e Initial load
duke
parents:
diff changeset
53
a61af66fc99e Initial load
duke
parents:
diff changeset
54 outputDirectory = System.getProperty("sun.jvm.hotspot.tools.jcore.outputDir");
a61af66fc99e Initial load
duke
parents:
diff changeset
55 if (outputDirectory == null)
a61af66fc99e Initial load
duke
parents:
diff changeset
56 outputDirectory = ".";
a61af66fc99e Initial load
duke
parents:
diff changeset
57
a61af66fc99e Initial load
duke
parents:
diff changeset
58 // walk through the system dictionary
a61af66fc99e Initial load
duke
parents:
diff changeset
59 SystemDictionary dict = VM.getVM().getSystemDictionary();
a61af66fc99e Initial load
duke
parents:
diff changeset
60 dict.classesDo(new SystemDictionary.ClassVisitor() {
a61af66fc99e Initial load
duke
parents:
diff changeset
61 public void visit(Klass k) {
705
1f2abec69714 6826261: class file dumping from SA is broken
never
parents: 0
diff changeset
62 if (k instanceof InstanceKlass) {
1f2abec69714 6826261: class file dumping from SA is broken
never
parents: 0
diff changeset
63 try {
1f2abec69714 6826261: class file dumping from SA is broken
never
parents: 0
diff changeset
64 dumpKlass((InstanceKlass) k);
1f2abec69714 6826261: class file dumping from SA is broken
never
parents: 0
diff changeset
65 } catch (Exception e) {
1f2abec69714 6826261: class file dumping from SA is broken
never
parents: 0
diff changeset
66 System.out.println(k.getName().asString());
1f2abec69714 6826261: class file dumping from SA is broken
never
parents: 0
diff changeset
67 e.printStackTrace();
1f2abec69714 6826261: class file dumping from SA is broken
never
parents: 0
diff changeset
68 }
1f2abec69714 6826261: class file dumping from SA is broken
never
parents: 0
diff changeset
69 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
70 }
a61af66fc99e Initial load
duke
parents:
diff changeset
71 });
a61af66fc99e Initial load
duke
parents:
diff changeset
72 }
a61af66fc99e Initial load
duke
parents:
diff changeset
73 catch (AddressException e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
74 System.err.println("Error accessing address 0x"
a61af66fc99e Initial load
duke
parents:
diff changeset
75 + Long.toHexString(e.getAddress()));
a61af66fc99e Initial load
duke
parents:
diff changeset
76 e.printStackTrace();
a61af66fc99e Initial load
duke
parents:
diff changeset
77 }
a61af66fc99e Initial load
duke
parents:
diff changeset
78 }
a61af66fc99e Initial load
duke
parents:
diff changeset
79
a61af66fc99e Initial load
duke
parents:
diff changeset
80 public String getName() {
a61af66fc99e Initial load
duke
parents:
diff changeset
81 return "jcore";
a61af66fc99e Initial load
duke
parents:
diff changeset
82 }
a61af66fc99e Initial load
duke
parents:
diff changeset
83
a61af66fc99e Initial load
duke
parents:
diff changeset
84 private void dumpKlass(InstanceKlass kls) {
a61af66fc99e Initial load
duke
parents:
diff changeset
85 if (classFilter != null && ! classFilter.canInclude(kls) ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
86 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
87 }
a61af66fc99e Initial load
duke
parents:
diff changeset
88
a61af66fc99e Initial load
duke
parents:
diff changeset
89 String klassName = kls.getName().asString();
a61af66fc99e Initial load
duke
parents:
diff changeset
90 klassName = klassName.replace('/', File.separatorChar);
a61af66fc99e Initial load
duke
parents:
diff changeset
91 int index = klassName.lastIndexOf(File.separatorChar);
a61af66fc99e Initial load
duke
parents:
diff changeset
92 File dir = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
93 if (index != -1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
94 String dirName = klassName.substring(0, index);
a61af66fc99e Initial load
duke
parents:
diff changeset
95 dir = new File(outputDirectory, dirName);
a61af66fc99e Initial load
duke
parents:
diff changeset
96 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
97 dir = new File(outputDirectory);
a61af66fc99e Initial load
duke
parents:
diff changeset
98 }
a61af66fc99e Initial load
duke
parents:
diff changeset
99
a61af66fc99e Initial load
duke
parents:
diff changeset
100 dir.mkdirs();
a61af66fc99e Initial load
duke
parents:
diff changeset
101 File f = new File(dir, klassName.substring(klassName.lastIndexOf(File.separatorChar) + 1)
a61af66fc99e Initial load
duke
parents:
diff changeset
102 + ".class");
a61af66fc99e Initial load
duke
parents:
diff changeset
103 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
104 f.createNewFile();
a61af66fc99e Initial load
duke
parents:
diff changeset
105 OutputStream os = new BufferedOutputStream(new FileOutputStream(f));
a61af66fc99e Initial load
duke
parents:
diff changeset
106 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
107 ClassWriter cw = new ClassWriter(kls, os);
a61af66fc99e Initial load
duke
parents:
diff changeset
108 cw.write();
a61af66fc99e Initial load
duke
parents:
diff changeset
109 } finally {
a61af66fc99e Initial load
duke
parents:
diff changeset
110 os.close();
a61af66fc99e Initial load
duke
parents:
diff changeset
111 }
a61af66fc99e Initial load
duke
parents:
diff changeset
112 } catch(IOException exp) {
a61af66fc99e Initial load
duke
parents:
diff changeset
113 exp.printStackTrace();
a61af66fc99e Initial load
duke
parents:
diff changeset
114 }
a61af66fc99e Initial load
duke
parents:
diff changeset
115 }
a61af66fc99e Initial load
duke
parents:
diff changeset
116
a61af66fc99e Initial load
duke
parents:
diff changeset
117 public static void main(String[] args) {
a61af66fc99e Initial load
duke
parents:
diff changeset
118 ClassDump cd = new ClassDump();
a61af66fc99e Initial load
duke
parents:
diff changeset
119 cd.start(args);
a61af66fc99e Initial load
duke
parents:
diff changeset
120 cd.stop();
a61af66fc99e Initial load
duke
parents:
diff changeset
121 }
a61af66fc99e Initial load
duke
parents:
diff changeset
122 }