hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/JMap.java
author kevinw
Wed, 26 Jun 2013 00:01:20 +0100
changeset 18481 fa7f6ad24216
parent 17118 cea2adff853d
child 21065 b8281f54308b
permissions -rw-r--r--
8010278: SA: provide mechanism for using an alternative SA debugger back-end. Reviewed-by: sla, dsamersoff
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
15809
e2516b8c8f35 8008536: Add HotSpot support for printing class loader statistics for JMap
ehelin
parents: 5547
diff changeset
     2
 * Copyright (c) 2004, 2013, Oracle and/or its affiliates. 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
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    21
 * questions.
1
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;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
18481
fa7f6ad24216 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 17118
diff changeset
    28
import sun.jvm.hotspot.debugger.JVMDebugger;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
import sun.jvm.hotspot.utilities.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
public class JMap extends Tool {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
    public JMap(int m) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
        mode = m;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
    public JMap() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
        this(MODE_PMAP);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
18481
fa7f6ad24216 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 17118
diff changeset
    40
    public JMap(JVMDebugger d) {
fa7f6ad24216 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 17118
diff changeset
    41
        super(d);
fa7f6ad24216 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 17118
diff changeset
    42
    }
fa7f6ad24216 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 17118
diff changeset
    43
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
    protected boolean needsJavaPrefix() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
        return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
    public String getName() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
        return "jmap";
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
    protected String getCommandFlags() {
15809
e2516b8c8f35 8008536: Add HotSpot support for printing class loader statistics for JMap
ehelin
parents: 5547
diff changeset
    53
        return "-heap|-heap:format=b|-histo|-clstats|-finalizerinfo";
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
    protected void printFlagsUsage() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
        System.out.println("    <no option>\tto print same info as Solaris pmap");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
        System.out.println("    -heap\tto print java heap summary");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
        System.out.println("    -heap:format=b\tto dump java heap in hprof binary format");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
        System.out.println("    -histo\tto print histogram of java object heap");
15809
e2516b8c8f35 8008536: Add HotSpot support for printing class loader statistics for JMap
ehelin
parents: 5547
diff changeset
    61
        System.out.println("    -clstats\tto print class loader statistics");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
        System.out.println("    -finalizerinfo\tto print information on objects awaiting finalization");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
        super.printFlagsUsage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
    public static final int MODE_HEAP_SUMMARY = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
    public static final int MODE_HISTOGRAM = 1;
15809
e2516b8c8f35 8008536: Add HotSpot support for printing class loader statistics for JMap
ehelin
parents: 5547
diff changeset
    68
    public static final int MODE_CLSTATS = 2;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
    public static final int MODE_PMAP = 3;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
    public static final int MODE_HEAP_GRAPH_HPROF_BIN = 4;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
    public static final int MODE_HEAP_GRAPH_GXL = 5;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
    public static final int MODE_FINALIZERINFO = 6;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
    public void run() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
        Tool tool = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
        switch (mode) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
        case MODE_HEAP_SUMMARY:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
            tool = new HeapSummary();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
        case MODE_HISTOGRAM:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
            tool = new ObjectHistogram();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
15809
e2516b8c8f35 8008536: Add HotSpot support for printing class loader statistics for JMap
ehelin
parents: 5547
diff changeset
    86
        case MODE_CLSTATS:
e2516b8c8f35 8008536: Add HotSpot support for printing class loader statistics for JMap
ehelin
parents: 5547
diff changeset
    87
            tool = new ClassLoaderStats();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
        case MODE_PMAP:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
            tool = new PMap();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
        case MODE_HEAP_GRAPH_HPROF_BIN:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
            writeHeapHprofBin();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
            return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
        case MODE_HEAP_GRAPH_GXL:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
            writeHeapGXL();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
            return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
        case MODE_FINALIZERINFO:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
            tool = new FinalizerInfo();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
        default:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
            usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
       }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
       tool.setAgent(getAgent());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
       tool.setDebugeeType(getDebugeeType());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
       tool.run();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
    public static void main(String[] args) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
        int mode = MODE_PMAP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
        if (args.length > 1 ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
            String modeFlag = args[0];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
            boolean copyArgs = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
            if (modeFlag.equals("-heap")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
                mode = MODE_HEAP_SUMMARY;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
            } else if (modeFlag.equals("-histo")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
                mode = MODE_HISTOGRAM;
15809
e2516b8c8f35 8008536: Add HotSpot support for printing class loader statistics for JMap
ehelin
parents: 5547
diff changeset
   125
            } else if (modeFlag.equals("-clstats")) {
e2516b8c8f35 8008536: Add HotSpot support for printing class loader statistics for JMap
ehelin
parents: 5547
diff changeset
   126
                mode = MODE_CLSTATS;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
            } else if (modeFlag.equals("-finalizerinfo")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
                mode = MODE_FINALIZERINFO;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
            } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
                int index = modeFlag.indexOf("-heap:format=");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
                if (index != -1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
                    String format = modeFlag.substring(1 + modeFlag.indexOf('='));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
                    if (format.equals("b")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
                        mode = MODE_HEAP_GRAPH_HPROF_BIN;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
                    } else if (format.equals("x")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
                        mode = MODE_HEAP_GRAPH_GXL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
                    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
                        System.err.println("unknown heap format:" + format);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
                        return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
                    copyArgs = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
            if (copyArgs) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
                String[] newArgs = new String[args.length - 1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
                for (int i = 0; i < newArgs.length; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
                    newArgs[i] = args[i + 1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
                args = newArgs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
        JMap jmap = new JMap(mode);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
        jmap.start(args);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
        jmap.stop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
    public boolean writeHeapHprofBin(String fileName) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
            HeapGraphWriter hgw = new HeapHprofBinWriter();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
            hgw.write(fileName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
            System.out.println("heap written to " + fileName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
            return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
        } catch (IOException exp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
            System.err.println(exp.getMessage());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
            return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
    public boolean writeHeapHprofBin() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
        return writeHeapHprofBin("heap.bin");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
    private boolean writeHeapGXL(String fileName) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
            HeapGraphWriter hgw = new HeapGXLWriter();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
            hgw.write(fileName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
            System.out.println("heap written to " + fileName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
            return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
        } catch (IOException exp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
            System.err.println(exp.getMessage());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
            return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
    public boolean writeHeapGXL() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
        return writeHeapGXL("heap.xml");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
    private int mode;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
}