Debugging Silverlight applications with windbg and sos.dll

2 minute read

If you have hangs, performance, memory issues, exceptions or crashes in Silverlight applications you can debug them using windbg and sos just like you would if the issues occurred in other .net applications.

The difference is that Silverlight in IE runs a subset of the framework where the main dll is coreclr.dll rather than mscorwks.dll so you can’t use the regular 2.0 version of sos.dll that you can find in the framework directory. Instead you can install the Silverlight Developer Runtime from http://www.microsoft.com/silverlight/resources/tools.aspx and the sos.dll for silverlight will be located in the C:\Program Files\Microsoft Silverlight\2.0.30523.8 directory.

To troubleshoot Silverlight applications with windbg and sos you would take a memory dump of the IExplore.exe process that acts as the host for your silverlight application. You can do so with either adplus (as shown in my lab series) or with debug diag, just remember that the process should be IExplore.exe rather than w3wp.exe.

Debugging Basics

For demo purposes I just created a very simple silverlight app with a button that had the following code really bad code in the onclick method, which will make the application throw an exception that we just swallow, and then “hang” for 25 seconds.

private void MyButton_Click(object sender, RoutedEventArgs e)
{
    try
    {
        throw new Exception("Test exception");
    }
    catch
    {
    }
    Thread.Sleep(25000);
    MyButton.Content = "Pushed";
}

To debug the app I follow these steps

  1. While it is hanging I take a memory dump of the process using

     adplus -p <PID for the IExplore.exe process> -hang
    
  2. I open up the dump in windbg and set the symbol path

     .sympath SRV*c:\websymbols*http://msdl.microsoft.com/download/symbols
     .reload
    
  3. I load up sos.dll from the silverlight directory

     .load C:\Program Files (x86)\Microsoft Silverlight\2.0.30523.8\sos
    

And now I can debug as i would debug any other .net application

Looking at the stacks to see what is executing

0:010> ~* e !clrstack
...
OS Thread Id: 0x1460 (5)
ESP       EIP
02edf858 7d61cca8 [HelperMethodFrame: 02edf858] System.Threading.Thread.SleepInternal(Int32)
02edf8a4 05cdd5f7 System.Threading.Thread.Sleep(Int32)
02edf8b0 05c603b5 TestSLControl.Page.MyButton_Click(System.Object, System.Windows.RoutedEventArgs)
02edf8e8 05cdd425 System.Windows.Controls.Primitives.ButtonBase.OnClick()
02edf900 05cdd328 System.Windows.Controls.Button.OnClick()
02edf910 05cdd2ac System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(System.Windows.Input.MouseButtonEventArgs)
02edf924 05cdd1e4 System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(System.Object, System.Windows.Input.MouseButtonEventArgs)
02edf934 05c9a3d2 System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32, System.Delegate, System.Object, System.Object)
02edf9bc 05c9838d MS.Internal.JoltHelper.FireEvent(IntPtr, IntPtr, Int32, System.String)
02edfb74 7b7b1720 [GCFrame: 02edfb74]
02edfc30 7b7b1720 [ContextTransitionFrame: 02edfc30]
02edfd28 7b7b1720 [UMThkCallFrame: 02edfd28]

Here we can see the sleep method called from TestSLControl.Page.MyButton_Click

Looking at recent exceptions

0:010> !dumpheap -type Exception
Address       MT     Size
...
total 10 objects
Statistics:
      MT    Count    TotalSize Class Name
...
06307110        1           72 System.Exception
...
Total 10 objects
0:010> !dumpheap -mt 06307110
Address       MT     Size
0638d870 06307110       72
total 1 objects
Statistics:
      MT    Count    TotalSize Class Name
06307110        1           72 System.Exception
Total 1 objects
0:010> !pe 0638d870
Exception object: 0638d870
Exception type: System.Exception
Message: Test exception
InnerException: <none>
StackTrace (generated):
    SP       IP       Function
    02EDF8B0 05C603A0 UNKNOWN!TestSLControl.Page.MyButton_Click(System.Object, System.Windows.RoutedEventArgs)+0x48

StackTraceString: <none>
HResult: 80131500
The current thread is unmanaged

… to name a few examples. In essence, most of what you can look at using regular sos is also available in the silverlight sos.

Have fun, Tess