3 Copyright 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
5 This software is a copyrighted work licensed under the terms of the
6 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
13 #include "perthread.h"
14 #include "perprocess.h"
27 /* Here lies extra debugging routines which help track down internal
28 Cygwin problems when compiled with -DDEBUGGING . */
30 #define NFREEH (sizeof (cygheap->debug.freeh) / sizeof (cygheap->debug.freeh[0]))
37 lock_debug () : acquired (0)
39 if (locker && !exit_state)
40 acquired = !!locker->acquire (INFINITE);
44 if (locker && acquired)
50 ~lock_debug () {unlock ();}
51 friend void debug_init ();
54 muto NO_COPY *lock_debug::locker = NULL;
56 static bool __stdcall mark_closed (const char *, int, HANDLE, const char *, bool);
61 muto *debug_lock_muto;
62 lock_debug::locker = new_muto (debug_lock_muto);
65 /* Find a registered handle in the linked list of handles. */
66 static handle_list * __stdcall
67 find_handle (HANDLE h)
70 for (hl = &cygheap->debug.starth; hl->next != NULL; hl = hl->next)
73 cygheap->debug.endh = hl;
80 #ifdef DEBUGGING_AND_FDS_PROTECTED
82 setclexec (HANDLE oh, HANDLE nh, bool not_inheriting)
84 handle_list *hl = find_handle (oh);
88 hl->inherited = !not_inheriting;
94 /* Create a new handle record */
95 static handle_list * __stdcall
101 for (hl = cygheap->debug.freeh; hl < cygheap->debug.freeh + NFREEH; hl++)
102 if (hl->name == NULL)
108 /* Add a handle to the linked list of known handles. */
110 add_handle (const char *func, int ln, HANDLE h, const char *name, bool inh)
115 if ((hl = find_handle (h)))
118 if (hl->name == name && hl->func == func && hl->ln == ln)
120 system_printf ("%s:%d - multiple attempts to add handle %s<%p>", func,
122 system_printf (" previously allocated by %s:%d(%s<%p>) winpid %d",
123 hl->func, hl->ln, hl->name, hl->h, hl->pid);
127 if ((hl = newh ()) == NULL)
130 debug_printf ("couldn't allocate memory for %s(%d): %s(%p)",
140 hl->pid = GetCurrentProcessId ();
141 cygheap->debug.endh->next = hl;
142 cygheap->debug.endh = hl;
143 debug_printf ("protecting handle '%s', inherited flag %d", hl->name, hl->inherited);
148 static void __stdcall
149 delete_handle (handle_list *hl)
151 handle_list *hnuke = hl->next;
152 debug_printf ("nuking handle '%s'", hnuke->name);
153 hl->next = hl->next->next;
154 memset (hnuke, 0, sizeof (*hnuke));
158 debug_fixup_after_fork_exec ()
160 /* No lock needed at this point */
162 for (hl = &cygheap->debug.starth; hl->next != NULL; /* nothing */)
163 if (hl->next->inherited)
166 delete_handle (hl); // removes hl->next
169 static bool __stdcall
170 mark_closed (const char *func, int ln, HANDLE h, const char *name, bool force)
175 if ((hl = find_handle (h)) && !force)
178 here.unlock (); // race here
179 system_printf ("attempt to close protected handle %s:%d(%s<%p>) winpid %d",
180 hl->func, hl->ln, hl->name, hl->h, hl->pid);
181 system_printf (" by %s:%d(%s<%p>)", func, ln, name, h);
186 if (hl && (hln = hl->next) && strcmp (name, hln->name))
188 system_printf ("closing protected handle %s:%d(%s<%p>)",
189 hln->func, hln->ln, hln->name, hln->h);
190 system_printf (" by %s:%d(%s<%p>)", func, ln, name, h);
199 /* Close a known handle. Complain if !force and closing a known handle or
200 if the name of the handle being closed does not match the registered name. */
202 close_handle (const char *func, int ln, HANDLE h, const char *name, bool force)
207 if (!mark_closed (func, ln, h, name, force))
210 ret = CloseHandle (h);
212 #if 0 /* Uncomment to see CloseHandle failures */
214 small_printf ("CloseHandle(%s) failed %s:%d\n", name, func, ln);
220 __set_errno (const char *func, int ln, int val)
222 debug_printf ("%s:%d val %d", func, ln, val);