Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/tools/JMap.java
41161 views
/*1* Copyright (c) 2004, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324package sun.jvm.hotspot.tools;2526import java.io.*;27import sun.jvm.hotspot.debugger.JVMDebugger;28import sun.jvm.hotspot.utilities.*;2930public class JMap extends Tool {31public JMap(int m) {32mode = m;33}3435public JMap() {36this(MODE_PMAP);37}3839public JMap(JVMDebugger d) {40super(d);41}4243protected boolean needsJavaPrefix() {44return false;45}4647public String getName() {48return "jmap";49}5051protected String getCommandFlags() {52return "-heap|-heap:format=b[,gz=<1-9>][,file=<dumpfile>]|-heap:format=x[,file=<dumpfile>]|{-histo|-clstats|-finalizerinfo";53}5455protected void printFlagsUsage() {56System.out.println(" <no option>\tTo print same info as Solaris pmap.");57System.out.println(" -heap\tTo print java heap summary.");58System.out.println(" -heap:format=b[,gz=<1-9>][,file=<dumpfile>] \tTo dump java heap in hprof binary format.");59System.out.println(" \tIf gz specified, the heap dump is written in gzipped format");60System.out.println(" \tusing the given compression level.");61System.err.println(" \t1 (recommended) is the fastest, 9 the strongest compression.");62System.out.println(" -heap:format=x[,file=<dumpfile>] \tTo dump java heap in GXL format.");63System.out.println(" \tPlease be aware that \"gz\" option is not valid for heap dump in GXL format.");64System.out.println(" -histo\tTo print histogram of java object heap.");65System.out.println(" -clstats\tTo print class loader statistics.");66System.out.println(" -finalizerinfo\tTo print information on objects awaiting finalization.");67super.printFlagsUsage();68}6970public static final int MODE_HEAP_SUMMARY = 0;71public static final int MODE_HISTOGRAM = 1;72public static final int MODE_CLSTATS = 2;73public static final int MODE_PMAP = 3;74public static final int MODE_HEAP_GRAPH_HPROF_BIN = 4;75public static final int MODE_HEAP_GRAPH_GXL = 5;76public static final int MODE_FINALIZERINFO = 6;7778private static String dumpfile = "heap.bin";79private static int gzLevel = 0;8081public void run() {82Tool tool = null;83switch (mode) {8485case MODE_HEAP_SUMMARY:86tool = new HeapSummary();87break;8889case MODE_HISTOGRAM:90tool = new ObjectHistogram();91break;9293case MODE_CLSTATS:94tool = new ClassLoaderStats();95break;9697case MODE_PMAP:98tool = new PMap();99break;100101case MODE_HEAP_GRAPH_HPROF_BIN:102writeHeapHprofBin(dumpfile, gzLevel);103return;104105case MODE_HEAP_GRAPH_GXL:106writeHeapGXL(dumpfile);107return;108109case MODE_FINALIZERINFO:110tool = new FinalizerInfo();111break;112113default:114usage();115break;116}117118tool.setAgent(getAgent());119tool.setDebugeeType(getDebugeeType());120tool.run();121}122123public static void main(String[] args) {124int mode = MODE_PMAP;125if (args.length > 1 ) {126String modeFlag = args[0];127boolean copyArgs = true;128if (modeFlag.equals("-heap")) {129mode = MODE_HEAP_SUMMARY;130} else if (modeFlag.equals("-histo")) {131mode = MODE_HISTOGRAM;132} else if (modeFlag.equals("-clstats")) {133mode = MODE_CLSTATS;134} else if (modeFlag.equals("-finalizerinfo")) {135mode = MODE_FINALIZERINFO;136} else {137int index = modeFlag.indexOf("-heap:");138if (index != -1) {139String[] options = modeFlag.substring(6).split(",");140for (String option : options) {141String[] keyValue = option.split("=");142if (keyValue[0].equals("format")) {143if (keyValue[1].equals("b")) {144mode = MODE_HEAP_GRAPH_HPROF_BIN;145} else if (keyValue[1].equals("x")) {146mode = MODE_HEAP_GRAPH_GXL;147} else {148System.err.println("unknown heap format:" + keyValue[0]);149150// Exit with error status151System.exit(1);152}153} else if (keyValue[0].equals("file")) {154if ((keyValue[1] == null) || keyValue[1].equals("")) {155System.err.println("File name must be set.");156System.exit(1);157}158dumpfile = keyValue[1];159} else if (keyValue[0].equals("gz")) {160if (mode == MODE_HEAP_GRAPH_GXL) {161System.err.println("\"gz\" option is not compatible with heap dump in GXL format");162System.exit(1);163}164if (keyValue.length == 1) {165System.err.println("Argument is expected for \"gz\"");166System.exit(1);167}168String level = keyValue[1];169try {170gzLevel = Integer.parseInt(level);171} catch (NumberFormatException e) {172System.err.println("\"gz\" option value not an integer ("+level+")");173System.exit(1);174}175if (gzLevel < 1 || gzLevel > 9) {176System.err.println("compression level out of range (1-9): " + level);177System.exit(1);178}179} else {180System.err.println("unknown option:" + keyValue[0]);181182// Exit with error status183System.exit(1);184}185}186} else {187copyArgs = false;188}189}190191if (copyArgs) {192String[] newArgs = new String[args.length - 1];193for (int i = 0; i < newArgs.length; i++) {194newArgs[i] = args[i + 1];195}196args = newArgs;197}198}199200JMap jmap = new JMap(mode);201jmap.execute(args);202}203204public boolean writeHeapHprofBin(String fileName, int gzLevel) {205try {206HeapGraphWriter hgw;207if (gzLevel == 0) {208hgw = new HeapHprofBinWriter();209} else if (gzLevel >=1 && gzLevel <= 9) {210hgw = new HeapHprofBinWriter(gzLevel);211} else {212System.err.println("Illegal compression level: " + gzLevel);213return false;214}215hgw.write(fileName);216System.out.println("heap written to " + fileName);217return true;218} catch (IOException exp) {219throw new RuntimeException(exp);220}221}222223public boolean writeHeapHprofBin() {224return writeHeapHprofBin("heap.bin", -1);225}226227private boolean writeHeapGXL(String fileName) {228try {229HeapGraphWriter hgw = new HeapGXLWriter();230hgw.write(fileName);231System.out.println("heap written to " + fileName);232return true;233} catch (IOException exp) {234throw new RuntimeException(exp);235}236}237238public boolean writeHeapGXL() {239return writeHeapGXL("heap.xml");240}241242private int mode;243}244245246