1 7 34 package com.sun.tools.example.trace; 35 36 import com.sun.jdi.VirtualMachine; 37 import com.sun.jdi.Bootstrap; 38 import com.sun.jdi.connect.*; 39 40 import java.util.Map ; 41 import java.util.List ; 42 import java.util.Iterator ; 43 44 import java.io.PrintWriter ; 45 import java.io.FileWriter ; 46 import java.io.IOException ; 47 48 56 public class Trace { 57 58 private final VirtualMachine vm; 60 61 private Thread errThread = null; 63 64 private Thread outThread = null; 66 67 private int debugTraceMode = 0; 69 70 private boolean watchFields = false; 72 73 private String [] excludes = {"java.*", "javax.*", "sun.*", 75 "com.sun.*"}; 76 77 80 public static void main(String [] args) { 81 new Trace(args); 82 } 83 84 89 Trace(String [] args) { 90 PrintWriter writer = new PrintWriter (System.out); 91 int inx; 92 for (inx = 0; inx < args.length; ++inx) { 93 String arg = args[inx]; 94 if (arg.charAt(0) != '-') { 95 break; 96 } 97 if (arg.equals("-output")) { 98 try { 99 writer = new PrintWriter (new FileWriter (args[++inx])); 100 } catch (IOException exc) { 101 System.err.println("Cannot open output file: " + args[inx] 102 + " - " + exc); 103 System.exit(1); 104 } 105 } else if (arg.equals("-all")) { 106 excludes = new String [0]; 107 } else if (arg.equals("-fields")) { 108 watchFields = true; 109 } else if (arg.equals("-dbgtrace")) { 110 debugTraceMode = Integer.parseInt(args[++inx]); 111 } else if (arg.equals("-help")) { 112 usage(); 113 System.exit(0); 114 } else { 115 System.err.println("No option: " + arg); 116 usage(); 117 System.exit(1); 118 } 119 } 120 if (inx >= args.length) { 121 System.err.println("<class> missing"); 122 usage(); 123 System.exit(1); 124 } 125 StringBuffer sb = new StringBuffer (); 126 sb.append(args[inx]); 127 for (++inx; inx < args.length; ++inx) { 128 sb.append(' '); 129 sb.append(args[inx]); 130 } 131 vm = launchTarget(sb.toString()); 132 generateTrace(writer); 133 } 134 135 136 142 void generateTrace(PrintWriter writer) { 143 vm.setDebugTraceMode(debugTraceMode); 144 EventThread eventThread = new EventThread(vm, excludes, writer); 145 eventThread.setEventRequests(watchFields); 146 eventThread.start(); 147 redirectOutput(); 148 vm.resume(); 149 150 try { 152 eventThread.join(); 153 errThread.join(); outThread.join(); } catch (InterruptedException exc) { 156 } 158 writer.close(); 159 } 160 161 165 VirtualMachine launchTarget(String mainArgs) { 166 LaunchingConnector connector = findLaunchingConnector(); 167 Map arguments = connectorArguments(connector, mainArgs); 168 try { 169 return connector.launch(arguments); 170 } catch (IOException exc) { 171 throw new Error ("Unable to launch target VM: " + exc); 172 } catch (IllegalConnectorArgumentsException exc) { 173 throw new Error ("Internal error: " + exc); 174 } catch (VMStartException exc) { 175 throw new Error ("Target VM failed to initialize: " + 176 exc.getMessage()); 177 } 178 } 179 180 void redirectOutput() { 181 Process process = vm.process(); 182 183 errThread = new StreamRedirectThread("error reader", 185 process.getErrorStream(), 186 System.err); 187 outThread = new StreamRedirectThread("output reader", 188 process.getInputStream(), 189 System.out); 190 errThread.start(); 191 outThread.start(); 192 } 193 194 197 LaunchingConnector findLaunchingConnector() { 198 List connectors = Bootstrap.virtualMachineManager().allConnectors(); 199 Iterator iter = connectors.iterator(); 200 while (iter.hasNext()) { 201 Connector connector = (Connector)iter.next(); 202 if (connector.name().equals("com.sun.jdi.CommandLineLaunch")) { 203 return (LaunchingConnector)connector; 204 } 205 } 206 throw new Error ("No launching connector"); 207 } 208 209 212 Map connectorArguments(LaunchingConnector connector, String mainArgs) { 213 Map arguments = connector.defaultArguments(); 214 Connector.Argument mainArg = 215 (Connector.Argument)arguments.get("main"); 216 if (mainArg == null) { 217 throw new Error ("Bad launching connector"); 218 } 219 mainArg.setValue(mainArgs); 220 221 if (watchFields) { 222 Connector.Argument optionArg = 224 (Connector.Argument)arguments.get("options"); 225 if (optionArg == null) { 226 throw new Error ("Bad launching connector"); 227 } 228 optionArg.setValue("-classic"); 229 } 230 return arguments; 231 } 232 233 236 void usage() { 237 System.err.println("Usage: java Trace <options> <class> <args>"); 238 System.err.println("<options> are:"); 239 System.err.println( 240 " -output <filename> Output trace to <filename>"); 241 System.err.println( 242 " -all Include system classes in output"); 243 System.err.println( 244 " -help Print this help message"); 245 System.err.println("<class> is the program to trace"); 246 System.err.println("<args> are the arguments to <class>"); 247 } 248 } 249 | Popular Tags |