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;
        }
Beispiel #2
0
        internal void StartProfiler(string device, string logFile, string transport, 
            string exePath, string buildPath, ArrayList referenceList, bool isDevEnvironment, string assemblyName )
        {
            try
            {
                PortDefinition port = Utils.GetPort(device, transport, exePath);

                m_engine = new Engine(port);
                m_session = new ProfilerSession(m_engine);

#if DEBUG
                m_exporter = new Exporter_OffProf(m_session, logFile);
#endif

                lock (m_engine)
                {
                    m_engine.StopDebuggerOnConnect = true;
                    m_engine.Start();

                    bool connected = false;

                    connected = m_engine.TryToConnect(20, 500, true, 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.");
                        }

                        // Deploy the test files to the device.                    
                        Utils.DeployToDevice(buildPath, referenceList, m_engine, transport, isDevEnvironment, assemblyName);

                        // Move IsDeviceInInitializeState(), IsDeviceInExitedState(), 
                        // GetDeviceState(),EnsureProcessIsInInitializedState() to Debugger.dll?

                        m_engine.RebootDevice(Engine.RebootOption.RebootClrWaitForDebugger);

                        if (!m_engine.TryToConnect(100, 500))
                        {
                            throw new ApplicationException("Connection Failed");
                        }

                        m_engine.ThrowOnCommunicationFailure = true;
                        m_session.EnableProfiling();

                        m_session.SetProfilingOptions(true, false);
                        m_engine.OnCommand += new CommandEventHandler(OnWPCommand);
                        m_engine.ResumeExecution();
                    }
                    else
                    {
                        throw new ApplicationException("Connection failed");
                    }
                }
            }
            catch (Exception ex)
            {
                SoftDisconnectDone(null, null);
                throw ex;
            }            
        }
        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;
        }