< prev index next >

src/jdk.jcmd/share/classes/sun/tools/jcmd/JCmd.java

Print this page


   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;


 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;


   1 /*
   2  * Copyright (c) 2011, 2018, 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.IOException;
  30 import java.io.UnsupportedEncodingException;
  31 import java.util.List;
  32 import java.util.Collection;
  33 import java.util.Collections;
  34 import java.util.Comparator;
  35 import java.net.URISyntaxException;
  36 
  37 import com.sun.tools.attach.AttachOperationFailedException;
  38 import com.sun.tools.attach.VirtualMachine;
  39 import com.sun.tools.attach.VirtualMachineDescriptor;
  40 import com.sun.tools.attach.AttachNotSupportedException;
  41 
  42 import sun.tools.attach.HotSpotVirtualMachine;
  43 import sun.tools.common.ProcessArgumentMatcher;
  44 import sun.tools.jstat.JStatLogger;
  45 import sun.jvmstat.monitor.Monitor;
  46 import sun.jvmstat.monitor.MonitoredHost;
  47 import sun.jvmstat.monitor.MonitoredVm;
  48 import sun.jvmstat.monitor.MonitorException;


 104                     success = false;
 105                 }
 106             }
 107         }
 108         System.exit(success ? 0 : 1);
 109     }
 110 
 111     private static void executeCommandForPid(String pid, String command)
 112         throws AttachNotSupportedException, IOException,
 113                UnsupportedEncodingException {
 114         VirtualMachine vm = VirtualMachine.attach(pid);
 115 
 116         // Cast to HotSpotVirtualMachine as this is an
 117         // implementation specific method.
 118         HotSpotVirtualMachine hvm = (HotSpotVirtualMachine) vm;
 119         String lines[] = command.split("\\n");
 120         for (String line : lines) {
 121             if (line.trim().equals("stop")) {
 122                 break;
 123             }
 124             try (InputStream in = hvm.executeJCmd(line);) {

 125                 // read to EOF and just print output
 126                 byte b[] = new byte[256];
 127                 int n;
 128                 boolean messagePrinted = false;
 129                 do {
 130                     n = in.read(b);
 131                     if (n > 0) {
 132                         String s = new String(b, 0, n, "UTF-8");
 133                         System.out.print(s);
 134                         messagePrinted = true;
 135                     }
 136                 } while (n > 0);
 137                 if (!messagePrinted) {
 138                     System.out.println("Command executed successfully");
 139                 }
 140             }
 141         }
 142         vm.detach();
 143     }
 144 
 145     private static void listCounters(String pid) {
 146         // Code from JStat (can't call it directly since it does System.exit)
 147         VmIdentifier vmId = null;
 148         try {
 149             vmId = new VmIdentifier(pid);
 150         } catch (URISyntaxException e) {
 151             System.err.println("Malformed VM Identifier: " + pid);
 152             return;


< prev index next >