1 /*
   2  * Copyright (c) 2002, 2009, 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 sun.jvm.hotspot.memory.*;
  29 import sun.jvm.hotspot.oops.*;
  30 import sun.jvm.hotspot.debugger.*;
  31 import sun.jvm.hotspot.runtime.*;
  32 import sun.jvm.hotspot.tools.*;
  33 
  34 public class ClassDump extends Tool {
  35     private ClassFilter classFilter;
  36     private String      outputDirectory;
  37 
  38     public void run() {
  39         // Ready to go with the database...
  40         try {
  41 
  42             // load class filters
  43 
  44             String filterClassName = System.getProperty("sun.jvm.hotspot.tools.jcore.filter");
  45             if (filterClassName != null) {
  46                 try {
  47                     Class filterClass = Class.forName(filterClassName);
  48                     classFilter = (ClassFilter) filterClass.newInstance();
  49                 } catch(Exception exp) {
  50                     System.err.println("Warning: Can not create class filter!");
  51                 }
  52             }
  53 
  54             outputDirectory = System.getProperty("sun.jvm.hotspot.tools.jcore.outputDir");
  55             if (outputDirectory == null)
  56                 outputDirectory = ".";
  57 
  58             // walk through the system dictionary
  59             SystemDictionary dict = VM.getVM().getSystemDictionary();
  60             dict.classesDo(new SystemDictionary.ClassVisitor() {
  61                     public void visit(Klass k) {
  62                         if (k instanceof InstanceKlass) {
  63                             try {
  64                                 dumpKlass((InstanceKlass) k);
  65                             } catch (Exception e) {
  66                                 System.out.println(k.getName().asString());
  67                                 e.printStackTrace();
  68                             }
  69                         }
  70                     }
  71                 });
  72         }
  73         catch (AddressException e) {
  74             System.err.println("Error accessing address 0x"
  75                                + Long.toHexString(e.getAddress()));
  76             e.printStackTrace();
  77         }
  78     }
  79 
  80     public String getName() {
  81         return "jcore";
  82     }
  83 
  84     private void dumpKlass(InstanceKlass kls) {
  85         if (classFilter != null && ! classFilter.canInclude(kls) ) {
  86             return;
  87         }
  88 
  89         String klassName = kls.getName().asString();
  90         klassName = klassName.replace('/', File.separatorChar);
  91         int index = klassName.lastIndexOf(File.separatorChar);
  92         File dir = null;
  93         if (index != -1) {
  94             String dirName = klassName.substring(0, index);
  95             dir =  new File(outputDirectory,  dirName);
  96         } else {
  97             dir = new File(outputDirectory);
  98         }
  99 
 100         dir.mkdirs();
 101         File f = new File(dir, klassName.substring(klassName.lastIndexOf(File.separatorChar) + 1)
 102                           + ".class");
 103         try {
 104             f.createNewFile();
 105             OutputStream os = new BufferedOutputStream(new FileOutputStream(f));
 106             try {
 107                 ClassWriter cw = new ClassWriter(kls, os);
 108                 cw.write();
 109             } finally {
 110                 os.close();
 111             }
 112         } catch(IOException exp) {
 113             exp.printStackTrace();
 114         }
 115     }
 116 
 117     public static void main(String[] args) {
 118         ClassDump cd = new ClassDump();
 119         cd.start(args);
 120         cd.stop();
 121     }
 122 }