Just-In-Time Debugging on the .NET Compact Framework Version 2

In May, at MEDC 2005, I had the pleasure of debuting Just-In-Time debugging on the .NET Compact Framework version 2.  Today's topic is based on my session.  Mike Zintel's MEDC post has some pictures from the conference.

To enable Just-In-Time debugging, you will first need to enable attach to process support on your device.  Details on how to attach the Visual Studio 2005 debugger to a .NET Compact Framework application can be found here.

Along with enabling attach to process support, the debugger transports must be present on your device.  By far, the easiest way to deploy the transports is to debug any managed application on your device.  I typically create a very simple application, in Visual Studio 2005, deploy with F5 and click the stop debugging button once the application starts running.  Not only with this ensure that you have the transports on your device, it will also install version 2 of the .NET Compact Framework, if it is not already installed.

With your device configured with attach to process support enabled, the steps to Just-In-Time debug an exception are pretty simple:

  1. Run your application on your device (or emulator)
  2. When an unhandled exception occurs, an Error dialog will be shown.
    The text in the message will be something similar to this:
    An unexpected error has occurred in TestApplication.exe.Select Quit and then restart this program, or select Details for more information.
  3. Click the Details button to view the stack trace
    In my simple test application, I see:
    TestApplication.exeDivideByZeroExceptionat TestApplication.MainForm.WorkerThread()
  4. Start Visual Studio 2005
  5. Follow the steps listed here to attach the debugger to your application
    It is very important to remember not to click the Debug button prior to attaching the Visual Studio debugger.  If you do so, you will be unable to attach to the process.
  6. Once attached, click the Debug button (on the device).
    Visual Studio 2005 will create a solution, load the appropriate symbols and source and enter debug mode at the point of the exception.
    In my application, the debugger brings me to the line where I attempted a divide by zero (line shown below).
    result = i / j;
  7. You can now debug the cause of the exception.

There is one thing to be aware of with regards to user interface threads.  If your application happens to encounter an exception on the user interface thread, when you attach, the debugger may stop at the call to Application.Run().  For example, my example application encounters an exception during the click event handler (if the input data is invalid). 

If this occurs during your debugging, I suggest walking the call stack to find the first instance of your code and adding a call to Debug.Assert() to the method listed.  I have included the stack trace from my example application below.  The method highlighted in purple is where I will add the Debug.Assert() call.
TestApplication.exeFormatExceptionat Number.ParseInt32()at Int32.Parse()at Int32.Parse()at MainForm.goButton_Click()at Control.OnClick()at Button.OnClick()at ButtonBase.WnProc()at EVL.EnterMainLoop()at Application.Run()at Program.Main()

By adding the Debug.Assert(), possibly with a message to the user with instructions on how to proceed, the debugger can be attached prior to the exception.  Once attached, the application is running under the debugger and debugging can take place when the exception occurs.  When I add the Debug.Assert() and attach, I find that my application was failing to handle non-numeric strings being sent to Int32.Parse().

With a Debug.Assert() added, the above steps become:

  1. Run your application on your device (or emulator)
  2. When the Debug.Assert is encountered, your message will be displayed
    The message will look something like this
    Assertion FailedPlease call (###) ###-#### before continuingThe stack trace at the time of the assert will also be displayed.
  3. Start Visual Studio 2005
  4. Follow the steps listed here to attach the debugger to your application
    It is very important to remember not to click the Debug button prior to attaching the Visual Studio debugger.  If you do so, you will be unable to attach to the process.
  5. Once attached, click the Continue button (on the device).
    Visual Studio 2005 will create a solution, load the appropriate symbols and source and enter debug mode at the point of the assert.
  6. The exception will be encountered and you can debug as desired

It is important to note that you will need to have the matching symbols (.pdb) file for the application available on the PC that is running Visual Studio.

Enjoy!
-- DK

Disclaimer(s):
This posting is provided "AS IS" with no warranties, and confers no rights.
Some of the information contained within this post may be in relation to beta software. Any and all details are subject to change.