Discover the CLR Debugger

Debug your Web applications like you never could before.

Ken McNamee

October 30, 2009

5 Min Read
ITPro Today logo

ToolKit

LANGUAGES: All .NETLanguages

VERSIONS: 1.0 | 1.1

 

Discover the CLR Debugger

Debug your Web applications like you never could before.

 

By Ken McNamee

 

I must say I have been quite lucky the past few yearssince I started working exclusively with ASP.NET because I've been able to stepthrough my code as it executes, line by line, even down to SQL Server'sstored-procedure level. I'm lucky because, even years later, I remember what itwas like trying to debug an ASP 3.0 application using Visual InterDev orNotepad. As long as I'm developing with ASP.NET, I don't need to leaveResponse.Write statements strewn throughout my code to output the value of onevariable or another. Using the CLR Debugger (DbgCLR), which ships free with the.NET Framework SDK, I can debug an ASP.NET Web application like any other kindof software developed with a professional, high-level language IDE.

 

Speaking of professional development environments, DbgCLRactually is a slightly limited version of the debugger included with VisualStudio .NET. Or, is the Visual Studio .NET debugger a more powerful version ofDbgCLR? Well, it doesn't matter. Either way they are great debuggers. Thedifferences between them really are quite small. As the name suggests, DbgCLRsupports only debugging code written for the CLR; the VS .NET debuggeradditionally supports debugging native Win32 code. The only major differencethat might affect ASP.NET developers is DbgCLR's lack of a remote debuggingfeature. DbgCLR's few other differences are either minor inconveniences - suchas no Autos window - or not relevant to Web development - such as no Registerswindow.

 

Use DbgCLR With ASP.NET

The first thing you must do to debug a Web page usingDbgCLR is tell the ASP.NET runtime to emit debug symbols when compiling thepage. You do this by adding the Debug page directive:

 

<%@ Page Language="C#" Debug="True" %>

 

You can set this on a page-by-page basis, or you canrequire all pages to compile in debug mode with this web.config option:

 

           This is a flexible model because you can turn on the debugmode by default in web.config and still turn it off on a page-by-page basisusing the Debug page directive. Or, you can turn it off by default inweb.config, then use the Debug page directive to turn it on in selected pages.   The next step in using DbgCLR is to start the programitself, which is installed in the GuiDebug folder under your Framework SDKroot. Without Visual Studio .NET installed, this folder normally is located inC:Program FilesMicrosoft.NETFrameworkSDKGuiDebug for version 1.0, andC:Program FilesMicrosoft.NETSDKv1.1GuiDebug for version 1.1.   Although nearly identical to the VS .NET debugger, DbgCLRis not quite as simple to use. For ASP.NET pages, three requirements must bemet for DbgCLR to operate successfully: the source code for the page must beopened, the page must have been compiled in debug mode, and it must "attach"itself to the executable program running the page. If you are familiar onlywith integrated debuggers and have never used one that is external to your IDE,the process of attaching one program to another might seem a little alien. Inreality, however, you are doing manually what Visual Studio .NET does behindthe scenes automatically .   Once the source code for the page is opened in DbgCLR andit's compiled - you do this by requesting the page in the browser - click onthe Tools menu and select Debug Processes. You should see something similar tothe screen displayed in Figure 1. The process you need to attach to is theASP.NET worker process (aspnet_wp.exe). Remember, the IIS process,inetinfo.exe, merely accepts the request for the page from the browser client,invokes the ASP.NET ISAPI DLL, and passes off the page's execution toaspnet_wp.exe. If you don't see aspnet_wp.exe in the list of running processes,select the "Show system processes" checkbox because, by default, DbgCLRdisplays only the processes your user account has started.  
Figure 1. The Debug Process dialog is used to let DbgCLR attach to theASP.NET worker process, aspnet_wp.exe, which is where your Web pages areactually executed.   Now you can set breakpoints in the code. Run the pageagain and you should be able to step through not only the code in the page, butalso components called from the page. You also can edit the code from withinDbgCLR, but it doesn't include an Edit and Continue feature, nor does itinclude a Save function if you decide to edit the page from the debugger. Youcan circumvent these limitations by editing the page and closing it, at whichpoint a dialog pops up asking if you want to save it. This is certainly notideal, yet it's understandable because DbgCLR is meant to function only as adebugger, not a full-featured IDE.   Because I primarily use Visual Studio .NET, I don't get touse DbgCLR often. One use I have found for it is on machines where it isillogical or impossible to install the entire Visual Studio .NET environment.For example, I might use DbgCLR in QA or even production where an IDE isn'trequired but a powerful debugger can be invaluable. Visual Studio .NET isintended to debug remote processes, but I must admit I've had some trouble gettingthis to work. Also, some companies might employ a network architecture thatprohibits you from using a developer workstation to access a QA or productionmachine in this manner. Of course, if you don't own Visual Studio .NET, DbgCLRis the next best thing for your debugging needs.   The sample code in thisarticle is available for download.   Ken McNamee is an independent consultant who works withcompanies in need of highly scalable, data-driven Web applications. And whodoesn't need one of those these days? Prior to this, he led a team ofdevelopers in re-architecting the Home Shopping Network's e-commerce site,HSN.com, to 100 percent ASP.NET with C#. E-mail him at [email protected].      

Read more about:

Microsoft
Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like