19971Syan/* Multi-threaded debugging support for GNU/Linux (LWP layer). 211926Sserb Copyright 2000, 2001, 2002, 2003 Free Software Foundation, Inc. 39971Syan 49971Syan This file is part of GDB. 59971Syan 69971Syan This program is free software; you can redistribute it and/or modify 79971Syan it under the terms of the GNU General Public License as published by 89971Syan the Free Software Foundation; either version 2 of the License, or 99971Syan (at your option) any later version. 109971Syan 119971Syan This program is distributed in the hope that it will be useful, 129971Syan but WITHOUT ANY WARRANTY; without even the implied warranty of 139971Syan MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 149971Syan GNU General Public License for more details. 159971Syan 169971Syan You should have received a copy of the GNU General Public License 179971Syan along with this program; if not, write to the Free Software 189971Syan Foundation, Inc., 59 Temple Place - Suite 330, 199971Syan Boston, MA 02111-1307, USA. */ 209971Syan 219971Syan#include "defs.h" 229971Syan 239971Syan#include "gdb_assert.h" 249971Syan#include "gdb_string.h" 259971Syan#include <errno.h> 269971Syan#include <signal.h> 279971Syan#ifdef HAVE_TKILL_SYSCALL 289971Syan#include <unistd.h> 299971Syan#include <sys/syscall.h> 309971Syan#endif 319971Syan#include <sys/ptrace.h> 3211926Sserb#include "gdb_wait.h" 3311926Sserb 3411127Syan#include "gdbthread.h" 359971Syan#include "inferior.h" 3610154Syan#include "target.h" 379971Syan#include "regcache.h" 389971Syan#include "gdbcmd.h" 399971Syan 409971Syanstatic int debug_lin_lwp; 419971Syanextern char *strsignal (int sig); 429971Syan 439971Syan#include "linux-nat.h" 449971Syan 459971Syan/* On GNU/Linux there are no real LWP's. The closest thing to LWP's 469971Syan are processes sharing the same VM space. A multi-threaded process 479971Syan is basically a group of such processes. However, such a grouping 489971Syan is almost entirely a user-space issue; the kernel doesn't enforce 499971Syan such a grouping at all (this might change in the future). In 509971Syan general, we'll rely on the threads library (i.e. the GNU/Linux 519971Syan Threads library) to provide such a grouping. 529971Syan 539971Syan It is perfectly well possible to write a multi-threaded application 549971Syan without the assistance of a threads library, by using the clone 559971Syan system call directly. This module should be able to give some 569971Syan rudimentary support for debugging such applications if developers 579971Syan specify the CLONE_PTRACE flag in the clone system call, and are 589971Syan using the Linux kernel 2.4 or above. 599971Syan 609971Syan Note that there are some peculiarities in GNU/Linux that affect 619971Syan this code: 629971Syan 639971Syan - In general one should specify the __WCLONE flag to waitpid in 649971Syan order to make it report events for any of the cloned processes 659971Syan (and leave it out for the initial process). However, if a cloned 669971Syan process has exited the exit status is only reported if the 679971Syan __WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but 689971Syan we cannot use it since GDB must work on older systems too. 699971Syan 709971Syan - When a traced, cloned process exits and is waited for by the 719971Syan debugger, the kernel reassigns it to the original parent and 729971Syan keeps it around as a "zombie". Somehow, the GNU/Linux Threads 739971Syan library doesn't notice this, which leads to the "zombie problem": 749971Syan When debugged a multi-threaded process that spawns a lot of 759971Syan threads will run out of processes, even if the threads exit, 769971Syan because the "zombies" stay around. */ 779971Syan 789971Syan/* List of known LWPs. */ 799971Syanstatic struct lwp_info *lwp_list; 809971Syan 819971Syan/* Number of LWPs in the list. */ 829971Syanstatic int num_lwps; 839971Syan 849971Syan/* Non-zero if we're running in "threaded" mode. */ 859971Syanstatic int threaded; 869971Syan 879971Syan 889971Syan#define GET_LWP(ptid) ptid_get_lwp (ptid) 899971Syan#define GET_PID(ptid) ptid_get_pid (ptid) 909971Syan#define is_lwp(ptid) (GET_LWP (ptid) != 0) 919971Syan#define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0) 929971Syan 939971Syan/* If the last reported event was a SIGTRAP, this variable is set to 949971Syan the process id of the LWP/thread that got it. */ 959971Syanptid_t trap_ptid; 969971Syan 979971Syan 989971Syan/* This module's target-specific operations. */ 999971Syanstatic struct target_ops lin_lwp_ops; 1009971Syan 1019971Syan/* The standard child operations. */ 1029971Syanextern struct target_ops child_ops; 1039971Syan 1049971Syan/* Since we cannot wait (in lin_lwp_wait) for the initial process and 1059971Syan any cloned processes with a single call to waitpid, we have to use 1069971Syan the WNOHANG flag and call waitpid in a loop. To optimize 1079971Syan things a bit we use `sigsuspend' to wake us up when a process has 1089971Syan something to report (it will send us a SIGCHLD if it has). To make 1099971Syan this work we have to juggle with the signal mask. We save the 1109971Syan original signal mask such that we can restore it before creating a 11113901Salanb new process in order to avoid blocking certain signals in the 11213901Salanb inferior. We then block SIGCHLD during the waitpid/sigsuspend 1139971Syan loop. */ 1149971Syan 1159971Syan/* Original signal mask. */ 1169971Syanstatic sigset_t normal_mask; 1179971Syan 1189971Syan/* Signal mask for use with sigsuspend in lin_lwp_wait, initialized in 1199971Syan _initialize_lin_lwp. */ 1209971Syanstatic sigset_t suspend_mask; 1219971Syan 1229971Syan/* Signals to block to make that sigsuspend work. */ 1239971Syanstatic sigset_t blocked_mask; 1249971Syan 1259971Syan 1269971Syan/* Prototypes for local functions. */ 1279971Syanstatic int stop_wait_callback (struct lwp_info *lp, void *data); 1289971Syanstatic int lin_lwp_thread_alive (ptid_t ptid); 1299971Syan 1309971Syan/* Convert wait status STATUS to a string. Used for printing debug 1319971Syan messages only. */ 1329971Syan 1339971Syanstatic char * 1349971Syanstatus_to_str (int status) 1359971Syan{ 1369971Syan static char buf[64]; 13710154Syan 1389971Syan if (WIFSTOPPED (status)) 1399971Syan snprintf (buf, sizeof (buf), "%s (stopped)", 1409971Syan strsignal (WSTOPSIG (status))); 1419971Syan else if (WIFSIGNALED (status)) 1429971Syan snprintf (buf, sizeof (buf), "%s (terminated)", 1439971Syan strsignal (WSTOPSIG (status))); 1449971Syan else 1459971Syan snprintf (buf, sizeof (buf), "%d (exited)", WEXITSTATUS (status)); 1469971Syan 1479971Syan return buf; 1489971Syan} 1499971Syan 1509971Syan/* Initialize the list of LWPs. Note that this module, contrary to 1519971Syan what GDB's generic threads layer does for its thread list, 1529971Syan re-initializes the LWP lists whenever we mourn or detach (which 1539971Syan doesn't involve mourning) the inferior. */ 1549971Syan 1559971Syanstatic void 1569971Syaninit_lwp_list (void) 1579971Syan{ 1589971Syan struct lwp_info *lp, *lpnext; 15911127Syan 1609971Syan for (lp = lwp_list; lp; lp = lpnext) 1619971Syan { 1629971Syan lpnext = lp->next; 1639971Syan xfree (lp); 1649971Syan } 1659971Syan 1669971Syan lwp_list = NULL; 1679971Syan num_lwps = 0; 1689971Syan threaded = 0; 1699971Syan} 1709971Syan 1719971Syan/* Add the LWP specified by PID to the list. If this causes the 1729971Syan number of LWPs to become larger than one, go into "threaded" mode. 1739971Syan Return a pointer to the structure describing the new LWP. */ 1749971Syan 1759971Syanstatic struct lwp_info * 1769971Syanadd_lwp (ptid_t ptid) 1779971Syan{ 1789971Syan struct lwp_info *lp; 1799971Syan 1809971Syan gdb_assert (is_lwp (ptid)); 1819971Syan 1829971Syan lp = (struct lwp_info *) xmalloc (sizeof (struct lwp_info)); 1839971Syan 1849971Syan memset (lp, 0, sizeof (struct lwp_info)); 1859971Syan 1869971Syan lp->ptid = ptid; 1879971Syan 1889971Syan lp->next = lwp_list; 1899971Syan lwp_list = lp; 1909971Syan if (++num_lwps > 1) 1919971Syan threaded = 1; 1929971Syan 1939971Syan return lp; 1949971Syan} 1959971Syan 1969971Syan/* Remove the LWP specified by PID from the list. */ 1979971Syan 1989971Syanstatic void 1999971Syandelete_lwp (ptid_t ptid) 2009971Syan{ 2019971Syan struct lwp_info *lp, *lpprev; 2029971Syan 2039971Syan lpprev = NULL; 2049971Syan 2059971Syan for (lp = lwp_list; lp; lpprev = lp, lp = lp->next) 2069971Syan if (ptid_equal (lp->ptid, ptid)) 2079971Syan break; 2089971Syan 2099971Syan if (!lp) 2109971Syan return; 2119971Syan 2129971Syan /* We don't go back to "non-threaded" mode if the number of threads 2139971Syan becomes less than two. */ 2149971Syan num_lwps--; 2159971Syan 2169971Syan if (lpprev) 2179971Syan lpprev->next = lp->next; 2189971Syan else 2199971Syan lwp_list = lp->next; 2209971Syan 2219971Syan xfree (lp); 2229971Syan} 2239971Syan 2249971Syan/* Return a pointer to the structure describing the LWP corresponding 2259971Syan to PID. If no corresponding LWP could be found, return NULL. */ 2269971Syan 2279971Syanstatic struct lwp_info * 2289971Syanfind_lwp_pid (ptid_t ptid) 2299971Syan{ 2309971Syan struct lwp_info *lp; 2319971Syan int lwp; 2329971Syan 2339971Syan if (is_lwp (ptid)) 2349971Syan lwp = GET_LWP (ptid); 2359971Syan else 2369971Syan lwp = GET_PID (ptid); 2379971Syan 2389971Syan for (lp = lwp_list; lp; lp = lp->next) 2399971Syan if (lwp == GET_LWP (lp->ptid)) 2409971Syan return lp; 2419971Syan 2429971Syan return NULL; 2439971Syan} 24413901Salanb 24513901Salanb/* Call CALLBACK with its second argument set to DATA for every LWP in 2469971Syan the list. If CALLBACK returns 1 for a particular LWP, return a 24713901Salanb pointer to the structure describing that LWP immediately. 2489971Syan Otherwise return NULL. */ 24913901Salanb 25013901Salanbstruct lwp_info * 25113901Salanbiterate_over_lwps (int (*callback) (struct lwp_info *, void *), void *data) 25213901Salanb{ 25313901Salanb struct lwp_info *lp, *lpnext; 25413901Salanb 25513901Salanb for (lp = lwp_list; lp; lp = lpnext) 25613901Salanb { 25713901Salanb lpnext = lp->next; 2589971Syan if ((*callback) (lp, data)) 25913901Salanb return lp; 26011926Sserb } 26111926Sserb 26213901Salanb return NULL; 26313901Salanb} 26413901Salanb 26513901Salanb 26613901Salanb#if 0 26713901Salanbstatic void 26813901Salanblin_lwp_open (char *args, int from_tty) 2699971Syan{ 27013901Salanb push_target (&lin_lwp_ops); 27113901Salanb} 27213901Salanb#endif 27313901Salanb 27413901Salanb/* Attach to the LWP specified by PID. If VERBOSE is non-zero, print 27513901Salanb a message telling the user that a new LWP has been added to the 2769971Syan process. */ 27713901Salanb 2789971Syanvoid 2799971Syanlin_lwp_attach_lwp (ptid_t ptid, int verbose) 2809971Syan{ 2819971Syan struct lwp_info *lp; 2829971Syan 2839971Syan gdb_assert (is_lwp (ptid)); 2849971Syan 2859971Syan /* Make sure SIGCHLD is blocked. We don't want SIGCHLD events 2869971Syan to interrupt either the ptrace() or waitpid() calls below. */ 2879971Syan if (!sigismember (&blocked_mask, SIGCHLD)) 2889971Syan { 2899971Syan sigaddset (&blocked_mask, SIGCHLD); 2909971Syan sigprocmask (SIG_BLOCK, &blocked_mask, NULL); 2919971Syan } 2929971Syan 2939971Syan if (verbose) 2949971Syan printf_filtered ("[New %s]\n", target_pid_to_str (ptid)); 2959971Syan 2969971Syan lp = find_lwp_pid (ptid); 2979971Syan if (lp == NULL) 2989971Syan lp = add_lwp (ptid); 2999971Syan 3009971Syan /* We assume that we're already attached to any LWP that has an 3019971Syan id equal to the overall process id. */ 3029971Syan if (GET_LWP (ptid) != GET_PID (ptid)) 3039971Syan { 3049971Syan pid_t pid; 3059971Syan int status; 3069971Syan 3079971Syan if (ptrace (PTRACE_ATTACH, GET_LWP (ptid), 0, 0) < 0) 3089971Syan error ("Can't attach %s: %s", target_pid_to_str (ptid), 3099971Syan safe_strerror (errno)); 3109971Syan 3119971Syan if (debug_lin_lwp) 3129971Syan fprintf_unfiltered (gdb_stdlog, 3139971Syan "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n", 3149971Syan target_pid_to_str (ptid)); 3159971Syan 3169971Syan pid = waitpid (GET_LWP (ptid), &status, 0); 3179971Syan if (pid == -1 && errno == ECHILD) 3189971Syan { 3199971Syan /* Try again with __WCLONE to check cloned processes. */ 3209971Syan pid = waitpid (GET_LWP (ptid), &status, __WCLONE); 3219971Syan lp->cloned = 1; 3229971Syan } 3239971Syan 3249971Syan gdb_assert (pid == GET_LWP (ptid) 3259971Syan && WIFSTOPPED (status) && WSTOPSIG (status)); 3269971Syan 3279971Syan child_post_attach (pid); 3289971Syan 3299971Syan lp->stopped = 1; 3309971Syan 3319971Syan if (debug_lin_lwp) 3329971Syan { 3339971Syan fprintf_unfiltered (gdb_stdlog, 3349971Syan "LLAL: waitpid %s received %s\n", 3359971Syan target_pid_to_str (ptid), 3369971Syan status_to_str (status)); 3379971Syan } 3389971Syan } 3399971Syan else 3409971Syan { 3419971Syan /* We assume that the LWP representing the original process 3429971Syan is already stopped. Mark it as stopped in the data structure 3439971Syan that the lin-lwp layer uses to keep track of threads. Note 3449971Syan that this won't have already been done since the main thread 3459971Syan will have, we assume, been stopped by an attach from a 3469971Syan different layer. */ 3479971Syan lp->stopped = 1; 3489971Syan } 3499971Syan} 3509971Syan 3519971Syanstatic void 3529971Syanlin_lwp_attach (char *args, int from_tty) 3539971Syan{ 3549971Syan struct lwp_info *lp; 3559971Syan pid_t pid; 3569971Syan int status; 3579971Syan 3589971Syan /* FIXME: We should probably accept a list of process id's, and 3599971Syan attach all of them. */ 3609971Syan child_ops.to_attach (args, from_tty); 3619971Syan 3629971Syan /* Add the initial process as the first LWP to the list. */ 3639971Syan lp = add_lwp (BUILD_LWP (GET_PID (inferior_ptid), GET_PID (inferior_ptid))); 3649971Syan 3659971Syan /* Make sure the initial process is stopped. The user-level threads 3669971Syan layer might want to poke around in the inferior, and that won't 3679971Syan work if things haven't stabilized yet. */ 3689971Syan pid = waitpid (GET_PID (inferior_ptid), &status, 0); 3699971Syan if (pid == -1 && errno == ECHILD) 3709971Syan { 3719971Syan warning ("%s is a cloned process", target_pid_to_str (inferior_ptid)); 3729971Syan 3739971Syan /* Try again with __WCLONE to check cloned processes. */ 3749971Syan pid = waitpid (GET_PID (inferior_ptid), &status, __WCLONE); 3759971Syan lp->cloned = 1; 3769971Syan } 3779971Syan 3789971Syan gdb_assert (pid == GET_PID (inferior_ptid) 3799971Syan && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP); 3809971Syan 3819971Syan lp->stopped = 1; 3829971Syan 3839971Syan /* Fake the SIGSTOP that core GDB expects. */ 3849971Syan lp->status = W_STOPCODE (SIGSTOP); 3859971Syan lp->resumed = 1; 3869971Syan if (debug_lin_lwp) 3879971Syan { 3889971Syan fprintf_unfiltered (gdb_stdlog, 3899971Syan "LLA: waitpid %ld, faking SIGSTOP\n", (long) pid); 3909971Syan } 3919971Syan} 3929971Syan 3939971Syanstatic int 3949971Syandetach_callback (struct lwp_info *lp, void *data) 3959971Syan{ 3969971Syan gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status)); 3979971Syan 3989971Syan if (debug_lin_lwp && lp->status) 3999971Syan fprintf_unfiltered (gdb_stdlog, "DC: Pending %s for %s on detach.\n", 4009971Syan strsignal (WSTOPSIG (lp->status)), 4019971Syan target_pid_to_str (lp->ptid)); 4029971Syan 4039971Syan while (lp->signalled && lp->stopped) 4049971Syan { 4059971Syan errno = 0; 4069971Syan if (ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 4079971Syan WSTOPSIG (lp->status)) < 0) 4089971Syan error ("Can't continue %s: %s", target_pid_to_str (lp->ptid), 4099971Syan safe_strerror (errno)); 4109971Syan 4119971Syan if (debug_lin_lwp) 4129971Syan fprintf_unfiltered (gdb_stdlog, 4139971Syan "DC: PTRACE_CONTINUE (%s, 0, %s) (OK)\n", 4149971Syan target_pid_to_str (lp->ptid), 4159971Syan status_to_str (lp->status)); 4169971Syan 4179971Syan lp->stopped = 0; 4189971Syan lp->signalled = 0; 4199971Syan lp->status = 0; 4209971Syan /* FIXME drow/2003-08-26: There was a call to stop_wait_callback 4219971Syan here. But since lp->signalled was cleared above, 4229971Syan stop_wait_callback didn't do anything; the process was left 4239971Syan running. Shouldn't we be waiting for it to stop? 4249971Syan I've removed the call, since stop_wait_callback now does do 4259971Syan something when called with lp->signalled == 0. */ 4269971Syan 4279971Syan gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status)); 4289971Syan } 4299971Syan 4309971Syan /* We don't actually detach from the LWP that has an id equal to the 4319971Syan overall process id just yet. */ 4329971Syan if (GET_LWP (lp->ptid) != GET_PID (lp->ptid)) 4339971Syan { 4349971Syan errno = 0; 4359971Syan if (ptrace (PTRACE_DETACH, GET_LWP (lp->ptid), 0, 4369971Syan WSTOPSIG (lp->status)) < 0) 4379971Syan error ("Can't detach %s: %s", target_pid_to_str (lp->ptid), 4389971Syan safe_strerror (errno)); 4399971Syan 4409971Syan if (debug_lin_lwp) 4419971Syan fprintf_unfiltered (gdb_stdlog, 4429971Syan "PTRACE_DETACH (%s, %s, 0) (OK)\n", 4439971Syan target_pid_to_str (lp->ptid), 4449971Syan strsignal (WSTOPSIG (lp->status))); 4459971Syan 4469971Syan delete_lwp (lp->ptid); 4479971Syan } 4489971Syan 4499971Syan return 0; 4509971Syan} 4519971Syan 4529971Syanstatic void 4539971Syanlin_lwp_detach (char *args, int from_tty) 4549971Syan{ 4559971Syan iterate_over_lwps (detach_callback, NULL); 4569971Syan 4579971Syan /* Only the initial process should be left right now. */ 45811127Syan gdb_assert (num_lwps == 1); 45911127Syan 46011127Syan trap_ptid = null_ptid; 46111127Syan 46211127Syan /* Destroy LWP info; it's no longer valid. */ 4639971Syan init_lwp_list (); 4649971Syan 4659971Syan /* Restore the original signal mask. */ 4669971Syan sigprocmask (SIG_SETMASK, &normal_mask, NULL); 4679971Syan sigemptyset (&blocked_mask); 4689971Syan 4699971Syan inferior_ptid = pid_to_ptid (GET_PID (inferior_ptid)); 4709971Syan child_ops.to_detach (args, from_tty); 4719971Syan} 4729971Syan 47311127Syan 4749971Syan/* Resume LP. */ 4759971Syan 4769971Syanstatic int 4779971Syanresume_callback (struct lwp_info *lp, void *data) 4789971Syan{ 4799971Syan if (lp->stopped && lp->status == 0) 4809971Syan { 4819971Syan struct thread_info *tp; 4829971Syan 4839971Syan child_resume (pid_to_ptid (GET_LWP (lp->ptid)), 0, TARGET_SIGNAL_0); 4849971Syan if (debug_lin_lwp) 4859971Syan fprintf_unfiltered (gdb_stdlog, 4869971Syan "RC: PTRACE_CONT %s, 0, 0 (resume sibling)\n", 4879971Syan target_pid_to_str (lp->ptid)); 4889971Syan lp->stopped = 0; 4899971Syan lp->step = 0; 49011127Syan } 49111127Syan 49211127Syan return 0; 49311127Syan} 49411127Syan 4959971Syanstatic int 4969971Syanresume_clear_callback (struct lwp_info *lp, void *data) 4979971Syan{ 4989971Syan lp->resumed = 0; 4999971Syan return 0; 5009971Syan} 5019971Syan 50211127Syanstatic int 5039971Syanresume_set_callback (struct lwp_info *lp, void *data) 5049971Syan{ 5059971Syan lp->resumed = 1; 5069971Syan return 0; 5079971Syan} 5089971Syan 5099971Syanstatic void 5109971Syanlin_lwp_resume (ptid_t ptid, int step, enum target_signal signo) 5119971Syan{ 5129971Syan struct lwp_info *lp; 5139971Syan int resume_all; 5149971Syan 5159971Syan /* A specific PTID means `step only this process id'. */ 5169971Syan resume_all = (PIDGET (ptid) == -1); 5179971Syan 5189971Syan if (resume_all) 5199971Syan iterate_over_lwps (resume_set_callback, NULL); 5209971Syan else 5219971Syan iterate_over_lwps (resume_clear_callback, NULL); 5229971Syan 5239971Syan /* If PID is -1, it's the current inferior that should be 5249971Syan handled specially. */ 5259971Syan if (PIDGET (ptid) == -1) 5269971Syan ptid = inferior_ptid; 5279971Syan 5289971Syan lp = find_lwp_pid (ptid); 5299971Syan if (lp) 5309971Syan { 5319971Syan ptid = pid_to_ptid (GET_LWP (lp->ptid)); 5329971Syan 5339971Syan /* Remember if we're stepping. */ 5349971Syan lp->step = step; 5359971Syan 5369971Syan /* Mark this LWP as resumed. */ 5379971Syan lp->resumed = 1; 5389971Syan 5399971Syan /* If we have a pending wait status for this thread, there is no 5409971Syan point in resuming the process. */ 5419971Syan if (lp->status) 5429971Syan { 5439971Syan /* FIXME: What should we do if we are supposed to continue 5449971Syan this thread with a signal? */ 5459971Syan gdb_assert (signo == TARGET_SIGNAL_0); 5469971Syan return; 5479971Syan } 5489971Syan 5499971Syan /* Mark LWP as not stopped to prevent it from being continued by 5509971Syan resume_callback. */ 5519971Syan lp->stopped = 0; 5529971Syan } 5539971Syan 5549971Syan if (resume_all) 5559971Syan iterate_over_lwps (resume_callback, NULL); 5569971Syan 5579971Syan child_resume (ptid, step, signo); 5589971Syan if (debug_lin_lwp) 5599971Syan fprintf_unfiltered (gdb_stdlog, 5609971Syan "LLR: %s %s, %s (resume event thread)\n", 5619971Syan step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT", 5629971Syan target_pid_to_str (ptid), 5639971Syan signo ? strsignal (signo) : "0"); 5649971Syan} 5659971Syan 5669971Syan 5679971Syan/* Issue kill to specified lwp. */ 5689971Syan 5699971Syanstatic int tkill_failed; 5709971Syan 5719971Syanstatic int 5729971Syankill_lwp (int lwpid, int signo) 5739971Syan{ 5749971Syan errno = 0; 5759971Syan 5769971Syan/* Use tkill, if possible, in case we are using nptl threads. If tkill 5779971Syan fails, then we are not using nptl threads and we should be using kill. */ 5789971Syan 5799971Syan#ifdef HAVE_TKILL_SYSCALL 5809971Syan if (!tkill_failed) 5819971Syan { 5829971Syan int ret = syscall (__NR_tkill, lwpid, signo); 5839971Syan if (errno != ENOSYS) 5849971Syan return ret; 5859971Syan errno = 0; 5869971Syan tkill_failed = 1; 5879971Syan } 5889971Syan#endif 5899971Syan 5909971Syan return kill (lwpid, signo); 5919971Syan} 5929971Syan 5939971Syan/* Wait for LP to stop. Returns the wait status, or 0 if the LWP has 5949971Syan exited. */ 5959971Syan 5969971Syanstatic int 5979971Syanwait_lwp (struct lwp_info *lp) 5989971Syan{ 5999971Syan pid_t pid; 6009971Syan int status; 6019971Syan int thread_dead = 0; 6029971Syan 6039971Syan gdb_assert (!lp->stopped); 6049971Syan gdb_assert (lp->status == 0); 6059971Syan 6069971Syan pid = waitpid (GET_LWP (lp->ptid), &status, 0); 6079971Syan if (pid == -1 && errno == ECHILD) 6089971Syan { 6099971Syan pid = waitpid (GET_LWP (lp->ptid), &status, __WCLONE); 6109971Syan if (pid == -1 && errno == ECHILD) 6119971Syan { 6129971Syan /* The thread has previously exited. We need to delete it now 6139971Syan because in the case of NPTL threads, there won't be an 6149971Syan exit event unless it is the main thread. */ 6159971Syan thread_dead = 1; 6169971Syan if (debug_lin_lwp) 6179971Syan fprintf_unfiltered (gdb_stdlog, "WL: %s vanished.\n", 6189971Syan target_pid_to_str (lp->ptid)); 6199971Syan } 6209971Syan } 6219971Syan 6229971Syan if (!thread_dead) 6239971Syan { 6249971Syan gdb_assert (pid == GET_LWP (lp->ptid)); 6259971Syan 6269971Syan if (debug_lin_lwp) 6279971Syan { 6289971Syan fprintf_unfiltered (gdb_stdlog, 6299971Syan "WL: waitpid %s received %s\n", 6309971Syan target_pid_to_str (lp->ptid), 6319971Syan status_to_str (status)); 6329971Syan } 6339971Syan } 6349971Syan 6359971Syan /* Check if the thread has exited. */ 6369971Syan if (WIFEXITED (status) || WIFSIGNALED (status)) 6379971Syan { 6389971Syan thread_dead = 1; 6399971Syan if (debug_lin_lwp) 6409971Syan fprintf_unfiltered (gdb_stdlog, "WL: %s exited.\n", 6419971Syan target_pid_to_str (lp->ptid)); 6429971Syan } 6439971Syan 6449971Syan if (thread_dead) 6459971Syan { 6469971Syan if (in_thread_list (lp->ptid)) 6479971Syan { 6489971Syan /* Core GDB cannot deal with us deleting the current thread. */ 6499971Syan if (!ptid_equal (lp->ptid, inferior_ptid)) 6509971Syan delete_thread (lp->ptid); 6519971Syan printf_unfiltered ("[%s exited]\n", 6529971Syan target_pid_to_str (lp->ptid)); 6539971Syan } 6549971Syan 6559971Syan delete_lwp (lp->ptid); 6569971Syan return 0; 6579971Syan } 6589971Syan 6599971Syan gdb_assert (WIFSTOPPED (status)); 6609971Syan 6619971Syan return status; 6629971Syan} 6639971Syan 6649971Syan/* Send a SIGSTOP to LP. */ 6659971Syan 6669971Syanstatic int 6679971Syanstop_callback (struct lwp_info *lp, void *data) 6689971Syan{ 6699971Syan if (!lp->stopped && !lp->signalled) 6709971Syan { 6719971Syan int ret; 6729971Syan 6739971Syan if (debug_lin_lwp) 6749971Syan { 6759971Syan fprintf_unfiltered (gdb_stdlog, 6769971Syan "SC: kill %s **<SIGSTOP>**\n", 6779971Syan target_pid_to_str (lp->ptid)); 6789971Syan } 6799971Syan errno = 0; 6809971Syan ret = kill_lwp (GET_LWP (lp->ptid), SIGSTOP); 6819971Syan if (debug_lin_lwp) 6829971Syan { 6839971Syan fprintf_unfiltered (gdb_stdlog, 6849971Syan "SC: lwp kill %d %s\n", 6859971Syan ret, 6869971Syan errno ? safe_strerror (errno) : "ERRNO-OK"); 6879971Syan } 6889971Syan 6899971Syan lp->signalled = 1; 6909971Syan gdb_assert (lp->status == 0); 6919971Syan } 6929971Syan 6939971Syan return 0; 6949971Syan} 6959971Syan 6969971Syan/* Wait until LP is stopped. If DATA is non-null it is interpreted as 6979971Syan a pointer to a set of signals to be flushed immediately. */ 6989971Syan 6999971Syanstatic int 7009971Syanstop_wait_callback (struct lwp_info *lp, void *data) 7019971Syan{ 7029971Syan sigset_t *flush_mask = data; 7039971Syan 7049971Syan if (!lp->stopped) 7059971Syan { 7069971Syan int status; 7079971Syan 7089971Syan status = wait_lwp (lp); 7099971Syan if (status == 0) 7109971Syan return 0; 7119971Syan 7129971Syan /* Ignore any signals in FLUSH_MASK. */ 7139971Syan if (flush_mask && sigismember (flush_mask, WSTOPSIG (status))) 7149971Syan { 7159971Syan if (!lp->signalled) 7169971Syan { 7179971Syan lp->stopped = 1; 7189971Syan return 0; 7199971Syan } 7209971Syan 7219971Syan errno = 0; 7229971Syan ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0); 7239971Syan if (debug_lin_lwp) 7249971Syan fprintf_unfiltered (gdb_stdlog, 7259971Syan "PTRACE_CONT %s, 0, 0 (%s)\n", 7269971Syan target_pid_to_str (lp->ptid), 7279971Syan errno ? safe_strerror (errno) : "OK"); 7289971Syan 7299971Syan return stop_wait_callback (lp, flush_mask); 7309971Syan } 7319971Syan 7329971Syan if (WSTOPSIG (status) != SIGSTOP) 7339971Syan { 7349971Syan if (WSTOPSIG (status) == SIGTRAP) 7359971Syan { 7369971Syan /* If a LWP other than the LWP that we're reporting an 7379971Syan event for has hit a GDB breakpoint (as opposed to 7389971Syan some random trap signal), then just arrange for it to 7399971Syan hit it again later. We don't keep the SIGTRAP status 7409971Syan and don't forward the SIGTRAP signal to the LWP. We 7419971Syan will handle the current event, eventually we will 7429971Syan resume all LWPs, and this one will get its breakpoint 7439971Syan trap again. 7449971Syan 7459971Syan If we do not do this, then we run the risk that the 7469971Syan user will delete or disable the breakpoint, but the 7479971Syan thread will have already tripped on it. */ 7489971Syan 7499971Syan /* Now resume this LWP and get the SIGSTOP event. */ 7509971Syan errno = 0; 7519971Syan ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0); 7529971Syan if (debug_lin_lwp) 7539971Syan { 7549971Syan fprintf_unfiltered (gdb_stdlog, 7559971Syan "PTRACE_CONT %s, 0, 0 (%s)\n", 7569971Syan target_pid_to_str (lp->ptid), 7579971Syan errno ? safe_strerror (errno) : "OK"); 7589971Syan 7599971Syan fprintf_unfiltered (gdb_stdlog, 7609971Syan "SWC: Candidate SIGTRAP event in %s\n", 7619971Syan target_pid_to_str (lp->ptid)); 7629971Syan } 7639971Syan /* Hold the SIGTRAP for handling by lin_lwp_wait. */ 7649971Syan stop_wait_callback (lp, data); 7659971Syan /* If there's another event, throw it back into the queue. */ 7669971Syan if (lp->status) 7679971Syan { 7689971Syan if (debug_lin_lwp) 7699971Syan { 7709971Syan fprintf_unfiltered (gdb_stdlog, 7719971Syan "SWC: kill %s, %s\n", 7729971Syan target_pid_to_str (lp->ptid), 7739971Syan status_to_str ((int) status)); 7749971Syan } 7759971Syan kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (lp->status)); 7769971Syan } 7779971Syan /* Save the sigtrap event. */ 7789971Syan lp->status = status; 7799971Syan return 0; 7809971Syan } 7819971Syan else 7829971Syan { 7839971Syan /* The thread was stopped with a signal other than 7849971Syan SIGSTOP, and didn't accidentally trip a breakpoint. */ 7859971Syan 7869971Syan if (debug_lin_lwp) 7879971Syan { 7889971Syan fprintf_unfiltered (gdb_stdlog, 7899971Syan "SWC: Pending event %s in %s\n", 7909971Syan status_to_str ((int) status), 7919971Syan target_pid_to_str (lp->ptid)); 7929971Syan } 7939971Syan /* Now resume this LWP and get the SIGSTOP event. */ 7949971Syan errno = 0; 7959971Syan ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0); 7969971Syan if (debug_lin_lwp) 7979971Syan fprintf_unfiltered (gdb_stdlog, 7989971Syan "SWC: PTRACE_CONT %s, 0, 0 (%s)\n", 7999971Syan target_pid_to_str (lp->ptid), 8009971Syan errno ? safe_strerror (errno) : "OK"); 8019971Syan 8029971Syan /* Hold this event/waitstatus while we check to see if 8039971Syan there are any more (we still want to get that SIGSTOP). */ 8049971Syan stop_wait_callback (lp, data); 8059971Syan /* If the lp->status field is still empty, use it to hold 8069971Syan this event. If not, then this event must be returned 8079971Syan to the event queue of the LWP. */ 8089971Syan if (lp->status == 0) 8099971Syan lp->status = status; 8109971Syan else 8119971Syan { 8129971Syan if (debug_lin_lwp) 8139971Syan { 8149971Syan fprintf_unfiltered (gdb_stdlog, 8159971Syan "SWC: kill %s, %s\n", 8169971Syan target_pid_to_str (lp->ptid), 8179971Syan status_to_str ((int) status)); 8189971Syan } 8199971Syan kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (status)); 8209971Syan } 8219971Syan return 0; 8229971Syan } 8239971Syan } 8249971Syan else 8259971Syan { 8269971Syan /* We caught the SIGSTOP that we intended to catch, so 8279971Syan there's no SIGSTOP pending. */ 8289971Syan lp->stopped = 1; 8299971Syan lp->signalled = 0; 8309971Syan } 8319971Syan } 8329971Syan 833 return 0; 834} 835 836/* Check whether PID has any pending signals in FLUSH_MASK. If so set 837 the appropriate bits in PENDING, and return 1 - otherwise return 0. */ 838 839static int 840lin_lwp_has_pending (int pid, sigset_t *pending, sigset_t *flush_mask) 841{ 842 sigset_t blocked, ignored; 843 int i; 844 845 linux_proc_pending_signals (pid, pending, &blocked, &ignored); 846 847 if (!flush_mask) 848 return 0; 849 850 for (i = 1; i < NSIG; i++) 851 if (sigismember (pending, i)) 852 if (!sigismember (flush_mask, i) 853 || sigismember (&blocked, i) 854 || sigismember (&ignored, i)) 855 sigdelset (pending, i); 856 857 if (sigisemptyset (pending)) 858 return 0; 859 860 return 1; 861} 862 863/* DATA is interpreted as a mask of signals to flush. If LP has 864 signals pending, and they are all in the flush mask, then arrange 865 to flush them. LP should be stopped, as should all other threads 866 it might share a signal queue with. */ 867 868static int 869flush_callback (struct lwp_info *lp, void *data) 870{ 871 sigset_t *flush_mask = data; 872 sigset_t pending, intersection, blocked, ignored; 873 int pid, status; 874 875 /* Normally, when an LWP exits, it is removed from the LWP list. The 876 last LWP isn't removed till later, however. So if there is only 877 one LWP on the list, make sure it's alive. */ 878 if (lwp_list == lp && lp->next == NULL) 879 if (!lin_lwp_thread_alive (lp->ptid)) 880 return 0; 881 882 /* Just because the LWP is stopped doesn't mean that new signals 883 can't arrive from outside, so this function must be careful of 884 race conditions. However, because all threads are stopped, we 885 can assume that the pending mask will not shrink unless we resume 886 the LWP, and that it will then get another signal. We can't 887 control which one, however. */ 888 889 if (lp->status) 890 { 891 if (debug_lin_lwp) 892 printf_unfiltered ("FC: LP has pending status %06x\n", lp->status); 893 if (WIFSTOPPED (lp->status) && sigismember (flush_mask, WSTOPSIG (lp->status))) 894 lp->status = 0; 895 } 896 897 while (lin_lwp_has_pending (GET_LWP (lp->ptid), &pending, flush_mask)) 898 { 899 int ret; 900 901 errno = 0; 902 ret = ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0); 903 if (debug_lin_lwp) 904 fprintf_unfiltered (gdb_stderr, 905 "FC: Sent PTRACE_CONT, ret %d %d\n", ret, errno); 906 907 lp->stopped = 0; 908 stop_wait_callback (lp, flush_mask); 909 if (debug_lin_lwp) 910 fprintf_unfiltered (gdb_stderr, 911 "FC: Wait finished; saved status is %d\n", 912 lp->status); 913 } 914 915 return 0; 916} 917 918/* Return non-zero if LP has a wait status pending. */ 919 920static int 921status_callback (struct lwp_info *lp, void *data) 922{ 923 /* Only report a pending wait status if we pretend that this has 924 indeed been resumed. */ 925 return (lp->status != 0 && lp->resumed); 926} 927 928/* Return non-zero if LP isn't stopped. */ 929 930static int 931running_callback (struct lwp_info *lp, void *data) 932{ 933 return (lp->stopped == 0 || (lp->status != 0 && lp->resumed)); 934} 935 936/* Count the LWP's that have had events. */ 937 938static int 939count_events_callback (struct lwp_info *lp, void *data) 940{ 941 int *count = data; 942 943 gdb_assert (count != NULL); 944 945 /* Count only LWPs that have a SIGTRAP event pending. */ 946 if (lp->status != 0 947 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP) 948 (*count)++; 949 950 return 0; 951} 952 953/* Select the LWP (if any) that is currently being single-stepped. */ 954 955static int 956select_singlestep_lwp_callback (struct lwp_info *lp, void *data) 957{ 958 if (lp->step && lp->status != 0) 959 return 1; 960 else 961 return 0; 962} 963 964/* Select the Nth LWP that has had a SIGTRAP event. */ 965 966static int 967select_event_lwp_callback (struct lwp_info *lp, void *data) 968{ 969 int *selector = data; 970 971 gdb_assert (selector != NULL); 972 973 /* Select only LWPs that have a SIGTRAP event pending. */ 974 if (lp->status != 0 975 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP) 976 if ((*selector)-- == 0) 977 return 1; 978 979 return 0; 980} 981 982static int 983cancel_breakpoints_callback (struct lwp_info *lp, void *data) 984{ 985 struct lwp_info *event_lp = data; 986 987 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */ 988 if (lp == event_lp) 989 return 0; 990 991 /* If a LWP other than the LWP that we're reporting an event for has 992 hit a GDB breakpoint (as opposed to some random trap signal), 993 then just arrange for it to hit it again later. We don't keep 994 the SIGTRAP status and don't forward the SIGTRAP signal to the 995 LWP. We will handle the current event, eventually we will resume 996 all LWPs, and this one will get its breakpoint trap again. 997 998 If we do not do this, then we run the risk that the user will 999 delete or disable the breakpoint, but the LWP will have already 1000 tripped on it. */ 1001 1002 if (lp->status != 0 1003 && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP 1004 && breakpoint_inserted_here_p (read_pc_pid (lp->ptid) - 1005 DECR_PC_AFTER_BREAK)) 1006 { 1007 if (debug_lin_lwp) 1008 fprintf_unfiltered (gdb_stdlog, 1009 "CBC: Push back breakpoint for %s\n", 1010 target_pid_to_str (lp->ptid)); 1011 1012 /* Back up the PC if necessary. */ 1013 if (DECR_PC_AFTER_BREAK) 1014 write_pc_pid (read_pc_pid (lp->ptid) - DECR_PC_AFTER_BREAK, lp->ptid); 1015 1016 /* Throw away the SIGTRAP. */ 1017 lp->status = 0; 1018 } 1019 1020 return 0; 1021} 1022 1023/* Select one LWP out of those that have events pending. */ 1024 1025static void 1026select_event_lwp (struct lwp_info **orig_lp, int *status) 1027{ 1028 int num_events = 0; 1029 int random_selector; 1030 struct lwp_info *event_lp; 1031 1032 /* Record the wait status for the origional LWP. */ 1033 (*orig_lp)->status = *status; 1034 1035 /* Give preference to any LWP that is being single-stepped. */ 1036 event_lp = iterate_over_lwps (select_singlestep_lwp_callback, NULL); 1037 if (event_lp != NULL) 1038 { 1039 if (debug_lin_lwp) 1040 fprintf_unfiltered (gdb_stdlog, 1041 "SEL: Select single-step %s\n", 1042 target_pid_to_str (event_lp->ptid)); 1043 } 1044 else 1045 { 1046 /* No single-stepping LWP. Select one at random, out of those 1047 which have had SIGTRAP events. */ 1048 1049 /* First see how many SIGTRAP events we have. */ 1050 iterate_over_lwps (count_events_callback, &num_events); 1051 1052 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */ 1053 random_selector = (int) 1054 ((num_events * (double) rand ()) / (RAND_MAX + 1.0)); 1055 1056 if (debug_lin_lwp && num_events > 1) 1057 fprintf_unfiltered (gdb_stdlog, 1058 "SEL: Found %d SIGTRAP events, selecting #%d\n", 1059 num_events, random_selector); 1060 1061 event_lp = iterate_over_lwps (select_event_lwp_callback, 1062 &random_selector); 1063 } 1064 1065 if (event_lp != NULL) 1066 { 1067 /* Switch the event LWP. */ 1068 *orig_lp = event_lp; 1069 *status = event_lp->status; 1070 } 1071 1072 /* Flush the wait status for the event LWP. */ 1073 (*orig_lp)->status = 0; 1074} 1075 1076/* Return non-zero if LP has been resumed. */ 1077 1078static int 1079resumed_callback (struct lwp_info *lp, void *data) 1080{ 1081 return lp->resumed; 1082} 1083 1084#ifdef CHILD_WAIT 1085 1086/* We need to override child_wait to support attaching to cloned 1087 processes, since a normal wait (as done by the default version) 1088 ignores those processes. */ 1089 1090/* Wait for child PTID to do something. Return id of the child, 1091 minus_one_ptid in case of error; store status into *OURSTATUS. */ 1092 1093ptid_t 1094child_wait (ptid_t ptid, struct target_waitstatus *ourstatus) 1095{ 1096 int save_errno; 1097 int status; 1098 pid_t pid; 1099 1100 do 1101 { 1102 set_sigint_trap (); /* Causes SIGINT to be passed on to the 1103 attached process. */ 1104 set_sigio_trap (); 1105 1106 pid = waitpid (GET_PID (ptid), &status, 0); 1107 if (pid == -1 && errno == ECHILD) 1108 /* Try again with __WCLONE to check cloned processes. */ 1109 pid = waitpid (GET_PID (ptid), &status, __WCLONE); 1110 1111 if (debug_lin_lwp) 1112 { 1113 fprintf_unfiltered (gdb_stdlog, 1114 "CW: waitpid %ld received %s\n", 1115 (long) pid, status_to_str (status)); 1116 } 1117 1118 save_errno = errno; 1119 1120 /* Make sure we don't report an event for the exit of the 1121 original program, if we've detached from it. */ 1122 if (pid != -1 && !WIFSTOPPED (status) && pid != GET_PID (inferior_ptid)) 1123 { 1124 pid = -1; 1125 save_errno = EINTR; 1126 } 1127 1128 /* Check for stop events reported by a process we didn't already 1129 know about - in this case, anything other than inferior_ptid. 1130 1131 If we're expecting to receive stopped processes after fork, 1132 vfork, and clone events, then we'll just add the new one to 1133 our list and go back to waiting for the event to be reported 1134 - the stopped process might be returned from waitpid before 1135 or after the event is. If we want to handle debugging of 1136 CLONE_PTRACE processes we need to do more here, i.e. switch 1137 to multi-threaded mode. */ 1138 if (pid != -1 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP 1139 && pid != GET_PID (inferior_ptid)) 1140 { 1141 linux_record_stopped_pid (pid); 1142 pid = -1; 1143 save_errno = EINTR; 1144 } 1145 1146 clear_sigio_trap (); 1147 clear_sigint_trap (); 1148 } 1149 while (pid == -1 && save_errno == EINTR); 1150 1151 if (pid == -1) 1152 { 1153 warning ("Child process unexpectedly missing: %s", 1154 safe_strerror (errno)); 1155 1156 /* Claim it exited with unknown signal. */ 1157 ourstatus->kind = TARGET_WAITKIND_SIGNALLED; 1158 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN; 1159 return minus_one_ptid; 1160 } 1161 1162 /* Handle GNU/Linux's extended waitstatus for trace events. */ 1163 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0) 1164 return linux_handle_extended_wait (pid, status, ourstatus); 1165 1166 store_waitstatus (ourstatus, status); 1167 return pid_to_ptid (pid); 1168} 1169 1170#endif 1171 1172/* Stop an active thread, verify it still exists, then resume it. */ 1173 1174static int 1175stop_and_resume_callback (struct lwp_info *lp, void *data) 1176{ 1177 struct lwp_info *ptr; 1178 1179 if (!lp->stopped && !lp->signalled) 1180 { 1181 stop_callback (lp, NULL); 1182 stop_wait_callback (lp, NULL); 1183 /* Resume if the lwp still exists. */ 1184 for (ptr = lwp_list; ptr; ptr = ptr->next) 1185 if (lp == ptr) 1186 { 1187 resume_callback (lp, NULL); 1188 resume_set_callback (lp, NULL); 1189 } 1190 } 1191 return 0; 1192} 1193 1194static ptid_t 1195lin_lwp_wait (ptid_t ptid, struct target_waitstatus *ourstatus) 1196{ 1197 struct lwp_info *lp = NULL; 1198 int options = 0; 1199 int status = 0; 1200 pid_t pid = PIDGET (ptid); 1201 sigset_t flush_mask; 1202 1203 sigemptyset (&flush_mask); 1204 1205 /* Make sure SIGCHLD is blocked. */ 1206 if (!sigismember (&blocked_mask, SIGCHLD)) 1207 { 1208 sigaddset (&blocked_mask, SIGCHLD); 1209 sigprocmask (SIG_BLOCK, &blocked_mask, NULL); 1210 } 1211 1212retry: 1213 1214 /* Make sure there is at least one LWP that has been resumed, at 1215 least if there are any LWPs at all. */ 1216 gdb_assert (num_lwps == 0 || iterate_over_lwps (resumed_callback, NULL)); 1217 1218 /* First check if there is a LWP with a wait status pending. */ 1219 if (pid == -1) 1220 { 1221 /* Any LWP that's been resumed will do. */ 1222 lp = iterate_over_lwps (status_callback, NULL); 1223 if (lp) 1224 { 1225 status = lp->status; 1226 lp->status = 0; 1227 1228 if (debug_lin_lwp && status) 1229 fprintf_unfiltered (gdb_stdlog, 1230 "LLW: Using pending wait status %s for %s.\n", 1231 status_to_str (status), 1232 target_pid_to_str (lp->ptid)); 1233 } 1234 1235 /* But if we don't fine one, we'll have to wait, and check both 1236 cloned and uncloned processes. We start with the cloned 1237 processes. */ 1238 options = __WCLONE | WNOHANG; 1239 } 1240 else if (is_lwp (ptid)) 1241 { 1242 if (debug_lin_lwp) 1243 fprintf_unfiltered (gdb_stdlog, 1244 "LLW: Waiting for specific LWP %s.\n", 1245 target_pid_to_str (ptid)); 1246 1247 /* We have a specific LWP to check. */ 1248 lp = find_lwp_pid (ptid); 1249 gdb_assert (lp); 1250 status = lp->status; 1251 lp->status = 0; 1252 1253 if (debug_lin_lwp && status) 1254 fprintf_unfiltered (gdb_stdlog, 1255 "LLW: Using pending wait status %s for %s.\n", 1256 status_to_str (status), 1257 target_pid_to_str (lp->ptid)); 1258 1259 /* If we have to wait, take into account whether PID is a cloned 1260 process or not. And we have to convert it to something that 1261 the layer beneath us can understand. */ 1262 options = lp->cloned ? __WCLONE : 0; 1263 pid = GET_LWP (ptid); 1264 } 1265 1266 if (status && lp->signalled) 1267 { 1268 /* A pending SIGSTOP may interfere with the normal stream of 1269 events. In a typical case where interference is a problem, 1270 we have a SIGSTOP signal pending for LWP A while 1271 single-stepping it, encounter an event in LWP B, and take the 1272 pending SIGSTOP while trying to stop LWP A. After processing 1273 the event in LWP B, LWP A is continued, and we'll never see 1274 the SIGTRAP associated with the last time we were 1275 single-stepping LWP A. */ 1276 1277 /* Resume the thread. It should halt immediately returning the 1278 pending SIGSTOP. */ 1279 registers_changed (); 1280 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step, 1281 TARGET_SIGNAL_0); 1282 if (debug_lin_lwp) 1283 fprintf_unfiltered (gdb_stdlog, 1284 "LLW: %s %s, 0, 0 (expect SIGSTOP)\n", 1285 lp->step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT", 1286 target_pid_to_str (lp->ptid)); 1287 lp->stopped = 0; 1288 gdb_assert (lp->resumed); 1289 1290 /* This should catch the pending SIGSTOP. */ 1291 stop_wait_callback (lp, NULL); 1292 } 1293 1294 set_sigint_trap (); /* Causes SIGINT to be passed on to the 1295 attached process. */ 1296 set_sigio_trap (); 1297 1298 while (status == 0) 1299 { 1300 pid_t lwpid; 1301 1302 lwpid = waitpid (pid, &status, options); 1303 if (lwpid > 0) 1304 { 1305 gdb_assert (pid == -1 || lwpid == pid); 1306 1307 if (debug_lin_lwp) 1308 { 1309 fprintf_unfiltered (gdb_stdlog, 1310 "LLW: waitpid %ld received %s\n", 1311 (long) lwpid, status_to_str (status)); 1312 } 1313 1314 lp = find_lwp_pid (pid_to_ptid (lwpid)); 1315 1316 /* Check for stop events reported by a process we didn't 1317 already know about - anything not already in our LWP 1318 list. 1319 1320 If we're expecting to receive stopped processes after 1321 fork, vfork, and clone events, then we'll just add the 1322 new one to our list and go back to waiting for the event 1323 to be reported - the stopped process might be returned 1324 from waitpid before or after the event is. */ 1325 if (WIFSTOPPED (status) && !lp) 1326 { 1327 linux_record_stopped_pid (lwpid); 1328 status = 0; 1329 continue; 1330 } 1331 1332 /* Make sure we don't report an event for the exit of an LWP not in 1333 our list, i.e. not part of the current process. This can happen 1334 if we detach from a program we original forked and then it 1335 exits. */ 1336 if (!WIFSTOPPED (status) && !lp) 1337 { 1338 status = 0; 1339 continue; 1340 } 1341 1342 /* NOTE drow/2003-06-17: This code seems to be meant for debugging 1343 CLONE_PTRACE processes which do not use the thread library - 1344 otherwise we wouldn't find the new LWP this way. That doesn't 1345 currently work, and the following code is currently unreachable 1346 due to the two blocks above. If it's fixed some day, this code 1347 should be broken out into a function so that we can also pick up 1348 LWPs from the new interface. */ 1349 if (!lp) 1350 { 1351 lp = add_lwp (BUILD_LWP (lwpid, GET_PID (inferior_ptid))); 1352 if (options & __WCLONE) 1353 lp->cloned = 1; 1354 1355 if (threaded) 1356 { 1357 gdb_assert (WIFSTOPPED (status) 1358 && WSTOPSIG (status) == SIGSTOP); 1359 lp->signalled = 1; 1360 1361 if (!in_thread_list (inferior_ptid)) 1362 { 1363 inferior_ptid = BUILD_LWP (GET_PID (inferior_ptid), 1364 GET_PID (inferior_ptid)); 1365 add_thread (inferior_ptid); 1366 } 1367 1368 add_thread (lp->ptid); 1369 printf_unfiltered ("[New %s]\n", 1370 target_pid_to_str (lp->ptid)); 1371 } 1372 } 1373 1374 /* Check if the thread has exited. */ 1375 if ((WIFEXITED (status) || WIFSIGNALED (status)) && num_lwps > 1) 1376 { 1377 if (in_thread_list (lp->ptid)) 1378 { 1379 /* Core GDB cannot deal with us deleting the current 1380 thread. */ 1381 if (!ptid_equal (lp->ptid, inferior_ptid)) 1382 delete_thread (lp->ptid); 1383 printf_unfiltered ("[%s exited]\n", 1384 target_pid_to_str (lp->ptid)); 1385 } 1386 1387 /* If this is the main thread, we must stop all threads and 1388 verify if they are still alive. This is because in the nptl 1389 thread model, there is no signal issued for exiting LWPs 1390 other than the main thread. We only get the main thread 1391 exit signal once all child threads have already exited. 1392 If we stop all the threads and use the stop_wait_callback 1393 to check if they have exited we can determine whether this 1394 signal should be ignored or whether it means the end of the 1395 debugged application, regardless of which threading model 1396 is being used. */ 1397 if (GET_PID (lp->ptid) == GET_LWP (lp->ptid)) 1398 { 1399 lp->stopped = 1; 1400 iterate_over_lwps (stop_and_resume_callback, NULL); 1401 } 1402 1403 if (debug_lin_lwp) 1404 fprintf_unfiltered (gdb_stdlog, 1405 "LLW: %s exited.\n", 1406 target_pid_to_str (lp->ptid)); 1407 1408 delete_lwp (lp->ptid); 1409 1410 /* If there is at least one more LWP, then the exit signal 1411 was not the end of the debugged application and should be 1412 ignored. */ 1413 if (num_lwps > 0) 1414 { 1415 /* Make sure there is at least one thread running. */ 1416 gdb_assert (iterate_over_lwps (running_callback, NULL)); 1417 1418 /* Discard the event. */ 1419 status = 0; 1420 continue; 1421 } 1422 } 1423 1424 /* Check if the current LWP has previously exited. In the nptl 1425 thread model, LWPs other than the main thread do not issue 1426 signals when they exit so we must check whenever the thread 1427 has stopped. A similar check is made in stop_wait_callback(). */ 1428 if (num_lwps > 1 && !lin_lwp_thread_alive (lp->ptid)) 1429 { 1430 if (in_thread_list (lp->ptid)) 1431 { 1432 /* Core GDB cannot deal with us deleting the current 1433 thread. */ 1434 if (!ptid_equal (lp->ptid, inferior_ptid)) 1435 delete_thread (lp->ptid); 1436 printf_unfiltered ("[%s exited]\n", 1437 target_pid_to_str (lp->ptid)); 1438 } 1439 if (debug_lin_lwp) 1440 fprintf_unfiltered (gdb_stdlog, 1441 "LLW: %s exited.\n", 1442 target_pid_to_str (lp->ptid)); 1443 1444 delete_lwp (lp->ptid); 1445 1446 /* Make sure there is at least one thread running. */ 1447 gdb_assert (iterate_over_lwps (running_callback, NULL)); 1448 1449 /* Discard the event. */ 1450 status = 0; 1451 continue; 1452 } 1453 1454 /* Make sure we don't report a SIGSTOP that we sent 1455 ourselves in an attempt to stop an LWP. */ 1456 if (lp->signalled 1457 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP) 1458 { 1459 if (debug_lin_lwp) 1460 fprintf_unfiltered (gdb_stdlog, 1461 "LLW: Delayed SIGSTOP caught for %s.\n", 1462 target_pid_to_str (lp->ptid)); 1463 1464 /* This is a delayed SIGSTOP. */ 1465 lp->signalled = 0; 1466 1467 registers_changed (); 1468 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step, 1469 TARGET_SIGNAL_0); 1470 if (debug_lin_lwp) 1471 fprintf_unfiltered (gdb_stdlog, 1472 "LLW: %s %s, 0, 0 (discard SIGSTOP)\n", 1473 lp->step ? 1474 "PTRACE_SINGLESTEP" : "PTRACE_CONT", 1475 target_pid_to_str (lp->ptid)); 1476 1477 lp->stopped = 0; 1478 gdb_assert (lp->resumed); 1479 1480 /* Discard the event. */ 1481 status = 0; 1482 continue; 1483 } 1484 1485 break; 1486 } 1487 1488 if (pid == -1) 1489 { 1490 /* Alternate between checking cloned and uncloned processes. */ 1491 options ^= __WCLONE; 1492 1493 /* And suspend every time we have checked both. */ 1494 if (options & __WCLONE) 1495 sigsuspend (&suspend_mask); 1496 } 1497 1498 /* We shouldn't end up here unless we want to try again. */ 1499 gdb_assert (status == 0); 1500 } 1501 1502 clear_sigio_trap (); 1503 clear_sigint_trap (); 1504 1505 gdb_assert (lp); 1506 1507 /* Don't report signals that GDB isn't interested in, such as 1508 signals that are neither printed nor stopped upon. Stopping all 1509 threads can be a bit time-consuming so if we want decent 1510 performance with heavily multi-threaded programs, especially when 1511 they're using a high frequency timer, we'd better avoid it if we 1512 can. */ 1513 1514 if (WIFSTOPPED (status)) 1515 { 1516 int signo = target_signal_from_host (WSTOPSIG (status)); 1517 1518 if (signal_stop_state (signo) == 0 1519 && signal_print_state (signo) == 0 1520 && signal_pass_state (signo) == 1) 1521 { 1522 /* FIMXE: kettenis/2001-06-06: Should we resume all threads 1523 here? It is not clear we should. GDB may not expect 1524 other threads to run. On the other hand, not resuming 1525 newly attached threads may cause an unwanted delay in 1526 getting them running. */ 1527 registers_changed (); 1528 child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step, signo); 1529 if (debug_lin_lwp) 1530 fprintf_unfiltered (gdb_stdlog, 1531 "LLW: %s %s, %s (preempt 'handle')\n", 1532 lp->step ? 1533 "PTRACE_SINGLESTEP" : "PTRACE_CONT", 1534 target_pid_to_str (lp->ptid), 1535 signo ? strsignal (signo) : "0"); 1536 lp->stopped = 0; 1537 status = 0; 1538 goto retry; 1539 } 1540 1541 if (signo == TARGET_SIGNAL_INT && signal_pass_state (signo) == 0) 1542 { 1543 /* If ^C/BREAK is typed at the tty/console, SIGINT gets 1544 forwarded to the entire process group, that is, all LWP's 1545 will receive it. Since we only want to report it once, 1546 we try to flush it from all LWPs except this one. */ 1547 sigaddset (&flush_mask, SIGINT); 1548 } 1549 } 1550 1551 /* This LWP is stopped now. */ 1552 lp->stopped = 1; 1553 1554 if (debug_lin_lwp) 1555 fprintf_unfiltered (gdb_stdlog, "LLW: Candidate event %s in %s.\n", 1556 status_to_str (status), target_pid_to_str (lp->ptid)); 1557 1558 /* Now stop all other LWP's ... */ 1559 iterate_over_lwps (stop_callback, NULL); 1560 1561 /* ... and wait until all of them have reported back that they're no 1562 longer running. */ 1563 iterate_over_lwps (stop_wait_callback, &flush_mask); 1564 iterate_over_lwps (flush_callback, &flush_mask); 1565 1566 /* If we're not waiting for a specific LWP, choose an event LWP from 1567 among those that have had events. Giving equal priority to all 1568 LWPs that have had events helps prevent starvation. */ 1569 if (pid == -1) 1570 select_event_lwp (&lp, &status); 1571 1572 /* Now that we've selected our final event LWP, cancel any 1573 breakpoints in other LWPs that have hit a GDB breakpoint. See 1574 the comment in cancel_breakpoints_callback to find out why. */ 1575 iterate_over_lwps (cancel_breakpoints_callback, lp); 1576 1577 /* If we're not running in "threaded" mode, we'll report the bare 1578 process id. */ 1579 1580 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP) 1581 { 1582 trap_ptid = (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid))); 1583 if (debug_lin_lwp) 1584 fprintf_unfiltered (gdb_stdlog, 1585 "LLW: trap_ptid is %s.\n", 1586 target_pid_to_str (trap_ptid)); 1587 } 1588 else 1589 trap_ptid = null_ptid; 1590 1591 /* Handle GNU/Linux's extended waitstatus for trace events. */ 1592 if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0) 1593 { 1594 linux_handle_extended_wait (ptid_get_pid (trap_ptid), 1595 status, ourstatus); 1596 return trap_ptid; 1597 } 1598 1599 store_waitstatus (ourstatus, status); 1600 return (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid))); 1601} 1602 1603static int 1604kill_callback (struct lwp_info *lp, void *data) 1605{ 1606 errno = 0; 1607 ptrace (PTRACE_KILL, GET_LWP (lp->ptid), 0, 0); 1608 if (debug_lin_lwp) 1609 fprintf_unfiltered (gdb_stdlog, 1610 "KC: PTRACE_KILL %s, 0, 0 (%s)\n", 1611 target_pid_to_str (lp->ptid), 1612 errno ? safe_strerror (errno) : "OK"); 1613 1614 return 0; 1615} 1616 1617static int 1618kill_wait_callback (struct lwp_info *lp, void *data) 1619{ 1620 pid_t pid; 1621 1622 /* We must make sure that there are no pending events (delayed 1623 SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current 1624 program doesn't interfere with any following debugging session. */ 1625 1626 /* For cloned processes we must check both with __WCLONE and 1627 without, since the exit status of a cloned process isn't reported 1628 with __WCLONE. */ 1629 if (lp->cloned) 1630 { 1631 do 1632 { 1633 pid = waitpid (GET_LWP (lp->ptid), NULL, __WCLONE); 1634 if (pid != (pid_t) -1 && debug_lin_lwp) 1635 { 1636 fprintf_unfiltered (gdb_stdlog, 1637 "KWC: wait %s received unknown.\n", 1638 target_pid_to_str (lp->ptid)); 1639 } 1640 } 1641 while (pid == GET_LWP (lp->ptid)); 1642 1643 gdb_assert (pid == -1 && errno == ECHILD); 1644 } 1645 1646 do 1647 { 1648 pid = waitpid (GET_LWP (lp->ptid), NULL, 0); 1649 if (pid != (pid_t) -1 && debug_lin_lwp) 1650 { 1651 fprintf_unfiltered (gdb_stdlog, 1652 "KWC: wait %s received unk.\n", 1653 target_pid_to_str (lp->ptid)); 1654 } 1655 } 1656 while (pid == GET_LWP (lp->ptid)); 1657 1658 gdb_assert (pid == -1 && errno == ECHILD); 1659 return 0; 1660} 1661 1662static void 1663lin_lwp_kill (void) 1664{ 1665 /* Kill all LWP's ... */ 1666 iterate_over_lwps (kill_callback, NULL); 1667 1668 /* ... and wait until we've flushed all events. */ 1669 iterate_over_lwps (kill_wait_callback, NULL); 1670 1671 target_mourn_inferior (); 1672} 1673 1674static void 1675lin_lwp_create_inferior (char *exec_file, char *allargs, char **env) 1676{ 1677 child_ops.to_create_inferior (exec_file, allargs, env); 1678} 1679 1680static void 1681lin_lwp_mourn_inferior (void) 1682{ 1683 trap_ptid = null_ptid; 1684 1685 /* Destroy LWP info; it's no longer valid. */ 1686 init_lwp_list (); 1687 1688 /* Restore the original signal mask. */ 1689 sigprocmask (SIG_SETMASK, &normal_mask, NULL); 1690 sigemptyset (&blocked_mask); 1691 1692 child_ops.to_mourn_inferior (); 1693} 1694 1695static int 1696lin_lwp_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write, 1697 struct mem_attrib *attrib, struct target_ops *target) 1698{ 1699 struct cleanup *old_chain = save_inferior_ptid (); 1700 int xfer; 1701 1702 if (is_lwp (inferior_ptid)) 1703 inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid)); 1704 1705 xfer = linux_proc_xfer_memory (memaddr, myaddr, len, write, attrib, target); 1706 if (xfer == 0) 1707 xfer = child_xfer_memory (memaddr, myaddr, len, write, attrib, target); 1708 1709 do_cleanups (old_chain); 1710 return xfer; 1711} 1712 1713static int 1714lin_lwp_thread_alive (ptid_t ptid) 1715{ 1716 gdb_assert (is_lwp (ptid)); 1717 1718 errno = 0; 1719 ptrace (PTRACE_PEEKUSER, GET_LWP (ptid), 0, 0); 1720 if (debug_lin_lwp) 1721 fprintf_unfiltered (gdb_stdlog, 1722 "LLTA: PTRACE_PEEKUSER %s, 0, 0 (%s)\n", 1723 target_pid_to_str (ptid), 1724 errno ? safe_strerror (errno) : "OK"); 1725 if (errno) 1726 return 0; 1727 1728 return 1; 1729} 1730 1731static char * 1732lin_lwp_pid_to_str (ptid_t ptid) 1733{ 1734 static char buf[64]; 1735 1736 if (is_lwp (ptid)) 1737 { 1738 snprintf (buf, sizeof (buf), "LWP %ld", GET_LWP (ptid)); 1739 return buf; 1740 } 1741 1742 return normal_pid_to_str (ptid); 1743} 1744 1745static void 1746init_lin_lwp_ops (void) 1747{ 1748#if 0 1749 lin_lwp_ops.to_open = lin_lwp_open; 1750#endif 1751 lin_lwp_ops.to_shortname = "lwp-layer"; 1752 lin_lwp_ops.to_longname = "lwp-layer"; 1753 lin_lwp_ops.to_doc = "Low level threads support (LWP layer)"; 1754 lin_lwp_ops.to_attach = lin_lwp_attach; 1755 lin_lwp_ops.to_detach = lin_lwp_detach; 1756 lin_lwp_ops.to_resume = lin_lwp_resume; 1757 lin_lwp_ops.to_wait = lin_lwp_wait; 1758 /* fetch_inferior_registers and store_inferior_registers will 1759 honor the LWP id, so we can use them directly. */ 1760 lin_lwp_ops.to_fetch_registers = fetch_inferior_registers; 1761 lin_lwp_ops.to_store_registers = store_inferior_registers; 1762 lin_lwp_ops.to_xfer_memory = lin_lwp_xfer_memory; 1763 lin_lwp_ops.to_kill = lin_lwp_kill; 1764 lin_lwp_ops.to_create_inferior = lin_lwp_create_inferior; 1765 lin_lwp_ops.to_mourn_inferior = lin_lwp_mourn_inferior; 1766 lin_lwp_ops.to_thread_alive = lin_lwp_thread_alive; 1767 lin_lwp_ops.to_pid_to_str = lin_lwp_pid_to_str; 1768 lin_lwp_ops.to_post_startup_inferior = child_post_startup_inferior; 1769 lin_lwp_ops.to_post_attach = child_post_attach; 1770 lin_lwp_ops.to_insert_fork_catchpoint = child_insert_fork_catchpoint; 1771 lin_lwp_ops.to_insert_vfork_catchpoint = child_insert_vfork_catchpoint; 1772 lin_lwp_ops.to_insert_exec_catchpoint = child_insert_exec_catchpoint; 1773 1774 lin_lwp_ops.to_stratum = thread_stratum; 1775 lin_lwp_ops.to_has_thread_control = tc_schedlock; 1776 lin_lwp_ops.to_magic = OPS_MAGIC; 1777} 1778 1779static void 1780sigchld_handler (int signo) 1781{ 1782 /* Do nothing. The only reason for this handler is that it allows 1783 us to use sigsuspend in lin_lwp_wait above to wait for the 1784 arrival of a SIGCHLD. */ 1785} 1786 1787void 1788_initialize_lin_lwp (void) 1789{ 1790 struct sigaction action; 1791 1792 extern void thread_db_init (struct target_ops *); 1793 1794 init_lin_lwp_ops (); 1795 add_target (&lin_lwp_ops); 1796 thread_db_init (&lin_lwp_ops); 1797 1798 /* Save the original signal mask. */ 1799 sigprocmask (SIG_SETMASK, NULL, &normal_mask); 1800 1801 action.sa_handler = sigchld_handler; 1802 sigemptyset (&action.sa_mask); 1803 action.sa_flags = 0; 1804 sigaction (SIGCHLD, &action, NULL); 1805 1806 /* Make sure we don't block SIGCHLD during a sigsuspend. */ 1807 sigprocmask (SIG_SETMASK, NULL, &suspend_mask); 1808 sigdelset (&suspend_mask, SIGCHLD); 1809 1810 sigemptyset (&blocked_mask); 1811 1812 add_show_from_set (add_set_cmd ("lin-lwp", no_class, var_zinteger, 1813 (char *) &debug_lin_lwp, 1814 "Set debugging of GNU/Linux lwp module.\n\ 1815Enables printf debugging output.\n", &setdebuglist), &showdebuglist); 1816} 1817 1818 1819/* FIXME: kettenis/2000-08-26: The stuff on this page is specific to 1820 the GNU/Linux Threads library and therefore doesn't really belong 1821 here. */ 1822 1823/* Read variable NAME in the target and return its value if found. 1824 Otherwise return zero. It is assumed that the type of the variable 1825 is `int'. */ 1826 1827static int 1828get_signo (const char *name) 1829{ 1830 struct minimal_symbol *ms; 1831 int signo; 1832 1833 ms = lookup_minimal_symbol (name, NULL, NULL); 1834 if (ms == NULL) 1835 return 0; 1836 1837 if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms), (char *) &signo, 1838 sizeof (signo)) != 0) 1839 return 0; 1840 1841 return signo; 1842} 1843 1844/* Return the set of signals used by the threads library in *SET. */ 1845 1846void 1847lin_thread_get_thread_signals (sigset_t *set) 1848{ 1849 struct sigaction action; 1850 int restart, cancel; 1851 1852 sigemptyset (set); 1853 1854 restart = get_signo ("__pthread_sig_restart"); 1855 if (restart == 0) 1856 return; 1857 1858 cancel = get_signo ("__pthread_sig_cancel"); 1859 if (cancel == 0) 1860 return; 1861 1862 sigaddset (set, restart); 1863 sigaddset (set, cancel); 1864 1865 /* The GNU/Linux Threads library makes terminating threads send a 1866 special "cancel" signal instead of SIGCHLD. Make sure we catch 1867 those (to prevent them from terminating GDB itself, which is 1868 likely to be their default action) and treat them the same way as 1869 SIGCHLD. */ 1870 1871 action.sa_handler = sigchld_handler; 1872 sigemptyset (&action.sa_mask); 1873 action.sa_flags = 0; 1874 sigaction (cancel, &action, NULL); 1875 1876 /* We block the "cancel" signal throughout this code ... */ 1877 sigaddset (&blocked_mask, cancel); 1878 sigprocmask (SIG_BLOCK, &blocked_mask, NULL); 1879 1880 /* ... except during a sigsuspend. */ 1881 sigdelset (&suspend_mask, cancel); 1882} 1883