Debugging the Service Application Automatically
A debugger can be launched automatically when the service application starts up. Alternatively, it can be launched automatically when the service application encounters an exception or executes a DebugBreak command. If you have chosen one of these methods, this topic explains how to proceed. If you are not sure which method to choose, see Choosing the Best Method.
Then use the following procedure:
Do one of the following preparatory steps:
- If you plan to debug the service application from the very beginning, including its initialization code, follow the procedure described in Enabling the Debugging of the Initialization Code. Alternatively, if you want the service application to break into the debugger when it crashes or encounters an exception, follow the procedure described in Enabling the Service Application to Break Into the Debugger.
- To assure that the service application will allow the debugger to run properly, perform the procedure described in Adjusting the Service Application Timeout.
- If the service is combined with other services in a single SvcHost process, perform the procedure described in Isolating the Service.
If the service is already running, you must restart it for these changes to take effect. We recommend that you restart Windows itself, in order to remove any effects of the running service. If you do not want to restart Windows, use the following commands, where ServiceName is the name of the service:
net stop ServiceName net start ServiceName
If you have chosen to debug the service application's initialization code, when the service starts, the debugger is launched and attaches to the service application.
If you have chosen to let the debugger be triggered by an exception, the service application executes normally until it encounters an exception or executes a DebugBreak function. At this point, the debugger is launched and attaches to the service application.
The next step depends on the debugger command line you specified during step 1:
- If you specified a debugger without any remoting options, this debugger is launched and its window becomes visible.
- If you specified NTSD with the -server and -noio options, NTSD is launched without a console window. You can then connect to the debugging session from another computer by starting any user-mode debugger with the -remote parameter. For instructions, see Activating a Debugging Client.
- If you specified NTSD with the -d option, NTSD is launched without a console window. You can then connect to the debugging session by using kernel debugger running on another computer. For instructions, see Controlling the User-Mode Debugger from the Kernel Debugger.
- If you specified NTSD with the -ddefer and -server options, NTSD is launched without a console window. You can then connect to the debugging session by using both a kernel debugger and a user-mode remote debugger, running on a different computer than the service (but possibly the same computer as each other). For instructions, see Combining This Method with Remote Debugging.
When the debugger starts, the service pauses at the initial process breakpoint, the exception, or the DebugBreak command. This enables you to examine the current state of the service application, set breakpoints, and make any other desired configuration choices.
Use g (Go) or another execution command to resume the execution of the service application.