private void Connect() { _DBG.PortDefinition port = comboBoxDevice.PortDefinition; if (port == null) { SetInvalidDevice(); return; } try { comboBoxTransport.Enabled = false; comboBoxDevice.Enabled = false; buttonConnect.Enabled = false; groupBoxOutput.Enabled = false; groupBoxOptions.Enabled = false; m_state = ProfilingState.Connecting; if (port is _DBG.PortDefinition_Emulator) { _DBG.PortDefinition_Emulator emuport = port as _DBG.PortDefinition_Emulator; if (emuport.Pid == 0) { OpenFileDialog fd = new OpenFileDialog(); fd.DefaultExt = "exe"; fd.Filter = ".NET MF Exe files (*.exe)|*.exe"; fd.Title = "Choose an application to emulate"; if (fd.ShowDialog(this) == DialogResult.OK) { m_emuProcess = EmulatorLauncher.LaunchEmulator(emuport, true, fd.FileName); if (m_emuProcess == null) { LogText("Could not launch emulator."); Disconnect(); return; } else { m_emuLaunched = true; LogText(string.Format("Started emulator process {0}", m_emuProcess.Id)); _DBG.PlatformInfo pi = new _DBG.PlatformInfo(null); port = new _DBG.PortDefinition_Emulator("Emulator - pid " + m_emuProcess.Id, m_emuProcess.Id); comboBoxDevice.SelectLaunchedEmulator((_DBG.PortDefinition_Emulator)port); } } else { Disconnect(); return; } } else { try { m_emuProcess = Process.GetProcessById(emuport.Pid); } catch { m_state = ProfilingState.Disconnected; EnableUI(); SetInvalidDevice(); return; } } } buttonConnect.Text = c_Cancel; buttonConnect.Enabled = true; BackgroundConnectorArguments bca = new BackgroundConnectorArguments(); bca.connectPort = port; bca.outputFileName = textLogFile.Text; #if DEBUG if (radioOffProf.Checked) { bca.exporter = BackgroundConnectorArguments.ExporterType.OffProf; } else { bca.exporter = BackgroundConnectorArguments.ExporterType.CLRProfiler; } #else bca.exporter = BackgroundConnectorArguments.ExporterType.CLRProfiler; #endif //Rebooting the emulator just makes it quit. bca.reboot = checkReboot.Checked && !(port is _DBG.PortDefinition_Emulator); bwConnecter.RunWorkerAsync(bca); } catch (ApplicationException e) { comboBoxTransport.Enabled = true; comboBoxDevice.Enabled = true; buttonConnect.Enabled = true; groupBoxOutput.Enabled = true; groupBoxOptions.Enabled = true; m_state = ProfilingState.Disconnected; MessageBox.Show(this, e.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void bwConnecter_DoWork(System.Object sender, DoWorkEventArgs e) { BackgroundConnectorArguments bca = (BackgroundConnectorArguments)e.Argument; _DBG.PortDefinition port = bca.connectPort; Debug.Assert(m_engine == null); e.Result = false; #if USE_CONNECTION_MANAGER m_engine = m_port.DebugPortSupplier.Manager.Connect(port); #else m_engine = new _DBG.Engine(port); #endif m_killEmulator = false; lock (m_engine) { m_engine.StopDebuggerOnConnect = true; m_engine.OnCommand += new _DBG.CommandEventHandler(OnWPCommand); m_engine.OnMessage += new _DBG.MessageEventHandler(OnWPMessage); m_engine.Start(); const int retries = 50; bool connected = false; for (int i = 0; connected == false && i < retries; i++) { if (bwConnecter.CancellationPending) { e.Cancel = true; return; } connected = m_engine.TryToConnect(1, 100, false, _DBG.ConnectionSource.TinyCLR); } if (connected) { if (m_engine.Capabilities.Profiling == false) { throw new ApplicationException("This device is not running a version of TinyCLR that supports profiling."); } //Move IsDeviceInInitializeState(), IsDeviceInExitedState(), GetDeviceState(),EnsureProcessIsInInitializedState() to Debugger.dll? uint executionMode = 0; m_engine.SetExecutionMode(0, 0, out executionMode); if (bca.reboot || (executionMode & _WP.Commands.Debugging_Execution_ChangeConditions.c_State_Mask) != _WP.Commands.Debugging_Execution_ChangeConditions.c_State_Initialize) { m_engine.RebootDevice(_DBG.Engine.RebootOption.RebootClrWaitForDebugger); m_engine.TryToConnect(10, 1000); m_engine.SetExecutionMode(0, 0, out executionMode); Debug.Assert((executionMode & _WP.Commands.Debugging_Execution_ChangeConditions.c_State_Mask) == _WP.Commands.Debugging_Execution_ChangeConditions.c_State_Initialize); } m_engine.ThrowOnCommunicationFailure = true; m_session = new _PRF.ProfilerSession(m_engine); if (m_exporter != null) { m_exporter.Close(); } switch (bca.exporter) { case BackgroundConnectorArguments.ExporterType.CLRProfiler: m_exporter = new _PRF.Exporter_CLRProfiler(m_session, bca.outputFileName); break; #if DEBUG case BackgroundConnectorArguments.ExporterType.OffProf: m_exporter = new _PRF.Exporter_OffProf(m_session, bca.outputFileName); break; #endif default: throw new ArgumentException("Unsupported export format"); } m_session.EnableProfiling(); e.Result = true; } } return; }