Debugging Silverlight applications with windbg and sos.dll


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 FilesMicrosoft Silverlight2.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 Silverlight2.0.30523.8sos

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

Comments (33)

  1. My latest in a series of the weekly, or more often, summary of interesting links I come across related to Visual Studio. The Web Developer Tools Team announced the release of the Dynamic Data Wizard Preview 0806 for VS 2008 SP1 . US ISV Developer Evangelism

  2. Great post Tess: concise and informative.

    I’d be most interested in any real world Silverlight problems you’ve solved/investigated ‘using the powers of the debugger’ in the future.

    Thanks,

    Peter

  3. Link Listing – August 21, 2008

  4. Post: Approved at: Aug-24-2008 Intersoft demos Silverlight 2 controls http://www.sdtimes.com/INTERSOFT_DEMOS_SILVERLIGHT_2_CONTROLS

  5. Tess just posted an article on how to debug Silverlight application using sos.dll. If you have been using

  6. gOODiDEA says:

    DebugDebuggingSilverlightapplicationswithwindbgandsos.dllWebJavaScriptMemoryLeakDetec…

  7. gOODiDEA.NET says:

    Debug Debugging Silverlight applications with windbg and sos.dll Web JavaScript Memory Leak Detector

  8. Back in August I wrote a post about how to debug these types of issues in silverlight applications.&#160;

  9. Silverlight 2.0 RTW = Production Debugging Silverlight 2.0 Apps Ok, so we all know that Silverlight 2

  10. Marc Sherman says:

    What if some non-Silverlight component that used mscorwks.dll (a regular .NET component) was loaded into IE? And then a Silverlight app was loaded. Would both mscorwks.dll and coreclr.dll be loaded in the IE process? If so, I guess you could load both versions of sos when looking at a dump (since they have different paths). Is any of this possible?

    thanks,

    Marc

  11. Tess says:

    Yes, all of the above is possible…

    In that case you might have to prefix the command with the path !<path>/sos.command  to get the command from the right sos loaded  or alternatively unload one sos and load the other to avoid confusion about where the commands come from…

    if a command is present in multiple loaded extensions, the extension that is last loaded will have presedence  (see .chain for loading order and full name of the extensions)

  12. Doug says:

    Is this supposed to work by directly attaching to the iexplore.exe process or does it only work by taking a dump? I’ve some huge memory leaks with my SL2 app (~50 MBs in one transaction) on the client side. I’m trying to investigate the culprit for this.

    Thank you.

  13. gOODiDEA.NET says:

    .NET ASP.NET MVC Beta Source Code Release – Partial Output Caching in ASP.NET MVC – Partial Requests

  14. Tess says:

    Doug, you can attach live or take a dump, either one will work.

    The reasons for takig a dump would be

    a) to be able to look at it on a different machine

    b) to be able to continue working with the process while you are debugging

    c) to save it if you want to refer back to it

    but there is no difference between what you will see live or in a dump for this purpose…

  15. Hola Iba a poner esto como respuesta al art&iacute;culo de Octavio sobre &quot;Qu&eacute; problema tiene

  16. Scott says:

    Tess,

    THANK YOU!  

    This, combined with a number of other posts on your blog, helped me to identify and resolve a nasty memory leak in a Silverlight 2 photo gallery available on CodePlex called Slide.Show 2.

    If you’re curious, here’s some of the details…

    http://slideshow2.codeplex.com/Thread/View.aspx?ThreadId=60173

    You are a debugging goddess.  I stand in awe.  Thanks for imparting a little of your wisdom to the rest of us.

  17. deano says:

    Hi Tess,

    Thanks for the helpful articles. I am finding them very useful.

    I am currently developing a silverlight app that gets live updates from a TCP publishing server. When to works the application runs very well but i think i have either a network threading problem or UI update exception because I can get some very random exceptions. Typically the output (with no callstack) is:

    System.ExecutionEngineException was unhandled

    Message: An unhandled exception of type ‘System.ExecutionEngineException’ occurred in Unknown Module.

    I also sometime get a NullReferenceException with a stacktrace as follows:

    StackTrace "   at System.Threading.Overlapped.get_UserCallback()    at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)"

    I would appreciate it if you could suggest any tools or methodologies for silverlight that would help diagnose what is casuing these issues.

    Thanks very much

    Deano

  18. Hi Tess,

    I am getting this error while using the SOS.dll in Silverlight 3

    SOS not available while Managed only debugging.  To load SOS, enable unmanaged debugging in your project properties.

    I have looked on the net for this error all the solutions there are for windows applciations and there is no solution to tell how to enable unmanged debugging in Silverlight or Web applictions so could you please help me in finding that option.

    In windows application it comes when you do a right click on your solution and then click porperties and then debug

    and then enable unmanged debugging.

    Thanks

    Aashish Gupta

  19. Tess says:

    If you are talking about sos in visual studio then I dont think you can set this for silverlight or asp.net apps (might be wrong) but you can debug by attaching to a process in visual studio and then you can choose what you want to debug (managed, native etc.)

  20. David says:

    I’ve just started debugging my silverlight application as i think there may be some memory leaks.

    I am attaching windbg to the iexpore process, loading the silverlight 3 sos.dll and then debugging the application.  I then run a !dumpheap.

    From the list of items returned from the !dumpheap command, i then do a !gcroot of the MT value.

    I then receive the following:

    Please note that 07a4fc70 is not a valid object.

    What does this mean?

    Thanks

  21. Tess says:

    David,

    You can’t do !gcroot on the MT.  The MT is a method table which is basically a description of a type.  You would instead need to do

    !dumpheap -mt <mtvalue>

    to get the instances out, and then you can gcroot one of the objects in that list…

  22. nativecpp says:

    I am trying to follow the steps you indicated in this blog. After doing a fulldump on IE where my Silverlight ran, I loaded windbg and load the sos.dll from the Silverlight tool.

    When I did !dumpheap -stat, it complains

    Failed to find runtime DLL (mscorwks.dll), 0x80004005

    Extension commands need mscorwks.dll in order to have something to do.

    So, my guess is that I need to specify the mscorwks.dll from the same path. I tried to follow your suggestion of

    !<path>/sos.command

    by doing the following

    !C:Program FilesMicrosoft Silverlight3.0.50106.0/dumpheap -stat

    But it complains as follow:

    No export C:Program found

    I tried to add quote (single or double) and it didn’t help. Any idea ??

    Thanks

  23. Tess says:

    nativecpp,

    The fact that it is complaining about mscorwks means that you have loaded the framework sos rather than the silverlight sos…

    If you have multiple versions of sos loaded in the debugger, the easiest way to get to the right one without having to specify paths etc. is to run .chain to see which ones you have loaded and then unload the ones you dont want…

    Tess

  24. Vinod says:

    I did use Windbg for Silverlight debugging, basically I wanted to find out memory profile, I was just curious to know about how many solid color brush objects were created for the button that I was using and memory used by Shape Objects, When I used dumpheap I got size for the Button Objects and other objects too like Grid, Strangely the SolidColorBrush and Ellipse were not listed, I am using the SOS,dll for Silverlight, application is in Silverlight 3, IE is the browser

  25. Tess says:

    Vinod,

    If you are seeing the other objects you are definitely using the right SOS version.

    If you can’t see the SolidColorBrush objects there may be 2 reasons for this

    1. you are in the middle of a GC so the heaps are "in motion" and "dumpheap -stat" is not showing correct data… you should have gotten a warning when running !dumpheap if that was the case though

    2. or… there are no solidcolorbrush objects, not sure why that would be though…

  26. Vinod says:

    I created following simple Silverlight application now; even this doesnt show;

    <StackPanel x:Name="LayoutRoot">

    <Button Content="Hello" Background="Red"/>

         <Button Content="Hello" Background="Green"/>

         <Button Content="Hello" Background="Yellow"/>

         <Button Content="Hello" Background="Brown"/>

           <Rectangle Width="100" Height="200"/>

       </StackPanel>

  27. Alexandre Grenier says:

    Hello Tess! I had to look pretty hard for decent Son-of-Strike info… but this is freaking amazing… SoS is already like another dimension in Visual Studio, making me spend a lot of time in my little immediate window…

    But now you’re telling me Silverlight also has its own SoS! Woohoooooo :)

  28. Alexandre Grenier says:

    Oh also, I am wondering, do you know of differences or more extra fun things that can be used when the Silverlight application is running Out Of Browser / Trusted ?

  29. nativecpp says:

    Hi Tess,

    I am trying to understand more about debugging Sliverlight.

    Let say I have a siliverlight app. These are what I have done so far:

    1) I attach to IE and do a dump

    2) load Silverlight’s SOS and is able to see the objects generated by the app.

    Question that I need your help:

    If my Silverlight app call some WCF web service, can I see the objects from the same dump? If not, I would assume that I need to attach aspnet_wp ??

  30. Tess says:

    yeah, basically in that case the silverlight app would be the client so you could see anything used on the clientside for the WCF calls but to debug the requests themselves you would have to get a dump of the process hosting the WCF app, probably aspnet_wp.exe or w3wp.exe depending on OS.

  31. Sam says:

    Hi there,

    I have installed the SL 4.0 runtime and the SL4.0 SDK, but sos.dll is nowhere to be foudn in Program Files. Is this still shipping with 4.0.50524.0?

  32. Tess says:

    Sam,

    Funny this question should come now, as i just had to ask my colleague about this yesterday because i was debugging a 4.0 issue.  I got mine from him, but it should be in the developer runtime http://www.microsoft.com/…/details.aspx

    Tess