1 /* 2 * Copyright (c) 2004, 2015, 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 main(String[] args)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(' ').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(' ').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(' ') 110 .append( 111 // multi-line args are permitted 112 jvmArgs.replace("\n", "\\n").replace("\r", "\\r") 113 ); 114 } 115 } 116 if (arguments.showVmFlags()) { 117 errorString = " -- jvm flags information unavailable"; 118 String jvmFlags = MonitoredVmUtil.jvmFlags(vm); 119 if (jvmFlags != null && jvmFlags.length() > 0) { 120 output.append(' ').append(jvmFlags); 121 } 122 } 123 124 errorString = " -- detach failed"; 125 monitoredHost.detach(vm); 126 127 System.out.println(output); 128 129 errorString = null; 130 } catch (URISyntaxException e) { 131 // unexpected as vmidString is based on a validated hostid 132 lastError = e; 133 assert false; 134 } catch (Exception e) { 135 lastError = e; 136 } finally { 137 if (errorString != null) { 138 /* 139 * we ignore most exceptions, as there are race 140 * conditions where a JVM in 'jvms' may terminate 141 * before we get a chance to list its information. 142 * Other errors, such as access and I/O exceptions 143 * should stop us from iterating over the complete set. 144 */ 145 output.append(errorString); 146 if (arguments.isDebug()) { 147 if ((lastError != null) 148 && (lastError.getMessage() != null)) { 149 output.append("\n\t"); 150 output.append(lastError.getMessage()); 151 } 152 } 153 System.out.println(output); 154 if (arguments.printStackTrace()) { 155 lastError.printStackTrace(); 156 } 157 continue; 158 } 159 } 160 } 161 } catch (MonitorException e) { 162 if (e.getMessage() != null) { 163 System.err.println(e.getMessage()); 164 } else { 165 Throwable cause = e.getCause(); 166 if ((cause != null) && (cause.getMessage() != null)) { 167 System.err.println(cause.getMessage()); 168 } else { 169 e.printStackTrace(); 170 } 171 } 172 System.exit(1); 173 } 174 } 175 } 176