src/jdk.jcmd/share/classes/sun/tools/jinfo/JInfo.java
author goetz
Tue, 16 Jan 2018 08:48:34 +0100
changeset 48543 7067fe4e054e
parent 47216 71c04702a3d5
child 55758 bbe9c361a477
permissions -rw-r--r--
8189102: All tools should support -?, -h and --help Reviewed-by: kvn, jjg, weijun, alanb, rfield, ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
     2
 * Copyright (c) 2006, 2018, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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.jinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.InputStream;
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
    30
import java.util.Collection;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import com.sun.tools.attach.VirtualMachine;
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    33
import com.sun.tools.attach.VirtualMachineDescriptor;
23058
2e261b4319e9 8036599: Use Diagnostic Commands instead of SA by default in jinfo
sla
parents: 23010
diff changeset
    34
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
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
    36
import sun.tools.common.ProcessArgumentMatcher;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This class is the main class for the JInfo utility. It parses its arguments
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 14342
diff changeset
    40
 * and decides if the command should be satisfied using the VM attach mechanism
23058
2e261b4319e9 8036599: Use Diagnostic Commands instead of SA by default in jinfo
sla
parents: 23010
diff changeset
    41
 * or an SA tool.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
23939
63144ee0e772 8039080: "jinfo server_id@host" fails with "Invalid process identifier"
jbachorik
parents: 23740
diff changeset
    43
final public class JInfo {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    45
    public static void main(String[] args) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        if (args.length == 0) {
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    47
            usage(1); // no arguments
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        }
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    49
        checkForUnsupportedOptions(args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    51
        boolean doFlag = false;
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    52
        boolean doFlags = false;
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    53
        boolean doSysprops = false;
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    54
        int flag = -1;
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    55
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    56
        // Parse the options (arguments starting with "-" )
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    57
        int optionCount = 0;
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    58
        while (optionCount < args.length) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    59
            String arg = args[optionCount];
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    60
            if (!arg.startsWith("-")) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    61
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            }
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    63
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    64
            optionCount++;
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    65
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
    66
            if (arg.equals("-?") ||
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
    67
                arg.equals("-h") ||
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
    68
                arg.equals("--help") ||
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
    69
                // -help: legacy.
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
    70
                arg.equals("-help")) {
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    71
                usage(0);
23058
2e261b4319e9 8036599: Use Diagnostic Commands instead of SA by default in jinfo
sla
parents: 23010
diff changeset
    72
            }
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    73
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    74
            if (arg.equals("-flag")) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    75
                doFlag = true;
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    76
                // Consume the flag
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    77
                if (optionCount < args.length) {
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    78
                    flag = optionCount++;
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    79
                    break;
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    80
                }
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    81
                usage(1);
23939
63144ee0e772 8039080: "jinfo server_id@host" fails with "Invalid process identifier"
jbachorik
parents: 23740
diff changeset
    82
            }
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    83
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    84
            if (arg.equals("-flags")) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    85
                doFlags = true;
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    86
                break;
23939
63144ee0e772 8039080: "jinfo server_id@host" fails with "Invalid process identifier"
jbachorik
parents: 23740
diff changeset
    87
            }
63144ee0e772 8039080: "jinfo server_id@host" fails with "Invalid process identifier"
jbachorik
parents: 23740
diff changeset
    88
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    89
            if (arg.equals("-sysprops")) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    90
                doSysprops = true;
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    91
                break;
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    92
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    95
        int paramCount = args.length - optionCount;
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    96
        if (paramCount != 1) {
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    97
            usage(1);
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    98
        }
23058
2e261b4319e9 8036599: Use Diagnostic Commands instead of SA by default in jinfo
sla
parents: 23010
diff changeset
    99
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   100
        String parg = args[optionCount];
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   101
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   102
        ProcessArgumentMatcher ap = new ProcessArgumentMatcher(parg);
44422
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   103
        Collection<String> pids = ap.getVirtualMachinePids(JInfo.class);
