Path: blob/master/src/jdk.jcmd/share/classes/sun/tools/jcmd/JCmd.java
41159 views
/*1* Copyright (c) 2011, 2019, 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.jcmd;2627import java.io.IOException;28import java.io.InputStream;29import java.io.UnsupportedEncodingException;30import java.util.Collection;31import java.util.Comparator;32import java.net.URISyntaxException;3334import com.sun.tools.attach.AttachOperationFailedException;35import com.sun.tools.attach.VirtualMachine;36import com.sun.tools.attach.VirtualMachineDescriptor;37import com.sun.tools.attach.AttachNotSupportedException;3839import sun.tools.attach.HotSpotVirtualMachine;40import sun.tools.common.ProcessArgumentMatcher;41import sun.tools.common.PrintStreamPrinter;42import sun.tools.jstat.JStatLogger;43import sun.jvmstat.monitor.Monitor;44import sun.jvmstat.monitor.MonitoredHost;45import sun.jvmstat.monitor.MonitoredVm;46import sun.jvmstat.monitor.MonitorException;47import sun.jvmstat.monitor.VmIdentifier;4849public class JCmd {50public static void main(String[] args) {51Arguments arg = null;52try {53arg = new Arguments(args);54} catch (IllegalArgumentException ex) {55System.err.println("Error parsing arguments: " + ex.getMessage()56+ "\n");57Arguments.usage();58System.exit(1);59}6061if (arg.isShowUsage()) {62Arguments.usage();63System.exit(0);64}6566ProcessArgumentMatcher ap = null;67try {68ap = new ProcessArgumentMatcher(arg.getProcessString());69} catch (IllegalArgumentException iae) {70System.err.println("Invalid pid '" + arg.getProcessString() + "' specified");71System.exit(1);72}7374if (arg.isListProcesses()) {75for (VirtualMachineDescriptor vmd : ap.getVirtualMachineDescriptors(/* include jcmd in listing */)) {76System.out.println(vmd.id() + " " + vmd.displayName());77}78System.exit(0);79}8081Collection<String> pids = ap.getVirtualMachinePids(JCmd.class);8283if (pids.isEmpty()) {84System.err.println("Could not find any processes matching : '"85+ arg.getProcessString() + "'");86System.exit(1);87}8889boolean success = true;90for (String pid : pids) {91System.out.println(pid + ":");92if (arg.isListCounters()) {93listCounters(pid);94} else {95try {96executeCommandForPid(pid, arg.getCommand());97} catch(AttachOperationFailedException ex) {98System.err.println(ex.getMessage());99success = false;100} catch(Exception ex) {101ex.printStackTrace();102success = false;103}104}105}106System.exit(success ? 0 : 1);107}108109private static void executeCommandForPid(String pid, String command)110throws AttachNotSupportedException, IOException,111UnsupportedEncodingException {112VirtualMachine vm = VirtualMachine.attach(pid);113114// Cast to HotSpotVirtualMachine as this is an115// implementation specific method.116HotSpotVirtualMachine hvm = (HotSpotVirtualMachine) vm;117String lines[] = command.split("\\n");118for (String line : lines) {119if (line.trim().equals("stop")) {120break;121}122123InputStream is = hvm.executeJCmd(line);124125if (PrintStreamPrinter.drainUTF8(is, System.out) == 0) {126System.out.println("Command executed successfully");127}128}129vm.detach();130}131132private static void listCounters(String pid) {133// Code from JStat (can't call it directly since it does System.exit)134VmIdentifier vmId = null;135try {136vmId = new VmIdentifier(pid);137} catch (URISyntaxException e) {138System.err.println("Malformed VM Identifier: " + pid);139return;140}141try {142MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(vmId);143MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, -1);144JStatLogger logger = new JStatLogger(monitoredVm);145logger.printSnapShot("\\w*", // all names146new AscendingMonitorComparator(), // comparator147false, // not verbose148true, // show unsupported149System.out);150monitoredHost.detach(monitoredVm);151} catch (MonitorException ex) {152ex.printStackTrace();153}154}155156/**157* Class to compare two Monitor objects by name in ascending order.158* (from jstat)159*/160static class AscendingMonitorComparator implements Comparator<Monitor> {161162public int compare(Monitor m1, Monitor m2) {163String name1 = m1.getName();164String name2 = m2.getName();165return name1.compareTo(name2);166}167}168}169170171