summaryrefslogtreecommitdiffstats
path: root/winsup/cygwin/debug.cc
blob: 075803cc00a56df423bbf03b4dfe45551bc4b216 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* debug.cc

   Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
   2008, 2009, 2011
   Red Hat, Inc.

This software is a copyrighted work licensed under the terms of the
Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
details. */

#include "winsup.h"
#include "cygerrno.h"
#ifdef DEBUGGING
#include "path.h"
#include "fhandler.h"
#include "dtable.h"
#include "cygheap.h"
#endif

#undef CloseHandle

#ifdef DEBUGGING
/* Here lies extra debugging routines which help track down internal
   Cygwin problems when compiled with -DDEBUGGING . */
#define NFREEH (sizeof (cygheap->debug.freeh) / sizeof (cygheap->debug.freeh[0]))

class lock_debug
{
  static muto locker;
 public:
  lock_debug ()
  {
    locker.acquire (INFINITE);
  }
  void unlock ()
  {
    locker.release ();
  }
  ~lock_debug () {unlock ();}
  friend void debug_init ();
};

muto NO_COPY lock_debug::locker;

static bool __stdcall mark_closed (const char *, int, HANDLE, const char *, bool);

void
debug_init ()
{
  lock_debug::locker.init ("debug_lock");
}

/* Find a registered handle in the linked list of handles. */
static handle_list * __stdcall
find_handle (HANDLE h)
{
  handle_list *hl;
  for (hl = &cygheap->debug.starth; hl->next != NULL; hl = hl->next)
    if (hl->next->h == h)
      goto out;
  hl = NULL;

out:
  return hl;
}

void
verify_handle (const char *func, int ln, HANDLE h)
{
  lock_debug here;
  handle_list *hl = find_handle (h);
  if (!hl)
    return;
  system_printf ("%s:%d - multiple attempts to add handle %p", func, ln, h);

  system_printf (" previously allocated by %s:%d(%s<%p>) winpid %d",
		 hl->func, hl->ln, hl->name, hl->h, hl->pid);
}

void
setclexec (HANDLE oh, HANDLE nh, bool not_inheriting)
{
  lock_debug here;
  handle_list *hl = find_handle (oh);
  if (hl)
    {
      hl = hl->next;
      hl->inherited = !not_inheriting;
      hl->h = nh;
    }
}

/* Create a new handle record */
static handle_list * __stdcall
newh ()
{
  handle_list *hl;

  for (hl = cygheap->debug.freeh; hl < cygheap->debug.freeh + NFREEH; hl++)
    if (hl->name == NULL)
      return hl;

  return NULL;
}

void __reg3
modify_handle (const char *func, int ln, HANDLE h, const char *name, bool inh)
{
  lock_debug here;
  handle_list *hl = find_handle (h);
  if (!hl)
    {
      system_printf ("%s:%d handle %s<%p> not found", func, ln, name, h);
      return;
    }
  hl->next->inherited = inh;
  debug_printf ("%s:%d set handle %s<%p> inheritance flag to %d", func, ln,
		name, h, inh);
}

/* Add a handle to the linked list of known handles. */
void __reg3
add_handle (const char *func, int ln, HANDLE h, const char *name, bool inh)
{
  handle_list *hl;

  if (!cygheap)
    return;

  lock_debug here;
  if ((hl = find_handle (h)))
    {
      hl = hl->next;
      if (hl->name == name && hl->func == func && hl->ln == ln)
	return;
      system_printf ("%s:%d - multiple attempts to add handle %s<%p>", func,
		     ln, name, h);
      system_printf (" previously allocated by %s:%d(%s<%p>) winpid %d",
		     hl->func, hl->ln, hl->name, hl->h, hl->pid);
      return;
    }

  if ((hl = newh ()) == NULL)
    {
      here.unlock ();
      debug_printf ("couldn't allocate memory for %s(%d): %s(%p)",
		    func, ln, name, h);
      return;
    }
  hl->h = h;
  hl->name = name;
  hl->func = func;
  hl->ln = ln;
  hl->inherited = inh;
  hl->pid = GetCurrentProcessId ();
  hl->next = cygheap->debug.starth.next;
  cygheap->debug.starth.next = hl;
  SetHandleInformation (h, HANDLE_FLAG_PROTECT_FROM_CLOSE, HANDLE_FLAG_PROTECT_FROM_CLOSE);
  debug_printf ("protecting handle '%s'(%p), inherited flag %d", hl->name, hl->h, hl->inherited);
}

static void __stdcall
delete_handle (handle_list *hl)
{
  handle_list *hnuke = hl->next;
  debug_printf ("nuking handle '%s' (%p)", hnuke->name, hnuke->h);
  hl->next = hnuke->next;
  memset (hnuke, 0, sizeof (*hnuke));
}

void
debug_fixup_after_fork_exec ()
{
  /* No lock needed at this point */
  handle_list *hl;
  for (hl = &cygheap->debug.starth; hl->next != NULL; /* nothing */)
    if (hl->next->inherited)
      hl = hl->next;
    else
      delete_handle (hl);	// removes hl->next
}

static bool __stdcall
mark_closed (const char *func, int ln, HANDLE h, const char *name, bool force)
{
  handle_list *hl;

  if (!cygheap)
    return true;

  if ((hl = find_handle (h)) && !force)
    {
      hl = hl->next;
      system_printf ("attempt to close protected handle %s:%d(%s<%p>) winpid %d",
		     hl->func, hl->ln, hl->name, hl->h, hl->pid);
      system_printf (" by %s:%d(%s<%p>)", func, ln, name, h);
      return false;
    }

  handle_list *hln;
  if (hl && (hln = hl->next) && strcmp (name, hln->name) != 0)
    {
      system_printf ("closing protected handle %s:%d(%s<%p>)",
		     hln->func, hln->ln, hln->name, hln->h);
      system_printf (" by %s:%d(%s<%p>)", func, ln, name, h);
    }

  if (hl)
    delete_handle (hl);

  return true;
}

/* Close a known handle.  Complain if !force and closing a known handle or
   if the name of the handle being closed does not match the registered name. */
bool __reg3
close_handle (const char *func, int ln, HANDLE h, const char *name, bool force)
{
  bool ret;

  lock_debug here;
  if (!mark_closed (func, ln, h, name, force))
    return false;

  SetHandleInformation (h, HANDLE_FLAG_PROTECT_FROM_CLOSE, 0);
  ret = CloseHandle (h);

  if (!ret)
    {
      system_printf ("CloseHandle(%s<%p>) failed %s:%d, %E", name, h, func, ln);
      try_to_debug ();
    }
  return ret;
}
#endif /*DEBUGGING*/