jdk/src/share/classes/sun/tools/jps/Jps.java
author dcubed
Mon, 14 Feb 2011 09:31:58 -0800
changeset 8390 f9ed509a2634
parent 5506 202f599c92aa
child 11125 99b115114fa3
permissions -rw-r--r--
6637230: 2/3 jps doesn't work for application waiting for direct attach Summary: Properly handle exceptions thrown when querying a monitored VM. Reviewed-by: dsamersoff, swamyv
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
8390
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
     2
 * Copyright (c) 2004, 2011, 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.jps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.jvmstat.monitor.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Application to provide a listing of monitorable java processes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author Brian Doherty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class Jps {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static Arguments arguments;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            arguments = new Arguments(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            System.err.println(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            Arguments.printUsage(System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        if (arguments.isHelp()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            Arguments.printUsage(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            System.exit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            HostIdentifier hostId = arguments.hostId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            MonitoredHost monitoredHost =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                    MonitoredHost.getMonitoredHost(hostId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            // get the set active JVMs on the specified host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            Set jvms = monitoredHost.activeVms();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            for (Iterator j = jvms.iterator(); j.hasNext(); /* empty */ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                StringBuilder output = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                Throwable lastError = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                int lvmid = ((Integer)j.next()).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                output.append(String.valueOf(lvmid));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                if (arguments.isQuiet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    System.out.println(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                MonitoredVm vm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                String vmidString = "//" + lvmid + "?mode=r";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
8390
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    80
                String errorString = null;
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    81
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                try {
8390
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    83
                    // Note: The VM associated with the current VM id may
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    84
                    // no longer be running so these queries may fail. We
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    85
                    // already added the VM id to the output stream above.
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    86
                    // If one of the queries fails, then we try to add a
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    87
                    // reasonable message to indicate that the requested
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    88
                    // info is not available.
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    89
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    90
                    errorString = " -- process information unavailable";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    VmIdentifier id = new VmIdentifier(vmidString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    vm = monitoredHost.getMonitoredVm(id, 0);
8390
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    93
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    94
                    errorString = " -- main class information unavailable";
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    95
                    output.append(" " + MonitoredVmUtil.mainClass(vm,
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    96
                            arguments.showLongPaths()));
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    97
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    98
                    if (arguments.showMainArgs()) {
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    99
                        errorString = " -- main args information unavailable";
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   100
                        String mainArgs = MonitoredVmUtil.mainArgs(vm);
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   101
                        if (mainArgs != null && mainArgs.length() > 0) {
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   102
                            output.append(" " + mainArgs);
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   103
                        }
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   104
                    }
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   105
                    if (arguments.showVmArgs()) {
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   106
                        errorString = " -- jvm args information unavailable";
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   107
                        String jvmArgs = MonitoredVmUtil.jvmArgs(vm);
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   108
                        if (jvmArgs != null && jvmArgs.length() > 0) {
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   109
                          output.append(" " + jvmArgs);
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   110
                        }
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   111
                    }
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   112
                    if (arguments.showVmFlags()) {
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   113
                        errorString = " -- jvm flags information unavailable";
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   114
                        String jvmFlags = MonitoredVmUtil.jvmFlags(vm);
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   115
                        if (jvmFlags != null && jvmFlags.length() > 0) {
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   116
                            output.append(" " + jvmFlags);
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   117
                        }
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   118
                    }
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   119
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   120
                    errorString = " -- detach failed";
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   121
                    monitoredHost.detach(vm);
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   122
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   123
                    System.out.println(output);
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   124
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   125
                    errorString = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                } catch (URISyntaxException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    // unexpected as vmidString is based on a validated hostid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    lastError = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    lastError = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                } finally {
8390
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   133
                    if (errorString != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                         * we ignore most exceptions, as there are race
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                         * conditions where a JVM in 'jvms' may terminate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                         * before we get a chance to list its information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                         * Other errors, such as access and I/O exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                         * should stop us from iterating over the complete set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                         */
8390
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   141
                        output.append(errorString);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                        if (arguments.isDebug()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                            if ((lastError != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                                    && (lastError.getMessage() != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                output.append("\n\t");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                                output.append(lastError.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        System.out.println(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                        if (arguments.printStackTrace()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                            lastError.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        } catch (MonitorException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            if (e.getMessage() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                System.err.println(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                Throwable cause = e.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                if ((cause != null) && (cause.getMessage() != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    System.err.println(cause.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
}