Diagnosing Errors with Managed Debugging Assistants 

Managed debugging assistants (MDAs) are debugging aids that work in conjunction with the common language runtime (CLR) to provide information on runtime state. The assistants generate informational messages about runtime events that you cannot otherwise trap. You can use MDAs to isolate hard-to-find application bugs that occur when transitioning between managed and unmanaged code. You can enable or disable all MDAs by adding a key to the Windows registry or by setting an environment variable. You can enable specific MDAs by using application configuration settings. You can set additional configuration settings for some individual MDAs in the application's configuration file. Because these configuration files are parsed when the runtime is loaded, you must enable the MDA before the managed application starts. You cannot enable it for applications that have already started.

The following table lists the MDAs that ship with the .NET Framework.

Enabling and Disabling MDAs

You can enable and disable MDAs by using a registry key, an environment variable, and application configuration settings. Either the registry key or the environment variable must be enabled to use the application configuration settings.

Enabling and Disabling by Using a Registry Key

You can enable MDAs by adding the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\MDA subkey in the Windows registry. Copy the following example into a text file named "MDAEnable.reg":

Windows Registry Editor Version 5.00

From a command prompt, execute the MDAEnable.reg file to enable MDAs on that computer. To disable MDAs, copy the following example into a text file named "MDADisable.reg":

Windows Registry Editor Version 5.00

Then, run the MDADisable.reg file from a command prompt.

By default, some MDAs are enabled when running the application attached to a debugger, even without adding the registry key. Examples of such assistants are PInvokeStackImbalance and InvalidApartmentStateChange. You can disable these assistants by running the MDADisable.reg file as described above.

Enabling and Disabling by Using an Environment Variable

MDA activation can also controlled by the environment variable COMPLUS_MDA. The environment variable overrides the registry key. The string is a case-insensitive, semicolon delimited list of MDA names or other special control strings. Starting under a managed or unmanaged debugger enables a set of MDAs by default. This is done by implicitly prepending the semicolon delimited list of MDAs enabled by default under debuggers to the value of the environment variable or registry key. The special control strings are the following:

  • 0 - Deactivates all MDAs.

  • 1 - Reads MDA settings from ApplicationName.mda.config.

  • managedDebugger - Explicitly activates all MDAs that are implicitly activated when a managed executable is started under a debugger.

  • unmanagedDebugger - Explicitly activates all MDAs that are implicitly activated when an unmanaged executable is started under a debugger.

If there are conflicting settings, the most recent settings override previous settings:

  • COMPLUS_MDA=0 disables all MDAs including those implicitly enabled under a debugger.

  • COMPLUS_MDA=gcUnmanagedToManaged enables gcUnmanagedToManaged in addition to any implicitly enabled under a debugger.

  • COMPLUS_MDA =0;gcUnmanagedToManaged enables gcUnmanagedToManaged but disables those MDAs that would otherwise be implicitly enabled under a debugger.

Enabling and Disabling by Using Application-Specific Configuration Settings

You can enable, disable, and configure some assistants individually in the MDA configuration file for the application. To enable the use of an application configuration file for configuring MDAs, either the MDA registry key or the COMPLUS_MDA environment variable must be set. The application configuration file is typically located in the same directory as the application's executable file (.exe). The file name takes the form ApplicationName.mda.config; for example, notepad.exe.mda.config. Assistants enabled in the application configuration file may have attributes or elements specifically designed to control that assistant's behavior. The following example shows how to enable and configure the Marshaling MDA.

        <match name="*"/>
        <match name="*"/>

These settings enable and configure the Marshaling MDA, which emits information describing the managed type that is being marshaled to an unmanaged type for each managed-to-unmanaged transition in the application. The Marshaling MDA has further flexibility to filter the name of the method and structure fields supplied in the <methodFilter> and <fieldFilter> child elements, respectively.

For more information on the settings specific to each individual MDA, see the documentation for that MDA.

MDA Output

MDA output is similar to the following example, which shows the output from the pInvokeStackImbalance MDA.

A call to PInvoke function 'MDATest!MDATest.Program::StdCall' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

See Also

Community Additions