Microsoft KB Archive/824344

= How to debug Windows services =

Article ID: 824344

Article Last Modified on 5/16/2007

-

APPLIES TO


 * Microsoft Windows 2000 Server
 * Microsoft Windows 2000 Advanced Server
 * Microsoft Windows 2000 Datacenter Server
 * Microsoft Windows 2000 Professional Edition
 * Microsoft Windows Server 2003, Enterprise Edition (32-bit x86)
 * Microsoft Windows Server 2003, Standard Edition (32-bit x86)
 * Microsoft Windows Server 2003, Datacenter Edition (32-bit x86)
 * Microsoft Windows XP Home Edition
 * Microsoft Windows XP Home Edition
 * Microsoft Windows XP Service Pack 1a
 * Microsoft Windows XP Home Edition
 * Microsoft Windows XP Professional
 * Microsoft Windows XP Professional
 * Microsoft Windows XP Service Pack 1a
 * Microsoft Windows XP Professional

-





Important This article contains information about how to modify the registry. Make sure to back up the registry before you modify it. Make sure that you know how to restore the registry if a problem occurs. For more information about how to back up, restore, and modify the registry, click the following article number to view the article in the Microsoft Knowledge Base:

256986 Description of the Microsoft Windows registry

IN THIS TASK

 * SUMMARY
 * Requirements
 * Attach the WinDbg debugger to a service after the service starts
 * Use the process ID of the process that hosts the service that you want to debug
 * Use the image name of the process that hosts the service that you want to debug
 * Start the WinDbg debugger and attach to the process that hosts the service that you want to debug
 * Configure a service to start with the WinDbg debugger attached
 * Troubleshooting
 * REFERENCES



SUMMARY
This step-by-step article describes how to debug a Windows service by using the WinDbg debugger (windbg.exe). To debug a Windows service, you can attach the WinDbg debugger to the process that hosts the service after the service starts, or you can configure the service to start with the WinDbg debugger attached so that you can troubleshoot service-startup-related problems. This article describes both these methods.

back to the top

Requirements
This article assumes that you are familiar with the following topics:
 * Windows Services
 * WinDbg Debugger

back to the top

Attach the WinDbg debugger to a service after the service starts
This method is similar to the method that you can use to attach a debugger to a process and then debug a process.

Use the process ID of the process that hosts the service that you want to debug
 To determine the process ID (PID) of the process that hosts the service that you want to debug, use one of the following methods.  ===== Method 1: Use the Task Manager =====

 Right-click the taskbar, and then click Task Manager. The Windows Task Manager dialog box appears. Click the Processes tab of the Windows Task Manager dialog box. Under Image Name, click the image name of the process that hosts the service that you want to debug. Note the process ID of this process as specified by the value of the corresponding PID field.  ===== Method 2: Use the Task List Utility (tlist.exe) =====

 Click Start, and then click Run. The Run dialog box appears.</li> In the Open box, type cmd, and then click OK.</li> At the command prompt, change the directory path to reflect the location of the tlist.exe file on your computer.

Note The tlist.exe file is typically located in the following directory: C:\Program Files\Debugging Tools for Windows</li> At the command prompt, type tlist to list the image names and the process IDs of all processes that are currently running on your computer.

Note Make a note of the process ID of the process that hosts the service that you want to debug.</li></ol> </li></ul> </li> At a command prompt, change the directory path to reflect the location of the windbg.exe file on your computer.

Note If a command prompt is not open, follow steps a and b of Method 1. The windbg.exe file is typically located in the following directory: C:\Program Files\Debugging Tools for Windows.</li> At the command prompt, type windbg –p  /g to attach the WinDbg debugger to the process that hosts the service that you want to debug.

Note  is a placeholder for the process ID of the process that hosts the service that you want to debug.</li></ol>

back to the top

Use the image name of the process that hosts the service that you want to debug
You can use this method only if there is exactly one running instance of the process that hosts the service that you want to run. To do this, follow these steps:
 * 1) Click Start, and then click Run. The Run dialog box appears.
 * 2) In the Open box, type cmd, and then click OK to open a command prompt.
 * 3) At the command prompt, change the directory path to reflect the location of the windbg.exe file on your computer.

Note The windbg.exe file is typically located in the following directory: C:\Program Files\Debugging Tools for Windows.
 * 1) At the command prompt, type windbg –pn   /g to attach the WinDbg debugger to the process that hosts the service that you want to debug.

Note is a placeholder for the image name of the process that hosts the service that you want to debug. The &quot;-pn&quot; command-line option specifies that the  command-line argument is the image name of a process.

back to the top

Start the WinDbg debugger and attach to the process that hosts the service that you want to debug

 * 1) Start Windows Explorer.
 * 2) Locate the windbg.exe file on your computer.

Note The windbg.exe file is typically located in the following directory: C:\Program Files\Debugging Tools for Windows
 * 1) Run the windbg.exe file together with the /g command-line switch to start the WinDbg debugger. The /g command-line switch allows the tracked process to continue after the break point is set.
 * 2) On the File menu, click Attach to a Process to display the Attach to Process dialog box.
 * 3) Click to select the node that corresponds to the process that hosts the service that you want to debug, and then click OK.
 * 4) In the dialog box that appears, click Yes to save base workspace information. Notice that you can now debug the disassembled code of your service.

back to the top

Configure a service to start with the WinDbg debugger attached
You can use this method to debug services if you want to troubleshoot service-startup-related problems.  Configure the &quot;Image File Execution&quot; options. To do this, use one of the following methods: <ul> ===== Method 1: Use the Global Flags Editor (gflags.exe) =====

 Start Windows Explorer.</li> Locate the gflags.exe file on your computer.

