src/jdk.jcmd/share/classes/sun/tools/jmap/JMap.java
author phh
Wed, 20 Feb 2019 11:36:02 -0800
changeset 53858 e7cf035682e3
parent 52902 e3398b2e1ab0
child 54065 f984aca565c1
permissions -rw-r--r--
8215622: Add dump to file support for jmap –histo Summary: Add "file=" to jmap "histo" jmap, Hotspot support in attachListener.cpp:heap_inspection. Reviewed-by: phh, sspitsyn Contributed-by: Lin Zang <zanglin5@jd.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
53858
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
     2
 * Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.tools.jmap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.InputStream;
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    31
import java.io.UnsupportedEncodingException;
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
    32
import java.util.Collection;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import com.sun.tools.attach.VirtualMachine;
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    35
import com.sun.tools.attach.VirtualMachineDescriptor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.tools.attach.AttachNotSupportedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.tools.attach.HotSpotVirtualMachine;
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
    38
import sun.tools.common.ProcessArgumentMatcher;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This class is the main class for the JMap utility. It parses its arguments
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 16501
diff changeset
    42
 * and decides if the command should be satisfied using the VM attach mechanism
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * or an SA tool. At this time the only option that uses the VM attach mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * is the -dump option to get a heap dump of a running application. All other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * options are mapped to SA tools.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class JMap {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        if (args.length == 0) {
21673
c5d341bc60dd 8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents: 21278
diff changeset
    51
            usage(1); // no arguments
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    54
        checkForUnsupportedOptions(args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    56
        // the chosen option
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        String option = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        // First iterate over the options (arguments starting with -).  There should be
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    60
        // one.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        int optionCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        while (optionCount < args.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            String arg = args[optionCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            if (!arg.startsWith("-")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            }
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
    67
            if (arg.equals("-?") ||
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
    68
                arg.equals("-h") ||
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
    69
                arg.equals("--help") ||
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
    70
                // -help: legacy. Undocumented.
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
    71
                arg.equals("-help")) {
21673
c5d341bc60dd 8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents: 21278
diff changeset
    72
                usage(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                if (option != null) {
21673
c5d341bc60dd 8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents: 21278
diff changeset
    75
                    usage(1);  // option already specified
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                option = arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            optionCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // if no option provided then use default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (option == null) {
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    84
            usage(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    87
        // Next we check the parameter count.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        int paramCount = args.length - optionCount;
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    89
        if (paramCount != 1) {
21673
c5d341bc60dd 8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents: 21278
diff changeset
    90
            usage(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
    93
        String pidArg = args[1];
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    94
        // Here we handle the built-in options
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    95
        // As more options are added we should create an abstract tool class and
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    96
        // have a table to map the options
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    97
        ProcessArgumentMatcher ap = new ProcessArgumentMatcher(pidArg);
44422
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
    98
        Collection<String> pids = ap.getVirtualMachinePids(JMap.class);
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    99
44422
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   100
        if (pids.isEmpty()) {
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   101
            System.err.println("Could not find any processes matching : '" + pidArg + "'");
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   102
            System.exit(1);
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   103
        }
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   104
44422
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   105
        for (String pid : pids) {
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   106
            if (pids.size() > 1) {
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
   107
                System.out.println("Pid:" + pid);
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
   108
            }
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
   109
            if (option.equals("-histo")) {
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
   110
                histo(pid, "");
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
   111
            } else if (option.startsWith("-histo:")) {
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
   112
                histo(pid, option.substring("-histo:".length()));
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
   113
            } else if (option.startsWith("-dump:")) {
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
   114
                dump(pid, option.substring("-dump:".length()));
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
   115
            } else if (option.equals("-finalizerinfo")) {
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
   116
                executeCommandForPid(pid, "jcmd", "GC.finalizer_info");
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
   117
            } else if (option.equals("-clstats")) {
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
   118
                executeCommandForPid(pid, "jcmd", "GC.class_stats");
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
   119
            } else {
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
   120
              usage(1);
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
   121
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   125
    private static void executeCommandForPid(String pid, String command, Object ... args)
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   126
        throws AttachNotSupportedException, IOException,
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   127
               UnsupportedEncodingException {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   128
        VirtualMachine vm = VirtualMachine.attach(pid);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   130
        // Cast to HotSpotVirtualMachine as this is an
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   131
        // implementation specific method.
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   132
        HotSpotVirtualMachine hvm = (HotSpotVirtualMachine) vm;
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   133
        try (InputStream in = hvm.executeCommand(command, args)) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   134
          // read to EOF and just print output
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   135
          byte b[] = new byte[256];
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   136
          int n;
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   137
          do {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   138
              n = in.read(b);
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   139
              if (n > 0) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   140
                  String s = new String(b, 0, n, "UTF-8");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   141
                  System.out.print(s);
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   142
              }
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   143
          } while (n > 0);
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   144
        }
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   145
        vm.detach();
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   146
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
53858
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   148
    private static String parseFileName(String opt) {
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   149
        // opt starts with "file="
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   150
        if (opt.length() > 5) {
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   151
            //  pass whole "file=" string
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   152
            String filename = opt.substring(5);
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   153
            try {
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   154
                // Get the canonical path - important to avoid just
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   155
                // passing a "heap.bin" and having the dump created
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   156
                // in the target VM working directory rather than the
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   157
                // directory where jmap is executed.
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   158
                return new File(filename).getCanonicalPath();
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   159
            } catch (IOException ioe) {
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   160
              return null;
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   161
            }
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   162
        }
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   163
        // no filename
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   164
        return null;
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   165
    }
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   166
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   167
    private static void histo(String pid, String options)
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   168
        throws AttachNotSupportedException, IOException,
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   169
               UnsupportedEncodingException {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   170
        String liveopt = "-all";
53858
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   171
        String filename = null;
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   172
        String subopts[] = options.split(",");
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   173
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   174
        for (int i = 0; i < subopts.length; i++) {
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   175
            String subopt = subopts[i];
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   176
            if (subopt.equals("") || subopt.equals("all")) {
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   177
                // pass
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   178
            } else if (subopt.equals("live")) {
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   179
                liveopt = "-live";
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   180
            } else if (subopt.startsWith("file=")) {
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   181
                filename = parseFileName(subopt);
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   182
                if (filename == null) {
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   183
                    usage(1); // invalid options or no filename
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   184
                }
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   185
            } else {
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   186
                usage(1);
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   187
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
53858
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   190
        System.out.flush();
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   191
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   192
        // inspectHeap is not the same as jcmd GC.class_histogram
53858
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   193
        executeCommandForPid(pid, "inspectheap", filename, liveopt);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   196
    private static void dump(String pid, String options)
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   197
        throws AttachNotSupportedException, IOException,
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   198
               UnsupportedEncodingException {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   199
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   200
        String subopts[] = options.split(",");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   201
        String filename = null;
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   202
        String liveopt = "-all";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   204
        for (int i = 0; i < subopts.length; i++) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   205
            String subopt = subopts[i];
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   206
            if (subopt.equals("live")) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   207
                liveopt = "-live";
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   208
            } else if (subopt.startsWith("file=")) {
53858
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   209
                filename = parseFileName(subopt);
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   210
            }
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   211
        }
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   212
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (filename == null) {
21673
c5d341bc60dd 8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents: 21278
diff changeset
   214
            usage(1);  // invalid options or no filename
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   217
        // dumpHeap is not the same as jcmd GC.heap_dump
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   218
        executeCommandForPid(pid, "dumpheap", filename, liveopt);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   221
    private static void checkForUnsupportedOptions(String[] args) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   222
        // Check arguments for -F, -m, and non-numeric value
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   223
        // and warn the user that SA is not supported anymore
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   224
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   225
        int paramCount = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   227
        for (String s : args) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   228
            if (s.equals("-F")) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   229
                SAOptionError("-F option used");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   230
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   232
            if (s.equals("-heap")) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   233
                SAOptionError("-heap option used");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   234
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   236
            /* Reimplemented using jcmd, output format is different
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   237
               from original one
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   238
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   239
            if (s.equals("-clstats")) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   240
                warnSA("-clstats option used");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   241
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   243
            if (s.equals("-finalizerinfo")) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   244
                warnSA("-finalizerinfo option used");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   245
            }
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   246
            */
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   247
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   248
            if (! s.startsWith("-")) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   249
                paramCount += 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   253
        if (paramCount > 1) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   254
            SAOptionError("More than one non-option argument");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   258
    private static void SAOptionError(String msg) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   259
        System.err.println("Error: " + msg);
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   260
        System.err.println("Cannot connect to core dump or remote debug server. Use jhsdb jmap instead");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   261
        System.exit(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    // print usage message
21673
c5d341bc60dd 8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents: 21278
diff changeset
   265
    private static void usage(int exit) {
c5d341bc60dd 8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents: 21278
diff changeset
   266
        System.err.println("Usage:");
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   267
        System.err.println("    jmap -clstats <pid>");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   268
        System.err.println("        to connect to running process and print class loader statistics");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   269
        System.err.println("    jmap -finalizerinfo <pid>");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   270
        System.err.println("        to connect to running process and print information on objects awaiting finalization");
53858
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   271
        System.err.println("    jmap -histo[:[<histo-options>]] <pid>");
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   272
        System.err.println("        to connect to running process and print histogram of java object heap");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   273
        System.err.println("    jmap -dump:<dump-options> <pid>");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   274
        System.err.println("        to connect to running process and dump java heap");
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
   275
        System.err.println("    jmap -? -h --help");
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
   276
        System.err.println("        to print this help message");
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   277
        System.err.println("");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   278
        System.err.println("    dump-options:");
53858
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   279
        System.err.println("      live         dump only live objects");
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   280
        System.err.println("      all          dump all objects in the heap (default if one of \"live\" or \"all\" is not specified");
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   281
        System.err.println("      format=b     binary format");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   282
        System.err.println("      file=<file>  dump heap to <file>");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   283
        System.err.println("");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   284
        System.err.println("    Example: jmap -dump:live,format=b,file=heap.bin <pid>");
53858
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   285
        System.err.println("");
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   286
        System.err.println("    histo-options:");
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   287
        System.err.println("      live         count only live objects");
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   288
        System.err.println("      all          count all objects in the heap (default if one of \"live\" or \"all\" is not specified)");
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   289
        System.err.println("      file=<file>  dump data to <file>");
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   290
        System.err.println("");
e7cf035682e3 8215622: Add dump to file support for jmap –histo
phh
parents: 52902
diff changeset
   291
        System.err.println("    Example: jmap -histo:live,file=/tmp/histo.data <pid>");
21673
c5d341bc60dd 8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents: 21278
diff changeset
   292
        System.exit(exit);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
}