synchronized VirtualMachine open() { if (connector instanceof LaunchingConnector) { vm = launchTarget(); } else if (connector instanceof AttachingConnector) { vm = attachTarget(); } else if (connector instanceof ListeningConnector) { vm = listenTarget(); } else { throw new InternalError(MessageOutput.format("Invalid connect type")); } vm.setDebugTraceMode(traceFlags); if (vm.canBeModified()) { setEventRequests(vm); resolveEventRequests(); } /* * Now that the vm connection is open, fetch the debugee * classpath and set up a default sourcepath. * (Unless user supplied a sourcepath on the command line) * (Bug ID 4186582) */ if (Env.getSourcePath().length() == 0) { if (vm instanceof PathSearchingVirtualMachine) { PathSearchingVirtualMachine psvm = (PathSearchingVirtualMachine) vm; Env.setSourcePath(psvm.classPath()); } else { Env.setSourcePath("."); } } return vm; }
/** 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(); }
/** Go. */ public synchronized void go() throws DebuggerException { if (virtualMachine == null) return; setLastAction(ACTION_GO); removeStepRequest(); virtualMachine.resume(); threadGroup.refresh(); super.go(); }
/* launch child target vm */ private VirtualMachine launchTarget() { LaunchingConnector launcher = (LaunchingConnector) connector; try { VirtualMachine vm = launcher.launch(connectorArgs); process = vm.process(); displayRemoteOutput(process.getErrorStream()); displayRemoteOutput(process.getInputStream()); return vm; } catch (IOException ioe) { ioe.printStackTrace(); MessageOutput.fatalError("Unable to launch target VM."); } catch (IllegalConnectorArgumentsException icae) { icae.printStackTrace(); MessageOutput.fatalError("Internal debugger error."); } catch (VMStartException vmse) { MessageOutput.println("vmstartexception", vmse.getMessage()); MessageOutput.println(); dumpFailedLaunchInfo(vmse.process()); MessageOutput.fatalError("Target VM failed to initialize."); } return null; // Shuts up the compiler }
public void disposeVM() { try { if (vm != null) { vm.dispose(); vm = null; } } finally { if (process != null) { process.destroy(); process = null; } waitOutputComplete(); } }
public void close() { // TODO make sure stop() has already been called to exit the sketch // TODO actually kill off the vm here if (vm != null) { try { vm.exit(0); } catch (com.sun.jdi.VMDisconnectedException vmde) { // if the vm has disconnected on its own, ignore message // System.out.println("harmless disconnect " + vmde.getMessage()); // TODO shouldn't need to do this, need to do more cleanup } vm = null; } }
/** 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(); }
/** Step out. */ public synchronized void stepOut() throws DebuggerException { if (virtualMachine == null) return; removeStepRequest(); try { setLastAction(ACTION_STEP_OUT); stepRequest = requestManager.createStepRequest( currentThread.getThreadReference(), StepRequest.STEP_LINE, StepRequest.STEP_OUT); stepRequest.addCountFilter(1); stepRequest.setSuspendPolicy(EventRequest.SUSPEND_ALL); operator.register(stepRequest, this); stepRequest.enable(); virtualMachine.resume(); super.stepOut(); } catch (DuplicateRequestException e) { e.printStackTrace(); } }
private void setEventRequests(VirtualMachine vm) { EventRequestManager erm = vm.eventRequestManager(); // Normally, we want all uncaught exceptions. We request them // via the same mechanism as Commands.commandCatchException() // so the user can ignore them later if they are not // interested. // FIXME: this works but generates spurious messages on stdout // during startup: // Set uncaught java.lang.Throwable // Set deferred uncaught java.lang.Throwable Commands evaluator = new Commands(); evaluator.commandCatchException(new StringTokenizer("uncaught java.lang.Throwable")); ThreadStartRequest tsr = erm.createThreadStartRequest(); tsr.enable(); ThreadDeathRequest tdr = erm.createThreadDeathRequest(); tdr.enable(); }
/** * 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(); }
/** * Starts the debugger. The method stops the current debugging (if any) and takes information from * the provided info (containing the class to start and arguments to pass it and name of class to * stop debugging in) and starts new debugging session. * * @param info debugger info about class to start * @exception DebuggerException if an error occures during the start of the debugger */ public void startDebugger(DebuggerInfo info) throws DebuggerException { debuggerInfo = info; if (virtualMachine != null) finishDebugger(); stopOnMain = info.getStopClassName() != null; mainClassName = info .getClassName(); // S ystem.out.println ("JPDADebugger stop on " + info.getStopClassName // ()); // NOI18N // open output window ... super.startDebugger(info); // stop on main if (stopOnMain) { try { String stopClassName = debuggerInfo.getStopClassName(); AbstractDebugger d = (AbstractDebugger) TopManager.getDefault().getDebugger(); breakpointMain = new CoreBreakpoint[stopMethodNames.length]; for (int x = 0; x < breakpointMain.length; x++) { breakpointMain[x] = (CoreBreakpoint) d.createBreakpoint(true); breakpointMain[x].setClassName(""); // NOI18N breakpointMain[x].setMethodName(stopMethodNames[x]); // NOI18N CoreBreakpoint.Action[] a = breakpointMain[x].getActions(); int i, ii = a.length; for (i = 0; i < ii; i++) if (a[i] instanceof PrintAction) { ((PrintAction) a[i]).setPrintText(bundle.getString("CTL_Stop_On_Main_print_text")); } breakpointMain[x].setClassName(stopClassName); } addPropertyChangeListener( new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent ev) { if (ev.getPropertyName().equals(PROP_STATE)) { if ((((Integer) ev.getNewValue()).intValue() == DEBUGGER_STOPPED) || (((Integer) ev.getNewValue()).intValue() == DEBUGGER_NOT_RUNNING)) { if (breakpointMain != null) { for (int x = 0; x < breakpointMain.length; x++) breakpointMain[x].remove(); breakpointMain = null; } removePropertyChangeListener(this); } } } }); } catch (DebuggerException e) { e.printStackTrace(); } } // start & init remote debugger ............................................ boolean launch = false; if (info instanceof ReconnectDebuggerInfo) { virtualMachine = reconnect((ReconnectDebuggerInfo) info); } else if (info instanceof RemoteDebuggerInfo) { virtualMachine = connect((RemoteDebuggerInfo) info); } else { virtualMachine = launch(info); process = virtualMachine.process(); showOutput(process, STD_OUT, STD_OUT); connectInput(process); launch = true; } requestManager = virtualMachine.eventRequestManager(); operator = new Operator( virtualMachine, launch ? new Runnable() { public void run() { startDebugger(); } } : null, new Runnable() { public void run() { try { finishDebugger(); } catch (DebuggerException e) { } } }); operator.start(); if (!launch) startDebugger(); }