Debugging of Windows Services in general is not an easy task. Due to their nature Windows Services require some tricks from a developer. One of the most hardest problem with Windows Services is the crashes in the production environment due to unhandled exceptions in the program, e.g. access violation, division by zero, etc. In such situations crash dumps are invaluable. Of course, you should always keep in mind to save the binaries and debug symbols for each build and do correct versioning. Some developers include exception handlers right into Windows Services, so the program itself can write a crash dump. Another option is to write an external watch-dog application (usually, another small Windows Service, which controls the execution of main Service and writes a mini dump in case of problems).

One of the easiest thing you can do in order to get crash dumps for Windows Services is the Post-Mortem debugger. In general, this is a command which the Operating System will execute when one of the applications in the system is terminated due to unhandled exceptions. This is a preferred way when you don’t want to make changes in your Windows Service.
In general, several debuggers have options to write crash dumps through command line arguments. I recommend to use cdb. Why cdb and not WinDbg or a VS Debugger, for example? Well, the answer is simple. The cdb debugger perfectly matches the goal to write a crash dump for the Windows Service and exit immediately. So the Windows Service can be restarted. The WinDbg will write a crash dump but will remain in memory so locking the process. The crash dumps written by cdb can be perfectly analyzed in the VS Debugger (assuming native 32- or 64-bit C++ code).

At last, the instructions how to configure the cdb as a Post-Mort Debugger on your 64-bit system.

  1. Dowload and install Windows Debugging Tools for you platform (x86, x64, I64). The latest Setup will detect your platform automatically.
  2. Set the HKLM\SOFTWARE\Microsoft\WindowsNT\CurrentVersion\AeDebug\Debugger value to something like this: “C:\Program Files\Debugging Tools for Windows (x64)\cdb.exe” -p %ld -e %ld -g -c “~*k;.dump /mfh /u C:\CrashDump.dmp;.kill;qd”
  3. Set the HKLM\SOFTWARE\Microsoft\WindowsNT\CurrentVersion\AeDebug\Auto to 1.

Notes

To configure the Post-Mortem Debugger on your 32-bit platform just adjust the paths to the cdb Debugger.

To configure the Post-Mortem Debugger for 32-bit applications on 64-bit platform you should add additional settings

  1. Set the HKLM\SOFTWARE\Wow6432Node\Microsoft\WindowsNT\CurrentVersion\AeDebug\Debugger value to something like this: “C:\Program Files\Debugging Tools for Windows (x64)\cdb.exe” -p %ld -e %ld -g -c “~*k;.dump /mfh /u C:\CrashDump.dmp;.kill;qd”
  2. Set the HKLM\SOFTWARE\Wow6432Node\Microsoft\WindowsNT\CurrentVersion\AeDebug\Auto to 1.

So now when your Windows Service will crash the cdb Debugger will write a crash dump for it and you can analyze it later.