FAQ: How do I debug a custom rule?

While writing your custom rule, you will likely come across a need to debug it and work out why it's behaving in a particular manner.

Debugging within FxCop 

Debugging a custom rule within the FxCop UI is actually quite easy. To do so, simply:

  1. Open the FxCop project that contains your custom rule (making sure the PDB is alongside the assembly)
  2. Open the solution that contains your custom rule and set a breakpoint in the rule your want to debug
  3. Choose Debug -> Attach to Process
  4. Under Available Processes, choose FxCop.exe and click Attach
  5. In FxCop, choose Project -> Analyze

The debugger should break when it hits the breakpoint.

Debugging within Visual Studio or via command-line

However, if you want debug a rule that you've written for Visual Studio Code Analysis, or via the command-line, the procedure is not so obvious:

  1. Open a Visual Studio Command Prompt

  2. If debugging a custom rule written for Visual Studio, run the following:

    > cd %VSINSTALLDIR%\Team Tools\Static Analysis Tools\FxCop

    Otherwise, if debugging a custom rule written for FxCop, run the following:

    > cd %PROGRAMFILES%\Microsoft FxCop 1.35

  3. Run the following command, replacing [TargetAssembly] and [RuleAssembly] with the assembly you want to analyze and the assembly that contains your custom rule respectively.

    > devenv /debugexe FxCopCmd.exe /file: [TargetAssembly] /rule: [RuleAssembly] /console

    This will open Visual Studio in a state that is ready to debug your custom rule.

  4. Choose File -> Open -> File and browse to and open the source file that contains your custom rule

  5. Set a breakpoint in the rule

  6. Choose Debug -> Start Debugging

The debugger should break when it hits the breakpoint.

The Modules window is your friend

 

If the breakpoint is not being hit, check to make sure that you actually have symbols for your custom rule loaded:

  1. While debugging, choose Debug -> Windows -> Modules

    To do this if you are debugging via /debugexe switch, as soon as the command window opens to start running FxCop, click PAUSE on the keyboard .

  2. Look for your rule assembly and check under the Symbol Status column.

  3. If this column states anything other Symbols Loaded, then right-click on the assembly and choose Load Symbols and browse manually to the PDB.

Comments