< prev index next >

src/jdk.jcmd/share/classes/sun/tools/jstack/JStack.java

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


  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.jstack;
  27 
  28 import java.io.InputStream;
  29 import java.util.Collection;
  30 
  31 import com.sun.tools.attach.VirtualMachine;
  32 import com.sun.tools.attach.VirtualMachineDescriptor;
  33 import sun.tools.attach.HotSpotVirtualMachine;
  34 import sun.tools.common.ProcessArgumentMatcher;

  35 
  36 /*
  37  * This class is the main class for the JStack utility. It parses its arguments
  38  * and decides if the command should be executed by the SA JStack tool or by
  39  * obtained the thread dump from a target process using the VM attach mechanism
  40  */
  41 public class JStack {
  42 
  43     public static void main(String[] args) throws Exception {
  44         if (args.length == 0) {
  45             usage(1); // no arguments
  46         }
  47 
  48         checkForUnsupportedOptions(args);
  49 
  50         boolean locks = false;
  51         boolean extended = false;
  52 
  53         // Parse the options (arguments starting with "-" )
  54         int optionCount = 0;


 111     }
 112 
 113     // Attach to pid and perform a thread dump
 114     private static void runThreadDump(String pid, String args[]) throws Exception {
 115         VirtualMachine vm = null;
 116         try {
 117             vm = VirtualMachine.attach(pid);
 118         } catch (Exception x) {
 119             String msg = x.getMessage();
 120             if (msg != null) {
 121                 System.err.println(pid + ": " + msg);
 122             } else {
 123                 x.printStackTrace();
 124             }
 125             System.exit(1);
 126         }
 127 
 128         // Cast to HotSpotVirtualMachine as this is implementation specific
 129         // method.
 130         InputStream in = ((HotSpotVirtualMachine)vm).remoteDataDump((Object[])args);
 131 
 132         // read to EOF and just print output
 133         byte b[] = new byte[256];
 134         int n;
 135         do {
 136             n = in.read(b);
 137             if (n > 0) {
 138                 String s = new String(b, 0, n, "UTF-8");
 139                 System.out.print(s);
 140             }
 141         } while (n > 0);
 142         in.close();
 143         vm.detach();
 144     }
 145 
 146     private static void checkForUnsupportedOptions(String[] args) {
 147         // Check arguments for -F, -m, and non-numeric value
 148         // and warn the user that SA is not supported anymore
 149 
 150         int paramCount = 0;
 151 
 152         for (String s : args) {
 153             if (s.equals("-F")) {
 154                 SAOptionError("-F option used");
 155             }
 156 
 157             if (s.equals("-m")) {
 158                 SAOptionError("-m option used");
 159             }
 160 
 161             if (! s.startsWith("-")) {
 162                 paramCount += 1;




  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.jstack;
  27 
  28 import java.io.InputStream;
  29 import java.util.Collection;
  30 
  31 import com.sun.tools.attach.VirtualMachine;

  32 import sun.tools.attach.HotSpotVirtualMachine;
  33 import sun.tools.common.ProcessArgumentMatcher;
  34 import sun.tools.common.PrintStreamPrinter;
  35 
  36 /*
  37  * This class is the main class for the JStack utility. It parses its arguments
  38  * and decides if the command should be executed by the SA JStack tool or by
  39  * obtained the thread dump from a target process using the VM attach mechanism
  40  */
  41 public class JStack {
  42 
  43     public static void main(String[] args) throws Exception {
  44         if (args.length == 0) {
  45             usage(1); // no arguments
  46         }
  47 
  48         checkForUnsupportedOptions(args);
  49 
  50         boolean locks = false;
  51         boolean extended = false;
  52 
  53         // Parse the options (arguments starting with "-" )
  54         int optionCount = 0;


 111     }
 112 
 113     // Attach to pid and perform a thread dump
 114     private static void runThreadDump(String pid, String args[]) throws Exception {
 115         VirtualMachine vm = null;
 116         try {
 117             vm = VirtualMachine.attach(pid);
 118         } catch (Exception x) {
 119             String msg = x.getMessage();
 120             if (msg != null) {
 121                 System.err.println(pid + ": " + msg);
 122             } else {
 123                 x.printStackTrace();
 124             }
 125             System.exit(1);
 126         }
 127 
 128         // Cast to HotSpotVirtualMachine as this is implementation specific
 129         // method.
 130         InputStream in = ((HotSpotVirtualMachine)vm).remoteDataDump((Object[])args);

 131         // read to EOF and just print output
 132         PrintStreamPrinter.drainUTF8(in, System.out);









 133         vm.detach();
 134     }
 135 
 136     private static void checkForUnsupportedOptions(String[] args) {
 137         // Check arguments for -F, -m, and non-numeric value
 138         // and warn the user that SA is not supported anymore
 139 
 140         int paramCount = 0;
 141 
 142         for (String s : args) {
 143             if (s.equals("-F")) {
 144                 SAOptionError("-F option used");
 145             }
 146 
 147             if (s.equals("-m")) {
 148                 SAOptionError("-m option used");
 149             }
 150 
 151             if (! s.startsWith("-")) {
 152                 paramCount += 1;


< prev index next >