Esempio n. 1
0
        public static Process LaunchEmulator(_DBG.PortDefinition_Emulator pd, bool fWaitForDebugger, string program)
        {
            _DBG.PlatformInfo          pi  = new _DBG.PlatformInfo(null);
            _DBG.PlatformInfo.Emulator emu = pi.FindEmulator(pd.Port);

            _DBG.CommandLineBuilder cb = new _DBG.CommandLineBuilder();

            if (emu == null)
            {
                throw new ArgumentException();
            }
            if (emu.legacyCommandLine)
            {
                throw new NotSupportedException("Legacy emulators not supported.");
            }

            if (!string.IsNullOrEmpty(emu.additionalOptions))
            {
                _DBG.CommandLineBuilder cbT = new _DBG.CommandLineBuilder(emu.additionalOptions);
                cb.AddArguments(cbT.Arguments);
            }

            if (!string.IsNullOrEmpty(emu.config))
            {
                cb.AddArguments("/config:" + emu.config);
            }

            if (fWaitForDebugger)
            {
                cb.AddArguments("/waitfordebugger");
            }

            string[] files = EmulatorLauncher.GetFilesToLoad(program);
            foreach (string pe in files)
            {
                cb.AddArguments("/load:" + pe);
            }

            string args = "";

            args = args.Trim();
            if (args.Length > 0)
            {
                cb.AddArguments("/commandlinearguments:" + args);
            }

            string commandLine = cb.ToString();

            commandLine = Environment.ExpandEnvironmentVariables(commandLine);

            Process p = new System.Diagnostics.Process();

            p.StartInfo.FileName         = emu.application;
            p.StartInfo.Arguments        = commandLine;
            p.StartInfo.UseShellExecute  = false;
            p.StartInfo.WorkingDirectory = Path.GetDirectoryName(emu.application);

            try
            {
                p.Start();
            }
            catch (System.ComponentModel.Win32Exception we)
            {
                MessageBox.Show(string.Format("Failed to launch emulator: {0}", we.NativeErrorCode),
                                "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(null);
            }

            return(p);
        }
        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);
            }
        }