newlib/winsup/cygwin/debug.cc
Christopher Faylor 92ddb74290 * cygheap.cc (cygheap::close_ctty): Close ctty via close_with_arch().
* debug.cc (close_handle): Call debugger on failure.
* devices.in (device::tty_to_real_device): Delete.
* devices.h (device::tty_to_real_device): Ditto.
* devices.cc: Regenerate.
* dtable.cc: Delete old ifdef'ed vfork code.
(dtable::release): Don't handle archetype here.
(dtable::init_std_file_from_handle): Consolidate console tests.  Generate
major/minor for tty ASAP.  Fix incorrect setting of DEV_TTYS* for serial.
(fh_alloc): New function derived from build_fh_pc.  Pass current tty when
building tty.
(build_pc_pc): Use fh_alloc to create.  Set name from fh->dev if appropriate.
Generate an archetype or point to one here.
(dtable::dup_worker): Deal with archetypes.  Rely on = operator copying whole
class rather than just fhandler_base.
(dtable::fixup_after_exec): Call close_with_arch to handle closing of fhandlers
with archetypes.
* fhandler.cc (fhandler_base::operator =): Call memcpy with fhandler's size()
rather than sizeof fhandler_base.
(fhandler_base::open_with_arch): New function.  Handles opening of fhandler's
with archetypes, dealing with usecounts, etc.
(fhandler_base::close_with_arch): Ditto for close.
* fhandler.h: Many changes for archetypes.
(fhandler_base::set_name): Set both normalized path and regular path.
(fhandler_base::open_with_arch): New function.
(fhandler_base::open_setup): Ditto.
(fhandler_base::use_archetype): Ditto.
(fhandler_base::_archetype_usecount): Ditto.
(fhandler_*::size): Ditto.
(fhandler_dev_tape::open): Remove virtual decoration.
(fhandler_console::use_archetype): New function.  Return true.
(fhandler_console::open_setup): New function.
(fhandler_console::dup): Delete.
(fhandler_tty_slave::fhandler_tty_slave): Redeclare to take an argument.
(fhandler_tty_slave::use_archetype): New function.  Return true.
(fhandler_tty_slave::cleanup): New function.
(fhandler_pty_master::use_archetype): New function.  Return true.
(fhandler_pty_master::cleanup): New function.
(fhandler_pty_master::is_tty_master): New function.  Return false.
(fhandler_tty_master::is_tty_master): New function.  Return true.
(fhandler_dev_dsp::fhandler_dev_dsp): New function.  Return true.
(report_tty_counts): Only report on archetype's usecount if there is one.
* fhandler_console.cc (fhandler_console::get_tty_stuff): Remove handling of
setsid, set_ctty, set_flags, and manage_console_count.
(fhandler_console::open_setup): New function.  Implement functionality removed
from get_tty_stuff.
(fhandler_console::dup): Delete.
(fhandler_console::output_tcsetattr): Set errno on error.
(fhandler_console::fhandler_console): Set device early.
(fhandler_console::init): Use open_with_arch to open console handles.
(fhandler_console::fixup_after_fork_exec): Nuke most of the stuff for dealing
with console handles.
* fhandler_dsp.cc (fhandler_dev_dsp::open): Remove archetype handling.
(fhandler_dev_dsp::write): Ditto.
(fhandler_dev_dsp::read): Ditto.
(fhandler_dev_dsp::close): Ditto.
(fhandler_dev_dsp::dup): Ditto.
(fhandler_dev_dsp::ioctl): Ditto.
(fhandler_dev_dsp::fixup_after_fork): Ditto.
(fhandler_dev_dsp::fixup_after_exec): Ditto.
* fhandler_tty.cc (fhandler_tty_common::__acquire_output_mutex): Add a little
more debugging.
(fhandler_tty_common::__release_output_mutex): Ditto.
(fhandler_pty_master::process_slave_output): Ditto.  Don't do signal handling
or pthread_cancel handling in the tty master thread.
(process_output): Minor reorg.
(fhandler_tty_slave::fhandler_tty_slave): Set device based on new ntty
argument.
(fhandler_tty_slave::open): Remove archetype handling.  Move some processing
into open_setup().
(fhandler_tty_slave::open_setup): New function.
(fhandler_tty_slave::cleanup): New function.
(fhandler_tty_slave::close): Remove archetype handling.  Move some processing
into cleanup().
(fhandler_tty_slave::init): Rename argument from f to h.  Open device using
open_with_arch().  Remove archetype handling.
(fhandler_pty_master::dup): Ditto.
(fhandler_pty_master::open): Ditto.
(fhandler_pty_master::close): Ditto.  Move some handling to cleanup().
(fhandler_pty_master::cleanup): New function.
(fhandler_tty_master::init_console): Give unique name to captive console
fhandler.
* pinfo.cc (_pinfo::set_ctty): Rename argument from arch to fh.  Eliminate
archetype assumption.
* syscalls.cc (close_all_files): Use close_with_arch for closing.
(open): Use open_with_arch() rather than open().
(close): Use close_with_arch() rather than close().
2011-05-05 22:30:53 +00:00

236 lines
5.4 KiB
C++

/* debug.cc
Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
2008, 2009
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 __stdcall
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 __stdcall
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 __stdcall
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*/