1 /* 2 * Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved. 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 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 package sun.jvm.hotspot.tools.jcore; 26 27 import java.io.*; 28 import java.util.jar.JarOutputStream; 29 import java.util.jar.JarEntry; 30 import java.util.jar.Manifest; 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; 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 } 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) { 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 } 79 } 80 }); 81 } 82 catch (AddressException e) { 83 System.err.println("Error accessing address 0x" 84 + Long.toHexString(e.getAddress())); 85 e.printStackTrace(); 86 } 87 if (jarStream != null) { 88 try { 89 jarStream.close(); 90 } catch (IOException ioe) { 91 ioe.printStackTrace(); 92 } 93 jarStream = null; 94 } 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); 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 } 122 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 } 128 try { 129 ClassWriter cw = new ClassWriter(kls, os); 130 cw.write(); 131 } finally { 132 if (os != jarStream) { 133 os.close(); 134 } 135 } 136 } catch(IOException exp) { 137 exp.printStackTrace(); 138 } 139 } 140 141 public static void main(String[] args) { 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 159 ClassDump cd = new ClassDump(); 160 cd.setClassFilter(classFilter); 161 cd.setOutputDirectory(outputDirectory); 162 cd.start(args); 163 cd.stop(); 164 } 165 }