Home | History | Annotate | Line # | Download | only in gdb
linux-nat.c revision 1.6
      1  1.1  christos /* GNU/Linux native-dependent code common to multiple platforms.
      2  1.1  christos 
      3  1.6  christos    Copyright (C) 2001-2016 Free Software Foundation, Inc.
      4  1.1  christos 
      5  1.1  christos    This file is part of GDB.
      6  1.1  christos 
      7  1.1  christos    This program is free software; you can redistribute it and/or modify
      8  1.1  christos    it under the terms of the GNU General Public License as published by
      9  1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10  1.1  christos    (at your option) any later version.
     11  1.1  christos 
     12  1.1  christos    This program is distributed in the hope that it will be useful,
     13  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15  1.1  christos    GNU General Public License for more details.
     16  1.1  christos 
     17  1.1  christos    You should have received a copy of the GNU General Public License
     18  1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19  1.1  christos 
     20  1.1  christos #include "defs.h"
     21  1.1  christos #include "inferior.h"
     22  1.3  christos #include "infrun.h"
     23  1.1  christos #include "target.h"
     24  1.1  christos #include "nat/linux-nat.h"
     25  1.1  christos #include "nat/linux-waitpid.h"
     26  1.1  christos #include "gdb_wait.h"
     27  1.1  christos #include <unistd.h>
     28  1.1  christos #include <sys/syscall.h>
     29  1.6  christos #include "nat/gdb_ptrace.h"
     30  1.1  christos #include "linux-nat.h"
     31  1.3  christos #include "nat/linux-ptrace.h"
     32  1.3  christos #include "nat/linux-procfs.h"
     33  1.5  christos #include "nat/linux-personality.h"
     34  1.1  christos #include "linux-fork.h"
     35  1.1  christos #include "gdbthread.h"
     36  1.1  christos #include "gdbcmd.h"
     37  1.1  christos #include "regcache.h"
     38  1.1  christos #include "regset.h"
     39  1.1  christos #include "inf-child.h"
     40  1.1  christos #include "inf-ptrace.h"
     41  1.1  christos #include "auxv.h"
     42  1.1  christos #include <sys/procfs.h>		/* for elf_gregset etc.  */
     43  1.1  christos #include "elf-bfd.h"		/* for elfcore_write_* */
     44  1.1  christos #include "gregset.h"		/* for gregset */
     45  1.1  christos #include "gdbcore.h"		/* for get_exec_file */
     46  1.1  christos #include <ctype.h>		/* for isdigit */
     47  1.1  christos #include <sys/stat.h>		/* for struct stat */
     48  1.1  christos #include <fcntl.h>		/* for O_RDONLY */
     49  1.1  christos #include "inf-loop.h"
     50  1.1  christos #include "event-loop.h"
     51  1.1  christos #include "event-top.h"
     52  1.1  christos #include <pwd.h>
     53  1.1  christos #include <sys/types.h>
     54  1.1  christos #include <dirent.h>
     55  1.1  christos #include "xml-support.h"
     56  1.1  christos #include <sys/vfs.h>
     57  1.1  christos #include "solib.h"
     58  1.3  christos #include "nat/linux-osdata.h"
     59  1.1  christos #include "linux-tdep.h"
     60  1.1  christos #include "symfile.h"
     61  1.1  christos #include "agent.h"
     62  1.1  christos #include "tracepoint.h"
     63  1.1  christos #include "buffer.h"
     64  1.1  christos #include "target-descriptions.h"
     65  1.1  christos #include "filestuff.h"
     66  1.3  christos #include "objfiles.h"
     67  1.5  christos #include "nat/linux-namespaces.h"
     68  1.5  christos #include "fileio.h"
     69  1.1  christos 
     70  1.1  christos #ifndef SPUFS_MAGIC
     71  1.1  christos #define SPUFS_MAGIC 0x23c9b64e
     72  1.1  christos #endif
     73  1.1  christos 
     74  1.1  christos /* This comment documents high-level logic of this file.
     75  1.1  christos 
     76  1.1  christos Waiting for events in sync mode
     77  1.1  christos ===============================
     78  1.1  christos 
     79  1.6  christos When waiting for an event in a specific thread, we just use waitpid,
     80  1.6  christos passing the specific pid, and not passing WNOHANG.
     81  1.6  christos 
     82  1.6  christos When waiting for an event in all threads, waitpid is not quite good:
     83  1.6  christos 
     84  1.6  christos - If the thread group leader exits while other threads in the thread
     85  1.6  christos   group still exist, waitpid(TGID, ...) hangs.  That waitpid won't
     86  1.6  christos   return an exit status until the other threads in the group are
     87  1.6  christos   reaped.
     88  1.6  christos 
     89  1.6  christos - When a non-leader thread execs, that thread just vanishes without
     90  1.6  christos   reporting an exit (so we'd hang if we waited for it explicitly in
     91  1.6  christos   that case).  The exec event is instead reported to the TGID pid.
     92  1.1  christos 
     93  1.6  christos The solution is to always use -1 and WNOHANG, together with
     94  1.6  christos sigsuspend.
     95  1.6  christos 
     96  1.6  christos First, we use non-blocking waitpid to check for events.  If nothing is
     97  1.6  christos found, we use sigsuspend to wait for SIGCHLD.  When SIGCHLD arrives,
     98  1.6  christos it means something happened to a child process.  As soon as we know
     99  1.6  christos there's an event, we get back to calling nonblocking waitpid.
    100  1.6  christos 
    101  1.6  christos Note that SIGCHLD should be blocked between waitpid and sigsuspend
    102  1.6  christos calls, so that we don't miss a signal.  If SIGCHLD arrives in between,
    103  1.6  christos when it's blocked, the signal becomes pending and sigsuspend
    104  1.6  christos immediately notices it and returns.
    105  1.1  christos 
    106  1.6  christos Waiting for events in async mode (TARGET_WNOHANG)
    107  1.6  christos =================================================
    108  1.1  christos 
    109  1.1  christos In async mode, GDB should always be ready to handle both user input
    110  1.1  christos and target events, so neither blocking waitpid nor sigsuspend are
    111  1.1  christos viable options.  Instead, we should asynchronously notify the GDB main
    112  1.1  christos event loop whenever there's an unprocessed event from the target.  We
    113  1.1  christos detect asynchronous target events by handling SIGCHLD signals.  To
    114  1.1  christos notify the event loop about target events, the self-pipe trick is used
    115  1.1  christos --- a pipe is registered as waitable event source in the event loop,
    116  1.1  christos the event loop select/poll's on the read end of this pipe (as well on
    117  1.1  christos other event sources, e.g., stdin), and the SIGCHLD handler writes a
    118  1.1  christos byte to this pipe.  This is more portable than relying on
    119  1.1  christos pselect/ppoll, since on kernels that lack those syscalls, libc
    120  1.1  christos emulates them with select/poll+sigprocmask, and that is racy
    121  1.1  christos (a.k.a. plain broken).
    122  1.1  christos 
    123  1.1  christos Obviously, if we fail to notify the event loop if there's a target
    124  1.1  christos event, it's bad.  OTOH, if we notify the event loop when there's no
    125  1.1  christos event from the target, linux_nat_wait will detect that there's no real
    126  1.1  christos event to report, and return event of type TARGET_WAITKIND_IGNORE.
    127  1.1  christos This is mostly harmless, but it will waste time and is better avoided.
    128  1.1  christos 
    129  1.1  christos The main design point is that every time GDB is outside linux-nat.c,
    130  1.1  christos we have a SIGCHLD handler installed that is called when something
    131  1.1  christos happens to the target and notifies the GDB event loop.  Whenever GDB
    132  1.1  christos core decides to handle the event, and calls into linux-nat.c, we
    133  1.1  christos process things as in sync mode, except that the we never block in
    134  1.1  christos sigsuspend.
    135  1.1  christos 
    136  1.1  christos While processing an event, we may end up momentarily blocked in
    137  1.1  christos waitpid calls.  Those waitpid calls, while blocking, are guarantied to
    138  1.1  christos return quickly.  E.g., in all-stop mode, before reporting to the core
    139  1.1  christos that an LWP hit a breakpoint, all LWPs are stopped by sending them
    140  1.1  christos SIGSTOP, and synchronously waiting for the SIGSTOP to be reported.
    141  1.1  christos Note that this is different from blocking indefinitely waiting for the
    142  1.1  christos next event --- here, we're already handling an event.
    143  1.1  christos 
    144  1.1  christos Use of signals
    145  1.1  christos ==============
    146  1.1  christos 
    147  1.1  christos We stop threads by sending a SIGSTOP.  The use of SIGSTOP instead of another
    148  1.1  christos signal is not entirely significant; we just need for a signal to be delivered,
    149  1.1  christos so that we can intercept it.  SIGSTOP's advantage is that it can not be
    150  1.1  christos blocked.  A disadvantage is that it is not a real-time signal, so it can only
    151  1.1  christos be queued once; we do not keep track of other sources of SIGSTOP.
    152  1.1  christos 
    153  1.1  christos Two other signals that can't be blocked are SIGCONT and SIGKILL.  But we can't
    154  1.1  christos use them, because they have special behavior when the signal is generated -
    155  1.1  christos not when it is delivered.  SIGCONT resumes the entire thread group and SIGKILL
    156  1.1  christos kills the entire thread group.
    157  1.1  christos 
    158  1.1  christos A delivered SIGSTOP would stop the entire thread group, not just the thread we
    159  1.1  christos tkill'd.  But we never let the SIGSTOP be delivered; we always intercept and
    160  1.1  christos cancel it (by PTRACE_CONT without passing SIGSTOP).
    161  1.1  christos 
    162  1.1  christos We could use a real-time signal instead.  This would solve those problems; we
    163  1.1  christos could use PTRACE_GETSIGINFO to locate the specific stop signals sent by GDB.
    164  1.1  christos But we would still have to have some support for SIGSTOP, since PTRACE_ATTACH
    165  1.1  christos generates it, and there are races with trying to find a signal that is not
    166  1.6  christos blocked.
    167  1.6  christos 
    168  1.6  christos Exec events
    169  1.6  christos ===========
    170  1.6  christos 
    171  1.6  christos The case of a thread group (process) with 3 or more threads, and a
    172  1.6  christos thread other than the leader execs is worth detailing:
    173  1.6  christos 
    174  1.6  christos On an exec, the Linux kernel destroys all threads except the execing
    175  1.6  christos one in the thread group, and resets the execing thread's tid to the
    176  1.6  christos tgid.  No exit notification is sent for the execing thread -- from the
    177  1.6  christos ptracer's perspective, it appears as though the execing thread just
    178  1.6  christos vanishes.  Until we reap all other threads except the leader and the
    179  1.6  christos execing thread, the leader will be zombie, and the execing thread will
    180  1.6  christos be in `D (disc sleep)' state.  As soon as all other threads are
    181  1.6  christos reaped, the execing thread changes its tid to the tgid, and the
    182  1.6  christos previous (zombie) leader vanishes, giving place to the "new"
    183  1.6  christos leader.  */
    184  1.1  christos 
    185  1.1  christos #ifndef O_LARGEFILE
    186  1.1  christos #define O_LARGEFILE 0
    187  1.1  christos #endif
    188  1.1  christos 
    189  1.5  christos /* Does the current host support PTRACE_GETREGSET?  */
    190  1.5  christos enum tribool have_ptrace_getregset = TRIBOOL_UNKNOWN;
    191  1.5  christos 
    192  1.1  christos /* The single-threaded native GNU/Linux target_ops.  We save a pointer for
    193  1.1  christos    the use of the multi-threaded target.  */
    194  1.1  christos static struct target_ops *linux_ops;
    195  1.1  christos static struct target_ops linux_ops_saved;
    196  1.1  christos 
    197  1.1  christos /* The method to call, if any, when a new thread is attached.  */
    198  1.1  christos static void (*linux_nat_new_thread) (struct lwp_info *);
    199  1.1  christos 
    200  1.1  christos /* The method to call, if any, when a new fork is attached.  */
    201  1.1  christos static linux_nat_new_fork_ftype *linux_nat_new_fork;
    202  1.1  christos 
    203  1.1  christos /* The method to call, if any, when a process is no longer
    204  1.1  christos    attached.  */
    205  1.1  christos static linux_nat_forget_process_ftype *linux_nat_forget_process_hook;
    206  1.1  christos 
    207  1.1  christos /* Hook to call prior to resuming a thread.  */
    208  1.1  christos static void (*linux_nat_prepare_to_resume) (struct lwp_info *);
    209  1.1  christos 
    210  1.1  christos /* The method to call, if any, when the siginfo object needs to be
    211  1.1  christos    converted between the layout returned by ptrace, and the layout in
    212  1.1  christos    the architecture of the inferior.  */
    213  1.1  christos static int (*linux_nat_siginfo_fixup) (siginfo_t *,
    214  1.1  christos 				       gdb_byte *,
    215  1.1  christos 				       int);
    216  1.1  christos 
    217  1.1  christos /* The saved to_xfer_partial method, inherited from inf-ptrace.c.
    218  1.1  christos    Called by our to_xfer_partial.  */
    219  1.1  christos static target_xfer_partial_ftype *super_xfer_partial;
    220  1.1  christos 
    221  1.3  christos /* The saved to_close method, inherited from inf-ptrace.c.
    222  1.3  christos    Called by our to_close.  */
    223  1.3  christos static void (*super_close) (struct target_ops *);
    224  1.3  christos 
    225  1.1  christos static unsigned int debug_linux_nat;
    226  1.1  christos static void
    227  1.1  christos show_debug_linux_nat (struct ui_file *file, int from_tty,
    228  1.1  christos 		      struct cmd_list_element *c, const char *value)
    229  1.1  christos {
    230  1.1  christos   fprintf_filtered (file, _("Debugging of GNU/Linux lwp module is %s.\n"),
    231  1.1  christos 		    value);
    232  1.1  christos }
    233  1.1  christos 
    234  1.1  christos struct simple_pid_list
    235  1.1  christos {
    236  1.1  christos   int pid;
    237  1.1  christos   int status;
    238  1.1  christos   struct simple_pid_list *next;
    239  1.1  christos };
    240  1.1  christos struct simple_pid_list *stopped_pids;
    241  1.1  christos 
    242  1.6  christos /* Whether target_thread_events is in effect.  */
    243  1.6  christos static int report_thread_events;
    244  1.6  christos 
    245  1.1  christos /* Async mode support.  */
    246  1.1  christos 
    247  1.1  christos /* The read/write ends of the pipe registered as waitable file in the
    248  1.1  christos    event loop.  */
    249  1.1  christos static int linux_nat_event_pipe[2] = { -1, -1 };
    250  1.1  christos 
    251  1.3  christos /* True if we're currently in async mode.  */
    252  1.3  christos #define linux_is_async_p() (linux_nat_event_pipe[0] != -1)
    253  1.3  christos 
    254  1.1  christos /* Flush the event pipe.  */
    255  1.1  christos 
    256  1.1  christos static void
    257  1.1  christos async_file_flush (void)
    258  1.1  christos {
    259  1.1  christos   int ret;
    260  1.1  christos   char buf;
    261  1.1  christos 
    262  1.1  christos   do
    263  1.1  christos     {
    264  1.1  christos       ret = read (linux_nat_event_pipe[0], &buf, 1);
    265  1.1  christos     }
    266  1.1  christos   while (ret >= 0 || (ret == -1 && errno == EINTR));
    267  1.1  christos }
    268  1.1  christos 
    269  1.1  christos /* Put something (anything, doesn't matter what, or how much) in event
    270  1.1  christos    pipe, so that the select/poll in the event-loop realizes we have
    271  1.1  christos    something to process.  */
    272  1.1  christos 
    273  1.1  christos static void
    274  1.1  christos async_file_mark (void)
    275  1.1  christos {
    276  1.1  christos   int ret;
    277  1.1  christos 
    278  1.1  christos   /* It doesn't really matter what the pipe contains, as long we end
    279  1.1  christos      up with something in it.  Might as well flush the previous
    280  1.1  christos      left-overs.  */
    281  1.1  christos   async_file_flush ();
    282  1.1  christos 
    283  1.1  christos   do
    284  1.1  christos     {
    285  1.1  christos       ret = write (linux_nat_event_pipe[1], "+", 1);
    286  1.1  christos     }
    287  1.1  christos   while (ret == -1 && errno == EINTR);
    288  1.1  christos 
    289  1.1  christos   /* Ignore EAGAIN.  If the pipe is full, the event loop will already
    290  1.1  christos      be awakened anyway.  */
    291  1.1  christos }
    292  1.1  christos 
    293  1.1  christos static int kill_lwp (int lwpid, int signo);
    294  1.1  christos 
    295  1.1  christos static int stop_callback (struct lwp_info *lp, void *data);
    296  1.5  christos static int resume_stopped_resumed_lwps (struct lwp_info *lp, void *data);
    297  1.1  christos 
    298  1.1  christos static void block_child_signals (sigset_t *prev_mask);
    299  1.1  christos static void restore_child_signals_mask (sigset_t *prev_mask);
    300  1.1  christos 
    301  1.1  christos struct lwp_info;
    302  1.1  christos static struct lwp_info *add_lwp (ptid_t ptid);
    303  1.1  christos static void purge_lwp_list (int pid);
    304  1.1  christos static void delete_lwp (ptid_t ptid);
    305  1.1  christos static struct lwp_info *find_lwp_pid (ptid_t ptid);
    306  1.1  christos 
    307  1.3  christos static int lwp_status_pending_p (struct lwp_info *lp);
    308  1.3  christos 
    309  1.3  christos static int sigtrap_is_event (int status);
    310  1.3  christos static int (*linux_nat_status_is_event) (int status) = sigtrap_is_event;
    311  1.3  christos 
    312  1.6  christos static void save_stop_reason (struct lwp_info *lp);
    313  1.6  christos 
    314  1.1  christos 
    315  1.5  christos /* LWP accessors.  */
    317  1.5  christos 
    318  1.5  christos /* See nat/linux-nat.h.  */
    319  1.5  christos 
    320  1.5  christos ptid_t
    321  1.5  christos ptid_of_lwp (struct lwp_info *lwp)
    322  1.5  christos {
    323  1.5  christos   return lwp->ptid;
    324  1.5  christos }
    325  1.5  christos 
    326  1.5  christos /* See nat/linux-nat.h.  */
    327  1.5  christos 
    328  1.5  christos void
    329  1.5  christos lwp_set_arch_private_info (struct lwp_info *lwp,
    330  1.5  christos 			   struct arch_lwp_info *info)
    331  1.5  christos {
    332  1.5  christos   lwp->arch_private = info;
    333  1.5  christos }
    334  1.5  christos 
    335  1.5  christos /* See nat/linux-nat.h.  */
    336  1.5  christos 
    337  1.5  christos struct arch_lwp_info *
    338  1.5  christos lwp_arch_private_info (struct lwp_info *lwp)
    339  1.5  christos {
    340  1.5  christos   return lwp->arch_private;
    341  1.5  christos }
    342  1.5  christos 
    343  1.5  christos /* See nat/linux-nat.h.  */
    344  1.5  christos 
    345  1.5  christos int
    346  1.5  christos lwp_is_stopped (struct lwp_info *lwp)
    347  1.5  christos {
    348  1.5  christos   return lwp->stopped;
    349  1.5  christos }
    350  1.5  christos 
    351  1.5  christos /* See nat/linux-nat.h.  */
    352  1.5  christos 
    353  1.5  christos enum target_stop_reason
    354  1.5  christos lwp_stop_reason (struct lwp_info *lwp)
    355  1.5  christos {
    356  1.5  christos   return lwp->stop_reason;
    357  1.5  christos }
    358  1.5  christos 
    359  1.1  christos 
    360  1.1  christos /* Trivial list manipulation functions to keep track of a list of
    362  1.1  christos    new stopped processes.  */
    363  1.1  christos static void
    364  1.6  christos add_to_pid_list (struct simple_pid_list **listp, int pid, int status)
    365  1.1  christos {
    366  1.1  christos   struct simple_pid_list *new_pid = XNEW (struct simple_pid_list);
    367  1.1  christos 
    368  1.1  christos   new_pid->pid = pid;
    369  1.1  christos   new_pid->status = status;
    370  1.1  christos   new_pid->next = *listp;
    371  1.1  christos   *listp = new_pid;
    372  1.1  christos }
    373  1.1  christos 
    374  1.1  christos static int
    375  1.1  christos pull_pid_from_list (struct simple_pid_list **listp, int pid, int *statusp)
    376  1.1  christos {
    377  1.1  christos   struct simple_pid_list **p;
    378  1.1  christos 
    379  1.1  christos   for (p = listp; *p != NULL; p = &(*p)->next)
    380  1.1  christos     if ((*p)->pid == pid)
    381  1.1  christos       {
    382  1.1  christos 	struct simple_pid_list *next = (*p)->next;
    383  1.1  christos 
    384  1.1  christos 	*statusp = (*p)->status;
    385  1.1  christos 	xfree (*p);
    386  1.1  christos 	*p = next;
    387  1.1  christos 	return 1;
    388  1.1  christos       }
    389  1.1  christos   return 0;
    390  1.5  christos }
    391  1.5  christos 
    392  1.5  christos /* Return the ptrace options that we want to try to enable.  */
    393  1.5  christos 
    394  1.5  christos static int
    395  1.5  christos linux_nat_ptrace_options (int attached)
    396  1.5  christos {
    397  1.5  christos   int options = 0;
    398  1.5  christos 
    399  1.5  christos   if (!attached)
    400  1.5  christos     options |= PTRACE_O_EXITKILL;
    401  1.5  christos 
    402  1.5  christos   options |= (PTRACE_O_TRACESYSGOOD
    403  1.5  christos 	      | PTRACE_O_TRACEVFORKDONE
    404  1.5  christos 	      | PTRACE_O_TRACEVFORK
    405  1.5  christos 	      | PTRACE_O_TRACEFORK
    406  1.5  christos 	      | PTRACE_O_TRACEEXEC);
    407  1.5  christos 
    408  1.5  christos   return options;
    409  1.1  christos }
    410  1.3  christos 
    411  1.3  christos /* Initialize ptrace warnings and check for supported ptrace
    412  1.3  christos    features given PID.
    413  1.1  christos 
    414  1.1  christos    ATTACHED should be nonzero iff we attached to the inferior.  */
    415  1.3  christos 
    416  1.1  christos static void
    417  1.5  christos linux_init_ptrace (pid_t pid, int attached)
    418  1.5  christos {
    419  1.5  christos   int options = linux_nat_ptrace_options (attached);
    420  1.1  christos 
    421  1.1  christos   linux_enable_event_reporting (pid, options);
    422  1.1  christos   linux_ptrace_init_warnings ();
    423  1.1  christos }
    424  1.3  christos 
    425  1.1  christos static void
    426  1.3  christos linux_child_post_attach (struct target_ops *self, int pid)
    427  1.1  christos {
    428  1.1  christos   linux_init_ptrace (pid, 1);
    429  1.1  christos }
    430  1.3  christos 
    431  1.1  christos static void
    432  1.3  christos linux_child_post_startup_inferior (struct target_ops *self, ptid_t ptid)
    433  1.1  christos {
    434  1.1  christos   linux_init_ptrace (ptid_get_pid (ptid), 0);
    435  1.1  christos }
    436  1.1  christos 
    437  1.1  christos /* Return the number of known LWPs in the tgid given by PID.  */
    438  1.1  christos 
    439  1.1  christos static int
    440  1.1  christos num_lwps (int pid)
    441  1.1  christos {
    442  1.1  christos   int count = 0;
    443  1.1  christos   struct lwp_info *lp;
    444  1.1  christos 
    445  1.1  christos   for (lp = lwp_list; lp; lp = lp->next)
    446  1.1  christos     if (ptid_get_pid (lp->ptid) == pid)
    447  1.1  christos       count++;
    448  1.1  christos 
    449  1.1  christos   return count;
    450  1.1  christos }
    451  1.1  christos 
    452  1.1  christos /* Call delete_lwp with prototype compatible for make_cleanup.  */
    453  1.1  christos 
    454  1.1  christos static void
    455  1.6  christos delete_lwp_cleanup (void *lp_voidp)
    456  1.1  christos {
    457  1.1  christos   struct lwp_info *lp = (struct lwp_info *) lp_voidp;
    458  1.1  christos 
    459  1.1  christos   delete_lwp (lp->ptid);
    460  1.3  christos }
    461  1.3  christos 
    462  1.3  christos /* Target hook for follow_fork.  On entry inferior_ptid must be the
    463  1.3  christos    ptid of the followed inferior.  At return, inferior_ptid will be
    464  1.1  christos    unchanged.  */
    465  1.1  christos 
    466  1.1  christos static int
    467  1.1  christos linux_child_follow_fork (struct target_ops *ops, int follow_child,
    468  1.3  christos 			 int detach_fork)
    469  1.1  christos {
    470  1.1  christos   if (!follow_child)
    471  1.3  christos     {
    472  1.3  christos       struct lwp_info *child_lp = NULL;
    473  1.3  christos       int status = W_STOPCODE (0);
    474  1.5  christos       struct cleanup *old_chain;
    475  1.3  christos       int has_vforked;
    476  1.3  christos       ptid_t parent_ptid, child_ptid;
    477  1.3  christos       int parent_pid, child_pid;
    478  1.3  christos 
    479  1.5  christos       has_vforked = (inferior_thread ()->pending_follow.kind
    480  1.5  christos 		     == TARGET_WAITKIND_VFORKED);
    481  1.5  christos       parent_ptid = inferior_ptid;
    482  1.5  christos       child_ptid = inferior_thread ()->pending_follow.value.related_pid;
    483  1.1  christos       parent_pid = ptid_get_lwp (parent_ptid);
    484  1.1  christos       child_pid = ptid_get_lwp (child_ptid);
    485  1.3  christos 
    486  1.5  christos       /* We're already attached to the parent, by default.  */
    487  1.3  christos       old_chain = save_inferior_ptid ();
    488  1.3  christos       inferior_ptid = child_ptid;
    489  1.3  christos       child_lp = add_lwp (inferior_ptid);
    490  1.1  christos       child_lp->stopped = 1;
    491  1.1  christos       child_lp->last_resume_kind = resume_stop;
    492  1.1  christos 
    493  1.1  christos       /* Detach new forked process?  */
    494  1.3  christos       if (detach_fork)
    495  1.3  christos 	{
    496  1.3  christos 	  make_cleanup (delete_lwp_cleanup, child_lp);
    497  1.3  christos 
    498  1.1  christos 	  if (linux_nat_prepare_to_resume != NULL)
    499  1.3  christos 	    linux_nat_prepare_to_resume (child_lp);
    500  1.3  christos 
    501  1.3  christos 	  /* When debugging an inferior in an architecture that supports
    502  1.3  christos 	     hardware single stepping on a kernel without commit
    503  1.3  christos 	     6580807da14c423f0d0a708108e6df6ebc8bc83d, the vfork child
    504  1.3  christos 	     process starts with the TIF_SINGLESTEP/X86_EFLAGS_TF bits
    505  1.3  christos 	     set if the parent process had them set.
    506  1.3  christos 	     To work around this, single step the child process
    507  1.3  christos 	     once before detaching to clear the flags.  */
    508  1.3  christos 
    509  1.1  christos 	  if (!gdbarch_software_single_step_p (target_thread_architecture
    510  1.3  christos 						   (child_lp->ptid)))
    511  1.3  christos 	    {
    512  1.3  christos 	      linux_disable_event_reporting (child_pid);
    513  1.3  christos 	      if (ptrace (PTRACE_SINGLESTEP, child_pid, 0, 0) < 0)
    514  1.3  christos 		perror_with_name (_("Couldn't do single step"));
    515  1.1  christos 	      if (my_waitpid (child_pid, &status, 0) < 0)
    516  1.1  christos 		perror_with_name (_("Couldn't wait vfork process"));
    517  1.3  christos 	    }
    518  1.1  christos 
    519  1.3  christos 	  if (WIFSTOPPED (status))
    520  1.3  christos 	    {
    521  1.3  christos 	      int signo;
    522  1.3  christos 
    523  1.3  christos 	      signo = WSTOPSIG (status);
    524  1.3  christos 	      if (signo != 0
    525  1.3  christos 		  && !signal_pass_state (gdb_signal_from_host (signo)))
    526  1.1  christos 		signo = 0;
    527  1.1  christos 	      ptrace (PTRACE_DETACH, child_pid, 0, signo);
    528  1.3  christos 	    }
    529  1.1  christos 
    530  1.1  christos 	  /* Resets value of inferior_ptid to parent ptid.  */
    531  1.1  christos 	  do_cleanups (old_chain);
    532  1.1  christos 	}
    533  1.1  christos       else
    534  1.1  christos 	{
    535  1.3  christos 	  /* Let the thread_db layer learn about this new process.  */
    536  1.1  christos 	  check_for_thread_db ();
    537  1.3  christos 	}
    538  1.1  christos 
    539  1.1  christos       do_cleanups (old_chain);
    540  1.1  christos 
    541  1.1  christos       if (has_vforked)
    542  1.1  christos 	{
    543  1.5  christos 	  struct lwp_info *parent_lp;
    544  1.1  christos 
    545  1.1  christos 	  parent_lp = find_lwp_pid (parent_ptid);
    546  1.1  christos 	  gdb_assert (linux_supports_tracefork () >= 0);
    547  1.1  christos 
    548  1.1  christos 	  if (linux_supports_tracevforkdone ())
    549  1.1  christos 	    {
    550  1.1  christos   	      if (debug_linux_nat)
    551  1.1  christos   		fprintf_unfiltered (gdb_stdlog,
    552  1.1  christos   				    "LCFF: waiting for VFORK_DONE on %d\n",
    553  1.1  christos   				    parent_pid);
    554  1.1  christos 	      parent_lp->stopped = 1;
    555  1.1  christos 
    556  1.1  christos 	      /* We'll handle the VFORK_DONE event like any other
    557  1.1  christos 		 event, in target_wait.  */
    558  1.1  christos 	    }
    559  1.1  christos 	  else
    560  1.1  christos 	    {
    561  1.1  christos 	      /* We can't insert breakpoints until the child has
    562  1.1  christos 		 finished with the shared memory region.  We need to
    563  1.1  christos 		 wait until that happens.  Ideal would be to just
    564  1.1  christos 		 call:
    565  1.1  christos 		 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
    566  1.1  christos 		 - waitpid (parent_pid, &status, __WALL);
    567  1.1  christos 		 However, most architectures can't handle a syscall
    568  1.1  christos 		 being traced on the way out if it wasn't traced on
    569  1.1  christos 		 the way in.
    570  1.1  christos 
    571  1.1  christos 		 We might also think to loop, continuing the child
    572  1.1  christos 		 until it exits or gets a SIGTRAP.  One problem is
    573  1.1  christos 		 that the child might call ptrace with PTRACE_TRACEME.
    574  1.1  christos 
    575  1.1  christos 		 There's no simple and reliable way to figure out when
    576  1.1  christos 		 the vforked child will be done with its copy of the
    577  1.1  christos 		 shared memory.  We could step it out of the syscall,
    578  1.1  christos 		 two instructions, let it go, and then single-step the
    579  1.1  christos 		 parent once.  When we have hardware single-step, this
    580  1.1  christos 		 would work; with software single-step it could still
    581  1.1  christos 		 be made to work but we'd have to be able to insert
    582  1.1  christos 		 single-step breakpoints in the child, and we'd have
    583  1.1  christos 		 to insert -just- the single-step breakpoint in the
    584  1.1  christos 		 parent.  Very awkward.
    585  1.1  christos 
    586  1.1  christos 		 In the end, the best we can do is to make sure it
    587  1.1  christos 		 runs for a little while.  Hopefully it will be out of
    588  1.1  christos 		 range of any breakpoints we reinsert.  Usually this
    589  1.1  christos 		 is only the single-step breakpoint at vfork's return
    590  1.1  christos 		 point.  */
    591  1.1  christos 
    592  1.1  christos   	      if (debug_linux_nat)
    593  1.1  christos   		fprintf_unfiltered (gdb_stdlog,
    594  1.1  christos 				    "LCFF: no VFORK_DONE "
    595  1.1  christos 				    "support, sleeping a bit\n");
    596  1.1  christos 
    597  1.1  christos 	      usleep (10000);
    598  1.1  christos 
    599  1.1  christos 	      /* Pretend we've seen a PTRACE_EVENT_VFORK_DONE event,
    600  1.1  christos 		 and leave it pending.  The next linux_nat_resume call
    601  1.1  christos 		 will notice a pending event, and bypasses actually
    602  1.1  christos 		 resuming the inferior.  */
    603  1.1  christos 	      parent_lp->status = 0;
    604  1.1  christos 	      parent_lp->waitstatus.kind = TARGET_WAITKIND_VFORK_DONE;
    605  1.1  christos 	      parent_lp->stopped = 1;
    606  1.1  christos 
    607  1.3  christos 	      /* If we're in async mode, need to tell the event loop
    608  1.1  christos 		 there's something here to process.  */
    609  1.1  christos 	      if (target_is_async_p ())
    610  1.1  christos 		async_file_mark ();
    611  1.1  christos 	    }
    612  1.1  christos 	}
    613  1.1  christos     }
    614  1.1  christos   else
    615  1.1  christos     {
    616  1.1  christos       struct lwp_info *child_lp;
    617  1.1  christos 
    618  1.1  christos       child_lp = add_lwp (inferior_ptid);
    619  1.1  christos       child_lp->stopped = 1;
    620  1.1  christos       child_lp->last_resume_kind = resume_stop;
    621  1.1  christos 
    622  1.1  christos       /* Let the thread_db layer learn about this new process.  */
    623  1.1  christos       check_for_thread_db ();
    624  1.1  christos     }
    625  1.1  christos 
    626  1.1  christos   return 0;
    627  1.1  christos }
    628  1.1  christos 
    629  1.3  christos 
    630  1.1  christos static int
    632  1.1  christos linux_child_insert_fork_catchpoint (struct target_ops *self, int pid)
    633  1.1  christos {
    634  1.1  christos   return !linux_supports_tracefork ();
    635  1.3  christos }
    636  1.1  christos 
    637  1.1  christos static int
    638  1.1  christos linux_child_remove_fork_catchpoint (struct target_ops *self, int pid)
    639  1.1  christos {
    640  1.1  christos   return 0;
    641  1.3  christos }
    642  1.1  christos 
    643  1.1  christos static int
    644  1.1  christos linux_child_insert_vfork_catchpoint (struct target_ops *self, int pid)
    645  1.1  christos {
    646  1.1  christos   return !linux_supports_tracefork ();
    647  1.3  christos }
    648  1.1  christos 
    649  1.1  christos static int
    650  1.1  christos linux_child_remove_vfork_catchpoint (struct target_ops *self, int pid)
    651  1.1  christos {
    652  1.1  christos   return 0;
    653  1.3  christos }
    654  1.1  christos 
    655  1.1  christos static int
    656  1.1  christos linux_child_insert_exec_catchpoint (struct target_ops *self, int pid)
    657  1.1  christos {
    658  1.1  christos   return !linux_supports_tracefork ();
    659  1.3  christos }
    660  1.1  christos 
    661  1.1  christos static int
    662  1.1  christos linux_child_remove_exec_catchpoint (struct target_ops *self, int pid)
    663  1.1  christos {
    664  1.1  christos   return 0;
    665  1.3  christos }
    666  1.3  christos 
    667  1.1  christos static int
    668  1.1  christos linux_child_set_syscall_catchpoint (struct target_ops *self,
    669  1.1  christos 				    int pid, int needed, int any_count,
    670  1.1  christos 				    int table_size, int *table)
    671  1.1  christos {
    672  1.1  christos   if (!linux_supports_tracesysgood ())
    673  1.1  christos     return 1;
    674  1.1  christos 
    675  1.1  christos   /* On GNU/Linux, we ignore the arguments.  It means that we only
    676  1.1  christos      enable the syscall catchpoints, but do not disable them.
    677  1.1  christos 
    678  1.1  christos      Also, we do not use the `table' information because we do not
    679  1.1  christos      filter system calls here.  We let GDB do the logic for us.  */
    680  1.6  christos   return 0;
    681  1.6  christos }
    682  1.6  christos 
    683  1.6  christos /* List of known LWPs, keyed by LWP PID.  This speeds up the common
    684  1.6  christos    case of mapping a PID returned from the kernel to our corresponding
    685  1.6  christos    lwp_info data structure.  */
    686  1.6  christos static htab_t lwp_lwpid_htab;
    687  1.6  christos 
    688  1.6  christos /* Calculate a hash from a lwp_info's LWP PID.  */
    689  1.6  christos 
    690  1.6  christos static hashval_t
    691  1.6  christos lwp_info_hash (const void *ap)
    692  1.6  christos {
    693  1.6  christos   const struct lwp_info *lp = (struct lwp_info *) ap;
    694  1.6  christos   pid_t pid = ptid_get_lwp (lp->ptid);
    695  1.6  christos 
    696  1.6  christos   return iterative_hash_object (pid, 0);
    697  1.6  christos }
    698  1.1  christos 
    699  1.6  christos /* Equality function for the lwp_info hash table.  Compares the LWP's
    700  1.6  christos    PID.  */
    701  1.6  christos 
    702  1.6  christos static int
    703  1.6  christos lwp_lwpid_htab_eq (const void *a, const void *b)
    704  1.6  christos {
    705  1.6  christos   const struct lwp_info *entry = (const struct lwp_info *) a;
    706  1.6  christos   const struct lwp_info *element = (const struct lwp_info *) b;
    707  1.6  christos 
    708  1.6  christos   return ptid_get_lwp (entry->ptid) == ptid_get_lwp (element->ptid);
    709  1.6  christos }
    710  1.6  christos 
    711  1.6  christos /* Create the lwp_lwpid_htab hash table.  */
    712  1.6  christos 
    713  1.6  christos static void
    714  1.6  christos lwp_lwpid_htab_create (void)
    715  1.6  christos {
    716  1.6  christos   lwp_lwpid_htab = htab_create (100, lwp_info_hash, lwp_lwpid_htab_eq, NULL);
    717  1.6  christos }
    718  1.6  christos 
    719  1.6  christos /* Add LP to the hash table.  */
    720  1.6  christos 
    721  1.6  christos static void
    722  1.6  christos lwp_lwpid_htab_add_lwp (struct lwp_info *lp)
    723  1.6  christos {
    724  1.6  christos   void **slot;
    725  1.6  christos 
    726  1.6  christos   slot = htab_find_slot (lwp_lwpid_htab, lp, INSERT);
    727  1.6  christos   gdb_assert (slot != NULL && *slot == NULL);
    728  1.6  christos   *slot = lp;
    729  1.6  christos }
    730  1.6  christos 
    731  1.6  christos /* Head of doubly-linked list of known LWPs.  Sorted by reverse
    732  1.1  christos    creation order.  This order is assumed in some cases.  E.g.,
    733  1.6  christos    reaping status after killing alls lwps of a process: the leader LWP
    734  1.6  christos    must be reaped last.  */
    735  1.6  christos struct lwp_info *lwp_list;
    736  1.6  christos 
    737  1.6  christos /* Add LP to sorted-by-reverse-creation-order doubly-linked list.  */
    738  1.6  christos 
    739  1.6  christos static void
    740  1.6  christos lwp_list_add (struct lwp_info *lp)
    741  1.6  christos {
    742  1.6  christos   lp->next = lwp_list;
    743  1.6  christos   if (lwp_list != NULL)
    744  1.6  christos     lwp_list->prev = lp;
    745  1.6  christos   lwp_list = lp;
    746  1.6  christos }
    747  1.6  christos 
    748  1.6  christos /* Remove LP from sorted-by-reverse-creation-order doubly-linked
    749  1.6  christos    list.  */
    750  1.6  christos 
    751  1.6  christos static void
    752  1.6  christos lwp_list_remove (struct lwp_info *lp)
    753  1.6  christos {
    754  1.6  christos   /* Remove from sorted-by-creation-order list.  */
    755  1.6  christos   if (lp->next != NULL)
    756  1.6  christos     lp->next->prev = lp->prev;
    757  1.6  christos   if (lp->prev != NULL)
    758  1.6  christos     lp->prev->next = lp->next;
    759  1.6  christos   if (lp == lwp_list)
    760  1.1  christos     lwp_list = lp->next;
    761  1.1  christos }
    762  1.1  christos 
    763  1.1  christos 
    764  1.1  christos 
    766  1.1  christos /* Original signal mask.  */
    767  1.1  christos static sigset_t normal_mask;
    768  1.1  christos 
    769  1.1  christos /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
    770  1.1  christos    _initialize_linux_nat.  */
    771  1.1  christos static sigset_t suspend_mask;
    772  1.1  christos 
    773  1.1  christos /* Signals to block to make that sigsuspend work.  */
    774  1.1  christos static sigset_t blocked_mask;
    775  1.1  christos 
    776  1.1  christos /* SIGCHLD action.  */
    777  1.1  christos struct sigaction sigchld_action;
    778  1.1  christos 
    779  1.1  christos /* Block child signals (SIGCHLD and linux threads signals), and store
    780  1.1  christos    the previous mask in PREV_MASK.  */
    781  1.1  christos 
    782  1.1  christos static void
    783  1.1  christos block_child_signals (sigset_t *prev_mask)
    784  1.1  christos {
    785  1.1  christos   /* Make sure SIGCHLD is blocked.  */
    786  1.1  christos   if (!sigismember (&blocked_mask, SIGCHLD))
    787  1.1  christos     sigaddset (&blocked_mask, SIGCHLD);
    788  1.1  christos 
    789  1.1  christos   sigprocmask (SIG_BLOCK, &blocked_mask, prev_mask);
    790  1.1  christos }
    791  1.1  christos 
    792  1.1  christos /* Restore child signals mask, previously returned by
    793  1.1  christos    block_child_signals.  */
    794  1.1  christos 
    795  1.1  christos static void
    796  1.1  christos restore_child_signals_mask (sigset_t *prev_mask)
    797  1.1  christos {
    798  1.1  christos   sigprocmask (SIG_SETMASK, prev_mask, NULL);
    799  1.1  christos }
    800  1.1  christos 
    801  1.1  christos /* Mask of signals to pass directly to the inferior.  */
    802  1.3  christos static sigset_t pass_mask;
    803  1.3  christos 
    804  1.1  christos /* Update signals to pass to the inferior.  */
    805  1.1  christos static void
    806  1.1  christos linux_nat_pass_signals (struct target_ops *self,
    807  1.1  christos 			int numsigs, unsigned char *pass_signals)
    808  1.1  christos {
    809  1.1  christos   int signo;
    810  1.1  christos 
    811  1.1  christos   sigemptyset (&pass_mask);
    812  1.1  christos 
    813  1.1  christos   for (signo = 1; signo < NSIG; signo++)
    814  1.1  christos     {
    815  1.1  christos       int target_signo = gdb_signal_from_host (signo);
    816  1.1  christos       if (target_signo < numsigs && pass_signals[target_signo])
    817  1.1  christos         sigaddset (&pass_mask, signo);
    818  1.1  christos     }
    819  1.1  christos }
    820  1.1  christos 
    821  1.3  christos 
    822  1.5  christos 
    824  1.1  christos /* Prototypes for local functions.  */
    825  1.1  christos static int stop_wait_callback (struct lwp_info *lp, void *data);
    826  1.1  christos static char *linux_child_pid_to_exec_file (struct target_ops *self, int pid);
    827  1.1  christos static int resume_stopped_resumed_lwps (struct lwp_info *lp, void *data);
    828  1.1  christos static int check_ptrace_stopped_lwp_gone (struct lwp_info *lp);
    829  1.1  christos 
    830  1.1  christos 
    831  1.1  christos 
    833  1.1  christos /* Destroy and free LP.  */
    834  1.1  christos 
    835  1.1  christos static void
    836  1.6  christos lwp_free (struct lwp_info *lp)
    837  1.1  christos {
    838  1.6  christos   xfree (lp->arch_private);
    839  1.6  christos   xfree (lp);
    840  1.1  christos }
    841  1.6  christos 
    842  1.6  christos /* Traversal function for purge_lwp_list.  */
    843  1.1  christos 
    844  1.6  christos static int
    845  1.6  christos lwp_lwpid_htab_remove_pid (void **slot, void *info)
    846  1.6  christos {
    847  1.6  christos   struct lwp_info *lp = (struct lwp_info *) *slot;
    848  1.6  christos   int pid = *(int *) info;
    849  1.6  christos 
    850  1.1  christos   if (ptid_get_pid (lp->ptid) == pid)
    851  1.6  christos     {
    852  1.6  christos       htab_clear_slot (lwp_lwpid_htab, slot);
    853  1.1  christos       lwp_list_remove (lp);
    854  1.6  christos       lwp_free (lp);
    855  1.1  christos     }
    856  1.6  christos 
    857  1.6  christos   return 1;
    858  1.6  christos }
    859  1.6  christos 
    860  1.1  christos /* Remove all LWPs belong to PID from the lwp list.  */
    861  1.1  christos 
    862  1.1  christos static void
    863  1.1  christos purge_lwp_list (int pid)
    864  1.1  christos {
    865  1.1  christos   htab_traverse_noresize (lwp_lwpid_htab, lwp_lwpid_htab_remove_pid, &pid);
    866  1.1  christos }
    867  1.1  christos 
    868  1.1  christos /* Add the LWP specified by PTID to the list.  PTID is the first LWP
    869  1.1  christos    in the process.  Return a pointer to the structure describing the
    870  1.1  christos    new LWP.
    871  1.1  christos 
    872  1.1  christos    This differs from add_lwp in that we don't let the arch specific
    873  1.1  christos    bits know about this new thread.  Current clients of this callback
    874  1.1  christos    take the opportunity to install watchpoints in the new thread, and
    875  1.1  christos    we shouldn't do that for the first thread.  If we're spawning a
    876  1.1  christos    child ("run"), the thread executes the shell wrapper first, and we
    877  1.1  christos    shouldn't touch it until it execs the program we want to debug.
    878  1.1  christos    For "attach", it'd be okay to call the callback, but it's not
    879  1.1  christos    necessary, because watchpoints can't yet have been inserted into
    880  1.1  christos    the inferior.  */
    881  1.1  christos 
    882  1.1  christos static struct lwp_info *
    883  1.6  christos add_initial_lwp (ptid_t ptid)
    884  1.1  christos {
    885  1.1  christos   struct lwp_info *lp;
    886  1.1  christos 
    887  1.1  christos   gdb_assert (ptid_lwp_p (ptid));
    888  1.1  christos 
    889  1.1  christos   lp = XNEW (struct lwp_info);
    890  1.1  christos 
    891  1.1  christos   memset (lp, 0, sizeof (struct lwp_info));
    892  1.1  christos 
    893  1.6  christos   lp->last_resume_kind = resume_continue;
    894  1.6  christos   lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
    895  1.6  christos 
    896  1.6  christos   lp->ptid = ptid;
    897  1.6  christos   lp->core = -1;
    898  1.1  christos 
    899  1.1  christos   /* Add to sorted-by-reverse-creation-order list.  */
    900  1.1  christos   lwp_list_add (lp);
    901  1.1  christos 
    902  1.1  christos   /* Add to keyed-by-pid htab.  */
    903  1.1  christos   lwp_lwpid_htab_add_lwp (lp);
    904  1.1  christos 
    905  1.1  christos   return lp;
    906  1.1  christos }
    907  1.1  christos 
    908  1.1  christos /* Add the LWP specified by PID to the list.  Return a pointer to the
    909  1.1  christos    structure describing the new LWP.  The LWP should already be
    910  1.1  christos    stopped.  */
    911  1.1  christos 
    912  1.1  christos static struct lwp_info *
    913  1.1  christos add_lwp (ptid_t ptid)
    914  1.1  christos {
    915  1.1  christos   struct lwp_info *lp;
    916  1.1  christos 
    917  1.1  christos   lp = add_initial_lwp (ptid);
    918  1.1  christos 
    919  1.1  christos   /* Let the arch specific bits know about this new thread.  Current
    920  1.1  christos      clients of this callback take the opportunity to install
    921  1.1  christos      watchpoints in the new thread.  We don't do this for the first
    922  1.1  christos      thread though.  See add_initial_lwp.  */
    923  1.1  christos   if (linux_nat_new_thread != NULL)
    924  1.1  christos     linux_nat_new_thread (lp);
    925  1.1  christos 
    926  1.1  christos   return lp;
    927  1.1  christos }
    928  1.6  christos 
    929  1.6  christos /* Remove the LWP specified by PID from the list.  */
    930  1.6  christos 
    931  1.1  christos static void
    932  1.6  christos delete_lwp (ptid_t ptid)
    933  1.6  christos {
    934  1.6  christos   struct lwp_info *lp;
    935  1.6  christos   void **slot;
    936  1.1  christos   struct lwp_info dummy;
    937  1.6  christos 
    938  1.6  christos   dummy.ptid = ptid;
    939  1.1  christos   slot = htab_find_slot (lwp_lwpid_htab, &dummy, NO_INSERT);
    940  1.6  christos   if (slot == NULL)
    941  1.1  christos     return;
    942  1.6  christos 
    943  1.6  christos   lp = *(struct lwp_info **) slot;
    944  1.1  christos   gdb_assert (lp != NULL);
    945  1.6  christos 
    946  1.1  christos   htab_clear_slot (lwp_lwpid_htab, slot);
    947  1.1  christos 
    948  1.1  christos   /* Remove from sorted-by-creation-order list.  */
    949  1.1  christos   lwp_list_remove (lp);
    950  1.1  christos 
    951  1.1  christos   /* Release.  */
    952  1.1  christos   lwp_free (lp);
    953  1.1  christos }
    954  1.1  christos 
    955  1.1  christos /* Return a pointer to the structure describing the LWP corresponding
    956  1.1  christos    to PID.  If no corresponding LWP could be found, return NULL.  */
    957  1.6  christos 
    958  1.1  christos static struct lwp_info *
    959  1.1  christos find_lwp_pid (ptid_t ptid)
    960  1.1  christos {
    961  1.1  christos   struct lwp_info *lp;
    962  1.1  christos   int lwp;
    963  1.1  christos   struct lwp_info dummy;
    964  1.6  christos 
    965  1.6  christos   if (ptid_lwp_p (ptid))
    966  1.6  christos     lwp = ptid_get_lwp (ptid);
    967  1.1  christos   else
    968  1.1  christos     lwp = ptid_get_pid (ptid);
    969  1.5  christos 
    970  1.1  christos   dummy.ptid = ptid_build (0, lwp, 0);
    971  1.1  christos   lp = (struct lwp_info *) htab_find (lwp_lwpid_htab, &dummy);
    972  1.1  christos   return lp;
    973  1.5  christos }
    974  1.1  christos 
    975  1.1  christos /* See nat/linux-nat.h.  */
    976  1.1  christos 
    977  1.1  christos struct lwp_info *
    978  1.1  christos iterate_over_lwps (ptid_t filter,
    979  1.1  christos 		   iterate_over_lwps_ftype callback,
    980  1.1  christos 		   void *data)
    981  1.1  christos {
    982  1.1  christos   struct lwp_info *lp, *lpnext;
    983  1.1  christos 
    984  1.5  christos   for (lp = lwp_list; lp; lp = lpnext)
    985  1.1  christos     {
    986  1.1  christos       lpnext = lp->next;
    987  1.1  christos 
    988  1.1  christos       if (ptid_match (lp->ptid, filter))
    989  1.1  christos 	{
    990  1.1  christos 	  if ((*callback) (lp, data) != 0)
    991  1.1  christos 	    return lp;
    992  1.1  christos 	}
    993  1.1  christos     }
    994  1.1  christos 
    995  1.1  christos   return NULL;
    996  1.1  christos }
    997  1.1  christos 
    998  1.1  christos /* Update our internal state when changing from one checkpoint to
    999  1.1  christos    another indicated by NEW_PTID.  We can only switch single-threaded
   1000  1.1  christos    applications, so we only create one new LWP, and the previous list
   1001  1.1  christos    is discarded.  */
   1002  1.1  christos 
   1003  1.1  christos void
   1004  1.1  christos linux_nat_switch_fork (ptid_t new_ptid)
   1005  1.1  christos {
   1006  1.1  christos   struct lwp_info *lp;
   1007  1.1  christos 
   1008  1.1  christos   purge_lwp_list (ptid_get_pid (inferior_ptid));
   1009  1.1  christos 
   1010  1.1  christos   lp = add_lwp (new_ptid);
   1011  1.1  christos   lp->stopped = 1;
   1012  1.1  christos 
   1013  1.1  christos   /* This changes the thread's ptid while preserving the gdb thread
   1014  1.1  christos      num.  Also changes the inferior pid, while preserving the
   1015  1.1  christos      inferior num.  */
   1016  1.1  christos   thread_change_ptid (inferior_ptid, new_ptid);
   1017  1.1  christos 
   1018  1.1  christos   /* We've just told GDB core that the thread changed target id, but,
   1019  1.1  christos      in fact, it really is a different thread, with different register
   1020  1.1  christos      contents.  */
   1021  1.1  christos   registers_changed ();
   1022  1.1  christos }
   1023  1.1  christos 
   1024  1.1  christos /* Handle the exit of a single thread LP.  */
   1025  1.1  christos 
   1026  1.1  christos static void
   1027  1.1  christos exit_lwp (struct lwp_info *lp)
   1028  1.1  christos {
   1029  1.1  christos   struct thread_info *th = find_thread_ptid (lp->ptid);
   1030  1.1  christos 
   1031  1.1  christos   if (th)
   1032  1.1  christos     {
   1033  1.1  christos       if (print_thread_events)
   1034  1.1  christos 	printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (lp->ptid));
   1035  1.1  christos 
   1036  1.1  christos       delete_thread (lp->ptid);
   1037  1.1  christos     }
   1038  1.1  christos 
   1039  1.1  christos   delete_lwp (lp->ptid);
   1040  1.6  christos }
   1041  1.1  christos 
   1042  1.1  christos /* Wait for the LWP specified by LP, which we have just attached to.
   1043  1.1  christos    Returns a wait status for that LWP, to cache.  */
   1044  1.1  christos 
   1045  1.1  christos static int
   1046  1.1  christos linux_nat_post_attach_wait (ptid_t ptid, int first, int *signalled)
   1047  1.1  christos {
   1048  1.1  christos   pid_t new_pid, pid = ptid_get_lwp (ptid);
   1049  1.1  christos   int status;
   1050  1.1  christos 
   1051  1.1  christos   if (linux_proc_pid_is_stopped (pid))
   1052  1.1  christos     {
   1053  1.1  christos       if (debug_linux_nat)
   1054  1.1  christos 	fprintf_unfiltered (gdb_stdlog,
   1055  1.1  christos 			    "LNPAW: Attaching to a stopped process\n");
   1056  1.1  christos 
   1057  1.1  christos       /* The process is definitely stopped.  It is in a job control
   1058  1.1  christos 	 stop, unless the kernel predates the TASK_STOPPED /
   1059  1.1  christos 	 TASK_TRACED distinction, in which case it might be in a
   1060  1.1  christos 	 ptrace stop.  Make sure it is in a ptrace stop; from there we
   1061  1.1  christos 	 can kill it, signal it, et cetera.
   1062  1.1  christos 
   1063  1.1  christos          First make sure there is a pending SIGSTOP.  Since we are
   1064  1.1  christos 	 already attached, the process can not transition from stopped
   1065  1.1  christos 	 to running without a PTRACE_CONT; so we know this signal will
   1066  1.1  christos 	 go into the queue.  The SIGSTOP generated by PTRACE_ATTACH is
   1067  1.1  christos 	 probably already in the queue (unless this kernel is old
   1068  1.1  christos 	 enough to use TASK_STOPPED for ptrace stops); but since SIGSTOP
   1069  1.1  christos 	 is not an RT signal, it can only be queued once.  */
   1070  1.1  christos       kill_lwp (pid, SIGSTOP);
   1071  1.1  christos 
   1072  1.1  christos       /* Finally, resume the stopped process.  This will deliver the SIGSTOP
   1073  1.1  christos 	 (or a higher priority signal, just like normal PTRACE_ATTACH).  */
   1074  1.6  christos       ptrace (PTRACE_CONT, pid, 0, 0);
   1075  1.1  christos     }
   1076  1.1  christos 
   1077  1.1  christos   /* Make sure the initial process is stopped.  The user-level threads
   1078  1.1  christos      layer might want to poke around in the inferior, and that won't
   1079  1.1  christos      work if things haven't stabilized yet.  */
   1080  1.1  christos   new_pid = my_waitpid (pid, &status, __WALL);
   1081  1.1  christos   gdb_assert (pid == new_pid);
   1082  1.1  christos 
   1083  1.1  christos   if (!WIFSTOPPED (status))
   1084  1.1  christos     {
   1085  1.1  christos       /* The pid we tried to attach has apparently just exited.  */
   1086  1.1  christos       if (debug_linux_nat)
   1087  1.1  christos 	fprintf_unfiltered (gdb_stdlog, "LNPAW: Failed to stop %d: %s",
   1088  1.1  christos 			    pid, status_to_str (status));
   1089  1.1  christos       return status;
   1090  1.1  christos     }
   1091  1.1  christos 
   1092  1.1  christos   if (WSTOPSIG (status) != SIGSTOP)
   1093  1.1  christos     {
   1094  1.1  christos       *signalled = 1;
   1095  1.1  christos       if (debug_linux_nat)
   1096  1.1  christos 	fprintf_unfiltered (gdb_stdlog,
   1097  1.1  christos 			    "LNPAW: Received %s after attaching\n",
   1098  1.1  christos 			    status_to_str (status));
   1099  1.1  christos     }
   1100  1.1  christos 
   1101  1.1  christos   return status;
   1102  1.1  christos }
   1103  1.5  christos 
   1104  1.5  christos static void
   1105  1.1  christos linux_nat_create_inferior (struct target_ops *ops,
   1106  1.1  christos 			   char *exec_file, char *allargs, char **env,
   1107  1.1  christos 			   int from_tty)
   1108  1.1  christos {
   1109  1.1  christos   struct cleanup *restore_personality
   1110  1.3  christos     = maybe_disable_address_space_randomization (disable_randomization);
   1111  1.1  christos 
   1112  1.1  christos   /* The fork_child mechanism is synchronous and calls target_wait, so
   1113  1.1  christos      we have to mask the async mode.  */
   1114  1.5  christos 
   1115  1.1  christos   /* Make sure we report all signals during startup.  */
   1116  1.1  christos   linux_nat_pass_signals (ops, 0, NULL);
   1117  1.3  christos 
   1118  1.3  christos   linux_ops->to_create_inferior (ops, exec_file, allargs, env, from_tty);
   1119  1.3  christos 
   1120  1.3  christos   do_cleanups (restore_personality);
   1121  1.3  christos }
   1122  1.3  christos 
   1123  1.3  christos /* Callback for linux_proc_attach_tgid_threads.  Attach to PTID if not
   1124  1.3  christos    already attached.  Returns true if a new LWP is found, false
   1125  1.3  christos    otherwise.  */
   1126  1.3  christos 
   1127  1.3  christos static int
   1128  1.3  christos attach_proc_task_lwp_callback (ptid_t ptid)
   1129  1.3  christos {
   1130  1.3  christos   struct lwp_info *lp;
   1131  1.3  christos 
   1132  1.3  christos   /* Ignore LWPs we're already attached to.  */
   1133  1.3  christos   lp = find_lwp_pid (ptid);
   1134  1.3  christos   if (lp == NULL)
   1135  1.3  christos     {
   1136  1.3  christos       int lwpid = ptid_get_lwp (ptid);
   1137  1.3  christos 
   1138  1.3  christos       if (ptrace (PTRACE_ATTACH, lwpid, 0, 0) < 0)
   1139  1.3  christos 	{
   1140  1.3  christos 	  int err = errno;
   1141  1.3  christos 
   1142  1.3  christos 	  /* Be quiet if we simply raced with the thread exiting.
   1143  1.3  christos 	     EPERM is returned if the thread's task still exists, and
   1144  1.3  christos 	     is marked as exited or zombie, as well as other
   1145  1.3  christos 	     conditions, so in that case, confirm the status in
   1146  1.3  christos 	     /proc/PID/status.  */
   1147  1.3  christos 	  if (err == ESRCH
   1148  1.3  christos 	      || (err == EPERM && linux_proc_pid_is_gone (lwpid)))
   1149  1.3  christos 	    {
   1150  1.3  christos 	      if (debug_linux_nat)
   1151  1.3  christos 		{
   1152  1.3  christos 		  fprintf_unfiltered (gdb_stdlog,
   1153  1.3  christos 				      "Cannot attach to lwp %d: "
   1154  1.3  christos 				      "thread is gone (%d: %s)\n",
   1155  1.3  christos 				      lwpid, err, safe_strerror (err));
   1156  1.3  christos 		}
   1157  1.3  christos 	    }
   1158  1.3  christos 	  else
   1159  1.3  christos 	    {
   1160  1.3  christos 	      warning (_("Cannot attach to lwp %d: %s"),
   1161  1.3  christos 		       lwpid,
   1162  1.3  christos 		       linux_ptrace_attach_fail_reason_string (ptid,
   1163  1.3  christos 							       err));
   1164  1.3  christos 	    }
   1165  1.3  christos 	}
   1166  1.3  christos       else
   1167  1.3  christos 	{
   1168  1.3  christos 	  if (debug_linux_nat)
   1169  1.3  christos 	    fprintf_unfiltered (gdb_stdlog,
   1170  1.3  christos 				"PTRACE_ATTACH %s, 0, 0 (OK)\n",
   1171  1.3  christos 				target_pid_to_str (ptid));
   1172  1.3  christos 
   1173  1.3  christos 	  lp = add_lwp (ptid);
   1174  1.3  christos 
   1175  1.3  christos 	  /* The next time we wait for this LWP we'll see a SIGSTOP as
   1176  1.6  christos 	     PTRACE_ATTACH brings it to a halt.  */
   1177  1.6  christos 	  lp->signalled = 1;
   1178  1.6  christos 
   1179  1.6  christos 	  /* We need to wait for a stop before being able to make the
   1180  1.6  christos 	     next ptrace call on this LWP.  */
   1181  1.6  christos 	  lp->must_set_ptrace_flags = 1;
   1182  1.6  christos 
   1183  1.6  christos 	  /* So that wait collects the SIGSTOP.  */
   1184  1.6  christos 	  lp->resumed = 1;
   1185  1.6  christos 
   1186  1.3  christos 	  /* Also add the LWP to gdb's thread list, in case a
   1187  1.3  christos 	     matching libthread_db is not found (or the process uses
   1188  1.3  christos 	     raw clone).  */
   1189  1.3  christos 	  add_thread (lp->ptid);
   1190  1.3  christos 	  set_running (lp->ptid, 1);
   1191  1.3  christos 	  set_executing (lp->ptid, 1);
   1192  1.3  christos 	}
   1193  1.1  christos 
   1194  1.3  christos       return 1;
   1195  1.1  christos     }
   1196  1.1  christos   return 0;
   1197  1.1  christos }
   1198  1.1  christos 
   1199  1.1  christos static void
   1200  1.1  christos linux_nat_attach (struct target_ops *ops, const char *args, int from_tty)
   1201  1.3  christos {
   1202  1.1  christos   struct lwp_info *lp;
   1203  1.5  christos   int status;
   1204  1.1  christos   ptid_t ptid;
   1205  1.1  christos 
   1206  1.1  christos   /* Make sure we report all signals during attach.  */
   1207  1.5  christos   linux_nat_pass_signals (ops, 0, NULL);
   1208  1.1  christos 
   1209  1.1  christos   TRY
   1210  1.1  christos     {
   1211  1.1  christos       linux_ops->to_attach (ops, args, from_tty);
   1212  1.1  christos     }
   1213  1.1  christos   CATCH (ex, RETURN_MASK_ERROR)
   1214  1.1  christos     {
   1215  1.1  christos       pid_t pid = parse_pid_to_attach (args);
   1216  1.1  christos       struct buffer buffer;
   1217  1.3  christos       char *message, *buffer_s;
   1218  1.1  christos 
   1219  1.1  christos       message = xstrdup (ex.message);
   1220  1.1  christos       make_cleanup (xfree, message);
   1221  1.1  christos 
   1222  1.1  christos       buffer_init (&buffer);
   1223  1.3  christos       linux_ptrace_attach_fail_reason (pid, &buffer);
   1224  1.3  christos 
   1225  1.3  christos       buffer_grow_str0 (&buffer, "");
   1226  1.3  christos       buffer_s = buffer_finish (&buffer);
   1227  1.1  christos       make_cleanup (xfree, buffer_s);
   1228  1.5  christos 
   1229  1.1  christos       if (*buffer_s != '\0')
   1230  1.1  christos 	throw_error (ex.error, "warning: %s\n%s", buffer_s, message);
   1231  1.1  christos       else
   1232  1.1  christos 	throw_error (ex.error, "%s", message);
   1233  1.1  christos     }
   1234  1.1  christos   END_CATCH
   1235  1.1  christos 
   1236  1.1  christos   /* The ptrace base target adds the main thread with (pid,0,0)
   1237  1.1  christos      format.  Decorate it with lwp info.  */
   1238  1.1  christos   ptid = ptid_build (ptid_get_pid (inferior_ptid),
   1239  1.1  christos 		     ptid_get_pid (inferior_ptid),
   1240  1.6  christos 		     0);
   1241  1.1  christos   thread_change_ptid (inferior_ptid, ptid);
   1242  1.1  christos 
   1243  1.1  christos   /* Add the initial process as the first LWP to the list.  */
   1244  1.1  christos   lp = add_initial_lwp (ptid);
   1245  1.1  christos 
   1246  1.1  christos   status = linux_nat_post_attach_wait (lp->ptid, 1, &lp->signalled);
   1247  1.1  christos   if (!WIFSTOPPED (status))
   1248  1.1  christos     {
   1249  1.1  christos       if (WIFEXITED (status))
   1250  1.1  christos 	{
   1251  1.1  christos 	  int exit_code = WEXITSTATUS (status);
   1252  1.1  christos 
   1253  1.1  christos 	  target_terminal_ours ();
   1254  1.1  christos 	  target_mourn_inferior ();
   1255  1.1  christos 	  if (exit_code == 0)
   1256  1.1  christos 	    error (_("Unable to attach: program exited normally."));
   1257  1.1  christos 	  else
   1258  1.1  christos 	    error (_("Unable to attach: program exited with code %d."),
   1259  1.1  christos 		   exit_code);
   1260  1.1  christos 	}
   1261  1.1  christos       else if (WIFSIGNALED (status))
   1262  1.1  christos 	{
   1263  1.1  christos 	  enum gdb_signal signo;
   1264  1.1  christos 
   1265  1.1  christos 	  target_terminal_ours ();
   1266  1.1  christos 	  target_mourn_inferior ();
   1267  1.1  christos 
   1268  1.1  christos 	  signo = gdb_signal_from_host (WTERMSIG (status));
   1269  1.1  christos 	  error (_("Unable to attach: program terminated with signal "
   1270  1.1  christos 		   "%s, %s."),
   1271  1.1  christos 		 gdb_signal_to_name (signo),
   1272  1.1  christos 		 gdb_signal_to_string (signo));
   1273  1.1  christos 	}
   1274  1.1  christos 
   1275  1.1  christos       internal_error (__FILE__, __LINE__,
   1276  1.1  christos 		      _("unexpected status %d for PID %ld"),
   1277  1.1  christos 		      status, (long) ptid_get_lwp (ptid));
   1278  1.1  christos     }
   1279  1.1  christos 
   1280  1.1  christos   lp->stopped = 1;
   1281  1.1  christos 
   1282  1.1  christos   /* Save the wait status to report later.  */
   1283  1.1  christos   lp->resumed = 1;
   1284  1.1  christos   if (debug_linux_nat)
   1285  1.3  christos     fprintf_unfiltered (gdb_stdlog,
   1286  1.3  christos 			"LNA: waitpid %ld, saving status %s\n",
   1287  1.3  christos 			(long) ptid_get_pid (lp->ptid), status_to_str (status));
   1288  1.3  christos 
   1289  1.3  christos   lp->status = status;
   1290  1.3  christos 
   1291  1.3  christos   /* We must attach to every LWP.  If /proc is mounted, use that to
   1292  1.3  christos      find them now.  The inferior may be using raw clone instead of
   1293  1.3  christos      using pthreads.  But even if it is using pthreads, thread_db
   1294  1.3  christos      walks structures in the inferior's address space to find the list
   1295  1.1  christos      of threads/LWPs, and those structures may well be corrupted.
   1296  1.5  christos      Note that once thread_db is loaded, we'll still use it to list
   1297  1.1  christos      threads and associate pthread info with each LWP.  */
   1298  1.1  christos   linux_proc_attach_tgid_threads (ptid_get_pid (lp->ptid),
   1299  1.6  christos 				  attach_proc_task_lwp_callback);
   1300  1.6  christos 
   1301  1.6  christos   if (target_can_async_p ())
   1302  1.6  christos     target_async (1);
   1303  1.6  christos }
   1304  1.1  christos 
   1305  1.6  christos /* Get pending signal of THREAD as a host signal number, for detaching
   1306  1.1  christos    purposes.  This is the signal the thread last stopped for, which we
   1307  1.1  christos    need to deliver to the thread when detaching, otherwise, it'd be
   1308  1.1  christos    suppressed/lost.  */
   1309  1.1  christos 
   1310  1.1  christos static int
   1311  1.1  christos get_detach_signal (struct lwp_info *lp)
   1312  1.1  christos {
   1313  1.1  christos   enum gdb_signal signo = GDB_SIGNAL_0;
   1314  1.1  christos 
   1315  1.1  christos   /* If we paused threads momentarily, we may have stored pending
   1316  1.1  christos      events in lp->status or lp->waitstatus (see stop_wait_callback),
   1317  1.1  christos      and GDB core hasn't seen any signal for those threads.
   1318  1.1  christos      Otherwise, the last signal reported to the core is found in the
   1319  1.1  christos      thread object's stop_signal.
   1320  1.1  christos 
   1321  1.1  christos      There's a corner case that isn't handled here at present.  Only
   1322  1.1  christos      if the thread stopped with a TARGET_WAITKIND_STOPPED does
   1323  1.1  christos      stop_signal make sense as a real signal to pass to the inferior.
   1324  1.1  christos      Some catchpoint related events, like
   1325  1.1  christos      TARGET_WAITKIND_(V)FORK|EXEC|SYSCALL, have their stop_signal set
   1326  1.1  christos      to GDB_SIGNAL_SIGTRAP when the catchpoint triggers.  But,
   1327  1.1  christos      those traps are debug API (ptrace in our case) related and
   1328  1.1  christos      induced; the inferior wouldn't see them if it wasn't being
   1329  1.1  christos      traced.  Hence, we should never pass them to the inferior, even
   1330  1.1  christos      when set to pass state.  Since this corner case isn't handled by
   1331  1.1  christos      infrun.c when proceeding with a signal, for consistency, neither
   1332  1.1  christos      do we handle it here (or elsewhere in the file we check for
   1333  1.1  christos      signal pass state).  Normally SIGTRAP isn't set to pass state, so
   1334  1.6  christos      this is really a corner case.  */
   1335  1.1  christos 
   1336  1.1  christos   if (lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
   1337  1.1  christos     signo = GDB_SIGNAL_0; /* a pending ptrace event, not a real signal.  */
   1338  1.6  christos   else if (lp->status)
   1339  1.6  christos     signo = gdb_signal_from_host (WSTOPSIG (lp->status));
   1340  1.6  christos   else if (target_is_non_stop_p () && !is_executing (lp->ptid))
   1341  1.6  christos     {
   1342  1.1  christos       struct thread_info *tp = find_thread_ptid (lp->ptid);
   1343  1.6  christos 
   1344  1.1  christos       if (tp->suspend.waitstatus_pending_p)
   1345  1.1  christos 	signo = tp->suspend.waitstatus.value.sig;
   1346  1.1  christos       else
   1347  1.1  christos 	signo = tp->suspend.stop_signal;
   1348  1.1  christos     }
   1349  1.1  christos   else if (!target_is_non_stop_p ())
   1350  1.1  christos     {
   1351  1.1  christos       struct target_waitstatus last;
   1352  1.1  christos       ptid_t last_ptid;
   1353  1.1  christos 
   1354  1.1  christos       get_last_target_status (&last_ptid, &last);
   1355  1.1  christos 
   1356  1.1  christos       if (ptid_get_lwp (lp->ptid) == ptid_get_lwp (last_ptid))
   1357  1.1  christos 	{
   1358  1.1  christos 	  struct thread_info *tp = find_thread_ptid (lp->ptid);
   1359  1.1  christos 
   1360  1.1  christos 	  signo = tp->suspend.stop_signal;
   1361  1.1  christos 	}
   1362  1.1  christos     }
   1363  1.1  christos 
   1364  1.1  christos   if (signo == GDB_SIGNAL_0)
   1365  1.1  christos     {
   1366  1.1  christos       if (debug_linux_nat)
   1367  1.1  christos 	fprintf_unfiltered (gdb_stdlog,
   1368  1.1  christos 			    "GPT: lwp %s has no pending signal\n",
   1369  1.1  christos 			    target_pid_to_str (lp->ptid));
   1370  1.1  christos     }
   1371  1.1  christos   else if (!signal_pass_state (signo))
   1372  1.1  christos     {
   1373  1.1  christos       if (debug_linux_nat)
   1374  1.1  christos 	fprintf_unfiltered (gdb_stdlog,
   1375  1.1  christos 			    "GPT: lwp %s had signal %s, "
   1376  1.1  christos 			    "but it is in no pass state\n",
   1377  1.1  christos 			    target_pid_to_str (lp->ptid),
   1378  1.1  christos 			    gdb_signal_to_string (signo));
   1379  1.1  christos     }
   1380  1.1  christos   else
   1381  1.6  christos     {
   1382  1.6  christos       if (debug_linux_nat)
   1383  1.1  christos 	fprintf_unfiltered (gdb_stdlog,
   1384  1.1  christos 			    "GPT: lwp %s has pending signal %s\n",
   1385  1.1  christos 			    target_pid_to_str (lp->ptid),
   1386  1.1  christos 			    gdb_signal_to_string (signo));
   1387  1.1  christos 
   1388  1.6  christos       return gdb_signal_to_host (signo);
   1389  1.6  christos     }
   1390  1.6  christos 
   1391  1.6  christos   return 0;
   1392  1.6  christos }
   1393  1.6  christos 
   1394  1.1  christos /* Detach from LP.  If SIGNO_P is non-NULL, then it points to the
   1395  1.6  christos    signal number that should be passed to the LWP when detaching.
   1396  1.6  christos    Otherwise pass any pending signal the LWP may have, if any.  */
   1397  1.6  christos 
   1398  1.1  christos static void
   1399  1.1  christos detach_one_lwp (struct lwp_info *lp, int *signo_p)
   1400  1.1  christos {
   1401  1.1  christos   int lwpid = ptid_get_lwp (lp->ptid);
   1402  1.1  christos   int signo;
   1403  1.1  christos 
   1404  1.1  christos   gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
   1405  1.1  christos 
   1406  1.1  christos   if (debug_linux_nat && lp->status)
   1407  1.1  christos     fprintf_unfiltered (gdb_stdlog, "DC:  Pending %s for %s on detach.\n",
   1408  1.1  christos 			strsignal (WSTOPSIG (lp->status)),
   1409  1.1  christos 			target_pid_to_str (lp->ptid));
   1410  1.1  christos 
   1411  1.1  christos   /* If there is a pending SIGSTOP, get rid of it.  */
   1412  1.1  christos   if (lp->signalled)
   1413  1.6  christos     {
   1414  1.1  christos       if (debug_linux_nat)
   1415  1.1  christos 	fprintf_unfiltered (gdb_stdlog,
   1416  1.1  christos 			    "DC: Sending SIGCONT to %s\n",
   1417  1.6  christos 			    target_pid_to_str (lp->ptid));
   1418  1.1  christos 
   1419  1.1  christos       kill_lwp (lwpid, SIGCONT);
   1420  1.6  christos       lp->signalled = 0;
   1421  1.6  christos     }
   1422  1.6  christos 
   1423  1.6  christos   if (signo_p == NULL)
   1424  1.1  christos     {
   1425  1.6  christos       /* Pass on any pending signal for this LWP.  */
   1426  1.6  christos       signo = get_detach_signal (lp);
   1427  1.6  christos     }
   1428  1.6  christos   else
   1429  1.6  christos     signo = *signo_p;
   1430  1.1  christos 
   1431  1.1  christos   /* Preparing to resume may try to write registers, and fail if the
   1432  1.6  christos      lwp is zombie.  If that happens, ignore the error.  We'll handle
   1433  1.6  christos      it below, when detach fails with ESRCH.  */
   1434  1.6  christos   TRY
   1435  1.6  christos     {
   1436  1.6  christos       if (linux_nat_prepare_to_resume != NULL)
   1437  1.6  christos 	linux_nat_prepare_to_resume (lp);
   1438  1.6  christos     }
   1439  1.6  christos   CATCH (ex, RETURN_MASK_ERROR)
   1440  1.6  christos     {
   1441  1.6  christos       if (!check_ptrace_stopped_lwp_gone (lp))
   1442  1.6  christos 	throw_exception (ex);
   1443  1.1  christos     }
   1444  1.6  christos   END_CATCH
   1445  1.6  christos 
   1446  1.6  christos   if (ptrace (PTRACE_DETACH, lwpid, 0, signo) < 0)
   1447  1.6  christos     {
   1448  1.6  christos       int save_errno = errno;
   1449  1.6  christos 
   1450  1.6  christos       /* We know the thread exists, so ESRCH must mean the lwp is
   1451  1.1  christos 	 zombie.  This can happen if one of the already-detached
   1452  1.6  christos 	 threads exits the whole thread group.  In that case we're
   1453  1.6  christos 	 still attached, and must reap the lwp.  */
   1454  1.6  christos       if (save_errno == ESRCH)
   1455  1.6  christos 	{
   1456  1.6  christos 	  int ret, status;
   1457  1.6  christos 
   1458  1.6  christos 	  ret = my_waitpid (lwpid, &status, __WALL);
   1459  1.6  christos 	  if (ret == -1)
   1460  1.6  christos 	    {
   1461  1.6  christos 	      warning (_("Couldn't reap LWP %d while detaching: %s"),
   1462  1.6  christos 		       lwpid, strerror (errno));
   1463  1.6  christos 	    }
   1464  1.6  christos 	  else if (!WIFEXITED (status) && !WIFSIGNALED (status))
   1465  1.6  christos 	    {
   1466  1.6  christos 	      warning (_("Reaping LWP %d while detaching "
   1467  1.6  christos 			 "returned unexpected status 0x%x"),
   1468  1.6  christos 		       lwpid, status);
   1469  1.6  christos 	    }
   1470  1.6  christos 	}
   1471  1.6  christos       else
   1472  1.6  christos 	{
   1473  1.6  christos 	  error (_("Can't detach %s: %s"), target_pid_to_str (lp->ptid),
   1474  1.6  christos 		 safe_strerror (save_errno));
   1475  1.6  christos 	}
   1476  1.6  christos     }
   1477  1.1  christos   else if (debug_linux_nat)
   1478  1.1  christos     {
   1479  1.6  christos       fprintf_unfiltered (gdb_stdlog,
   1480  1.6  christos 			  "PTRACE_DETACH (%s, %s, 0) (OK)\n",
   1481  1.6  christos 			  target_pid_to_str (lp->ptid),
   1482  1.6  christos 			  strsignal (signo));
   1483  1.6  christos     }
   1484  1.6  christos 
   1485  1.6  christos   delete_lwp (lp->ptid);
   1486  1.6  christos }
   1487  1.6  christos 
   1488  1.6  christos static int
   1489  1.6  christos detach_callback (struct lwp_info *lp, void *data)
   1490  1.1  christos {
   1491  1.1  christos   /* We don't actually detach from the thread group leader just yet.
   1492  1.1  christos      If the thread group exits, we must reap the zombie clone lwps
   1493  1.1  christos      before we're able to reap the leader.  */
   1494  1.1  christos   if (ptid_get_lwp (lp->ptid) != ptid_get_pid (lp->ptid))
   1495  1.1  christos     detach_one_lwp (lp, NULL);
   1496  1.1  christos   return 0;
   1497  1.1  christos }
   1498  1.1  christos 
   1499  1.1  christos static void
   1500  1.1  christos linux_nat_detach (struct target_ops *ops, const char *args, int from_tty)
   1501  1.1  christos {
   1502  1.1  christos   int pid;
   1503  1.1  christos   struct lwp_info *main_lwp;
   1504  1.1  christos 
   1505  1.1  christos   pid = ptid_get_pid (inferior_ptid);
   1506  1.1  christos 
   1507  1.1  christos   /* Don't unregister from the event loop, as there may be other
   1508  1.1  christos      inferiors running. */
   1509  1.1  christos 
   1510  1.1  christos   /* Stop all threads before detaching.  ptrace requires that the
   1511  1.1  christos      thread is stopped to sucessfully detach.  */
   1512  1.1  christos   iterate_over_lwps (pid_to_ptid (pid), stop_callback, NULL);
   1513  1.1  christos   /* ... and wait until all of them have reported back that
   1514  1.1  christos      they're no longer running.  */
   1515  1.1  christos   iterate_over_lwps (pid_to_ptid (pid), stop_wait_callback, NULL);
   1516  1.1  christos 
   1517  1.1  christos   iterate_over_lwps (pid_to_ptid (pid), detach_callback, NULL);
   1518  1.1  christos 
   1519  1.1  christos   /* Only the initial process should be left right now.  */
   1520  1.1  christos   gdb_assert (num_lwps (ptid_get_pid (inferior_ptid)) == 1);
   1521  1.1  christos 
   1522  1.1  christos   main_lwp = find_lwp_pid (pid_to_ptid (pid));
   1523  1.1  christos 
   1524  1.1  christos   if (forks_exist_p ())
   1525  1.1  christos     {
   1526  1.1  christos       /* Multi-fork case.  The current inferior_ptid is being detached
   1527  1.6  christos 	 from, but there are other viable forks to debug.  Detach from
   1528  1.6  christos 	 the current fork, and context-switch to the first
   1529  1.6  christos 	 available.  */
   1530  1.6  christos       linux_fork_detach (args, from_tty);
   1531  1.6  christos     }
   1532  1.6  christos   else
   1533  1.6  christos     {
   1534  1.6  christos       int signo;
   1535  1.6  christos 
   1536  1.6  christos       target_announce_detach (from_tty);
   1537  1.6  christos 
   1538  1.6  christos       /* Pass on any pending signal for the last LWP, unless the user
   1539  1.6  christos 	 requested detaching with a different signal (most likely 0,
   1540  1.6  christos 	 meaning, discard the signal).  */
   1541  1.6  christos       if (args != NULL)
   1542  1.6  christos 	signo = atoi (args);
   1543  1.6  christos       else
   1544  1.6  christos 	signo = get_detach_signal (main_lwp);
   1545  1.1  christos 
   1546  1.1  christos       detach_one_lwp (main_lwp, &signo);
   1547  1.3  christos 
   1548  1.3  christos       inf_ptrace_detach_success (ops);
   1549  1.3  christos     }
   1550  1.3  christos   delete_lwp (main_lwp->ptid);
   1551  1.5  christos }
   1552  1.5  christos 
   1553  1.3  christos /* Resume execution of the inferior process.  If STEP is nonzero,
   1554  1.3  christos    single-step it.  If SIGNAL is nonzero, give it that signal.  */
   1555  1.3  christos 
   1556  1.3  christos static void
   1557  1.3  christos linux_resume_one_lwp_throw (struct lwp_info *lp, int step,
   1558  1.3  christos 			    enum gdb_signal signo)
   1559  1.3  christos {
   1560  1.3  christos   lp->step = step;
   1561  1.3  christos 
   1562  1.3  christos   /* stop_pc doubles as the PC the LWP had when it was last resumed.
   1563  1.3  christos      We only presently need that if the LWP is stepped though (to
   1564  1.3  christos      handle the case of stepping a breakpoint instruction).  */
   1565  1.3  christos   if (step)
   1566  1.3  christos     {
   1567  1.3  christos       struct regcache *regcache = get_thread_regcache (lp->ptid);
   1568  1.3  christos 
   1569  1.3  christos       lp->stop_pc = regcache_read_pc (regcache);
   1570  1.5  christos     }
   1571  1.5  christos   else
   1572  1.5  christos     lp->stop_pc = 0;
   1573  1.5  christos 
   1574  1.5  christos   if (linux_nat_prepare_to_resume != NULL)
   1575  1.5  christos     linux_nat_prepare_to_resume (lp);
   1576  1.5  christos   linux_ops->to_resume (linux_ops, lp->ptid, step, signo);
   1577  1.5  christos 
   1578  1.3  christos   /* Successfully resumed.  Clear state that no longer makes sense,
   1579  1.6  christos      and mark the LWP as running.  Must not do this before resuming
   1580  1.5  christos      otherwise if that fails other code will be confused.  E.g., we'd
   1581  1.3  christos      later try to stop the LWP and hang forever waiting for a stop
   1582  1.3  christos      status.  Note that we must not throw after this is cleared,
   1583  1.3  christos      otherwise handle_zombie_lwp_error would get confused.  */
   1584  1.5  christos   lp->stopped = 0;
   1585  1.5  christos   lp->core = -1;
   1586  1.5  christos   lp->stop_reason = TARGET_STOPPED_BY_NO_REASON;
   1587  1.5  christos   registers_changed_ptid (lp->ptid);
   1588  1.5  christos }
   1589  1.5  christos 
   1590  1.5  christos /* Called when we try to resume a stopped LWP and that errors out.  If
   1591  1.5  christos    the LWP is no longer in ptrace-stopped state (meaning it's zombie,
   1592  1.5  christos    or about to become), discard the error, clear any pending status
   1593  1.5  christos    the LWP may have, and return true (we'll collect the exit status
   1594  1.5  christos    soon enough).  Otherwise, return false.  */
   1595  1.5  christos 
   1596  1.5  christos static int
   1597  1.5  christos check_ptrace_stopped_lwp_gone (struct lwp_info *lp)
   1598  1.5  christos {
   1599  1.5  christos   /* If we get an error after resuming the LWP successfully, we'd
   1600  1.5  christos      confuse !T state for the LWP being gone.  */
   1601  1.5  christos   gdb_assert (lp->stopped);
   1602  1.5  christos 
   1603  1.5  christos   /* We can't just check whether the LWP is in 'Z (Zombie)' state,
   1604  1.5  christos      because even if ptrace failed with ESRCH, the tracee may be "not
   1605  1.5  christos      yet fully dead", but already refusing ptrace requests.  In that
   1606  1.5  christos      case the tracee has 'R (Running)' state for a little bit
   1607  1.5  christos      (observed in Linux 3.18).  See also the note on ESRCH in the
   1608  1.5  christos      ptrace(2) man page.  Instead, check whether the LWP has any state
   1609  1.5  christos      other than ptrace-stopped.  */
   1610  1.5  christos 
   1611  1.5  christos   /* Don't assume anything if /proc/PID/status can't be read.  */
   1612  1.5  christos   if (linux_proc_pid_is_trace_stopped_nowarn (ptid_get_lwp (lp->ptid)) == 0)
   1613  1.5  christos     {
   1614  1.5  christos       lp->stop_reason = TARGET_STOPPED_BY_NO_REASON;
   1615  1.5  christos       lp->status = 0;
   1616  1.5  christos       lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
   1617  1.5  christos       return 1;
   1618  1.5  christos     }
   1619  1.5  christos   return 0;
   1620  1.5  christos }
   1621  1.5  christos 
   1622  1.5  christos /* Like linux_resume_one_lwp_throw, but no error is thrown if the LWP
   1623  1.5  christos    disappears while we try to resume it.  */
   1624  1.5  christos 
   1625  1.5  christos static void
   1626  1.5  christos linux_resume_one_lwp (struct lwp_info *lp, int step, enum gdb_signal signo)
   1627  1.5  christos {
   1628  1.5  christos   TRY
   1629  1.5  christos     {
   1630  1.5  christos       linux_resume_one_lwp_throw (lp, step, signo);
   1631  1.5  christos     }
   1632  1.5  christos   CATCH (ex, RETURN_MASK_ERROR)
   1633  1.5  christos     {
   1634  1.1  christos       if (!check_ptrace_stopped_lwp_gone (lp))
   1635  1.1  christos 	throw_exception (ex);
   1636  1.1  christos     }
   1637  1.1  christos   END_CATCH
   1638  1.1  christos }
   1639  1.1  christos 
   1640  1.1  christos /* Resume LP.  */
   1641  1.3  christos 
   1642  1.1  christos static void
   1643  1.1  christos resume_lwp (struct lwp_info *lp, int step, enum gdb_signal signo)
   1644  1.1  christos {
   1645  1.1  christos   if (lp->stopped)
   1646  1.1  christos     {
   1647  1.1  christos       struct inferior *inf = find_inferior_ptid (lp->ptid);
   1648  1.1  christos 
   1649  1.1  christos       if (inf->vfork_child != NULL)
   1650  1.3  christos 	{
   1651  1.1  christos 	  if (debug_linux_nat)
   1652  1.1  christos 	    fprintf_unfiltered (gdb_stdlog,
   1653  1.1  christos 				"RC: Not resuming %s (vfork parent)\n",
   1654  1.1  christos 				target_pid_to_str (lp->ptid));
   1655  1.1  christos 	}
   1656  1.1  christos       else if (!lwp_status_pending_p (lp))
   1657  1.1  christos 	{
   1658  1.1  christos 	  if (debug_linux_nat)
   1659  1.1  christos 	    fprintf_unfiltered (gdb_stdlog,
   1660  1.1  christos 				"RC: Resuming sibling %s, %s, %s\n",
   1661  1.3  christos 				target_pid_to_str (lp->ptid),
   1662  1.1  christos 				(signo != GDB_SIGNAL_0
   1663  1.1  christos 				 ? strsignal (gdb_signal_to_host (signo))
   1664  1.1  christos 				 : "0"),
   1665  1.1  christos 				step ? "step" : "resume");
   1666  1.1  christos 
   1667  1.1  christos 	  linux_resume_one_lwp (lp, step, signo);
   1668  1.1  christos 	}
   1669  1.1  christos       else
   1670  1.1  christos 	{
   1671  1.1  christos 	  if (debug_linux_nat)
   1672  1.1  christos 	    fprintf_unfiltered (gdb_stdlog,
   1673  1.1  christos 				"RC: Not resuming sibling %s (has pending)\n",
   1674  1.1  christos 				target_pid_to_str (lp->ptid));
   1675  1.1  christos 	}
   1676  1.1  christos     }
   1677  1.1  christos   else
   1678  1.1  christos     {
   1679  1.1  christos       if (debug_linux_nat)
   1680  1.3  christos 	fprintf_unfiltered (gdb_stdlog,
   1681  1.3  christos 			    "RC: Not resuming sibling %s (not stopped)\n",
   1682  1.1  christos 			    target_pid_to_str (lp->ptid));
   1683  1.1  christos     }
   1684  1.3  christos }
   1685  1.1  christos 
   1686  1.1  christos /* Callback for iterate_over_lwps.  If LWP is EXCEPT, do nothing.
   1687  1.1  christos    Resume LWP with the last stop signal, if it is in pass state.  */
   1688  1.3  christos 
   1689  1.3  christos static int
   1690  1.3  christos linux_nat_resume_callback (struct lwp_info *lp, void *except)
   1691  1.1  christos {
   1692  1.1  christos   enum gdb_signal signo = GDB_SIGNAL_0;
   1693  1.1  christos 
   1694  1.1  christos   if (lp == except)
   1695  1.1  christos     return 0;
   1696  1.1  christos 
   1697  1.1  christos   if (lp->stopped)
   1698  1.3  christos     {
   1699  1.1  christos       struct thread_info *thread;
   1700  1.1  christos 
   1701  1.1  christos       thread = find_thread_ptid (lp->ptid);
   1702  1.1  christos       if (thread != NULL)
   1703  1.1  christos 	{
   1704  1.1  christos 	  signo = thread->suspend.stop_signal;
   1705  1.1  christos 	  thread->suspend.stop_signal = GDB_SIGNAL_0;
   1706  1.1  christos 	}
   1707  1.1  christos     }
   1708  1.1  christos 
   1709  1.1  christos   resume_lwp (lp, 0, signo);
   1710  1.1  christos   return 0;
   1711  1.1  christos }
   1712  1.1  christos 
   1713  1.1  christos static int
   1714  1.1  christos resume_clear_callback (struct lwp_info *lp, void *data)
   1715  1.1  christos {
   1716  1.1  christos   lp->resumed = 0;
   1717  1.1  christos   lp->last_resume_kind = resume_stop;
   1718  1.1  christos   return 0;
   1719  1.1  christos }
   1720  1.1  christos 
   1721  1.1  christos static int
   1722  1.1  christos resume_set_callback (struct lwp_info *lp, void *data)
   1723  1.1  christos {
   1724  1.1  christos   lp->resumed = 1;
   1725  1.1  christos   lp->last_resume_kind = resume_continue;
   1726  1.1  christos   return 0;
   1727  1.1  christos }
   1728  1.1  christos 
   1729  1.1  christos static void
   1730  1.1  christos linux_nat_resume (struct target_ops *ops,
   1731  1.1  christos 		  ptid_t ptid, int step, enum gdb_signal signo)
   1732  1.1  christos {
   1733  1.1  christos   struct lwp_info *lp;
   1734  1.1  christos   int resume_many;
   1735  1.1  christos 
   1736  1.1  christos   if (debug_linux_nat)
   1737  1.1  christos     fprintf_unfiltered (gdb_stdlog,
   1738  1.1  christos 			"LLR: Preparing to %s %s, %s, inferior_ptid %s\n",
   1739  1.1  christos 			step ? "step" : "resume",
   1740  1.1  christos 			target_pid_to_str (ptid),
   1741  1.1  christos 			(signo != GDB_SIGNAL_0
   1742  1.1  christos 			 ? strsignal (gdb_signal_to_host (signo)) : "0"),
   1743  1.1  christos 			target_pid_to_str (inferior_ptid));
   1744  1.1  christos 
   1745  1.1  christos   /* A specific PTID means `step only this process id'.  */
   1746  1.1  christos   resume_many = (ptid_equal (minus_one_ptid, ptid)
   1747  1.1  christos 		 || ptid_is_pid (ptid));
   1748  1.1  christos 
   1749  1.1  christos   /* Mark the lwps we're resuming as resumed.  */
   1750  1.1  christos   iterate_over_lwps (ptid, resume_set_callback, NULL);
   1751  1.1  christos 
   1752  1.1  christos   /* See if it's the current inferior that should be handled
   1753  1.1  christos      specially.  */
   1754  1.1  christos   if (resume_many)
   1755  1.1  christos     lp = find_lwp_pid (inferior_ptid);
   1756  1.1  christos   else
   1757  1.1  christos     lp = find_lwp_pid (ptid);
   1758  1.1  christos   gdb_assert (lp != NULL);
   1759  1.1  christos 
   1760  1.1  christos   /* Remember if we're stepping.  */
   1761  1.1  christos   lp->last_resume_kind = step ? resume_step : resume_continue;
   1762  1.1  christos 
   1763  1.1  christos   /* If we have a pending wait status for this thread, there is no
   1764  1.1  christos      point in resuming the process.  But first make sure that
   1765  1.1  christos      linux_nat_wait won't preemptively handle the event - we
   1766  1.1  christos      should never take this short-circuit if we are going to
   1767  1.1  christos      leave LP running, since we have skipped resuming all the
   1768  1.1  christos      other threads.  This bit of code needs to be synchronized
   1769  1.1  christos      with linux_nat_wait.  */
   1770  1.1  christos 
   1771  1.1  christos   if (lp->status && WIFSTOPPED (lp->status))
   1772  1.1  christos     {
   1773  1.1  christos       if (!lp->step
   1774  1.1  christos 	  && WSTOPSIG (lp->status)
   1775  1.1  christos 	  && sigismember (&pass_mask, WSTOPSIG (lp->status)))
   1776  1.1  christos 	{
   1777  1.1  christos 	  if (debug_linux_nat)
   1778  1.1  christos 	    fprintf_unfiltered (gdb_stdlog,
   1779  1.1  christos 				"LLR: Not short circuiting for ignored "
   1780  1.1  christos 				"status 0x%x\n", lp->status);
   1781  1.1  christos 
   1782  1.1  christos 	  /* FIXME: What should we do if we are supposed to continue
   1783  1.1  christos 	     this thread with a signal?  */
   1784  1.3  christos 	  gdb_assert (signo == GDB_SIGNAL_0);
   1785  1.1  christos 	  signo = gdb_signal_from_host (WSTOPSIG (lp->status));
   1786  1.1  christos 	  lp->status = 0;
   1787  1.1  christos 	}
   1788  1.1  christos     }
   1789  1.1  christos 
   1790  1.1  christos   if (lwp_status_pending_p (lp))
   1791  1.1  christos     {
   1792  1.1  christos       /* FIXME: What should we do if we are supposed to continue
   1793  1.1  christos 	 this thread with a signal?  */
   1794  1.1  christos       gdb_assert (signo == GDB_SIGNAL_0);
   1795  1.1  christos 
   1796  1.1  christos       if (debug_linux_nat)
   1797  1.5  christos 	fprintf_unfiltered (gdb_stdlog,
   1798  1.1  christos 			    "LLR: Short circuiting for status 0x%x\n",
   1799  1.1  christos 			    lp->status);
   1800  1.1  christos 
   1801  1.1  christos       if (target_can_async_p ())
   1802  1.1  christos 	{
   1803  1.1  christos 	  target_async (1);
   1804  1.1  christos 	  /* Tell the event loop we have something to process.  */
   1805  1.3  christos 	  async_file_mark ();
   1806  1.1  christos 	}
   1807  1.1  christos       return;
   1808  1.1  christos     }
   1809  1.1  christos 
   1810  1.1  christos   if (resume_many)
   1811  1.5  christos     iterate_over_lwps (ptid, linux_nat_resume_callback, lp);
   1812  1.1  christos 
   1813  1.1  christos   if (debug_linux_nat)
   1814  1.1  christos     fprintf_unfiltered (gdb_stdlog,
   1815  1.5  christos 			"LLR: %s %s, %s (resume event thread)\n",
   1816  1.5  christos 			step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
   1817  1.1  christos 			target_pid_to_str (lp->ptid),
   1818  1.5  christos 			(signo != GDB_SIGNAL_0
   1819  1.1  christos 			 ? strsignal (gdb_signal_to_host (signo)) : "0"));
   1820  1.1  christos 
   1821  1.1  christos   linux_resume_one_lwp (lp, step, signo);
   1822  1.1  christos 
   1823  1.1  christos   if (target_can_async_p ())
   1824  1.1  christos     target_async (1);
   1825  1.1  christos }
   1826  1.6  christos 
   1827  1.1  christos /* Send a signal to an LWP.  */
   1828  1.6  christos 
   1829  1.6  christos static int
   1830  1.6  christos kill_lwp (int lwpid, int signo)
   1831  1.6  christos {
   1832  1.6  christos   int ret;
   1833  1.6  christos 
   1834  1.6  christos   errno = 0;
   1835  1.6  christos   ret = syscall (__NR_tkill, lwpid, signo);
   1836  1.6  christos   if (errno == ENOSYS)
   1837  1.1  christos     {
   1838  1.1  christos       /* If tkill fails, then we are not using nptl threads, a
   1839  1.1  christos 	 configuration we no longer support.  */
   1840  1.1  christos       perror_with_name (("tkill"));
   1841  1.1  christos     }
   1842  1.1  christos   return ret;
   1843  1.1  christos }
   1844  1.1  christos 
   1845  1.1  christos /* Handle a GNU/Linux syscall trap wait response.  If we see a syscall
   1846  1.1  christos    event, check if the core is interested in it: if not, ignore the
   1847  1.1  christos    event, and keep waiting; otherwise, we need to toggle the LWP's
   1848  1.1  christos    syscall entry/exit status, since the ptrace event itself doesn't
   1849  1.1  christos    indicate it, and report the trap to higher layers.  */
   1850  1.1  christos 
   1851  1.1  christos static int
   1852  1.1  christos linux_handle_syscall_trap (struct lwp_info *lp, int stopping)
   1853  1.1  christos {
   1854  1.1  christos   struct target_waitstatus *ourstatus = &lp->waitstatus;
   1855  1.1  christos   struct gdbarch *gdbarch = target_thread_architecture (lp->ptid);
   1856  1.1  christos   int syscall_number = (int) gdbarch_get_syscall_number (gdbarch, lp->ptid);
   1857  1.1  christos 
   1858  1.1  christos   if (stopping)
   1859  1.1  christos     {
   1860  1.1  christos       /* If we're stopping threads, there's a SIGSTOP pending, which
   1861  1.1  christos 	 makes it so that the LWP reports an immediate syscall return,
   1862  1.1  christos 	 followed by the SIGSTOP.  Skip seeing that "return" using
   1863  1.1  christos 	 PTRACE_CONT directly, and let stop_wait_callback collect the
   1864  1.1  christos 	 SIGSTOP.  Later when the thread is resumed, a new syscall
   1865  1.1  christos 	 entry event.  If we didn't do this (and returned 0), we'd
   1866  1.1  christos 	 leave a syscall entry pending, and our caller, by using
   1867  1.1  christos 	 PTRACE_CONT to collect the SIGSTOP, skips the syscall return
   1868  1.1  christos 	 itself.  Later, when the user re-resumes this LWP, we'd see
   1869  1.1  christos 	 another syscall entry event and we'd mistake it for a return.
   1870  1.1  christos 
   1871  1.1  christos 	 If stop_wait_callback didn't force the SIGSTOP out of the LWP
   1872  1.1  christos 	 (leaving immediately with LWP->signalled set, without issuing
   1873  1.1  christos 	 a PTRACE_CONT), it would still be problematic to leave this
   1874  1.1  christos 	 syscall enter pending, as later when the thread is resumed,
   1875  1.1  christos 	 it would then see the same syscall exit mentioned above,
   1876  1.1  christos 	 followed by the delayed SIGSTOP, while the syscall didn't
   1877  1.1  christos 	 actually get to execute.  It seems it would be even more
   1878  1.1  christos 	 confusing to the user.  */
   1879  1.1  christos 
   1880  1.1  christos       if (debug_linux_nat)
   1881  1.1  christos 	fprintf_unfiltered (gdb_stdlog,
   1882  1.1  christos 			    "LHST: ignoring syscall %d "
   1883  1.1  christos 			    "for LWP %ld (stopping threads), "
   1884  1.3  christos 			    "resuming with PTRACE_CONT for SIGSTOP\n",
   1885  1.1  christos 			    syscall_number,
   1886  1.1  christos 			    ptid_get_lwp (lp->ptid));
   1887  1.1  christos 
   1888  1.6  christos       lp->syscall_state = TARGET_WAITKIND_IGNORE;
   1889  1.6  christos       ptrace (PTRACE_CONT, ptid_get_lwp (lp->ptid), 0, 0);
   1890  1.6  christos       lp->stopped = 0;
   1891  1.6  christos       return 1;
   1892  1.6  christos     }
   1893  1.6  christos 
   1894  1.6  christos   /* Always update the entry/return state, even if this particular
   1895  1.6  christos      syscall isn't interesting to the core now.  In async mode,
   1896  1.6  christos      the user could install a new catchpoint for this syscall
   1897  1.1  christos      between syscall enter/return, and we'll need to know to
   1898  1.1  christos      report a syscall return if that happens.  */
   1899  1.1  christos   lp->syscall_state = (lp->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY
   1900  1.1  christos 		       ? TARGET_WAITKIND_SYSCALL_RETURN
   1901  1.1  christos 		       : TARGET_WAITKIND_SYSCALL_ENTRY);
   1902  1.1  christos 
   1903  1.1  christos   if (catch_syscall_enabled ())
   1904  1.1  christos     {
   1905  1.1  christos       if (catching_syscall_number (syscall_number))
   1906  1.1  christos 	{
   1907  1.1  christos 	  /* Alright, an event to report.  */
   1908  1.1  christos 	  ourstatus->kind = lp->syscall_state;
   1909  1.1  christos 	  ourstatus->value.syscall_number = syscall_number;
   1910  1.1  christos 
   1911  1.1  christos 	  if (debug_linux_nat)
   1912  1.1  christos 	    fprintf_unfiltered (gdb_stdlog,
   1913  1.1  christos 				"LHST: stopping for %s of syscall %d"
   1914  1.1  christos 				" for LWP %ld\n",
   1915  1.1  christos 				lp->syscall_state
   1916  1.1  christos 				== TARGET_WAITKIND_SYSCALL_ENTRY
   1917  1.1  christos 				? "entry" : "return",
   1918  1.1  christos 				syscall_number,
   1919  1.1  christos 				ptid_get_lwp (lp->ptid));
   1920  1.1  christos 	  return 0;
   1921  1.1  christos 	}
   1922  1.1  christos 
   1923  1.1  christos       if (debug_linux_nat)
   1924  1.1  christos 	fprintf_unfiltered (gdb_stdlog,
   1925  1.1  christos 			    "LHST: ignoring %s of syscall %d "
   1926  1.1  christos 			    "for LWP %ld\n",
   1927  1.1  christos 			    lp->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY
   1928  1.1  christos 			    ? "entry" : "return",
   1929  1.1  christos 			    syscall_number,
   1930  1.1  christos 			    ptid_get_lwp (lp->ptid));
   1931  1.1  christos     }
   1932  1.1  christos   else
   1933  1.1  christos     {
   1934  1.1  christos       /* If we had been syscall tracing, and hence used PT_SYSCALL
   1935  1.1  christos 	 before on this LWP, it could happen that the user removes all
   1936  1.1  christos 	 syscall catchpoints before we get to process this event.
   1937  1.1  christos 	 There are two noteworthy issues here:
   1938  1.1  christos 
   1939  1.1  christos 	 - When stopped at a syscall entry event, resuming with
   1940  1.1  christos 	   PT_STEP still resumes executing the syscall and reports a
   1941  1.1  christos 	   syscall return.
   1942  1.1  christos 
   1943  1.1  christos 	 - Only PT_SYSCALL catches syscall enters.  If we last
   1944  1.1  christos 	   single-stepped this thread, then this event can't be a
   1945  1.1  christos 	   syscall enter.  If we last single-stepped this thread, this
   1946  1.1  christos 	   has to be a syscall exit.
   1947  1.1  christos 
   1948  1.1  christos 	 The points above mean that the next resume, be it PT_STEP or
   1949  1.1  christos 	 PT_CONTINUE, can not trigger a syscall trace event.  */
   1950  1.1  christos       if (debug_linux_nat)
   1951  1.1  christos 	fprintf_unfiltered (gdb_stdlog,
   1952  1.1  christos 			    "LHST: caught syscall event "
   1953  1.1  christos 			    "with no syscall catchpoints."
   1954  1.1  christos 			    " %d for LWP %ld, ignoring\n",
   1955  1.1  christos 			    syscall_number,
   1956  1.1  christos 			    ptid_get_lwp (lp->ptid));
   1957  1.1  christos       lp->syscall_state = TARGET_WAITKIND_IGNORE;
   1958  1.1  christos     }
   1959  1.1  christos 
   1960  1.1  christos   /* The core isn't interested in this event.  For efficiency, avoid
   1961  1.3  christos      stopping all threads only to have the core resume them all again.
   1962  1.1  christos      Since we're not stopping threads, if we're still syscall tracing
   1963  1.1  christos      and not stepping, we can't use PTRACE_CONT here, as we'd miss any
   1964  1.1  christos      subsequent syscall.  Simply resume using the inf-ptrace layer,
   1965  1.1  christos      which knows when to use PT_SYSCALL or PT_CONTINUE.  */
   1966  1.1  christos 
   1967  1.1  christos   linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0);
   1968  1.1  christos   return 1;
   1969  1.1  christos }
   1970  1.1  christos 
   1971  1.1  christos /* Handle a GNU/Linux extended wait response.  If we see a clone
   1972  1.5  christos    event, we need to add the new LWP to our list (and not report the
   1973  1.1  christos    trap to higher layers).  This function returns non-zero if the
   1974  1.1  christos    event should be ignored and we should wait again.  If STOPPING is
   1975  1.1  christos    true, the new LWP remains stopped, otherwise it is continued.  */
   1976  1.3  christos 
   1977  1.1  christos static int
   1978  1.6  christos linux_handle_extended_wait (struct lwp_info *lp, int status)
   1979  1.6  christos {
   1980  1.6  christos   int pid = ptid_get_lwp (lp->ptid);
   1981  1.6  christos   struct target_waitstatus *ourstatus = &lp->waitstatus;
   1982  1.6  christos   int event = linux_ptrace_get_extended_event (status);
   1983  1.1  christos 
   1984  1.1  christos   /* All extended events we currently use are mid-syscall.  Only
   1985  1.1  christos      PTRACE_EVENT_STOP is delivered more like a signal-stop, but
   1986  1.1  christos      you have to be using PTRACE_SEIZE to get that.  */
   1987  1.1  christos   lp->syscall_state = TARGET_WAITKIND_SYSCALL_ENTRY;
   1988  1.1  christos 
   1989  1.1  christos   if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK
   1990  1.1  christos       || event == PTRACE_EVENT_CLONE)
   1991  1.1  christos     {
   1992  1.1  christos       unsigned long new_pid;
   1993  1.1  christos       int ret;
   1994  1.1  christos 
   1995  1.1  christos       ptrace (PTRACE_GETEVENTMSG, pid, 0, &new_pid);
   1996  1.6  christos 
   1997  1.1  christos       /* If we haven't already seen the new PID stop, wait for it now.  */
   1998  1.1  christos       if (! pull_pid_from_list (&stopped_pids, new_pid, &status))
   1999  1.1  christos 	{
   2000  1.1  christos 	  /* The new child has a pending SIGSTOP.  We can't affect it until it
   2001  1.1  christos 	     hits the SIGSTOP, but we're already attached.  */
   2002  1.1  christos 	  ret = my_waitpid (new_pid, &status, __WALL);
   2003  1.1  christos 	  if (ret == -1)
   2004  1.1  christos 	    perror_with_name (_("waiting for new child"));
   2005  1.1  christos 	  else if (ret != new_pid)
   2006  1.1  christos 	    internal_error (__FILE__, __LINE__,
   2007  1.1  christos 			    _("wait returned unexpected PID %d"), ret);
   2008  1.1  christos 	  else if (!WIFSTOPPED (status))
   2009  1.1  christos 	    internal_error (__FILE__, __LINE__,
   2010  1.1  christos 			    _("wait returned unexpected status 0x%x"), status);
   2011  1.1  christos 	}
   2012  1.1  christos 
   2013  1.1  christos       ourstatus->value.related_pid = ptid_build (new_pid, new_pid, 0);
   2014  1.1  christos 
   2015  1.1  christos       if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK)
   2016  1.1  christos 	{
   2017  1.1  christos 	  /* The arch-specific native code may need to know about new
   2018  1.1  christos 	     forks even if those end up never mapped to an
   2019  1.1  christos 	     inferior.  */
   2020  1.1  christos 	  if (linux_nat_new_fork != NULL)
   2021  1.1  christos 	    linux_nat_new_fork (lp, new_pid);
   2022  1.1  christos 	}
   2023  1.1  christos 
   2024  1.1  christos       if (event == PTRACE_EVENT_FORK
   2025  1.1  christos 	  && linux_fork_checkpointing_p (ptid_get_pid (lp->ptid)))
   2026  1.1  christos 	{
   2027  1.1  christos 	  /* Handle checkpointing by linux-fork.c here as a special
   2028  1.1  christos 	     case.  We don't want the follow-fork-mode or 'catch fork'
   2029  1.1  christos 	     to interfere with this.  */
   2030  1.1  christos 
   2031  1.1  christos 	  /* This won't actually modify the breakpoint list, but will
   2032  1.1  christos 	     physically remove the breakpoints from the child.  */
   2033  1.1  christos 	  detach_breakpoints (ptid_build (new_pid, new_pid, 0));
   2034  1.1  christos 
   2035  1.1  christos 	  /* Retain child fork in ptrace (stopped) state.  */
   2036  1.1  christos 	  if (!find_fork_pid (new_pid))
   2037  1.1  christos 	    add_fork (new_pid);
   2038  1.1  christos 
   2039  1.1  christos 	  /* Report as spurious, so that infrun doesn't want to follow
   2040  1.1  christos 	     this fork.  We're actually doing an infcall in
   2041  1.1  christos 	     linux-fork.c.  */
   2042  1.1  christos 	  ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
   2043  1.1  christos 
   2044  1.1  christos 	  /* Report the stop to the core.  */
   2045  1.1  christos 	  return 0;
   2046  1.5  christos 	}
   2047  1.1  christos 
   2048  1.1  christos       if (event == PTRACE_EVENT_FORK)
   2049  1.1  christos 	ourstatus->kind = TARGET_WAITKIND_FORKED;
   2050  1.1  christos       else if (event == PTRACE_EVENT_VFORK)
   2051  1.1  christos 	ourstatus->kind = TARGET_WAITKIND_VFORKED;
   2052  1.1  christos       else if (event == PTRACE_EVENT_CLONE)
   2053  1.1  christos 	{
   2054  1.1  christos 	  struct lwp_info *new_lp;
   2055  1.1  christos 
   2056  1.1  christos 	  ourstatus->kind = TARGET_WAITKIND_IGNORE;
   2057  1.1  christos 
   2058  1.1  christos 	  if (debug_linux_nat)
   2059  1.1  christos 	    fprintf_unfiltered (gdb_stdlog,
   2060  1.5  christos 				"LHEW: Got clone event "
   2061  1.5  christos 				"from LWP %d, new child is LWP %ld\n",
   2062  1.5  christos 				pid, new_pid);
   2063  1.5  christos 
   2064  1.5  christos 	  new_lp = add_lwp (ptid_build (ptid_get_pid (lp->ptid), new_pid, 0));
   2065  1.5  christos 	  new_lp->stopped = 1;
   2066  1.5  christos 	  new_lp->resumed = 1;
   2067  1.5  christos 
   2068  1.5  christos 	  /* If the thread_db layer is active, let it record the user
   2069  1.5  christos 	     level thread id and status, and add the thread to GDB's
   2070  1.5  christos 	     list.  */
   2071  1.5  christos 	  if (!thread_db_notice_clone (lp->ptid, new_lp->ptid))
   2072  1.5  christos 	    {
   2073  1.5  christos 	      /* The process is not using thread_db.  Add the LWP to
   2074  1.5  christos 		 GDB's list.  */
   2075  1.5  christos 	      target_post_attach (ptid_get_lwp (new_lp->ptid));
   2076  1.5  christos 	      add_thread (new_lp->ptid);
   2077  1.5  christos 	    }
   2078  1.5  christos 
   2079  1.1  christos 	  /* Even if we're stopping the thread for some reason
   2080  1.1  christos 	     internal to this module, from the perspective of infrun
   2081  1.1  christos 	     and the user/frontend, this new thread is running until
   2082  1.1  christos 	     it next reports a stop.  */
   2083  1.1  christos 	  set_running (new_lp->ptid, 1);
   2084  1.1  christos 	  set_executing (new_lp->ptid, 1);
   2085  1.1  christos 
   2086  1.1  christos 	  if (WSTOPSIG (status) != SIGSTOP)
   2087  1.5  christos 	    {
   2088  1.1  christos 	      /* This can happen if someone starts sending signals to
   2089  1.1  christos 		 the new thread before it gets a chance to run, which
   2090  1.1  christos 		 have a lower number than SIGSTOP (e.g. SIGUSR1).
   2091  1.1  christos 		 This is an unlikely case, and harder to handle for
   2092  1.1  christos 		 fork / vfork than for clone, so we do not try - but
   2093  1.1  christos 		 we handle it for clone events here.  */
   2094  1.1  christos 
   2095  1.1  christos 	      new_lp->signalled = 1;
   2096  1.1  christos 
   2097  1.1  christos 	      /* We created NEW_LP so it cannot yet contain STATUS.  */
   2098  1.1  christos 	      gdb_assert (new_lp->status == 0);
   2099  1.1  christos 
   2100  1.1  christos 	      /* Save the wait status to report later.  */
   2101  1.1  christos 	      if (debug_linux_nat)
   2102  1.1  christos 		fprintf_unfiltered (gdb_stdlog,
   2103  1.6  christos 				    "LHEW: waitpid of new LWP %ld, "
   2104  1.6  christos 				    "saving status %s\n",
   2105  1.6  christos 				    (long) ptid_get_lwp (new_lp->ptid),
   2106  1.6  christos 				    status_to_str (status));
   2107  1.6  christos 	      new_lp->status = status;
   2108  1.1  christos 	    }
   2109  1.1  christos 	  else if (report_thread_events)
   2110  1.1  christos 	    {
   2111  1.1  christos 	      new_lp->waitstatus.kind = TARGET_WAITKIND_THREAD_CREATED;
   2112  1.1  christos 	      new_lp->status = status;
   2113  1.1  christos 	    }
   2114  1.1  christos 
   2115  1.1  christos 	  return 1;
   2116  1.1  christos 	}
   2117  1.1  christos 
   2118  1.1  christos       return 0;
   2119  1.1  christos     }
   2120  1.1  christos 
   2121  1.1  christos   if (event == PTRACE_EVENT_EXEC)
   2122  1.1  christos     {
   2123  1.1  christos       if (debug_linux_nat)
   2124  1.3  christos 	fprintf_unfiltered (gdb_stdlog,
   2125  1.1  christos 			    "LHEW: Got exec event from LWP %ld\n",
   2126  1.3  christos 			    ptid_get_lwp (lp->ptid));
   2127  1.3  christos 
   2128  1.3  christos       ourstatus->kind = TARGET_WAITKIND_EXECD;
   2129  1.3  christos       ourstatus->value.execd_pathname
   2130  1.1  christos 	= xstrdup (linux_child_pid_to_exec_file (NULL, pid));
   2131  1.1  christos 
   2132  1.1  christos       /* The thread that execed must have been resumed, but, when a
   2133  1.1  christos 	 thread execs, it changes its tid to the tgid, and the old
   2134  1.1  christos 	 tgid thread might have not been resumed.  */
   2135  1.1  christos       lp->resumed = 1;
   2136  1.1  christos       return 0;
   2137  1.1  christos     }
   2138  1.1  christos 
   2139  1.1  christos   if (event == PTRACE_EVENT_VFORK_DONE)
   2140  1.1  christos     {
   2141  1.1  christos       if (current_inferior ()->waiting_for_vfork_done)
   2142  1.1  christos 	{
   2143  1.1  christos 	  if (debug_linux_nat)
   2144  1.1  christos 	    fprintf_unfiltered (gdb_stdlog,
   2145  1.1  christos 				"LHEW: Got expected PTRACE_EVENT_"
   2146  1.1  christos 				"VFORK_DONE from LWP %ld: stopping\n",
   2147  1.1  christos 				ptid_get_lwp (lp->ptid));
   2148  1.1  christos 
   2149  1.1  christos 	  ourstatus->kind = TARGET_WAITKIND_VFORK_DONE;
   2150  1.5  christos 	  return 0;
   2151  1.1  christos 	}
   2152  1.1  christos 
   2153  1.1  christos       if (debug_linux_nat)
   2154  1.1  christos 	fprintf_unfiltered (gdb_stdlog,
   2155  1.1  christos 			    "LHEW: Got PTRACE_EVENT_VFORK_DONE "
   2156  1.1  christos 			    "from LWP %ld: ignoring\n",
   2157  1.1  christos 			    ptid_get_lwp (lp->ptid));
   2158  1.1  christos       return 1;
   2159  1.1  christos     }
   2160  1.1  christos 
   2161  1.1  christos   internal_error (__FILE__, __LINE__,
   2162  1.1  christos 		  _("unknown ptrace event %d"), event);
   2163  1.1  christos }
   2164  1.1  christos 
   2165  1.1  christos /* Wait for LP to stop.  Returns the wait status, or 0 if the LWP has
   2166  1.1  christos    exited.  */
   2167  1.1  christos 
   2168  1.1  christos static int
   2169  1.1  christos wait_lwp (struct lwp_info *lp)
   2170  1.1  christos {
   2171  1.1  christos   pid_t pid;
   2172  1.1  christos   int status = 0;
   2173  1.1  christos   int thread_dead = 0;
   2174  1.1  christos   sigset_t prev_mask;
   2175  1.1  christos 
   2176  1.1  christos   gdb_assert (!lp->stopped);
   2177  1.1  christos   gdb_assert (lp->status == 0);
   2178  1.6  christos 
   2179  1.1  christos   /* Make sure SIGCHLD is blocked for sigsuspend avoiding a race below.  */
   2180  1.1  christos   block_child_signals (&prev_mask);
   2181  1.1  christos 
   2182  1.6  christos   for (;;)
   2183  1.6  christos     {
   2184  1.6  christos       pid = my_waitpid (ptid_get_lwp (lp->ptid), &status, __WALL | WNOHANG);
   2185  1.1  christos       if (pid == -1 && errno == ECHILD)
   2186  1.1  christos 	{
   2187  1.1  christos 	  /* The thread has previously exited.  We need to delete it
   2188  1.1  christos 	     now because if this was a non-leader thread execing, we
   2189  1.1  christos 	     won't get an exit event.  See comments on exec events at
   2190  1.1  christos 	     the top of the file.  */
   2191  1.1  christos 	  thread_dead = 1;
   2192  1.1  christos 	  if (debug_linux_nat)
   2193  1.1  christos 	    fprintf_unfiltered (gdb_stdlog, "WL: %s vanished.\n",
   2194  1.1  christos 				target_pid_to_str (lp->ptid));
   2195  1.1  christos 	}
   2196  1.1  christos       if (pid != 0)
   2197  1.6  christos 	break;
   2198  1.6  christos 
   2199  1.1  christos       /* Bugs 10970, 12702.
   2200  1.1  christos 	 Thread group leader may have exited in which case we'll lock up in
   2201  1.1  christos 	 waitpid if there are other threads, even if they are all zombies too.
   2202  1.1  christos 	 Basically, we're not supposed to use waitpid this way.
   2203  1.1  christos 	  tkill(pid,0) cannot be used here as it gets ESRCH for both
   2204  1.1  christos 	 for zombie and running processes.
   2205  1.1  christos 
   2206  1.1  christos 	 As a workaround, check if we're waiting for the thread group leader and
   2207  1.1  christos 	 if it's a zombie, and avoid calling waitpid if it is.
   2208  1.1  christos 
   2209  1.1  christos 	 This is racy, what if the tgl becomes a zombie right after we check?
   2210  1.1  christos 	 Therefore always use WNOHANG with sigsuspend - it is equivalent to
   2211  1.1  christos 	 waiting waitpid but linux_proc_pid_is_zombie is safe this way.  */
   2212  1.1  christos 
   2213  1.1  christos       if (ptid_get_pid (lp->ptid) == ptid_get_lwp (lp->ptid)
   2214  1.1  christos 	  && linux_proc_pid_is_zombie (ptid_get_lwp (lp->ptid)))
   2215  1.1  christos 	{
   2216  1.1  christos 	  thread_dead = 1;
   2217  1.1  christos 	  if (debug_linux_nat)
   2218  1.1  christos 	    fprintf_unfiltered (gdb_stdlog,
   2219  1.1  christos 				"WL: Thread group leader %s vanished.\n",
   2220  1.1  christos 				target_pid_to_str (lp->ptid));
   2221  1.1  christos 	  break;
   2222  1.1  christos 	}
   2223  1.1  christos 
   2224  1.1  christos       /* Wait for next SIGCHLD and try again.  This may let SIGCHLD handlers
   2225  1.3  christos 	 get invoked despite our caller had them intentionally blocked by
   2226  1.3  christos 	 block_child_signals.  This is sensitive only to the loop of
   2227  1.1  christos 	 linux_nat_wait_1 and there if we get called my_waitpid gets called
   2228  1.1  christos 	 again before it gets to sigsuspend so we can safely let the handlers
   2229  1.1  christos 	 get executed here.  */
   2230  1.1  christos 
   2231  1.1  christos       if (debug_linux_nat)
   2232  1.1  christos 	fprintf_unfiltered (gdb_stdlog, "WL: about to sigsuspend\n");
   2233  1.1  christos       sigsuspend (&suspend_mask);
   2234  1.1  christos     }
   2235  1.1  christos 
   2236  1.1  christos   restore_child_signals_mask (&prev_mask);
   2237  1.1  christos 
   2238  1.1  christos   if (!thread_dead)
   2239  1.1  christos     {
   2240  1.1  christos       gdb_assert (pid == ptid_get_lwp (lp->ptid));
   2241  1.1  christos 
   2242  1.1  christos       if (debug_linux_nat)
   2243  1.1  christos 	{
   2244  1.1  christos 	  fprintf_unfiltered (gdb_stdlog,
   2245  1.1  christos 			      "WL: waitpid %s received %s\n",
   2246  1.1  christos 			      target_pid_to_str (lp->ptid),
   2247  1.6  christos 			      status_to_str (status));
   2248  1.6  christos 	}
   2249  1.5  christos 
   2250  1.5  christos       /* Check if the thread has exited.  */
   2251  1.6  christos       if (WIFEXITED (status) || WIFSIGNALED (status))
   2252  1.5  christos 	{
   2253  1.5  christos 	  if (report_thread_events
   2254  1.6  christos 	      || ptid_get_pid (lp->ptid) == ptid_get_lwp (lp->ptid))
   2255  1.5  christos 	    {
   2256  1.5  christos 	      if (debug_linux_nat)
   2257  1.5  christos 		fprintf_unfiltered (gdb_stdlog, "WL: LWP %d exited.\n",
   2258  1.5  christos 				    ptid_get_pid (lp->ptid));
   2259  1.5  christos 
   2260  1.5  christos 	      /* If this is the leader exiting, it means the whole
   2261  1.5  christos 		 process is gone.  Store the status to report to the
   2262  1.1  christos 		 core.  Store it in lp->waitstatus, because lp->status
   2263  1.1  christos 		 would be ambiguous (W_EXITCODE(0,0) == 0).  */
   2264  1.1  christos 	      store_waitstatus (&lp->waitstatus, status);
   2265  1.1  christos 	      return 0;
   2266  1.1  christos 	    }
   2267  1.1  christos 
   2268  1.1  christos 	  thread_dead = 1;
   2269  1.1  christos 	  if (debug_linux_nat)
   2270  1.1  christos 	    fprintf_unfiltered (gdb_stdlog, "WL: %s exited.\n",
   2271  1.1  christos 				target_pid_to_str (lp->ptid));
   2272  1.1  christos 	}
   2273  1.1  christos     }
   2274  1.1  christos 
   2275  1.1  christos   if (thread_dead)
   2276  1.3  christos     {
   2277  1.3  christos       exit_lwp (lp);
   2278  1.3  christos       return 0;
   2279  1.3  christos     }
   2280  1.3  christos 
   2281  1.5  christos   gdb_assert (WIFSTOPPED (status));
   2282  1.3  christos   lp->stopped = 1;
   2283  1.5  christos 
   2284  1.3  christos   if (lp->must_set_ptrace_flags)
   2285  1.3  christos     {
   2286  1.1  christos       struct inferior *inf = find_inferior_pid (ptid_get_pid (lp->ptid));
   2287  1.1  christos       int options = linux_nat_ptrace_options (inf->attach_flag);
   2288  1.1  christos 
   2289  1.1  christos       linux_enable_event_reporting (ptid_get_lwp (lp->ptid), options);
   2290  1.1  christos       lp->must_set_ptrace_flags = 0;
   2291  1.1  christos     }
   2292  1.1  christos 
   2293  1.1  christos   /* Handle GNU/Linux's syscall SIGTRAPs.  */
   2294  1.1  christos   if (WIFSTOPPED (status) && WSTOPSIG (status) == SYSCALL_SIGTRAP)
   2295  1.1  christos     {
   2296  1.1  christos       /* No longer need the sysgood bit.  The ptrace event ends up
   2297  1.1  christos 	 recorded in lp->waitstatus if we care for it.  We can carry
   2298  1.6  christos 	 on handling the event like a regular SIGTRAP from here
   2299  1.6  christos 	 on.  */
   2300  1.6  christos       status = W_STOPCODE (SIGTRAP);
   2301  1.6  christos       if (linux_handle_syscall_trap (lp, 1))
   2302  1.6  christos 	return wait_lwp (lp);
   2303  1.6  christos     }
   2304  1.1  christos   else
   2305  1.1  christos     {
   2306  1.3  christos       /* Almost all other ptrace-stops are known to be outside of system
   2307  1.3  christos 	 calls, with further exceptions in linux_handle_extended_wait.  */
   2308  1.1  christos       lp->syscall_state = TARGET_WAITKIND_IGNORE;
   2309  1.1  christos     }
   2310  1.1  christos 
   2311  1.1  christos   /* Handle GNU/Linux's extended waitstatus for trace events.  */
   2312  1.1  christos   if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP
   2313  1.5  christos       && linux_is_extended_waitstatus (status))
   2314  1.5  christos     {
   2315  1.1  christos       if (debug_linux_nat)
   2316  1.1  christos 	fprintf_unfiltered (gdb_stdlog,
   2317  1.1  christos 			    "WL: Handling extended status 0x%06x\n",
   2318  1.1  christos 			    status);
   2319  1.1  christos       linux_handle_extended_wait (lp, status);
   2320  1.1  christos       return 0;
   2321  1.1  christos     }
   2322  1.1  christos 
   2323  1.1  christos   return status;
   2324  1.1  christos }
   2325  1.1  christos 
   2326  1.1  christos /* Send a SIGSTOP to LP.  */
   2327  1.1  christos 
   2328  1.1  christos static int
   2329  1.1  christos stop_callback (struct lwp_info *lp, void *data)
   2330  1.1  christos {
   2331  1.1  christos   if (!lp->stopped && !lp->signalled)
   2332  1.1  christos     {
   2333  1.1  christos       int ret;
   2334  1.1  christos 
   2335  1.1  christos       if (debug_linux_nat)
   2336  1.1  christos 	{
   2337  1.1  christos 	  fprintf_unfiltered (gdb_stdlog,
   2338  1.1  christos 			      "SC:  kill %s **<SIGSTOP>**\n",
   2339  1.1  christos 			      target_pid_to_str (lp->ptid));
   2340  1.1  christos 	}
   2341  1.1  christos       errno = 0;
   2342  1.1  christos       ret = kill_lwp (ptid_get_lwp (lp->ptid), SIGSTOP);
   2343  1.1  christos       if (debug_linux_nat)
   2344  1.1  christos 	{
   2345  1.1  christos 	  fprintf_unfiltered (gdb_stdlog,
   2346  1.1  christos 			      "SC:  lwp kill %d %s\n",
   2347  1.1  christos 			      ret,
   2348  1.1  christos 			      errno ? safe_strerror (errno) : "ERRNO-OK");
   2349  1.1  christos 	}
   2350  1.1  christos 
   2351  1.1  christos       lp->signalled = 1;
   2352  1.1  christos       gdb_assert (lp->status == 0);
   2353  1.1  christos     }
   2354  1.1  christos 
   2355  1.1  christos   return 0;
   2356  1.1  christos }
   2357  1.1  christos 
   2358  1.1  christos /* Request a stop on LWP.  */
   2359  1.1  christos 
   2360  1.5  christos void
   2361  1.5  christos linux_stop_lwp (struct lwp_info *lwp)
   2362  1.5  christos {
   2363  1.5  christos   stop_callback (lwp, NULL);
   2364  1.5  christos }
   2365  1.5  christos 
   2366  1.5  christos /* See linux-nat.h  */
   2367  1.5  christos 
   2368  1.5  christos void
   2369  1.5  christos linux_stop_and_wait_all_lwps (void)
   2370  1.5  christos {
   2371  1.5  christos   /* Stop all LWP's ...  */
   2372  1.5  christos   iterate_over_lwps (minus_one_ptid, stop_callback, NULL);
   2373  1.5  christos 
   2374  1.5  christos   /* ... and wait until all of them have reported back that
   2375  1.5  christos      they're no longer running.  */
   2376  1.5  christos   iterate_over_lwps (minus_one_ptid, stop_wait_callback, NULL);
   2377  1.5  christos }
   2378  1.5  christos 
   2379  1.5  christos /* See linux-nat.h  */
   2380  1.5  christos 
   2381  1.5  christos void
   2382  1.1  christos linux_unstop_all_lwps (void)
   2383  1.1  christos {
   2384  1.1  christos   iterate_over_lwps (minus_one_ptid,
   2385  1.1  christos 		     resume_stopped_resumed_lwps, &minus_one_ptid);
   2386  1.1  christos }
   2387  1.1  christos 
   2388  1.1  christos /* Return non-zero if LWP PID has a pending SIGINT.  */
   2389  1.1  christos 
   2390  1.1  christos static int
   2391  1.1  christos linux_nat_has_pending_sigint (int pid)
   2392  1.1  christos {
   2393  1.1  christos   sigset_t pending, blocked, ignored;
   2394  1.1  christos 
   2395  1.1  christos   linux_proc_pending_signals (pid, &pending, &blocked, &ignored);
   2396  1.1  christos 
   2397  1.1  christos   if (sigismember (&pending, SIGINT)
   2398  1.1  christos       && !sigismember (&ignored, SIGINT))
   2399  1.1  christos     return 1;
   2400  1.1  christos 
   2401  1.1  christos   return 0;
   2402  1.1  christos }
   2403  1.1  christos 
   2404  1.1  christos /* Set a flag in LP indicating that we should ignore its next SIGINT.  */
   2405  1.1  christos 
   2406  1.1  christos static int
   2407  1.1  christos set_ignore_sigint (struct lwp_info *lp, void *data)
   2408  1.1  christos {
   2409  1.1  christos   /* If a thread has a pending SIGINT, consume it; otherwise, set a
   2410  1.1  christos      flag to consume the next one.  */
   2411  1.1  christos   if (lp->stopped && lp->status != 0 && WIFSTOPPED (lp->status)
   2412  1.1  christos       && WSTOPSIG (lp->status) == SIGINT)
   2413  1.1  christos     lp->status = 0;
   2414  1.1  christos   else
   2415  1.1  christos     lp->ignore_sigint = 1;
   2416  1.1  christos 
   2417  1.1  christos   return 0;
   2418  1.1  christos }
   2419  1.1  christos 
   2420  1.1  christos /* If LP does not have a SIGINT pending, then clear the ignore_sigint flag.
   2421  1.1  christos    This function is called after we know the LWP has stopped; if the LWP
   2422  1.1  christos    stopped before the expected SIGINT was delivered, then it will never have
   2423  1.1  christos    arrived.  Also, if the signal was delivered to a shared queue and consumed
   2424  1.1  christos    by a different thread, it will never be delivered to this LWP.  */
   2425  1.1  christos 
   2426  1.1  christos static void
   2427  1.1  christos maybe_clear_ignore_sigint (struct lwp_info *lp)
   2428  1.1  christos {
   2429  1.1  christos   if (!lp->ignore_sigint)
   2430  1.1  christos     return;
   2431  1.1  christos 
   2432  1.1  christos   if (!linux_nat_has_pending_sigint (ptid_get_lwp (lp->ptid)))
   2433  1.1  christos     {
   2434  1.1  christos       if (debug_linux_nat)
   2435  1.1  christos 	fprintf_unfiltered (gdb_stdlog,
   2436  1.1  christos 			    "MCIS: Clearing bogus flag for %s\n",
   2437  1.1  christos 			    target_pid_to_str (lp->ptid));
   2438  1.1  christos       lp->ignore_sigint = 0;
   2439  1.1  christos     }
   2440  1.1  christos }
   2441  1.1  christos 
   2442  1.1  christos /* Fetch the possible triggered data watchpoint info and store it in
   2443  1.1  christos    LP.
   2444  1.1  christos 
   2445  1.1  christos    On some archs, like x86, that use debug registers to set
   2446  1.1  christos    watchpoints, it's possible that the way to know which watched
   2447  1.1  christos    address trapped, is to check the register that is used to select
   2448  1.1  christos    which address to watch.  Problem is, between setting the watchpoint
   2449  1.1  christos    and reading back which data address trapped, the user may change
   2450  1.1  christos    the set of watchpoints, and, as a consequence, GDB changes the
   2451  1.3  christos    debug registers in the inferior.  To avoid reading back a stale
   2452  1.3  christos    stopped-data-address when that happens, we cache in LP the fact
   2453  1.1  christos    that a watchpoint trapped, and the corresponding data address, as
   2454  1.1  christos    soon as we see LP stop with a SIGTRAP.  If GDB changes the debug
   2455  1.1  christos    registers meanwhile, we have the cached data we can rely on.  */
   2456  1.1  christos 
   2457  1.3  christos static int
   2458  1.1  christos check_stopped_by_watchpoint (struct lwp_info *lp)
   2459  1.1  christos {
   2460  1.1  christos   struct cleanup *old_chain;
   2461  1.1  christos 
   2462  1.3  christos   if (linux_ops->to_stopped_by_watchpoint == NULL)
   2463  1.3  christos     return 0;
   2464  1.5  christos 
   2465  1.1  christos   old_chain = save_inferior_ptid ();
   2466  1.1  christos   inferior_ptid = lp->ptid;
   2467  1.1  christos 
   2468  1.1  christos   if (linux_ops->to_stopped_by_watchpoint (linux_ops))
   2469  1.1  christos     {
   2470  1.1  christos       lp->stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
   2471  1.1  christos 
   2472  1.1  christos       if (linux_ops->to_stopped_data_address != NULL)
   2473  1.1  christos 	lp->stopped_data_address_p =
   2474  1.1  christos 	  linux_ops->to_stopped_data_address (&current_target,
   2475  1.3  christos 					      &lp->stopped_data_address);
   2476  1.5  christos       else
   2477  1.3  christos 	lp->stopped_data_address_p = 0;
   2478  1.3  christos     }
   2479  1.3  christos 
   2480  1.1  christos   do_cleanups (old_chain);
   2481  1.1  christos 
   2482  1.3  christos   return lp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
   2483  1.1  christos }
   2484  1.1  christos 
   2485  1.1  christos /* Returns true if the LWP had stopped for a watchpoint.  */
   2486  1.1  christos 
   2487  1.1  christos static int
   2488  1.5  christos linux_nat_stopped_by_watchpoint (struct target_ops *ops)
   2489  1.1  christos {
   2490  1.1  christos   struct lwp_info *lp = find_lwp_pid (inferior_ptid);
   2491  1.1  christos 
   2492  1.1  christos   gdb_assert (lp != NULL);
   2493  1.1  christos 
   2494  1.1  christos   return lp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
   2495  1.1  christos }
   2496  1.1  christos 
   2497  1.1  christos static int
   2498  1.1  christos linux_nat_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
   2499  1.1  christos {
   2500  1.1  christos   struct lwp_info *lp = find_lwp_pid (inferior_ptid);
   2501  1.1  christos 
   2502  1.1  christos   gdb_assert (lp != NULL);
   2503  1.1  christos 
   2504  1.1  christos   *addr_p = lp->stopped_data_address;
   2505  1.1  christos 
   2506  1.1  christos   return lp->stopped_data_address_p;
   2507  1.1  christos }
   2508  1.1  christos 
   2509  1.1  christos /* Commonly any breakpoint / watchpoint generate only SIGTRAP.  */
   2510  1.1  christos 
   2511  1.1  christos static int
   2512  1.1  christos sigtrap_is_event (int status)
   2513  1.1  christos {
   2514  1.1  christos   return WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP;
   2515  1.1  christos }
   2516  1.1  christos 
   2517  1.1  christos /* Set alternative SIGTRAP-like events recognizer.  If
   2518  1.1  christos    breakpoint_inserted_here_p there then gdbarch_decr_pc_after_break will be
   2519  1.1  christos    applied.  */
   2520  1.1  christos 
   2521  1.1  christos void
   2522  1.1  christos linux_nat_set_status_is_event (struct target_ops *t,
   2523  1.1  christos 			       int (*status_is_event) (int status))
   2524  1.1  christos {
   2525  1.1  christos   linux_nat_status_is_event = status_is_event;
   2526  1.1  christos }
   2527  1.3  christos 
   2528  1.1  christos /* Wait until LP is stopped.  */
   2529  1.1  christos 
   2530  1.1  christos static int
   2531  1.1  christos stop_wait_callback (struct lwp_info *lp, void *data)
   2532  1.1  christos {
   2533  1.1  christos   struct inferior *inf = find_inferior_ptid (lp->ptid);
   2534  1.1  christos 
   2535  1.1  christos   /* If this is a vfork parent, bail out, it is not going to report
   2536  1.1  christos      any SIGSTOP until the vfork is done with.  */
   2537  1.1  christos   if (inf->vfork_child != NULL)
   2538  1.1  christos     return 0;
   2539  1.1  christos 
   2540  1.1  christos   if (!lp->stopped)
   2541  1.1  christos     {
   2542  1.1  christos       int status;
   2543  1.1  christos 
   2544  1.1  christos       status = wait_lwp (lp);
   2545  1.1  christos       if (status == 0)
   2546  1.1  christos 	return 0;
   2547  1.1  christos 
   2548  1.1  christos       if (lp->ignore_sigint && WIFSTOPPED (status)
   2549  1.3  christos 	  && WSTOPSIG (status) == SIGINT)
   2550  1.1  christos 	{
   2551  1.1  christos 	  lp->ignore_sigint = 0;
   2552  1.1  christos 
   2553  1.1  christos 	  errno = 0;
   2554  1.1  christos 	  ptrace (PTRACE_CONT, ptid_get_lwp (lp->ptid), 0, 0);
   2555  1.1  christos 	  lp->stopped = 0;
   2556  1.1  christos 	  if (debug_linux_nat)
   2557  1.1  christos 	    fprintf_unfiltered (gdb_stdlog,
   2558  1.1  christos 				"PTRACE_CONT %s, 0, 0 (%s) "
   2559  1.1  christos 				"(discarding SIGINT)\n",
   2560  1.1  christos 				target_pid_to_str (lp->ptid),
   2561  1.1  christos 				errno ? safe_strerror (errno) : "OK");
   2562  1.1  christos 
   2563  1.1  christos 	  return stop_wait_callback (lp, NULL);
   2564  1.1  christos 	}
   2565  1.1  christos 
   2566  1.1  christos       maybe_clear_ignore_sigint (lp);
   2567  1.1  christos 
   2568  1.1  christos       if (WSTOPSIG (status) != SIGSTOP)
   2569  1.1  christos 	{
   2570  1.1  christos 	  /* The thread was stopped with a signal other than SIGSTOP.  */
   2571  1.1  christos 
   2572  1.1  christos 	  if (debug_linux_nat)
   2573  1.1  christos 	    fprintf_unfiltered (gdb_stdlog,
   2574  1.1  christos 				"SWC: Pending event %s in %s\n",
   2575  1.6  christos 				status_to_str ((int) status),
   2576  1.1  christos 				target_pid_to_str (lp->ptid));
   2577  1.1  christos 
   2578  1.1  christos 	  /* Save the sigtrap event.  */
   2579  1.1  christos 	  lp->status = status;
   2580  1.1  christos 	  gdb_assert (lp->signalled);
   2581  1.1  christos 	  save_stop_reason (lp);
   2582  1.1  christos 	}
   2583  1.1  christos       else
   2584  1.5  christos 	{
   2585  1.1  christos 	  /* We caught the SIGSTOP that we intended to catch, so
   2586  1.1  christos 	     there's no SIGSTOP pending.  */
   2587  1.1  christos 
   2588  1.1  christos 	  if (debug_linux_nat)
   2589  1.1  christos 	    fprintf_unfiltered (gdb_stdlog,
   2590  1.1  christos 				"SWC: Expected SIGSTOP caught for %s.\n",
   2591  1.1  christos 				target_pid_to_str (lp->ptid));
   2592  1.1  christos 
   2593  1.1  christos 	  /* Reset SIGNALLED only after the stop_wait_callback call
   2594  1.1  christos 	     above as it does gdb_assert on SIGNALLED.  */
   2595  1.1  christos 	  lp->signalled = 0;
   2596  1.3  christos 	}
   2597  1.3  christos     }
   2598  1.3  christos 
   2599  1.1  christos   return 0;
   2600  1.1  christos }
   2601  1.1  christos 
   2602  1.1  christos /* Return non-zero if LP has a wait status pending.  Discard the
   2603  1.1  christos    pending event and resume the LWP if the event that originally
   2604  1.1  christos    caused the stop became uninteresting.  */
   2605  1.1  christos 
   2606  1.1  christos static int
   2607  1.1  christos status_callback (struct lwp_info *lp, void *data)
   2608  1.5  christos {
   2609  1.5  christos   /* Only report a pending wait status if we pretend that this has
   2610  1.5  christos      indeed been resumed.  */
   2611  1.5  christos   if (!lp->resumed)
   2612  1.5  christos     return 0;
   2613  1.1  christos 
   2614  1.3  christos   if (!lwp_status_pending_p (lp))
   2615  1.3  christos     return 0;
   2616  1.3  christos 
   2617  1.3  christos   if (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
   2618  1.3  christos       || lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT)
   2619  1.3  christos     {
   2620  1.3  christos       struct regcache *regcache = get_thread_regcache (lp->ptid);
   2621  1.3  christos       CORE_ADDR pc;
   2622  1.3  christos       int discard = 0;
   2623  1.3  christos 
   2624  1.3  christos       pc = regcache_read_pc (regcache);
   2625  1.3  christos 
   2626  1.3  christos       if (pc != lp->stop_pc)
   2627  1.3  christos 	{
   2628  1.3  christos 	  if (debug_linux_nat)
   2629  1.3  christos 	    fprintf_unfiltered (gdb_stdlog,
   2630  1.5  christos 				"SC: PC of %s changed.  was=%s, now=%s\n",
   2631  1.5  christos 				target_pid_to_str (lp->ptid),
   2632  1.3  christos 				paddress (target_gdbarch (), lp->stop_pc),
   2633  1.3  christos 				paddress (target_gdbarch (), pc));
   2634  1.3  christos 	  discard = 1;
   2635  1.3  christos 	}
   2636  1.3  christos 
   2637  1.3  christos #if !USE_SIGTRAP_SIGINFO
   2638  1.3  christos       else if (!breakpoint_inserted_here_p (get_regcache_aspace (regcache), pc))
   2639  1.3  christos 	{
   2640  1.3  christos 	  if (debug_linux_nat)
   2641  1.3  christos 	    fprintf_unfiltered (gdb_stdlog,
   2642  1.5  christos 				"SC: previous breakpoint of %s, at %s gone\n",
   2643  1.3  christos 				target_pid_to_str (lp->ptid),
   2644  1.3  christos 				paddress (target_gdbarch (), lp->stop_pc));
   2645  1.3  christos 
   2646  1.3  christos 	  discard = 1;
   2647  1.3  christos 	}
   2648  1.3  christos #endif
   2649  1.3  christos 
   2650  1.3  christos       if (discard)
   2651  1.3  christos 	{
   2652  1.3  christos 	  if (debug_linux_nat)
   2653  1.3  christos 	    fprintf_unfiltered (gdb_stdlog,
   2654  1.3  christos 				"SC: pending event of %s cancelled.\n",
   2655  1.1  christos 				target_pid_to_str (lp->ptid));
   2656  1.1  christos 
   2657  1.5  christos 	  lp->status = 0;
   2658  1.1  christos 	  linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0);
   2659  1.1  christos 	  return 0;
   2660  1.1  christos 	}
   2661  1.1  christos     }
   2662  1.1  christos 
   2663  1.1  christos   return 1;
   2664  1.1  christos }
   2665  1.6  christos 
   2666  1.1  christos /* Count the LWP's that have had events.  */
   2667  1.1  christos 
   2668  1.1  christos static int
   2669  1.3  christos count_events_callback (struct lwp_info *lp, void *data)
   2670  1.3  christos {
   2671  1.1  christos   int *count = (int *) data;
   2672  1.1  christos 
   2673  1.1  christos   gdb_assert (count != NULL);
   2674  1.1  christos 
   2675  1.1  christos   /* Select only resumed LWPs that have an event pending.  */
   2676  1.1  christos   if (lp->resumed && lwp_status_pending_p (lp))
   2677  1.1  christos     (*count)++;
   2678  1.1  christos 
   2679  1.1  christos   return 0;
   2680  1.1  christos }
   2681  1.1  christos 
   2682  1.1  christos /* Select the LWP (if any) that is currently being single-stepped.  */
   2683  1.1  christos 
   2684  1.1  christos static int
   2685  1.1  christos select_singlestep_lwp_callback (struct lwp_info *lp, void *data)
   2686  1.1  christos {
   2687  1.1  christos   if (lp->last_resume_kind == resume_step
   2688  1.3  christos       && lp->status != 0)
   2689  1.3  christos     return 1;
   2690  1.3  christos   else
   2691  1.3  christos     return 0;
   2692  1.3  christos }
   2693  1.3  christos 
   2694  1.3  christos /* Returns true if LP has a status pending.  */
   2695  1.3  christos 
   2696  1.3  christos static int
   2697  1.3  christos lwp_status_pending_p (struct lwp_info *lp)
   2698  1.3  christos {
   2699  1.5  christos   /* We check for lp->waitstatus in addition to lp->status, because we
   2700  1.1  christos      can have pending process exits recorded in lp->status and
   2701  1.1  christos      W_EXITCODE(0,0) happens to be 0.  */
   2702  1.1  christos   return lp->status != 0 || lp->waitstatus.kind != TARGET_WAITKIND_IGNORE;
   2703  1.1  christos }
   2704  1.6  christos 
   2705  1.1  christos /* Select the Nth LWP that has had an event.  */
   2706  1.1  christos 
   2707  1.1  christos static int
   2708  1.3  christos select_event_lwp_callback (struct lwp_info *lp, void *data)
   2709  1.3  christos {
   2710  1.1  christos   int *selector = (int *) data;
   2711  1.1  christos 
   2712  1.1  christos   gdb_assert (selector != NULL);
   2713  1.1  christos 
   2714  1.1  christos   /* Select only resumed LWPs that have an event pending.  */
   2715  1.1  christos   if (lp->resumed && lwp_status_pending_p (lp))
   2716  1.6  christos     if ((*selector)-- == 0)
   2717  1.6  christos       return 1;
   2718  1.6  christos 
   2719  1.6  christos   return 0;
   2720  1.6  christos }
   2721  1.3  christos 
   2722  1.6  christos /* Called when the LWP stopped for a signal/trap.  If it stopped for a
   2723  1.6  christos    trap check what caused it (breakpoint, watchpoint, trace, etc.),
   2724  1.1  christos    and save the result in the LWP's stop_reason field.  If it stopped
   2725  1.6  christos    for a breakpoint, decrement the PC if necessary on the lwp's
   2726  1.6  christos    architecture.  */
   2727  1.1  christos 
   2728  1.3  christos static void
   2729  1.5  christos save_stop_reason (struct lwp_info *lp)
   2730  1.5  christos {
   2731  1.5  christos   struct regcache *regcache;
   2732  1.3  christos   struct gdbarch *gdbarch;
   2733  1.6  christos   CORE_ADDR pc;
   2734  1.6  christos   CORE_ADDR sw_bp_pc;
   2735  1.6  christos #if USE_SIGTRAP_SIGINFO
   2736  1.6  christos   siginfo_t siginfo;
   2737  1.6  christos #endif
   2738  1.6  christos 
   2739  1.6  christos   gdb_assert (lp->stop_reason == TARGET_STOPPED_BY_NO_REASON);
   2740  1.6  christos   gdb_assert (lp->status != 0);
   2741  1.6  christos 
   2742  1.3  christos   if (!linux_nat_status_is_event (lp->status))
   2743  1.5  christos     return;
   2744  1.1  christos 
   2745  1.5  christos   regcache = get_thread_regcache (lp->ptid);
   2746  1.5  christos   gdbarch = get_regcache_arch (regcache);
   2747  1.5  christos 
   2748  1.5  christos   pc = regcache_read_pc (regcache);
   2749  1.5  christos   sw_bp_pc = pc - gdbarch_decr_pc_after_break (gdbarch);
   2750  1.6  christos 
   2751  1.6  christos #if USE_SIGTRAP_SIGINFO
   2752  1.6  christos   if (linux_nat_get_siginfo (lp->ptid, &siginfo))
   2753  1.6  christos     {
   2754  1.6  christos       if (siginfo.si_signo == SIGTRAP)
   2755  1.6  christos 	{
   2756  1.6  christos 	  if (GDB_ARCH_IS_TRAP_BRKPT (siginfo.si_code)
   2757  1.6  christos 	      && GDB_ARCH_IS_TRAP_HWBKPT (siginfo.si_code))
   2758  1.6  christos 	    {
   2759  1.5  christos 	      /* The si_code is ambiguous on this arch -- check debug
   2760  1.6  christos 		 registers.  */
   2761  1.6  christos 	      if (!check_stopped_by_watchpoint (lp))
   2762  1.6  christos 		lp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
   2763  1.6  christos 	    }
   2764  1.6  christos 	  else if (GDB_ARCH_IS_TRAP_BRKPT (siginfo.si_code))
   2765  1.5  christos 	    {
   2766  1.5  christos 	      /* If we determine the LWP stopped for a SW breakpoint,
   2767  1.6  christos 		 trust it.  Particularly don't check watchpoint
   2768  1.5  christos 		 registers, because at least on s390, we'd find
   2769  1.6  christos 		 stopped-by-watchpoint as long as there's a watchpoint
   2770  1.6  christos 		 set.  */
   2771  1.6  christos 	      lp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
   2772  1.6  christos 	    }
   2773  1.5  christos 	  else if (GDB_ARCH_IS_TRAP_HWBKPT (siginfo.si_code))
   2774  1.5  christos 	    {
   2775  1.5  christos 	      /* This can indicate either a hardware breakpoint or
   2776  1.5  christos 		 hardware watchpoint.  Check debug registers.  */
   2777  1.5  christos 	      if (!check_stopped_by_watchpoint (lp))
   2778  1.5  christos 		lp->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
   2779  1.5  christos 	    }
   2780  1.6  christos 	  else if (siginfo.si_code == TRAP_TRACE)
   2781  1.6  christos 	    {
   2782  1.6  christos 	      if (debug_linux_nat)
   2783  1.6  christos 		fprintf_unfiltered (gdb_stdlog,
   2784  1.6  christos 				    "CSBB: %s stopped by trace\n",
   2785  1.6  christos 				    target_pid_to_str (lp->ptid));
   2786  1.6  christos 
   2787  1.5  christos 	      /* We may have single stepped an instruction that
   2788  1.5  christos 		 triggered a watchpoint.  In that case, on some
   2789  1.5  christos 		 architectures (such as x86), instead of TRAP_HWBKPT,
   2790  1.5  christos 		 si_code indicates TRAP_TRACE, and we need to check
   2791  1.3  christos 		 the debug registers separately.  */
   2792  1.3  christos 	      check_stopped_by_watchpoint (lp);
   2793  1.3  christos 	    }
   2794  1.1  christos 	}
   2795  1.3  christos     }
   2796  1.3  christos #else
   2797  1.6  christos   if ((!lp->step || lp->stop_pc == sw_bp_pc)
   2798  1.6  christos       && software_breakpoint_inserted_here_p (get_regcache_aspace (regcache),
   2799  1.6  christos 					      sw_bp_pc))
   2800  1.6  christos     {
   2801  1.6  christos       /* The LWP was either continued, or stepped a software
   2802  1.6  christos 	 breakpoint instruction.  */
   2803  1.6  christos       lp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
   2804  1.6  christos     }
   2805  1.6  christos 
   2806  1.6  christos   if (hardware_breakpoint_inserted_here_p (get_regcache_aspace (regcache), pc))
   2807  1.6  christos     lp->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
   2808  1.6  christos 
   2809  1.1  christos   if (lp->stop_reason == TARGET_STOPPED_BY_NO_REASON)
   2810  1.1  christos     check_stopped_by_watchpoint (lp);
   2811  1.5  christos #endif
   2812  1.1  christos 
   2813  1.1  christos   if (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT)
   2814  1.1  christos     {
   2815  1.3  christos       if (debug_linux_nat)
   2816  1.3  christos 	fprintf_unfiltered (gdb_stdlog,
   2817  1.1  christos 			    "CSBB: %s stopped by software breakpoint\n",
   2818  1.6  christos 			    target_pid_to_str (lp->ptid));
   2819  1.6  christos 
   2820  1.6  christos       /* Back up the PC if necessary.  */
   2821  1.6  christos       if (pc != sw_bp_pc)
   2822  1.6  christos 	regcache_write_pc (regcache, sw_bp_pc);
   2823  1.6  christos 
   2824  1.6  christos       /* Update this so we record the correct stop PC below.  */
   2825  1.6  christos       pc = sw_bp_pc;
   2826  1.6  christos     }
   2827  1.1  christos   else if (lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT)
   2828  1.6  christos     {
   2829  1.3  christos       if (debug_linux_nat)
   2830  1.3  christos 	fprintf_unfiltered (gdb_stdlog,
   2831  1.3  christos 			    "CSBB: %s stopped by hardware breakpoint\n",
   2832  1.6  christos 			    target_pid_to_str (lp->ptid));
   2833  1.3  christos     }
   2834  1.3  christos   else if (lp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT)
   2835  1.1  christos     {
   2836  1.6  christos       if (debug_linux_nat)
   2837  1.1  christos 	fprintf_unfiltered (gdb_stdlog,
   2838  1.1  christos 			    "CSBB: %s stopped by hardware watchpoint\n",
   2839  1.5  christos 			    target_pid_to_str (lp->ptid));
   2840  1.5  christos     }
   2841  1.5  christos 
   2842  1.5  christos   lp->stop_pc = pc;
   2843  1.5  christos }
   2844  1.5  christos 
   2845  1.5  christos 
   2846  1.5  christos /* Returns true if the LWP had stopped for a software breakpoint.  */
   2847  1.5  christos 
   2848  1.5  christos static int
   2849  1.5  christos linux_nat_stopped_by_sw_breakpoint (struct target_ops *ops)
   2850  1.5  christos {
   2851  1.5  christos   struct lwp_info *lp = find_lwp_pid (inferior_ptid);
   2852  1.5  christos 
   2853  1.5  christos   gdb_assert (lp != NULL);
   2854  1.5  christos 
   2855  1.5  christos   return lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT;
   2856  1.5  christos }
   2857  1.5  christos 
   2858  1.5  christos /* Implement the supports_stopped_by_sw_breakpoint method.  */
   2859  1.5  christos 
   2860  1.5  christos static int
   2861  1.5  christos linux_nat_supports_stopped_by_sw_breakpoint (struct target_ops *ops)
   2862  1.5  christos {
   2863  1.5  christos   return USE_SIGTRAP_SIGINFO;
   2864  1.5  christos }
   2865  1.5  christos 
   2866  1.5  christos /* Returns true if the LWP had stopped for a hardware
   2867  1.5  christos    breakpoint/watchpoint.  */
   2868  1.5  christos 
   2869  1.5  christos static int
   2870  1.5  christos linux_nat_stopped_by_hw_breakpoint (struct target_ops *ops)
   2871  1.5  christos {
   2872  1.5  christos   struct lwp_info *lp = find_lwp_pid (inferior_ptid);
   2873  1.5  christos 
   2874  1.5  christos   gdb_assert (lp != NULL);
   2875  1.5  christos 
   2876  1.5  christos   return lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT;
   2877  1.5  christos }
   2878  1.5  christos 
   2879  1.5  christos /* Implement the supports_stopped_by_hw_breakpoint method.  */
   2880  1.5  christos 
   2881  1.1  christos static int
   2882  1.1  christos linux_nat_supports_stopped_by_hw_breakpoint (struct target_ops *ops)
   2883  1.1  christos {
   2884  1.1  christos   return USE_SIGTRAP_SIGINFO;
   2885  1.1  christos }
   2886  1.1  christos 
   2887  1.1  christos /* Select one LWP out of those that have events pending.  */
   2888  1.3  christos 
   2889  1.1  christos static void
   2890  1.1  christos select_event_lwp (ptid_t filter, struct lwp_info **orig_lp, int *status)
   2891  1.1  christos {
   2892  1.1  christos   int num_events = 0;
   2893  1.3  christos   int random_selector;
   2894  1.3  christos   struct lwp_info *event_lp = NULL;
   2895  1.3  christos 
   2896  1.3  christos   /* Record the wait status for the original LWP.  */
   2897  1.3  christos   (*orig_lp)->status = *status;
   2898  1.3  christos 
   2899  1.3  christos   /* In all-stop, give preference to the LWP that is being
   2900  1.3  christos      single-stepped.  There will be at most one, and it will be the
   2901  1.3  christos      LWP that the core is most interested in.  If we didn't do this,
   2902  1.6  christos      then we'd have to handle pending step SIGTRAPs somehow in case
   2903  1.1  christos      the core later continues the previously-stepped thread, as
   2904  1.3  christos      otherwise we'd report the pending SIGTRAP then, and the core, not
   2905  1.3  christos      having stepped the thread, wouldn't understand what the trap was
   2906  1.3  christos      for, and therefore would report it to the user as a random
   2907  1.3  christos      signal.  */
   2908  1.3  christos   if (!target_is_non_stop_p ())
   2909  1.3  christos     {
   2910  1.3  christos       event_lp = iterate_over_lwps (filter,
   2911  1.3  christos 				    select_singlestep_lwp_callback, NULL);
   2912  1.3  christos       if (event_lp != NULL)
   2913  1.1  christos 	{
   2914  1.3  christos 	  if (debug_linux_nat)
   2915  1.3  christos 	    fprintf_unfiltered (gdb_stdlog,
   2916  1.1  christos 				"SEL: Select single-step %s\n",
   2917  1.3  christos 				target_pid_to_str (event_lp->ptid));
   2918  1.1  christos 	}
   2919  1.3  christos     }
   2920  1.1  christos 
   2921  1.5  christos   if (event_lp == NULL)
   2922  1.1  christos     {
   2923  1.3  christos       /* Pick one at random, out of those which have had events.  */
   2924  1.3  christos 
   2925  1.1  christos       /* First see how many events we have.  */
   2926  1.1  christos       iterate_over_lwps (filter, count_events_callback, &num_events);
   2927  1.1  christos       gdb_assert (num_events > 0);
   2928  1.1  christos 
   2929  1.1  christos       /* Now randomly pick a LWP out of those that have had
   2930  1.3  christos 	 events.  */
   2931  1.1  christos       random_selector = (int)
   2932  1.1  christos 	((num_events * (double) rand ()) / (RAND_MAX + 1.0));
   2933  1.1  christos 
   2934  1.1  christos       if (debug_linux_nat && num_events > 1)
   2935  1.1  christos 	fprintf_unfiltered (gdb_stdlog,
   2936  1.1  christos 			    "SEL: Found %d events, selecting #%d\n",
   2937  1.1  christos 			    num_events, random_selector);
   2938  1.1  christos 
   2939  1.1  christos       event_lp = iterate_over_lwps (filter,
   2940  1.1  christos 				    select_event_lwp_callback,
   2941  1.1  christos 				    &random_selector);
   2942  1.1  christos     }
   2943  1.1  christos 
   2944  1.1  christos   if (event_lp != NULL)
   2945  1.1  christos     {
   2946  1.1  christos       /* Switch the event LWP.  */
   2947  1.1  christos       *orig_lp = event_lp;
   2948  1.1  christos       *status = event_lp->status;
   2949  1.1  christos     }
   2950  1.1  christos 
   2951  1.1  christos   /* Flush the wait status for the event LWP.  */
   2952  1.1  christos   (*orig_lp)->status = 0;
   2953  1.1  christos }
   2954  1.1  christos 
   2955  1.1  christos /* Return non-zero if LP has been resumed.  */
   2956  1.1  christos 
   2957  1.1  christos static int
   2958  1.3  christos resumed_callback (struct lwp_info *lp, void *data)
   2959  1.1  christos {
   2960  1.1  christos   return lp->resumed;
   2961  1.3  christos }
   2962  1.1  christos 
   2963  1.1  christos /* Check if we should go on and pass this event to common code.
   2964  1.3  christos    Return the affected lwp if we are, or NULL otherwise.  */
   2965  1.1  christos 
   2966  1.1  christos static struct lwp_info *
   2967  1.1  christos linux_nat_filter_event (int lwpid, int status)
   2968  1.1  christos {
   2969  1.1  christos   struct lwp_info *lp;
   2970  1.1  christos   int event = linux_ptrace_get_extended_event (status);
   2971  1.1  christos 
   2972  1.1  christos   lp = find_lwp_pid (pid_to_ptid (lwpid));
   2973  1.1  christos 
   2974  1.1  christos   /* Check for stop events reported by a process we didn't already
   2975  1.1  christos      know about - anything not already in our LWP list.
   2976  1.1  christos 
   2977  1.1  christos      If we're expecting to receive stopped processes after
   2978  1.1  christos      fork, vfork, and clone events, then we'll just add the
   2979  1.1  christos      new one to our list and go back to waiting for the event
   2980  1.1  christos      to be reported - the stopped process might be returned
   2981  1.1  christos      from waitpid before or after the event is.
   2982  1.3  christos 
   2983  1.1  christos      But note the case of a non-leader thread exec'ing after the
   2984  1.1  christos      leader having exited, and gone from our lists.  The non-leader
   2985  1.1  christos      thread changes its tid to the tgid.  */
   2986  1.1  christos 
   2987  1.1  christos   if (WIFSTOPPED (status) && lp == NULL
   2988  1.1  christos       && (WSTOPSIG (status) == SIGTRAP && event == PTRACE_EVENT_EXEC))
   2989  1.1  christos     {
   2990  1.1  christos       /* A multi-thread exec after we had seen the leader exiting.  */
   2991  1.1  christos       if (debug_linux_nat)
   2992  1.1  christos 	fprintf_unfiltered (gdb_stdlog,
   2993  1.1  christos 			    "LLW: Re-adding thread group leader LWP %d.\n",
   2994  1.1  christos 			    lwpid);
   2995  1.1  christos 
   2996  1.1  christos       lp = add_lwp (ptid_build (lwpid, lwpid, 0));
   2997  1.1  christos       lp->stopped = 1;
   2998  1.5  christos       lp->resumed = 1;
   2999  1.5  christos       add_thread (lp->ptid);
   3000  1.5  christos     }
   3001  1.5  christos 
   3002  1.1  christos   if (WIFSTOPPED (status) && !lp)
   3003  1.1  christos     {
   3004  1.1  christos       if (debug_linux_nat)
   3005  1.1  christos 	fprintf_unfiltered (gdb_stdlog,
   3006  1.1  christos 			    "LHEW: saving LWP %ld status %s in stopped_pids list\n",
   3007  1.1  christos 			    (long) lwpid, status_to_str (status));
   3008  1.1  christos       add_to_pid_list (&stopped_pids, lwpid, status);
   3009  1.1  christos       return NULL;
   3010  1.1  christos     }
   3011  1.1  christos 
   3012  1.1  christos   /* Make sure we don't report an event for the exit of an LWP not in
   3013  1.3  christos      our list, i.e. not part of the current process.  This can happen
   3014  1.3  christos      if we detach from a program we originally forked and then it
   3015  1.3  christos      exits.  */
   3016  1.3  christos   if (!WIFSTOPPED (status) && !lp)
   3017  1.3  christos     return NULL;
   3018  1.3  christos 
   3019  1.3  christos   /* This LWP is stopped now.  (And if dead, this prevents it from
   3020  1.5  christos      ever being continued.)  */
   3021  1.3  christos   lp->stopped = 1;
   3022  1.5  christos 
   3023  1.3  christos   if (WIFSTOPPED (status) && lp->must_set_ptrace_flags)
   3024  1.3  christos     {
   3025  1.3  christos       struct inferior *inf = find_inferior_pid (ptid_get_pid (lp->ptid));
   3026  1.1  christos       int options = linux_nat_ptrace_options (inf->attach_flag);
   3027  1.1  christos 
   3028  1.1  christos       linux_enable_event_reporting (ptid_get_lwp (lp->ptid), options);
   3029  1.1  christos       lp->must_set_ptrace_flags = 0;
   3030  1.1  christos     }
   3031  1.1  christos 
   3032  1.1  christos   /* Handle GNU/Linux's syscall SIGTRAPs.  */
   3033  1.1  christos   if (WIFSTOPPED (status) && WSTOPSIG (status) == SYSCALL_SIGTRAP)
   3034  1.1  christos     {
   3035  1.1  christos       /* No longer need the sysgood bit.  The ptrace event ends up
   3036  1.1  christos 	 recorded in lp->waitstatus if we care for it.  We can carry
   3037  1.6  christos 	 on handling the event like a regular SIGTRAP from here
   3038  1.6  christos 	 on.  */
   3039  1.6  christos       status = W_STOPCODE (SIGTRAP);
   3040  1.6  christos       if (linux_handle_syscall_trap (lp, 0))
   3041  1.6  christos 	return NULL;
   3042  1.6  christos     }
   3043  1.1  christos   else
   3044  1.1  christos     {
   3045  1.3  christos       /* Almost all other ptrace-stops are known to be outside of system
   3046  1.3  christos 	 calls, with further exceptions in linux_handle_extended_wait.  */
   3047  1.1  christos       lp->syscall_state = TARGET_WAITKIND_IGNORE;
   3048  1.1  christos     }
   3049  1.1  christos 
   3050  1.1  christos   /* Handle GNU/Linux's extended waitstatus for trace events.  */
   3051  1.1  christos   if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP
   3052  1.5  christos       && linux_is_extended_waitstatus (status))
   3053  1.1  christos     {
   3054  1.1  christos       if (debug_linux_nat)
   3055  1.1  christos 	fprintf_unfiltered (gdb_stdlog,
   3056  1.1  christos 			    "LLW: Handling extended status 0x%06x\n",
   3057  1.3  christos 			    status);
   3058  1.1  christos       if (linux_handle_extended_wait (lp, status))
   3059  1.6  christos 	return NULL;
   3060  1.6  christos     }
   3061  1.3  christos 
   3062  1.3  christos   /* Check if the thread has exited.  */
   3063  1.3  christos   if (WIFEXITED (status) || WIFSIGNALED (status))
   3064  1.3  christos     {
   3065  1.3  christos       if (!report_thread_events
   3066  1.3  christos 	  && num_lwps (ptid_get_pid (lp->ptid)) > 1)
   3067  1.6  christos 	{
   3068  1.6  christos 	  if (debug_linux_nat)
   3069  1.6  christos 	    fprintf_unfiltered (gdb_stdlog,
   3070  1.6  christos 				"LLW: %s exited.\n",
   3071  1.6  christos 				target_pid_to_str (lp->ptid));
   3072  1.1  christos 
   3073  1.1  christos 	  /* If there is at least one more LWP, then the exit signal
   3074  1.6  christos 	     was not the end of the debugged application and should be
   3075  1.6  christos 	     ignored.  */
   3076  1.6  christos 	  exit_lwp (lp);
   3077  1.6  christos 	  return NULL;
   3078  1.1  christos 	}
   3079  1.1  christos 
   3080  1.6  christos       /* Note that even if the leader was ptrace-stopped, it can still
   3081  1.6  christos 	 exit, if e.g., some other thread brings down the whole
   3082  1.3  christos 	 process (calls `exit').  So don't assert that the lwp is
   3083  1.3  christos 	 resumed.  */
   3084  1.3  christos       if (debug_linux_nat)
   3085  1.1  christos 	fprintf_unfiltered (gdb_stdlog,
   3086  1.3  christos 			    "LWP %ld exited (resumed=%d)\n",
   3087  1.3  christos 			    ptid_get_lwp (lp->ptid), lp->resumed);
   3088  1.3  christos 
   3089  1.3  christos       /* Dead LWP's aren't expected to reported a pending sigstop.  */
   3090  1.1  christos       lp->signalled = 0;
   3091  1.1  christos 
   3092  1.1  christos       /* Store the pending event in the waitstatus, because
   3093  1.1  christos 	 W_EXITCODE(0,0) == 0.  */
   3094  1.1  christos       store_waitstatus (&lp->waitstatus, status);
   3095  1.1  christos       return lp;
   3096  1.1  christos     }
   3097  1.1  christos 
   3098  1.1  christos   /* Make sure we don't report a SIGSTOP that we sent ourselves in
   3099  1.5  christos      an attempt to stop an LWP.  */
   3100  1.5  christos   if (lp->signalled
   3101  1.5  christos       && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP)
   3102  1.5  christos     {
   3103  1.5  christos       lp->signalled = 0;
   3104  1.5  christos 
   3105  1.5  christos       if (lp->last_resume_kind == resume_stop)
   3106  1.5  christos 	{
   3107  1.1  christos 	  if (debug_linux_nat)
   3108  1.5  christos 	    fprintf_unfiltered (gdb_stdlog,
   3109  1.1  christos 				"LLW: resume_stop SIGSTOP caught for %s.\n",
   3110  1.1  christos 				target_pid_to_str (lp->ptid));
   3111  1.1  christos 	}
   3112  1.5  christos       else
   3113  1.1  christos 	{
   3114  1.1  christos 	  /* This is a delayed SIGSTOP.  Filter out the event.  */
   3115  1.1  christos 
   3116  1.1  christos 	  if (debug_linux_nat)
   3117  1.5  christos 	    fprintf_unfiltered (gdb_stdlog,
   3118  1.1  christos 				"LLW: %s %s, 0, 0 (discard delayed SIGSTOP)\n",
   3119  1.1  christos 				lp->step ?
   3120  1.1  christos 				"PTRACE_SINGLESTEP" : "PTRACE_CONT",
   3121  1.1  christos 				target_pid_to_str (lp->ptid));
   3122  1.1  christos 
   3123  1.1  christos 	  linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0);
   3124  1.1  christos 	  gdb_assert (lp->resumed);
   3125  1.1  christos 	  return NULL;
   3126  1.1  christos 	}
   3127  1.1  christos     }
   3128  1.1  christos 
   3129  1.1  christos   /* Make sure we don't report a SIGINT that we have already displayed
   3130  1.1  christos      for another thread.  */
   3131  1.1  christos   if (lp->ignore_sigint
   3132  1.1  christos       && WIFSTOPPED (status) && WSTOPSIG (status) == SIGINT)
   3133  1.1  christos     {
   3134  1.1  christos       if (debug_linux_nat)
   3135  1.1  christos 	fprintf_unfiltered (gdb_stdlog,
   3136  1.3  christos 			    "LLW: Delayed SIGINT caught for %s.\n",
   3137  1.1  christos 			    target_pid_to_str (lp->ptid));
   3138  1.1  christos 
   3139  1.1  christos       /* This is a delayed SIGINT.  */
   3140  1.1  christos       lp->ignore_sigint = 0;
   3141  1.1  christos 
   3142  1.1  christos       linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0);
   3143  1.1  christos       if (debug_linux_nat)
   3144  1.1  christos 	fprintf_unfiltered (gdb_stdlog,
   3145  1.1  christos 			    "LLW: %s %s, 0, 0 (discard SIGINT)\n",
   3146  1.1  christos 			    lp->step ?
   3147  1.1  christos 			    "PTRACE_SINGLESTEP" : "PTRACE_CONT",
   3148  1.1  christos 			    target_pid_to_str (lp->ptid));
   3149  1.3  christos       gdb_assert (lp->resumed);
   3150  1.3  christos 
   3151  1.3  christos       /* Discard the event.  */
   3152  1.3  christos       return NULL;
   3153  1.3  christos     }
   3154  1.3  christos 
   3155  1.3  christos   /* Don't report signals that GDB isn't interested in, such as
   3156  1.3  christos      signals that are neither printed nor stopped upon.  Stopping all
   3157  1.3  christos      threads can be a bit time-consuming so if we want decent
   3158  1.3  christos      performance with heavily multi-threaded programs, especially when
   3159  1.6  christos      they're using a high frequency timer, we'd better avoid it if we
   3160  1.3  christos      can.  */
   3161  1.3  christos   if (WIFSTOPPED (status))
   3162  1.3  christos     {
   3163  1.3  christos       enum gdb_signal signo = gdb_signal_from_host (WSTOPSIG (status));
   3164  1.3  christos 
   3165  1.3  christos       if (!target_is_non_stop_p ())
   3166  1.3  christos 	{
   3167  1.3  christos 	  /* Only do the below in all-stop, as we currently use SIGSTOP
   3168  1.3  christos 	     to implement target_stop (see linux_nat_stop) in
   3169  1.3  christos 	     non-stop.  */
   3170  1.3  christos 	  if (signo == GDB_SIGNAL_INT && signal_pass_state (signo) == 0)
   3171  1.3  christos 	    {
   3172  1.3  christos 	      /* If ^C/BREAK is typed at the tty/console, SIGINT gets
   3173  1.3  christos 		 forwarded to the entire process group, that is, all LWPs
   3174  1.3  christos 		 will receive it - unless they're using CLONE_THREAD to
   3175  1.3  christos 		 share signals.  Since we only want to report it once, we
   3176  1.3  christos 		 mark it as ignored for all LWPs except this one.  */
   3177  1.3  christos 	      iterate_over_lwps (pid_to_ptid (ptid_get_pid (lp->ptid)),
   3178  1.3  christos 					      set_ignore_sigint, NULL);
   3179  1.3  christos 	      lp->ignore_sigint = 0;
   3180  1.5  christos 	    }
   3181  1.5  christos 	  else
   3182  1.3  christos 	    maybe_clear_ignore_sigint (lp);
   3183  1.5  christos 	}
   3184  1.5  christos 
   3185  1.3  christos       /* When using hardware single-step, we need to report every signal.
   3186  1.3  christos 	 Otherwise, signals in pass_mask may be short-circuited
   3187  1.3  christos 	 except signals that might be caused by a breakpoint.  */
   3188  1.3  christos       if (!lp->step
   3189  1.3  christos 	  && WSTOPSIG (status) && sigismember (&pass_mask, WSTOPSIG (status))
   3190  1.3  christos 	  && !linux_wstatus_maybe_breakpoint (status))
   3191  1.3  christos 	{
   3192  1.3  christos 	  linux_resume_one_lwp (lp, lp->step, signo);
   3193  1.3  christos 	  if (debug_linux_nat)
   3194  1.3  christos 	    fprintf_unfiltered (gdb_stdlog,
   3195  1.3  christos 				"LLW: %s %s, %s (preempt 'handle')\n",
   3196  1.3  christos 				lp->step ?
   3197  1.3  christos 				"PTRACE_SINGLESTEP" : "PTRACE_CONT",
   3198  1.3  christos 				target_pid_to_str (lp->ptid),
   3199  1.3  christos 				(signo != GDB_SIGNAL_0
   3200  1.1  christos 				 ? strsignal (gdb_signal_to_host (signo))
   3201  1.1  christos 				 : "0"));
   3202  1.1  christos 	  return NULL;
   3203  1.6  christos 	}
   3204  1.1  christos     }
   3205  1.1  christos 
   3206  1.1  christos   /* An interesting event.  */
   3207  1.1  christos   gdb_assert (lp);
   3208  1.1  christos   lp->status = status;
   3209  1.1  christos   save_stop_reason (lp);
   3210  1.1  christos   return lp;
   3211  1.1  christos }
   3212  1.1  christos 
   3213  1.1  christos /* Detect zombie thread group leaders, and "exit" them.  We can't reap
   3214  1.1  christos    their exits until all other threads in the group have exited.  */
   3215  1.1  christos 
   3216  1.1  christos static void
   3217  1.1  christos check_zombie_leaders (void)
   3218  1.1  christos {
   3219  1.1  christos   struct inferior *inf;
   3220  1.1  christos 
   3221  1.1  christos   ALL_INFERIORS (inf)
   3222  1.1  christos     {
   3223  1.1  christos       struct lwp_info *leader_lp;
   3224  1.1  christos 
   3225  1.1  christos       if (inf->pid == 0)
   3226  1.1  christos 	continue;
   3227  1.1  christos 
   3228  1.1  christos       leader_lp = find_lwp_pid (pid_to_ptid (inf->pid));
   3229  1.1  christos       if (leader_lp != NULL
   3230  1.1  christos 	  /* Check if there are other threads in the group, as we may
   3231  1.1  christos 	     have raced with the inferior simply exiting.  */
   3232  1.1  christos 	  && num_lwps (inf->pid) > 1
   3233  1.1  christos 	  && linux_proc_pid_is_zombie (inf->pid))
   3234  1.1  christos 	{
   3235  1.1  christos 	  if (debug_linux_nat)
   3236  1.1  christos 	    fprintf_unfiltered (gdb_stdlog,
   3237  1.1  christos 				"CZL: Thread group leader %d zombie "
   3238  1.1  christos 				"(it exited, or another thread execd).\n",
   3239  1.1  christos 				inf->pid);
   3240  1.1  christos 
   3241  1.1  christos 	  /* A leader zombie can mean one of two things:
   3242  1.1  christos 
   3243  1.6  christos 	     - It exited, and there's an exit status pending
   3244  1.6  christos 	     available, or only the leader exited (not the whole
   3245  1.1  christos 	     program).  In the latter case, we can't waitpid the
   3246  1.1  christos 	     leader's exit status until all other threads are gone.
   3247  1.1  christos 
   3248  1.1  christos 	     - There are 3 or more threads in the group, and a thread
   3249  1.1  christos 	     other than the leader exec'd.  See comments on exec
   3250  1.1  christos 	     events at the top of the file.  We could try
   3251  1.1  christos 	     distinguishing the exit and exec cases, by waiting once
   3252  1.1  christos 	     more, and seeing if something comes out, but it doesn't
   3253  1.1  christos 	     sound useful.  The previous leader _does_ go away, and
   3254  1.1  christos 	     we'll re-add the new one once we see the exec event
   3255  1.1  christos 	     (which is just the same as what would happen if the
   3256  1.1  christos 	     previous leader did exit voluntarily before some other
   3257  1.1  christos 	     thread execs).  */
   3258  1.1  christos 
   3259  1.1  christos 	  if (debug_linux_nat)
   3260  1.1  christos 	    fprintf_unfiltered (gdb_stdlog,
   3261  1.1  christos 				"CZL: Thread group leader %d vanished.\n",
   3262  1.6  christos 				inf->pid);
   3263  1.6  christos 	  exit_lwp (leader_lp);
   3264  1.6  christos 	}
   3265  1.6  christos     }
   3266  1.6  christos }
   3267  1.6  christos 
   3268  1.6  christos /* Convenience function that is called when the kernel reports an exit
   3269  1.6  christos    event.  This decides whether to report the event to GDB as a
   3270  1.6  christos    process exit event, a thread exit event, or to suppress the
   3271  1.6  christos    event.  */
   3272  1.6  christos 
   3273  1.6  christos static ptid_t
   3274  1.6  christos filter_exit_event (struct lwp_info *event_child,
   3275  1.6  christos 		   struct target_waitstatus *ourstatus)
   3276  1.6  christos {
   3277  1.6  christos   ptid_t ptid = event_child->ptid;
   3278  1.6  christos 
   3279  1.6  christos   if (num_lwps (ptid_get_pid (ptid)) > 1)
   3280  1.6  christos     {
   3281  1.6  christos       if (report_thread_events)
   3282  1.6  christos 	ourstatus->kind = TARGET_WAITKIND_THREAD_EXITED;
   3283  1.6  christos       else
   3284  1.6  christos 	ourstatus->kind = TARGET_WAITKIND_IGNORE;
   3285  1.6  christos 
   3286  1.1  christos       exit_lwp (event_child);
   3287  1.1  christos     }
   3288  1.1  christos 
   3289  1.1  christos   return ptid;
   3290  1.1  christos }
   3291  1.3  christos 
   3292  1.1  christos static ptid_t
   3293  1.1  christos linux_nat_wait_1 (struct target_ops *ops,
   3294  1.1  christos 		  ptid_t ptid, struct target_waitstatus *ourstatus,
   3295  1.1  christos 		  int target_options)
   3296  1.1  christos {
   3297  1.1  christos   sigset_t prev_mask;
   3298  1.1  christos   enum resume_kind last_resume_kind;
   3299  1.1  christos   struct lwp_info *lp;
   3300  1.1  christos   int status;
   3301  1.1  christos 
   3302  1.1  christos   if (debug_linux_nat)
   3303  1.1  christos     fprintf_unfiltered (gdb_stdlog, "LLW: enter\n");
   3304  1.1  christos 
   3305  1.1  christos   /* The first time we get here after starting a new inferior, we may
   3306  1.1  christos      not have added it to the LWP list yet - this is the earliest
   3307  1.1  christos      moment at which we know its PID.  */
   3308  1.1  christos   if (ptid_is_pid (inferior_ptid))
   3309  1.1  christos     {
   3310  1.1  christos       /* Upgrade the main thread's ptid.  */
   3311  1.1  christos       thread_change_ptid (inferior_ptid,
   3312  1.1  christos 			  ptid_build (ptid_get_pid (inferior_ptid),
   3313  1.1  christos 				      ptid_get_pid (inferior_ptid), 0));
   3314  1.1  christos 
   3315  1.1  christos       lp = add_initial_lwp (inferior_ptid);
   3316  1.1  christos       lp->resumed = 1;
   3317  1.3  christos     }
   3318  1.3  christos 
   3319  1.1  christos   /* Make sure SIGCHLD is blocked until the sigsuspend below.  */
   3320  1.1  christos   block_child_signals (&prev_mask);
   3321  1.1  christos 
   3322  1.1  christos   /* First check if there is a LWP with a wait status pending.  */
   3323  1.1  christos   lp = iterate_over_lwps (ptid, status_callback, NULL);
   3324  1.1  christos   if (lp != NULL)
   3325  1.1  christos     {
   3326  1.1  christos       if (debug_linux_nat)
   3327  1.3  christos 	fprintf_unfiltered (gdb_stdlog,
   3328  1.3  christos 			    "LLW: Using pending wait status %s for %s.\n",
   3329  1.3  christos 			    status_to_str (lp->status),
   3330  1.1  christos 			    target_pid_to_str (lp->ptid));
   3331  1.1  christos     }
   3332  1.1  christos 
   3333  1.1  christos   /* But if we don't find a pending event, we'll have to wait.  Always
   3334  1.1  christos      pull all events out of the kernel.  We'll randomly select an
   3335  1.1  christos      event LWP out of all that have events, to prevent starvation.  */
   3336  1.1  christos 
   3337  1.1  christos   while (lp == NULL)
   3338  1.1  christos     {
   3339  1.1  christos       pid_t lwpid;
   3340  1.1  christos 
   3341  1.1  christos       /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace
   3342  1.1  christos 	 quirks:
   3343  1.1  christos 
   3344  1.1  christos 	 - If the thread group leader exits while other threads in the
   3345  1.1  christos 	   thread group still exist, waitpid(TGID, ...) hangs.  That
   3346  1.1  christos 	   waitpid won't return an exit status until the other threads
   3347  1.1  christos 	   in the group are reapped.
   3348  1.1  christos 
   3349  1.6  christos 	 - When a non-leader thread execs, that thread just vanishes
   3350  1.1  christos 	   without reporting an exit (so we'd hang if we waited for it
   3351  1.1  christos 	   explicitly in that case).  The exec event is reported to
   3352  1.1  christos 	   the TGID pid.  */
   3353  1.1  christos 
   3354  1.1  christos       errno = 0;
   3355  1.1  christos       lwpid = my_waitpid (-1, &status,  __WALL | WNOHANG);
   3356  1.1  christos 
   3357  1.1  christos       if (debug_linux_nat)
   3358  1.1  christos 	fprintf_unfiltered (gdb_stdlog,
   3359  1.1  christos 			    "LNW: waitpid(-1, ...) returned %d, %s\n",
   3360  1.1  christos 			    lwpid, errno ? safe_strerror (errno) : "ERRNO-OK");
   3361  1.1  christos 
   3362  1.1  christos       if (lwpid > 0)
   3363  1.1  christos 	{
   3364  1.1  christos 	  if (debug_linux_nat)
   3365  1.3  christos 	    {
   3366  1.1  christos 	      fprintf_unfiltered (gdb_stdlog,
   3367  1.1  christos 				  "LLW: waitpid %ld received %s\n",
   3368  1.1  christos 				  (long) lwpid, status_to_str (status));
   3369  1.1  christos 	    }
   3370  1.1  christos 
   3371  1.5  christos 	  linux_nat_filter_event (lwpid, status);
   3372  1.5  christos 	  /* Retry until nothing comes out of waitpid.  A single
   3373  1.5  christos 	     SIGCHLD can indicate more than one child stopped.  */
   3374  1.5  christos 	  continue;
   3375  1.5  christos 	}
   3376  1.5  christos 
   3377  1.5  christos       /* Now that we've pulled all events out of the kernel, resume
   3378  1.3  christos 	 LWPs that don't have an interesting event to report.  */
   3379  1.3  christos       iterate_over_lwps (minus_one_ptid,
   3380  1.3  christos 			 resume_stopped_resumed_lwps, &minus_one_ptid);
   3381  1.3  christos 
   3382  1.1  christos       /* ... and find an LWP with a status to report to the core, if
   3383  1.1  christos 	 any.  */
   3384  1.1  christos       lp = iterate_over_lwps (ptid, status_callback, NULL);
   3385  1.1  christos       if (lp != NULL)
   3386  1.1  christos 	break;
   3387  1.1  christos 
   3388  1.1  christos       /* Check for zombie thread group leaders.  Those can't be reaped
   3389  1.1  christos 	 until all other threads in the thread group are.  */
   3390  1.1  christos       check_zombie_leaders ();
   3391  1.1  christos 
   3392  1.1  christos       /* If there are no resumed children left, bail.  We'd be stuck
   3393  1.1  christos 	 forever in the sigsuspend call below otherwise.  */
   3394  1.1  christos       if (iterate_over_lwps (ptid, resumed_callback, NULL) == NULL)
   3395  1.1  christos 	{
   3396  1.1  christos 	  if (debug_linux_nat)
   3397  1.1  christos 	    fprintf_unfiltered (gdb_stdlog, "LLW: exit (no resumed LWP)\n");
   3398  1.1  christos 
   3399  1.1  christos 	  ourstatus->kind = TARGET_WAITKIND_NO_RESUMED;
   3400  1.1  christos 
   3401  1.1  christos 	  restore_child_signals_mask (&prev_mask);
   3402  1.1  christos 	  return minus_one_ptid;
   3403  1.1  christos 	}
   3404  1.1  christos 
   3405  1.1  christos       /* No interesting event to report to the core.  */
   3406  1.1  christos 
   3407  1.1  christos       if (target_options & TARGET_WNOHANG)
   3408  1.1  christos 	{
   3409  1.1  christos 	  if (debug_linux_nat)
   3410  1.1  christos 	    fprintf_unfiltered (gdb_stdlog, "LLW: exit (ignore)\n");
   3411  1.1  christos 
   3412  1.1  christos 	  ourstatus->kind = TARGET_WAITKIND_IGNORE;
   3413  1.1  christos 	  restore_child_signals_mask (&prev_mask);
   3414  1.1  christos 	  return minus_one_ptid;
   3415  1.3  christos 	}
   3416  1.3  christos 
   3417  1.1  christos       /* We shouldn't end up here unless we want to try again.  */
   3418  1.1  christos       gdb_assert (lp == NULL);
   3419  1.1  christos 
   3420  1.1  christos       /* Block until we get an event reported with SIGCHLD.  */
   3421  1.1  christos       if (debug_linux_nat)
   3422  1.1  christos 	fprintf_unfiltered (gdb_stdlog, "LNW: about to sigsuspend\n");
   3423  1.1  christos       sigsuspend (&suspend_mask);
   3424  1.1  christos     }
   3425  1.6  christos 
   3426  1.3  christos   gdb_assert (lp);
   3427  1.3  christos 
   3428  1.3  christos   status = lp->status;
   3429  1.3  christos   lp->status = 0;
   3430  1.3  christos 
   3431  1.3  christos   if (!target_is_non_stop_p ())
   3432  1.3  christos     {
   3433  1.3  christos       /* Now stop all other LWP's ...  */
   3434  1.3  christos       iterate_over_lwps (minus_one_ptid, stop_callback, NULL);
   3435  1.3  christos 
   3436  1.3  christos       /* ... and wait until all of them have reported back that
   3437  1.3  christos 	 they're no longer running.  */
   3438  1.3  christos       iterate_over_lwps (minus_one_ptid, stop_wait_callback, NULL);
   3439  1.3  christos     }
   3440  1.3  christos 
   3441  1.3  christos   /* If we're not waiting for a specific LWP, choose an event LWP from
   3442  1.1  christos      among those that have had events.  Giving equal priority to all
   3443  1.3  christos      LWPs that have had events helps prevent starvation.  */
   3444  1.5  christos   if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
   3445  1.5  christos     select_event_lwp (ptid, &lp, &status);
   3446  1.5  christos 
   3447  1.5  christos   gdb_assert (lp != NULL);
   3448  1.1  christos 
   3449  1.3  christos   /* Now that we've selected our final event LWP, un-adjust its PC if
   3450  1.3  christos      it was a software breakpoint, and we can't reliably support the
   3451  1.5  christos      "stopped by software breakpoint" stop reason.  */
   3452  1.1  christos   if (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
   3453  1.3  christos       && !USE_SIGTRAP_SIGINFO)
   3454  1.1  christos     {
   3455  1.3  christos       struct regcache *regcache = get_thread_regcache (lp->ptid);
   3456  1.1  christos       struct gdbarch *gdbarch = get_regcache_arch (regcache);
   3457  1.3  christos       int decr_pc = gdbarch_decr_pc_after_break (gdbarch);
   3458  1.3  christos 
   3459  1.1  christos       if (decr_pc != 0)
   3460  1.1  christos 	{
   3461  1.1  christos 	  CORE_ADDR pc;
   3462  1.3  christos 
   3463  1.3  christos 	  pc = regcache_read_pc (regcache);
   3464  1.3  christos 	  regcache_write_pc (regcache, pc + decr_pc);
   3465  1.3  christos 	}
   3466  1.1  christos     }
   3467  1.6  christos 
   3468  1.1  christos   /* We'll need this to determine whether to report a SIGSTOP as
   3469  1.1  christos      GDB_SIGNAL_0.  Need to take a copy because resume_clear_callback
   3470  1.1  christos      clears it.  */
   3471  1.1  christos   last_resume_kind = lp->last_resume_kind;
   3472  1.1  christos 
   3473  1.1  christos   if (!target_is_non_stop_p ())
   3474  1.1  christos     {
   3475  1.1  christos       /* In all-stop, from the core's perspective, all LWPs are now
   3476  1.1  christos 	 stopped until a new resume action is sent over.  */
   3477  1.1  christos       iterate_over_lwps (minus_one_ptid, resume_clear_callback, NULL);
   3478  1.1  christos     }
   3479  1.1  christos   else
   3480  1.1  christos     {
   3481  1.1  christos       resume_clear_callback (lp, NULL);
   3482  1.1  christos     }
   3483  1.1  christos 
   3484  1.1  christos   if (linux_nat_status_is_event (status))
   3485  1.1  christos     {
   3486  1.1  christos       if (debug_linux_nat)
   3487  1.1  christos 	fprintf_unfiltered (gdb_stdlog,
   3488  1.1  christos 			    "LLW: trap ptid is %s.\n",
   3489  1.1  christos 			    target_pid_to_str (lp->ptid));
   3490  1.1  christos     }
   3491  1.1  christos 
   3492  1.1  christos   if (lp->waitstatus.kind != TARGET_WAITKIND_IGNORE)
   3493  1.1  christos     {
   3494  1.1  christos       *ourstatus = lp->waitstatus;
   3495  1.1  christos       lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
   3496  1.1  christos     }
   3497  1.1  christos   else
   3498  1.1  christos     store_waitstatus (ourstatus, status);
   3499  1.1  christos 
   3500  1.1  christos   if (debug_linux_nat)
   3501  1.1  christos     fprintf_unfiltered (gdb_stdlog, "LLW: exit\n");
   3502  1.1  christos 
   3503  1.1  christos   restore_child_signals_mask (&prev_mask);
   3504  1.1  christos 
   3505  1.1  christos   if (last_resume_kind == resume_stop
   3506  1.1  christos       && ourstatus->kind == TARGET_WAITKIND_STOPPED
   3507  1.1  christos       && WSTOPSIG (status) == SIGSTOP)
   3508  1.1  christos     {
   3509  1.1  christos       /* A thread that has been requested to stop by GDB with
   3510  1.1  christos 	 target_stop, and it stopped cleanly, so report as SIG0.  The
   3511  1.1  christos 	 use of SIGSTOP is an implementation detail.  */
   3512  1.1  christos       ourstatus->value.sig = GDB_SIGNAL_0;
   3513  1.1  christos     }
   3514  1.1  christos 
   3515  1.6  christos   if (ourstatus->kind == TARGET_WAITKIND_EXITED
   3516  1.6  christos       || ourstatus->kind == TARGET_WAITKIND_SIGNALLED)
   3517  1.6  christos     lp->core = -1;
   3518  1.1  christos   else
   3519  1.1  christos     lp->core = linux_common_core_of_thread (lp->ptid);
   3520  1.1  christos 
   3521  1.1  christos   if (ourstatus->kind == TARGET_WAITKIND_EXITED)
   3522  1.1  christos     return filter_exit_event (lp, ourstatus);
   3523  1.1  christos 
   3524  1.1  christos   return lp->ptid;
   3525  1.1  christos }
   3526  1.1  christos 
   3527  1.6  christos /* Resume LWPs that are currently stopped without any pending status
   3528  1.1  christos    to report, but are resumed from the core's perspective.  */
   3529  1.5  christos 
   3530  1.5  christos static int
   3531  1.5  christos resume_stopped_resumed_lwps (struct lwp_info *lp, void *data)
   3532  1.5  christos {
   3533  1.5  christos   ptid_t *wait_ptid_p = (ptid_t *) data;
   3534  1.5  christos 
   3535  1.5  christos   if (!lp->stopped)
   3536  1.5  christos     {
   3537  1.5  christos       if (debug_linux_nat)
   3538  1.5  christos 	fprintf_unfiltered (gdb_stdlog,
   3539  1.5  christos 			    "RSRL: NOT resuming LWP %s, not stopped\n",
   3540  1.5  christos 			    target_pid_to_str (lp->ptid));
   3541  1.5  christos     }
   3542  1.5  christos   else if (!lp->resumed)
   3543  1.5  christos     {
   3544  1.5  christos       if (debug_linux_nat)
   3545  1.5  christos 	fprintf_unfiltered (gdb_stdlog,
   3546  1.5  christos 			    "RSRL: NOT resuming LWP %s, not resumed\n",
   3547  1.5  christos 			    target_pid_to_str (lp->ptid));
   3548  1.5  christos     }
   3549  1.5  christos   else if (lwp_status_pending_p (lp))
   3550  1.5  christos     {
   3551  1.1  christos       if (debug_linux_nat)
   3552  1.1  christos 	fprintf_unfiltered (gdb_stdlog,
   3553  1.1  christos 			    "RSRL: NOT resuming LWP %s, has pending status\n",
   3554  1.1  christos 			    target_pid_to_str (lp->ptid));
   3555  1.5  christos     }
   3556  1.5  christos   else
   3557  1.5  christos     {
   3558  1.5  christos       struct regcache *regcache = get_thread_regcache (lp->ptid);
   3559  1.5  christos       struct gdbarch *gdbarch = get_regcache_arch (regcache);
   3560  1.5  christos 
   3561  1.5  christos       TRY
   3562  1.5  christos 	{
   3563  1.5  christos 	  CORE_ADDR pc = regcache_read_pc (regcache);
   3564  1.5  christos 	  int leave_stopped = 0;
   3565  1.5  christos 
   3566  1.5  christos 	  /* Don't bother if there's a breakpoint at PC that we'd hit
   3567  1.5  christos 	     immediately, and we're not waiting for this LWP.  */
   3568  1.5  christos 	  if (!ptid_match (lp->ptid, *wait_ptid_p))
   3569  1.5  christos 	    {
   3570  1.5  christos 	      if (breakpoint_inserted_here_p (get_regcache_aspace (regcache), pc))
   3571  1.5  christos 		leave_stopped = 1;
   3572  1.5  christos 	    }
   3573  1.5  christos 
   3574  1.5  christos 	  if (!leave_stopped)
   3575  1.5  christos 	    {
   3576  1.5  christos 	      if (debug_linux_nat)
   3577  1.1  christos 		fprintf_unfiltered (gdb_stdlog,
   3578  1.5  christos 				    "RSRL: resuming stopped-resumed LWP %s at "
   3579  1.5  christos 				    "%s: step=%d\n",
   3580  1.5  christos 				    target_pid_to_str (lp->ptid),
   3581  1.5  christos 				    paddress (gdbarch, pc),
   3582  1.1  christos 				    lp->step);
   3583  1.5  christos 
   3584  1.5  christos 	      linux_resume_one_lwp_throw (lp, lp->step, GDB_SIGNAL_0);
   3585  1.1  christos 	    }
   3586  1.5  christos 	}
   3587  1.1  christos       CATCH (ex, RETURN_MASK_ERROR)
   3588  1.1  christos 	{
   3589  1.1  christos 	  if (!check_ptrace_stopped_lwp_gone (lp))
   3590  1.1  christos 	    throw_exception (ex);
   3591  1.1  christos 	}
   3592  1.1  christos       END_CATCH
   3593  1.1  christos     }
   3594  1.1  christos 
   3595  1.1  christos   return 0;
   3596  1.1  christos }
   3597  1.1  christos 
   3598  1.1  christos static ptid_t
   3599  1.1  christos linux_nat_wait (struct target_ops *ops,
   3600  1.1  christos 		ptid_t ptid, struct target_waitstatus *ourstatus,
   3601  1.1  christos 		int target_options)
   3602  1.1  christos {
   3603  1.1  christos   ptid_t event_ptid;
   3604  1.1  christos 
   3605  1.1  christos   if (debug_linux_nat)
   3606  1.1  christos     {
   3607  1.1  christos       char *options_string;
   3608  1.1  christos 
   3609  1.1  christos       options_string = target_options_to_string (target_options);
   3610  1.1  christos       fprintf_unfiltered (gdb_stdlog,
   3611  1.1  christos 			  "linux_nat_wait: [%s], [%s]\n",
   3612  1.3  christos 			  target_pid_to_str (ptid),
   3613  1.1  christos 			  options_string);
   3614  1.1  christos       xfree (options_string);
   3615  1.1  christos     }
   3616  1.1  christos 
   3617  1.1  christos   /* Flush the async file first.  */
   3618  1.1  christos   if (target_is_async_p ())
   3619  1.1  christos     async_file_flush ();
   3620  1.1  christos 
   3621  1.1  christos   /* Resume LWPs that are currently stopped without any pending status
   3622  1.6  christos      to report, but are resumed from the core's perspective.  LWPs get
   3623  1.1  christos      in this state if we find them stopping at a time we're not
   3624  1.1  christos      interested in reporting the event (target_wait on a
   3625  1.1  christos      specific_process, for example, see linux_nat_wait_1), and
   3626  1.1  christos      meanwhile the event became uninteresting.  Don't bother resuming
   3627  1.1  christos      LWPs we're not going to wait for if they'd stop immediately.  */
   3628  1.1  christos   if (target_is_non_stop_p ())
   3629  1.1  christos     iterate_over_lwps (minus_one_ptid, resume_stopped_resumed_lwps, &ptid);
   3630  1.3  christos 
   3631  1.1  christos   event_ptid = linux_nat_wait_1 (ops, ptid, ourstatus, target_options);
   3632  1.1  christos 
   3633  1.1  christos   /* If we requested any event, and something came out, assume there
   3634  1.1  christos      may be more.  If we requested a specific lwp or process, also
   3635  1.1  christos      assume there may be more.  */
   3636  1.1  christos   if (target_is_async_p ()
   3637  1.1  christos       && ((ourstatus->kind != TARGET_WAITKIND_IGNORE
   3638  1.1  christos 	   && ourstatus->kind != TARGET_WAITKIND_NO_RESUMED)
   3639  1.6  christos 	  || !ptid_equal (ptid, minus_one_ptid)))
   3640  1.6  christos     async_file_mark ();
   3641  1.6  christos 
   3642  1.6  christos   return event_ptid;
   3643  1.1  christos }
   3644  1.1  christos 
   3645  1.1  christos /* Kill one LWP.  */
   3646  1.1  christos 
   3647  1.6  christos static void
   3648  1.1  christos kill_one_lwp (pid_t pid)
   3649  1.3  christos {
   3650  1.3  christos   /* PTRACE_KILL may resume the inferior.  Send SIGKILL first.  */
   3651  1.3  christos 
   3652  1.3  christos   errno = 0;
   3653  1.6  christos   kill_lwp (pid, SIGKILL);
   3654  1.3  christos   if (debug_linux_nat)
   3655  1.3  christos     {
   3656  1.1  christos       int save_errno = errno;
   3657  1.1  christos 
   3658  1.1  christos       fprintf_unfiltered (gdb_stdlog,
   3659  1.1  christos 			  "KC:  kill (SIGKILL) %ld, 0, 0 (%s)\n", (long) pid,
   3660  1.6  christos 			  save_errno ? safe_strerror (save_errno) : "OK");
   3661  1.1  christos     }
   3662  1.3  christos 
   3663  1.3  christos   /* Some kernels ignore even SIGKILL for processes under ptrace.  */
   3664  1.3  christos 
   3665  1.3  christos   errno = 0;
   3666  1.6  christos   ptrace (PTRACE_KILL, pid, 0, 0);
   3667  1.3  christos   if (debug_linux_nat)
   3668  1.3  christos     {
   3669  1.6  christos       int save_errno = errno;
   3670  1.1  christos 
   3671  1.6  christos       fprintf_unfiltered (gdb_stdlog,
   3672  1.1  christos 			  "KC:  PTRACE_KILL %ld, 0, 0 (%s)\n", (long) pid,
   3673  1.6  christos 			  save_errno ? safe_strerror (save_errno) : "OK");
   3674  1.6  christos     }
   3675  1.1  christos }
   3676  1.6  christos 
   3677  1.1  christos /* Wait for an LWP to die.  */
   3678  1.1  christos 
   3679  1.1  christos static void
   3680  1.1  christos kill_wait_one_lwp (pid_t pid)
   3681  1.1  christos {
   3682  1.1  christos   pid_t res;
   3683  1.1  christos 
   3684  1.6  christos   /* We must make sure that there are no pending events (delayed
   3685  1.6  christos      SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
   3686  1.1  christos      program doesn't interfere with any following debugging session.  */
   3687  1.1  christos 
   3688  1.1  christos   do
   3689  1.6  christos     {
   3690  1.6  christos       res = my_waitpid (pid, NULL, __WALL);
   3691  1.6  christos       if (res != (pid_t) -1)
   3692  1.6  christos 	{
   3693  1.6  christos 	  if (debug_linux_nat)
   3694  1.6  christos 	    fprintf_unfiltered (gdb_stdlog,
   3695  1.6  christos 				"KWC: wait %ld received unknown.\n",
   3696  1.1  christos 				(long) pid);
   3697  1.1  christos 	  /* The Linux kernel sometimes fails to kill a thread
   3698  1.6  christos 	     completely after PTRACE_KILL; that goes from the stop
   3699  1.6  christos 	     point in do_fork out to the one in get_signal_to_deliver
   3700  1.6  christos 	     and waits again.  So kill it again.  */
   3701  1.6  christos 	  kill_one_lwp (pid);
   3702  1.1  christos 	}
   3703  1.6  christos     }
   3704  1.6  christos   while (res == pid);
   3705  1.6  christos 
   3706  1.6  christos   gdb_assert (res == -1 && errno == ECHILD);
   3707  1.6  christos }
   3708  1.6  christos 
   3709  1.1  christos /* Callback for iterate_over_lwps.  */
   3710  1.1  christos 
   3711  1.1  christos static int
   3712  1.6  christos kill_callback (struct lwp_info *lp, void *data)
   3713  1.6  christos {
   3714  1.6  christos   kill_one_lwp (ptid_get_lwp (lp->ptid));
   3715  1.6  christos   return 0;
   3716  1.6  christos }
   3717  1.6  christos 
   3718  1.6  christos /* Callback for iterate_over_lwps.  */
   3719  1.6  christos 
   3720  1.6  christos static int
   3721  1.6  christos kill_wait_callback (struct lwp_info *lp, void *data)
   3722  1.6  christos {
   3723  1.6  christos   kill_wait_one_lwp (ptid_get_lwp (lp->ptid));
   3724  1.6  christos   return 0;
   3725  1.6  christos }
   3726  1.6  christos 
   3727  1.6  christos /* Kill the fork children of any threads of inferior INF that are
   3728  1.6  christos    stopped at a fork event.  */
   3729  1.6  christos 
   3730  1.6  christos static void
   3731  1.6  christos kill_unfollowed_fork_children (struct inferior *inf)
   3732  1.6  christos {
   3733  1.6  christos   struct thread_info *thread;
   3734  1.6  christos 
   3735  1.6  christos   ALL_NON_EXITED_THREADS (thread)
   3736  1.6  christos     if (thread->inf == inf)
   3737  1.6  christos       {
   3738  1.6  christos 	struct target_waitstatus *ws = &thread->pending_follow;
   3739  1.6  christos 
   3740  1.6  christos 	if (ws->kind == TARGET_WAITKIND_FORKED
   3741  1.6  christos 	    || ws->kind == TARGET_WAITKIND_VFORKED)
   3742  1.6  christos 	  {
   3743  1.6  christos 	    ptid_t child_ptid = ws->value.related_pid;
   3744  1.6  christos 	    int child_pid = ptid_get_pid (child_ptid);
   3745  1.6  christos 	    int child_lwp = ptid_get_lwp (child_ptid);
   3746  1.6  christos 
   3747  1.6  christos 	    kill_one_lwp (child_lwp);
   3748  1.6  christos 	    kill_wait_one_lwp (child_lwp);
   3749  1.6  christos 
   3750  1.6  christos 	    /* Let the arch-specific native code know this process is
   3751  1.1  christos 	       gone.  */
   3752  1.1  christos 	    linux_nat_forget_process (child_pid);
   3753  1.1  christos 	  }
   3754  1.1  christos       }
   3755  1.1  christos }
   3756  1.1  christos 
   3757  1.6  christos static void
   3758  1.1  christos linux_nat_kill (struct target_ops *ops)
   3759  1.1  christos {
   3760  1.1  christos   /* If we're stopped while forking and we haven't followed yet,
   3761  1.1  christos      kill the other task.  We need to do this first because the
   3762  1.1  christos      parent will be sleeping if this is a vfork.  */
   3763  1.1  christos   kill_unfollowed_fork_children (current_inferior ());
   3764  1.1  christos 
   3765  1.1  christos   if (forks_exist_p ())
   3766  1.1  christos     linux_fork_killall ();
   3767  1.1  christos   else
   3768  1.1  christos     {
   3769  1.1  christos       ptid_t ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
   3770  1.1  christos 
   3771  1.1  christos       /* Stop all threads before killing them, since ptrace requires
   3772  1.1  christos 	 that the thread is stopped to sucessfully PTRACE_KILL.  */
   3773  1.1  christos       iterate_over_lwps (ptid, stop_callback, NULL);
   3774  1.1  christos       /* ... and wait until all of them have reported back that
   3775  1.1  christos 	 they're no longer running.  */
   3776  1.1  christos       iterate_over_lwps (ptid, stop_wait_callback, NULL);
   3777  1.1  christos 
   3778  1.1  christos       /* Kill all LWP's ...  */
   3779  1.1  christos       iterate_over_lwps (ptid, kill_callback, NULL);
   3780  1.1  christos 
   3781  1.1  christos       /* ... and wait until we've flushed all events.  */
   3782  1.1  christos       iterate_over_lwps (ptid, kill_wait_callback, NULL);
   3783  1.1  christos     }
   3784  1.1  christos 
   3785  1.1  christos   target_mourn_inferior ();
   3786  1.1  christos }
   3787  1.1  christos 
   3788  1.1  christos static void
   3789  1.1  christos linux_nat_mourn_inferior (struct target_ops *ops)
   3790  1.1  christos {
   3791  1.1  christos   int pid = ptid_get_pid (inferior_ptid);
   3792  1.1  christos 
   3793  1.1  christos   purge_lwp_list (pid);
   3794  1.1  christos 
   3795  1.1  christos   if (! forks_exist_p ())
   3796  1.1  christos     /* Normal case, no other forks available.  */
   3797  1.1  christos     linux_ops->to_mourn_inferior (ops);
   3798  1.1  christos   else
   3799  1.1  christos     /* Multi-fork case.  The current inferior_ptid has exited, but
   3800  1.1  christos        there are other viable forks to debug.  Delete the exiting
   3801  1.1  christos        one and context-switch to the first available.  */
   3802  1.1  christos     linux_fork_mourn_inferior ();
   3803  1.1  christos 
   3804  1.1  christos   /* Let the arch-specific native code know this process is gone.  */
   3805  1.1  christos   linux_nat_forget_process (pid);
   3806  1.1  christos }
   3807  1.1  christos 
   3808  1.1  christos /* Convert a native/host siginfo object, into/from the siginfo in the
   3809  1.1  christos    layout of the inferiors' architecture.  */
   3810  1.1  christos 
   3811  1.1  christos static void
   3812  1.1  christos siginfo_fixup (siginfo_t *siginfo, gdb_byte *inf_siginfo, int direction)
   3813  1.1  christos {
   3814  1.1  christos   int done = 0;
   3815  1.1  christos 
   3816  1.1  christos   if (linux_nat_siginfo_fixup != NULL)
   3817  1.1  christos     done = linux_nat_siginfo_fixup (siginfo, inf_siginfo, direction);
   3818  1.1  christos 
   3819  1.1  christos   /* If there was no callback, or the callback didn't do anything,
   3820  1.1  christos      then just do a straight memcpy.  */
   3821  1.1  christos   if (!done)
   3822  1.1  christos     {
   3823  1.1  christos       if (direction == 1)
   3824  1.3  christos 	memcpy (siginfo, inf_siginfo, sizeof (siginfo_t));
   3825  1.1  christos       else
   3826  1.1  christos 	memcpy (inf_siginfo, siginfo, sizeof (siginfo_t));
   3827  1.3  christos     }
   3828  1.3  christos }
   3829  1.1  christos 
   3830  1.1  christos static enum target_xfer_status
   3831  1.1  christos linux_xfer_siginfo (struct target_ops *ops, enum target_object object,
   3832  1.1  christos                     const char *annex, gdb_byte *readbuf,
   3833  1.1  christos 		    const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
   3834  1.1  christos 		    ULONGEST *xfered_len)
   3835  1.1  christos {
   3836  1.1  christos   int pid;
   3837  1.1  christos   siginfo_t siginfo;
   3838  1.1  christos   gdb_byte inf_siginfo[sizeof (siginfo_t)];
   3839  1.1  christos 
   3840  1.1  christos   gdb_assert (object == TARGET_OBJECT_SIGNAL_INFO);
   3841  1.1  christos   gdb_assert (readbuf || writebuf);
   3842  1.3  christos 
   3843  1.1  christos   pid = ptid_get_lwp (inferior_ptid);
   3844  1.1  christos   if (pid == 0)
   3845  1.1  christos     pid = ptid_get_pid (inferior_ptid);
   3846  1.1  christos 
   3847  1.3  christos   if (offset > sizeof (siginfo))
   3848  1.1  christos     return TARGET_XFER_E_IO;
   3849  1.1  christos 
   3850  1.1  christos   errno = 0;
   3851  1.1  christos   ptrace (PTRACE_GETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo);
   3852  1.1  christos   if (errno != 0)
   3853  1.1  christos     return TARGET_XFER_E_IO;
   3854  1.1  christos 
   3855  1.1  christos   /* When GDB is built as a 64-bit application, ptrace writes into
   3856  1.1  christos      SIGINFO an object with 64-bit layout.  Since debugging a 32-bit
   3857  1.1  christos      inferior with a 64-bit GDB should look the same as debugging it
   3858  1.1  christos      with a 32-bit GDB, we need to convert it.  GDB core always sees
   3859  1.1  christos      the converted layout, so any read/write will have to be done
   3860  1.1  christos      post-conversion.  */
   3861  1.1  christos   siginfo_fixup (&siginfo, inf_siginfo, 0);
   3862  1.1  christos 
   3863  1.1  christos   if (offset + len > sizeof (siginfo))
   3864  1.1  christos     len = sizeof (siginfo) - offset;
   3865  1.1  christos 
   3866  1.1  christos   if (readbuf != NULL)
   3867  1.1  christos     memcpy (readbuf, inf_siginfo + offset, len);
   3868  1.1  christos   else
   3869  1.1  christos     {
   3870  1.1  christos       memcpy (inf_siginfo + offset, writebuf, len);
   3871  1.1  christos 
   3872  1.3  christos       /* Convert back to ptrace layout before flushing it out.  */
   3873  1.1  christos       siginfo_fixup (&siginfo, inf_siginfo, 1);
   3874  1.1  christos 
   3875  1.3  christos       errno = 0;
   3876  1.3  christos       ptrace (PTRACE_SETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo);
   3877  1.1  christos       if (errno != 0)
   3878  1.1  christos 	return TARGET_XFER_E_IO;
   3879  1.3  christos     }
   3880  1.1  christos 
   3881  1.1  christos   *xfered_len = len;
   3882  1.1  christos   return TARGET_XFER_OK;
   3883  1.3  christos }
   3884  1.1  christos 
   3885  1.1  christos static enum target_xfer_status
   3886  1.3  christos linux_nat_xfer_partial (struct target_ops *ops, enum target_object object,
   3887  1.1  christos 			const char *annex, gdb_byte *readbuf,
   3888  1.1  christos 			const gdb_byte *writebuf,
   3889  1.1  christos 			ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
   3890  1.3  christos {
   3891  1.1  christos   struct cleanup *old_chain;
   3892  1.1  christos   enum target_xfer_status xfer;
   3893  1.1  christos 
   3894  1.1  christos   if (object == TARGET_OBJECT_SIGNAL_INFO)
   3895  1.1  christos     return linux_xfer_siginfo (ops, object, annex, readbuf, writebuf,
   3896  1.3  christos 			       offset, len, xfered_len);
   3897  1.1  christos 
   3898  1.1  christos   /* The target is connected but no live inferior is selected.  Pass
   3899  1.1  christos      this request down to a lower stratum (e.g., the executable
   3900  1.1  christos      file).  */
   3901  1.1  christos   if (object == TARGET_OBJECT_MEMORY && ptid_equal (inferior_ptid, null_ptid))
   3902  1.1  christos     return TARGET_XFER_EOF;
   3903  1.1  christos 
   3904  1.3  christos   old_chain = save_inferior_ptid ();
   3905  1.1  christos 
   3906  1.1  christos   if (ptid_lwp_p (inferior_ptid))
   3907  1.1  christos     inferior_ptid = pid_to_ptid (ptid_get_lwp (inferior_ptid));
   3908  1.1  christos 
   3909  1.1  christos   xfer = linux_ops->to_xfer_partial (ops, object, annex, readbuf, writebuf,
   3910  1.1  christos 				     offset, len, xfered_len);
   3911  1.1  christos 
   3912  1.1  christos   do_cleanups (old_chain);
   3913  1.6  christos   return xfer;
   3914  1.6  christos }
   3915  1.1  christos 
   3916  1.1  christos static int
   3917  1.5  christos linux_nat_thread_alive (struct target_ops *ops, ptid_t ptid)
   3918  1.5  christos {
   3919  1.5  christos   /* As long as a PTID is in lwp list, consider it alive.  */
   3920  1.5  christos   return find_lwp_pid (ptid) != NULL;
   3921  1.5  christos }
   3922  1.5  christos 
   3923  1.6  christos /* Implement the to_update_thread_list target method for this
   3924  1.6  christos    target.  */
   3925  1.6  christos 
   3926  1.6  christos static void
   3927  1.6  christos linux_nat_update_thread_list (struct target_ops *ops)
   3928  1.6  christos {
   3929  1.6  christos   struct lwp_info *lwp;
   3930  1.6  christos 
   3931  1.6  christos   /* We add/delete threads from the list as clone/exit events are
   3932  1.6  christos      processed, so just try deleting exited threads still in the
   3933  1.6  christos      thread list.  */
   3934  1.6  christos   delete_exited_threads ();
   3935  1.6  christos 
   3936  1.6  christos   /* Update the processor core that each lwp/thread was last seen
   3937  1.6  christos      running on.  */
   3938  1.6  christos   ALL_LWPS (lwp)
   3939  1.5  christos     {
   3940  1.5  christos       /* Avoid accessing /proc if the thread hasn't run since we last
   3941  1.5  christos 	 time we fetched the thread's core.  Accessing /proc becomes
   3942  1.1  christos 	 noticeably expensive when we have thousands of LWPs.  */
   3943  1.1  christos       if (lwp->core == -1)
   3944  1.1  christos 	lwp->core = linux_common_core_of_thread (lwp->ptid);
   3945  1.1  christos     }
   3946  1.1  christos }
   3947  1.1  christos 
   3948  1.1  christos static char *
   3949  1.1  christos linux_nat_pid_to_str (struct target_ops *ops, ptid_t ptid)
   3950  1.1  christos {
   3951  1.1  christos   static char buf[64];
   3952  1.1  christos 
   3953  1.1  christos   if (ptid_lwp_p (ptid)
   3954  1.1  christos       && (ptid_get_pid (ptid) != ptid_get_lwp (ptid)
   3955  1.1  christos 	  || num_lwps (ptid_get_pid (ptid)) > 1))
   3956  1.1  christos     {
   3957  1.1  christos       snprintf (buf, sizeof (buf), "LWP %ld", ptid_get_lwp (ptid));
   3958  1.6  christos       return buf;
   3959  1.3  christos     }
   3960  1.1  christos 
   3961  1.6  christos   return normal_pid_to_str (ptid);
   3962  1.1  christos }
   3963  1.1  christos 
   3964  1.1  christos static const char *
   3965  1.1  christos linux_nat_thread_name (struct target_ops *self, struct thread_info *thr)
   3966  1.1  christos {
   3967  1.1  christos   return linux_proc_tid_get_name (thr->ptid);
   3968  1.3  christos }
   3969  1.1  christos 
   3970  1.5  christos /* Accepts an integer PID; Returns a string representing a file that
   3971  1.1  christos    can be opened to get the symbols for the child process.  */
   3972  1.1  christos 
   3973  1.1  christos static char *
   3974  1.1  christos linux_child_pid_to_exec_file (struct target_ops *self, int pid)
   3975  1.1  christos {
   3976  1.1  christos   return linux_proc_pid_to_exec_file (pid);
   3977  1.1  christos }
   3978  1.3  christos 
   3979  1.1  christos /* Implement the to_xfer_partial interface for memory reads using the /proc
   3980  1.1  christos    filesystem.  Because we can use a single read() call for /proc, this
   3981  1.1  christos    can be much more efficient than banging away at PTRACE_PEEKTEXT,
   3982  1.3  christos    but it doesn't support writes.  */
   3983  1.1  christos 
   3984  1.1  christos static enum target_xfer_status
   3985  1.1  christos linux_proc_xfer_partial (struct target_ops *ops, enum target_object object,
   3986  1.1  christos 			 const char *annex, gdb_byte *readbuf,
   3987  1.1  christos 			 const gdb_byte *writebuf,
   3988  1.1  christos 			 ULONGEST offset, LONGEST len, ULONGEST *xfered_len)
   3989  1.6  christos {
   3990  1.1  christos   LONGEST ret;
   3991  1.1  christos   int fd;
   3992  1.1  christos   char filename[64];
   3993  1.3  christos 
   3994  1.1  christos   if (object != TARGET_OBJECT_MEMORY || !readbuf)
   3995  1.1  christos     return TARGET_XFER_EOF;
   3996  1.1  christos 
   3997  1.1  christos   /* Don't bother for one word.  */
   3998  1.1  christos   if (len < 3 * sizeof (long))
   3999  1.1  christos     return TARGET_XFER_EOF;
   4000  1.1  christos 
   4001  1.3  christos   /* We could keep this file open and cache it - possibly one per
   4002  1.1  christos      thread.  That requires some juggling, but is even faster.  */
   4003  1.1  christos   xsnprintf (filename, sizeof filename, "/proc/%d/mem",
   4004  1.1  christos 	     ptid_get_pid (inferior_ptid));
   4005  1.1  christos   fd = gdb_open_cloexec (filename, O_RDONLY | O_LARGEFILE, 0);
   4006  1.1  christos   if (fd == -1)
   4007  1.1  christos     return TARGET_XFER_EOF;
   4008  1.1  christos 
   4009  1.1  christos   /* If pread64 is available, use it.  It's faster if the kernel
   4010  1.1  christos      supports it (only one syscall), and it's 64-bit safe even on
   4011  1.1  christos      32-bit platforms (for instance, SPARC debugging a SPARC64
   4012  1.1  christos      application).  */
   4013  1.1  christos #ifdef HAVE_PREAD64
   4014  1.1  christos   if (pread64 (fd, readbuf, len, offset) != len)
   4015  1.1  christos #else
   4016  1.1  christos   if (lseek (fd, offset, SEEK_SET) == -1 || read (fd, readbuf, len) != len)
   4017  1.3  christos #endif
   4018  1.3  christos     ret = 0;
   4019  1.3  christos   else
   4020  1.3  christos     ret = len;
   4021  1.3  christos 
   4022  1.3  christos   close (fd);
   4023  1.3  christos 
   4024  1.3  christos   if (ret == 0)
   4025  1.1  christos     return TARGET_XFER_EOF;
   4026  1.1  christos   else
   4027  1.1  christos     {
   4028  1.1  christos       *xfered_len = ret;
   4029  1.1  christos       return TARGET_XFER_OK;
   4030  1.3  christos     }
   4031  1.1  christos }
   4032  1.1  christos 
   4033  1.1  christos 
   4034  1.1  christos /* Enumerate spufs IDs for process PID.  */
   4035  1.1  christos static LONGEST
   4036  1.1  christos spu_enumerate_spu_ids (int pid, gdb_byte *buf, ULONGEST offset, ULONGEST len)
   4037  1.1  christos {
   4038  1.1  christos   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
   4039  1.1  christos   LONGEST pos = 0;
   4040  1.1  christos   LONGEST written = 0;
   4041  1.1  christos   char path[128];
   4042  1.1  christos   DIR *dir;
   4043  1.1  christos   struct dirent *entry;
   4044  1.1  christos 
   4045  1.1  christos   xsnprintf (path, sizeof path, "/proc/%d/fd", pid);
   4046  1.1  christos   dir = opendir (path);
   4047  1.1  christos   if (!dir)
   4048  1.1  christos     return -1;
   4049  1.1  christos 
   4050  1.1  christos   rewinddir (dir);
   4051  1.1  christos   while ((entry = readdir (dir)) != NULL)
   4052  1.1  christos     {
   4053  1.1  christos       struct stat st;
   4054  1.1  christos       struct statfs stfs;
   4055  1.1  christos       int fd;
   4056  1.1  christos 
   4057  1.1  christos       fd = atoi (entry->d_name);
   4058  1.1  christos       if (!fd)
   4059  1.1  christos 	continue;
   4060  1.1  christos 
   4061  1.1  christos       xsnprintf (path, sizeof path, "/proc/%d/fd/%d", pid, fd);
   4062  1.1  christos       if (stat (path, &st) != 0)
   4063  1.1  christos 	continue;
   4064  1.1  christos       if (!S_ISDIR (st.st_mode))
   4065  1.1  christos 	continue;
   4066  1.1  christos 
   4067  1.1  christos       if (statfs (path, &stfs) != 0)
   4068  1.1  christos 	continue;
   4069  1.1  christos       if (stfs.f_type != SPUFS_MAGIC)
   4070  1.1  christos 	continue;
   4071  1.1  christos 
   4072  1.1  christos       if (pos >= offset && pos + 4 <= offset + len)
   4073  1.1  christos 	{
   4074  1.1  christos 	  store_unsigned_integer (buf + pos - offset, 4, byte_order, fd);
   4075  1.1  christos 	  written += 4;
   4076  1.1  christos 	}
   4077  1.1  christos       pos += 4;
   4078  1.1  christos     }
   4079  1.1  christos 
   4080  1.3  christos   closedir (dir);
   4081  1.3  christos   return written;
   4082  1.1  christos }
   4083  1.1  christos 
   4084  1.1  christos /* Implement the to_xfer_partial interface for the TARGET_OBJECT_SPU
   4085  1.3  christos    object type, using the /proc file system.  */
   4086  1.1  christos 
   4087  1.1  christos static enum target_xfer_status
   4088  1.1  christos linux_proc_xfer_spu (struct target_ops *ops, enum target_object object,
   4089  1.1  christos 		     const char *annex, gdb_byte *readbuf,
   4090  1.1  christos 		     const gdb_byte *writebuf,
   4091  1.1  christos 		     ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
   4092  1.1  christos {
   4093  1.1  christos   char buf[128];
   4094  1.1  christos   int fd = 0;
   4095  1.3  christos   int ret = -1;
   4096  1.1  christos   int pid = ptid_get_pid (inferior_ptid);
   4097  1.3  christos 
   4098  1.3  christos   if (!annex)
   4099  1.3  christos     {
   4100  1.3  christos       if (!readbuf)
   4101  1.3  christos 	return TARGET_XFER_E_IO;
   4102  1.3  christos       else
   4103  1.3  christos 	{
   4104  1.3  christos 	  LONGEST l = spu_enumerate_spu_ids (pid, readbuf, offset, len);
   4105  1.3  christos 
   4106  1.3  christos 	  if (l < 0)
   4107  1.3  christos 	    return TARGET_XFER_E_IO;
   4108  1.3  christos 	  else if (l == 0)
   4109  1.3  christos 	    return TARGET_XFER_EOF;
   4110  1.1  christos 	  else
   4111  1.1  christos 	    {
   4112  1.1  christos 	      *xfered_len = (ULONGEST) l;
   4113  1.1  christos 	      return TARGET_XFER_OK;
   4114  1.1  christos 	    }
   4115  1.3  christos 	}
   4116  1.1  christos     }
   4117  1.1  christos 
   4118  1.1  christos   xsnprintf (buf, sizeof buf, "/proc/%d/fd/%s", pid, annex);
   4119  1.1  christos   fd = gdb_open_cloexec (buf, writebuf? O_WRONLY : O_RDONLY, 0);
   4120  1.1  christos   if (fd <= 0)
   4121  1.3  christos     return TARGET_XFER_E_IO;
   4122  1.1  christos 
   4123  1.1  christos   if (offset != 0
   4124  1.1  christos       && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
   4125  1.1  christos     {
   4126  1.1  christos       close (fd);
   4127  1.1  christos       return TARGET_XFER_EOF;
   4128  1.1  christos     }
   4129  1.1  christos 
   4130  1.3  christos   if (writebuf)
   4131  1.3  christos     ret = write (fd, writebuf, (size_t) len);
   4132  1.3  christos   else if (readbuf)
   4133  1.3  christos     ret = read (fd, readbuf, (size_t) len);
   4134  1.3  christos 
   4135  1.3  christos   close (fd);
   4136  1.3  christos 
   4137  1.3  christos   if (ret < 0)
   4138  1.3  christos     return TARGET_XFER_E_IO;
   4139  1.3  christos   else if (ret == 0)
   4140  1.1  christos     return TARGET_XFER_EOF;
   4141  1.1  christos   else
   4142  1.1  christos     {
   4143  1.1  christos       *xfered_len = (ULONGEST) ret;
   4144  1.1  christos       return TARGET_XFER_OK;
   4145  1.1  christos     }
   4146  1.1  christos }
   4147  1.1  christos 
   4148  1.1  christos 
   4149  1.1  christos /* Parse LINE as a signal set and add its set bits to SIGS.  */
   4150  1.1  christos 
   4151  1.1  christos static void
   4152  1.1  christos add_line_to_sigset (const char *line, sigset_t *sigs)
   4153  1.1  christos {
   4154  1.1  christos   int len = strlen (line) - 1;
   4155  1.1  christos   const char *p;
   4156  1.1  christos   int signum;
   4157  1.1  christos 
   4158  1.1  christos   if (line[len] != '\n')
   4159  1.1  christos     error (_("Could not parse signal set: %s"), line);
   4160  1.1  christos 
   4161  1.1  christos   p = line;
   4162  1.1  christos   signum = len * 4;
   4163  1.1  christos   while (len-- > 0)
   4164  1.1  christos     {
   4165  1.1  christos       int digit;
   4166  1.1  christos 
   4167  1.1  christos       if (*p >= '0' && *p <= '9')
   4168  1.1  christos 	digit = *p - '0';
   4169  1.1  christos       else if (*p >= 'a' && *p <= 'f')
   4170  1.1  christos 	digit = *p - 'a' + 10;
   4171  1.1  christos       else
   4172  1.1  christos 	error (_("Could not parse signal set: %s"), line);
   4173  1.1  christos 
   4174  1.1  christos       signum -= 4;
   4175  1.1  christos 
   4176  1.1  christos       if (digit & 1)
   4177  1.1  christos 	sigaddset (sigs, signum + 1);
   4178  1.1  christos       if (digit & 2)
   4179  1.1  christos 	sigaddset (sigs, signum + 2);
   4180  1.1  christos       if (digit & 4)
   4181  1.1  christos 	sigaddset (sigs, signum + 3);
   4182  1.1  christos       if (digit & 8)
   4183  1.1  christos 	sigaddset (sigs, signum + 4);
   4184  1.1  christos 
   4185  1.1  christos       p++;
   4186  1.1  christos     }
   4187  1.1  christos }
   4188  1.1  christos 
   4189  1.1  christos /* Find process PID's pending signals from /proc/pid/status and set
   4190  1.1  christos    SIGS to match.  */
   4191  1.1  christos 
   4192  1.1  christos void
   4193  1.1  christos linux_proc_pending_signals (int pid, sigset_t *pending,
   4194  1.1  christos 			    sigset_t *blocked, sigset_t *ignored)
   4195  1.1  christos {
   4196  1.1  christos   FILE *procfile;
   4197  1.1  christos   char buffer[PATH_MAX], fname[PATH_MAX];
   4198  1.1  christos   struct cleanup *cleanup;
   4199  1.1  christos 
   4200  1.1  christos   sigemptyset (pending);
   4201  1.1  christos   sigemptyset (blocked);
   4202  1.1  christos   sigemptyset (ignored);
   4203  1.1  christos   xsnprintf (fname, sizeof fname, "/proc/%d/status", pid);
   4204  1.1  christos   procfile = gdb_fopen_cloexec (fname, "r");
   4205  1.1  christos   if (procfile == NULL)
   4206  1.1  christos     error (_("Could not open %s"), fname);
   4207  1.1  christos   cleanup = make_cleanup_fclose (procfile);
   4208  1.1  christos 
   4209  1.1  christos   while (fgets (buffer, PATH_MAX, procfile) != NULL)
   4210  1.1  christos     {
   4211  1.1  christos       /* Normal queued signals are on the SigPnd line in the status
   4212  1.1  christos 	 file.  However, 2.6 kernels also have a "shared" pending
   4213  1.5  christos 	 queue for delivering signals to a thread group, so check for
   4214  1.1  christos 	 a ShdPnd line also.
   4215  1.5  christos 
   4216  1.1  christos 	 Unfortunately some Red Hat kernels include the shared pending
   4217  1.5  christos 	 queue but not the ShdPnd status field.  */
   4218  1.1  christos 
   4219  1.5  christos       if (startswith (buffer, "SigPnd:\t"))
   4220  1.1  christos 	add_line_to_sigset (buffer + 8, pending);
   4221  1.1  christos       else if (startswith (buffer, "ShdPnd:\t"))
   4222  1.1  christos 	add_line_to_sigset (buffer + 8, pending);
   4223  1.1  christos       else if (startswith (buffer, "SigBlk:\t"))
   4224  1.1  christos 	add_line_to_sigset (buffer + 8, blocked);
   4225  1.1  christos       else if (startswith (buffer, "SigIgn:\t"))
   4226  1.3  christos 	add_line_to_sigset (buffer + 8, ignored);
   4227  1.1  christos     }
   4228  1.1  christos 
   4229  1.3  christos   do_cleanups (cleanup);
   4230  1.3  christos }
   4231  1.1  christos 
   4232  1.1  christos static enum target_xfer_status
   4233  1.1  christos linux_nat_xfer_osdata (struct target_ops *ops, enum target_object object,
   4234  1.3  christos 		       const char *annex, gdb_byte *readbuf,
   4235  1.3  christos 		       const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
   4236  1.3  christos 		       ULONGEST *xfered_len)
   4237  1.3  christos {
   4238  1.3  christos   gdb_assert (object == TARGET_OBJECT_OSDATA);
   4239  1.1  christos 
   4240  1.1  christos   *xfered_len = linux_common_xfer_osdata (annex, readbuf, offset, len);
   4241  1.3  christos   if (*xfered_len == 0)
   4242  1.1  christos     return TARGET_XFER_EOF;
   4243  1.1  christos   else
   4244  1.3  christos     return TARGET_XFER_OK;
   4245  1.3  christos }
   4246  1.1  christos 
   4247  1.3  christos static enum target_xfer_status
   4248  1.1  christos linux_xfer_partial (struct target_ops *ops, enum target_object object,
   4249  1.1  christos                     const char *annex, gdb_byte *readbuf,
   4250  1.1  christos 		    const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
   4251  1.3  christos 		    ULONGEST *xfered_len)
   4252  1.1  christos {
   4253  1.1  christos   enum target_xfer_status xfer;
   4254  1.1  christos 
   4255  1.3  christos   if (object == TARGET_OBJECT_AUXV)
   4256  1.1  christos     return memory_xfer_auxv (ops, object, annex, readbuf, writebuf,
   4257  1.1  christos 			     offset, len, xfered_len);
   4258  1.1  christos 
   4259  1.3  christos   if (object == TARGET_OBJECT_OSDATA)
   4260  1.1  christos     return linux_nat_xfer_osdata (ops, object, annex, readbuf, writebuf,
   4261  1.1  christos 				  offset, len, xfered_len);
   4262  1.1  christos 
   4263  1.1  christos   if (object == TARGET_OBJECT_SPU)
   4264  1.1  christos     return linux_proc_xfer_spu (ops, object, annex, readbuf, writebuf,
   4265  1.1  christos 				offset, len, xfered_len);
   4266  1.1  christos 
   4267  1.1  christos   /* GDB calculates all the addresses in possibly larget width of the address.
   4268  1.1  christos      Address width needs to be masked before its final use - either by
   4269  1.1  christos      linux_proc_xfer_partial or inf_ptrace_xfer_partial.
   4270  1.1  christos 
   4271  1.1  christos      Compare ADDR_BIT first to avoid a compiler warning on shift overflow.  */
   4272  1.1  christos 
   4273  1.1  christos   if (object == TARGET_OBJECT_MEMORY)
   4274  1.1  christos     {
   4275  1.1  christos       int addr_bit = gdbarch_addr_bit (target_gdbarch ());
   4276  1.3  christos 
   4277  1.3  christos       if (addr_bit < (sizeof (ULONGEST) * HOST_CHAR_BIT))
   4278  1.1  christos 	offset &= ((ULONGEST) 1 << addr_bit) - 1;
   4279  1.1  christos     }
   4280  1.1  christos 
   4281  1.3  christos   xfer = linux_proc_xfer_partial (ops, object, annex, readbuf, writebuf,
   4282  1.1  christos 				  offset, len, xfered_len);
   4283  1.1  christos   if (xfer != TARGET_XFER_EOF)
   4284  1.1  christos     return xfer;
   4285  1.1  christos 
   4286  1.1  christos   return super_xfer_partial (ops, object, annex, readbuf, writebuf,
   4287  1.1  christos 			     offset, len, xfered_len);
   4288  1.1  christos }
   4289  1.1  christos 
   4290  1.1  christos static void
   4291  1.1  christos cleanup_target_stop (void *arg)
   4292  1.1  christos {
   4293  1.1  christos   ptid_t *ptid = (ptid_t *) arg;
   4294  1.1  christos 
   4295  1.1  christos   gdb_assert (arg != NULL);
   4296  1.3  christos 
   4297  1.3  christos   /* Unpause all */
   4298  1.1  christos   target_resume (*ptid, 0, GDB_SIGNAL_0);
   4299  1.1  christos }
   4300  1.1  christos 
   4301  1.1  christos static VEC(static_tracepoint_marker_p) *
   4302  1.1  christos linux_child_static_tracepoint_markers_by_strid (struct target_ops *self,
   4303  1.1  christos 						const char *strid)
   4304  1.1  christos {
   4305  1.1  christos   char s[IPA_CMD_BUF_SIZE];
   4306  1.1  christos   struct cleanup *old_chain;
   4307  1.1  christos   int pid = ptid_get_pid (inferior_ptid);
   4308  1.1  christos   VEC(static_tracepoint_marker_p) *markers = NULL;
   4309  1.1  christos   struct static_tracepoint_marker *marker = NULL;
   4310  1.1  christos   char *p = s;
   4311  1.1  christos   ptid_t ptid = ptid_build (pid, 0, 0);
   4312  1.1  christos 
   4313  1.1  christos   /* Pause all */
   4314  1.1  christos   target_stop (ptid);
   4315  1.1  christos 
   4316  1.1  christos   memcpy (s, "qTfSTM", sizeof ("qTfSTM"));
   4317  1.1  christos   s[sizeof ("qTfSTM")] = 0;
   4318  1.1  christos 
   4319  1.1  christos   agent_run_command (pid, s, strlen (s) + 1);
   4320  1.1  christos 
   4321  1.1  christos   old_chain = make_cleanup (free_current_marker, &marker);
   4322  1.1  christos   make_cleanup (cleanup_target_stop, &ptid);
   4323  1.1  christos 
   4324  1.1  christos   while (*p++ == 'm')
   4325  1.1  christos     {
   4326  1.1  christos       if (marker == NULL)
   4327  1.1  christos 	marker = XCNEW (struct static_tracepoint_marker);
   4328  1.1  christos 
   4329  1.1  christos       do
   4330  1.1  christos 	{
   4331  1.1  christos 	  parse_static_tracepoint_marker_definition (p, &p, marker);
   4332  1.1  christos 
   4333  1.1  christos 	  if (strid == NULL || strcmp (strid, marker->str_id) == 0)
   4334  1.1  christos 	    {
   4335  1.1  christos 	      VEC_safe_push (static_tracepoint_marker_p,
   4336  1.1  christos 			     markers, marker);
   4337  1.1  christos 	      marker = NULL;
   4338  1.1  christos 	    }
   4339  1.1  christos 	  else
   4340  1.1  christos 	    {
   4341  1.1  christos 	      release_static_tracepoint_marker (marker);
   4342  1.1  christos 	      memset (marker, 0, sizeof (*marker));
   4343  1.1  christos 	    }
   4344  1.1  christos 	}
   4345  1.1  christos       while (*p++ == ',');	/* comma-separated list */
   4346  1.1  christos 
   4347  1.1  christos       memcpy (s, "qTsSTM", sizeof ("qTsSTM"));
   4348  1.1  christos       s[sizeof ("qTsSTM")] = 0;
   4349  1.1  christos       agent_run_command (pid, s, strlen (s) + 1);
   4350  1.1  christos       p = s;
   4351  1.1  christos     }
   4352  1.1  christos 
   4353  1.1  christos   do_cleanups (old_chain);
   4354  1.1  christos 
   4355  1.1  christos   return markers;
   4356  1.1  christos }
   4357  1.1  christos 
   4358  1.1  christos /* Create a prototype generic GNU/Linux target.  The client can override
   4359  1.1  christos    it with local methods.  */
   4360  1.1  christos 
   4361  1.1  christos static void
   4362  1.1  christos linux_target_install_ops (struct target_ops *t)
   4363  1.1  christos {
   4364  1.1  christos   t->to_insert_fork_catchpoint = linux_child_insert_fork_catchpoint;
   4365  1.1  christos   t->to_remove_fork_catchpoint = linux_child_remove_fork_catchpoint;
   4366  1.1  christos   t->to_insert_vfork_catchpoint = linux_child_insert_vfork_catchpoint;
   4367  1.1  christos   t->to_remove_vfork_catchpoint = linux_child_remove_vfork_catchpoint;
   4368  1.1  christos   t->to_insert_exec_catchpoint = linux_child_insert_exec_catchpoint;
   4369  1.1  christos   t->to_remove_exec_catchpoint = linux_child_remove_exec_catchpoint;
   4370  1.1  christos   t->to_set_syscall_catchpoint = linux_child_set_syscall_catchpoint;
   4371  1.1  christos   t->to_pid_to_exec_file = linux_child_pid_to_exec_file;
   4372  1.1  christos   t->to_post_startup_inferior = linux_child_post_startup_inferior;
   4373  1.1  christos   t->to_post_attach = linux_child_post_attach;
   4374  1.1  christos   t->to_follow_fork = linux_child_follow_fork;
   4375  1.1  christos 
   4376  1.1  christos   super_xfer_partial = t->to_xfer_partial;
   4377  1.1  christos   t->to_xfer_partial = linux_xfer_partial;
   4378  1.1  christos 
   4379  1.1  christos   t->to_static_tracepoint_markers_by_strid
   4380  1.1  christos     = linux_child_static_tracepoint_markers_by_strid;
   4381  1.1  christos }
   4382  1.1  christos 
   4383  1.1  christos struct target_ops *
   4384  1.1  christos linux_target (void)
   4385  1.1  christos {
   4386  1.1  christos   struct target_ops *t;
   4387  1.1  christos 
   4388  1.1  christos   t = inf_ptrace_target ();
   4389  1.1  christos   linux_target_install_ops (t);
   4390  1.1  christos 
   4391  1.1  christos   return t;
   4392  1.1  christos }
   4393  1.1  christos 
   4394  1.1  christos struct target_ops *
   4395  1.1  christos linux_trad_target (CORE_ADDR (*register_u_offset)(struct gdbarch *, int, int))
   4396  1.1  christos {
   4397  1.1  christos   struct target_ops *t;
   4398  1.1  christos 
   4399  1.1  christos   t = inf_ptrace_trad_target (register_u_offset);
   4400  1.1  christos   linux_target_install_ops (t);
   4401  1.1  christos 
   4402  1.3  christos   return t;
   4403  1.1  christos }
   4404  1.3  christos 
   4405  1.1  christos /* target_is_async_p implementation.  */
   4406  1.1  christos 
   4407  1.1  christos static int
   4408  1.1  christos linux_nat_is_async_p (struct target_ops *ops)
   4409  1.1  christos {
   4410  1.3  christos   return linux_is_async_p ();
   4411  1.1  christos }
   4412  1.1  christos 
   4413  1.1  christos /* target_can_async_p implementation.  */
   4414  1.1  christos 
   4415  1.1  christos static int
   4416  1.1  christos linux_nat_can_async_p (struct target_ops *ops)
   4417  1.1  christos {
   4418  1.1  christos   /* NOTE: palves 2008-03-21: We're only async when the user requests
   4419  1.3  christos      it explicitly with the "set target-async" command.
   4420  1.1  christos      Someday, linux will always be async.  */
   4421  1.1  christos   return target_async_permitted;
   4422  1.1  christos }
   4423  1.1  christos 
   4424  1.6  christos static int
   4425  1.6  christos linux_nat_supports_non_stop (struct target_ops *self)
   4426  1.6  christos {
   4427  1.6  christos   return 1;
   4428  1.6  christos }
   4429  1.6  christos 
   4430  1.6  christos /* to_always_non_stop_p implementation.  */
   4431  1.6  christos 
   4432  1.1  christos static int
   4433  1.1  christos linux_nat_always_non_stop_p (struct target_ops *self)
   4434  1.1  christos {
   4435  1.1  christos   return 1;
   4436  1.1  christos }
   4437  1.1  christos 
   4438  1.3  christos /* True if we want to support multi-process.  To be removed when GDB
   4439  1.1  christos    supports multi-exec.  */
   4440  1.1  christos 
   4441  1.1  christos int linux_multi_process = 1;
   4442  1.1  christos 
   4443  1.1  christos static int
   4444  1.3  christos linux_nat_supports_multi_process (struct target_ops *self)
   4445  1.1  christos {
   4446  1.1  christos   return linux_multi_process;
   4447  1.1  christos }
   4448  1.1  christos 
   4449  1.1  christos static int
   4450  1.1  christos linux_nat_supports_disable_randomization (struct target_ops *self)
   4451  1.1  christos {
   4452  1.1  christos #ifdef HAVE_PERSONALITY
   4453  1.1  christos   return 1;
   4454  1.1  christos #else
   4455  1.3  christos   return 0;
   4456  1.3  christos #endif
   4457  1.3  christos }
   4458  1.1  christos 
   4459  1.1  christos static int async_terminal_is_ours = 1;
   4460  1.3  christos 
   4461  1.1  christos /* target_terminal_inferior implementation.
   4462  1.3  christos 
   4463  1.1  christos    This is a wrapper around child_terminal_inferior to add async support.  */
   4464  1.1  christos 
   4465  1.1  christos static void
   4466  1.1  christos linux_nat_terminal_inferior (struct target_ops *self)
   4467  1.1  christos {
   4468  1.1  christos   child_terminal_inferior (self);
   4469  1.1  christos 
   4470  1.1  christos   /* Calls to target_terminal_*() are meant to be idempotent.  */
   4471  1.1  christos   if (!async_terminal_is_ours)
   4472  1.3  christos     return;
   4473  1.3  christos 
   4474  1.3  christos   async_terminal_is_ours = 0;
   4475  1.3  christos   set_sigint_trap ();
   4476  1.3  christos }
   4477  1.3  christos 
   4478  1.3  christos /* target_terminal_ours implementation.
   4479  1.3  christos 
   4480  1.1  christos    This is a wrapper around child_terminal_ours to add async support (and
   4481  1.1  christos    implement the target_terminal_ours vs target_terminal_ours_for_output
   4482  1.3  christos    distinction).  child_terminal_ours is currently no different than
   4483  1.1  christos    child_terminal_ours_for_output.
   4484  1.1  christos    We leave target_terminal_ours_for_output alone, leaving it to
   4485  1.1  christos    child_terminal_ours_for_output.  */
   4486  1.1  christos 
   4487  1.3  christos static void
   4488  1.1  christos linux_nat_terminal_ours (struct target_ops *self)
   4489  1.1  christos {
   4490  1.1  christos   /* GDB should never give the terminal to the inferior if the
   4491  1.1  christos      inferior is running in the background (run&, continue&, etc.),
   4492  1.1  christos      but claiming it sure should.  */
   4493  1.1  christos   child_terminal_ours (self);
   4494  1.1  christos 
   4495  1.1  christos   if (async_terminal_is_ours)
   4496  1.1  christos     return;
   4497  1.1  christos 
   4498  1.1  christos   clear_sigint_trap ();
   4499  1.1  christos   async_terminal_is_ours = 1;
   4500  1.1  christos }
   4501  1.1  christos 
   4502  1.1  christos /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
   4503  1.1  christos    so we notice when any child changes state, and notify the
   4504  1.1  christos    event-loop; it allows us to use sigsuspend in linux_nat_wait_1
   4505  1.1  christos    above to wait for the arrival of a SIGCHLD.  */
   4506  1.1  christos 
   4507  1.1  christos static void
   4508  1.1  christos sigchld_handler (int signo)
   4509  1.1  christos {
   4510  1.1  christos   int old_errno = errno;
   4511  1.1  christos 
   4512  1.1  christos   if (debug_linux_nat)
   4513  1.1  christos     ui_file_write_async_safe (gdb_stdlog,
   4514  1.1  christos 			      "sigchld\n", sizeof ("sigchld\n") - 1);
   4515  1.1  christos 
   4516  1.1  christos   if (signo == SIGCHLD
   4517  1.1  christos       && linux_nat_event_pipe[0] != -1)
   4518  1.1  christos     async_file_mark (); /* Let the event loop know that there are
   4519  1.1  christos 			   events to handle.  */
   4520  1.1  christos 
   4521  1.1  christos   errno = old_errno;
   4522  1.1  christos }
   4523  1.5  christos 
   4524  1.1  christos /* Callback registered with the target events file descriptor.  */
   4525  1.1  christos 
   4526  1.1  christos static void
   4527  1.1  christos handle_target_event (int error, gdb_client_data client_data)
   4528  1.1  christos {
   4529  1.1  christos   inferior_event_handler (INF_REG_EVENT, NULL);
   4530  1.1  christos }
   4531  1.3  christos 
   4532  1.1  christos /* Create/destroy the target events pipe.  Returns previous state.  */
   4533  1.1  christos 
   4534  1.1  christos static int
   4535  1.1  christos linux_async_pipe (int enable)
   4536  1.1  christos {
   4537  1.1  christos   int previous = linux_is_async_p ();
   4538  1.1  christos 
   4539  1.1  christos   if (previous != enable)
   4540  1.1  christos     {
   4541  1.1  christos       sigset_t prev_mask;
   4542  1.1  christos 
   4543  1.1  christos       /* Block child signals while we create/destroy the pipe, as
   4544  1.1  christos 	 their handler writes to it.  */
   4545  1.1  christos       block_child_signals (&prev_mask);
   4546  1.1  christos 
   4547  1.1  christos       if (enable)
   4548  1.1  christos 	{
   4549  1.1  christos 	  if (gdb_pipe_cloexec (linux_nat_event_pipe) == -1)
   4550  1.1  christos 	    internal_error (__FILE__, __LINE__,
   4551  1.1  christos 			    "creating event pipe failed.");
   4552  1.1  christos 
   4553  1.1  christos 	  fcntl (linux_nat_event_pipe[0], F_SETFL, O_NONBLOCK);
   4554  1.1  christos 	  fcntl (linux_nat_event_pipe[1], F_SETFL, O_NONBLOCK);
   4555  1.1  christos 	}
   4556  1.1  christos       else
   4557  1.1  christos 	{
   4558  1.1  christos 	  close (linux_nat_event_pipe[0]);
   4559  1.1  christos 	  close (linux_nat_event_pipe[1]);
   4560  1.1  christos 	  linux_nat_event_pipe[0] = -1;
   4561  1.1  christos 	  linux_nat_event_pipe[1] = -1;
   4562  1.1  christos 	}
   4563  1.1  christos 
   4564  1.1  christos       restore_child_signals_mask (&prev_mask);
   4565  1.1  christos     }
   4566  1.1  christos 
   4567  1.5  christos   return previous;
   4568  1.1  christos }
   4569  1.5  christos 
   4570  1.1  christos /* target_async implementation.  */
   4571  1.1  christos 
   4572  1.1  christos static void
   4573  1.1  christos linux_nat_async (struct target_ops *ops, int enable)
   4574  1.1  christos {
   4575  1.1  christos   if (enable)
   4576  1.1  christos     {
   4577  1.1  christos       if (!linux_async_pipe (1))
   4578  1.1  christos 	{
   4579  1.1  christos 	  add_file_handler (linux_nat_event_pipe[0],
   4580  1.1  christos 			    handle_target_event, NULL);
   4581  1.1  christos 	  /* There may be pending events to handle.  Tell the event loop
   4582  1.1  christos 	     to poll them.  */
   4583  1.1  christos 	  async_file_mark ();
   4584  1.1  christos 	}
   4585  1.1  christos     }
   4586  1.1  christos   else
   4587  1.1  christos     {
   4588  1.1  christos       delete_file_handler (linux_nat_event_pipe[0]);
   4589  1.1  christos       linux_async_pipe (0);
   4590  1.1  christos     }
   4591  1.1  christos   return;
   4592  1.1  christos }
   4593  1.1  christos 
   4594  1.1  christos /* Stop an LWP, and push a GDB_SIGNAL_0 stop status if no other
   4595  1.1  christos    event came out.  */
   4596  1.1  christos 
   4597  1.1  christos static int
   4598  1.1  christos linux_nat_stop_lwp (struct lwp_info *lwp, void *data)
   4599  1.1  christos {
   4600  1.1  christos   if (!lwp->stopped)
   4601  1.1  christos     {
   4602  1.1  christos       if (debug_linux_nat)
   4603  1.1  christos 	fprintf_unfiltered (gdb_stdlog,
   4604  1.1  christos 			    "LNSL: running -> suspending %s\n",
   4605  1.1  christos 			    target_pid_to_str (lwp->ptid));
   4606  1.1  christos 
   4607  1.1  christos 
   4608  1.1  christos       if (lwp->last_resume_kind == resume_stop)
   4609  1.1  christos 	{
   4610  1.1  christos 	  if (debug_linux_nat)
   4611  1.1  christos 	    fprintf_unfiltered (gdb_stdlog,
   4612  1.1  christos 				"linux-nat: already stopping LWP %ld at "
   4613  1.1  christos 				"GDB's request\n",
   4614  1.1  christos 				ptid_get_lwp (lwp->ptid));
   4615  1.1  christos 	  return 0;
   4616  1.1  christos 	}
   4617  1.1  christos 
   4618  1.1  christos       stop_callback (lwp, NULL);
   4619  1.1  christos       lwp->last_resume_kind = resume_stop;
   4620  1.1  christos     }
   4621  1.1  christos   else
   4622  1.1  christos     {
   4623  1.1  christos       /* Already known to be stopped; do nothing.  */
   4624  1.1  christos 
   4625  1.1  christos       if (debug_linux_nat)
   4626  1.1  christos 	{
   4627  1.1  christos 	  if (find_thread_ptid (lwp->ptid)->stop_requested)
   4628  1.1  christos 	    fprintf_unfiltered (gdb_stdlog,
   4629  1.1  christos 				"LNSL: already stopped/stop_requested %s\n",
   4630  1.1  christos 				target_pid_to_str (lwp->ptid));
   4631  1.1  christos 	  else
   4632  1.1  christos 	    fprintf_unfiltered (gdb_stdlog,
   4633  1.1  christos 				"LNSL: already stopped/no "
   4634  1.1  christos 				"stop_requested yet %s\n",
   4635  1.1  christos 				target_pid_to_str (lwp->ptid));
   4636  1.3  christos 	}
   4637  1.1  christos     }
   4638  1.6  christos   return 0;
   4639  1.1  christos }
   4640  1.1  christos 
   4641  1.1  christos static void
   4642  1.3  christos linux_nat_stop (struct target_ops *self, ptid_t ptid)
   4643  1.1  christos {
   4644  1.1  christos   iterate_over_lwps (ptid, linux_nat_stop_lwp, NULL);
   4645  1.3  christos }
   4646  1.5  christos 
   4647  1.1  christos static void
   4648  1.1  christos linux_nat_close (struct target_ops *self)
   4649  1.3  christos {
   4650  1.3  christos   /* Unregister from the event loop.  */
   4651  1.3  christos   if (linux_nat_is_async_p (self))
   4652  1.1  christos     linux_nat_async (self, 0);
   4653  1.1  christos 
   4654  1.1  christos   if (linux_ops->to_close)
   4655  1.1  christos     linux_ops->to_close (linux_ops);
   4656  1.1  christos 
   4657  1.1  christos   super_close (self);
   4658  1.1  christos }
   4659  1.1  christos 
   4660  1.1  christos /* When requests are passed down from the linux-nat layer to the
   4661  1.1  christos    single threaded inf-ptrace layer, ptids of (lwpid,0,0) form are
   4662  1.1  christos    used.  The address space pointer is stored in the inferior object,
   4663  1.1  christos    but the common code that is passed such ptid can't tell whether
   4664  1.1  christos    lwpid is a "main" process id or not (it assumes so).  We reverse
   4665  1.1  christos    look up the "main" process id from the lwp here.  */
   4666  1.1  christos 
   4667  1.1  christos static struct address_space *
   4668  1.1  christos linux_nat_thread_address_space (struct target_ops *t, ptid_t ptid)
   4669  1.1  christos {
   4670  1.1  christos   struct lwp_info *lwp;
   4671  1.1  christos   struct inferior *inf;
   4672  1.1  christos   int pid;
   4673  1.1  christos 
   4674  1.1  christos   if (ptid_get_lwp (ptid) == 0)
   4675  1.1  christos     {
   4676  1.1  christos       /* An (lwpid,0,0) ptid.  Look up the lwp object to get at the
   4677  1.1  christos 	 tgid.  */
   4678  1.1  christos       lwp = find_lwp_pid (ptid);
   4679  1.1  christos       pid = ptid_get_pid (lwp->ptid);
   4680  1.1  christos     }
   4681  1.1  christos   else
   4682  1.1  christos     {
   4683  1.1  christos       /* A (pid,lwpid,0) ptid.  */
   4684  1.1  christos       pid = ptid_get_pid (ptid);
   4685  1.1  christos     }
   4686  1.1  christos 
   4687  1.1  christos   inf = find_inferior_pid (pid);
   4688  1.1  christos   gdb_assert (inf != NULL);
   4689  1.1  christos   return inf->aspace;
   4690  1.1  christos }
   4691  1.1  christos 
   4692  1.1  christos /* Return the cached value of the processor core for thread PTID.  */
   4693  1.1  christos 
   4694  1.1  christos static int
   4695  1.1  christos linux_nat_core_of_thread (struct target_ops *ops, ptid_t ptid)
   4696  1.1  christos {
   4697  1.1  christos   struct lwp_info *info = find_lwp_pid (ptid);
   4698  1.5  christos 
   4699  1.5  christos   if (info)
   4700  1.5  christos     return info->core;
   4701  1.5  christos   return -1;
   4702  1.5  christos }
   4703  1.5  christos 
   4704  1.5  christos /* Implementation of to_filesystem_is_local.  */
   4705  1.5  christos 
   4706  1.5  christos static int
   4707  1.5  christos linux_nat_filesystem_is_local (struct target_ops *ops)
   4708  1.5  christos {
   4709  1.5  christos   struct inferior *inf = current_inferior ();
   4710  1.5  christos 
   4711  1.5  christos   if (inf->fake_pid_p || inf->pid == 0)
   4712  1.5  christos     return 1;
   4713  1.5  christos 
   4714  1.5  christos   return linux_ns_same (inf->pid, LINUX_NS_MNT);
   4715  1.5  christos }
   4716  1.5  christos 
   4717  1.5  christos /* Convert the INF argument passed to a to_fileio_* method
   4718  1.5  christos    to a process ID suitable for passing to its corresponding
   4719  1.5  christos    linux_mntns_* function.  If INF is non-NULL then the
   4720  1.5  christos    caller is requesting the filesystem seen by INF.  If INF
   4721  1.5  christos    is NULL then the caller is requesting the filesystem seen
   4722  1.5  christos    by the GDB.  We fall back to GDB's filesystem in the case
   4723  1.5  christos    that INF is non-NULL but its PID is unknown.  */
   4724  1.5  christos 
   4725  1.5  christos static pid_t
   4726  1.5  christos linux_nat_fileio_pid_of (struct inferior *inf)
   4727  1.5  christos {
   4728  1.5  christos   if (inf == NULL || inf->fake_pid_p || inf->pid == 0)
   4729  1.5  christos     return getpid ();
   4730  1.5  christos   else
   4731  1.5  christos     return inf->pid;
   4732  1.5  christos }
   4733  1.5  christos 
   4734  1.5  christos /* Implementation of to_fileio_open.  */
   4735  1.5  christos 
   4736  1.5  christos static int
   4737  1.5  christos linux_nat_fileio_open (struct target_ops *self,
   4738  1.5  christos 		       struct inferior *inf, const char *filename,
   4739  1.5  christos 		       int flags, int mode, int warn_if_slow,
   4740  1.5  christos 		       int *target_errno)
   4741  1.5  christos {
   4742  1.5  christos   int nat_flags;
   4743  1.5  christos   mode_t nat_mode;
   4744  1.5  christos   int fd;
   4745  1.5  christos 
   4746  1.5  christos   if (fileio_to_host_openflags (flags, &nat_flags) == -1
   4747  1.5  christos       || fileio_to_host_mode (mode, &nat_mode) == -1)
   4748  1.5  christos     {
   4749  1.5  christos       *target_errno = FILEIO_EINVAL;
   4750  1.5  christos       return -1;
   4751  1.5  christos     }
   4752  1.5  christos 
   4753  1.5  christos   fd = linux_mntns_open_cloexec (linux_nat_fileio_pid_of (inf),
   4754  1.5  christos 				 filename, nat_flags, nat_mode);
   4755  1.5  christos   if (fd == -1)
   4756  1.5  christos     *target_errno = host_to_fileio_error (errno);
   4757  1.5  christos 
   4758  1.5  christos   return fd;
   4759  1.5  christos }
   4760  1.5  christos 
   4761  1.5  christos /* Implementation of to_fileio_readlink.  */
   4762  1.5  christos 
   4763  1.5  christos static char *
   4764  1.5  christos linux_nat_fileio_readlink (struct target_ops *self,
   4765  1.5  christos 			   struct inferior *inf, const char *filename,
   4766  1.5  christos 			   int *target_errno)
   4767  1.5  christos {
   4768  1.5  christos   char buf[PATH_MAX];
   4769  1.5  christos   int len;
   4770  1.5  christos   char *ret;
   4771  1.5  christos 
   4772  1.5  christos   len = linux_mntns_readlink (linux_nat_fileio_pid_of (inf),
   4773  1.5  christos 			      filename, buf, sizeof (buf));
   4774  1.6  christos   if (len < 0)
   4775  1.5  christos     {
   4776  1.5  christos       *target_errno = host_to_fileio_error (errno);
   4777  1.5  christos       return NULL;
   4778  1.5  christos     }
   4779  1.5  christos 
   4780  1.5  christos   ret = (char *) xmalloc (len + 1);
   4781  1.5  christos   memcpy (ret, buf, len);
   4782  1.5  christos   ret[len] = '\0';
   4783  1.5  christos   return ret;
   4784  1.5  christos }
   4785  1.5  christos 
   4786  1.5  christos /* Implementation of to_fileio_unlink.  */
   4787  1.5  christos 
   4788  1.5  christos static int
   4789  1.5  christos linux_nat_fileio_unlink (struct target_ops *self,
   4790  1.5  christos 			 struct inferior *inf, const char *filename,
   4791  1.5  christos 			 int *target_errno)
   4792  1.5  christos {
   4793  1.5  christos   int ret;
   4794  1.5  christos 
   4795  1.5  christos   ret = linux_mntns_unlink (linux_nat_fileio_pid_of (inf),
   4796  1.5  christos 			    filename);
   4797  1.6  christos   if (ret == -1)
   4798  1.6  christos     *target_errno = host_to_fileio_error (errno);
   4799  1.6  christos 
   4800  1.6  christos   return ret;
   4801  1.6  christos }
   4802  1.6  christos 
   4803  1.6  christos /* Implementation of the to_thread_events method.  */
   4804  1.6  christos 
   4805  1.1  christos static void
   4806  1.1  christos linux_nat_thread_events (struct target_ops *ops, int enable)
   4807  1.1  christos {
   4808  1.1  christos   report_thread_events = enable;
   4809  1.1  christos }
   4810  1.1  christos 
   4811  1.1  christos void
   4812  1.1  christos linux_nat_add_target (struct target_ops *t)
   4813  1.1  christos {
   4814  1.1  christos   /* Save the provided single-threaded target.  We save this in a separate
   4815  1.1  christos      variable because another target we've inherited from (e.g. inf-ptrace)
   4816  1.1  christos      may have saved a pointer to T; we want to use it for the final
   4817  1.1  christos      process stratum target.  */
   4818  1.1  christos   linux_ops_saved = *t;
   4819  1.1  christos   linux_ops = &linux_ops_saved;
   4820  1.1  christos 
   4821  1.1  christos   /* Override some methods for multithreading.  */
   4822  1.1  christos   t->to_create_inferior = linux_nat_create_inferior;
   4823  1.1  christos   t->to_attach = linux_nat_attach;
   4824  1.1  christos   t->to_detach = linux_nat_detach;
   4825  1.1  christos   t->to_resume = linux_nat_resume;
   4826  1.5  christos   t->to_wait = linux_nat_wait;
   4827  1.1  christos   t->to_pass_signals = linux_nat_pass_signals;
   4828  1.1  christos   t->to_xfer_partial = linux_nat_xfer_partial;
   4829  1.1  christos   t->to_kill = linux_nat_kill;
   4830  1.1  christos   t->to_mourn_inferior = linux_nat_mourn_inferior;
   4831  1.1  christos   t->to_thread_alive = linux_nat_thread_alive;
   4832  1.1  christos   t->to_update_thread_list = linux_nat_update_thread_list;
   4833  1.5  christos   t->to_pid_to_str = linux_nat_pid_to_str;
   4834  1.5  christos   t->to_thread_name = linux_nat_thread_name;
   4835  1.5  christos   t->to_has_thread_control = tc_schedlock;
   4836  1.5  christos   t->to_thread_address_space = linux_nat_thread_address_space;
   4837  1.6  christos   t->to_stopped_by_watchpoint = linux_nat_stopped_by_watchpoint;
   4838  1.1  christos   t->to_stopped_data_address = linux_nat_stopped_data_address;
   4839  1.1  christos   t->to_stopped_by_sw_breakpoint = linux_nat_stopped_by_sw_breakpoint;
   4840  1.1  christos   t->to_supports_stopped_by_sw_breakpoint = linux_nat_supports_stopped_by_sw_breakpoint;
   4841  1.1  christos   t->to_stopped_by_hw_breakpoint = linux_nat_stopped_by_hw_breakpoint;
   4842  1.6  christos   t->to_supports_stopped_by_hw_breakpoint = linux_nat_supports_stopped_by_hw_breakpoint;
   4843  1.1  christos   t->to_thread_events = linux_nat_thread_events;
   4844  1.1  christos 
   4845  1.1  christos   t->to_can_async_p = linux_nat_can_async_p;
   4846  1.3  christos   t->to_is_async_p = linux_nat_is_async_p;
   4847  1.3  christos   t->to_supports_non_stop = linux_nat_supports_non_stop;
   4848  1.1  christos   t->to_always_non_stop_p = linux_nat_always_non_stop_p;
   4849  1.1  christos   t->to_async = linux_nat_async;
   4850  1.1  christos   t->to_terminal_inferior = linux_nat_terminal_inferior;
   4851  1.1  christos   t->to_terminal_ours = linux_nat_terminal_ours;
   4852  1.1  christos 
   4853  1.1  christos   super_close = t->to_close;
   4854  1.1  christos   t->to_close = linux_nat_close;
   4855  1.1  christos 
   4856  1.1  christos   t->to_stop = linux_nat_stop;
   4857  1.1  christos 
   4858  1.1  christos   t->to_supports_multi_process = linux_nat_supports_multi_process;
   4859  1.5  christos 
   4860  1.5  christos   t->to_supports_disable_randomization
   4861  1.5  christos     = linux_nat_supports_disable_randomization;
   4862  1.5  christos 
   4863  1.5  christos   t->to_core_of_thread = linux_nat_core_of_thread;
   4864  1.1  christos 
   4865  1.1  christos   t->to_filesystem_is_local = linux_nat_filesystem_is_local;
   4866  1.1  christos   t->to_fileio_open = linux_nat_fileio_open;
   4867  1.1  christos   t->to_fileio_readlink = linux_nat_fileio_readlink;
   4868  1.1  christos   t->to_fileio_unlink = linux_nat_fileio_unlink;
   4869  1.1  christos 
   4870  1.1  christos   /* We don't change the stratum; this target will sit at
   4871  1.1  christos      process_stratum and thread_db will set at thread_stratum.  This
   4872  1.1  christos      is a little strange, since this is a multi-threaded-capable
   4873  1.1  christos      target, but we want to be on the stack below thread_db, and we
   4874  1.1  christos      also want to be used for single-threaded processes.  */
   4875  1.1  christos 
   4876  1.1  christos   add_target (t);
   4877  1.1  christos }
   4878  1.1  christos 
   4879  1.1  christos /* Register a method to call whenever a new thread is attached.  */
   4880  1.1  christos void
   4881  1.1  christos linux_nat_set_new_thread (struct target_ops *t,
   4882  1.1  christos 			  void (*new_thread) (struct lwp_info *))
   4883  1.1  christos {
   4884  1.1  christos   /* Save the pointer.  We only support a single registered instance
   4885  1.1  christos      of the GNU/Linux native target, so we do not need to map this to
   4886  1.1  christos      T.  */
   4887  1.1  christos   linux_nat_new_thread = new_thread;
   4888  1.1  christos }
   4889  1.1  christos 
   4890  1.1  christos /* See declaration in linux-nat.h.  */
   4891  1.1  christos 
   4892  1.1  christos void
   4893  1.1  christos linux_nat_set_new_fork (struct target_ops *t,
   4894  1.1  christos 			linux_nat_new_fork_ftype *new_fork)
   4895  1.1  christos {
   4896  1.1  christos   /* Save the pointer.  */
   4897  1.1  christos   linux_nat_new_fork = new_fork;
   4898  1.1  christos }
   4899  1.1  christos 
   4900  1.1  christos /* See declaration in linux-nat.h.  */
   4901  1.1  christos 
   4902  1.1  christos void
   4903  1.1  christos linux_nat_set_forget_process (struct target_ops *t,
   4904  1.1  christos 			      linux_nat_forget_process_ftype *fn)
   4905  1.1  christos {
   4906  1.1  christos   /* Save the pointer.  */
   4907  1.1  christos   linux_nat_forget_process_hook = fn;
   4908  1.1  christos }
   4909  1.1  christos 
   4910  1.1  christos /* See declaration in linux-nat.h.  */
   4911  1.1  christos 
   4912  1.1  christos void
   4913  1.1  christos linux_nat_forget_process (pid_t pid)
   4914  1.1  christos {
   4915  1.1  christos   if (linux_nat_forget_process_hook != NULL)
   4916  1.1  christos     linux_nat_forget_process_hook (pid);
   4917  1.1  christos }
   4918  1.1  christos 
   4919  1.1  christos /* Register a method that converts a siginfo object between the layout
   4920  1.1  christos    that ptrace returns, and the layout in the architecture of the
   4921  1.1  christos    inferior.  */
   4922  1.1  christos void
   4923  1.1  christos linux_nat_set_siginfo_fixup (struct target_ops *t,
   4924  1.1  christos 			     int (*siginfo_fixup) (siginfo_t *,
   4925  1.1  christos 						   gdb_byte *,
   4926  1.1  christos 						   int))
   4927  1.1  christos {
   4928  1.1  christos   /* Save the pointer.  */
   4929  1.1  christos   linux_nat_siginfo_fixup = siginfo_fixup;
   4930  1.1  christos }
   4931  1.1  christos 
   4932  1.1  christos /* Register a method to call prior to resuming a thread.  */
   4933  1.1  christos 
   4934  1.1  christos void
   4935  1.1  christos linux_nat_set_prepare_to_resume (struct target_ops *t,
   4936  1.1  christos 				 void (*prepare_to_resume) (struct lwp_info *))
   4937  1.1  christos {
   4938  1.1  christos   /* Save the pointer.  */
   4939  1.1  christos   linux_nat_prepare_to_resume = prepare_to_resume;
   4940  1.1  christos }
   4941  1.1  christos 
   4942  1.1  christos /* See linux-nat.h.  */
   4943  1.1  christos 
   4944  1.1  christos int
   4945  1.1  christos linux_nat_get_siginfo (ptid_t ptid, siginfo_t *siginfo)
   4946  1.1  christos {
   4947  1.1  christos   int pid;
   4948  1.1  christos 
   4949  1.1  christos   pid = ptid_get_lwp (ptid);
   4950  1.1  christos   if (pid == 0)
   4951  1.1  christos     pid = ptid_get_pid (ptid);
   4952  1.1  christos 
   4953  1.1  christos   errno = 0;
   4954  1.1  christos   ptrace (PTRACE_GETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, siginfo);
   4955  1.1  christos   if (errno != 0)
   4956  1.1  christos     {
   4957  1.5  christos       memset (siginfo, 0, sizeof (*siginfo));
   4958  1.5  christos       return 0;
   4959  1.5  christos     }
   4960  1.5  christos   return 1;
   4961  1.5  christos }
   4962  1.5  christos 
   4963  1.5  christos /* See nat/linux-nat.h.  */
   4964  1.5  christos 
   4965  1.5  christos ptid_t
   4966  1.1  christos current_lwp_ptid (void)
   4967  1.1  christos {
   4968  1.1  christos   gdb_assert (ptid_lwp_p (inferior_ptid));
   4969  1.1  christos   return inferior_ptid;
   4970  1.1  christos }
   4971  1.1  christos 
   4972  1.1  christos /* Provide a prototype to silence -Wmissing-prototypes.  */
   4973  1.1  christos extern initialize_file_ftype _initialize_linux_nat;
   4974  1.1  christos 
   4975  1.1  christos void
   4976  1.1  christos _initialize_linux_nat (void)
   4977  1.1  christos {
   4978  1.1  christos   add_setshow_zuinteger_cmd ("lin-lwp", class_maintenance,
   4979  1.1  christos 			     &debug_linux_nat, _("\
   4980  1.1  christos Set debugging of GNU/Linux lwp module."), _("\
   4981  1.5  christos Show debugging of GNU/Linux lwp module."), _("\
   4982  1.5  christos Enables printf debugging output."),
   4983  1.5  christos 			     NULL,
   4984  1.5  christos 			     show_debug_linux_nat,
   4985  1.5  christos 			     &setdebuglist, &showdebuglist);
   4986  1.5  christos 
   4987  1.5  christos   add_setshow_boolean_cmd ("linux-namespaces", class_maintenance,
   4988  1.5  christos 			   &debug_linux_namespaces, _("\
   4989  1.5  christos Set debugging of GNU/Linux namespaces module."), _("\
   4990  1.1  christos Show debugging of GNU/Linux namespaces module."), _("\
   4991  1.1  christos Enables printf debugging output."),
   4992  1.1  christos 			   NULL,
   4993  1.1  christos 			   NULL,
   4994  1.1  christos 			   &setdebuglist, &showdebuglist);
   4995  1.1  christos 
   4996  1.1  christos   /* Save this mask as the default.  */
   4997  1.1  christos   sigprocmask (SIG_SETMASK, NULL, &normal_mask);
   4998  1.1  christos 
   4999  1.1  christos   /* Install a SIGCHLD handler.  */
   5000  1.1  christos   sigchld_action.sa_handler = sigchld_handler;
   5001  1.1  christos   sigemptyset (&sigchld_action.sa_mask);
   5002  1.1  christos   sigchld_action.sa_flags = SA_RESTART;
   5003  1.1  christos 
   5004  1.1  christos   /* Make it the default.  */
   5005  1.1  christos   sigaction (SIGCHLD, &sigchld_action, NULL);
   5006  1.6  christos 
   5007  1.6  christos   /* Make sure we don't block SIGCHLD during a sigsuspend.  */
   5008  1.1  christos   sigprocmask (SIG_SETMASK, NULL, &suspend_mask);
   5009  1.1  christos   sigdelset (&suspend_mask, SIGCHLD);
   5010  1.1  christos 
   5011  1.1  christos   sigemptyset (&blocked_mask);
   5012  1.1  christos 
   5013  1.1  christos   lwp_lwpid_htab_create ();
   5014  1.1  christos }
   5015  1.1  christos 
   5016  1.1  christos 
   5018  1.1  christos /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
   5019  1.1  christos    the GNU/Linux Threads library and therefore doesn't really belong
   5020  1.1  christos    here.  */
   5021  1.1  christos 
   5022  1.6  christos /* Return the set of signals used by the threads library in *SET.  */
   5023  1.6  christos 
   5024  1.6  christos void
   5025  1.6  christos lin_thread_get_thread_signals (sigset_t *set)
   5026  1.6  christos {
   5027  1.1  christos   sigemptyset (set);
   5028                
   5029                  /* NPTL reserves the first two RT signals, but does not provide any
   5030                     way for the debugger to query the signal numbers - fortunately
   5031                     they don't change.  */
   5032                  sigaddset (set, __SIGRTMIN);
   5033                  sigaddset (set, __SIGRTMIN + 1);
   5034                }
   5035