newlib/winsup/cygwin/debug.cc
Christopher Faylor 8cb359d947 * child_info.h (child_info_spawn::hexec_proc): Eliminate.
* dcrt0.cc (dll_crt0_0): Remove hexec_proc stuff.
* fork.cc (fork_child): Remove call to pinfo_fixup_after_fork.
* pinfo.cc (set_myself): Close and zero pid_handle if set.
(pinfo_fixup_after_fork): Delete.
(proc_waiter): Don't close vchild.hProcess here.  Do that when we are remove
the vchild from procs.  Save hProcess as pid_handle only on first reparent
operation.
(pinfo::wait): Don't set pid_handle here.
(pinfo::alert_parent): Always try to send signal.  If unsuccessful then close
and zero wr_proc_pipe.
* pinfo.h (pinfo::pinfo): Make sure that appropriate parts of the class are
zeroed on construction.
(pinfo::alert_parent): Take char argument.
(pinfo_fixup_after_fork): Delete declaration.
(hexec_proc): Ditto.
* sigproc.cc (remove_proc): Close pid_handle and hProcess if appropriate.
* spawn.cc (spawn_guts): Set cygheap->pid_handle on first exec.
* cygheap.h (init_cygheap::pid_handle): New element.
* pinfo.cc (set_myself): Clear previously existing cygheap->pid_handle when a
new process has been started.
(pinfo::wait): Make sure that a handle to the newly forked/spawned process is
kept around so that the pid will not be reused.
* pinfo.h (_pinfo::pid_handle): Move.
(pinfo::pid_handle): to here.
* spawn.cc (spawn_guts): Create a pid_handle in cygheap prior to spawning to
ensure that the pid does not get reused during the lifetime of the "cygwin
pid".
* pinfo.h (pinfo::alert_parent): New function.
* exceptions.cc (sig_handle_tty_stop): Use alert_parent to send "signals" to
parent.
* fork.cc (fork_parent): Don't close pi.hProcess.  Let the waiter thread do
that.
* pinfo.cc (proc_waiter): Detect case where process exits without setting the
exit code and use value from GetExitCodeProcess.  Reluctantly implement
__SIGREPARENT.
(pinfo::alert_parent): Define.
* sigproc.h (__SIGREPARENT): New enum.
* spawn.cc (spawn_guts): Send reparent signal to parent on exec.  Always create
process in suspended state to avoid races.  Remove cygthread.h in favor of
cygtls.h throughout since cygtls now includes cygthread.h.  Eliminate
ppid_handle usage throughout.
* child_info.h: Regenerate magic number
(child_info): Remove pppid_handle.
* cygthread.h (cygthread::release): New method.  Frees thread without waiting.
* cygthread.cc (cygthread::stub): Set _ctinfo in _mytls to point to information
for executing thread.  Don't call SetEvent if thread is no longer in use.
(cygthread::simplestub): Ditto.
* cygtls.h (_cygtls::_ctinfo): New element contains pointer to information
about executing cygthread, if any.
* dcrt0.cc: Remove last vestiges of per_thread stuff.
(dll_crt0_0): Ditto.  Remove accommodation for ppid_handle.
(do_exit): Remove obsolete reparenting test.
(_exit): Exit with a more SUSv3-like exit value.
* dtable.cc (dtable::stdio_init): Check for myself->cygstarted rather than
myself->ppid_handle to see if we were started by a cygwin process.
* exceptions.cc (open_stackdumpfile): Ditto.
(handle_exceptions): Ditto.
(ctrl_c_handler): Ditto.
(sig_handle_tty_stop): Ditto.  Let parent send signal to itself on STOP.
(sigpacket::process): Comment out vfork test.
(signal_exit): Use more SUSv3-like exit value on signal.
* external.cc (fillout_pinfo): Don't set hProcess.
* fork.cc: Remove VFORK cruft.
(per_thread::set): Delete.
(fork_child): Remove perthread stuff.
(fork_parent): Remove obsolete subproc_init.  Accommodate new method for
tracking subprocesses.
* pinfo.cc (set_myself): Accommodate new pinfo/_pinfo layout.  Set some things
here that used to be set in wait_sig.
(_pinfo::exit): Set exitcode here.  Close process pipe.
(_pinfo::commune_send): Accommodeate new pinfo/_pinfo layout.
(proc_waiter): New function.  Waits, in a thread for subprocess to go away.
(pinfo::wait): New function.  Initialization for proc_waiter.
* pinfo.h (_pinfo::exitcode): New element.
(_pinfo::cygstarted): Ditto.
(_pinfo::wr_proc_pipe): Ditto.
(_pinfo::ppid_handle): Delete.
(_pinfo::hProcess): Delete.
(_pinfo::lock): Delete.
(pinfo::hProcess): New element.
(pinfo::lock): Ditto.
(pinfo::wait): Declare new function.
(pinfo::preserve): Define new function.
* sigproc.cc: Remove old stuff from wait_subproc thread based method.
(zombies): Remove.
(procs): New.
(my_parent_is_alive): Just check that the parent pid exists.
(mychild): Just use pinfo methods to determine if child is mine.
(proc_subproc): Revamp PROC_ADDCHILD to use pinfo::wait.  Remove
PROC_CHILDTERMINATED logic.  Use different method to remove processes from list
when SIGCHLD == SIG_IGN.
(proc_terminate): Gut.
(subproc_init): Delete.
(init_child_info): Remove setting of pppid_handle.
(checkstate): Revamp to only scan procs array.
(remove_proc): Rename from remove_zombie.  Don't close hProcess or pid_handle.
Don't release memory if it's myself.
(stopped_or_terminated): Change logic to handle new consolidated proc/zombie
array.
(wait_subproc): Delete.
* sigproc.h: Remove obsolete EXIT_* defines.
(subproc_init): Remove declaration.
* spawn.cc (spawn_guts): Remove reparenting stuff.  Use standard wait logic to
wait for child if started from a non-cygwin process.
* tlsoffsets.h: Regenerate.
* tty.cc (tty_init): Check for myself->cygstarted rather than
myself->ppid_handle to see if we were started by a cygwin process.
* include/sys/signal.h (external_pinfo::exitcode): Replace hProcess.
* include/sys/wait.h (WCOREDUMP): Define.
* fhandler_tty.cc (fhandler_tty_slave::read): Add debugging output for timeout
case.
* signal.cc (abort): Flag that we are exiting with the ABORT signal.
2004-11-26 04:15:10 +00:00

