Microsoft KB Archive/298603

= PRB: Error Attaching a Debugger from a Terminal Services Client Session =

PSS ID Number: 298603

Article Last Modified on 4/24/2003

-

The information in this article applies to:


 * Microsoft Terminal Services Advanced Client

-



This article was previously published under Q298603



SYMPTOMS
If you try to attach a debugger to a running process in a Terminal Services client session, an error may be returned. The exact error depends on the debugger that is used.

When you use the Windows 2000 symbolic debugger, Ntsd.exe, you receive the following error message:

NTSD: cannot debug pid  (error = 5)

When you use the Microsoft Windows Debugger, WinDbg.exe, you receive the following error message:

Cannot debug pid , Win32 error 5

Access is denied.

When you use the Microsoft Visual C++ debugger, Msdev.exe, you receive the following error message:

Cannot execute program.

NOTE:  is the process ID (PID) of the process to which you are trying to attach the debugger.



CAUSE
Under Windows 2000, Terminal Services does not permit a debugger to cross the session boundary to attach to a process. This is also true for services. Because service processes run in Session 0, attempts to debug such a process from a Terminal Services client session also fail.



RESOLUTION
To work around this problem, use one of the following methods:  Use the Image File Execution Options registry key to attach a debugger at the time that the program launches. The NT loader attaches the debugger to the process at the moment before the process begins to execute. Because the process boundary does not yet exist, the attach succeeds. To debug a service, change the program so that it can run as a console application. When the program is launched in the same session as the Terminal Services client, it is possible to connect a debugger.

This approach may require additional code (if not already present). For example, it would be appropriate to change the main function of a service to check for a special command-line switch to indicate console mode. Then, the main function calls ServiceMain directly, instead of calling StartServiceCtrlDispatcher. To debug the service in its most natural environment and avoid the process boundary issue, attach a kernel debugger and enable remote debugging. Use remote debugging features of WinDbg.exe. To debug a service, follow these steps:  To install the latest debuggers, visit the following Microsoft Web site: http://www.microsoft.com/ddk/debugging</li>  If the debuggers were installed to c:\debuggers, run the following command at the command prompt: at 0:00 c:\debuggers\cdb.exe -server npipe:pipe=boo -p -g </li> In Control Panel, double-click the Scheduled Tasks icon, right-click the task that was created by at.exe, and then click Run. You see the cdb.exe process appear in Windows Task Manager.</li>  Connect the WinDbg client to the cdb.exe debugging session by running the following command: c:\debuggers\windbg.exe -remote npipe:server=%computername%,pipe=boo </li></ol> </li></ul>

<div class="status_section">

STATUS
This behavior is by design.

<div class="moreinformation_section">

Steps to Reproduce the Behavior
The following steps reproduce the problem using the Microsoft Visual C++ Debugger and Notepad. These steps assume that Msdev.exe is in the user path of the client: <ol> Log on at the Terminal Server console.</li> Start Notepad.</li> Start a Terminal Services client session with an account (such as Administrator) that has the correct permissions to debug processes.</li> Open a command window.</li> Obtain the PID for Notepad by using TLIST or Task Manager.</li> At the same command prompt, type the following:

msdev -p <notepad_pid>

The following error is returned:

Cannot execute program.

</li></ol>

<div class="references_section">