Visual Studio Hacks [Electronic resources]

Andrew Lockhart

نسخه متنی -صفحه : 172/ 71
نمايش فراداده

Hack 42. Debug a Failing Application

Launch the debugger when an application starts to crash.

Just-in-time debugging, or JIT debugging for short, is the ability to attach to a running program that has failed by throwing an unhandled exception. For instance, you might be testing an application, but not running the application in the debugger, and the application throws an exception. Without JIT debugging, you would have to try and reproduce the error after restarting the application in the debugger. With JIT debugging, you can immediately attach to the program and investigate the exception.

JIT debugging does not have anything to do with Just-in-time compilation; the abbreviation may mean the same thing but represents vastly different things. Just-in-time compilation is the part of the Common Language Runtime that compiles intermediate language into machine code "just in time" for the machine to process it. (Literally when you call a method, the JIT compiler checks to make sure the code for that method exists; if it does not, then it is compiled from the IL [Hack #63] .)

5.8.1. Using JIT Debugging

Using JIT debugging is pretty simple. When you are running an application that throws an unhandled exception, you will see the dialog shown in Figure 5-30.

Figure 5-30. Just-in-Time Debugging dialog

The Just-in-Time Debugging dialog offers you a number of different debuggers that you can attach to your application based on the debuggers that are currently installed on your machine. The first debugger listed is the instance of Visual Studio that is currently open on my machine. The two versions of the Microsoft CLR Debugger (DBGCLR.EXE) are the free debuggers that ship with each version of the .NET framework (1.0 and 1.1). The next two debuggers are the two versions of Visual Studio installed on my machine (2002 and 2003). From this dialog, you can attach the application to any one of these debuggers. If you select a version of Visual Studio and click Yes, the next dialog you will see is shown in Figure 5-31.

Figure 5-31. Attach to Process dialog

The dialog shown in Figure 5-31 is the standard Attach to Process dialog. After clicking the OK button, you will be given the standard exception dialog where you can choose to continue on in the execution of the program or break in the current location.

JIT debugging is a valuable feature that can save time when trying to debug hard to find bugs. Knowing how to use and configure this feature will help save you from the frustrating task of re-creating hard to find bugs.

5.8.2. Windows Forms JIT Debugging

When you first try JIT debugging with a Windows Forms application, it probably won't work. By default, Windows Forms will display a dialog that gives you the ability to see the details of the exception, continue, or quit, but not the ability to attach a debugger to the process. To enable JIT debugging with Windows Forms, you will need to add a configuration setting either to the app.config of your application or, if you want to enable JIT debugging for all Windows Forms applications, to the machine.config. To enable JIT debugging, you need to add the following element inside the <configuration> element:

<system.windows.forms jitDebugging="true" />

This will enable JIT debugging either for the application (if you added it to the app.config) or for all Windows Forms applications on your machine (if you added it to the machine.config).

If your application does not already have an app.config, you will need to create one by right-clicking on the project, selecting Add New Item, and then selecting the application configuration file from the list of files.

If you want to modify the machine.config file to enable JIT debugging for all Windows Forms applications, the file can be found in the following directory:

%SystemRoot%\Microsoft.NET\Framework\v1.1.4322\machine.config

The machine.config file actually already has this elementit simply needs to be uncommented.

5.8.3. JIT Settings

A number of settings, in both Visual Studio and the registry, allow you to control how JIT debugging works. The first way that you can configure JIT debugging is through the Tool Options window. Under the Debugging folder is an item for JIT debugging, which is shown in Figure 5-32.

Figure 5-32. Just-in-Time Debugging settings

On this screen, you can disable JIT debugging for any of the various program types. Disabling JIT debugging here overrides any settings in your configuration files. When an exception is thrown, you will see a dialog stating that JIT debugging is disabled.

A number of registry settings can also be used to configure JIT debugging and the .NET framework. Both keys are located at:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework

The first key is called DbgManagedDebugger and specifies what application should be launched when an exception is detected. The default value for this is vs7jit.exe, which launches the standard Visual Studio .NET JIT debugger dialog that was shown in Figure 5-30. The second key is DbgJITDebugLaunchSetting, which specifies what should be done when an unhandled exception is caught. Table 5-1 shows the possible values for this key.

Table 5-1. DbgJITDebugLaunchSetting key values

Value

Behavior

0

Will prompt you with a dialog giving you two options:

OK

Terminates the application

Cancel

Calls the application specified in DbgManagedDebugger

1

Disables JIT debugging and passes control back to the application and the default error handler.

2

Control is immediately passed to the application specified in DbgManagedDebugger. This is the default behavior.

Using the settings available in Visual Studio and the registry, you will be able to configure JIT debugging to your specific needs.