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.
|
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.
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.
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.
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 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.
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
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.
Value |
Behavior |
---|---|
0 |
Will prompt you with a dialog giving you two options:
Terminates the application
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.