3 This software is a copyrighted work licensed under the terms of the
4 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
19 /* Here lies extra debugging routines which help track down internal
20 Cygwin problems when compiled with -DDEBUGGING . */
21 #define NFREEH (sizeof (cygheap->debug.freeh) / sizeof (cygheap->debug.freeh[0]))
25 static NO_COPY SRWLOCK lock
;
27 lock_debug () { AcquireSRWLockExclusive (&lock
); }
28 ~lock_debug () { ReleaseSRWLockExclusive (&lock
); }
31 SRWLOCK NO_COPY
lock_debug::lock
= SRWLOCK_INIT
;
33 static bool mark_closed (const char *, int, HANDLE
, const char *, bool);
35 /* Find a registered handle in the linked list of handles. */
37 find_handle (HANDLE h
)
40 for (hl
= &cygheap
->debug
.starth
; hl
->next
!= NULL
; hl
= hl
->next
)
50 verify_handle (const char *func
, int ln
, HANDLE h
)
53 handle_list
*hl
= find_handle (h
);
56 system_printf ("%s:%d - multiple attempts to add handle %p", func
, ln
, h
);
58 system_printf (" previously allocated by %s:%d(%s<%p>) winpid %d",
59 hl
->func
, hl
->ln
, hl
->name
, hl
->h
, hl
->pid
);
63 setclexec (HANDLE oh
, HANDLE nh
, bool not_inheriting
)
66 handle_list
*hl
= find_handle (oh
);
70 hl
->inherited
= !not_inheriting
;
75 /* Create a new handle record */
81 for (hl
= cygheap
->debug
.freeh
; hl
< cygheap
->debug
.freeh
+ NFREEH
; hl
++)
89 modify_handle (const char *func
, int ln
, HANDLE h
, const char *name
, bool inh
)
92 handle_list
*hl
= find_handle (h
);
95 system_printf ("%s:%d handle %s<%p> not found", func
, ln
, name
, h
);
98 hl
->next
->inherited
= inh
;
99 debug_printf ("%s:%d set handle %s<%p> inheritance flag to %d", func
, ln
,
103 /* Add a handle to the linked list of known handles. */
105 add_handle (const char *func
, int ln
, HANDLE h
, const char *name
, bool inh
)
113 if ((hl
= find_handle (h
)))
116 if (hl
->name
== name
&& hl
->func
== func
&& hl
->ln
== ln
)
118 system_printf ("%s:%d - multiple attempts to add handle %s<%p>", func
,
120 system_printf (" previously allocated by %s:%d(%s<%p>) winpid %d",
121 hl
->func
, hl
->ln
, hl
->name
, hl
->h
, hl
->pid
);
125 if ((hl
= newh ()) == NULL
)
127 debug_printf ("couldn't allocate memory for %s(%d): %s(%p)",
136 hl
->pid
= GetCurrentProcessId ();
137 hl
->next
= cygheap
->debug
.starth
.next
;
138 cygheap
->debug
.starth
.next
= hl
;
139 SetHandleInformation (h
, HANDLE_FLAG_PROTECT_FROM_CLOSE
, HANDLE_FLAG_PROTECT_FROM_CLOSE
);
140 debug_printf ("protecting handle '%s'(%p), inherited flag %d", hl
->name
, hl
->h
, hl
->inherited
);
144 delete_handle (handle_list
*hl
)
146 handle_list
*hnuke
= hl
->next
;
147 debug_printf ("nuking handle '%s' (%p)", hnuke
->name
, hnuke
->h
);
148 hl
->next
= hnuke
->next
;
149 memset (hnuke
, 0, sizeof (*hnuke
));
153 debug_fixup_after_fork_exec ()
155 /* No lock needed at this point */
157 for (hl
= &cygheap
->debug
.starth
; hl
->next
!= NULL
; /* nothing */)
158 if (hl
->next
->inherited
)
161 delete_handle (hl
); // removes hl->next
165 mark_closed (const char *func
, int ln
, HANDLE h
, const char *name
, bool force
)
172 if ((hl
= find_handle (h
)) && !force
)
175 system_printf ("attempt to close protected handle %s:%d(%s<%p>) winpid %d",
176 hl
->func
, hl
->ln
, hl
->name
, hl
->h
, hl
->pid
);
177 system_printf (" by %s:%d(%s<%p>)", func
, ln
, name
, h
);
182 if (hl
&& (hln
= hl
->next
) && strcmp (name
, hln
->name
) != 0)
184 system_printf ("closing protected handle %s:%d(%s<%p>)",
185 hln
->func
, hln
->ln
, hln
->name
, hln
->h
);
186 system_printf (" by %s:%d(%s<%p>)", func
, ln
, name
, h
);
195 /* Close a known handle. Complain if !force and closing a known handle or
196 if the name of the handle being closed does not match the registered name. */
198 close_handle (const char *func
, int ln
, HANDLE h
, const char *name
, bool force
)
203 if (!mark_closed (func
, ln
, h
, name
, force
))
206 SetHandleInformation (h
, HANDLE_FLAG_PROTECT_FROM_CLOSE
, 0);
207 ret
= CloseHandle (h
);
211 system_printf ("CloseHandle(%s<%p>) failed %s:%d, %E", name
, h
, func
, ln
);