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