38956
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
        if (pids.isEmpty()) {
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   106
            System.err.println("Could not find any processes matching : '" + parg + "'");
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   107
            System.exit(1);
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   108
        }
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   109
44422
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   110
        for (String pid : pids) {
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   111
            if (pids.size() > 1) {
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   112
                System.out.println("Pid:" + pid);
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   113
            }
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   114
            if (!doFlag && !doFlags && !doSysprops) {
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   115
                // Print flags and sysporps if no options given
44422
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   116
                sysprops(pid);
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   117
                System.out.println();
44422
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   118
                flags(pid);
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   119
                System.out.println();
44422
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   120
                commandLine(pid);
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
   121
            }
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   122
            if (doFlag) {
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   123
                if (flag < 0) {
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   124
                    System.err.println("Missing flag");
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   125
                    usage(1);
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
   126
                }
44422
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   127
                flag(pid, args[flag]);
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   128
            }
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   129
            if (doFlags) {
44422
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   130
                flags(pid);
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   131
            }
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   132
            if (doSysprops) {
44422
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   133
                sysprops(pid);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private static void flag(String pid, String option) throws IOException {
23058
2e261b4319e9 8036599: Use Diagnostic Commands instead of SA by default in jinfo
sla
parents: 23010
diff changeset
   139
        HotSpotVirtualMachine vm = (HotSpotVirtualMachine) attach(pid);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        String flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        InputStream in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        int index = option.indexOf('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            flag = option.substring(0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            String value = option.substring(index + 1);
23058
2e261b4319e9 8036599: Use Diagnostic Commands instead of SA by default in jinfo
sla
parents: 23010
diff changeset
   146
            in = vm.setFlag(flag, value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            char c = option.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            switch (c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                case '+':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    flag = option.substring(1);
23058
2e261b4319e9 8036599: Use Diagnostic Commands instead of SA by default in jinfo
sla
parents: 23010
diff changeset
   152
                    in = vm.setFlag(flag, "1");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                case '-':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    flag = option.substring(1);
23058
2e261b4319e9 8036599: Use Diagnostic Commands instead of SA by default in jinfo
sla
parents: 23010
diff changeset
   156
                    in = vm.setFlag(flag, "0");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    flag = option;
23058
2e261b4319e9 8036599: Use Diagnostic Commands instead of SA by default in jinfo
sla
parents: 23010
diff changeset
   160
                    in = vm.printFlag(flag);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        drain(vm, in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
23058
2e261b4319e9 8036599: Use Diagnostic Commands instead of SA by default in jinfo
sla
parents: 23010
diff changeset
   168
    private static void flags(String pid) throws IOException {
2e261b4319e9 8036599: Use Diagnostic Commands instead of SA by default in jinfo
sla
parents: 23010
diff changeset
   169
        HotSpotVirtualMachine vm = (HotSpotVirtualMachine) attach(pid);
2e261b4319e9 8036599: Use Diagnostic Commands instead of SA by default in jinfo
sla
parents: 23010
diff changeset
   170
        InputStream in = vm.executeJCmd("VM.flags");
2e261b4319e9 8036599: Use Diagnostic Commands instead of SA by default in jinfo
sla
parents: 23010
diff changeset
   171
        System.out.println("VM Flags:");
2e261b4319e9 8036599: Use Diagnostic Commands instead of SA by default in jinfo
sla
parents: 23010
diff changeset
   172
        drain(vm, in);
2e261b4319e9 8036599: Use Diagnostic Commands instead of SA by default in jinfo
sla
parents: 23010
diff changeset
   173
    }
2e261b4319e9 8036599: Use Diagnostic Commands instead of SA by default in jinfo
sla
parents: 23010
diff changeset
   174
24569
9e8d972e5e3b 8041923: Command line output is missing from jinfo
sla
parents: 23939
diff changeset
   175
    private static void commandLine(String pid) throws IOException {
9e8d972e5e3b 8041923: Command line output is missing from jinfo
sla
parents: 23939
diff changeset
   176
        HotSpotVirtualMachine vm = (HotSpotVirtualMachine) attach(pid);
9e8d972e5e3b 8041923: Command line output is missing from jinfo
sla
parents: 23939
diff changeset
   177
        InputStream in = vm.executeJCmd("VM.command_line");
9e8d972e5e3b 8041923: Command line output is missing from jinfo
sla
parents: 23939
diff changeset
   178
        drain(vm, in);
9e8d972e5e3b 8041923: Command line output is missing from jinfo
sla
parents: 23939
diff changeset
   179
    }
9e8d972e5e3b 8041923: Command line output is missing from jinfo
sla
parents: 23939
diff changeset
   180
23058
2e261b4319e9 8036599: Use Diagnostic Commands instead of SA by default in jinfo
sla
parents: 23010
diff changeset
   181
    private static void sysprops(String pid) throws IOException {
2e261b4319e9 8036599: Use Diagnostic Commands instead of SA by default in jinfo
sla
parents: 23010
diff changeset
   182
        HotSpotVirtualMachine vm = (HotSpotVirtualMachine) attach(pid);
2e261b4319e9 8036599: Use Diagnostic Commands instead of SA by default in jinfo
sla
parents: 23010
diff changeset
   183
        InputStream in = vm.executeJCmd("VM.system_properties");
2e261b4319e9 8036599: Use Diagnostic Commands instead of SA by default in jinfo
sla
parents: 23010
diff changeset
   184
        System.out.println("Java System Properties:");
2e261b4319e9 8036599: Use Diagnostic Commands instead of SA by default in jinfo
sla
parents: 23010
diff changeset
   185
        drain(vm, in);
2e261b4319e9 8036599: Use Diagnostic Commands instead of SA by default in jinfo
sla
parents: 23010
diff changeset
   186
    }
2e261b4319e9 8036599: Use Diagnostic Commands instead of SA by default in jinfo
sla
parents: 23010
diff changeset
   187
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    // Attach to <pid>, exiting if we fail to attach
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    private static VirtualMachine attach(String pid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            return VirtualMachine.attach(pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            String msg = x.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            if (msg != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                System.err.println(pid + ": " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                x.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            return null; // keep compiler happy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    // Read the stream from the target VM until EOF, then detach
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    private static void drain(VirtualMachine vm, InputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        // read to EOF and just print output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        byte b[] = new byte[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            n = in.read(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                String s = new String(b, 0, n, "UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                System.out.print(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        } while (n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        vm.detach();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   220
    private static void checkForUnsupportedOptions(String[] args) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   221
        // Check arguments for -F, and non-numeric value
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   222
        // and warn the user that SA is not supported anymore
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
   223
        int maxCount = 1;
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   224
        int paramCount = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   226
        for (String s : args) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   227
            if (s.equals("-F")) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   228
                SAOptionError("-F option used");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   229
            }
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
   230
            if (s.equals("-flag")) {
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
   231
                maxCount = 2;
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
   232
            }
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   233
            if (! s.startsWith("-")) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   234
                paramCount += 1;
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   235
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
   238
        if (paramCount > maxCount) {
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
   239
            SAOptionError("More than " + maxCount + " non-option argument");
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   240
        }
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   241
    }
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   242
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   243
    private static void SAOptionError(String msg) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   244
        System.err.println("Error: " + msg);
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   245
        System.err.println("Cannot connect to core dump or remote debug server. Use jhsdb jinfo instead");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   246
        System.exit(1);
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
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   249
     // print usage message
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   250
    private static void usage(int exit) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   251
        System.err.println("Usage:");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   252
        System.err.println("    jinfo <option> <pid>");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   253
        System.err.println("       (to connect to a running process)");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   254
        System.err.println("");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   255
        System.err.println("where <option> is one of:");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   256
        System.err.println("    -flag <name>         to print the value of the named VM flag");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   257
        System.err.println("    -flag [+|-]<name>    to enable or disable the named VM flag");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   258
        System.err.println("    -flag <name>=<value> to set the named VM flag to the given value");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   259
        System.err.println("    -flags               to print VM flags");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   260
        System.err.println("    -sysprops            to print Java system properties");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   261
        System.err.println("    <no option>          to print both VM flags and system properties");
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
   262
        System.err.println("    -? | -h | --help | -help to print this help message");
21673
c5d341bc60dd 8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents: 21278
diff changeset
   263
        System.exit(exit);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
}