243 lines
5.4 KiB
C++

/* debug.cc
Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004 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 <malloc.h>
#include "sync.h"
#include "sigproc.h"
#include "pinfo.h"
#include "perprocess.h"
#include "security.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 . */
#include <stdlib.h>
#define NFREEH (sizeof (cygheap->debug.freeh) / sizeof (cygheap->debug.freeh[0]))
class lock_debug
{
static muto *locker;
bool acquired;
public:
lock_debug () : acquired (0)
{
if (locker && !exit_state)
acquired = !!locker->acquire (INFINITE);
}
void unlock ()
{
if (locker && acquired)
{
locker->release ();
acquired = false;
}
}
~lock_debug () {unlock ();}
friend void debug_init ();
};
muto NO_COPY *lock_debug::locker = NULL;
static bool __stdcall mark_closed (const char *, int, HANDLE, const char *, bool);
void
debug_init ()
{
muto *debug_lock_muto;
lock_debug::locker = new_muto (debug_lock_muto);
}
/* 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;
cygheap->debug.endh = hl;
hl = NULL;
out:
return hl;
}
void
verify_handle (const char *func, int ln, HANDLE h)
{
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)
{
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;
lock_debug here;
for (hl = cygheap->debug.freeh; hl < cygheap->debug.freeh + NFREEH; hl++)
if (hl->name == NULL)
return hl;
return NULL;
}
/* 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;
lock_debug here;
if (!cygheap)
return;
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->next = NULL;
hl->inherited = inh;
hl->pid = GetCurrentProcessId ();
cygheap->debug.endh->next = hl;
cygheap->debug.endh = hl;
debug_printf ("protecting handle '%s', inherited flag %d", hl->name, hl->inherited);
return;
}
static void __stdcall
delete_handle (handle_list *hl)
{
handle_list *hnuke = hl->next;
debug_printf ("nuking handle '%s'", hnuke->name);
hl->next = hl->next->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;
lock_debug here;
if (!cygheap)
return true;
if ((hl = find_handle (h)) && !force)
{
hl = hl->next;
here.unlock (); // race here
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))
{
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;
ret = CloseHandle (h);
#if 0 /* Uncomment to see CloseHandle failures */
if (!ret)
small_printf ("CloseHandle(%s) failed %s:%d\n", name, func, ln);
#endif
return ret;
}
int __stdcall
__set_errno (const char *func, int ln, int val)
{
debug_printf ("%s:%d val %d", func, ln, val);
_impure_ptr->_errno = val;
return errno = val;
}
#endif /*DEBUGGING*/