198944Sobrien/* Multi-threaded debugging support for GNU/Linux (LWP layer).
2130803Smarcel   Copyright 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
398944Sobrien
498944Sobrien   This file is part of GDB.
598944Sobrien
698944Sobrien   This program is free software; you can redistribute it and/or modify
798944Sobrien   it under the terms of the GNU General Public License as published by
898944Sobrien   the Free Software Foundation; either version 2 of the License, or
998944Sobrien   (at your option) any later version.
1098944Sobrien
1198944Sobrien   This program is distributed in the hope that it will be useful,
1298944Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1398944Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1498944Sobrien   GNU General Public License for more details.
1598944Sobrien
1698944Sobrien   You should have received a copy of the GNU General Public License
1798944Sobrien   along with this program; if not, write to the Free Software
1898944Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
1998944Sobrien   Boston, MA 02111-1307, USA.  */
2098944Sobrien
2198944Sobrien#include "defs.h"
2298944Sobrien
2398944Sobrien#include "gdb_assert.h"
24130803Smarcel#include "gdb_string.h"
2598944Sobrien#include <errno.h>
2698944Sobrien#include <signal.h>
27130803Smarcel#ifdef HAVE_TKILL_SYSCALL
28130803Smarcel#include <unistd.h>
29130803Smarcel#include <sys/syscall.h>
30130803Smarcel#endif
3198944Sobrien#include <sys/ptrace.h>
3298944Sobrien#include "gdb_wait.h"
3398944Sobrien
3498944Sobrien#include "gdbthread.h"
3598944Sobrien#include "inferior.h"
3698944Sobrien#include "target.h"
3798944Sobrien#include "regcache.h"
3898944Sobrien#include "gdbcmd.h"
3998944Sobrien
4098944Sobrienstatic int debug_lin_lwp;
41130803Smarcelextern char *strsignal (int sig);
4298944Sobrien
43130803Smarcel#include "linux-nat.h"
44130803Smarcel
4598944Sobrien/* On GNU/Linux there are no real LWP's.  The closest thing to LWP's
4698944Sobrien   are processes sharing the same VM space.  A multi-threaded process
4798944Sobrien   is basically a group of such processes.  However, such a grouping
4898944Sobrien   is almost entirely a user-space issue; the kernel doesn't enforce
4998944Sobrien   such a grouping at all (this might change in the future).  In
5098944Sobrien   general, we'll rely on the threads library (i.e. the GNU/Linux
5198944Sobrien   Threads library) to provide such a grouping.
5298944Sobrien
5398944Sobrien   It is perfectly well possible to write a multi-threaded application
5498944Sobrien   without the assistance of a threads library, by using the clone
5598944Sobrien   system call directly.  This module should be able to give some
5698944Sobrien   rudimentary support for debugging such applications if developers
5798944Sobrien   specify the CLONE_PTRACE flag in the clone system call, and are
5898944Sobrien   using the Linux kernel 2.4 or above.
5998944Sobrien
6098944Sobrien   Note that there are some peculiarities in GNU/Linux that affect
6198944Sobrien   this code:
6298944Sobrien
6398944Sobrien   - In general one should specify the __WCLONE flag to waitpid in
6498944Sobrien     order to make it report events for any of the cloned processes
6598944Sobrien     (and leave it out for the initial process).  However, if a cloned
6698944Sobrien     process has exited the exit status is only reported if the
6798944Sobrien     __WCLONE flag is absent.  Linux kernel 2.4 has a __WALL flag, but
6898944Sobrien     we cannot use it since GDB must work on older systems too.
6998944Sobrien
7098944Sobrien   - When a traced, cloned process exits and is waited for by the
7198944Sobrien     debugger, the kernel reassigns it to the original parent and
7298944Sobrien     keeps it around as a "zombie".  Somehow, the GNU/Linux Threads
7398944Sobrien     library doesn't notice this, which leads to the "zombie problem":
7498944Sobrien     When debugged a multi-threaded process that spawns a lot of
7598944Sobrien     threads will run out of processes, even if the threads exit,
7698944Sobrien     because the "zombies" stay around.  */
7798944Sobrien
7898944Sobrien/* List of known LWPs.  */
7998944Sobrienstatic struct lwp_info *lwp_list;
8098944Sobrien
8198944Sobrien/* Number of LWPs in the list.  */
8298944Sobrienstatic int num_lwps;
8398944Sobrien
8498944Sobrien/* Non-zero if we're running in "threaded" mode.  */
8598944Sobrienstatic int threaded;
8698944Sobrien
8798944Sobrien
8898944Sobrien#define GET_LWP(ptid)		ptid_get_lwp (ptid)
8998944Sobrien#define GET_PID(ptid)		ptid_get_pid (ptid)
9098944Sobrien#define is_lwp(ptid)		(GET_LWP (ptid) != 0)
9198944Sobrien#define BUILD_LWP(lwp, pid)	ptid_build (pid, lwp, 0)
9298944Sobrien
9398944Sobrien/* If the last reported event was a SIGTRAP, this variable is set to
9498944Sobrien   the process id of the LWP/thread that got it.  */
9598944Sobrienptid_t trap_ptid;
9698944Sobrien
9798944Sobrien
9898944Sobrien/* This module's target-specific operations.  */
9998944Sobrienstatic struct target_ops lin_lwp_ops;
10098944Sobrien
10198944Sobrien/* The standard child operations.  */
10298944Sobrienextern struct target_ops child_ops;
10398944Sobrien
10498944Sobrien/* Since we cannot wait (in lin_lwp_wait) for the initial process and
10598944Sobrien   any cloned processes with a single call to waitpid, we have to use
10698944Sobrien   the WNOHANG flag and call waitpid in a loop.  To optimize
10798944Sobrien   things a bit we use `sigsuspend' to wake us up when a process has
10898944Sobrien   something to report (it will send us a SIGCHLD if it has).  To make
10998944Sobrien   this work we have to juggle with the signal mask.  We save the
11098944Sobrien   original signal mask such that we can restore it before creating a
11198944Sobrien   new process in order to avoid blocking certain signals in the
11298944Sobrien   inferior.  We then block SIGCHLD during the waitpid/sigsuspend
11398944Sobrien   loop.  */
11498944Sobrien
11598944Sobrien/* Original signal mask.  */
11698944Sobrienstatic sigset_t normal_mask;
11798944Sobrien
11898944Sobrien/* Signal mask for use with sigsuspend in lin_lwp_wait, initialized in
11998944Sobrien   _initialize_lin_lwp.  */
12098944Sobrienstatic sigset_t suspend_mask;
12198944Sobrien
12298944Sobrien/* Signals to block to make that sigsuspend work.  */
12398944Sobrienstatic sigset_t blocked_mask;
12498944Sobrien
12598944Sobrien
12698944Sobrien/* Prototypes for local functions.  */
12798944Sobrienstatic int stop_wait_callback (struct lwp_info *lp, void *data);
128130803Smarcelstatic int lin_lwp_thread_alive (ptid_t ptid);
12998944Sobrien
13098944Sobrien/* Convert wait status STATUS to a string.  Used for printing debug
13198944Sobrien   messages only.  */
13298944Sobrien
13398944Sobrienstatic char *
13498944Sobrienstatus_to_str (int status)
13598944Sobrien{
13698944Sobrien  static char buf[64];
13798944Sobrien
13898944Sobrien  if (WIFSTOPPED (status))
13998944Sobrien    snprintf (buf, sizeof (buf), "%s (stopped)",
14098944Sobrien	      strsignal (WSTOPSIG (status)));
14198944Sobrien  else if (WIFSIGNALED (status))
14298944Sobrien    snprintf (buf, sizeof (buf), "%s (terminated)",
14398944Sobrien	      strsignal (WSTOPSIG (status)));
14498944Sobrien  else
145130803Smarcel    snprintf (buf, sizeof (buf), "%d (exited)", WEXITSTATUS (status));
14698944Sobrien
14798944Sobrien  return buf;
14898944Sobrien}
14998944Sobrien
15098944Sobrien/* Initialize the list of LWPs.  Note that this module, contrary to
15198944Sobrien   what GDB's generic threads layer does for its thread list,
15298944Sobrien   re-initializes the LWP lists whenever we mourn or detach (which
15398944Sobrien   doesn't involve mourning) the inferior.  */
15498944Sobrien
15598944Sobrienstatic void
15698944Sobrieninit_lwp_list (void)
15798944Sobrien{
15898944Sobrien  struct lwp_info *lp, *lpnext;
15998944Sobrien
16098944Sobrien  for (lp = lwp_list; lp; lp = lpnext)
16198944Sobrien    {
16298944Sobrien      lpnext = lp->next;
16398944Sobrien      xfree (lp);
16498944Sobrien    }
16598944Sobrien
16698944Sobrien  lwp_list = NULL;
16798944Sobrien  num_lwps = 0;
16898944Sobrien  threaded = 0;
16998944Sobrien}
17098944Sobrien
17198944Sobrien/* Add the LWP specified by PID to the list.  If this causes the
17298944Sobrien   number of LWPs to become larger than one, go into "threaded" mode.
17398944Sobrien   Return a pointer to the structure describing the new LWP.  */
17498944Sobrien
17598944Sobrienstatic struct lwp_info *
17698944Sobrienadd_lwp (ptid_t ptid)
17798944Sobrien{
17898944Sobrien  struct lwp_info *lp;
17998944Sobrien
18098944Sobrien  gdb_assert (is_lwp (ptid));
18198944Sobrien
18298944Sobrien  lp = (struct lwp_info *) xmalloc (sizeof (struct lwp_info));
18398944Sobrien
18498944Sobrien  memset (lp, 0, sizeof (struct lwp_info));
18598944Sobrien
18698944Sobrien  lp->ptid = ptid;
18798944Sobrien
18898944Sobrien  lp->next = lwp_list;
18998944Sobrien  lwp_list = lp;
19098944Sobrien  if (++num_lwps > 1)
19198944Sobrien    threaded = 1;
19298944Sobrien
19398944Sobrien  return lp;
19498944Sobrien}
19598944Sobrien
19698944Sobrien/* Remove the LWP specified by PID from the list.  */
19798944Sobrien
19898944Sobrienstatic void
19998944Sobriendelete_lwp (ptid_t ptid)
20098944Sobrien{
20198944Sobrien  struct lwp_info *lp, *lpprev;
20298944Sobrien
20398944Sobrien  lpprev = NULL;
20498944Sobrien
20598944Sobrien  for (lp = lwp_list; lp; lpprev = lp, lp = lp->next)
20698944Sobrien    if (ptid_equal (lp->ptid, ptid))
20798944Sobrien      break;
20898944Sobrien
20998944Sobrien  if (!lp)
21098944Sobrien    return;
21198944Sobrien
21298944Sobrien  /* We don't go back to "non-threaded" mode if the number of threads
21398944Sobrien     becomes less than two.  */
21498944Sobrien  num_lwps--;
21598944Sobrien
21698944Sobrien  if (lpprev)
21798944Sobrien    lpprev->next = lp->next;
21898944Sobrien  else
21998944Sobrien    lwp_list = lp->next;
22098944Sobrien
22198944Sobrien  xfree (lp);
22298944Sobrien}
22398944Sobrien
22498944Sobrien/* Return a pointer to the structure describing the LWP corresponding
22598944Sobrien   to PID.  If no corresponding LWP could be found, return NULL.  */
22698944Sobrien
22798944Sobrienstatic struct lwp_info *
22898944Sobrienfind_lwp_pid (ptid_t ptid)
22998944Sobrien{
23098944Sobrien  struct lwp_info *lp;
23198944Sobrien  int lwp;
23298944Sobrien
23398944Sobrien  if (is_lwp (ptid))
23498944Sobrien    lwp = GET_LWP (ptid);
23598944Sobrien  else
23698944Sobrien    lwp = GET_PID (ptid);
23798944Sobrien
23898944Sobrien  for (lp = lwp_list; lp; lp = lp->next)
23998944Sobrien    if (lwp == GET_LWP (lp->ptid))
24098944Sobrien      return lp;
24198944Sobrien
24298944Sobrien  return NULL;
24398944Sobrien}
24498944Sobrien
24598944Sobrien/* Call CALLBACK with its second argument set to DATA for every LWP in
24698944Sobrien   the list.  If CALLBACK returns 1 for a particular LWP, return a
24798944Sobrien   pointer to the structure describing that LWP immediately.
24898944Sobrien   Otherwise return NULL.  */
24998944Sobrien
25098944Sobrienstruct lwp_info *
25198944Sobrieniterate_over_lwps (int (*callback) (struct lwp_info *, void *), void *data)
25298944Sobrien{
25398944Sobrien  struct lwp_info *lp, *lpnext;
25498944Sobrien
25598944Sobrien  for (lp = lwp_list; lp; lp = lpnext)
25698944Sobrien    {
25798944Sobrien      lpnext = lp->next;
25898944Sobrien      if ((*callback) (lp, data))
25998944Sobrien	return lp;
26098944Sobrien    }
26198944Sobrien
26298944Sobrien  return NULL;
26398944Sobrien}
26498944Sobrien
26598944Sobrien
26698944Sobrien#if 0
26798944Sobrienstatic void
26898944Sobrienlin_lwp_open (char *args, int from_tty)
26998944Sobrien{
27098944Sobrien  push_target (&lin_lwp_ops);
27198944Sobrien}
27298944Sobrien#endif
27398944Sobrien
27498944Sobrien/* Attach to the LWP specified by PID.  If VERBOSE is non-zero, print
27598944Sobrien   a message telling the user that a new LWP has been added to the
27698944Sobrien   process.  */
27798944Sobrien
27898944Sobrienvoid
27998944Sobrienlin_lwp_attach_lwp (ptid_t ptid, int verbose)
28098944Sobrien{
28198944Sobrien  struct lwp_info *lp;
28298944Sobrien
28398944Sobrien  gdb_assert (is_lwp (ptid));
28498944Sobrien
28598944Sobrien  /* Make sure SIGCHLD is blocked.  We don't want SIGCHLD events
28698944Sobrien     to interrupt either the ptrace() or waitpid() calls below.  */
287130803Smarcel  if (!sigismember (&blocked_mask, SIGCHLD))
28898944Sobrien    {
28998944Sobrien      sigaddset (&blocked_mask, SIGCHLD);
29098944Sobrien      sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
29198944Sobrien    }
29298944Sobrien
29398944Sobrien  if (verbose)
29498944Sobrien    printf_filtered ("[New %s]\n", target_pid_to_str (ptid));
29598944Sobrien
29698944Sobrien  lp = find_lwp_pid (ptid);
29798944Sobrien  if (lp == NULL)
29898944Sobrien    lp = add_lwp (ptid);
29998944Sobrien
30098944Sobrien  /* We assume that we're already attached to any LWP that has an
30198944Sobrien     id equal to the overall process id.  */
30298944Sobrien  if (GET_LWP (ptid) != GET_PID (ptid))
30398944Sobrien    {
30498944Sobrien      pid_t pid;
30598944Sobrien      int status;
30698944Sobrien
30798944Sobrien      if (ptrace (PTRACE_ATTACH, GET_LWP (ptid), 0, 0) < 0)
30898944Sobrien	error ("Can't attach %s: %s", target_pid_to_str (ptid),
309130803Smarcel	       safe_strerror (errno));
31098944Sobrien
311130803Smarcel      if (debug_lin_lwp)
312130803Smarcel	fprintf_unfiltered (gdb_stdlog,
313130803Smarcel			    "LLAL: PTRACE_ATTACH %s, 0, 0 (OK)\n",
314130803Smarcel			    target_pid_to_str (ptid));
315130803Smarcel
31698944Sobrien      pid = waitpid (GET_LWP (ptid), &status, 0);
31798944Sobrien      if (pid == -1 && errno == ECHILD)
31898944Sobrien	{
31998944Sobrien	  /* Try again with __WCLONE to check cloned processes.  */
32098944Sobrien	  pid = waitpid (GET_LWP (ptid), &status, __WCLONE);
32198944Sobrien	  lp->cloned = 1;
32298944Sobrien	}
32398944Sobrien
32498944Sobrien      gdb_assert (pid == GET_LWP (ptid)
32598944Sobrien		  && WIFSTOPPED (status) && WSTOPSIG (status));
32698944Sobrien
327130803Smarcel      child_post_attach (pid);
328130803Smarcel
32998944Sobrien      lp->stopped = 1;
330130803Smarcel
331130803Smarcel      if (debug_lin_lwp)
332130803Smarcel	{
333130803Smarcel	  fprintf_unfiltered (gdb_stdlog,
334130803Smarcel			      "LLAL: waitpid %s received %s\n",
335130803Smarcel			      target_pid_to_str (ptid),
336130803Smarcel			      status_to_str (status));
337130803Smarcel	}
33898944Sobrien    }
33998944Sobrien  else
34098944Sobrien    {
34198944Sobrien      /* We assume that the LWP representing the original process
342130803Smarcel         is already stopped.  Mark it as stopped in the data structure
343130803Smarcel         that the lin-lwp layer uses to keep track of threads.  Note
344130803Smarcel         that this won't have already been done since the main thread
345130803Smarcel         will have, we assume, been stopped by an attach from a
346130803Smarcel         different layer.  */
34798944Sobrien      lp->stopped = 1;
34898944Sobrien    }
34998944Sobrien}
35098944Sobrien
35198944Sobrienstatic void
35298944Sobrienlin_lwp_attach (char *args, int from_tty)
35398944Sobrien{
35498944Sobrien  struct lwp_info *lp;
35598944Sobrien  pid_t pid;
35698944Sobrien  int status;
35798944Sobrien
35898944Sobrien  /* FIXME: We should probably accept a list of process id's, and
35998944Sobrien     attach all of them.  */
36098944Sobrien  child_ops.to_attach (args, from_tty);
36198944Sobrien
36298944Sobrien  /* Add the initial process as the first LWP to the list.  */
36398944Sobrien  lp = add_lwp (BUILD_LWP (GET_PID (inferior_ptid), GET_PID (inferior_ptid)));
36498944Sobrien
36598944Sobrien  /* Make sure the initial process is stopped.  The user-level threads
36698944Sobrien     layer might want to poke around in the inferior, and that won't
36798944Sobrien     work if things haven't stabilized yet.  */
36898944Sobrien  pid = waitpid (GET_PID (inferior_ptid), &status, 0);
36998944Sobrien  if (pid == -1 && errno == ECHILD)
37098944Sobrien    {
37198944Sobrien      warning ("%s is a cloned process", target_pid_to_str (inferior_ptid));
37298944Sobrien
37398944Sobrien      /* Try again with __WCLONE to check cloned processes.  */
37498944Sobrien      pid = waitpid (GET_PID (inferior_ptid), &status, __WCLONE);
37598944Sobrien      lp->cloned = 1;
37698944Sobrien    }
37798944Sobrien
37898944Sobrien  gdb_assert (pid == GET_PID (inferior_ptid)
37998944Sobrien	      && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP);
38098944Sobrien
38198944Sobrien  lp->stopped = 1;
38298944Sobrien
38398944Sobrien  /* Fake the SIGSTOP that core GDB expects.  */
38498944Sobrien  lp->status = W_STOPCODE (SIGSTOP);
38598944Sobrien  lp->resumed = 1;
386130803Smarcel  if (debug_lin_lwp)
387130803Smarcel    {
388130803Smarcel      fprintf_unfiltered (gdb_stdlog,
389130803Smarcel			  "LLA: waitpid %ld, faking SIGSTOP\n", (long) pid);
390130803Smarcel    }
39198944Sobrien}
39298944Sobrien
39398944Sobrienstatic int
39498944Sobriendetach_callback (struct lwp_info *lp, void *data)
39598944Sobrien{
39698944Sobrien  gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
39798944Sobrien
39898944Sobrien  if (debug_lin_lwp && lp->status)
399130803Smarcel    fprintf_unfiltered (gdb_stdlog, "DC:  Pending %s for %s on detach.\n",
400130803Smarcel			strsignal (WSTOPSIG (lp->status)),
401130803Smarcel			target_pid_to_str (lp->ptid));
40298944Sobrien
40398944Sobrien  while (lp->signalled && lp->stopped)
40498944Sobrien    {
405130803Smarcel      errno = 0;
40698944Sobrien      if (ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0,
40798944Sobrien		  WSTOPSIG (lp->status)) < 0)
40898944Sobrien	error ("Can't continue %s: %s", target_pid_to_str (lp->ptid),
409130803Smarcel	       safe_strerror (errno));
41098944Sobrien
411130803Smarcel      if (debug_lin_lwp)
412130803Smarcel	fprintf_unfiltered (gdb_stdlog,
413130803Smarcel			    "DC:  PTRACE_CONTINUE (%s, 0, %s) (OK)\n",
414130803Smarcel			    target_pid_to_str (lp->ptid),
415130803Smarcel			    status_to_str (lp->status));
416130803Smarcel
41798944Sobrien      lp->stopped = 0;
41898944Sobrien      lp->signalled = 0;
41998944Sobrien      lp->status = 0;
420130803Smarcel      /* FIXME drow/2003-08-26: There was a call to stop_wait_callback
421130803Smarcel	 here.  But since lp->signalled was cleared above,
422130803Smarcel	 stop_wait_callback didn't do anything; the process was left
423130803Smarcel	 running.  Shouldn't we be waiting for it to stop?
424130803Smarcel	 I've removed the call, since stop_wait_callback now does do
425130803Smarcel	 something when called with lp->signalled == 0.  */
42698944Sobrien
42798944Sobrien      gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
42898944Sobrien    }
42998944Sobrien
43098944Sobrien  /* We don't actually detach from the LWP that has an id equal to the
43198944Sobrien     overall process id just yet.  */
43298944Sobrien  if (GET_LWP (lp->ptid) != GET_PID (lp->ptid))
43398944Sobrien    {
434130803Smarcel      errno = 0;
43598944Sobrien      if (ptrace (PTRACE_DETACH, GET_LWP (lp->ptid), 0,
43698944Sobrien		  WSTOPSIG (lp->status)) < 0)
43798944Sobrien	error ("Can't detach %s: %s", target_pid_to_str (lp->ptid),
438130803Smarcel	       safe_strerror (errno));
43998944Sobrien
440130803Smarcel      if (debug_lin_lwp)
441130803Smarcel	fprintf_unfiltered (gdb_stdlog,
442130803Smarcel			    "PTRACE_DETACH (%s, %s, 0) (OK)\n",
443130803Smarcel			    target_pid_to_str (lp->ptid),
444130803Smarcel			    strsignal (WSTOPSIG (lp->status)));
445130803Smarcel
44698944Sobrien      delete_lwp (lp->ptid);
44798944Sobrien    }
44898944Sobrien
44998944Sobrien  return 0;
45098944Sobrien}
45198944Sobrien
45298944Sobrienstatic void
45398944Sobrienlin_lwp_detach (char *args, int from_tty)
45498944Sobrien{
45598944Sobrien  iterate_over_lwps (detach_callback, NULL);
45698944Sobrien
45798944Sobrien  /* Only the initial process should be left right now.  */
45898944Sobrien  gdb_assert (num_lwps == 1);
45998944Sobrien
46098944Sobrien  trap_ptid = null_ptid;
46198944Sobrien
46298944Sobrien  /* Destroy LWP info; it's no longer valid.  */
46398944Sobrien  init_lwp_list ();
46498944Sobrien
46598944Sobrien  /* Restore the original signal mask.  */
46698944Sobrien  sigprocmask (SIG_SETMASK, &normal_mask, NULL);
46798944Sobrien  sigemptyset (&blocked_mask);
46898944Sobrien
46998944Sobrien  inferior_ptid = pid_to_ptid (GET_PID (inferior_ptid));
47098944Sobrien  child_ops.to_detach (args, from_tty);
47198944Sobrien}
47298944Sobrien
47398944Sobrien
47498944Sobrien/* Resume LP.  */
47598944Sobrien
47698944Sobrienstatic int
47798944Sobrienresume_callback (struct lwp_info *lp, void *data)
47898944Sobrien{
47998944Sobrien  if (lp->stopped && lp->status == 0)
48098944Sobrien    {
48198944Sobrien      struct thread_info *tp;
48298944Sobrien
48398944Sobrien      child_resume (pid_to_ptid (GET_LWP (lp->ptid)), 0, TARGET_SIGNAL_0);
484130803Smarcel      if (debug_lin_lwp)
485130803Smarcel	fprintf_unfiltered (gdb_stdlog,
486130803Smarcel			    "RC:  PTRACE_CONT %s, 0, 0 (resume sibling)\n",
487130803Smarcel			    target_pid_to_str (lp->ptid));
48898944Sobrien      lp->stopped = 0;
48998944Sobrien      lp->step = 0;
49098944Sobrien    }
49198944Sobrien
49298944Sobrien  return 0;
49398944Sobrien}
49498944Sobrien
49598944Sobrienstatic int
49698944Sobrienresume_clear_callback (struct lwp_info *lp, void *data)
49798944Sobrien{
49898944Sobrien  lp->resumed = 0;
49998944Sobrien  return 0;
50098944Sobrien}
50198944Sobrien
50298944Sobrienstatic int
50398944Sobrienresume_set_callback (struct lwp_info *lp, void *data)
50498944Sobrien{
50598944Sobrien  lp->resumed = 1;
50698944Sobrien  return 0;
50798944Sobrien}
50898944Sobrien
50998944Sobrienstatic void
51098944Sobrienlin_lwp_resume (ptid_t ptid, int step, enum target_signal signo)
51198944Sobrien{
51298944Sobrien  struct lwp_info *lp;
51398944Sobrien  int resume_all;
51498944Sobrien
515130803Smarcel  /* A specific PTID means `step only this process id'.  */
516130803Smarcel  resume_all = (PIDGET (ptid) == -1);
51798944Sobrien
51898944Sobrien  if (resume_all)
51998944Sobrien    iterate_over_lwps (resume_set_callback, NULL);
52098944Sobrien  else
52198944Sobrien    iterate_over_lwps (resume_clear_callback, NULL);
52298944Sobrien
52398944Sobrien  /* If PID is -1, it's the current inferior that should be
52498944Sobrien     handled specially.  */
52598944Sobrien  if (PIDGET (ptid) == -1)
52698944Sobrien    ptid = inferior_ptid;
52798944Sobrien
52898944Sobrien  lp = find_lwp_pid (ptid);
52998944Sobrien  if (lp)
53098944Sobrien    {
53198944Sobrien      ptid = pid_to_ptid (GET_LWP (lp->ptid));
53298944Sobrien
53398944Sobrien      /* Remember if we're stepping.  */
53498944Sobrien      lp->step = step;
53598944Sobrien
53698944Sobrien      /* Mark this LWP as resumed.  */
53798944Sobrien      lp->resumed = 1;
53898944Sobrien
53998944Sobrien      /* If we have a pending wait status for this thread, there is no
54098944Sobrien         point in resuming the process.  */
54198944Sobrien      if (lp->status)
54298944Sobrien	{
54398944Sobrien	  /* FIXME: What should we do if we are supposed to continue
544130803Smarcel	     this thread with a signal?  */
54598944Sobrien	  gdb_assert (signo == TARGET_SIGNAL_0);
54698944Sobrien	  return;
54798944Sobrien	}
54898944Sobrien
54998944Sobrien      /* Mark LWP as not stopped to prevent it from being continued by
550130803Smarcel         resume_callback.  */
55198944Sobrien      lp->stopped = 0;
55298944Sobrien    }
55398944Sobrien
55498944Sobrien  if (resume_all)
55598944Sobrien    iterate_over_lwps (resume_callback, NULL);
55698944Sobrien
55798944Sobrien  child_resume (ptid, step, signo);
558130803Smarcel  if (debug_lin_lwp)
559130803Smarcel    fprintf_unfiltered (gdb_stdlog,
560130803Smarcel			"LLR: %s %s, %s (resume event thread)\n",
561130803Smarcel			step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
562130803Smarcel			target_pid_to_str (ptid),
563130803Smarcel			signo ? strsignal (signo) : "0");
56498944Sobrien}
56598944Sobrien
56698944Sobrien
567130803Smarcel/* Issue kill to specified lwp.  */
568130803Smarcel
569130803Smarcelstatic int tkill_failed;
570130803Smarcel
571130803Smarcelstatic int
572130803Smarcelkill_lwp (int lwpid, int signo)
573130803Smarcel{
574130803Smarcel  errno = 0;
575130803Smarcel
576130803Smarcel/* Use tkill, if possible, in case we are using nptl threads.  If tkill
577130803Smarcel   fails, then we are not using nptl threads and we should be using kill.  */
578130803Smarcel
579130803Smarcel#ifdef HAVE_TKILL_SYSCALL
580130803Smarcel  if (!tkill_failed)
581130803Smarcel    {
582130803Smarcel      int ret = syscall (__NR_tkill, lwpid, signo);
583130803Smarcel      if (errno != ENOSYS)
584130803Smarcel	return ret;
585130803Smarcel      errno = 0;
586130803Smarcel      tkill_failed = 1;
587130803Smarcel    }
588130803Smarcel#endif
589130803Smarcel
590130803Smarcel  return kill (lwpid, signo);
591130803Smarcel}
592130803Smarcel
593130803Smarcel/* Wait for LP to stop.  Returns the wait status, or 0 if the LWP has
594130803Smarcel   exited.  */
595130803Smarcel
596130803Smarcelstatic int
597130803Smarcelwait_lwp (struct lwp_info *lp)
598130803Smarcel{
599130803Smarcel  pid_t pid;
600130803Smarcel  int status;
601130803Smarcel  int thread_dead = 0;
602130803Smarcel
603130803Smarcel  gdb_assert (!lp->stopped);
604130803Smarcel  gdb_assert (lp->status == 0);
605130803Smarcel
606130803Smarcel  pid = waitpid (GET_LWP (lp->ptid), &status, 0);
607130803Smarcel  if (pid == -1 && errno == ECHILD)
608130803Smarcel    {
609130803Smarcel      pid = waitpid (GET_LWP (lp->ptid), &status, __WCLONE);
610130803Smarcel      if (pid == -1 && errno == ECHILD)
611130803Smarcel	{
612130803Smarcel	  /* The thread has previously exited.  We need to delete it now
613130803Smarcel	     because in the case of NPTL threads, there won't be an
614130803Smarcel	     exit event unless it is the main thread.  */
615130803Smarcel	  thread_dead = 1;
616130803Smarcel	  if (debug_lin_lwp)
617130803Smarcel	    fprintf_unfiltered (gdb_stdlog, "WL: %s vanished.\n",
618130803Smarcel				target_pid_to_str (lp->ptid));
619130803Smarcel	}
620130803Smarcel    }
621130803Smarcel
622130803Smarcel  if (!thread_dead)
623130803Smarcel    {
624130803Smarcel      gdb_assert (pid == GET_LWP (lp->ptid));
625130803Smarcel
626130803Smarcel      if (debug_lin_lwp)
627130803Smarcel	{
628130803Smarcel	  fprintf_unfiltered (gdb_stdlog,
629130803Smarcel			      "WL: waitpid %s received %s\n",
630130803Smarcel			      target_pid_to_str (lp->ptid),
631130803Smarcel			      status_to_str (status));
632130803Smarcel	}
633130803Smarcel    }
634130803Smarcel
635130803Smarcel  /* Check if the thread has exited.  */
636130803Smarcel  if (WIFEXITED (status) || WIFSIGNALED (status))
637130803Smarcel    {
638130803Smarcel      thread_dead = 1;
639130803Smarcel      if (debug_lin_lwp)
640130803Smarcel	fprintf_unfiltered (gdb_stdlog, "WL: %s exited.\n",
641130803Smarcel			    target_pid_to_str (lp->ptid));
642130803Smarcel    }
643130803Smarcel
644130803Smarcel  if (thread_dead)
645130803Smarcel    {
646130803Smarcel      if (in_thread_list (lp->ptid))
647130803Smarcel	{
648130803Smarcel	  /* Core GDB cannot deal with us deleting the current thread.  */
649130803Smarcel	  if (!ptid_equal (lp->ptid, inferior_ptid))
650130803Smarcel	    delete_thread (lp->ptid);
651130803Smarcel	  printf_unfiltered ("[%s exited]\n",
652130803Smarcel			     target_pid_to_str (lp->ptid));
653130803Smarcel	}
654130803Smarcel
655130803Smarcel      delete_lwp (lp->ptid);
656130803Smarcel      return 0;
657130803Smarcel    }
658130803Smarcel
659130803Smarcel  gdb_assert (WIFSTOPPED (status));
660130803Smarcel
661130803Smarcel  return status;
662130803Smarcel}
663130803Smarcel
66498944Sobrien/* Send a SIGSTOP to LP.  */
66598944Sobrien
66698944Sobrienstatic int
66798944Sobrienstop_callback (struct lwp_info *lp, void *data)
66898944Sobrien{
669130803Smarcel  if (!lp->stopped && !lp->signalled)
67098944Sobrien    {
67198944Sobrien      int ret;
67298944Sobrien
673130803Smarcel      if (debug_lin_lwp)
674130803Smarcel	{
675130803Smarcel	  fprintf_unfiltered (gdb_stdlog,
676130803Smarcel			      "SC:  kill %s **<SIGSTOP>**\n",
677130803Smarcel			      target_pid_to_str (lp->ptid));
678130803Smarcel	}
679130803Smarcel      errno = 0;
680130803Smarcel      ret = kill_lwp (GET_LWP (lp->ptid), SIGSTOP);
681130803Smarcel      if (debug_lin_lwp)
682130803Smarcel	{
683130803Smarcel	  fprintf_unfiltered (gdb_stdlog,
684130803Smarcel			      "SC:  lwp kill %d %s\n",
685130803Smarcel			      ret,
686130803Smarcel			      errno ? safe_strerror (errno) : "ERRNO-OK");
687130803Smarcel	}
68898944Sobrien
68998944Sobrien      lp->signalled = 1;
69098944Sobrien      gdb_assert (lp->status == 0);
69198944Sobrien    }
69298944Sobrien
69398944Sobrien  return 0;
69498944Sobrien}
69598944Sobrien
69698944Sobrien/* Wait until LP is stopped.  If DATA is non-null it is interpreted as
69798944Sobrien   a pointer to a set of signals to be flushed immediately.  */
69898944Sobrien
69998944Sobrienstatic int
70098944Sobrienstop_wait_callback (struct lwp_info *lp, void *data)
70198944Sobrien{
70298944Sobrien  sigset_t *flush_mask = data;
70398944Sobrien
704130803Smarcel  if (!lp->stopped)
70598944Sobrien    {
70698944Sobrien      int status;
70798944Sobrien
708130803Smarcel      status = wait_lwp (lp);
709130803Smarcel      if (status == 0)
71098944Sobrien	return 0;
71198944Sobrien
712130803Smarcel      /* Ignore any signals in FLUSH_MASK.  */
713130803Smarcel      if (flush_mask && sigismember (flush_mask, WSTOPSIG (status)))
71498944Sobrien	{
715130803Smarcel	  if (!lp->signalled)
71698944Sobrien	    {
717130803Smarcel	      lp->stopped = 1;
718130803Smarcel	      return 0;
71998944Sobrien	    }
720130803Smarcel
721130803Smarcel	  errno = 0;
722130803Smarcel	  ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
72398944Sobrien	  if (debug_lin_lwp)
724130803Smarcel	    fprintf_unfiltered (gdb_stdlog,
725130803Smarcel				"PTRACE_CONT %s, 0, 0 (%s)\n",
726130803Smarcel				target_pid_to_str (lp->ptid),
727130803Smarcel				errno ? safe_strerror (errno) : "OK");
72898944Sobrien
72998944Sobrien	  return stop_wait_callback (lp, flush_mask);
73098944Sobrien	}
73198944Sobrien
73298944Sobrien      if (WSTOPSIG (status) != SIGSTOP)
73398944Sobrien	{
73498944Sobrien	  if (WSTOPSIG (status) == SIGTRAP)
73598944Sobrien	    {
73698944Sobrien	      /* If a LWP other than the LWP that we're reporting an
737130803Smarcel	         event for has hit a GDB breakpoint (as opposed to
738130803Smarcel	         some random trap signal), then just arrange for it to
739130803Smarcel	         hit it again later.  We don't keep the SIGTRAP status
740130803Smarcel	         and don't forward the SIGTRAP signal to the LWP.  We
741130803Smarcel	         will handle the current event, eventually we will
742130803Smarcel	         resume all LWPs, and this one will get its breakpoint
743130803Smarcel	         trap again.
74498944Sobrien
745130803Smarcel	         If we do not do this, then we run the risk that the
746130803Smarcel	         user will delete or disable the breakpoint, but the
747130803Smarcel	         thread will have already tripped on it.  */
74898944Sobrien
74998944Sobrien	      /* Now resume this LWP and get the SIGSTOP event. */
750130803Smarcel	      errno = 0;
75198944Sobrien	      ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
75298944Sobrien	      if (debug_lin_lwp)
75398944Sobrien		{
754130803Smarcel		  fprintf_unfiltered (gdb_stdlog,
755130803Smarcel				      "PTRACE_CONT %s, 0, 0 (%s)\n",
756130803Smarcel				      target_pid_to_str (lp->ptid),
757130803Smarcel				      errno ? safe_strerror (errno) : "OK");
758130803Smarcel
759130803Smarcel		  fprintf_unfiltered (gdb_stdlog,
760130803Smarcel				      "SWC: Candidate SIGTRAP event in %s\n",
761130803Smarcel				      target_pid_to_str (lp->ptid));
76298944Sobrien		}
76398944Sobrien	      /* Hold the SIGTRAP for handling by lin_lwp_wait. */
76498944Sobrien	      stop_wait_callback (lp, data);
76598944Sobrien	      /* If there's another event, throw it back into the queue. */
76698944Sobrien	      if (lp->status)
767130803Smarcel		{
768130803Smarcel		  if (debug_lin_lwp)
769130803Smarcel		    {
770130803Smarcel		      fprintf_unfiltered (gdb_stdlog,
771130803Smarcel					  "SWC: kill %s, %s\n",
772130803Smarcel					  target_pid_to_str (lp->ptid),
773130803Smarcel					  status_to_str ((int) status));
774130803Smarcel		    }
775130803Smarcel		  kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (lp->status));
776130803Smarcel		}
77798944Sobrien	      /* Save the sigtrap event. */
77898944Sobrien	      lp->status = status;
77998944Sobrien	      return 0;
78098944Sobrien	    }
78198944Sobrien	  else
78298944Sobrien	    {
78398944Sobrien	      /* The thread was stopped with a signal other than
784130803Smarcel	         SIGSTOP, and didn't accidentally trip a breakpoint. */
78598944Sobrien
78698944Sobrien	      if (debug_lin_lwp)
78798944Sobrien		{
788130803Smarcel		  fprintf_unfiltered (gdb_stdlog,
789130803Smarcel				      "SWC: Pending event %s in %s\n",
790130803Smarcel				      status_to_str ((int) status),
791130803Smarcel				      target_pid_to_str (lp->ptid));
79298944Sobrien		}
79398944Sobrien	      /* Now resume this LWP and get the SIGSTOP event. */
794130803Smarcel	      errno = 0;
79598944Sobrien	      ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
796130803Smarcel	      if (debug_lin_lwp)
797130803Smarcel		fprintf_unfiltered (gdb_stdlog,
798130803Smarcel				    "SWC: PTRACE_CONT %s, 0, 0 (%s)\n",
799130803Smarcel				    target_pid_to_str (lp->ptid),
800130803Smarcel				    errno ? safe_strerror (errno) : "OK");
80198944Sobrien
80298944Sobrien	      /* Hold this event/waitstatus while we check to see if
803130803Smarcel	         there are any more (we still want to get that SIGSTOP). */
80498944Sobrien	      stop_wait_callback (lp, data);
80598944Sobrien	      /* If the lp->status field is still empty, use it to hold
806130803Smarcel	         this event.  If not, then this event must be returned
807130803Smarcel	         to the event queue of the LWP.  */
80898944Sobrien	      if (lp->status == 0)
80998944Sobrien		lp->status = status;
81098944Sobrien	      else
811130803Smarcel		{
812130803Smarcel		  if (debug_lin_lwp)
813130803Smarcel		    {
814130803Smarcel		      fprintf_unfiltered (gdb_stdlog,
815130803Smarcel					  "SWC: kill %s, %s\n",
816130803Smarcel					  target_pid_to_str (lp->ptid),
817130803Smarcel					  status_to_str ((int) status));
818130803Smarcel		    }
819130803Smarcel		  kill_lwp (GET_LWP (lp->ptid), WSTOPSIG (status));
820130803Smarcel		}
82198944Sobrien	      return 0;
82298944Sobrien	    }
82398944Sobrien	}
82498944Sobrien      else
82598944Sobrien	{
82698944Sobrien	  /* We caught the SIGSTOP that we intended to catch, so
827130803Smarcel	     there's no SIGSTOP pending.  */
82898944Sobrien	  lp->stopped = 1;
82998944Sobrien	  lp->signalled = 0;
83098944Sobrien	}
83198944Sobrien    }
83298944Sobrien
83398944Sobrien  return 0;
83498944Sobrien}
83598944Sobrien
836130803Smarcel/* Check whether PID has any pending signals in FLUSH_MASK.  If so set
837130803Smarcel   the appropriate bits in PENDING, and return 1 - otherwise return 0.  */
838130803Smarcel
839130803Smarcelstatic int
840130803Smarcellin_lwp_has_pending (int pid, sigset_t *pending, sigset_t *flush_mask)
841130803Smarcel{
842130803Smarcel  sigset_t blocked, ignored;
843130803Smarcel  int i;
844130803Smarcel
845130803Smarcel  linux_proc_pending_signals (pid, pending, &blocked, &ignored);
846130803Smarcel
847130803Smarcel  if (!flush_mask)
848130803Smarcel    return 0;
849130803Smarcel
850130803Smarcel  for (i = 1; i < NSIG; i++)
851130803Smarcel    if (sigismember (pending, i))
852130803Smarcel      if (!sigismember (flush_mask, i)
853130803Smarcel	  || sigismember (&blocked, i)
854130803Smarcel	  || sigismember (&ignored, i))
855130803Smarcel	sigdelset (pending, i);
856130803Smarcel
857130803Smarcel  if (sigisemptyset (pending))
858130803Smarcel    return 0;
859130803Smarcel
860130803Smarcel  return 1;
861130803Smarcel}
862130803Smarcel
863130803Smarcel/* DATA is interpreted as a mask of signals to flush.  If LP has
864130803Smarcel   signals pending, and they are all in the flush mask, then arrange
865130803Smarcel   to flush them.  LP should be stopped, as should all other threads
866130803Smarcel   it might share a signal queue with.  */
867130803Smarcel
868130803Smarcelstatic int
869130803Smarcelflush_callback (struct lwp_info *lp, void *data)
870130803Smarcel{
871130803Smarcel  sigset_t *flush_mask = data;
872130803Smarcel  sigset_t pending, intersection, blocked, ignored;
873130803Smarcel  int pid, status;
874130803Smarcel
875130803Smarcel  /* Normally, when an LWP exits, it is removed from the LWP list.  The
876130803Smarcel     last LWP isn't removed till later, however.  So if there is only
877130803Smarcel     one LWP on the list, make sure it's alive.  */
878130803Smarcel  if (lwp_list == lp && lp->next == NULL)
879130803Smarcel    if (!lin_lwp_thread_alive (lp->ptid))
880130803Smarcel      return 0;
881130803Smarcel
882130803Smarcel  /* Just because the LWP is stopped doesn't mean that new signals
883130803Smarcel     can't arrive from outside, so this function must be careful of
884130803Smarcel     race conditions.  However, because all threads are stopped, we
885130803Smarcel     can assume that the pending mask will not shrink unless we resume
886130803Smarcel     the LWP, and that it will then get another signal.  We can't
887130803Smarcel     control which one, however.  */
888130803Smarcel
889130803Smarcel  if (lp->status)
890130803Smarcel    {
891130803Smarcel      if (debug_lin_lwp)
892130803Smarcel	printf_unfiltered ("FC: LP has pending status %06x\n", lp->status);
893130803Smarcel      if (WIFSTOPPED (lp->status) && sigismember (flush_mask, WSTOPSIG (lp->status)))
894130803Smarcel	lp->status = 0;
895130803Smarcel    }
896130803Smarcel
897130803Smarcel  while (lin_lwp_has_pending (GET_LWP (lp->ptid), &pending, flush_mask))
898130803Smarcel    {
899130803Smarcel      int ret;
900130803Smarcel
901130803Smarcel      errno = 0;
902130803Smarcel      ret = ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
903130803Smarcel      if (debug_lin_lwp)
904130803Smarcel	fprintf_unfiltered (gdb_stderr,
905130803Smarcel			    "FC: Sent PTRACE_CONT, ret %d %d\n", ret, errno);
906130803Smarcel
907130803Smarcel      lp->stopped = 0;
908130803Smarcel      stop_wait_callback (lp, flush_mask);
909130803Smarcel      if (debug_lin_lwp)
910130803Smarcel	fprintf_unfiltered (gdb_stderr,
911130803Smarcel			    "FC: Wait finished; saved status is %d\n",
912130803Smarcel			    lp->status);
913130803Smarcel    }
914130803Smarcel
915130803Smarcel  return 0;
916130803Smarcel}
917130803Smarcel
91898944Sobrien/* Return non-zero if LP has a wait status pending.  */
91998944Sobrien
92098944Sobrienstatic int
92198944Sobrienstatus_callback (struct lwp_info *lp, void *data)
92298944Sobrien{
92398944Sobrien  /* Only report a pending wait status if we pretend that this has
92498944Sobrien     indeed been resumed.  */
92598944Sobrien  return (lp->status != 0 && lp->resumed);
92698944Sobrien}
92798944Sobrien
92898944Sobrien/* Return non-zero if LP isn't stopped.  */
92998944Sobrien
93098944Sobrienstatic int
93198944Sobrienrunning_callback (struct lwp_info *lp, void *data)
93298944Sobrien{
933130803Smarcel  return (lp->stopped == 0 || (lp->status != 0 && lp->resumed));
93498944Sobrien}
93598944Sobrien
93698944Sobrien/* Count the LWP's that have had events.  */
93798944Sobrien
93898944Sobrienstatic int
93998944Sobriencount_events_callback (struct lwp_info *lp, void *data)
94098944Sobrien{
94198944Sobrien  int *count = data;
94298944Sobrien
94398944Sobrien  gdb_assert (count != NULL);
94498944Sobrien
94598944Sobrien  /* Count only LWPs that have a SIGTRAP event pending.  */
94698944Sobrien  if (lp->status != 0
94798944Sobrien      && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
94898944Sobrien    (*count)++;
94998944Sobrien
95098944Sobrien  return 0;
95198944Sobrien}
95298944Sobrien
95398944Sobrien/* Select the LWP (if any) that is currently being single-stepped.  */
95498944Sobrien
95598944Sobrienstatic int
95698944Sobrienselect_singlestep_lwp_callback (struct lwp_info *lp, void *data)
95798944Sobrien{
95898944Sobrien  if (lp->step && lp->status != 0)
95998944Sobrien    return 1;
96098944Sobrien  else
96198944Sobrien    return 0;
96298944Sobrien}
96398944Sobrien
96498944Sobrien/* Select the Nth LWP that has had a SIGTRAP event.  */
96598944Sobrien
96698944Sobrienstatic int
96798944Sobrienselect_event_lwp_callback (struct lwp_info *lp, void *data)
96898944Sobrien{
96998944Sobrien  int *selector = data;
97098944Sobrien
97198944Sobrien  gdb_assert (selector != NULL);
97298944Sobrien
97398944Sobrien  /* Select only LWPs that have a SIGTRAP event pending. */
97498944Sobrien  if (lp->status != 0
97598944Sobrien      && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP)
97698944Sobrien    if ((*selector)-- == 0)
97798944Sobrien      return 1;
97898944Sobrien
97998944Sobrien  return 0;
98098944Sobrien}
98198944Sobrien
98298944Sobrienstatic int
98398944Sobriencancel_breakpoints_callback (struct lwp_info *lp, void *data)
98498944Sobrien{
98598944Sobrien  struct lwp_info *event_lp = data;
98698944Sobrien
98798944Sobrien  /* Leave the LWP that has been elected to receive a SIGTRAP alone.  */
98898944Sobrien  if (lp == event_lp)
98998944Sobrien    return 0;
99098944Sobrien
99198944Sobrien  /* If a LWP other than the LWP that we're reporting an event for has
99298944Sobrien     hit a GDB breakpoint (as opposed to some random trap signal),
99398944Sobrien     then just arrange for it to hit it again later.  We don't keep
99498944Sobrien     the SIGTRAP status and don't forward the SIGTRAP signal to the
99598944Sobrien     LWP.  We will handle the current event, eventually we will resume
99698944Sobrien     all LWPs, and this one will get its breakpoint trap again.
99798944Sobrien
99898944Sobrien     If we do not do this, then we run the risk that the user will
99998944Sobrien     delete or disable the breakpoint, but the LWP will have already
100098944Sobrien     tripped on it.  */
100198944Sobrien
100298944Sobrien  if (lp->status != 0
1003130803Smarcel      && WIFSTOPPED (lp->status) && WSTOPSIG (lp->status) == SIGTRAP
1004130803Smarcel      && breakpoint_inserted_here_p (read_pc_pid (lp->ptid) -
100598944Sobrien				     DECR_PC_AFTER_BREAK))
100698944Sobrien    {
100798944Sobrien      if (debug_lin_lwp)
100898944Sobrien	fprintf_unfiltered (gdb_stdlog,
1009130803Smarcel			    "CBC: Push back breakpoint for %s\n",
1010130803Smarcel			    target_pid_to_str (lp->ptid));
101198944Sobrien
101298944Sobrien      /* Back up the PC if necessary.  */
101398944Sobrien      if (DECR_PC_AFTER_BREAK)
101498944Sobrien	write_pc_pid (read_pc_pid (lp->ptid) - DECR_PC_AFTER_BREAK, lp->ptid);
101598944Sobrien
101698944Sobrien      /* Throw away the SIGTRAP.  */
101798944Sobrien      lp->status = 0;
101898944Sobrien    }
101998944Sobrien
102098944Sobrien  return 0;
102198944Sobrien}
102298944Sobrien
102398944Sobrien/* Select one LWP out of those that have events pending.  */
102498944Sobrien
102598944Sobrienstatic void
102698944Sobrienselect_event_lwp (struct lwp_info **orig_lp, int *status)
102798944Sobrien{
102898944Sobrien  int num_events = 0;
102998944Sobrien  int random_selector;
103098944Sobrien  struct lwp_info *event_lp;
103198944Sobrien
103298944Sobrien  /* Record the wait status for the origional LWP.  */
103398944Sobrien  (*orig_lp)->status = *status;
103498944Sobrien
103598944Sobrien  /* Give preference to any LWP that is being single-stepped.  */
103698944Sobrien  event_lp = iterate_over_lwps (select_singlestep_lwp_callback, NULL);
103798944Sobrien  if (event_lp != NULL)
103898944Sobrien    {
103998944Sobrien      if (debug_lin_lwp)
104098944Sobrien	fprintf_unfiltered (gdb_stdlog,
1041130803Smarcel			    "SEL: Select single-step %s\n",
1042130803Smarcel			    target_pid_to_str (event_lp->ptid));
104398944Sobrien    }
104498944Sobrien  else
104598944Sobrien    {
104698944Sobrien      /* No single-stepping LWP.  Select one at random, out of those
1047130803Smarcel         which have had SIGTRAP events.  */
104898944Sobrien
104998944Sobrien      /* First see how many SIGTRAP events we have.  */
105098944Sobrien      iterate_over_lwps (count_events_callback, &num_events);
105198944Sobrien
105298944Sobrien      /* Now randomly pick a LWP out of those that have had a SIGTRAP.  */
105398944Sobrien      random_selector = (int)
105498944Sobrien	((num_events * (double) rand ()) / (RAND_MAX + 1.0));
105598944Sobrien
105698944Sobrien      if (debug_lin_lwp && num_events > 1)
1057130803Smarcel	fprintf_unfiltered (gdb_stdlog,
1058130803Smarcel			    "SEL: Found %d SIGTRAP events, selecting #%d\n",
105998944Sobrien			    num_events, random_selector);
106098944Sobrien
106198944Sobrien      event_lp = iterate_over_lwps (select_event_lwp_callback,
106298944Sobrien				    &random_selector);
106398944Sobrien    }
106498944Sobrien
106598944Sobrien  if (event_lp != NULL)
106698944Sobrien    {
106798944Sobrien      /* Switch the event LWP.  */
106898944Sobrien      *orig_lp = event_lp;
1069130803Smarcel      *status = event_lp->status;
107098944Sobrien    }
107198944Sobrien
107298944Sobrien  /* Flush the wait status for the event LWP.  */
107398944Sobrien  (*orig_lp)->status = 0;
107498944Sobrien}
107598944Sobrien
107698944Sobrien/* Return non-zero if LP has been resumed.  */
107798944Sobrien
107898944Sobrienstatic int
107998944Sobrienresumed_callback (struct lwp_info *lp, void *data)
108098944Sobrien{
108198944Sobrien  return lp->resumed;
108298944Sobrien}
108398944Sobrien
108498944Sobrien#ifdef CHILD_WAIT
108598944Sobrien
108698944Sobrien/* We need to override child_wait to support attaching to cloned
108798944Sobrien   processes, since a normal wait (as done by the default version)
108898944Sobrien   ignores those processes.  */
108998944Sobrien
109098944Sobrien/* Wait for child PTID to do something.  Return id of the child,
109198944Sobrien   minus_one_ptid in case of error; store status into *OURSTATUS.  */
109298944Sobrien
109398944Sobrienptid_t
109498944Sobrienchild_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
109598944Sobrien{
109698944Sobrien  int save_errno;
109798944Sobrien  int status;
109898944Sobrien  pid_t pid;
109998944Sobrien
110098944Sobrien  do
110198944Sobrien    {
110298944Sobrien      set_sigint_trap ();	/* Causes SIGINT to be passed on to the
110398944Sobrien				   attached process.  */
110498944Sobrien      set_sigio_trap ();
110598944Sobrien
110698944Sobrien      pid = waitpid (GET_PID (ptid), &status, 0);
110798944Sobrien      if (pid == -1 && errno == ECHILD)
110898944Sobrien	/* Try again with __WCLONE to check cloned processes.  */
110998944Sobrien	pid = waitpid (GET_PID (ptid), &status, __WCLONE);
1110130803Smarcel
1111130803Smarcel      if (debug_lin_lwp)
1112130803Smarcel	{
1113130803Smarcel	  fprintf_unfiltered (gdb_stdlog,
1114130803Smarcel			      "CW:  waitpid %ld received %s\n",
1115130803Smarcel			      (long) pid, status_to_str (status));
1116130803Smarcel	}
1117130803Smarcel
111898944Sobrien      save_errno = errno;
111998944Sobrien
1120130803Smarcel      /* Make sure we don't report an event for the exit of the
1121130803Smarcel         original program, if we've detached from it.  */
1122130803Smarcel      if (pid != -1 && !WIFSTOPPED (status) && pid != GET_PID (inferior_ptid))
1123130803Smarcel	{
1124130803Smarcel	  pid = -1;
1125130803Smarcel	  save_errno = EINTR;
1126130803Smarcel	}
1127130803Smarcel
1128130803Smarcel      /* Check for stop events reported by a process we didn't already
1129130803Smarcel	 know about - in this case, anything other than inferior_ptid.
1130130803Smarcel
1131130803Smarcel	 If we're expecting to receive stopped processes after fork,
1132130803Smarcel	 vfork, and clone events, then we'll just add the new one to
1133130803Smarcel	 our list and go back to waiting for the event to be reported
1134130803Smarcel	 - the stopped process might be returned from waitpid before
1135130803Smarcel	 or after the event is.  If we want to handle debugging of
1136130803Smarcel	 CLONE_PTRACE processes we need to do more here, i.e. switch
1137130803Smarcel	 to multi-threaded mode.  */
1138130803Smarcel      if (pid != -1 && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP
1139130803Smarcel	  && pid != GET_PID (inferior_ptid))
1140130803Smarcel	{
1141130803Smarcel	  linux_record_stopped_pid (pid);
1142130803Smarcel	  pid = -1;
1143130803Smarcel	  save_errno = EINTR;
1144130803Smarcel	}
1145130803Smarcel
114698944Sobrien      clear_sigio_trap ();
114798944Sobrien      clear_sigint_trap ();
114898944Sobrien    }
1149130803Smarcel  while (pid == -1 && save_errno == EINTR);
115098944Sobrien
115198944Sobrien  if (pid == -1)
115298944Sobrien    {
1153130803Smarcel      warning ("Child process unexpectedly missing: %s",
1154130803Smarcel	       safe_strerror (errno));
115598944Sobrien
115698944Sobrien      /* Claim it exited with unknown signal.  */
115798944Sobrien      ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
115898944Sobrien      ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
115998944Sobrien      return minus_one_ptid;
116098944Sobrien    }
116198944Sobrien
1162130803Smarcel  /* Handle GNU/Linux's extended waitstatus for trace events.  */
1163130803Smarcel  if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0)
1164130803Smarcel    return linux_handle_extended_wait (pid, status, ourstatus);
1165130803Smarcel
116698944Sobrien  store_waitstatus (ourstatus, status);
116798944Sobrien  return pid_to_ptid (pid);
116898944Sobrien}
116998944Sobrien
117098944Sobrien#endif
117198944Sobrien
1172130803Smarcel/* Stop an active thread, verify it still exists, then resume it.  */
1173130803Smarcel
1174130803Smarcelstatic int
1175130803Smarcelstop_and_resume_callback (struct lwp_info *lp, void *data)
1176130803Smarcel{
1177130803Smarcel  struct lwp_info *ptr;
1178130803Smarcel
1179130803Smarcel  if (!lp->stopped && !lp->signalled)
1180130803Smarcel    {
1181130803Smarcel      stop_callback (lp, NULL);
1182130803Smarcel      stop_wait_callback (lp, NULL);
1183130803Smarcel      /* Resume if the lwp still exists.  */
1184130803Smarcel      for (ptr = lwp_list; ptr; ptr = ptr->next)
1185130803Smarcel	if (lp == ptr)
1186130803Smarcel	  {
1187130803Smarcel	    resume_callback (lp, NULL);
1188130803Smarcel	    resume_set_callback (lp, NULL);
1189130803Smarcel	  }
1190130803Smarcel    }
1191130803Smarcel  return 0;
1192130803Smarcel}
1193130803Smarcel
119498944Sobrienstatic ptid_t
119598944Sobrienlin_lwp_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
119698944Sobrien{
119798944Sobrien  struct lwp_info *lp = NULL;
119898944Sobrien  int options = 0;
119998944Sobrien  int status = 0;
120098944Sobrien  pid_t pid = PIDGET (ptid);
120198944Sobrien  sigset_t flush_mask;
120298944Sobrien
120398944Sobrien  sigemptyset (&flush_mask);
120498944Sobrien
120598944Sobrien  /* Make sure SIGCHLD is blocked.  */
1206130803Smarcel  if (!sigismember (&blocked_mask, SIGCHLD))
120798944Sobrien    {
120898944Sobrien      sigaddset (&blocked_mask, SIGCHLD);
120998944Sobrien      sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
121098944Sobrien    }
121198944Sobrien
1212130803Smarcelretry:
121398944Sobrien
121498944Sobrien  /* Make sure there is at least one LWP that has been resumed, at
121598944Sobrien     least if there are any LWPs at all.  */
121698944Sobrien  gdb_assert (num_lwps == 0 || iterate_over_lwps (resumed_callback, NULL));
121798944Sobrien
121898944Sobrien  /* First check if there is a LWP with a wait status pending.  */
121998944Sobrien  if (pid == -1)
122098944Sobrien    {
122198944Sobrien      /* Any LWP that's been resumed will do.  */
122298944Sobrien      lp = iterate_over_lwps (status_callback, NULL);
122398944Sobrien      if (lp)
122498944Sobrien	{
122598944Sobrien	  status = lp->status;
122698944Sobrien	  lp->status = 0;
122798944Sobrien
122898944Sobrien	  if (debug_lin_lwp && status)
122998944Sobrien	    fprintf_unfiltered (gdb_stdlog,
1230130803Smarcel				"LLW: Using pending wait status %s for %s.\n",
1231130803Smarcel				status_to_str (status),
1232130803Smarcel				target_pid_to_str (lp->ptid));
123398944Sobrien	}
123498944Sobrien
123598944Sobrien      /* But if we don't fine one, we'll have to wait, and check both
123698944Sobrien         cloned and uncloned processes.  We start with the cloned
123798944Sobrien         processes.  */
123898944Sobrien      options = __WCLONE | WNOHANG;
123998944Sobrien    }
124098944Sobrien  else if (is_lwp (ptid))
124198944Sobrien    {
124298944Sobrien      if (debug_lin_lwp)
1243130803Smarcel	fprintf_unfiltered (gdb_stdlog,
1244130803Smarcel			    "LLW: Waiting for specific LWP %s.\n",
1245130803Smarcel			    target_pid_to_str (ptid));
124698944Sobrien
124798944Sobrien      /* We have a specific LWP to check.  */
124898944Sobrien      lp = find_lwp_pid (ptid);
124998944Sobrien      gdb_assert (lp);
125098944Sobrien      status = lp->status;
125198944Sobrien      lp->status = 0;
125298944Sobrien
125398944Sobrien      if (debug_lin_lwp && status)
125498944Sobrien	fprintf_unfiltered (gdb_stdlog,
1255130803Smarcel			    "LLW: Using pending wait status %s for %s.\n",
1256130803Smarcel			    status_to_str (status),
1257130803Smarcel			    target_pid_to_str (lp->ptid));
125898944Sobrien
125998944Sobrien      /* If we have to wait, take into account whether PID is a cloned
126098944Sobrien         process or not.  And we have to convert it to something that
126198944Sobrien         the layer beneath us can understand.  */
126298944Sobrien      options = lp->cloned ? __WCLONE : 0;
126398944Sobrien      pid = GET_LWP (ptid);
126498944Sobrien    }
126598944Sobrien
126698944Sobrien  if (status && lp->signalled)
126798944Sobrien    {
126898944Sobrien      /* A pending SIGSTOP may interfere with the normal stream of
1269130803Smarcel         events.  In a typical case where interference is a problem,
1270130803Smarcel         we have a SIGSTOP signal pending for LWP A while
1271130803Smarcel         single-stepping it, encounter an event in LWP B, and take the
1272130803Smarcel         pending SIGSTOP while trying to stop LWP A.  After processing
1273130803Smarcel         the event in LWP B, LWP A is continued, and we'll never see
1274130803Smarcel         the SIGTRAP associated with the last time we were
1275130803Smarcel         single-stepping LWP A.  */
127698944Sobrien
127798944Sobrien      /* Resume the thread.  It should halt immediately returning the
1278130803Smarcel         pending SIGSTOP.  */
1279130803Smarcel      registers_changed ();
128098944Sobrien      child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step,
1281130803Smarcel		    TARGET_SIGNAL_0);
1282130803Smarcel      if (debug_lin_lwp)
1283130803Smarcel	fprintf_unfiltered (gdb_stdlog,
1284130803Smarcel			    "LLW: %s %s, 0, 0 (expect SIGSTOP)\n",
1285130803Smarcel			    lp->step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1286130803Smarcel			    target_pid_to_str (lp->ptid));
128798944Sobrien      lp->stopped = 0;
128898944Sobrien      gdb_assert (lp->resumed);
128998944Sobrien
129098944Sobrien      /* This should catch the pending SIGSTOP.  */
129198944Sobrien      stop_wait_callback (lp, NULL);
129298944Sobrien    }
129398944Sobrien
1294130803Smarcel  set_sigint_trap ();		/* Causes SIGINT to be passed on to the
1295130803Smarcel				   attached process. */
129698944Sobrien  set_sigio_trap ();
129798944Sobrien
129898944Sobrien  while (status == 0)
129998944Sobrien    {
130098944Sobrien      pid_t lwpid;
130198944Sobrien
130298944Sobrien      lwpid = waitpid (pid, &status, options);
130398944Sobrien      if (lwpid > 0)
130498944Sobrien	{
130598944Sobrien	  gdb_assert (pid == -1 || lwpid == pid);
130698944Sobrien
1307130803Smarcel	  if (debug_lin_lwp)
1308130803Smarcel	    {
1309130803Smarcel	      fprintf_unfiltered (gdb_stdlog,
1310130803Smarcel				  "LLW: waitpid %ld received %s\n",
1311130803Smarcel				  (long) lwpid, status_to_str (status));
1312130803Smarcel	    }
1313130803Smarcel
131498944Sobrien	  lp = find_lwp_pid (pid_to_ptid (lwpid));
1315130803Smarcel
1316130803Smarcel	  /* Check for stop events reported by a process we didn't
1317130803Smarcel	     already know about - anything not already in our LWP
1318130803Smarcel	     list.
1319130803Smarcel
1320130803Smarcel	     If we're expecting to receive stopped processes after
1321130803Smarcel	     fork, vfork, and clone events, then we'll just add the
1322130803Smarcel	     new one to our list and go back to waiting for the event
1323130803Smarcel	     to be reported - the stopped process might be returned
1324130803Smarcel	     from waitpid before or after the event is.  */
1325130803Smarcel	  if (WIFSTOPPED (status) && !lp)
132698944Sobrien	    {
1327130803Smarcel	      linux_record_stopped_pid (lwpid);
1328130803Smarcel	      status = 0;
1329130803Smarcel	      continue;
1330130803Smarcel	    }
1331130803Smarcel
1332130803Smarcel	  /* Make sure we don't report an event for the exit of an LWP not in
1333130803Smarcel	     our list, i.e.  not part of the current process.  This can happen
1334130803Smarcel	     if we detach from a program we original forked and then it
1335130803Smarcel	     exits.  */
1336130803Smarcel	  if (!WIFSTOPPED (status) && !lp)
1337130803Smarcel	    {
1338130803Smarcel	      status = 0;
1339130803Smarcel	      continue;
1340130803Smarcel	    }
1341130803Smarcel
1342130803Smarcel	  /* NOTE drow/2003-06-17: This code seems to be meant for debugging
1343130803Smarcel	     CLONE_PTRACE processes which do not use the thread library -
1344130803Smarcel	     otherwise we wouldn't find the new LWP this way.  That doesn't
1345130803Smarcel	     currently work, and the following code is currently unreachable
1346130803Smarcel	     due to the two blocks above.  If it's fixed some day, this code
1347130803Smarcel	     should be broken out into a function so that we can also pick up
1348130803Smarcel	     LWPs from the new interface.  */
1349130803Smarcel	  if (!lp)
1350130803Smarcel	    {
135198944Sobrien	      lp = add_lwp (BUILD_LWP (lwpid, GET_PID (inferior_ptid)));
135298944Sobrien	      if (options & __WCLONE)
135398944Sobrien		lp->cloned = 1;
135498944Sobrien
135598944Sobrien	      if (threaded)
135698944Sobrien		{
135798944Sobrien		  gdb_assert (WIFSTOPPED (status)
135898944Sobrien			      && WSTOPSIG (status) == SIGSTOP);
135998944Sobrien		  lp->signalled = 1;
136098944Sobrien
1361130803Smarcel		  if (!in_thread_list (inferior_ptid))
136298944Sobrien		    {
136398944Sobrien		      inferior_ptid = BUILD_LWP (GET_PID (inferior_ptid),
1364130803Smarcel						 GET_PID (inferior_ptid));
136598944Sobrien		      add_thread (inferior_ptid);
136698944Sobrien		    }
136798944Sobrien
136898944Sobrien		  add_thread (lp->ptid);
136998944Sobrien		  printf_unfiltered ("[New %s]\n",
137098944Sobrien				     target_pid_to_str (lp->ptid));
137198944Sobrien		}
137298944Sobrien	    }
137398944Sobrien
1374130803Smarcel	  /* Check if the thread has exited.  */
137598944Sobrien	  if ((WIFEXITED (status) || WIFSIGNALED (status)) && num_lwps > 1)
137698944Sobrien	    {
137798944Sobrien	      if (in_thread_list (lp->ptid))
137898944Sobrien		{
137998944Sobrien		  /* Core GDB cannot deal with us deleting the current
1380130803Smarcel		     thread.  */
1381130803Smarcel		  if (!ptid_equal (lp->ptid, inferior_ptid))
138298944Sobrien		    delete_thread (lp->ptid);
138398944Sobrien		  printf_unfiltered ("[%s exited]\n",
138498944Sobrien				     target_pid_to_str (lp->ptid));
138598944Sobrien		}
1386130803Smarcel
1387130803Smarcel	      /* If this is the main thread, we must stop all threads and
1388130803Smarcel	         verify if they are still alive.  This is because in the nptl
1389130803Smarcel	         thread model, there is no signal issued for exiting LWPs
1390130803Smarcel	         other than the main thread.  We only get the main thread
1391130803Smarcel	         exit signal once all child threads have already exited.
1392130803Smarcel	         If we stop all the threads and use the stop_wait_callback
1393130803Smarcel	         to check if they have exited we can determine whether this
1394130803Smarcel	         signal should be ignored or whether it means the end of the
1395130803Smarcel	         debugged application, regardless of which threading model
1396130803Smarcel	         is being used.  */
1397130803Smarcel	      if (GET_PID (lp->ptid) == GET_LWP (lp->ptid))
1398130803Smarcel		{
1399130803Smarcel		  lp->stopped = 1;
1400130803Smarcel		  iterate_over_lwps (stop_and_resume_callback, NULL);
1401130803Smarcel		}
1402130803Smarcel
140398944Sobrien	      if (debug_lin_lwp)
1404130803Smarcel		fprintf_unfiltered (gdb_stdlog,
1405130803Smarcel				    "LLW: %s exited.\n",
140698944Sobrien				    target_pid_to_str (lp->ptid));
140798944Sobrien
140898944Sobrien	      delete_lwp (lp->ptid);
140998944Sobrien
1410130803Smarcel	      /* If there is at least one more LWP, then the exit signal
1411130803Smarcel	         was not the end of the debugged application and should be
1412130803Smarcel	         ignored.  */
1413130803Smarcel	      if (num_lwps > 0)
1414130803Smarcel		{
1415130803Smarcel		  /* Make sure there is at least one thread running.  */
1416130803Smarcel		  gdb_assert (iterate_over_lwps (running_callback, NULL));
1417130803Smarcel
1418130803Smarcel		  /* Discard the event.  */
1419130803Smarcel		  status = 0;
1420130803Smarcel		  continue;
1421130803Smarcel		}
1422130803Smarcel	    }
1423130803Smarcel
1424130803Smarcel	  /* Check if the current LWP has previously exited.  In the nptl
1425130803Smarcel	     thread model, LWPs other than the main thread do not issue
1426130803Smarcel	     signals when they exit so we must check whenever the thread
1427130803Smarcel	     has stopped.  A similar check is made in stop_wait_callback().  */
1428130803Smarcel	  if (num_lwps > 1 && !lin_lwp_thread_alive (lp->ptid))
1429130803Smarcel	    {
1430130803Smarcel	      if (in_thread_list (lp->ptid))
1431130803Smarcel		{
1432130803Smarcel		  /* Core GDB cannot deal with us deleting the current
1433130803Smarcel		     thread.  */
1434130803Smarcel		  if (!ptid_equal (lp->ptid, inferior_ptid))
1435130803Smarcel		    delete_thread (lp->ptid);
1436130803Smarcel		  printf_unfiltered ("[%s exited]\n",
1437130803Smarcel				     target_pid_to_str (lp->ptid));
1438130803Smarcel		}
1439130803Smarcel	      if (debug_lin_lwp)
1440130803Smarcel		fprintf_unfiltered (gdb_stdlog,
1441130803Smarcel				    "LLW: %s exited.\n",
1442130803Smarcel				    target_pid_to_str (lp->ptid));
1443130803Smarcel
1444130803Smarcel	      delete_lwp (lp->ptid);
1445130803Smarcel
144698944Sobrien	      /* Make sure there is at least one thread running.  */
144798944Sobrien	      gdb_assert (iterate_over_lwps (running_callback, NULL));
144898944Sobrien
144998944Sobrien	      /* Discard the event.  */
145098944Sobrien	      status = 0;
145198944Sobrien	      continue;
145298944Sobrien	    }
145398944Sobrien
145498944Sobrien	  /* Make sure we don't report a SIGSTOP that we sent
1455130803Smarcel	     ourselves in an attempt to stop an LWP.  */
1456130803Smarcel	  if (lp->signalled
1457130803Smarcel	      && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP)
145898944Sobrien	    {
145998944Sobrien	      if (debug_lin_lwp)
1460130803Smarcel		fprintf_unfiltered (gdb_stdlog,
1461130803Smarcel				    "LLW: Delayed SIGSTOP caught for %s.\n",
146298944Sobrien				    target_pid_to_str (lp->ptid));
146398944Sobrien
146498944Sobrien	      /* This is a delayed SIGSTOP.  */
146598944Sobrien	      lp->signalled = 0;
146698944Sobrien
1467130803Smarcel	      registers_changed ();
146898944Sobrien	      child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step,
1469130803Smarcel			    TARGET_SIGNAL_0);
1470130803Smarcel	      if (debug_lin_lwp)
1471130803Smarcel		fprintf_unfiltered (gdb_stdlog,
1472130803Smarcel				    "LLW: %s %s, 0, 0 (discard SIGSTOP)\n",
1473130803Smarcel				    lp->step ?
1474130803Smarcel				    "PTRACE_SINGLESTEP" : "PTRACE_CONT",
1475130803Smarcel				    target_pid_to_str (lp->ptid));
1476130803Smarcel
147798944Sobrien	      lp->stopped = 0;
147898944Sobrien	      gdb_assert (lp->resumed);
147998944Sobrien
148098944Sobrien	      /* Discard the event.  */
148198944Sobrien	      status = 0;
148298944Sobrien	      continue;
148398944Sobrien	    }
148498944Sobrien
148598944Sobrien	  break;
148698944Sobrien	}
148798944Sobrien
148898944Sobrien      if (pid == -1)
148998944Sobrien	{
149098944Sobrien	  /* Alternate between checking cloned and uncloned processes.  */
149198944Sobrien	  options ^= __WCLONE;
149298944Sobrien
149398944Sobrien	  /* And suspend every time we have checked both.  */
149498944Sobrien	  if (options & __WCLONE)
149598944Sobrien	    sigsuspend (&suspend_mask);
149698944Sobrien	}
149798944Sobrien
149898944Sobrien      /* We shouldn't end up here unless we want to try again.  */
149998944Sobrien      gdb_assert (status == 0);
150098944Sobrien    }
150198944Sobrien
150298944Sobrien  clear_sigio_trap ();
150398944Sobrien  clear_sigint_trap ();
150498944Sobrien
150598944Sobrien  gdb_assert (lp);
150698944Sobrien
150798944Sobrien  /* Don't report signals that GDB isn't interested in, such as
150898944Sobrien     signals that are neither printed nor stopped upon.  Stopping all
150998944Sobrien     threads can be a bit time-consuming so if we want decent
151098944Sobrien     performance with heavily multi-threaded programs, especially when
151198944Sobrien     they're using a high frequency timer, we'd better avoid it if we
151298944Sobrien     can.  */
151398944Sobrien
151498944Sobrien  if (WIFSTOPPED (status))
151598944Sobrien    {
151698944Sobrien      int signo = target_signal_from_host (WSTOPSIG (status));
151798944Sobrien
151898944Sobrien      if (signal_stop_state (signo) == 0
151998944Sobrien	  && signal_print_state (signo) == 0
152098944Sobrien	  && signal_pass_state (signo) == 1)
152198944Sobrien	{
152298944Sobrien	  /* FIMXE: kettenis/2001-06-06: Should we resume all threads
1523130803Smarcel	     here?  It is not clear we should.  GDB may not expect
1524130803Smarcel	     other threads to run.  On the other hand, not resuming
1525130803Smarcel	     newly attached threads may cause an unwanted delay in
1526130803Smarcel	     getting them running.  */
1527130803Smarcel	  registers_changed ();
152898944Sobrien	  child_resume (pid_to_ptid (GET_LWP (lp->ptid)), lp->step, signo);
1529130803Smarcel	  if (debug_lin_lwp)
1530130803Smarcel	    fprintf_unfiltered (gdb_stdlog,
1531130803Smarcel				"LLW: %s %s, %s (preempt 'handle')\n",
1532130803Smarcel				lp->step ?
1533130803Smarcel				"PTRACE_SINGLESTEP" : "PTRACE_CONT",
1534130803Smarcel				target_pid_to_str (lp->ptid),
1535130803Smarcel				signo ? strsignal (signo) : "0");
153698944Sobrien	  lp->stopped = 0;
153798944Sobrien	  status = 0;
153898944Sobrien	  goto retry;
153998944Sobrien	}
154098944Sobrien
1541130803Smarcel      if (signo == TARGET_SIGNAL_INT && signal_pass_state (signo) == 0)
154298944Sobrien	{
154398944Sobrien	  /* If ^C/BREAK is typed at the tty/console, SIGINT gets
1544130803Smarcel	     forwarded to the entire process group, that is, all LWP's
1545130803Smarcel	     will receive it.  Since we only want to report it once,
1546130803Smarcel	     we try to flush it from all LWPs except this one.  */
154798944Sobrien	  sigaddset (&flush_mask, SIGINT);
154898944Sobrien	}
154998944Sobrien    }
155098944Sobrien
155198944Sobrien  /* This LWP is stopped now.  */
155298944Sobrien  lp->stopped = 1;
155398944Sobrien
155498944Sobrien  if (debug_lin_lwp)
1555130803Smarcel    fprintf_unfiltered (gdb_stdlog, "LLW: Candidate event %s in %s.\n",
1556130803Smarcel			status_to_str (status), target_pid_to_str (lp->ptid));
155798944Sobrien
155898944Sobrien  /* Now stop all other LWP's ...  */
155998944Sobrien  iterate_over_lwps (stop_callback, NULL);
156098944Sobrien
156198944Sobrien  /* ... and wait until all of them have reported back that they're no
156298944Sobrien     longer running.  */
156398944Sobrien  iterate_over_lwps (stop_wait_callback, &flush_mask);
1564130803Smarcel  iterate_over_lwps (flush_callback, &flush_mask);
156598944Sobrien
156698944Sobrien  /* If we're not waiting for a specific LWP, choose an event LWP from
156798944Sobrien     among those that have had events.  Giving equal priority to all
156898944Sobrien     LWPs that have had events helps prevent starvation.  */
156998944Sobrien  if (pid == -1)
157098944Sobrien    select_event_lwp (&lp, &status);
157198944Sobrien
157298944Sobrien  /* Now that we've selected our final event LWP, cancel any
157398944Sobrien     breakpoints in other LWPs that have hit a GDB breakpoint.  See
157498944Sobrien     the comment in cancel_breakpoints_callback to find out why.  */
157598944Sobrien  iterate_over_lwps (cancel_breakpoints_callback, lp);
157698944Sobrien
157798944Sobrien  /* If we're not running in "threaded" mode, we'll report the bare
157898944Sobrien     process id.  */
157998944Sobrien
158098944Sobrien  if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP)
158198944Sobrien    {
158298944Sobrien      trap_ptid = (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid)));
158398944Sobrien      if (debug_lin_lwp)
1584130803Smarcel	fprintf_unfiltered (gdb_stdlog,
1585130803Smarcel			    "LLW: trap_ptid is %s.\n",
1586130803Smarcel			    target_pid_to_str (trap_ptid));
158798944Sobrien    }
158898944Sobrien  else
158998944Sobrien    trap_ptid = null_ptid;
159098944Sobrien
1591130803Smarcel  /* Handle GNU/Linux's extended waitstatus for trace events.  */
1592130803Smarcel  if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP && status >> 16 != 0)
1593130803Smarcel    {
1594130803Smarcel      linux_handle_extended_wait (ptid_get_pid (trap_ptid),
1595130803Smarcel				  status, ourstatus);
1596130803Smarcel      return trap_ptid;
1597130803Smarcel    }
1598130803Smarcel
159998944Sobrien  store_waitstatus (ourstatus, status);
160098944Sobrien  return (threaded ? lp->ptid : pid_to_ptid (GET_LWP (lp->ptid)));
160198944Sobrien}
160298944Sobrien
160398944Sobrienstatic int
160498944Sobrienkill_callback (struct lwp_info *lp, void *data)
160598944Sobrien{
1606130803Smarcel  errno = 0;
160798944Sobrien  ptrace (PTRACE_KILL, GET_LWP (lp->ptid), 0, 0);
1608130803Smarcel  if (debug_lin_lwp)
1609130803Smarcel    fprintf_unfiltered (gdb_stdlog,
1610130803Smarcel			"KC:  PTRACE_KILL %s, 0, 0 (%s)\n",
1611130803Smarcel			target_pid_to_str (lp->ptid),
1612130803Smarcel			errno ? safe_strerror (errno) : "OK");
1613130803Smarcel
161498944Sobrien  return 0;
161598944Sobrien}
161698944Sobrien
161798944Sobrienstatic int
161898944Sobrienkill_wait_callback (struct lwp_info *lp, void *data)
161998944Sobrien{
162098944Sobrien  pid_t pid;
162198944Sobrien
162298944Sobrien  /* We must make sure that there are no pending events (delayed
162398944Sobrien     SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
162498944Sobrien     program doesn't interfere with any following debugging session.  */
162598944Sobrien
162698944Sobrien  /* For cloned processes we must check both with __WCLONE and
162798944Sobrien     without, since the exit status of a cloned process isn't reported
162898944Sobrien     with __WCLONE.  */
162998944Sobrien  if (lp->cloned)
163098944Sobrien    {
163198944Sobrien      do
163298944Sobrien	{
163398944Sobrien	  pid = waitpid (GET_LWP (lp->ptid), NULL, __WCLONE);
1634130803Smarcel	  if (pid != (pid_t) -1 && debug_lin_lwp)
1635130803Smarcel	    {
1636130803Smarcel	      fprintf_unfiltered (gdb_stdlog,
1637130803Smarcel				  "KWC: wait %s received unknown.\n",
1638130803Smarcel				  target_pid_to_str (lp->ptid));
1639130803Smarcel	    }
164098944Sobrien	}
164198944Sobrien      while (pid == GET_LWP (lp->ptid));
164298944Sobrien
164398944Sobrien      gdb_assert (pid == -1 && errno == ECHILD);
164498944Sobrien    }
164598944Sobrien
164698944Sobrien  do
164798944Sobrien    {
164898944Sobrien      pid = waitpid (GET_LWP (lp->ptid), NULL, 0);
1649130803Smarcel      if (pid != (pid_t) -1 && debug_lin_lwp)
1650130803Smarcel	{
1651130803Smarcel	  fprintf_unfiltered (gdb_stdlog,
1652130803Smarcel			      "KWC: wait %s received unk.\n",
1653130803Smarcel			      target_pid_to_str (lp->ptid));
1654130803Smarcel	}
165598944Sobrien    }
165698944Sobrien  while (pid == GET_LWP (lp->ptid));
165798944Sobrien
165898944Sobrien  gdb_assert (pid == -1 && errno == ECHILD);
165998944Sobrien  return 0;
166098944Sobrien}
166198944Sobrien
166298944Sobrienstatic void
166398944Sobrienlin_lwp_kill (void)
166498944Sobrien{
166598944Sobrien  /* Kill all LWP's ...  */
166698944Sobrien  iterate_over_lwps (kill_callback, NULL);
166798944Sobrien
166898944Sobrien  /* ... and wait until we've flushed all events.  */
166998944Sobrien  iterate_over_lwps (kill_wait_callback, NULL);
167098944Sobrien
167198944Sobrien  target_mourn_inferior ();
167298944Sobrien}
167398944Sobrien
167498944Sobrienstatic void
167598944Sobrienlin_lwp_create_inferior (char *exec_file, char *allargs, char **env)
167698944Sobrien{
167798944Sobrien  child_ops.to_create_inferior (exec_file, allargs, env);
167898944Sobrien}
167998944Sobrien
1680130803Smarcelstatic void
168198944Sobrienlin_lwp_mourn_inferior (void)
168298944Sobrien{
168398944Sobrien  trap_ptid = null_ptid;
168498944Sobrien
168598944Sobrien  /* Destroy LWP info; it's no longer valid.  */
168698944Sobrien  init_lwp_list ();
168798944Sobrien
168898944Sobrien  /* Restore the original signal mask.  */
168998944Sobrien  sigprocmask (SIG_SETMASK, &normal_mask, NULL);
169098944Sobrien  sigemptyset (&blocked_mask);
169198944Sobrien
169298944Sobrien  child_ops.to_mourn_inferior ();
169398944Sobrien}
169498944Sobrien
169598944Sobrienstatic int
169698944Sobrienlin_lwp_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
1697130803Smarcel		     struct mem_attrib *attrib, struct target_ops *target)
169898944Sobrien{
169998944Sobrien  struct cleanup *old_chain = save_inferior_ptid ();
170098944Sobrien  int xfer;
170198944Sobrien
170298944Sobrien  if (is_lwp (inferior_ptid))
170398944Sobrien    inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
170498944Sobrien
1705130803Smarcel  xfer = linux_proc_xfer_memory (memaddr, myaddr, len, write, attrib, target);
1706130803Smarcel  if (xfer == 0)
1707130803Smarcel    xfer = child_xfer_memory (memaddr, myaddr, len, write, attrib, target);
170898944Sobrien
170998944Sobrien  do_cleanups (old_chain);
171098944Sobrien  return xfer;
171198944Sobrien}
171298944Sobrien
171398944Sobrienstatic int
171498944Sobrienlin_lwp_thread_alive (ptid_t ptid)
171598944Sobrien{
171698944Sobrien  gdb_assert (is_lwp (ptid));
171798944Sobrien
171898944Sobrien  errno = 0;
171998944Sobrien  ptrace (PTRACE_PEEKUSER, GET_LWP (ptid), 0, 0);
1720130803Smarcel  if (debug_lin_lwp)
1721130803Smarcel    fprintf_unfiltered (gdb_stdlog,
1722130803Smarcel			"LLTA: PTRACE_PEEKUSER %s, 0, 0 (%s)\n",
1723130803Smarcel			target_pid_to_str (ptid),
1724130803Smarcel			errno ? safe_strerror (errno) : "OK");
172598944Sobrien  if (errno)
172698944Sobrien    return 0;
172798944Sobrien
172898944Sobrien  return 1;
172998944Sobrien}
173098944Sobrien
173198944Sobrienstatic char *
173298944Sobrienlin_lwp_pid_to_str (ptid_t ptid)
173398944Sobrien{
173498944Sobrien  static char buf[64];
173598944Sobrien
173698944Sobrien  if (is_lwp (ptid))
173798944Sobrien    {
173898944Sobrien      snprintf (buf, sizeof (buf), "LWP %ld", GET_LWP (ptid));
173998944Sobrien      return buf;
174098944Sobrien    }
174198944Sobrien
174298944Sobrien  return normal_pid_to_str (ptid);
174398944Sobrien}
174498944Sobrien
174598944Sobrienstatic void
174698944Sobrieninit_lin_lwp_ops (void)
174798944Sobrien{
174898944Sobrien#if 0
174998944Sobrien  lin_lwp_ops.to_open = lin_lwp_open;
175098944Sobrien#endif
175198944Sobrien  lin_lwp_ops.to_shortname = "lwp-layer";
175298944Sobrien  lin_lwp_ops.to_longname = "lwp-layer";
175398944Sobrien  lin_lwp_ops.to_doc = "Low level threads support (LWP layer)";
175498944Sobrien  lin_lwp_ops.to_attach = lin_lwp_attach;
175598944Sobrien  lin_lwp_ops.to_detach = lin_lwp_detach;
175698944Sobrien  lin_lwp_ops.to_resume = lin_lwp_resume;
175798944Sobrien  lin_lwp_ops.to_wait = lin_lwp_wait;
1758130803Smarcel  /* fetch_inferior_registers and store_inferior_registers will
1759130803Smarcel     honor the LWP id, so we can use them directly.  */
1760130803Smarcel  lin_lwp_ops.to_fetch_registers = fetch_inferior_registers;
1761130803Smarcel  lin_lwp_ops.to_store_registers = store_inferior_registers;
176298944Sobrien  lin_lwp_ops.to_xfer_memory = lin_lwp_xfer_memory;
176398944Sobrien  lin_lwp_ops.to_kill = lin_lwp_kill;
176498944Sobrien  lin_lwp_ops.to_create_inferior = lin_lwp_create_inferior;
176598944Sobrien  lin_lwp_ops.to_mourn_inferior = lin_lwp_mourn_inferior;
176698944Sobrien  lin_lwp_ops.to_thread_alive = lin_lwp_thread_alive;
176798944Sobrien  lin_lwp_ops.to_pid_to_str = lin_lwp_pid_to_str;
1768130803Smarcel  lin_lwp_ops.to_post_startup_inferior = child_post_startup_inferior;
1769130803Smarcel  lin_lwp_ops.to_post_attach = child_post_attach;
1770130803Smarcel  lin_lwp_ops.to_insert_fork_catchpoint = child_insert_fork_catchpoint;
1771130803Smarcel  lin_lwp_ops.to_insert_vfork_catchpoint = child_insert_vfork_catchpoint;
1772130803Smarcel  lin_lwp_ops.to_insert_exec_catchpoint = child_insert_exec_catchpoint;
1773130803Smarcel
177498944Sobrien  lin_lwp_ops.to_stratum = thread_stratum;
177598944Sobrien  lin_lwp_ops.to_has_thread_control = tc_schedlock;
177698944Sobrien  lin_lwp_ops.to_magic = OPS_MAGIC;
177798944Sobrien}
177898944Sobrien
177998944Sobrienstatic void
178098944Sobriensigchld_handler (int signo)
178198944Sobrien{
178298944Sobrien  /* Do nothing.  The only reason for this handler is that it allows
178398944Sobrien     us to use sigsuspend in lin_lwp_wait above to wait for the
178498944Sobrien     arrival of a SIGCHLD.  */
178598944Sobrien}
178698944Sobrien
178798944Sobrienvoid
178898944Sobrien_initialize_lin_lwp (void)
178998944Sobrien{
179098944Sobrien  struct sigaction action;
179198944Sobrien
179298944Sobrien  extern void thread_db_init (struct target_ops *);
179398944Sobrien
179498944Sobrien  init_lin_lwp_ops ();
179598944Sobrien  add_target (&lin_lwp_ops);
179698944Sobrien  thread_db_init (&lin_lwp_ops);
179798944Sobrien
179898944Sobrien  /* Save the original signal mask.  */
179998944Sobrien  sigprocmask (SIG_SETMASK, NULL, &normal_mask);
180098944Sobrien
180198944Sobrien  action.sa_handler = sigchld_handler;
180298944Sobrien  sigemptyset (&action.sa_mask);
180398944Sobrien  action.sa_flags = 0;
180498944Sobrien  sigaction (SIGCHLD, &action, NULL);
180598944Sobrien
180698944Sobrien  /* Make sure we don't block SIGCHLD during a sigsuspend.  */
180798944Sobrien  sigprocmask (SIG_SETMASK, NULL, &suspend_mask);
180898944Sobrien  sigdelset (&suspend_mask, SIGCHLD);
180998944Sobrien
181098944Sobrien  sigemptyset (&blocked_mask);
181198944Sobrien
181298944Sobrien  add_show_from_set (add_set_cmd ("lin-lwp", no_class, var_zinteger,
1813130803Smarcel				  (char *) &debug_lin_lwp,
181498944Sobrien				  "Set debugging of GNU/Linux lwp module.\n\
1815130803SmarcelEnables printf debugging output.\n", &setdebuglist), &showdebuglist);
181698944Sobrien}
181798944Sobrien
181898944Sobrien
181998944Sobrien/* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
182098944Sobrien   the GNU/Linux Threads library and therefore doesn't really belong
182198944Sobrien   here.  */
182298944Sobrien
182398944Sobrien/* Read variable NAME in the target and return its value if found.
182498944Sobrien   Otherwise return zero.  It is assumed that the type of the variable
182598944Sobrien   is `int'.  */
182698944Sobrien
182798944Sobrienstatic int
182898944Sobrienget_signo (const char *name)
182998944Sobrien{
183098944Sobrien  struct minimal_symbol *ms;
183198944Sobrien  int signo;
183298944Sobrien
183398944Sobrien  ms = lookup_minimal_symbol (name, NULL, NULL);
183498944Sobrien  if (ms == NULL)
183598944Sobrien    return 0;
183698944Sobrien
183798944Sobrien  if (target_read_memory (SYMBOL_VALUE_ADDRESS (ms), (char *) &signo,
183898944Sobrien			  sizeof (signo)) != 0)
183998944Sobrien    return 0;
184098944Sobrien
184198944Sobrien  return signo;
184298944Sobrien}
184398944Sobrien
184498944Sobrien/* Return the set of signals used by the threads library in *SET.  */
184598944Sobrien
184698944Sobrienvoid
184798944Sobrienlin_thread_get_thread_signals (sigset_t *set)
184898944Sobrien{
184998944Sobrien  struct sigaction action;
185098944Sobrien  int restart, cancel;
185198944Sobrien
185298944Sobrien  sigemptyset (set);
185398944Sobrien
185498944Sobrien  restart = get_signo ("__pthread_sig_restart");
185598944Sobrien  if (restart == 0)
185698944Sobrien    return;
185798944Sobrien
185898944Sobrien  cancel = get_signo ("__pthread_sig_cancel");
185998944Sobrien  if (cancel == 0)
186098944Sobrien    return;
186198944Sobrien
186298944Sobrien  sigaddset (set, restart);
186398944Sobrien  sigaddset (set, cancel);
186498944Sobrien
186598944Sobrien  /* The GNU/Linux Threads library makes terminating threads send a
186698944Sobrien     special "cancel" signal instead of SIGCHLD.  Make sure we catch
186798944Sobrien     those (to prevent them from terminating GDB itself, which is
186898944Sobrien     likely to be their default action) and treat them the same way as
186998944Sobrien     SIGCHLD.  */
187098944Sobrien
187198944Sobrien  action.sa_handler = sigchld_handler;
187298944Sobrien  sigemptyset (&action.sa_mask);
187398944Sobrien  action.sa_flags = 0;
187498944Sobrien  sigaction (cancel, &action, NULL);
187598944Sobrien
187698944Sobrien  /* We block the "cancel" signal throughout this code ...  */
187798944Sobrien  sigaddset (&blocked_mask, cancel);
187898944Sobrien  sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
187998944Sobrien
188098944Sobrien  /* ... except during a sigsuspend.  */
188198944Sobrien  sigdelset (&suspend_mask, cancel);
188298944Sobrien}
1883