Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/tools/jps/Jps.java
38918 views
/*1* Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.jps;2627import java.util.*;28import java.net.*;29import sun.jvmstat.monitor.*;3031/**32* Application to provide a listing of monitorable java processes.33*34* @author Brian Doherty35* @since 1.536*/37public class Jps {3839private static Arguments arguments;4041public static void main(String[] args) {42try {43arguments = new Arguments(args);44} catch (IllegalArgumentException e) {45System.err.println(e.getMessage());46Arguments.printUsage(System.err);47System.exit(1);48}4950if (arguments.isHelp()) {51Arguments.printUsage(System.err);52System.exit(0);53}5455try {56HostIdentifier hostId = arguments.hostId();57MonitoredHost monitoredHost =58MonitoredHost.getMonitoredHost(hostId);5960// get the set active JVMs on the specified host.61Set<Integer> jvms = monitoredHost.activeVms();6263for (Integer jvm: jvms) {64StringBuilder output = new StringBuilder();65Throwable lastError = null;6667int lvmid = jvm;6869output.append(String.valueOf(lvmid));7071if (arguments.isQuiet()) {72System.out.println(output);73continue;74}7576MonitoredVm vm = null;77String vmidString = "//" + lvmid + "?mode=r";7879String errorString = null;8081try {82// Note: The VM associated with the current VM id may83// no longer be running so these queries may fail. We84// already added the VM id to the output stream above.85// If one of the queries fails, then we try to add a86// reasonable message to indicate that the requested87// info is not available.8889errorString = " -- process information unavailable";90VmIdentifier id = new VmIdentifier(vmidString);91vm = monitoredHost.getMonitoredVm(id, 0);9293errorString = " -- main class information unavailable";94output.append(" " + MonitoredVmUtil.mainClass(vm,95arguments.showLongPaths()));9697if (arguments.showMainArgs()) {98errorString = " -- main args information unavailable";99String mainArgs = MonitoredVmUtil.mainArgs(vm);100if (mainArgs != null && mainArgs.length() > 0) {101output.append(" " + mainArgs);102}103}104if (arguments.showVmArgs()) {105errorString = " -- jvm args information unavailable";106String jvmArgs = MonitoredVmUtil.jvmArgs(vm);107if (jvmArgs != null && jvmArgs.length() > 0) {108output.append(" " + jvmArgs);109}110}111if (arguments.showVmFlags()) {112errorString = " -- jvm flags information unavailable";113String jvmFlags = MonitoredVmUtil.jvmFlags(vm);114if (jvmFlags != null && jvmFlags.length() > 0) {115output.append(" " + jvmFlags);116}117}118119errorString = " -- detach failed";120monitoredHost.detach(vm);121122System.out.println(output);123124errorString = null;125} catch (URISyntaxException e) {126// unexpected as vmidString is based on a validated hostid127lastError = e;128assert false;129} catch (Exception e) {130lastError = e;131} finally {132if (errorString != null) {133/*134* we ignore most exceptions, as there are race135* conditions where a JVM in 'jvms' may terminate136* before we get a chance to list its information.137* Other errors, such as access and I/O exceptions138* should stop us from iterating over the complete set.139*/140output.append(errorString);141if (arguments.isDebug()) {142if ((lastError != null)143&& (lastError.getMessage() != null)) {144output.append("\n\t");145output.append(lastError.getMessage());146}147}148System.out.println(output);149if (arguments.printStackTrace()) {150lastError.printStackTrace();151}152continue;153}154}155}156} catch (MonitorException e) {157if (e.getMessage() != null) {158System.err.println(e.getMessage());159} else {160Throwable cause = e.getCause();161if ((cause != null) && (cause.getMessage() != null)) {162System.err.println(cause.getMessage());163} else {164e.printStackTrace();165}166}167System.exit(1);168}169}170}171172173