Monday, September 15, 2008

Debugging Services

We all know the problem - to debug a program (particularly if it's the startup procedures that you need to look at), you simply load the solution in Visual Studio and hit F5.  Except if it's a service.  With a service, you can't just run it but instead it needs to be launched via the Service Control Manager, which makes debugging its startup a real pain.

The solutions that I've used in the past have either been to have a command line option to enable the process to launch as a regular process as opposed to being control by the SCM, or to have it sleep for a number of seconds when it is launched.  Either of these gives me a route to get a debugger attached before anything "interesting" happens.  But both of these also mean I've got code present that isn't going to be in the live, and I much prefer to be debugging "the real thing" rather than some (albeit close) approximation.

But there's a third way, which I'd not heard of before - there is a registry setting for "Image File Execution Options" which, amongst other things, allows you to specify that when an app is launched (via CreateProcess(), which covers most scenarios), instead of just firing up the exe it instead runs the required debugger.  Nice :)

For more details, check out this blog on the subject.  There's also this entry which describes a few more of the settings that are available.

No comments: