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