次の方法で共有


Managing Breakpoints Programmatically

Posted by: Sankar Ramasubbu 

As as extension of my earlier post, I am going to show you how to automate the following breakpoint operations: Enable, Disable and Delete. This post assumes that you have downloaded an image to a device and it has booted up completely. Please refer to Automating the download of a run-time image for details on downloading an image to a device. If you want details on how to automate setting breakpoints please look at my earlier post.

The sample in this post will show you how to automate the following operations:

1. Disable all breakpoints

2. Enable all breakpoints

3. Disable a specific breakpoint

4. Enable a specific breakpoint

5. Delete a specific breakpoint

Add the following references for this sample to work:

  1. System.Windows.Forms
  2. Interop.Microsoft.PlatformBuilder.Diagnostiocs(found in C:\Program Files\Microsoft Platform Builder\6.00\cepb\IdeVS on my machine)

You need the breakpoint template object for any breakpoint operation, which you can get to as follows:

    // Get the debugger object model

    diagnostics = (CCeSystemDiagnostics)dte.GetObject("Yamazaki-OM");

    // Get the runState Control

    runState = (CcsdRunStateControl)diagnostics.GetRunStateControl();

    // Get the breakpoint Templates object

    breakpointTemplates = (csdBreakpointTemplates)runState.GetBreakpointTemplates();

 

Please keep the breakpoint list window open (use menu Debug->Windows->Breakpoints) before executing this sample. Here is the complete sample that shows the various breakpoint operations:

namespace BreakpointsOperationsSample

{

    // Standard namespaces

    using System;

    using System.Collections;

    using System.Collections.Generic;

    using System.Text;

    using System.Windows.Forms;

    // PlatformBuilder and Visual Studio namespaces

    using EnvDTE;

    using EnvDTE80;

    using Interop.Microsoft.PlatformBuilder.Diagnostics;

    using Microsoft.PlatformBuilder.Test;

    internal static class BreakpointOperationsSample

    {

        /// <summary>

        /// The caption to use in message boxes

        /// </summary>

        private const string Caption = "PB - Breakpoint Operations Sample";

        /// <summary>

        /// Debugger Object

        /// </summary>

        private static CCeSystemDiagnostics diagnostics;

        /// <summary>

        /// Run State Object

        /// </summary>

        private static CcsdRunStateControl runState;

 

        /// <summary>

        /// Set a breakpoint

        /// </summary>

        /// <param name="function">Function name</param>

        private static void SetBreakpoint(string function)

