This is the mail archive of the
gdb-prs@sources.redhat.com
mailing list for the GDB project.
gdb/901: GDB can't attach to Windows applications running as services
- From: obry at gnat dot com
- To: gdb-gnats at sources dot redhat dot com
- Date: 22 Dec 2002 15:17:22 -0000
- Subject: gdb/901: GDB can't attach to Windows applications running as services
- Reply-to: obry at gnat dot com
>Number: 901
>Category: gdb
>Synopsis: GDB can't attach to Windows applications running as services
>Confidential: no
>Severity: non-critical
>Priority: medium
>Responsible: unassigned
>State: open
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Sun Dec 22 07:18:00 PST 2002
>Closed-Date:
>Last-Modified:
>Originator: obry@gnat.com
>Release: GDB 5.1.1
>Organization:
>Environment:
>Description:
GDB can't attach to Windows applications running as services because the child process privileges has not been correctly.
GDB answer is : Can't attach to process.
>How-To-Repeat:
Just try to attach to a pid that is a Windows services.
>Fix:
2002-12-22 Pascal Obry <obry@gnat.com>
* win32-nat.c: SetPrivilege new routine. Child_Attach call
SetPrivilege to correctly set child privileges to be able to debug
applications running as services.
Index: win32-nat.c
===================================================================
RCS file: /nile.c/cvs/Dev/gdb/gdb-5/gdb/win32-nat.c,v
retrieving revision 1.1.1.1.4.6
retrieving revision 1.1.1.1.4.7
diff -c -r1.1.1.1.4.6 -r1.1.1.1.4.7
*** win32-nat.c 29 Nov 2002 09:32:52 -0000 1.1.1.1.4.6
--- win32-nat.c 22 Dec 2002 14:44:26 -0000 1.1.1.1.4.7
***************
*** 1147,1161 ****
--- 1147,1240 ----
return 0;
}
+ /*
+ This routine is copied from the knowledge base of the SDK
+ see article PSS ID Number: Q131065 on http://support.microsoft.com.
+
+ This routine is used to gain the right privileges to be able to debug
+ Windows applications running as Services.
+ */
+
+ BOOL SetPrivilege (HANDLE hToken, // token handle
+ LPCTSTR Privilege, // Privilege to enable/disable
+ BOOL bEnablePrivilege) // TRUE to enable. FALSE to disable
+ {
+ TOKEN_PRIVILEGES tp;
+ LUID luid;
+ TOKEN_PRIVILEGES tpPrevious;
+ DWORD cbPrevious = sizeof (TOKEN_PRIVILEGES);
+
+ if (!LookupPrivilegeValue (NULL, Privilege, &luid))
+ return FALSE;
+
+ /*
+ First pass. Get current privilege setting.
+ */
+ tp.PrivilegeCount = 1;
+ tp.Privileges[0].Luid = luid;
+ tp.Privileges[0].Attributes = 0;
+
+ AdjustTokenPrivileges (hToken,
+ FALSE,
+ &tp,
+ sizeof (TOKEN_PRIVILEGES),
+ &tpPrevious,
+ &cbPrevious);
+
+ if (GetLastError() != ERROR_SUCCESS)
+ return FALSE;
+
+ /*
+ Second pass. Set privilege based on previous setting.
+ */
+ tpPrevious.PrivilegeCount = 1;
+ tpPrevious.Privileges[0].Luid = luid;
+
+ if (bEnablePrivilege)
+ {
+ tpPrevious.Privileges[0].Attributes |= (SE_PRIVILEGE_ENABLED);
+ }
+ else
+ {
+ tpPrevious.Privileges[0].Attributes ^=
+ (SE_PRIVILEGE_ENABLED & tpPrevious.Privileges[0].Attributes);
+ }
+
+ AdjustTokenPrivileges (hToken,
+ FALSE,
+ &tpPrevious,
+ cbPrevious,
+ NULL,
+ NULL);
+
+ if (GetLastError() != ERROR_SUCCESS)
+ return FALSE;
+
+ return TRUE;
+ }
+
/* Attach to process PID, then initialize for debugging it. */
static void
child_attach (char *args, int from_tty)
{
BOOL ok;
DWORD pid;
+ HANDLE hToken;
if (!args)
error_no_arg ("process-id to attach");
+
+ if (OpenProcessToken (GetCurrentProcess(),
+ TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
+ &hToken))
+ {
+ if (!SetPrivilege (hToken, SE_DEBUG_NAME, TRUE))
+ {
+ printf_unfiltered ("Failed to get SE_DEBUG_NAME priviledge");
+ printf_unfiltered ("This can cause attach to fail on Windows NT");
+ }
+ CloseHandle (hToken);
+ }
pid = strtoul (args, 0, 0);
ok = DebugActiveProcess (pid);
>Release-Note:
>Audit-Trail:
>Unformatted: