From ef8bff85be3057385b34a4c0f8ba2b274041ffcc Mon Sep 17 00:00:00 2001 From: Christopher Faylor Date: Thu, 24 Nov 2011 21:36:53 +0000 Subject: [PATCH] * cygthread.h (cygthread::name): Default name to "main" if we are early in the process of setting up the DLL and no name is known. * dcrt0.cc (initial_env): Remove CYGWIN_SLEEP stuff. (get_cygwin_startup_info): Activate strace here as appropriate. (dll_crt0_0): Move get_cygwin_startup_info as early as possible to avoid missing strace output. * fork.cc (frok::child): Move debugging statement to point where ppid will be set. * pinfo.cc (pinfo::thisproc): Remove obsolete call to strace.hello. Tweak debug output slightly. * select.cc (select_stuff::wait): Allow APCS to be triggered while waiting since we use them now. Report when that happens. * sigproc.cc (child_info::child_info): Use strace.active() rather than strace.attached(). * spawn.cc (child_info_spawn::worker): Only write strace child pid when we know it's a cygwin process. Accommodate change to write_child argument list. * strace.cc (strace::hello): Delete. Move functionality... (strace::activate): ...to here. (mypid): Just use raw GetCurrentProcessId () if myself isn't set. (strace::write_childpid): Don't wait for subproc_ready. Remove arg which was required for it. * include/sys/strace.h (strace::hello): Delete. (strace::write_childpid): Delete first argument. --- winsup/cygwin/ChangeLog | 27 +++++++++++++++++ winsup/cygwin/dcrt0.cc | 22 +++++--------- winsup/cygwin/fork.cc | 7 +++-- winsup/cygwin/include/sys/strace.h | 3 +- winsup/cygwin/pinfo.cc | 3 +- winsup/cygwin/select.cc | 8 +++-- winsup/cygwin/sigproc.cc | 3 +- winsup/cygwin/spawn.cc | 7 +++-- winsup/cygwin/strace.cc | 48 +++++++++++++++--------------- 9 files changed, 75 insertions(+), 53 deletions(-) diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog index 38a950edf..ff5054f95 100644 --- a/winsup/cygwin/ChangeLog +++ b/winsup/cygwin/ChangeLog @@ -1,3 +1,30 @@ +2011-11-24 Christopher Faylor + + * cygthread.h (cygthread::name): Default name to "main" if we are early + in the process of setting up the DLL and no name is known. + * dcrt0.cc (initial_env): Remove CYGWIN_SLEEP stuff. + (get_cygwin_startup_info): Activate strace here as appropriate. + (dll_crt0_0): Move get_cygwin_startup_info as early as possible to + avoid missing strace output. + * fork.cc (frok::child): Move debugging statement to point where ppid + will be set. + * pinfo.cc (pinfo::thisproc): Remove obsolete call to strace.hello. + Tweak debug output slightly. + * select.cc (select_stuff::wait): Allow APCS to be triggered while + waiting since we use them now. Report when that happens. + * sigproc.cc (child_info::child_info): Use strace.active() rather than + strace.attached(). + * spawn.cc (child_info_spawn::worker): Only write strace child pid + when we know it's a cygwin process. Accommodate change to write_child + argument list. + * strace.cc (strace::hello): Delete. Move functionality... + (strace::activate): ...to here. + (mypid): Just use raw GetCurrentProcessId () if myself isn't set. + (strace::write_childpid): Don't wait for subproc_ready. Remove arg + which was required for it. + * include/sys/strace.h (strace::hello): Delete. + (strace::write_childpid): Delete first argument. + 2011-11-23 Christopher Faylor * child_info.h (CURR_CHILD_INFO_MAGIC): Reset for previous changes. diff --git a/winsup/cygwin/dcrt0.cc b/winsup/cygwin/dcrt0.cc index 40c893538..6c13eff95 100644 --- a/winsup/cygwin/dcrt0.cc +++ b/winsup/cygwin/dcrt0.cc @@ -474,17 +474,8 @@ initial_env () _cygwin_testing = 1; #ifdef DEBUGGING - char buf[NT_MAX_PATH]; DWORD len; - - if (GetEnvironmentVariableA ("CYGWIN_SLEEP", buf, sizeof (buf) - 1)) - { - DWORD ms = atoi (buf); - console_printf ("Sleeping %d, pid %u %P\n", ms, GetCurrentProcessId ()); - Sleep (ms); - if (!strace.active () && !dynamically_loaded) - strace.hello (); - } + char buf[NT_MAX_PATH]; if (GetEnvironmentVariableA ("CYGWIN_DEBUG", buf, sizeof (buf) - 1)) { char buf1[NT_MAX_PATH]; @@ -506,7 +497,6 @@ initial_env () } } #endif - } child_info * @@ -519,7 +509,10 @@ get_cygwin_startup_info () if (si.cbReserved2 < EXEC_MAGIC_SIZE || !res || res->intro != PROC_MAGIC_GENERIC || res->magic != CHILD_INFO_MAGIC) - res = NULL; + { + strace.activate (false); + res = NULL; + } else { if ((res->intro & OPROC_MAGIC_MASK) == OPROC_MAGIC_GENERIC) @@ -545,8 +538,7 @@ get_cygwin_startup_info () multiple_cygwin_problem ("fhandler size", res->fhandler_union_cb, sizeof (fhandler_union)); if (res->isstraced ()) { - res->ready (false); - for (unsigned i = 0; !being_debugged () && i < 1000000; i++) + while (!being_debugged ()) yield (); strace.activate (res->type == _CH_FORK); } @@ -657,6 +649,7 @@ init_windows_system_directory () void dll_crt0_0 () { + child_proc_info = get_cygwin_startup_info (); init_windows_system_directory (); init_global_security (); initial_env (); @@ -683,7 +676,6 @@ dll_crt0_0 () do_global_ctors (&__CTOR_LIST__, 1); cygthread::init (); - child_proc_info = get_cygwin_startup_info (); if (!child_proc_info) memory_init (true); else diff --git a/winsup/cygwin/fork.cc b/winsup/cygwin/fork.cc index 972e89287..a2728ce72 100644 --- a/winsup/cygwin/fork.cc +++ b/winsup/cygwin/fork.cc @@ -193,8 +193,6 @@ frok::child (volatile char * volatile here) extern void fixup_lockf_after_fork (); extern void fixup_hooks_after_fork (); extern void fixup_timers_after_fork (); - debug_printf ("child is running. pid %d, ppid %d, stack here %p", - myself->pid, myself->ppid, __builtin_frame_address (0)); /* NOTE: Logically this belongs in dll_list::load_after_fork, but by doing it here, before the first sync_with_parent, we can exploit @@ -203,6 +201,8 @@ frok::child (volatile char * volatile here) dlls.reserve_space (); sync_with_parent ("after longjmp", true); + debug_printf ("child is running. pid %d, ppid %d, stack here %p", + myself->pid, myself->ppid, __builtin_frame_address (0)); sigproc_printf ("hParent %p, load_dlls %d", hParent, load_dlls); /* If we've played with the stack, stacksize != 0. That means that @@ -442,7 +442,7 @@ frok::parent (volatile char * volatile stack_here) be called in subproc handling. */ ProtectHandle1 (hchild, childhProc); - strace.write_childpid (ch, pi.dwProcessId); + strace.write_childpid (pi.dwProcessId); /* Wait for subproc to initialize itself. */ if (!ch.sync (pi.dwProcessId, hchild, FORK_WAIT_TIMEOUT)) @@ -503,6 +503,7 @@ frok::parent (volatile char * volatile stack_here) /* CHILD IS STOPPED */ debug_printf ("child is alive (but stopped)"); + /* Initialize, in order: stack, dll data, dll bss. data, bss, heap were done earlier (in dcrt0.cc) Note: variables marked as NO_COPY will not be copied since they are diff --git a/winsup/cygwin/include/sys/strace.h b/winsup/cygwin/include/sys/strace.h index 5b1ccb5b2..cfe818bc3 100644 --- a/winsup/cygwin/include/sys/strace.h +++ b/winsup/cygwin/include/sys/strace.h @@ -45,12 +45,11 @@ public: int version; int lmicrosec; bool execing; - void hello () __attribute__ ((regparm (1))); void dll_info () __attribute__ ((regparm (1))); void prntf (unsigned, const char *func, const char *, ...) /*__attribute__ ((regparm(3)))*/; void vprntf (unsigned, const char *func, const char *, va_list ap) /*__attribute__ ((regparm(3)))*/; void wm (int message, int word, int lon) __attribute__ ((regparm(3))); - void write_childpid (child_info&, unsigned long) __attribute__ ((regparm (3))); + void write_childpid (unsigned long) __attribute__ ((regparm (3))); bool attached () const {return _active == 3;} bool active () const {return _active & 1;} unsigned char& active_val () {return _active;} diff --git a/winsup/cygwin/pinfo.cc b/winsup/cygwin/pinfo.cc index 8c44fc5ad..48a5a99b2 100644 --- a/winsup/cygwin/pinfo.cc +++ b/winsup/cygwin/pinfo.cc @@ -64,8 +64,7 @@ pinfo::thisproc (HANDLE h) procinfo->dwProcessId = myself_initial.pid; procinfo->sendsig = myself_initial.sendsig; wcscpy (procinfo->progname, myself_initial.progname); - strace.hello (); - debug_printf ("myself->dwProcessId %u", procinfo->dwProcessId); + debug_printf ("myself dwProcessId %u", procinfo->dwProcessId); if (h) { /* here if execed */ diff --git a/winsup/cygwin/select.cc b/winsup/cygwin/select.cc index 465b4eba0..c3d3c950d 100644 --- a/winsup/cygwin/select.cc +++ b/winsup/cygwin/select.cc @@ -309,7 +309,7 @@ select_stuff::wait (fd_set *readfds, fd_set *writefds, fd_set *exceptfds, for (;;) { if (!windows_used) - wait_ret = WaitForMultipleObjects (m, w4, FALSE, ms); + wait_ret = WaitForMultipleObjectsEx (m, w4, FALSE, ms, true); else /* Using MWMO_INPUTAVAILABLE is the officially supported solution for the problem that the call to PeekMessage disarms the queue state @@ -317,10 +317,14 @@ select_stuff::wait (fd_set *readfds, fd_set *writefds, fd_set *exceptfds, in the queue. */ wait_ret = MsgWaitForMultipleObjectsEx (m, w4, ms, QS_ALLINPUT | QS_ALLPOSTMESSAGE, - MWMO_INPUTAVAILABLE); + MWMO_INPUTAVAILABLE | MWMO_ALERTABLE); switch (wait_ret) { + case WAIT_IO_COMPLETION: + syscall_printf ("woke due to apc"); + continue; /* Keep going */ + break; case WAIT_OBJECT_0: cleanup (); select_printf ("signal received"); diff --git a/winsup/cygwin/sigproc.cc b/winsup/cygwin/sigproc.cc index 666bdd8c8..d3737e35c 100644 --- a/winsup/cygwin/sigproc.cc +++ b/winsup/cygwin/sigproc.cc @@ -793,7 +793,7 @@ child_info::child_info (unsigned in_cb, child_info_types chtype, fhandler_union_cb = sizeof (fhandler_union); user_h = cygwin_user_h; - if (strace.attached ()) + if (strace.active ()) flag |= _CI_STRACED; if (need_subproc_ready) { @@ -1296,7 +1296,6 @@ wait_sig (VOID *) break; case __SIGSTRACE: strace.activate (false); - strace.hello (); break; case __SIGPENDING: *pack.mask = 0; diff --git a/winsup/cygwin/spawn.cc b/winsup/cygwin/spawn.cc index 688655c8a..6fd7fbe33 100644 --- a/winsup/cygwin/spawn.cc +++ b/winsup/cygwin/spawn.cc @@ -693,8 +693,8 @@ loop: } /* The CREATE_SUSPENDED case is handled below */ - if (!(c_flags & CREATE_SUSPENDED)) - strace.write_childpid (*this, pi.dwProcessId); + if (iscygwin () && !(c_flags & CREATE_SUSPENDED)) + strace.write_childpid (pi.dwProcessId); /* Fixup the parent data structures if needed and resume the child's main thread. */ @@ -787,7 +787,8 @@ loop: if (c_flags & CREATE_SUSPENDED) { ResumeThread (pi.hThread); - strace.write_childpid (*this, pi.dwProcessId); + if (iscygwin ()) + strace.write_childpid (pi.dwProcessId); } ForceCloseHandle (pi.hThread); diff --git a/winsup/cygwin/strace.cc b/winsup/cygwin/strace.cc index 58dbb8887..120ba32c3 100644 --- a/winsup/cygwin/strace.cc +++ b/winsup/cygwin/strace.cc @@ -39,29 +39,30 @@ strace::activate (bool isfork) char buf[30]; __small_sprintf (buf, "cYg%8x %x %d", _STRACE_INTERFACE_ACTIVATE_ADDR, &_active, isfork); OutputDebugString (buf); - } -} - -void -strace::hello () -{ - if (active ()) - { - char pidbuf[80]; - if (myself->progname[0]) - __small_sprintf (pidbuf, "(pid %d, ppid %d, windows pid %u)", myself->pid, - myself->ppid ?: 1, GetCurrentProcessId ()); - else + if (_active) { - GetModuleFileNameW (NULL, myself->progname, sizeof (myself->progname)); - __small_sprintf (pidbuf, "(windows pid %d)", GetCurrentProcessId ()); + char pidbuf[80]; + WCHAR progname_buf[NT_MAX_PATH - 512]; + WCHAR *progname; + if (myself) + { + __small_sprintf (pidbuf, "(pid %d, ppid %d, windows pid %u)", myself->pid, + myself->ppid ?: 1, GetCurrentProcessId ()); + progname = myself->progname; + } + else + { + GetModuleFileNameW (NULL, progname_buf, sizeof (myself->progname)); + __small_sprintf (pidbuf, "(windows pid %u)", GetCurrentProcessId ()); + progname = progname_buf; + } + prntf (1, NULL, "**********************************************"); + prntf (1, NULL, "Program name: %W %s", progname, pidbuf); + prntf (1, NULL, "OS version: Windows %s", wincap.osname ()); + if (cygheap && cygheap->user_heap.chunk) + prntf (1, NULL, "Heap size: %u", cygheap->user_heap.chunk); + prntf (1, NULL, "**********************************************"); } - prntf (1, NULL, "**********************************************"); - prntf (1, NULL, "Program name: %W %s", myself->progname, pidbuf); - prntf (1, NULL, "OS version: Windows %s", wincap.osname ()); - if (cygheap) - prntf (1, NULL, "Heap size: %u", cygheap->user_heap.chunk); - prntf (1, NULL, "**********************************************"); } } @@ -127,7 +128,7 @@ mypid (char *buf) if (myself && myself->pid) __small_sprintf (buf, "%d", myself->pid); else - __small_sprintf (buf, "(%d)", cygwin_pid (GetCurrentProcessId ())); + __small_sprintf (buf, "(%d)", GetCurrentProcessId ()); return buf; } @@ -224,13 +225,12 @@ strace::write (unsigned category, const char *buf, int count) } void -strace::write_childpid (child_info& ch, DWORD pid) +strace::write_childpid (DWORD pid) { char buf[30]; if (!attached () || !being_debugged ()) return; - WaitForSingleObject (ch.subproc_ready, 30000); __small_sprintf (buf, "cYg%8x %x", _STRACE_CHILD_PID, pid); OutputDebugString (buf); }