        {

            try

            {

                // Get the break point property object and set the Function Name property to where you want the breakpoint

                csdPropertyBag breakpointPropertyBag = (csdPropertyBag)diagnostics.GetPropertyBag();

                breakpointPropertyBag.Set("FunctionName", function);

                // Get the breakpoint templates object and add a new breakpoint

                CcsdBreakpointTemplates breakpointTemplates = (CcsdBreakpointTemplates)runState.GetBreakpointTemplates();

                breakpointTemplates.NewElement((IcsdPropertyBag)breakpointPropertyBag);

            }

            catch (Exception e)

            {

                MessageBox.Show(e.Message, Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

            return;

        }

        /// <summary>

        /// Get breakpoint Template for a given index

        /// </summary>

        /// <param name="Number">Breakpoint index</param>

        /// <returns>Breakpoint Template for the given index</returns>

        private static CcsdBreakpointTemplate GetBreakpointTemplate(int Number)

        {

            CcsdBreakpointTemplate breakpointTemplate;

            // Get the breakpoint templates object, to get to the required breakpoint template

            CcsdBreakpointTemplates breakpointTemplates = (CcsdBreakpointTemplates)runState.GetBreakpointTemplates();

// Walk through the breakpoint templates list, until we get to the required breakpoint template

            IEnumerator enumBreakpoint = (IEnumerator)breakpointTemplates._NewEnum;

            enumBreakpoint.MoveNext();

            int Count = 1;

            while ((breakpointTemplate = (CcsdBreakpointTemplate)enumBreakpoint.Current) != null)

            {

                if (Count == Number)

                    return (breakpointTemplate);

                Count++;

                enumBreakpoint.MoveNext();

            }

            return null;

        }

        /// <summary>

        /// Wait for the debugger to reach break state

        /// </summary>

        /// <param name="run"></param>

        static void WaitForBreak(int timeOut)

        {

            try

            {

                for (int i = 0; i < timeOut; i++)

                {

                    csdHaltReason haltReason = csdHaltReason.csdHaltReasonStepComplete;

                    object oBreakpoint = null;

                    object oException = null;

                    byte phase = 0;

                    csdTriState state;

                    System.Threading.Thread.Sleep(1000);

                    // get the current state of the debugger

   state = runState.GetCurrentRunState(ref haltReason, ref oBreakpoint, ref oException, ref phase);

                    // return if the debugger is in break state

                    if (state == csdTriState.csdTriStateTrue)

                        return;

                }

                MessageBox.Show("ERROR: Debugger did not reach break state in 60 seconds", Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

            catch (Exception e)

            {

  MessageBox.Show(e.Message, Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

        }

       

        /// <summary>

        /// Wait for the debugger to reach run state

        /// </summary>

        /// <param name="run"></param>

        static void WaitForRun(int timeOut)

        {

            try

            {

                for (int i = 0; i < timeOut; i++)

                {

                    csdHaltReason haltReason = csdHaltReason.csdHaltReasonStepComplete;

                    object oBreakpoint = null;

                    object oException = null;

                    byte phase = 0;

                    csdTriState state;

                    System.Threading.Thread.Sleep(1000);

                    // get the current state of the debugger

                    state = runState.GetCurrentRunState(ref haltReason, ref oBreakpoint, ref oException, ref phase);

                    // return if the debugger is in run state

                    if (state == csdTriState.csdTriStateFalse)

                        return;

                }

                MessageBox.Show("ERROR: Debugger did not reach run state in 60 seconds", Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

            catch (Exception e)

            {

                MessageBox.Show(e.Message, Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

        }

        /// <summary>

        /// Sample that illustrates Enable, Disable and Delete breakpoints

        /// </summary>

        public static void BreakpointOperationsSample()

        {

            // Get the debugger object model

            diagnostics = (CCeSystemDiagnostics)dte.GetObject("Yamazaki-OM");

            // Get the runState Control

            runState = (CcsdRunStateControl)diagnostics.GetRunStateControl();

            // Get the breakpoint Templates object

            CcsdBreakpointTemplates breakpointTemplates = (CcsdBreakpointTemplates)runState.GetBreakpointTemplates();

       // Go to break state, to ensure that the breatpoint we set is instantiated immediately

            runState.Break();

            // Wait until debugger reaches break state

            WaitForBreak(60);

            // Set a breakpoint on the function named DoHelp; which is defined in the module shell.exe

            SetBreakpoint("DoHelp");

            // Set a second breakpoint on the function named FindCommand; which is defined in the module shell.exe

            SetBreakpoint("FindCommand");

            MessageBox.Show("In breakpoint list window verify there are two break points and both are enabled.", Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);

                   

            // Disable all breakpoints

    breakpointTemplates.DisableAll();

            MessageBox.Show("In breakpoint list window verify that all breakpoints are disbled.", Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);

            // Enable all breakpoints

            breakpointTemplates.EnableAll();

            MessageBox.Show("In breakpoint list window verify that all breakpoints are enabled.", Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);

            // Disable the second breakpoint

            CcsdBreakpointTemplate breakpointTemplate = GetBreakpointTemplate(2);

            breakpointTemplate.Disable();

            MessageBox.Show("In breakpoint list window verify that the second breakpoint is disbled.", Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);

            // Enable the second breakpoint

            breakpointTemplate.Enable();

            MessageBox.Show("In breakpoint list window verify that the second breakpoint is enabled.", Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);

            // Delete the second breakpoint

            breakpointTemplate.Delete();

            MessageBox.Show("In breakpoint list window verify that the second breakpoint is deleted.", Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);

            // Delete the first breakpoint

            breakpointTemplate = GetBreakpointTemplate(1);

            breakpointTemplate.Delete();

            MessageBox.Show("In breakpoint list window verify that the first breakpoint is deleted.", Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);

            // Run so that the debugger is in run state again

            runState.Run(true);

        }

    }

}

Now as the above sample shows you can automate most of the breakpoint tasks.