Note The gflags.exe file is typically located in the following directory: C:\Program Files\Debugging Tools for Windows.</li> Run the gflags.exe file to start the Global Flags Editor.</li> In the Image File Name text box, type the image name of the process that hosts the service that you want to debug. For example, if you want to debug a service that is hosted by a process that has MyService.exe as the image name, type MyService.exe .</li> <li>Under Destination, click to select the Image File Options option.</li> <li>Under Image Debugger Options, click to select the Debugger check box.</li> <li>In the Debugger text box, type the full path of the debugger that you want to use. For example, if you want to use the WinDbg debugger to debug a service, you can type a full path that is similar to the following: C:\Program Files\Debugging Tools for Windows\windbg.exe </li> <li>Click Apply, and then click OK to quit the Global Flags Editor.</li></ol> </li> <li>===== Method 2: Use Registry Editor =====

<ol style="list-style-type: lower-alpha;"> <li>Click Start, and then click Run. The Run dialog box appears.</li> <li>In the Open box, type regedit, and then click OK to start Registry Editor.</li> <li>Warning Serious problems might occur if you modify the registry incorrectly by using Registry Editor or by using another method. These problems might require that you reinstall your operating system. Microsoft cannot guarantee that these problems can be solved. Modify the registry at your own risk.

In Registry Editor, locate, and then right-click the following registry subkey:

</li> <li>Point to New, and then click Key. In the left pane of Registry Editor, notice that New Key #1 (the name of a new registry subkey) is selected for editing.</li> <li>Type to replace New Key #1, and then press ENTER.

Note  is a placeholder for the image name of the process that hosts the service that you want to debug. For example, if you want to debug a service that is hosted by a process that has MyService.exe as the image name, type MyService.exe .</li> <li>Right-click the registry subkey that you created in step e.</li> <li>Point to New, and then click String Value. In the right pane of Registry Editor, notice that New Value #1, the name of a new registry entry, is selected for editing.</li> <li>Replace New Value #1 with Debugger, and then press ENTER.</li> <li>Right-click the Debugger registry entry that you created in step h, and then click Modify. The Edit String dialog box appears.</li> <li>In the Value data text box, type, and then click OK.

Note  is a placeholder for the full path of the debugger that you want to use. For example, if you want to use the WinDbg debugger to debug a service, you can type a full path that is similar to the following:

C:\Progra~1\Debugg~1\windbg.exe

</li></ol> </li></ul> </li> <li>For the debugger window to appear on your desktop, and to interact with the debugger, make your service interactive. If you do not make your service interactive, the debugger will start but you cannot see it and you cannot issue commands. To make your service interactive, use one of the following methods: <ul> <li>===== Method 1: Use the Services console =====

<ol style="list-style-type: lower-alpha;"> <li>Click Start, and then point to Programs.</li> <li>On the Programs menu, point to Administrative Tools, and then click Services. The Services console appears.</li> <li>In the right pane of the Services console, right-click  , and then click Properties.

Note  is a placeholder for the name of the service that you want to debug.</li> <li>On the Log On tab, click to select the Allow service to interact with desktop check box under Local System account, and then click OK.</li></ol> </li> <li>===== Method 2: Use Registry Editor =====

<ol style="list-style-type: lower-alpha;"> <li>In Registry Editor, locate, and then click the following registry subkey:

Note Replace  with the name of the service that you want to debug. For example, if you want to debug a service named MyService, locate and then click the following registry key:

</li> <li>Under the Name field in the right pane of Registry Editor, right-click Type, and then click Modify. The Edit DWORD Value dialog box appears.</li> <li>Change the text in the Value data text box to the result of the binary OR operation with the binary value of the current text and the binary value, 0x00000100, as the two operands. The binary value, 0x00000100, corresponds to the SERVICE_INTERACTIVE_PROCESS constant that is defined in the WinNT.h header file on your computer. This constant specifies that a service is interactive in nature.</li></ol> </li></ul> </li> <li>When a service starts, the service communicates to the Service Control Manager how long the service must have to start (the time-out period for the service). If the Service Control Manager does not receive a &quot;service started&quot; notice from the service within this time-out period, the Service Control Manager terminates the process that hosts the service. This time-out period is typically less than 30 seconds. If you do not adjust this time-out period, the Service Control Manager ends the process and the attached debugger while you are trying to debug. To adjust this time-out period, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>In Registry Editor, locate, and then right-click the following registry subkey:

</li> <li>Point to New, and then click DWORD Value. In the right pane of Registry Editor, notice that New Value #1 (the name of a new registry entry) is selected for editing.</li> <li>Type ServicesPipeTimeout to replace New Value #1, and then press ENTER.</li> <li>Right-click the ServicesPipeTimeout registry entry that you created in step c, and then click Modify. The Edit DWORD Value dialog box appears.</li> <li>In the Value data text box, type, and then click OK

Note  is a placeholder for the value of the time-out period (in milliseconds) that you want to set for the service. For example, if you want to set the time-out period to 24 hours (86400000 milliseconds), type 86400000 .</li> <li>Restart the computer. You must restart the computer for Service Control Manager to apply this change.</li></ol> </li> <li>Start your Windows service. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>Click Start, and then point to Programs.</li> <li>On the Programs menu, point to Administrative Tools, and then click Services. The Services console appears.</li> <li>In the right pane of the Services console, right-click  , and then click Start.

Note  is a placeholder for the name of the service that you want to debug.</li></ol> </li></ol>

back to the top

Troubleshooting
Before you try to debug a service across a network, make sure that the symbols and the source files that the service uses are accessible from the computer where the service will run. To do this, use one of the following methods:
 * Grant at least read-access permissions to everyone for the folder on your computer that contains the symbols and the source files that the service uses.
 * Copy these symbols and source files that the service uses to the computer where the service will run.

back to the top

<div class="references_section">