HOWTO: Debugging a serviceLast reviewed: October 10, 1997Article ID: Q98890 |
The information in this article applies to:
SUMMARYThe steps in this article illustrate how to debug a service under Windows NT using WinDbg and the interactive debugger included with Microsoft Visual C++ 4.x. WinDbg ships with the Win32 Software Development Kit (SDK). For illustration purposes, these procedures use the SERVICE sample, which is built with debugging information by default. This sample is located in:
Mstools\Samples\Win32\Winnt\ServiceThere are two techniques for debugging a service. The first technique involves adding a DebugBreak() statement to the service's code and letting the Just-in-time(JIT) debugging feature of Windows NT spawn the debugger when the service executes the DebugBreak(). The second technique involves attaching the debugger to the service while it is running.
MORE INFORMATION
Preparation
Technique 1To specify Microsoft Visual C++ 4.X as your Just-in-time debugger:
ARTICLE-ID: Q103861 TITLE : Choosing the Debugger That the System Will SpawnIf the service is running in any account other than the LocalSystem, the DebugBreak() technique will not work correctly. If the service account belongs to the administrator's group, the following error message appears when you are running Microsoft Visual C++ 4.X on Windows NT 4.0:
Runtime error! Program: <MSDevDir\bin>\MSDEV.exe abnormal program terminationThis error does not occur on Windows NT 3.51. If the spawned debugger is Windbg, the debugger will appear to have correctly attached to the service but you will notice a painting problem with the application. If the service account does not belong to the administrator's group, the following error message appears on both Microsoft Visual C++ 4.X and Windbg:
Initialization of the dynamic link library <system>\system32\USER32.dll failed. The process is terminating abnormally.These errors occur because the service account does not have the proper security access to the interactive windowstation and desktop. The easiest solution to the problems is to apply a NULL dacl to the interactive windowstation and desktop, "winsta0\\default" such that you can debug a service that is running in an account other than the LocalSystem.
Sample CodeThe following sample code applies a NULL dacl to the interactive windowstation and desktop objects. This application should be executed before debugging the service. Once the debugging session has been completed, the DACLs for the interactive windowstation and desktop objects can be reset by logging off and then logging on again.
#include <windows.h> #include <stdio.h> void main(void) { HDESK hdesk = NULL; HWINSTA hwinsta = NULL; SECURITY_DESCRIPTOR sd; SECURITY_INFORMATION si = DACL_SECURITY_INFORMATION; __try { // // Obtain a handle to the interactive windowstation. // hwinsta = OpenWindowStation("winsta0", FALSE, WRITE_DAC); if (hwinsta == NULL) __leave; // // Obtain a handle to the interactive desktop. // hdesk = OpenDesktop("default", 0, FALSE, WRITE_DAC | DESKTOP_WRITEOBJECTS | DESKTOP_READOBJECTS); if (hdesk == NULL) __leave; // // Create a null dacl. // if (!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION)) __leave; if (!SetSecurityDescriptorDacl(&sd, TRUE, (PACL) NULL, FALSE)) __leave; // // Apply NULL dacl to the windowstation and desktop objects. // if (!SetUserObjectSecurity(hwinsta, &si, &sd)) __leave; if (!SetUserObjectSecurity(hdesk, &si, &sd)) __leave; } __finally { if (hdesk != NULL) CloseDesktop(hdesk); if (hwinsta != NULL) CloseWindowStation(hwinsta); } }If you are using Windbg as your Just-in-time debugger, you need to either include the path of the service's debug symbols in the system's environment variables or make a call to SetCurrentDirectory() in the service so that the debugger is able to find the debug symbols for the service.
Technique 2Debugging a Service with WinDbg:
NOTE: The "System Account" and the "LocalSystem Account" are the same account. Keywords : BseDebug BseSync kbtool Version : 3.51 4.0 Platform : NT WINDOWS Issue type : kbhowto |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |