< prev index next >

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

Print this page
rev 55734 : 8227868: jinfo and jstack can fail converting UTF8 output to strings
Reviewed-by:


   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()) {


 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




   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()) {


 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


< prev index next >