Jun 26

Debugging F# Code

The F# compiler produces PDB files directly, including for optimized code. These enable debugging either within Visual Studio or with other dedicated debuggers such as dbgclr.exe or cordbg.exe.

Debugging with F# for Visual Studio 2003/2005

If you are using the “F# for Visual Studio” plug-in for your F# projects you can launch debugging sessions directly from the environment. This is as simple as creating an “F# Project”, populating it with your code, pressing F11 to “step into” your program, and then you’re away!

Using Another Editor? You can still debug using Visual Studio 2003/2005!

All versions of Microsoft Visual Studio can be used as debuggers, regardless of whether you have created an F# project or even if you don’t have the F# Visual Studio plug-in installed at all (e.g., you are using the command-line compiler and Makefiles). There are five ways to start a debugging session. In all cases you should compile your code with the -g option to generate debug symbols.

  • Just-in-time debugging. If an exception is not caught then a pop-up will typically give you the option to break directly into the Visual Studio debugger.
  • Click and launch debugging. If you built your .exe from the command-line with the -g option, from Visual Studio open a “Project/Solution” selecting your .exe file, then press F11 (Step Into) and you should be debugging your code.
  • Command-line launch debugging. You can launch directly into the debugger using devenv.exe /debugexe <executable-file> <args>
  • Attach debugging. You can attach to a running process containing F# code using the Visual Stduio menus and/or the options on the Visual Studio command-line
  • From your code. Calling System.Diagnostics.Debugger.Launch() causes a program to “ask for help”, which typically means it will pop up the standard “Just-in-time” debugging dialog box that allows the user to attach a debugger session to the program. This is very useful for assertion-like conditions that should never happen in your code. Calling System.Diagnostics.Debug.Assert() is similar, though a different assertion-specific dialog box is used. Likewise, calling System.Diagnostics.Debugger.Break() causes a break in an attached debugger, but only if a debugger is actually attached (it is ignored if no debugger is attached). System.Diagnostics.Debugger.Log() is used to send text to an attached debugger.

Tips. If debug symbols are not found for your code or for the F# library then you will probably want to disable “Just My Code” debugging in the “Options” menu (Visual Studio 2005 only). You can also set the _NT_SYMBOL_PATH variable globally on your machine.

Debugging with DBGCLR.EXE

The .NET Framework SDK comes with an excellent lightweight graphical debugger. To use this simply compile your code with the -g, make sure dbgclr.exe is on your path and then run it, e.g.,

dbgclr myexe.exe 

Using Visual C# 2005 Express and Friends

Visual C# 2005 Express, Visual Basic 2005 Express and some similar products are no-cost downloads from Microsoft site, and can all be used with F# code, or with multi-language programs. To use them with F#, open a console project and set the properties on the project to indicate the F# program to run.

Using CORDBG.EXE

This is the command-line debug shell from the .NET Framework SDK, and can be used with F# code. e.g.,

    C:devsrccode>cordbg
    (cordbg) run cdebug.exe
    (cordbg) help
    (cordbg) show 20
    (cordbg) si
    (cordbg) si
    (cordbg) si
    (cordbg) w