1 /*
   2  * Copyright (c) 2011, 2019, 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.jcmd;
  27 
  28 import java.io.IOException;
  29 import java.io.InputStream;
  30 import java.io.UnsupportedEncodingException;
  31 import java.util.Collection;
  32 import java.util.Comparator;
  33 import java.net.URISyntaxException;
  34 
  35 import com.sun.tools.attach.AttachOperationFailedException;
  36 import com.sun.tools.attach.VirtualMachine;
  37 import com.sun.tools.attach.VirtualMachineDescriptor;
  38 import com.sun.tools.attach.AttachNotSupportedException;
  39 
  40 import sun.tools.attach.HotSpotVirtualMachine;
  41 import sun.tools.common.ProcessArgumentMatcher;
  42 import sun.tools.common.PrintStreamPrinter;
  43 import sun.tools.jstat.JStatLogger;
  44 import sun.jvmstat.monitor.Monitor;
  45 import sun.jvmstat.monitor.MonitoredHost;
  46 import sun.jvmstat.monitor.MonitoredVm;
  47 import sun.jvmstat.monitor.MonitorException;
  48 import sun.jvmstat.monitor.VmIdentifier;
  49 
  50 public class JCmd {
  51     public static void main(String[] args) {
  52         Arguments arg = null;
  53         try {
  54             arg = new Arguments(args);
  55         } catch (IllegalArgumentException ex) {
  56             System.err.println("Error parsing arguments: " + ex.getMessage()
  57                                + "\n");
  58             Arguments.usage();
  59             System.exit(1);
  60         }
  61 
  62         if (arg.isShowUsage()) {
  63             Arguments.usage();
  64             System.exit(0);
  65         }
  66 
  67         ProcessArgumentMatcher ap = null;
  68         try {
  69             ap = new ProcessArgumentMatcher(arg.getProcessString());
  70         } catch (IllegalArgumentException iae) {
  71             System.err.println("Invalid pid '" + arg.getProcessString()  + "' specified");
  72             System.exit(1);
  73         }
  74 
  75         if (arg.isListProcesses()) {
  76             for (VirtualMachineDescriptor vmd : ap.getVirtualMachineDescriptors(/* include jcmd in listing */)) {
  77                 System.out.println(vmd.id() + " " + vmd.displayName());
  78             }
  79             System.exit(0);
  80         }
  81 
  82         Collection<String> pids = ap.getVirtualMachinePids(JCmd.class);
  83 
  84         if (pids.isEmpty()) {
  85             System.err.println("Could not find any processes matching : '"
  86                                + arg.getProcessString() + "'");
  87             System.exit(1);
  88         }
  89 
  90         boolean success = true;
  91         for (String pid : pids) {
  92             System.out.println(pid + ":");
  93             if (arg.isListCounters()) {
  94                 listCounters(pid);
  95             } else {
  96                 try {
  97                     executeCommandForPid(pid, arg.getCommand());
  98                 } catch(AttachOperationFailedException ex) {
  99                     System.err.println(ex.getMessage());
 100                     success = false;
 101                 } catch(Exception ex) {
 102                     ex.printStackTrace();
 103                     success = false;
 104                 }
 105             }
 106         }
 107         System.exit(success ? 0 : 1);
 108     }
 109 
 110     private static void executeCommandForPid(String pid, String command)
 111         throws AttachNotSupportedException, IOException,
 112                UnsupportedEncodingException {
 113         VirtualMachine vm = VirtualMachine.attach(pid);
 114 
 115         // Cast to HotSpotVirtualMachine as this is an
 116         // implementation specific method.
 117         HotSpotVirtualMachine hvm = (HotSpotVirtualMachine) vm;
 118         String lines[] = command.split("\\n");
 119         for (String line : lines) {
 120             if (line.trim().equals("stop")) {
 121                 break;
 122             }
 123 
 124             InputStream is = hvm.executeJCmd(line);
 125 
 126             if (PrintStreamPrinter.drainUTF8(is, System.out) == 0) {
 127                 System.out.println("Command executed successfully");
 128             }
 129         }
 130         vm.detach();
 131     }
 132 
 133     private static void listCounters(String pid) {
 134         // Code from JStat (can't call it directly since it does System.exit)
 135         VmIdentifier vmId = null;
 136         try {
 137             vmId = new VmIdentifier(pid);
 138         } catch (URISyntaxException e) {
 139             System.err.println("Malformed VM Identifier: " + pid);
 140             return;
 141         }
 142         try {
 143             MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(vmId);
 144             MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, -1);
 145             JStatLogger logger = new JStatLogger(monitoredVm);
 146             logger.printSnapShot("\\w*", // all names
 147                     new AscendingMonitorComparator(), // comparator
 148                     false, // not verbose
 149                     true, // show unsupported
 150                     System.out);
 151             monitoredHost.detach(monitoredVm);
 152         } catch (MonitorException ex) {
 153             ex.printStackTrace();
 154         }
 155     }
 156 
 157     /**
 158      * Class to compare two Monitor objects by name in ascending order.
 159      * (from jstat)
 160      */
 161     static class AscendingMonitorComparator implements Comparator<Monitor> {
 162 
 163         public int compare(Monitor m1, Monitor m2) {
 164             String name1 = m1.getName();
 165             String name2 = m2.getName();
 166             return name1.compareTo(name2);
 167         }
 168     }
 169 }