/** Second part of debugger start procedure. */ private void startDebugger() { threadManager = new ThreadManager(this); setBreakpoints(); updateWatches(); println(bundle.getString("CTL_Debugger_running"), STL_OUT); setDebuggerState(DEBUGGER_RUNNING); virtualMachine.resume(); // start refresh thread ................................................. if (debuggerThread != null) debuggerThread.stop(); debuggerThread = new Thread( new Runnable() { public void run() { for (; ; ) { try { Thread.sleep(5000); } catch (InterruptedException ex) { } if (getState() == DEBUGGER_RUNNING) threadGroup.refresh(); } } }, "Debugger refresh thread"); // NOI18N debuggerThread.setPriority(Thread.MIN_PRIORITY); debuggerThread.start(); }
/** Redirect a VMs output and error streams to System.out and System.err */ protected void redirectStreams(VirtualMachine vm) { MessageSiphon ms = new MessageSiphon(process.getErrorStream(), this); errThread = ms.getThread(); outThread = new StreamRedirectThread("VM output reader", process.getInputStream(), System.out); errThread.start(); outThread.start(); }
/** Disconnects from running debugged process. */ public void disconnect() throws DebuggerException { if (breakpointMain != null) { for (int x = 0; x < breakpointMain.length; x++) breakpointMain[x].remove(); breakpointMain = null; } try { if (virtualMachine != null) virtualMachine.dispose(); } catch (VMDisconnectedException e) { } if (threadManager != null) threadManager.finish(); if (debuggerThread != null) { debuggerThread.interrupt(); debuggerThread.stop(); } super.finishDebugger(); }
/** Hang so that test fails */ static void hang() { try { // ten minute nap Thread.currentThread().sleep(10 * 60 * 1000); } catch (InterruptedException exc) { // shouldn't happen } }
/** * Generate the trace. Enable events, start thread to display events, start threads to forward * remote error and output streams, resume the remote VM, wait for the final event, and shutdown. */ protected void generateTrace() { // vm.setDebugTraceMode(debugTraceMode); // vm.setDebugTraceMode(VirtualMachine.TRACE_ALL); // vm.setDebugTraceMode(VirtualMachine.TRACE_NONE); // formerly, seems to have no effect // For internal debugging PrintWriter writer = null; // Calling this seems to set something internally to make the // Eclipse JDI wake up. Without it, an ObjectCollectedException // is thrown on excReq.enable(). No idea why this works, // but at least exception handling has returned. (Suspect that it may // block until all or at least some threads are available, meaning // that the app has launched and we have legit objects to talk to). vm.allThreads(); // The bug may not have been noticed because the test suite waits for // a thread to be available, and queries it by calling allThreads(). // See org.eclipse.debug.jdi.tests.AbstractJDITest for the example. EventRequestManager mgr = vm.eventRequestManager(); // get only the uncaught exceptions ExceptionRequest excReq = mgr.createExceptionRequest(null, false, true); // System.out.println(excReq); // this version reports all exceptions, caught or uncaught // ExceptionRequest excReq = mgr.createExceptionRequest(null, true, true); // suspend so we can step excReq.setSuspendPolicy(EventRequest.SUSPEND_ALL); // excReq.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD); // excReq.setSuspendPolicy(EventRequest.SUSPEND_NONE); // another option? excReq.enable(); Thread eventThread = new Thread() { public void run() { try { boolean connected = true; while (connected) { EventQueue eventQueue = vm.eventQueue(); // remove() blocks until event(s) available EventSet eventSet = eventQueue.remove(); // listener.vmEvent(eventSet); for (Event event : eventSet) { // System.out.println("EventThread.handleEvent -> " + event); if (event instanceof VMStartEvent) { vm.resume(); } else if (event instanceof ExceptionEvent) { // for (ThreadReference thread : vm.allThreads()) { // System.out.println("thread : " + thread); //// thread.suspend(); // } exceptionEvent((ExceptionEvent) event); } else if (event instanceof VMDisconnectEvent) { connected = false; } } } // } catch (VMDisconnectedException e) { // Logger.getLogger(VMEventReader.class.getName()).log(Level.INFO, // "VMEventReader quit on VM disconnect"); } catch (Exception e) { System.err.println("crashed in event thread due to " + e.getMessage()); // Logger.getLogger(VMEventReader.class.getName()).log(Level.SEVERE, // "VMEventReader quit", e); e.printStackTrace(); } } }; eventThread.start(); errThread = new MessageSiphon(process.getErrorStream(), this).getThread(); outThread = new StreamRedirectThread("JVM stdout Reader", process.getInputStream(), System.out); errThread.start(); outThread.start(); // Shutdown begins when event thread terminates try { if (eventThread != null) eventThread.join(); // is this the problem? // System.out.println("in here"); // Bug #852 tracked to this next line in the code. // http://dev.processing.org/bugs/show_bug.cgi?id=852 errThread.join(); // Make sure output is forwarded // System.out.println("and then"); outThread.join(); // before we exit // System.out.println("finished join for errThread and outThread"); // At this point, disable the run button. // This happens when the sketch is exited by hitting ESC, // or the user manually closes the sketch window. // TODO this should be handled better, should it not? if (editor != null) { editor.deactivateRun(); } } catch (InterruptedException exc) { // we don't interrupt } // System.out.println("and leaving"); if (writer != null) writer.close(); }
public boolean launchVirtualMachine(boolean presenting) { String[] vmParams = getMachineParams(); String[] sketchParams = getSketchParams(presenting); int port = 8000 + (int) (Math.random() * 1000); String portStr = String.valueOf(port); // Older (Java 1.5 and earlier) version, go figure // String jdwpArg = "-Xrunjdwp:transport=dt_socket,address=" + portStr + // ",server=y,suspend=y"; // String debugArg = "-Xdebug"; // Newer (Java 1.5+) version that uses JVMTI String jdwpArg = "-agentlib:jdwp=transport=dt_socket,address=" + portStr + ",server=y,suspend=y"; // Everyone works the same under Java 7 (also on OS X) String[] commandArgs = new String[] {Base.getJavaPath(), jdwpArg}; /* String[] commandArgs = null; if (!Base.isMacOS()) { commandArgs = new String[] { Base.getJavaPath(), jdwpArg }; } else { // Decided to just set this to 1.6 only, because otherwise it's gonna // be a shitshow if folks are getting Apple's 1.6 with 32-bit and // Oracle's 1.7 when run in 64-bit mode. ("Why does my sketch suck in // 64-bit? Why is retina broken?) // The --request flag will prompt to install Apple's 1.6 JVM if none is // available. We're specifying 1.6 so that we can get support for both // 32- and 64-bit, because Oracle won't be releasing Java 1.7 in 32-bit. // Helpfully, the --request flag is not present on Mac OS X 10.6 // (luckily it is also not needed, because 1.6 is installed by default) // but it requires an additional workaround to not use that flag, // otherwise will see an error about an unsupported option. The flag is // available with 10.7 and 10.8, the only other supported versions of // OS X at this point, because we require 10.6.8 and higher. That also // means we don't need to check for any other OS versions, the user is // a douchebag and modifies Info.plist to get around the restriction. if (false) { if (System.getProperty("os.version").startsWith("10.6")) { commandArgs = new String[] { "/usr/libexec/java_home", "--version", "1.6", "--exec", "java", "-d" + Base.getNativeBits(), jdwpArg }; } else { // for 10.7, 10.8, etc commandArgs = new String[] { "/usr/libexec/java_home", "--request", // install on-demand "--version", "1.6", "--exec", "java", "-d" + Base.getNativeBits(), // debugArg, jdwpArg }; } } else { // testing jdk-7u40 commandArgs = new String[] { //"/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/bin/java", Base.getJavaPath(), jdwpArg }; } } */ commandArgs = PApplet.concat(commandArgs, vmParams); commandArgs = PApplet.concat(commandArgs, sketchParams); // PApplet.println(commandArgs); // commandArg.setValue(commandArgs); launchJava(commandArgs); AttachingConnector connector = (AttachingConnector) findConnector("com.sun.jdi.SocketAttach"); // PApplet.println(connector); // gets the defaults Map arguments = connector.defaultArguments(); // Connector.Argument addressArg = // (Connector.Argument)arguments.get("address"); // addressArg.setValue(addr); Connector.Argument portArg = (Connector.Argument) arguments.get("port"); portArg.setValue(portStr); // Connector.Argument timeoutArg = // (Connector.Argument)arguments.get("timeout"); // timeoutArg.setValue("10000"); // PApplet.println(connector); // prints the current // com.sun.tools.jdi.AbstractLauncher al; // com.sun.tools.jdi.RawCommandLineLauncher rcll; // System.out.println(PApplet.javaVersion); // http://java.sun.com/j2se/1.5.0/docs/guide/jpda/conninv.html#sunlaunch try { // boolean available = false; // while (!available) { while (true) { try { vm = connector.attach(arguments); // vm = connector.attach(arguments); if (vm != null) { // generateTrace(); // available = true; return true; } } catch (IOException e) { // System.out.println("waiting"); // e.printStackTrace(); try { Thread.sleep(100); } catch (InterruptedException e1) { e1.printStackTrace(); } } } // } catch (IOException exc) { // throw new Error("Unable to launch target VM: " + exc); } catch (IllegalConnectorArgumentsException exc) { throw new Error("Internal error: " + exc); } }
public boolean launchVirtualMachine(boolean presenting) { String[] vmParams = getMachineParams(); String[] sketchParams = getSketchParams(presenting); // PApplet.printArray(sketchParams); int port = 8000 + (int) (Math.random() * 1000); String portStr = String.valueOf(port); // Older (Java 1.5 and earlier) version, go figure // String jdwpArg = "-Xrunjdwp:transport=dt_socket,address=" + portStr + // ",server=y,suspend=y"; // String debugArg = "-Xdebug"; // Newer (Java 1.5+) version that uses JVMTI String jdwpArg = "-agentlib:jdwp=transport=dt_socket,address=" + portStr + ",server=y,suspend=y"; // Everyone works the same under Java 7 (also on OS X) String[] commandArgs = new String[] {Base.getJavaPath(), jdwpArg}; commandArgs = PApplet.concat(commandArgs, vmParams); commandArgs = PApplet.concat(commandArgs, sketchParams); // PApplet.println(commandArgs); // commandArg.setValue(commandArgs); launchJava(commandArgs); AttachingConnector connector = (AttachingConnector) findConnector("com.sun.jdi.SocketAttach"); // PApplet.println(connector); // gets the defaults Map<String, Argument> arguments = connector.defaultArguments(); // Connector.Argument addressArg = // (Connector.Argument)arguments.get("address"); // addressArg.setValue(addr); Connector.Argument portArg = arguments.get("port"); portArg.setValue(portStr); // Connector.Argument timeoutArg = // (Connector.Argument)arguments.get("timeout"); // timeoutArg.setValue("10000"); // PApplet.println(connector); // prints the current // com.sun.tools.jdi.AbstractLauncher al; // com.sun.tools.jdi.RawCommandLineLauncher rcll; // System.out.println(PApplet.javaVersion); // http://java.sun.com/j2se/1.5.0/docs/guide/jpda/conninv.html#sunlaunch try { // boolean available = false; // while (!available) { while (true) { try { vm = connector.attach(arguments); // vm = connector.attach(arguments); if (vm != null) { // generateTrace(); // available = true; return true; } } catch (IOException e) { // System.out.println("waiting"); // e.printStackTrace(); try { Thread.sleep(100); } catch (InterruptedException e1) { e1.printStackTrace(sketchErr); } } } // } catch (IOException exc) { // throw new Error("Unable to launch target VM: " + exc); } catch (IllegalConnectorArgumentsException exc) { throw new Error("Internal error: " + exc); } }
class EarlyReturnTarg { static boolean debuggerWatching = false; static int failureCount = 0; /* * These are the values that will be used by methods * returning normally. */ static URL[] urls = new URL[1]; public static byte byteValue = 89; public static char charValue = 'x'; public static double doubleValue = 2.2; public static float floatValue = 3.3f; public static int intValue = 1; public static long longValue = Long.MAX_VALUE; public static short shortValue = 8; public static boolean booleanValue = false; public static Class classValue = Object.class; public static ClassLoader classLoaderValue; { try { urls[0] = new URL("hi there"); } catch (java.net.MalformedURLException ee) { } classLoaderValue = new URLClassLoader(urls); } public static Thread threadValue = Thread.currentThread(); public static ThreadGroup threadGroupValue = threadValue.getThreadGroup(); public static String stringValue = "abc"; public static int[] intArrayValue = new int[] {1, 2, 3}; public static EarlyReturnTarg objectValue = new EarlyReturnTarg(); public String ivar = stringValue; /* * These are the values that will be used by methods * returning early. These are != the normal values * defined above. */ static URL[] eurls = new URL[1]; public static byte ebyteValue = 42; public static char echarValue = 'a'; public static double edoubleValue = 6.6; public static float efloatValue = 9.9f; public static int eintValue = 7; public static long elongValue = Long.MIN_VALUE; public static short eshortValue = 3; public static boolean ebooleanValue = true; public static Class eclassValue = String.class; public static ClassLoader eclassLoaderValue; { try { urls[0] = new URL("been there, done that"); } catch (java.net.MalformedURLException ee) { } classLoaderValue = new URLClassLoader(urls); } public static Thread ethreadValue; public static ThreadGroup ethreadGroupValue; public static String estringValue = "wxyz"; public static int[] eintArrayValue = new int[] {10, 11, 12}; public static java.util.Date eobjectValue = new java.util.Date(); // Used to check the return values seen on the debugee side public static boolean chk(byte v) { return v == (debuggerWatching ? ebyteValue : byteValue); } public static boolean chk(char v) { return v == (debuggerWatching ? echarValue : charValue); } public static boolean chk(double v) { return v == (debuggerWatching ? edoubleValue : doubleValue); } public static boolean chk(float v) { return v == (debuggerWatching ? efloatValue : floatValue); } public static boolean chk(int v) { return v == (debuggerWatching ? eintValue : intValue); } public static boolean chk(long v) { return v == (debuggerWatching ? elongValue : longValue); } public static boolean chk(short v) { return v == (debuggerWatching ? eshortValue : shortValue); } public static boolean chk(boolean v) { return v == (debuggerWatching ? ebooleanValue : booleanValue); } public static boolean chk(String v) { return v.equals(debuggerWatching ? estringValue : stringValue); } public static boolean chk(Object v) { return v.equals(debuggerWatching ? eobjectValue : objectValue); } // Used to show which set of tests follows public static String s_show(String p1) { return p1; } // These are the static methods public static byte s_bytef(int p1) { return byteValue; } public static char s_charf() { return charValue; } public static double s_doublef() { return doubleValue; } public static float s_floatf() { return floatValue; } public static int s_intf() { return intValue; } public static long s_longf() { return longValue; } public static short s_shortf() { return shortValue; } public static boolean s_booleanf() { return booleanValue; } public static String s_stringf() { return stringValue; } public static Class s_classf() { return classValue; } public static ClassLoader s_classLoaderf() { return classLoaderValue; } public static Thread s_threadf() { return threadValue; } public static ThreadGroup s_threadGroupf() { return threadGroupValue; } public static int[] s_intArrayf() { return intArrayValue; } public static Object s_nullObjectf() { return null; } public static Object s_objectf() { return objectValue; } public static void s_voidf() { System.err.println("debugee in s_voidf"); } // These are the instance methods public byte i_bytef(int p1) { return byteValue; } public char i_charf() { return charValue; } public double i_doublef() { return doubleValue; } public float i_floatf() { return floatValue; } public int i_intf() { return intValue; } public long i_longf() { return longValue; } public short i_shortf() { return shortValue; } public boolean i_booleanf() { return booleanValue; } public String i_stringf() { return stringValue; } public Class i_classf() { return classValue; } public ClassLoader i_classLoaderf() { return classLoaderValue; } public Thread i_threadf() { return threadValue; } public ThreadGroup i_threadGroupf() { return threadGroupValue; } public int[] i_intArrayf() { return intArrayValue; } public Object i_nullObjectf() { return null; } public Object i_objectf() { return objectValue; } public void i_voidf() {} static void doit(EarlyReturnTarg xx) throws Exception { System.err.print("debugee in doit "); if (debuggerWatching) { System.err.println("with a debugger watching. Early returns expected."); } else { System.err.println("with no debugger watching. Normal returns."); } s_show("========== Testing static methods ================"); if (!chk(s_bytef(88))) failureCount++; if (!chk(s_charf())) failureCount++; if (!chk(s_doublef())) failureCount++; if (!chk(s_floatf())) failureCount++; if (!chk(s_intf())) failureCount++; if (!chk(s_longf())) failureCount++; if (!chk(s_shortf())) failureCount++; if (!chk(s_booleanf())) failureCount++; if (!chk(s_stringf())) failureCount++; s_classf(); s_classLoaderf(); s_threadf(); s_threadGroupf(); s_intArrayf(); s_nullObjectf(); if (!chk(s_objectf())) failureCount++; s_voidf(); s_show("========== Testing instance methods ================"); if (!chk(xx.i_bytef(89))) failureCount++; if (!chk(xx.i_charf())) failureCount++; if (!chk(xx.i_doublef())) failureCount++; if (!chk(xx.i_floatf())) failureCount++; if (!chk(xx.i_intf())) failureCount++; if (!chk(xx.i_longf())) failureCount++; if (!chk(xx.i_shortf())) failureCount++; if (!chk(xx.i_booleanf())) failureCount++; if (!chk(xx.i_stringf())) failureCount++; xx.i_intArrayf(); xx.i_classf(); xx.i_classLoaderf(); xx.i_threadf(); xx.i_threadGroupf(); xx.i_nullObjectf(); if (!chk(xx.i_objectf())) failureCount++; xx.i_voidf(); } /** Hang so that test fails */ static void hang() { try { // ten minute nap Thread.currentThread().sleep(10 * 60 * 1000); } catch (InterruptedException exc) { // shouldn't happen } } public static void main(String[] args) throws Exception { // The debugger will stop at the start of main, // set breakpoints and then do a resume. System.err.println("debugee in main"); EarlyReturnTarg xx = new EarlyReturnTarg(); doit(xx); if (debuggerWatching && failureCount > 0) { hang(); throw new Exception("EarlyReturnTarg: failed"); } } }