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