Sat, 01 Dec 2007 00:00:00 +0000
Initial load
duke@435 | 1 | /* |
duke@435 | 2 | * Copyright 2002-2004 Sun Microsystems, Inc. All Rights Reserved. |
duke@435 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@435 | 4 | * |
duke@435 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@435 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@435 | 7 | * published by the Free Software Foundation. |
duke@435 | 8 | * |
duke@435 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@435 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@435 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@435 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@435 | 13 | * accompanied this code). |
duke@435 | 14 | * |
duke@435 | 15 | * You should have received a copy of the GNU General Public License version |
duke@435 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@435 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@435 | 18 | * |
duke@435 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@435 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@435 | 21 | * have any questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
duke@435 | 25 | package sun.jvm.hotspot.tools.jcore; |
duke@435 | 26 | |
duke@435 | 27 | import java.io.*; |
duke@435 | 28 | import sun.jvm.hotspot.memory.*; |
duke@435 | 29 | import sun.jvm.hotspot.oops.*; |
duke@435 | 30 | import sun.jvm.hotspot.debugger.*; |
duke@435 | 31 | import sun.jvm.hotspot.runtime.*; |
duke@435 | 32 | import sun.jvm.hotspot.tools.*; |
duke@435 | 33 | |
duke@435 | 34 | public class ClassDump extends Tool { |
duke@435 | 35 | private ClassFilter classFilter; |
duke@435 | 36 | private String outputDirectory; |
duke@435 | 37 | |
duke@435 | 38 | public void run() { |
duke@435 | 39 | // Ready to go with the database... |
duke@435 | 40 | try { |
duke@435 | 41 | |
duke@435 | 42 | // load class filters |
duke@435 | 43 | |
duke@435 | 44 | String filterClassName = System.getProperty("sun.jvm.hotspot.tools.jcore.filter"); |
duke@435 | 45 | if (filterClassName != null) { |
duke@435 | 46 | try { |
duke@435 | 47 | Class filterClass = Class.forName(filterClassName); |
duke@435 | 48 | classFilter = (ClassFilter) filterClass.newInstance(); |
duke@435 | 49 | } catch(Exception exp) { |
duke@435 | 50 | System.err.println("Warning: Can not create class filter!"); |
duke@435 | 51 | } |
duke@435 | 52 | } |
duke@435 | 53 | |
duke@435 | 54 | outputDirectory = System.getProperty("sun.jvm.hotspot.tools.jcore.outputDir"); |
duke@435 | 55 | if (outputDirectory == null) |
duke@435 | 56 | outputDirectory = "."; |
duke@435 | 57 | |
duke@435 | 58 | // walk through the system dictionary |
duke@435 | 59 | SystemDictionary dict = VM.getVM().getSystemDictionary(); |
duke@435 | 60 | dict.classesDo(new SystemDictionary.ClassVisitor() { |
duke@435 | 61 | public void visit(Klass k) { |
duke@435 | 62 | if (k instanceof InstanceKlass) |
duke@435 | 63 | dumpKlass((InstanceKlass) k); |
duke@435 | 64 | } |
duke@435 | 65 | }); |
duke@435 | 66 | } |
duke@435 | 67 | catch (AddressException e) { |
duke@435 | 68 | System.err.println("Error accessing address 0x" |
duke@435 | 69 | + Long.toHexString(e.getAddress())); |
duke@435 | 70 | e.printStackTrace(); |
duke@435 | 71 | } |
duke@435 | 72 | } |
duke@435 | 73 | |
duke@435 | 74 | public String getName() { |
duke@435 | 75 | return "jcore"; |
duke@435 | 76 | } |
duke@435 | 77 | |
duke@435 | 78 | private void dumpKlass(InstanceKlass kls) { |
duke@435 | 79 | if (classFilter != null && ! classFilter.canInclude(kls) ) { |
duke@435 | 80 | return; |
duke@435 | 81 | } |
duke@435 | 82 | |
duke@435 | 83 | String klassName = kls.getName().asString(); |
duke@435 | 84 | klassName = klassName.replace('/', File.separatorChar); |
duke@435 | 85 | int index = klassName.lastIndexOf(File.separatorChar); |
duke@435 | 86 | File dir = null; |
duke@435 | 87 | if (index != -1) { |
duke@435 | 88 | String dirName = klassName.substring(0, index); |
duke@435 | 89 | dir = new File(outputDirectory, dirName); |
duke@435 | 90 | } else { |
duke@435 | 91 | dir = new File(outputDirectory); |
duke@435 | 92 | } |
duke@435 | 93 | |
duke@435 | 94 | dir.mkdirs(); |
duke@435 | 95 | File f = new File(dir, klassName.substring(klassName.lastIndexOf(File.separatorChar) + 1) |
duke@435 | 96 | + ".class"); |
duke@435 | 97 | try { |
duke@435 | 98 | f.createNewFile(); |
duke@435 | 99 | OutputStream os = new BufferedOutputStream(new FileOutputStream(f)); |
duke@435 | 100 | try { |
duke@435 | 101 | ClassWriter cw = new ClassWriter(kls, os); |
duke@435 | 102 | cw.write(); |
duke@435 | 103 | } finally { |
duke@435 | 104 | os.close(); |
duke@435 | 105 | } |
duke@435 | 106 | } catch(IOException exp) { |
duke@435 | 107 | exp.printStackTrace(); |
duke@435 | 108 | } |
duke@435 | 109 | } |
duke@435 | 110 | |
duke@435 | 111 | public static void main(String[] args) { |
duke@435 | 112 | ClassDump cd = new ClassDump(); |
duke@435 | 113 | cd.start(args); |
duke@435 | 114 | cd.stop(); |
duke@435 | 115 | } |
duke@435 | 116 | } |