1 /*
   2  * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.tools.jps;
  27 
  28 import java.util.*;
  29 import java.net.*;
  30 import sun.jvmstat.monitor.*;
  31 
  32 /**
  33  * Application to provide a listing of monitorable java processes.
  34  *
  35  * @author Brian Doherty
  36  * @since 1.5
  37  */
  38 public class Jps {
  39 
  40     private static Arguments arguments;
  41 
  42     public static void main(String[] args) {
  43         try {
  44             arguments = new Arguments(args);
  45         } catch (IllegalArgumentException e) {
  46             System.err.println(e.getMessage());
  47             Arguments.printUsage(System.err);
  48             System.exit(1);
  49         }
  50 
  51         if (arguments.isHelp()) {
  52             Arguments.printUsage(System.err);
  53             System.exit(0);
  54         }
  55 
  56         try {
  57             HostIdentifier hostId = arguments.hostId();
  58             MonitoredHost monitoredHost =
  59                     MonitoredHost.getMonitoredHost(hostId);
  60 
  61             // get the set active JVMs on the specified host.
  62             Set<Integer> jvms = monitoredHost.activeVms();
  63 
  64             for (Integer jvm: jvms) {
  65                 StringBuilder output = new StringBuilder();
  66                 Throwable lastError = null;
  67 
  68                 int lvmid = jvm;
  69 
  70                 output.append(String.valueOf(lvmid));
  71 
  72                 if (arguments.isQuiet()) {
  73                     System.out.println(output);
  74                     continue;
  75                 }
  76 
  77                 MonitoredVm vm = null;
  78                 String vmidString = "//" + lvmid + "?mode=r";
  79 
  80                 String errorString = null;
  81 
  82                 try {
  83                     // Note: The VM associated with the current VM id may
  84                     // no longer be running so these queries may fail. We
  85                     // already added the VM id to the output stream above.
  86                     // If one of the queries fails, then we try to add a
  87                     // reasonable message to indicate that the requested
  88                     // info is not available.
  89 
  90                     errorString = " -- process information unavailable";
  91                     VmIdentifier id = new VmIdentifier(vmidString);
  92                     vm = monitoredHost.getMonitoredVm(id, 0);
  93 
  94                     errorString = " -- main class information unavailable";
  95                     output.append(" " + MonitoredVmUtil.mainClass(vm,
  96                             arguments.showLongPaths()));
  97 
  98                     if (arguments.showMainArgs()) {
  99                         errorString = " -- main args information unavailable";
 100                         String mainArgs = MonitoredVmUtil.mainArgs(vm);
 101                         if (mainArgs != null && mainArgs.length() > 0) {
 102                             output.append(" " + mainArgs);
 103                         }
 104                     }
 105                     if (arguments.showVmArgs()) {
 106                         errorString = " -- jvm args information unavailable";
 107                         String jvmArgs = MonitoredVmUtil.jvmArgs(vm);
 108                         if (jvmArgs != null && jvmArgs.length() > 0) {
 109                           output.append(" " + jvmArgs);
 110                         }
 111                     }
 112                     if (arguments.showVmFlags()) {
 113                         errorString = " -- jvm flags information unavailable";
 114                         String jvmFlags = MonitoredVmUtil.jvmFlags(vm);
 115                         if (jvmFlags != null && jvmFlags.length() > 0) {
 116                             output.append(" " + jvmFlags);
 117                         }
 118                     }
 119 
 120                     errorString = " -- detach failed";
 121                     monitoredHost.detach(vm);
 122 
 123                     System.out.println(output);
 124 
 125                     errorString = null;
 126                 } catch (URISyntaxException e) {
 127                     // unexpected as vmidString is based on a validated hostid
 128                     lastError = e;
 129                     assert false;
 130                 } catch (Exception e) {
 131                     lastError = e;
 132                 } finally {
 133                     if (errorString != null) {
 134                         /*
 135                          * we ignore most exceptions, as there are race
 136                          * conditions where a JVM in 'jvms' may terminate
 137                          * before we get a chance to list its information.
 138                          * Other errors, such as access and I/O exceptions
 139                          * should stop us from iterating over the complete set.
 140                          */
 141                         output.append(errorString);
 142                         if (arguments.isDebug()) {
 143                             if ((lastError != null)
 144                                     && (lastError.getMessage() != null)) {
 145                                 output.append("\n\t");
 146                                 output.append(lastError.getMessage());
 147                             }
 148                         }
 149                         System.out.println(output);
 150                         if (arguments.printStackTrace()) {
 151                             lastError.printStackTrace();
 152                         }
 153                         continue;
 154                     }
 155                 }
 156             }
 157         } catch (MonitorException e) {
 158             if (e.getMessage() != null) {
 159                 System.err.println(e.getMessage());
 160             } else {
 161                 Throwable cause = e.getCause();
 162                 if ((cause != null) && (cause.getMessage() != null)) {
 163                     System.err.println(cause.getMessage());
 164                 } else {
 165                     e.printStackTrace();
 166                 }
 167             }
 168             System.exit(1);
 169         }
 170     }
 171 }