OSDN Git Service

4f1d6dfa34320973ed4c6c1e7ad110c65e80fff2
[pf3gnuchains/sourceware.git] / winsup / cygwin / debug.cc
1 /* debug.cc
2
3    Copyright 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
4
5 This software is a copyrighted work licensed under the terms of the
6 Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
7 details. */
8
9 #include "winsup.h"
10 #include "sync.h"
11 #include "sigproc.h"
12 #include "pinfo.h"
13 #include "perthread.h"
14 #include "perprocess.h"
15 #include "security.h"
16 #include "cygerrno.h"
17 #ifdef DEBUGGING
18 #include "path.h"
19 #include "fhandler.h"
20 #include "dtable.h"
21 #include "cygheap.h"
22 #endif
23
24 #undef CloseHandle
25
26 #ifdef DEBUGGING
27 /* Here lies extra debugging routines which help track down internal
28    Cygwin problems when compiled with -DDEBUGGING . */
29 #include <stdlib.h>
30 #define NFREEH (sizeof (cygheap->debug.freeh) / sizeof (cygheap->debug.freeh[0]))
31
32 class lock_debug
33 {
34   static muto *locker;
35   bool acquired;
36  public:
37   lock_debug () : acquired (0)
38   {
39     if (locker && !exit_state)
40       acquired = !!locker->acquire (INFINITE);
41   }
42   void unlock ()
43   {
44     if (locker && acquired)
45       {
46         locker->release ();
47         acquired = false;
48       }
49   }
50   ~lock_debug () {unlock ();}
51   friend void debug_init ();
52 };
53
54 muto NO_COPY *lock_debug::locker = NULL;
55
56 static bool __stdcall mark_closed (const char *, int, HANDLE, const char *, bool);
57
58 void
59 debug_init ()
60 {
61   muto *debug_lock_muto;
62   lock_debug::locker = new_muto (debug_lock_muto);
63 }
64
65 /* Find a registered handle in the linked list of handles. */
66 static handle_list * __stdcall
67 find_handle (HANDLE h)
68 {
69   handle_list *hl;
70   for (hl = &cygheap->debug.starth; hl->next != NULL; hl = hl->next)
71     if (hl->next->h == h)
72       goto out;
73   cygheap->debug.endh = hl;
74   hl = NULL;
75
76 out:
77   return hl;
78 }
79
80 #ifdef DEBUGGING_AND_FDS_PROTECTED
81 void
82 setclexec (HANDLE oh, HANDLE nh, bool not_inheriting)
83 {
84   handle_list *hl = find_handle (oh);
85   if (hl)
86     {
87       hl = hl->next;
88       hl->inherited = !not_inheriting;
89       hl->h = nh;
90     }
91 }
92 #endif
93
94 /* Create a new handle record */
95 static handle_list * __stdcall
96 newh ()
97 {
98   handle_list *hl;
99   lock_debug here;
100
101   for (hl = cygheap->debug.freeh; hl < cygheap->debug.freeh + NFREEH; hl++)
102     if (hl->name == NULL)
103       return hl;
104
105   return NULL;
106 }
107
108 /* Add a handle to the linked list of known handles. */
109 void __stdcall
110 add_handle (const char *func, int ln, HANDLE h, const char *name, bool inh)
111 {
112   handle_list *hl;
113   lock_debug here;
114
115   if ((hl = find_handle (h)))
116     {
117       hl = hl->next;
118       if (hl->name == name && hl->func == func && hl->ln == ln)
119         return;
120       system_printf ("%s:%d - multiple attempts to add handle %s<%p>", func,
121                      ln, name, h);
122       system_printf (" previously allocated by %s:%d(%s<%p>) winpid %d",
123                      hl->func, hl->ln, hl->name, hl->h, hl->pid);
124       return;
125     }
126
127   if ((hl = newh ()) == NULL)
128     {
129       here.unlock ();
130       debug_printf ("couldn't allocate memory for %s(%d): %s(%p)",
131                     func, ln, name, h);
132       return;
133     }
134   hl->h = h;
135   hl->name = name;
136   hl->func = func;
137   hl->ln = ln;
138   hl->next = NULL;
139   hl->inherited = inh;
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);
144
145   return;
146 }
147
148 static void __stdcall
149 delete_handle (handle_list *hl)
150 {
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));
155 }
156
157 void
158 debug_fixup_after_fork_exec ()
159 {
160   /* No lock needed at this point */
161   handle_list *hl;
162   for (hl = &cygheap->debug.starth; hl->next != NULL; /* nothing */)
163     if (hl->next->inherited)
164       hl = hl->next;
165     else
166       delete_handle (hl);       // removes hl->next
167 }
168
169 static bool __stdcall
170 mark_closed (const char *func, int ln, HANDLE h, const char *name, bool force)
171 {
172   handle_list *hl;
173   lock_debug here;
174
175   if ((hl = find_handle (h)) && !force)
176     {
177       hl = hl->next;
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);
182       return false;
183     }
184
185   handle_list *hln;
186   if (hl && (hln = hl->next) && strcmp (name, hln->name))
187     {
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);
191     }
192
193   if (hl)
194     delete_handle (hl);
195
196   return true;
197 }
198
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. */
201 bool __stdcall
202 close_handle (const char *func, int ln, HANDLE h, const char *name, bool force)
203 {
204   bool ret;
205   lock_debug here;
206
207   if (!mark_closed (func, ln, h, name, force))
208     return false;
209
210   ret = CloseHandle (h);
211
212 #if 0 /* Uncomment to see CloseHandle failures */
213   if (!ret)
214     small_printf ("CloseHandle(%s) failed %s:%d\n", name, func, ln);
215 #endif
216   return ret;
217 }
218
219 int __stdcall
220 __set_errno (const char *func, int ln, int val)
221 {
222   debug_printf ("%s:%d val %d", func, ln, val);
223   return errno = val;
224 }
225 #endif /*DEBUGGING*/