2000-02-18 03:38:33 +08:00
|
|
|
/* debug.cc
|
|
|
|
|
2002-06-05 12:01:43 +08:00
|
|
|
Copyright 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
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"
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "sync.h"
|
|
|
|
#include "sigproc.h"
|
2000-08-12 13:35:42 +08:00
|
|
|
#include "pinfo.h"
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "perthread.h"
|
2000-09-08 10:56:55 +08:00
|
|
|
#include "perprocess.h"
|
|
|
|
#include "security.h"
|
2001-10-16 11:31:50 +08:00
|
|
|
#include "cygerrno.h"
|
2002-07-14 04:00:27 +08:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
#include <errno.h>
|
|
|
|
#include "fhandler.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "dtable.h"
|
|
|
|
#include "cygheap.h"
|
|
|
|
#endif
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-08-25 00:33:03 +08:00
|
|
|
#undef CloseHandle
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
/* Here lies extra debugging routines which help track down internal
|
|
|
|
Cygwin problems when compiled with -DDEBUGGING . */
|
|
|
|
#include <stdlib.h>
|
2002-07-14 04:00:27 +08:00
|
|
|
#define NFREEH (sizeof (cygheap->debug.freeh) / sizeof (cygheap->debug.freeh[0]))
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-07-09 12:37:13 +08:00
|
|
|
class lock_debug
|
2002-02-23 03:33:41 +08:00
|
|
|
{
|
2002-07-09 12:37:13 +08:00
|
|
|
static muto *locker;
|
|
|
|
bool acquired;
|
|
|
|
public:
|
|
|
|
lock_debug () : acquired (0)
|
|
|
|
{
|
|
|
|
if (locker)
|
|
|
|
acquired = !!locker->acquire (INFINITE);
|
|
|
|
}
|
|
|
|
void unlock ()
|
|
|
|
{
|
|
|
|
if (locker && acquired)
|
|
|
|
{
|
|
|
|
locker->release ();
|
|
|
|
acquired = false;
|
|
|
|
}
|
|
|
|
}
|
2002-02-23 03:33:41 +08:00
|
|
|
~lock_debug () {unlock ();}
|
2002-07-09 12:37:13 +08:00
|
|
|
friend void debug_init ();
|
2002-02-23 03:33:41 +08:00
|
|
|
};
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-07-09 12:37:13 +08:00
|
|
|
muto NO_COPY *lock_debug::locker = NULL;
|
|
|
|
|
2001-09-01 13:17:34 +08:00
|
|
|
static bool __stdcall mark_closed (const char *, int, HANDLE, const char *, BOOL);
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
void
|
|
|
|
debug_init ()
|
|
|
|
{
|
2002-07-09 12:37:13 +08:00
|
|
|
muto *debug_lock_muto;
|
|
|
|
lock_debug::locker = new_muto (debug_lock_muto);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Find a registered handle in the linked list of handles. */
|
|
|
|
static handle_list * __stdcall
|
|
|
|
find_handle (HANDLE h)
|
|
|
|
{
|
|
|
|
handle_list *hl;
|
2002-07-14 04:00:27 +08:00
|
|
|
for (hl = &cygheap->debug.starth; hl->next != NULL; hl = hl->next)
|
2000-02-18 03:38:33 +08:00
|
|
|
if (hl->next->h == h)
|
|
|
|
goto out;
|
2002-07-14 04:00:27 +08:00
|
|
|
cygheap->debug.endh = hl;
|
2000-02-18 03:38:33 +08:00
|
|
|
hl = NULL;
|
|
|
|
|
|
|
|
out:
|
|
|
|
return hl;
|
|
|
|
}
|
|
|
|
|
2002-07-15 03:15:32 +08:00
|
|
|
#ifdef DEBUGGING_AND_FDS_PROTECTED
|
2001-09-01 13:17:34 +08:00
|
|
|
void
|
2002-07-15 03:15:32 +08:00
|
|
|
setclexec (HANDLE oh, HANDLE nh, bool not_inheriting)
|
2001-09-01 13:17:34 +08:00
|
|
|
{
|
2001-10-23 05:09:41 +08:00
|
|
|
handle_list *hl = find_handle (oh);
|
2001-09-01 13:17:34 +08:00
|
|
|
if (hl)
|
2001-10-23 05:09:41 +08:00
|
|
|
{
|
2002-07-15 00:16:33 +08:00
|
|
|
hl = hl->next;
|
2002-07-15 03:15:32 +08:00
|
|
|
hl->inherited = !not_inheriting;
|
2001-10-23 05:09:41 +08:00
|
|
|
hl->h = nh;
|
|
|
|
}
|
2001-09-01 13:17:34 +08:00
|
|
|
}
|
2002-07-15 03:15:32 +08:00
|
|
|
#endif
|
2001-09-01 13:17:34 +08:00
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Create a new handle record */
|
|
|
|
static handle_list * __stdcall
|
|
|
|
newh ()
|
|
|
|
{
|
|
|
|
handle_list *hl;
|
2002-02-23 03:33:41 +08:00
|
|
|
lock_debug here;
|
|
|
|
|
2002-07-14 04:00:27 +08:00
|
|
|
for (hl = cygheap->debug.freeh; hl < cygheap->debug.freeh + NFREEH; hl++)
|
2000-02-18 03:38:33 +08:00
|
|
|
if (hl->name == NULL)
|
2002-08-06 11:40:40 +08:00
|
|
|
return hl;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-08-06 11:40:40 +08:00
|
|
|
return NULL;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add a handle to the linked list of known handles. */
|
|
|
|
void __stdcall
|
2002-07-14 04:00:27 +08:00
|
|
|
add_handle (const char *func, int ln, HANDLE h, const char *name, bool inh)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
handle_list *hl;
|
2002-02-23 03:33:41 +08:00
|
|
|
lock_debug here;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-10-17 09:42:04 +08:00
|
|
|
if ((hl = find_handle (h)))
|
|
|
|
{
|
2000-11-06 14:36:32 +08:00
|
|
|
hl = hl->next;
|
2002-07-14 04:00:27 +08:00
|
|
|
if (hl->name == name && hl->func == func && hl->ln == ln)
|
|
|
|
return;
|
2000-10-17 09:42:04 +08:00
|
|
|
system_printf ("%s:%d - multiple attempts to add handle %s<%p>", func,
|
|
|
|
ln, name, h);
|
2002-07-14 04:00:27 +08:00
|
|
|
system_printf (" previously allocated by %s:%d(%s<%p>) winpid %d",
|
|
|
|
hl->func, hl->ln, hl->name, hl->h, hl->pid);
|
2002-02-23 03:33:41 +08:00
|
|
|
return;
|
2000-10-17 09:42:04 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-03-03 11:56:34 +08:00
|
|
|
if ((hl = newh ()) == NULL)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2002-02-23 03:33:41 +08:00
|
|
|
here.unlock ();
|
2002-08-06 11:40:40 +08:00
|
|
|
debug_printf ("couldn't allocate memory for %s(%d): %s(%p)",
|
|
|
|
func, ln, name, h);
|
2000-02-18 03:38:33 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
hl->h = h;
|
|
|
|
hl->name = name;
|
|
|
|
hl->func = func;
|
|
|
|
hl->ln = ln;
|
|
|
|
hl->next = NULL;
|
2002-07-15 03:15:32 +08:00
|
|
|
hl->inherited = inh;
|
2002-07-14 04:00:27 +08:00
|
|
|
hl->pid = GetCurrentProcessId ();
|
|
|
|
cygheap->debug.endh->next = hl;
|
|
|
|
cygheap->debug.endh = hl;
|
2002-07-15 03:15:32 +08:00
|
|
|
debug_printf ("protecting handle '%s', inherited flag %d", hl->name, hl->inherited);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-02-23 03:33:41 +08:00
|
|
|
return;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2001-09-01 13:17:34 +08:00
|
|
|
static void __stdcall
|
|
|
|
delete_handle (handle_list *hl)
|
|
|
|
{
|
|
|
|
handle_list *hnuke = hl->next;
|
2002-07-15 03:15:32 +08:00
|
|
|
debug_printf ("nuking handle '%s'", hnuke->name);
|
2001-09-01 13:17:34 +08:00
|
|
|
hl->next = hl->next->next;
|
2002-08-06 11:40:40 +08:00
|
|
|
memset (hnuke, 0, sizeof (*hnuke));
|
2001-09-01 13:17:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-07-14 04:00:27 +08:00
|
|
|
debug_fixup_after_fork_exec ()
|
2001-09-01 13:17:34 +08:00
|
|
|
{
|
2002-07-09 12:37:13 +08:00
|
|
|
/* No lock needed at this point */
|
2001-09-01 13:17:34 +08:00
|
|
|
handle_list *hl;
|
2002-07-14 04:00:27 +08:00
|
|
|
for (hl = &cygheap->debug.starth; hl->next != NULL; /* nothing */)
|
2002-07-15 03:15:32 +08:00
|
|
|
if (hl->next->inherited)
|
2002-07-14 04:00:27 +08:00
|
|
|
hl = hl->next;
|
|
|
|
else
|
2002-07-15 03:15:32 +08:00
|
|
|
delete_handle (hl); // removes hl->next
|
2001-09-01 13:17:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool __stdcall
|
|
|
|
mark_closed (const char *func, int ln, HANDLE h, const char *name, BOOL force)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
handle_list *hl;
|
2002-02-23 03:33:41 +08:00
|
|
|
lock_debug here;
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
if ((hl = find_handle (h)) && !force)
|
|
|
|
{
|
|
|
|
hl = hl->next;
|
2002-02-23 03:33:41 +08:00
|
|
|
here.unlock (); // race here
|
2002-07-14 04:00:27 +08:00
|
|
|
system_printf ("attempt to close protected handle %s:%d(%s<%p>) winpid %d",
|
|
|
|
hl->func, hl->ln, hl->name, hl->h, hl->pid);
|
2000-02-18 03:38:33 +08:00
|
|
|
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))
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2001-08-23 01:50:22 +08:00
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
if (hl)
|
2001-09-01 13:17:34 +08:00
|
|
|
delete_handle (hl);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-08-23 01:50:22 +08:00
|
|
|
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 __stdcall
|
|
|
|
close_handle (const char *func, int ln, HANDLE h, const char *name, BOOL force)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
2002-02-23 03:33:41 +08:00
|
|
|
lock_debug here;
|
2001-08-23 01:50:22 +08:00
|
|
|
|
2001-09-01 13:17:34 +08:00
|
|
|
if (!mark_closed (func, ln, h, name, force))
|
2001-08-23 01:50:22 +08:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
ret = CloseHandle (h);
|
|
|
|
|
2000-10-13 06:15:47 +08:00
|
|
|
#if 0 /* Uncomment to see CloseHandle failures */
|
|
|
|
if (!ret)
|
|
|
|
small_printf ("CloseHandle(%s) failed %s:%d\n", name, func, ln);
|
|
|
|
#endif
|
2000-02-18 03:38:33 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2001-10-16 11:31:50 +08:00
|
|
|
|
2001-10-23 05:09:41 +08:00
|
|
|
int __stdcall
|
2001-10-16 11:31:50 +08:00
|
|
|
__set_errno (const char *func, int ln, int val)
|
|
|
|
{
|
|
|
|
debug_printf ("%s:%d val %d", func, ln, val);
|
2001-10-23 05:09:41 +08:00
|
|
|
return _impure_ptr->_errno = val;
|
2001-10-16 11:31:50 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
#endif /*DEBUGGING*/
|