Please visit http://www.ningzhang.org!

This is a backup site. Please go to http://www.ningzhang.org for the latest content and best viewing. Thanks!

Dec 17, 2008

Silverlight Debugging with Visual Studio

Introduction

Debugging is an important part of software development, so knowledge of debugging tools and techniques are very important for developer productivity and software quality. Even though information about debugging are readily available on MSDN, various blogs and sites, it seems that many, if not most, developers are too busy to dig out those information or get familiar with various tools and techniques, so they miss out big on the productivity and quality gains they could have with those debugging tools and techniques. I thought it may be helpful to pull those information together. It ended up too big for a blog post, so I trimmed it down to be an overview of just Visual Studio and WinDbg (and links to references for more information), and broke it into two posts. I use the following simplest Silverlight application for debugging, so I can focus on debugging tools and techniques, instead of using real world examples that may require too much irrelevant/confidential knowledge or take too much effort to explain.

SilverlightApplication1

SilverlightApplication1

 

Visual Studio Debug Windows

Visual Studio is a power tool and the most convenient for debugging managed applications (both .NET and Silverlight). Its GUI makes common debugging operations like breakpoint, backtracked, data manipulation, execution control etc super easy and straightforward. It is highly recommended that you check Debugging in Visual Studio on MSDN frequently, especially the following sections:

Every time I read those links or any thing else on MSDN, I learn something new.

Visual Studio Debug Windows

Immediate Window

Besides debug windows, if you want to use commands or prefer command line interface (especially those who are more accustomed to native coding and/or low level debugging with tools ntsd or gdb), you can use Immediate and Command windows.

You can examine/change objects and their properties and call methods from Immediate Window, and with the convenience of IntelliSense and tool:

Immediate Window

Immediate window has an subset of ntsd commands, and more importantly, can be used to load SOS (I will discuss SOS with WinDbg in next post). 

Immediate Window Commands

Unfortunately none of above commands works while debugging Silverlight, because Silverlight project settings doesn't have the option to turn on native debugging. If someone knows how to debug Silverlight with SOS and Visual Studio, please let me know.

Immediate Window

This limitation can be mitigated to some extend by using Visual Studio commands. You can either use those commands within Immediate Window with >: 

Visual Studio Commands in Immediate Window

or switch to Command Window from Immediate Window with >cmd:

Immediate Window

Command Window

Command Window provides a command line interface for more than just debugging. It is the CUI of Visual Studio, and with intellisense:

Command Window

Command Window

And you can switch from Command Window to Immediate Window with rimmed command.

Command Window

You can read more about Command Window on MSDN, especially:

Debug With .NET Framework Source Code

It is pretty common that sometimes some API's you use don't work as expected/assumed/documented, so being able to debug the source code of those API's is very helpful in identifying/fixing issues. The biggest API set is .NET framework. Scott Guthrie has a blog post .NET Framework Library Source Code now available earlier this year, and Shawn Burke, my boss, has a post with detailed instruction and FAQ/troubleshooting on Configure Visual Studio to Debug .NET Framework Source Code.

I tried with Silverlight:

Configure Visual Studio to Debug .NET Framework Source Code

but it seems Silverlight source isn't available yet:

No Silverlight Source Code

Before Silverlight source becomes available, you can use the great tool .NET Reflector, originally written by Lutz Redder, now owned by RedGate, to browse Silverlight source code:

.NET Reflector

Conclusion

As we can see, Visual Studio is the best tool for debugging managed applications, and it has lots of advanced features that may not be very well known, like the commands in Immediate and Command windows. Not all features are available for Silverlight debugging, particularly SOS and Silverlight source code. I hope SOS be part of Visual Studio, and all NTSD (and most extension) commands available in Visual Studio.